Professional Documents
Culture Documents
Lec19 PDF
Lec19 PDF
6.046J/18.401J LECTURE 19
Shortest Paths III All-pairs shortest paths Matrix-multiplication algorithm Floyd-Warshall algorithm Johnsons algorithm Prof. Charles E. Leiserson
November 21, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L19.1
Shortest paths
Single-source shortest paths Nonnegative edge weights General DAG
Dijkstras algorithm: O(E + V lg V) Bellman-Ford algorithm: O(VE) One pass of Bellman-Ford: O(V + E)
L19.2
Shortest paths
Single-source shortest paths Nonnegative edge weights General DAG
Bellman-Ford: O(VE) One pass of Bellman-Ford: O(V + E) Dijkstras algorithm |V| times: O(VE + V 2 lg V) Three algorithms today.
Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L19.3
L19.4
Dynamic programming
Consider the n n adjacency matrix A = (aij) of the digraph, and define dij(m) = weight of a shortest path from i to j that uses at most m edges. Claim: We have 0 if i = j, (0) dij = if i j; and for m = 1, 2, , n 1, dij(m) = mink{dik(m1) + akj }.
November 21, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L19.6
Proof of claim
dij(m) = mink{dik(m1) + akj } ii
m ges d e 1 m m 1 edg es es g d 1e
ks
jj M
m 1 edges
L19.7
Proof of claim
dij(m) = mink{dik(m1) + akj } ii Relaxation!
m ges d e 1 m m 1 edg es es g d 1e
ks
jj M
m 1 edges
L19.8
Proof of claim
dij(m) = mink{dik(m1) + akj } ii Relaxation!
m ges d e 1 m m 1 edg es es g d 1e
ks
jj M
m 1 edges
Note: No negative-weight cycles implies (i, j) = dij (n1) = dij (n) = dij (n+1) = L
November 21, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L19.9
Matrix multiplication
Compute C = A B, where C, A, and B are n n matrices: n cij = aik bkj . Time = (n3) using the standard algorithm.
k =1
L19.10
Matrix multiplication
Compute C = A B, where C, A, and B are n n matrices: n cij = aik bkj . Time = (n3) using the standard algorithm. What if we map + min and +?
k =1
L19.11
Matrix multiplication
Compute C = A B, where C, A, and B are n n matrices: n cij = aik bkj . Time = (n3) using the standard algorithm. What if we map + min and +? cij = mink {aik + bkj}. Thus, D(m) = D(m1) A. Identity matrix = I =
November 21, 2005
k =1
0 0 0 0
= D0 = (dij(0)).
L19.12
Floyd-Warshall algorithm
Also dynamic programming, but faster!
Define cij(k) = weight of a shortest path from i to j with intermediate vertices belonging to the set {1, 2, , k}.
ii
k k
k k
k k
k k
jj
Floyd-Warshall recurrence
cij(k) = mink {cij(k1), cik(k1) + ckj(k1)} cik ii
(k1)
ckj(k1) jj
cij(k1)
L19.16
relaxation
Notes: Okay to omit superscripts, since extra relaxations cant hurt. Runs in (n3) time. Simple to code. Efficient in practice.
November 21, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L19.17
L19.18
Graph reweighting
Theorem. Given a function h : V R, reweight each edge (u, v) E by wh(u, v) = w(u, v) + h(u) h(v). Then, for any two vertices, all paths between them are reweighted by the same amount.
L19.19
Graph reweighting
Theorem. Given a function h : V R, reweight each edge (u, v) E by wh(u, v) = w(u, v) + h(u) h(v). Then, for any two vertices, all paths between them are reweighted by the same amount. Proof. Let p = v1 v2 L vk be a path in G. We k 1 have
wh ( p ) = = =
i =1 k 1 i =1 k 1 i =1
wh ( vi ,vi+1 )
= w ( p ) + h ( v1 ) h ( v k ) .
November 21, 2005
L19.21
L19.22
Johnsons algorithm
1. Find a function h : V R such that wh(u, v) 0 for all (u, v) E by using Bellman-Ford to solve the difference constraints h(v) h(u) w(u, v), or determine that a negative-weight cycle exists. Time = O(V E). 2. Run Dijkstras algorithm using wh from each vertex u V to compute h(u, v) for all v V. Time = O(V E + V 2 lg V). 3. For each (u, v) V V, compute (u, v) = h(u, v) h(u) + h(v) . Time = O(V 2).