4.4 Shortest Paths in a Graph map from Microsoft Streets and Trips shortest path from Princeton CS department to Einstein's house
Shortest Path Problem Shortest path network. Directed graph G = (V, E). Source s, destination t. Length e = length of edge e. Shortest path problem: find shortest directed path from s to t. cost of path = sum of edge costs in path Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 48. 2 23 3 9 s Cost of path s-6-5-t = 14 + 30 + 16 = 60. 18 14 2 6 6 30 4 19 5 11 15 5 6 Shortest path need not have fewest # edges 20 16 t 7 44
Applications of Shortest Path Algorithms Driving directions (Mapquest, Google Maps) Routing protocols on the Internet (BGP, OSPF) Decision making and AI Robot motion planning Games Image and speech analysis problems Finding associations
Dijkstra's Algorithm Dijkstra's algorithm. Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) = 0, (v) = ∞, for each v≠s Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) = (v). shortest path to some u in explored part, followed by a single edge (u, v) e v d(u) u S s
Dijkstra's Algorithm Dijkstra's algorithm. Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) = 0 , (v) = ∞, for each v≠s Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) = (v). S is only needed for the proof of correctness, not the algorithm itself shortest path to some u in explored part, followed by a single edge (u, v) e v d(u) u S s
Dijkstra's Algorithm: Proof of Correctness Invariant. For each node u S, d(u) is the length of the shortest s-u path. Pf. (by induction on |S|) Base case: |S| = 1 is trivial. Inductive hypothesis: Assume true for |S| = k 1. Let v be next node added to S, and let u-v be the chosen edge. The shortest s-u path plus (u, v) is an s-v path of length (v). Consider any s-v path P. We'll see that it's no shorter than (v). Let x-y be the first edge in P that leaves S, and let P' be the subpath to x. P is already too long as soon as it leaves S. Consider any path P from s to v. It is already at least as long as (v) by the time it leaves S. P P' x y s S u v (P) (P') + (x,y) d(x) + (x, y) (y) (v) nonnegative weights inductive hypothesis defn of (y) Dijkstra chose v instead of y
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 } delmin 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 } delmin 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 delmin 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 delmin 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 } delmin 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 delmin 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 delmin 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 delmin 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
Dijkstra’s Implementation: Approach I In each iteration: For each v S, compute π(v) = min(u,v)E: uS{d(u)+l(u,v)} O(deg(v)) steps Choose v so that π(v) is minimum O(n) steps Add v to S and set d(v)= π(v) Total running time: O(∑v deg(v) + n) = O(m+n) per iteration O(nm) time overall
Dijkstra’s Implementation: Approach II Useful Property: If node u added to S in any iteration, π(v) can change only if (u,v) is an edge Modification: In any iteration if node u is added to S, update π(v) only for neighbors v of u O(deg(u)) time to update π values in each iteration O(∑u deg(u)) = O(m) time to update π values over all iterations What about the time to find the node with minimum π value in each iteration? Storing in a list O(n) time in each iteration to find the minimum Can we do better?
Priority Queues Insert(P, v): O(log n) steps FindMin(P): O(1) steps Delete(P,i): O(log n) steps ExtractMin(P): O(log n) steps ChangeKey(P,v,k): O(log n) steps
Dijkstra’s Algorithm using Priority Queues Store each node v with key π(v) in PQ Choose node v with minimum π(v) by ExtractMin operation For each neighbor w of chosen node v, ChangeKey(PQ,w, π(w)) if π(w) changes Total running time: one ExtractMin per node and at most one ChangeKey per edge = O(m log n) time
Dijkstra's Algorithm: Implementation For each unexplored node, explicitly maintain Next node to explore = node with minimum (v). When exploring v, for each incident edge e = (v, w), update Efficient implementation. Maintain a priority queue of unexplored nodes, prioritized by (v). Priority Queue PQ Operation Dijkstra Array Binary heap d-way Heap Fib heap † Insert n n log n d log d n 1 ExtractMin n n log n d log d n log n ChangeKey m 1 log n log d n 1 IsEmpty n 1 1 1 1 Total n2 m log n m log m/n n m + n log n † Individual ops are amortized bounds
Edsger W. Dijkstra The question of whether computers can think is like the question of whether submarines can swim. Do only what only you can do. In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind. The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums.