Introducton to Algorthms 6.046J/8.40J LECTURE 6 Shortest Paths III All-pars shortest paths Matrx-multplcaton algorthm Floyd-Warshall algorthm Johnson s algorthm Prof. Charles E. Leserson
Shortest paths Sngle-source shortest paths Nonnegate edge weghts Djkstra s algorthm: O(E + V lg V) General Bellman-Ford algorthm: O(VE) DAG One pass of Bellman-Ford: O(V + E) Introducton to Algorthms Noember 8, 2004 L6.2
Shortest paths Sngle-source shortest paths Nonnegate edge weghts Djkstra s algorthm: O(E + V lg V) General Bellman-Ford: O(VE) DAG One pass of Bellman-Ford: O(V + E) All-pars shortest paths Nonnegate edge weghts Djkstra s algorthm V tmes: O(VE + V 2 lg V) General Three algorthms today. Introducton to Algorthms Noember 8, 2004 L6.3
All-pars shortest paths Input: Dgraph G = (V, E), where V = {, 2,, n}, wth edge-weght functon w : E R. Output: n n matrx of shortest-path lengths δ(, j) for all, j V. Introducton to Algorthms Noember 8, 2004 L6.4
All-pars shortest paths Input: Dgraph G = (V, E), where V = {, 2,, n}, wth edge-weght functon w : E R. Output: n n matrx of shortest-path lengths δ(, j) for all, j V. IDEA: Run Bellman-Ford once from each ertex. Tme = O(V 2 E). Dense graph (n 2 edges) Θ(n 4 ) tme n the worst case. Good frst try! Introducton to Algorthms Noember 8, 2004 L6.5
Dynamc programmng Consder the n n adjacency matrx A = (a j ) of the dgraph, and defne d (m) j = weght of a shortest path from to j that uses at most m edges. Clam: We hae d (0) 0 f = j, j = f j; and for m =, 2,, n, d (m) j = mn k {d (m ) k + a kj }. Introducton to Algorthms Noember 8, 2004 L6.6
Proof of clam d j (m) = mn k {d k (m ) + a kj } m edges m edges m edges k s M j m edges Introducton to Algorthms Noember 8, 2004 L6.7
Proof of clam d j (m) = mn k {d k (m ) + a kj } Relaxaton! for k to n do f d j > d k + a kj then d j d k + a kj m edges m edges m edges k s M m edges j Introducton to Algorthms Noember 8, 2004 L6.8
Proof of clam d j (m) = mn k {d k (m ) + a kj } m edges m edges m edges k s Relaxaton! for k to n do f d j > d k + a kj then d j d k + a kj m edges Note: No negate-weght cycles mples δ(, j) = d (n ) j = d (n) j = d (n+) j = L M j Introducton to Algorthms Noember 8, 2004 L6.9
Matrx multplcaton Compute C = A B, where C, A, and B are n n matrces: n c = a b. j k= Tme = Θ(n 3 ) usng the standard algorthm. k kj Introducton to Algorthms Noember 8, 2004 L6.0
Matrx multplcaton Compute C = A B, where C, A, and B are n n matrces: n c = a b. j k= Tme = Θ(n 3 ) usng the standard algorthm. What f we map + mn and +? k kj Introducton to Algorthms Noember 8, 2004 L6.
Matrx multplcaton Compute C = A B, where C, A, and B are n n matrces: n c = a b. j k= Tme = Θ(n 3 ) usng the standard algorthm. What f we map + mn and +? c j = mn k {a k + b kj }. Thus, D (m) = D (m ) A. Identty matrx = I = k kj 0 0 0 0 = D 0 = (d j (0) ). Introducton to Algorthms Noember 8, 2004 L6.2
Matrx multplcaton (contnued) The (mn, +) multplcaton s assocate, and wth the real numbers, t forms an algebrac structure called a closed semrng. Consequently, we can compute D () = D (0) A = A D (2) = D () A = A 2 M M D (n ) = D (n 2) A = A n, yeldng D (n ) = (δ(, j)). Tme = Θ(n n 3 ) = Θ(n 4 ). No better than n B-F. Introducton to Algorthms Noember 8, 2004 L6.3
Improed matrx multplcaton algorthm Repeated squarng: A 2k = A k A k. Compute A 2, A 4,, A 2 lg(n ). O(lg n) squarngs Note: A n = A n = A n+ = L. Tme = Θ(n 3 lg n). To detect negate-weght cycles, check the dagonal for negate alues n O(n) addtonal tme. Introducton to Algorthms Noember 8, 2004 L6.4
Floyd-Warshall algorthm Also dynamc programmng, but faster! Defne c j (k) = weght of a shortest path from to j wth ntermedate ertces belongng to the set {, 2,, k}. k k k k j Thus, δ(, j) = c j (n). Also, c j (0) = a j. Introducton to Algorthms Noember 8, 2004 L6.5
Floyd-Warshall recurrence c j (k) = mn {c j (k ), c k (k ) + c kj (k ) } c k (k ) k c kj (k ) c j (k ) ntermedate ertces n {, 2,, k} j Introducton to Algorthms Noember 8, 2004 L6.6
Pseudocode for Floyd- Warshall for k to n do for to n do for j to n do f c j > c k + c kj then c j c k + c kj relaxaton Notes: Okay to omt superscrpts, snce extra relaxatons can t hurt. Runs n Θ(n 3 ) tme. Smple to code. Effcent n practce. Introducton to Algorthms Noember 8, 2004 L6.7
Transte closure of a drected graph Compute t j = f there exsts a path from to j, 0 otherwse. IDEA: Use Floyd-Warshall, but wth (, ) nstead of (mn, +): Tme = Θ(n 3 ). t j (k) = t j (k ) (t k (k ) t kj (k ) ). Introducton to Algorthms Noember 8, 2004 L6.8
Graph reweghtng Theorem. Gen a functon h : V R, reweght each edge (u, ) E by w h (u, ) = w(u, ) + h(u) h(). Then, for any two ertces, all paths between them are reweghted by the same amount. Introducton to Algorthms Noember 8, 2004 L6.9
Introducton to Algorthms Noember 8, 2004 L6.20 Graph reweghtng Proof. Let p = 2 L k be a path n G. We hae ( ) ) ( ) ( ) ( ) ( ) ( ), ( ) ( ) ( ), ( ), ( ) ( k k k k k h h h h p w h h w h h w w p w + = + = + = = = + = + + = +. Theorem. Gen a functon h : V R, reweght each edge (u, ) E by w h (u, ) = w(u, ) + h(u) h(). Then, for any two ertces, all paths between them are reweghted by the same amount. Same amount!
Shortest paths n reweghted graphs Corollary. δ h (u, ) = δ(u, ) + h(u) h(). Introducton to Algorthms Noember 8, 2004 L6.2
Shortest paths n reweghted graphs Corollary. δ h (u, ) = δ(u, ) + h(u) h(). IDEA: Fnd a functon h : V R such that w h (u, ) 0 for all (u, ) E. Then, run Djkstra s algorthm from each ertex on the reweghted graph. NOTE: w h (u, ) 0 ff h() h(u) w(u, ). Introducton to Algorthms Noember 8, 2004 L6.22
Johnson s algorthm. Fnd a functon h : V R such that w h (u, ) 0 for all (u, ) E by usng Bellman-Ford to sole the dfference constrants h() h(u) w(u, ), or determne that a negate-weght cycle exsts. Tme = O(VE). 2. Run Djkstra s algorthm usng w h from each ertex u V to compute δ h (u, ) for all V. Tme = O(VE + V 2 lg V). 3. For each (u, ) V V, compute δ(u, ) = δ h (u, ) h(u) + h(). Tme = O(V 2 ). Total tme = O(VE+ V 2 lg V). Introducton to Algorthms Noember 8, 2004 L6.23