I am trying to implement Dijkstra's algorithm for finding the shortest paths between nodes using Fibonacci Heap with Adjacency List representation for the graph. According to the algorithm I know, we have to find the minimum node in the Heap and then iterate through all its neighbours and update their distances. But to get the current distances of the neighbours(which is stored in each node in the heap), I have to find that particular node from the heap. 'Find' operation takes O(N) time where N is number of nodes in the Fibonacci Heap. So is my algorithm correct or am I missing something? Any help would be greatly appreciated.

- 362,284
- 104
- 897
- 1,065

- 41
- 5
-
I think you start out with "adjacency list"s, especially that of each minimum node, so you don't need to **find** these nodes using the heap. Updating their respective distances may invalidate their current position in the heap, which may be difficult to fix if the node offers no clue regarding this position. – greybeard Apr 09 '15 at 06:31
-
You can augment a heap with a hashtable which stores the current indices of each value in the heap; this can then be used to update an element's priority in O(log n) time. Alternatively, you can just re-insert the same element with its new priority, and then when polling from the heap you check if the element you just polled already has a confirmed shortest path, and skip it if it does. This leaves the algorithm correct, and is simpler to implement than a heap with a hashtable, but it affects the running time since the heap will tend to be larger. – kaya3 Oct 07 '22 at 21:17
-
Does this answer your question? [Dijkstra's algorithm (updating the heap)](https://stackoverflow.com/questions/16822821/dijkstras-algorithm-updating-the-heap) – kaya3 Oct 07 '22 at 21:20
1 Answers
An implicit assumption in a Fibonacci heap is that when you enqueue an element into the heap, you can, in time O(1), pull up the resulting node in the Fibonacci heap. There are two common ways you'll see this done.
First, you could use an invasive Fibonacci heap. In this approach, the actual node structures in the graph have extra fields stored in them corresponding to what the Fibonacci heap needs to keep things linked up the right order. If you set things up this way, then when you iterate over a node's neighbors, those neighbors already have the necessary fields built into them, which makes it easy to query the Fibonacci heap on those nodes without needing to do a search for them.
Second, you can have the enqueue operation on the Fibonacci heap return a pointer to the newly-created Fibonacci heap node, then somehow associate each node in the graph with that Fibonacci heap node (maybe store a pointer to it, or have a hash table, etc.). This is the approach I took when I coded up a version of Dijkstra's algorithm that uses Fibonacci heap many years back.

- 362,284
- 104
- 897
- 1,065