Algorithms – Fastest Way to Keep a List in Order

algorithmsjavascriptrouting

I implemented Dijkstra's path finding algorithm in JavaScript and a big part of it involves storing the distances to nodes and fetching the smallest. The distances change often and the smallest is fetched a lot.

I'm currently using an Ordered Linked List, with items being put in order when added.

Is there a faster way?

Edits:

Update:

Implemented arnaud's suggestion of a Fibonacci Heap. It was 32 to 85 times faster with diminishing returns the larger the dataset.

I deviated from the true Fibonacci Heap by not running consolidate every time a value was removed. I found running it once every 50 shifts performed best.

Source Code is here: https://github.com/nojacko/dijkstras-js/blob/bda68d1274629c30b1f902ffb7f95f1e3c695973/Dijkstras.js

Best Answer

If it is truly an ordered linked list, this should be a fairly bad choice because you have to traverse the list one by one until you find the right place to insert the item. In other words O(N). This is ok if the list is small but can get out of hand for big graphs.

Usually, what you will need for that kind of stuff is a heap:

http://en.wikipedia.org/wiki/Heap_%28data_structure%29

...where you can insert and pop in O(log(n)) ...it's also usually available out of the box in most programming languages.

Related Topic