algorithmsesslab.hanyang.ac.kr/uploads/algorithm_2018_2/lecture... · 2018-11-27 · the...
TRANSCRIPT
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
3
Using single source shortest path algorithms
• Positive edges
• Negative edges
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.
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).
6
Using single source shortest path algorithms
• Negative edges– Using Bellman-Ford algorithm:
• O(V2E)
• Dense graph– O(V4)
7
Using single source shortest path algorithms
• Use adjacency matrix
• Predecessor matrix
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
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.
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.
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
12
Shortest paths and matrix multiplication
• O(V4)-time algorithm– Use dynamic programming
– Use matrix multiplication
– Associative (short explanation)
– Computing predecessor matrix
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.
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}
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
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
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
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′
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’
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)
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
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
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
24
Shortest paths and matrix multiplication
1 2 3
4 5 6
-4
1 2
-810
5
7
-12
3
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)
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.
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.
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}
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
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
31
The Floyd-Warshall algorithm
32
The Floyd-Warshall algorithm
33
The Floyd-Warshall algorithm
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
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}.