Title: All Pairs Shortest Paths
1All Pairs Shortest Paths
2outline
- application of single source shortest path
algorithms - direct methods to solve the problem
- matrix multiplication
- Floyds algorithm
- transitive closure (Warshalls algorithm)
3 motivation
- computer network
- aircraft network (e.g. flying time, fares)
- railroad network
- table of distances between all pairs of cities
for a road atlas
4single source shortest path algorithms
- if edges are non-negative
- running the Dijkstra algorithm n-times, once for
each vertex as the source - running time O(nm lg n)
- (using binary-heap)
5single source shortest path algorithms
- negative-weight edges
- Bellman-Ford algorithm
- running time O(n2 m)
6data structure
- adjacency matrix
- c E ? ? as n x n matrix C
- 0 if i j,
- cij c(i,j) if i ? j and (i,j) ? E,
- 8 if i ? j and (i,j) ? E
7matrix multiplication (idea)
- dij(m) minimum weight of any path
- from i to j
- that contains at most m edges
8matrix multiplication (idea)
dik(m-1)
k
p
ckj
i
j
dij(m-1)
v
9matrix multiplication (idea)
- dij(m) min (dij(m-1), min1kn dik(m-1)
ckj) - look at all possible predecessors k of j and
compare
10matrix multiplication (structure)
- dij(1) cij
- dij(m) min (dij(m-1), min1kn dik(m-1)
ckj) - min1kn dik(m-1) ckj
11matrix multiplication (structure)
- Compute a series of matrices
- D(1), D(2), , D(n-1)
- D(m) D(m-1) ? C
- final matrix D(n-1) contains the final
shortest-path weights
12matrix multiplication (pseudo-code)
- EXTEND-SHORTEST-PATHS (D,C )
- n ? rows D
- let D' (d'ij ) be an n x n matrix
- for i ? 1 to n
- do for j ? 1 to n
- do d'ij ? 8
- for k ? 1 to n
- do d'ij ? min (d'ij , dik ckj)
- return D'
13matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
14matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
15matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
16matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
17matrix multiplication (example)
- ?
- 1
- d14(2) (0 3 8 ? ?4) ? ?
- 0
- 6
- min (?, 4, ?, ?, 2)
- 2
18matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
19matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
20relation to matrix multiplication
- C A ? B
- cij Sk1n aik ? bkj
- compare
- D(m) D(m-1) ? C
- ? dij(m) min1kn dik(m-1) ckj
21matrix multiplication
- compute the sequence of n-1 matrices
- D(1) D(0) ? C C,
- D(2) D(1) ? C C2,
- D(3) D(2) ? C C3,
-
- D(n-1) D(n-2) ? C Cn-1
22matrix multiplication (pseudo-code)
- ALL-PAIRS-SHORTEST-PATHS (C )
- n ? rows C
- D (1) ? C
- for m ? 2 to n - 1
- do D (m) ? EXTEND-SHORTEST-PATHS
- (D (m-1),C )
- return D (n-1)
- negative cycles dii lt 0
23matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
24matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
25matrix multiplication (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
26matrix multiplication (running time)
- O(n4)
- Improving the running time
- compute not all D(m) matrices
- interested only in D(n-1), which is D(m) for all
integers m n-1
27improving the running time
- compute the sequence
- D(1) C,
- D(2) C2 C ? C,
- D(4) C4 C2 ? C2,
- D(8) C8 C4 ? C4
- ...
- D(2 ? ?lg(n-1)?) C2? ?lg(n-1)?
- C2 ? ( ?lg(n-1)? -1) ? C2 ?( ?lg(n-1)? -1)
- so we need only ?lg(n-1)? matrix products
- O(n3 lg n)
28Floyds algorithm
1
2
.
i
j
k-1
k
k1
.
n
29Floyds algorithm
- dij(0) cij
- (no intermediate vertices at all)
- dij(k) min(dij(k-1), dik(k-1) dkj(k-1)) if
k 1 - result D(n) (dij(n)) d(i,j)
- (because all intermediate vertices are in the
set 1, 2, , n)
30Floyds algorithm (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
31Floyds algorithm (example)
2
2
?
3
3
3
8
2
4
4
1
?4
?
5
5
4 ? 3 ? 5
32Floyds algorithm (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
33Floyds algorithm (example)
1
1
?
3
3
3
4
4
4
2
1
5
5
5
7
4 ? 5 ? 2 3 ? 4 ? 1 ? 5 ? 4 3 ? 5 ?
34Floyds algorithm (example)
2
4
3
1
3
8
2
-4
1
-5
7
5
4
6
35Floyds algorithm (pseudo-code)
- FLOYD (C )
- n ? rows C
- D (0) ? C
- for k ? 1 to n
- do for i ? 1 to n
- do for j ? 1 to n
- dij(k) ? min (dij(k-1), dik(k-1) dkj(k-1))
- return D (n)
- running time O(n3)
36transitive closure (the problem)
- find out whether there is a path from i to j
and compute - G (V,E),
- where E (i,j) there is a path from i to
j in G
a
c
b
i
j
37transitive closure (the problem)
- find out whether there is a path from i to j
and compute - G (V,E),
- where E (i,j) there is a path from i to
j in G
a
c
b
i
j
38transitive closure
- one way
- set cij 1 and
- run the Floyd algorithm
- running time O(n3)
39transitive closure
- another way
- substitute and min by AND and OR
- in Floyds algorithm
- running time O(n3)
40transitive closure (pseudo-code)
- TRANSITIVE-CLOSURE (G )
- n ? ?V G ?
- for i ? 1 to n
- do for j ? 1 to n
- do if i j OR (i,j ) ? E G
- then tij(0) ? 1
- else tij(0) ? 0
- for k ? 1 to n
- do for i ? 1 to n
- do for j ? 1 to n
- do tij(k) ? tij(k-1) OR (tik(k-1) AND
tkj(k-1) ) - return T (n)
41table of running times
42Floyds algorithm
k
dik(k-1)
dkj(k-1)
i
dij(k-1)
j