shortest path problems
play

Shortest-Path Problems ! A weighted graph G=(V,E) is a graph in which - PDF document

Shortest-Path Problems ! A weighted graph G=(V,E) is a graph in which each edge e E has a weight (cost) w(e) associated with it. Graph Algorithms ! Path cost: the cost of a path p= v 0 ,,v k is


  1. Shortest-Path Problems ! A weighted graph G=(V,E) is a graph in which each edge e ∈ E has a weight (cost) w(e) associated with it. Graph Algorithms לע םימת ירוגלא םיפ רג ! Path cost: the cost of a path p= 〈 v 0 ,…,v k 〉 is the sum of the weights of its edges: k ∑ ( ) ( ) = w p w v 1 , v − i i i = 1 ! Shortest path problems : – Single source – All pairs 1 2 Shortest-Path Problems Dijkstra’s Algorithm ! Maintain a set of vertices S whose ! Single-source shortest paths shortest path from the source has problem: given a directed graph already been discovered (initially S G=(V,E), in which each edge has non- contains only the source vertex). negative cost , and a source s ∈ V, ! At each iteration add to S a vertex compute the cheapest path from s to every other vertex in V. from V-S with the minimum shortest- path estimate ! Update shortest-path estimates for V-S ! All-pairs shortest paths problem: given a directed graph G=(V,E), in which each edge has non-negative 1 cost , compute for every ordered pair of vertices (u,v) the cheapest path from u 10 9 2 3 to v. 4 6 7 5 2 3 4

  2. Dijkstra’s Algorithm (A) Dijkstra’s Algorithm (B) Dijkstra(G=(V,E), w, s ∈ V) Dijkstra(G=(V,E), w, s ∈ V) Q = V; PriorityQueue Q = V; S = ∅ ; S = ∅ ; for each v ∈ Q for each v ∈ Q v.dist = ∞ , v.from = null; v.dist = ∞ , v.from = null; s.dist = 0, s.from = null; s.dist = 0, s.from = null; while (Q is not empty) { while (Q is not empty) { u = vertex with min dist in Q; u = Q.deleteMin(); Q = Q – {u}; S = S ∪ {u}; S = S ∪ {u}; for each v ∈ Q adjacent to u for each v ∈ Q adjacent to u if (u.dist + w(u,v) < v.dist) { if (u.dist + w(u,v) < v.dist) { v.from = u; v.from = u; v.dist = u.dist + w(u,v); v.dist = u.dist + w(u,v); Q.decreaseKey(v, v.dist); } } } } 5 6 Correctness Proof All-Pairs Shortest Paths Theorem : Upon termination of Dijkstra’s ! Can be solved by calling Dijkstra once ! algorithm, v.dist is the length of the shortest on each vertex as a source. path from s to v, for each v ∈ V. ! A more direct algorithm (Floyd- Definition : we shall call a path from s to v ! “special” if it is the shortest path from s to v Warshall). passing only through vertices in S. ! Assume vertices are {1,…,n} Lemma : At each stage of Dijkstra’s alg. The ! following two properties hold: ! Maintain a cost matrix containing in 1. For each v ∈ S, v.dist is the length of the each entry (i,j) the cost of a path from shortest path from s to v. vertex i to vertex j. 2. For each v ∈ V-S, v.dist is the length of the shortest special path from s to v. ! Initially, the matrix is defined as Proof : by induction on the size of S. follows:  = 0 if i j The theorem follows from property 1 when S  ! [ ]  , = ( , ) if ( , ) ∈ contains all of the vertices in the graph. C i j w i j i j E   ∞ otherwise 7 8

  3. Example Floyd-Warshall Alg. 8 Floyd-Warshall(G=(V,E), w) 2 1 2 3 2 Initialize cost matrix C; 3 for (k = 1; k <= n; k++) { 5 for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (C[i,k] + C[k,j] < C[i,j]) C[i,j] = C[i,k] + C[k,j]; } 0 8 5 } } 3 0 ∞ 8 2 1 2 3 2 ∞ 2 0 3 5 9 10 What About the Paths? Transitive Closure Floyd-Warshall(G=(V,E), w) ! The transitive closure of a graph Initialize cost matrix C; G=(V,E) is the graph G*=(V,E*), in Initialize path matrix P; which (u,v) ∈ E* iff a path from u to v for (k = 1; k <= n; k++) { exists in G. for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (C[i,k] + C[k,j] < C[i,j]) Transitive-Closure(G=(V,E)) C[i,j] = C[i,k] + C[k,j]; // Assumes a boolean adjacency P[i,j] = k; } // matrix A[i,j] == true iff (i,j) ∈ E } for (k = 1; k <= n; k++) { } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { PrintShortestPath(P, i, j) k = P[i, j]; if (A[i,j] == false) if (k != 0) { A[I,j] = A[i,k] && A[k,j]; PrintShortestPath(P,i,k); } print(k); PrintShortestPath(P,k,j); } } } 11 12

Recommend


More recommend