Implementation of Dijkstras Algorithm Dijkstras Algorithm Implementations With

  • Slides: 21
Download presentation
Implementation of Dijkstra’s Algorithm

Implementation of Dijkstra’s Algorithm

Dijkstra’s Algorithm

Dijkstra’s Algorithm

Implementations • With min-priority queue, Dijkstra algorithm can be implemented in time • With

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 in time

An Example 2 0 4 4 2 2 1 1 2 3 6 4

An Example 2 0 4 4 2 2 1 1 2 3 6 4 2 3 Initialize Select the node with the minimum temporary distance label. 3 5 1 0 2 ∞ 4 5 ∞ 3 ∞ ∞ 6 ∞

Update Step 2 2 0 4 4 2 2 1 1 2 3 6

Update Step 2 2 0 4 4 2 2 1 1 2 3 6 4 2 3 3 5 1 0 2 ∞ 4 5 ∞ 3 ∞ ∞ 6 ∞

Update Step 2 2 0 4 4 2 2 1 1 2 3 6

Update Step 2 2 0 4 4 2 2 1 1 2 3 6 4 2 3 3 5 1 0 2 2 Decrease-Key(S, 2, 2) 4 5 ∞ 3 ∞ ∞ 6 ∞

Update Step 2 2 0 4 4 2 2 1 1 2 3 6

Update Step 2 2 0 4 4 2 2 1 1 2 3 6 4 2 3 4 Decrease-Key(S, 2, 2) Decrease-Key(S, 3, 4) 3 5 1 0 2 2 4 5 ∞ 3 4 ∞ 6 ∞

Update Step 2 2 0 4 4 2 2 1 1 2 3 6

Update Step 2 2 0 4 4 2 2 1 1 2 3 6 4 2 3 4 3 5 6 ∞ 2 2 Extract-Min(S) 4 5 ∞ 3 4 ∞

Choose Minimum Temporary Label 2 2 0 4 4 2 2 1 1 2

Choose Minimum Temporary Label 2 2 0 4 4 2 2 1 1 2 3 6 4 2 3 4 3 5 2 2 6 ∞ 4 5 ∞ 3 4 ∞

Update Step 2 2 0 4 The predecessor of node 3 is now node

Update Step 2 2 0 4 The predecessor of node 3 is now node 2 6 4 2 2 1 1 2 3 6 4 2 3 4 3 Decrease-Key(S, 3, 3) Decrease-Key(S, 4, 6) Decrease-Key(S, 5, 4) 3 5 4 2 2 5 4 6 ∞ 4 3 3 6

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1 2 3 6 4 2 3 3 3 5 4 4 6 5 4 Extract-Min(S) 6 ∞ 3 3

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1 2 3 6 4 2 3 3 3 5 4 Extract-Min(S) 6 ∞ 4 6

Update 2 2 0 4 6 4 2 2 1 1 2 3 6

Update 2 2 0 4 6 4 2 2 1 1 2 3 6 4 2 3 3 3 5 4 d(5) is not changed. 6 ∞ 4 6

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1 2 3 4 6 2 3 3 Extract-Min(S) 3 5 4 6 ∞ 5 4 4 6

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1 2 3 4 6 2 3 3 Extract-Min(S) 3 5 4 6 ∞ 4 6

Update 2 2 0 4 6 4 2 2 1 1 d(4) is not

Update 2 2 0 4 6 4 2 2 1 1 d(4) is not changed 2 3 4 6 6 2 3 3 3 5 4 6 Decrease-Key(S, 6, 6) 6 4 6

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1 2 3 4 6 6 2 3 3 3 5 4 4 6 6 6

Update 2 2 0 4 6 4 2 2 1 1 2 3 4

Update 2 2 0 4 6 4 2 2 1 1 2 3 4 6 6 2 3 3 3 5 4 4 6 6 d(6) is not updated 6

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1

Choose Minimum Temporary Label 2 2 0 4 6 4 2 2 1 1 2 3 4 6 6 2 3 3 3 5 4 There is nothing to update 6 6

End of Algorithm 2 2 0 4 6 4 2 2 1 1 2

End of Algorithm 2 2 0 4 6 4 2 2 1 1 2 3 4 6 6 2 3 3 3 5 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

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