Dijkstra's Shortest Path Algorithm Find shortest path from s to t. 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44
Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t } 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label
Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t } Here 2 24 3 9 s 18 14 2 6 6 30 4 19 11 15 5 5 6 20 16 t 7 44 distance label
Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } decrease key X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X
Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } Here X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X
Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t } X 9 2 24 3 9 s 18 14 X 14 2 6 6 30 4 19 11 15 5 5 6 20 16 t 7 44 15 X
Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t } decrease key X 33 X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X
Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t } X 33 X 9 2 24 3 9 Here s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X
Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 30 X 4 19 11 15 5 5 6 20 16 t 7 44 15 X
Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 30 X 4 19 11 15 5 5 6 20 16 t 7 44 15 Here X
Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 4 30 19 11 15 5 5 6 20 16 t 7 44 59 15 X X
Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } Here 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 4 30 X 19 11 15 5 5 6 20 16 t 7 44 59 15 X X
Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 4 30 X 19 11 15 5 5 6 20 16 t 7 44 51 59 15 X X X
Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 30 X 4 19 11 15 5 5 6 20 Here 16 t 7 44 51 59 15 X X X
Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 Here 5 6 20 16 t 7 44 50 51 X 59 15 X X X
Dijkstra's Shortest Path Algorithm PQ = { t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
Dijkstra's Shortest Path Algorithm PQ = { t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 Here 50 51 X 59 15 X X X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
The Algoritm 1. Assign to every node a distance value. Set it to zero for our initial node and to infinity (-9999) for all other nodes. 2. Mark all nodes as unvisited. Set initial node as current. 3. For current node, consider all its unvisited neighbors and calculate their distance (from the initial node). For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance. 4. When we are done considering all neighbors of the current node, mark it as visited. A visited node will not be checked ever again; its distance recorded now is final and minimal. 5. If all nodes have been visited, finish. Otherwise, set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3.
Pseudo Code of the Dijkstra’s Alg Algorithm Dijkstra(Graph, source): for each vertex v in Graph: // Initializations dist[v] := infinity // Unknown distance function from source to v previous[v] := undefined // Previous node in optimal path from source dist[source] := 0 // Distance from source to source Q := the set of all nodes in Graph // All nodes in the graph are unoptimized - thus are in Q while Q is not empty: // The main loop u:= dequeue (Q) // u := vertex in Q with smallest dist[] if dist[u] = infinity: break // all remaining vertices are inaccessible from source dequeue(u,Q) // remove u from Q for each neighbor v of u: // where v has not yet been removed from Q. alt := dist[u] + dist_between(u, v) // Dist. from start point to neighbor if alt < dist[v]: // Relax (u,v) dist[v] := alt previous[v] := u return dist