algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · the...
TRANSCRIPT
![Page 2: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/2.jpg)
2
Contents
• Using single source shortest path algorithms
• Presents O(V4)-time algorithm, O(V3 log V)-time algorithm, O(V3)-time algorithm
– O(V4)-time algorithm
– O(V3 log V)-time algorithm
– Floyd-Warshall algorithm
– Transitive closure of a directed graph
2
![Page 3: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/3.jpg)
3
Using single source shortest path algorithms
• Positive edges
• Negative edges
![Page 4: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/4.jpg)
4
Using single source shortest path algorithms
• Using single source shortest path algorithms– We can solve an all-pairs shortest-paths problem by
running a single-source shortest-paths algorithm |V| times, once for each vertex as the source.
![Page 5: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/5.jpg)
5
Using single source shortest path algorithms
• Positive edges– Using dijkstra algorithm:
• The linear-array implementation– O(V3 + V E) = O(V3).
• The binary min-heap implementation O(V E lg V),
– Fibonacci heap• O(V2 lg V + V E).
![Page 6: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/6.jpg)
6
Using single source shortest path algorithms
• Negative edges– Using Bellman-Ford algorithm:
• O(V2E)
• Dense graph– O(V4)
![Page 7: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/7.jpg)
7
Using single source shortest path algorithms
• Use adjacency matrix
• Predecessor matrix
![Page 8: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/8.jpg)
8
Using single source shortest path algorithms
• Use adjacency matrix• We assume that the vertices are numbered 1, 2,..., |V|
• The input is an n×n matrix W representing the edge weights of an n-vertex directed graph G = (V, E)
• Negative-weight edges are allowed, but we assume for the time being that the input graph contains no negative-weight cycles.
E j) (i, and j i if
E, j) (i, and j i if j) (i, edge directed of weight the
j, i if 0
ijw
![Page 9: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/9.jpg)
9
Using single source shortest path algorithms
• Predecessor matrix
– Say Π = (πij)
– πij = NIL if either i = j or there is not path from i to j.
– πij is the predecessor of j on some shortest path from i to j.
![Page 10: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/10.jpg)
10
Using single source shortest path algorithms
• For each vertex i ∈ V, we define predecessor subgraph of G for i as Gπ,i = (Vπ,i, Eπ,i), where– Vπ,i = {j ∈ V : πij = NIL} ∪ {i}
– Eπ,i = {(πij, j)|j ∈ Vπ,i − {i}}
• If Gπ,i is a shortest-paths tree, then PRINT-ALL-PAIRS-SHORTEST-PATH will print a shortest path from i to j.
![Page 11: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/11.jpg)
11
Using single source shortest path algorithms
PRINT-ALL-PAIRS-SHORTEST-PATH(Π, i, j)
1 if i = j
2 then print i
3 else if πij = NIL
4 then print “no path from” i “to” j “exists”5 else PRINT-ALL-PAIRS-SHORTEST-PATH(Π,i, πij)
6 print j
![Page 12: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/12.jpg)
12
Shortest paths and matrix multiplication
• O(V4)-time algorithm– Use dynamic programming
– Use matrix multiplication
– Associative (short explanation)
– Computing predecessor matrix
![Page 13: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/13.jpg)
13
Shortest paths and matrix multiplication
• Use dynamic programming
– Characterize the structure of an optimal solution.
– Recursively define the value of an optimal solution.
– Compute the value of an optimal solution in a bottom-up fashion.
![Page 14: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/14.jpg)
14
Shortest paths and matrix multiplication
• The structure of a shortest path
– On a graph G = (V, E), all subpaths of a shortest path are shortest paths. (Lemma 24.1)
– p’ is a shortest path from i to k, and so δ(i, j) = δ(i, k) + wkj.
i
k
j
P1P2
P` : {1,2,…,k-1}
![Page 15: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/15.jpg)
15
Shortest paths and matrix multiplication
• A recursive solution to the all-pairs shortest-paths problem
– Let be the minimum weight of any path from vertex ito vertex j that contains at most m edges
• When m = 0, then
• When m ≥ 1, then
)(m
ijl
j i if
ji if 0)0(
ijl
}{min
}){min ,min(
)1(
nk1
)1(
nk1
)1()(
kj
m
ij
kj
m
ij
m
ij
m
ij
wl
wlll
![Page 16: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/16.jpg)
16
Shortest paths and matrix multiplication
– If the graph contains no negative-weight cycles,
– For every pair of vertices i and j for which δ(i, j) < ∞
• There is a shortest path from i to j that is simple and thus contains at most n - 1 edges.
– A path from vertex i to vertex j with more than n - 1 edges cannot have lower weight than a shortest path from i to j.
– Therefore,
...),( )1()()1( n
ij
n
ij
n
ij lllji
![Page 17: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/17.jpg)
17
Shortest paths and matrix multiplication
• Computing the shortest-path weights bottom up
– Input the matrix W = (wij), we now compute a series of matrices L(1), L(2), ... , L(n-1),
• Where for m = 1, 2,..., n - 1, we have .
– The final matrix L(n-1) contains the actual shortest-path weights.
– Observe that for all vertices i, j ∈ V , and so L(1) = Wij.
)( )()( m
ij
m ll
![Page 18: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/18.jpg)
18
Shortest paths and matrix multiplication
– Let L(m−1) = L, L(m) = L. We have
– Costs Θ(n3) time.
EXTEND-SHORTEST-PATHS(L, W)
1 n ← rows[L]
2 let L′ = (lij ′) be an n × n matrix
3 for i ← 1 to n
4 do for j ← to n
5 do
6 for k ← 1 to n
7 do
8 return L′
![Page 19: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/19.jpg)
19
Shortest paths and matrix multiplication
EXTEND-SHORTEST-PATHS(L, W)1 n ← rows[L]
2 let C be an n × n matrix
3 for i ← 1 to n
4 do for j ← 1 to n
5 do cij ← 0
6 for k ← 1 to n
7 do cij ← cij + aik · bkj
8 return L’
![Page 20: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/20.jpg)
20
Shortest paths and matrix multiplication
• Costs Θ(n4) time.
SLOW-ALL-PAIRS-SHORTEST-PATHS(W)
1 n ← rows[W]
2 L(1) ←W
3 for m ← 2 to n − 1
4 do L(m) ← EXTEND-SHORTEST-PATHS(L(m−1),W)
5 return L(n−1)
![Page 21: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/21.jpg)
21
Shortest paths and matrix multiplication
• Improving the running time
– We are interested only in matrix L(n-1).
– Recall that in the absence of negative-weight cycles, implies L(m) = L(n-1) for all integers m ≥ n - 1.
– Therefore, we can compute L(n-1) with only ⌈lg(n - 1)⌉ matrix products by computing the sequence
![Page 22: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/22.jpg)
22
Shortest paths and matrix multiplication
2
5 4
1 3
6
7 1
8
-5
43
2
-4
0 6 1 5 8
2 0 5 1 2
3 5 0 4 7
1- 1 4 0 3
4- 2 3- 1 0
0 6 1 5 8
2 0 5 1 2
11 5 0 4 7
1- 1 4 0 3
4- 2 8 3 0
0 6 1 8
2 0 5 1 2
11 5 0 4
7 1 4 0 3
4- 2 8 3 0
0 6
0 5 2
0 4
7 1 0
4- 8 3 0
)4()3(
)2()1(
LL
LL
![Page 23: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/23.jpg)
23
Shortest paths and matrix multiplication
1)1lg(21)1lg(2)1lg(2)1lg(2(
448)8(
224)4(
2)2(
)1(
nnnnWWWL
WWWL
WWWL
WWWL
WL
![Page 24: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/24.jpg)
24
Shortest paths and matrix multiplication
1 2 3
4 5 6
-4
1 2
-810
5
7
-12
3
![Page 25: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/25.jpg)
25
Shortest paths and matrix multiplication
• Costs Θ(n3) time.
FASTER-ALL-PAIRS-SHORTEST-PATHS(W)
1 n ← rows[W]
2 L(1) ← W
3 m ← 1
4 while m < n - 1
5 do L(2m) ← EXTEND-SHORTEST-PATHS(L(m), L(m))
6 m ← 2m
7 return L(m)
![Page 26: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/26.jpg)
26
The Floyd-Warshall algorithm
– Intermediate Vertex• An intermediate vertex of a simple path p = <v1, v2, · · · , vl> is
any vertex of p other than v1 and vl.
![Page 27: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/27.jpg)
27
The Floyd-Warshall algorithm
• The structure of a shortest path
– Floyd-Warshall algorithm is based on the observation of the intermediate vertices, which costs Θ(|V|3) time.
– Let V = {1, 2, · · · , n}.
– For any pair of vertices i, j ∈ V , consider all paths from ito j whose intermediate vertices are all drawn from {1, 2, · ·
· , k}, and let p be a minimum weight path from among them.
![Page 28: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/28.jpg)
28
The Floyd-Warshall algorithm
– If k is not an intermediate vertex of path p, then all intermediate vertices of p are in {1, 2, · · · , k − 1}.
– If k is an intermediate vertex of path p, then we break pdown into
i
k
j
P1P2
P: all intermediate vertices in {1,2,…,k-1}
all intermediate vertices in {1,2..,k-1} all intermediate vertices in {1,2..,k-1}
![Page 29: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/29.jpg)
29
The Floyd-Warshall algorithm
• A recursive solution to the all-pairs shortest-paths problem
– Let be the weight of a shortest path from vertex i to vertex j for which all intermediate vertices are in the set {1, 2, · · · , k}.
– We have the following recurrence:
– Because for any path, all intermediate vertices are in the set {1, 2, · · · , n}, the matrix gives the final answer:
for all i, j ∈ V .
)(k
ijd
(25.5) 1.k if )dd,min(d
0, k if
1)-(k
kj
1)-(k
ik
1)-(k
ij
)(
ijk
ij
wd
d D (n)
ij
(n)
),()( jid n
ij
![Page 30: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/30.jpg)
30
The Floyd-Warshall algorithm
• costs Θ(n3) time.
FLOYD-WARSHALL(W)
1 n ← rows[W]
2 D(0) ←W
3 for k ← 1 to n
4 do for i ← 1 to n
5 do for j ← 1 to n
6 do
7 return D(n)
)1()1()1()( ,min k
kj
k
ik
k
ij
k
ij dddd
![Page 31: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/31.jpg)
31
The Floyd-Warshall algorithm
![Page 32: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/32.jpg)
32
The Floyd-Warshall algorithm
![Page 33: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/33.jpg)
33
The Floyd-Warshall algorithm
![Page 34: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/34.jpg)
34
The Floyd-Warshall algorithm
• Constructing A Shortest Path
– Let be the predecessor of vertex j on a shortest path from vertex i with all intermediate vertices in {1, 2, · · · ,
k}.
k
ij
. wand j i if i
, or w j i if NIL
ij
ij)0(
ij
. if
, if
)1()1()1()1(
)1()1()1()1(
)(
k
kj
k
ik
k
ij
k
ik
k
kj
k
ik
k
ij
k
ijk
ijddd
ddd
![Page 35: Algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · The Floyd-Warshall algorithm • A recursive solution to the all-pairs shortest-paths problem](https://reader030.vdocuments.us/reader030/viewer/2022040813/5e57f81e86d3727e542fc982/html5/thumbnails/35.jpg)
35
The Floyd-Warshall algorithm
• Transitive Closure of Graph
– Given a directed graph G = (V, E) with vertex set V = {1, 2, · · · , n}.
– The transitive closure of G is defined as the graph G∗ = (V,
E∗), where E∗ = {(i, j) : there is a path from vertex i to vertex j in G}.