Shortest Paths III: All-Pairs Shortest Paths, Dynamic Programming, Matrix Multiplication, Floyd-Warshall, Johnson
Shortest Paths III: All-Pairs Shortest Paths, Dynamic Programming, Matrix Multiplication, Floyd-Warshall, Johnson
Shortest Paths III: All-Pairs Shortest Paths, Dynamic Programming, Matrix Multiplication, Floyd-Warshall, Johnson
Shortest paths
Single-source shortest paths
Nonnegative edge weights
General
Bellman-Ford O(VE)
DAG
General
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 }.
L16.4
Proof of claim
ks
j
M
Relaxation!
for k 1 to n
do if dij > dik + akj
then dij dik + akj
m 1 edges
Matrix multiplication
Compute C = A B, where C, A, and B are n n
matrices:
n
cij aik bkj .
k 1
0
0
0
0
= D0 = (dij(0)).
L16.6
Matrix multiplication
(continued)
The (min, +) multiplication is associative, and
with the real numbers, it forms an algebraic
structure called a closed semiring.
Consequently, we can compute
D(1) = D(0) A = A1
D(2) = D(1) A = A2
M
M
D(n1) = D(n2) A = An1 ,
yielding D(n1) = (d(i, j)).
Time = Q(nn3) = Q(n4). No better than n B-F.
L16.7
Improved matrix
multiplication algorithm
Repeated squaring: A2k = Ak Ak.
lg(n1)
2
4
2
Compute A , A , , A
.
O(lg n) squarings
Note: An1 = An = An+1 = L.
Time = Q(n3 lg n).
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}.
i
Floyd-Warshall recurrence
cij(k) = mink {cij(k1), cik(k1) + ckj(k1)}
cik
(k1)
ckj(k1)
j
cij(k1)
intermediate vertices in {1, 2, , k}
L16.10
relaxation
Notes:
Okay to omit superscripts, since extra relaxations
cant hurt.
Runs in Q(n3) time.
Simple to code.
Efficient in practice.
L16.11
Transitive closure of a
directed graph
Compute tij =
L16.12
Graph reweighting
Theorem. Given a label h(v) for each v V, reweight
each edge (u, v) E by
(u, v) = w(u, v) + h(u) h(v).
Then, all paths between the same two vertices are
reweighted by the same amount.
Proof. Let p = v1 v2 L vk be a path in the graph.
k 1
i 1
k 1
i 1
k 1
i 1
w( p ) + h ( v k ) h ( v1 ) .
L16.13
Johnsons algorithm
1. Find a vertex labeling h such that (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 from each vertex using .
Time = O(V E + V 2 lg V).
3. Reweight each shortest-path length (p) to produce
the shortest-path lengths w(p) of the original graph.
Time = O(V 2).
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: