Algorithms CSE 245: All Pairs of Shortest Path
Algorithms CSE 245: All Pairs of Shortest Path
Algorithms CSE 245: All Pairs of Shortest Path
CSE 245
2
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. S ←
3. Q ← V[G] O(V) build min-heap
4. while Q Executed O(V) times
5. do u ← EXTRACT-MIN(Q) O(lgV)
6. S ← S {u}
7. for each vertex v Adj[u]
8. do RELAX(u, v, w) O(E) times; O(lgV)
Running time: O(VlgV + ElgV) = O(ElgV)
3
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. for i ← 1 to |V| - 1 O(V)
3. do for each edge (u, v) E O(E)
O(VE)
4. do RELAX(u, v, w)
5. for each edge (u, v) E O(E)
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
• m 1: l (m)
= min { lij
(m-1) , min {l (m-1) + w } }
ik kj
ij 1kn
= min {lik(m-1) + wkj}
1kn
11
Extending the Shortest Path
lij(m) = min {lik(m-1) + wkj}
1kn
k
j j
i * = i
k
12
EXTEND(L, W, n)
1. create L’, an n × n matrix
2. for i ← 1 to n
lij(m) = min {lik(m-1) + wkj}
3. do for j ← 1 to n 1kn
4. do lij’ ←∞
5. for k ← 1 to n
6. do lij’ ← min(lij’, lik + wkj)
7. return L’
Running time: (n3)
13
SLOW-ALL-PAIRS-SHORTEST-PATHS(W, n)
1. L(1) ← W
2. for m ← 2 to n - 1
3. do L(m) ←EXTEND (L(m - 1), W, n)
4. return L(n - 1)
14
Example lij(m) = min {lik(m-1) + wkj}
1kn
2 L(m-1) = L(1) W
3 4 0 3 8 -4 0 3 8 -4
8
1 3 0 1 7 0 1 7
2
-4
1
-5
4 0 4 0
7
5 4 2 -5 0 2 -5 0
6
6 0 6 0
L(m-1) = L(2)
2 0 3 8 2 -4
4
3 3 0 -4 1 7
8
1
2
3 4 0 5 11
-4 7
1
-5 2 -1 -5 0 -2
5 4 8 1 6 0
6
L(m-1) = L(3)
2 0 3 -3 2 -4
4
3 3 0 -4 1 -1
8
1
2
3 7 4 0 5 11
-4 7
1
-5 2 -1 -5 0 -2
5
6
4 8 5 1 6 0
20
The Structure of a Shortest Path
3
• Vertices in G are given by 6
0.5
1 2
V = {1, 2, …, n} 3 4
2 1
• Consider a path p = v1, v2, …, vl 2
5
– An intermediate vertex of p is any
21
The Structure of a Shortest Path
• For any pair of vertices i, j V, consider all
paths from i to j whose intermediate vertices
are all drawn from a subset {1, 2, …, k}
– Find p, a minimum-weight path from these paths
p1
pu j
i
pt
22
Example
2
4
3
d (0)
45
8
d (1)
16
1 3
2
45
14
1 -5
7
5 4
6
Example
2
4
3
d ( 2)
45 12
1 8 3
2
14
1 -5
7
5 4
6
Example
2
4
3 d ( 3)
45 6
1 8 3
2
14
1 -5
7
5 4
6
The Structure of a Shortest Path
• k is not an intermediate vertex of path p
k
– Shortest path from i to j with intermediate
i j
vertices from {1, 2, …, k} is a shortest path
from i to j with intermediate vertices from
{1, 2, …, k - 1}
• k is an intermediate vertex of path p
– p1 is a shortest path from i to k p1
k p2
j
– p2 is a shortest path from k to j i
– k is not intermediary vertex of p1, p2
– p1 and p2 are shortest paths from i to k with
vertices from {1, 2, …, k - 1}
26
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from
vertex i to vertex j with all intermediary
vertices drawn from {1, 2, …, k}
• k=0
• dij(k) = wij
27
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from vertex i to
vertex j with all intermediary vertices drawn from
{1, 2, …, k}
• k1
• Case 1: k is not an intermediate
k
vertex of path p i j
• dij(k) = dij(k-1)
28
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from vertex i to
vertex j with all intermediary vertices drawn from
{1, 2, …, k}
• k1
• Case 2: k is an intermediate
k
vertex of path p i
j
29
Computing the Shortest Path Weights
• dij(k) = wij if k = 0
min {dij(k-1) , dik(k-1) + dkj(k-1) }if k 1
• The final solution: D(n) = (dij(n)):
dij(n) = (i, j) i, j V
j j
+ (k, j)
i i
(i, k)
D(k-1) D(k)
30
The Floyd-Warshall algorithm
Floyd-Warshall(W[1..n][1..n])
01 D ¬ W // D(0)
02 for k ¬ 1 to n do // compute D(k)
03 for i ¬1 to n do
04 for j ¬1 to n do
05 if D[i][k] + D[k][j] < D[i][j] then
06 D[i][j] ¬ D[i][k] + D[k][j]
07 return D
31
Computing predecessor matrix
i if i j and wij
– Updating: p(k)(i,j) = p(k-1)(i,j) if(d(k-1)(i,j)<=d(k-1)(i,k)+(d(k-1)(k,j)
– p(k-1)(k,j) if(d(k-1)(i,j) > d(k-1)(i,k)+(d(k-1)(k,j)
Floyd-Warshall(W[1..n][1..n])
01 …
02 for k ¬ 1 to n do // compute D(k)
03 for i ¬1 to n do
04 for i ¬1 to n do
05 if D[i][k] + D[k][j] < D[i][j] then
06 D[i][j] ¬ D[i][k] + D[k][j]
07 P[i][j] ¬ P[k][j]
08 return D
32
Example dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }
D(0) = W D(1)
2 1 2 3 4 5 1 2 3 4 5
3 4 1 0 3 8 -4 1 0 3 8 -4
8
1 3 2 0 1 7 2 0 1 7
2
-4
1
-5
3 4 0 3 4 0
7
5 4 4 2 -5 0 4 2 5 -5 0 -2
6
D(2) 5 6 0 5 6 0
1 2 3 4 5 D(3) D(4)
1 0 3 8 4 -4 0 3 8 4 -4 0 3 -1 4 -4
2 0 1 7 0 1 7 3 0 -4 1 -1
3 4 0 5 11 4 0 5 11 7 4 0 5 3
4 2 5 -5 0 -2 2 -1 -5 0 -2 2 -1 -5 0 -2
5 6 0 6 0 8 5 1 6 0 33
Example dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }
D(5) P(5)
2 1 2 3 4 5 1 2 3 4 5
3 4 1 0 1 -3 2 -4 1 - 3 4 5 1
8
1
2
3 2 3 0 -4 1 -1 2 4 - 4 2 1
-4
1
-5
3 7 4 0 5 3 3 4 3 - 2 1
7
5 4 4 2 -1 -5 0 -2 4 4 3 4 - 1
6
5 8 5 1 6 0 5 4 3 4 5 -
Source: 5, Destination: 1
Shortest path: 8
Path: 5 …1 : 5…4…1: 5->4…1: 5->4->1
Source: 1, Destination: 3
Shortest path: -3
Path: 1 …3 : 1…4…3: 1…5…4…3: 1->5->4->3
In Class Exercise
2 D (0) W
4
3 1 2 3 4 5
1 0 3 8 ∞ -4
1 8 3
2 ∞ 0 ∞ 1 7
2 3 ∞ 4 0 ∞ ∞
-4 4 2 ∞ - 0 ∞
1 -5
5 5
7
5 4 ∞ ∞ ∞ 6 0
6
In Class Exercise
2 D (1)
4
3 1 2 3 4 5
1 0 3 8 ∞ -4
1 8 3
2 ∞ 0 ∞ 1 7
2 3 ∞ 4 0 ∞ ∞
-4 4 2 5 - 0 -2
1 -5
5 5
7
5 4 ∞ ∞ ∞ 6 0
6
In Class Exercise
2 D ( 2)
4
3 1 2 3 4 5
1 0 3 8 4 -4
1 8 3
2 ∞ 0 ∞ 1 7
2 3 ∞ 4 0 5 11
-4 4
1 -5 2 5 -5 0 -2
5
7 ∞ ∞ ∞ 6 0
5 4
6
In Class Exercise
2 D ( 3)
4
3 1 2 3 4 5
1 0 3 8 4 -4
1 8 3
2 ∞ 0 ∞ 1 7
2 3 ∞ 4 0 5 11
-4 4
1 -5 2 -1 -5 0 -2
5
7 ∞ ∞ ∞ 6 0
5 4
6
In Class Exercise
2 D ( 4)
4
3 1 2 3 4 5
1 0 3 -1 4 -4
1 8 3
2 3 0 -4 1 -1
2 3 7 4 0 5 3
-4 4
1 -5 2 -1 -5 0 -2
5
7 8 5 1 6 0
5 4
6
In Class Exercise
2 D (5)
4
3 1 2 3 4 5
1 0 1 -3 2 -4
1 8 3
2 3 0 -4 1 -1
2 3 7 4 0 5 3
-4 4
1 -5 2 -1 -5 0 -2
5
7 8 5 1 6 0
5 4
6
PrintPath for Warshall’s Algorithm
PrintPath(s, t)
{
if(P[s][t]==nil){
print(“No path”);
return;
}
else if (P[s][t]==s){
print(s);
}
else{
print_path(s,P[s][t]);
print_path(P[s][t], t);
}
}
Print (t) at the end of the PrintPath(s,t) 41