Implementation of Dijkstra’s Algorithm
Dijkstra’s Algorithm
Implementations With min-priority queue, Dijkstra algorithm can be implemented in time With Fibonacci heap, Dijkstra algorithm can be implemented in time With Radix heap, Dijkstra algorithm can be implemented
An Example Initialize 4 2 4 2 2 1 2 3 1 1 6 4 2 3 3 5 1 Initialize 2 ∞ 3 ∞ Select the node with the minimum temporary distance label. 4 5 6 ∞ ∞ ∞
Update Step 2 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 1 2 ∞ 3 ∞ 4 5 6 ∞ ∞ ∞
Update Step 2 Decrease-Key(S,2,2) 4 2 4 2 2 1 2 3 1 6 4 2 3 3 1 2 3 1 6 4 2 3 3 5 1 2 2 3 ∞ Decrease-Key(S,2,2) 4 5 6 ∞ ∞ ∞
Update Step 2 4 Decrease-Key(S,2,2) Decrease-Key(S,3,4) 4 2 4 1 2 3 1 6 4 2 3 3 5 1 4 2 2 3 4 Decrease-Key(S,2,2) Decrease-Key(S,3,4) 4 5 6 ∞ ∞ ∞
Update Step 2 4 Extract-Min(S) 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 ∞ 6 1 2 3 1 6 4 2 3 3 5 4 ∞ 6 2 2 3 4 Extract-Min(S) 4 5 ∞ ∞
Choose Minimum Temporary Label 2 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 4 2 2 6 ∞ 3 4 4 5 ∞ ∞
Update Step 6 The predecessor of node 3 is now node 2 2 4 4 3 1 2 3 1 6 4 2 3 3 5 4 2 2 4 3 5 4 3 3 Decrease-Key(S,3,3) Decrease-Key(S,4,6) Decrease-Key(S,5,4) 6 4 ∞ 6
Choose Minimum Temporary Label 2 6 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 3 4 6 4 5 4 3 3 Extract-Min(S) 6 ∞
Choose Minimum Temporary Label 2 6 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 3 4 3 3 5 4 4 6 Extract-Min(S) 6 ∞
Update 2 6 3 4 d(5) is not changed. 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 3 1 2 3 1 6 4 2 3 3 5 3 4 3 3 5 4 4 6 d(5) is not changed. 6 ∞
Choose Minimum Temporary Label 2 6 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 3 4 6 ∞ 5 4 4 6 Extract-Min(S)
Choose Minimum Temporary Label 2 6 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 3 4 5 4 6 4 6 Extract-Min(S) ∞
Update d(4) is not changed 2 6 6 3 4 Decrease-Key(S,6,6) 4 2 4 2 2 1 6 1 2 3 1 6 4 2 3 3 5 3 4 5 4 6 4 6 Decrease-Key(S,6,6) 6
Choose Minimum Temporary Label 2 6 4 2 4 2 2 1 2 3 6 1 6 4 2 3 3 5 3 4 4 6 6 6
Update 2 6 6 3 4 d(6) is not updated 4 2 4 2 2 1 2 3 1 6 4 2 3 3 5 4 6 1 2 3 6 1 6 4 2 3 3 5 3 4 4 6 6 6 d(6) is not updated
Choose Minimum Temporary Label 2 6 4 2 4 2 2 1 2 3 6 1 6 4 2 3 3 5 3 4 6 6 There is nothing to update
End of Algorithm 2 6 6 3 4 All nodes are now permanent 1 2 3 6 1 6 4 2 3 3 5 3 4 All nodes are now permanent The predecessors form a tree The shortest path from node 1 to node 6 can be found by tracing back predecessors
Implementations Decrease-Key(S,x,key): Call at most m times. Extract-Min(S): Call n times. With min-priority queue, Dijkstra algorithm can be implemented in time