algorithm design and analysis cse 565sxr48/cse565/lecture-notes/cse565-f16... · 2016-10-20 ·...
TRANSCRIPT
Sofya Raskhodnikova
Algorithm Design and Analysis
LECTURES 16Dynamic Programming
• Shortest Paths: Bellman-Ford
• Detecting negative cycles
Network Flow
•Duality of Max Flow and
Min Cut
10/19/2016S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.1
Belman-Ford: Efficient Implementation
10/19/2016
Bellman-Ford-Shortest-Path(G, s, t) {
foreach node v V {
M[v]
successor[v]
}
M[t] = 0
for i = 1 to n-1 {
foreach node w V {
if (M[w] has been updated in previous iteration) {
foreach node v such that (v, w) E {
if (M[v] > M[w] + cvw) {
M[v] M[w] + cvwsuccessor[v] w
}
}
}
If no M[w] value changed in iteration i, stop.
}
}
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.2
10/19/2016
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
The demonstration is for a sligtly different version of the algorithm (see
CLRS) that computes distances from the sourse node rather than distances
to the destination node.
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.3
10/19/2016
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
Initialization.
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.4
10/19/2016
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
Order of edge relaxation.
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.5
10/19/2016
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.6
10/19/2016
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.7
10/19/2016
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.8
10/19/2016
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.9
10/19/2016
4
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.10
10/19/2016
4
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.11
10/19/2016
42
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.12
10/19/2016
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.13
10/19/2016
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
End of pass 1.
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.14
10/19/2016
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.15
10/19/2016
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.16
10/19/2016
1
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.17
10/19/2016
1
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.18
10/19/2016
1
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.19
10/19/2016
1
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.20
10/19/2016
1
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.21
10/19/2016
1-2
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.22
10/19/2016
-2
1
2
-1
Example of Bellman-Ford
A
B
E
C D
–1
4
12
–3
2
5
3
0
1
2
34
5
7
8
6
End of pass 2 (and 3 and 4).
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.23
Distance Vector Protocol
10/19/2016S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.24
Distance Vector Protocol
Communication network.
Nodes routers.
Edges direct communication link.
Cost of edge delay on link.
Dijkstra's algorithm. Requires global information of network.
Bellman-Ford. Uses only local knowledge of neighboring nodes.
Synchronization. We don't expect routers to run in lockstep. The
order in which each foreach loop executes is not important. Moreover,
algorithm still converges even if updates are asynchronous.
naturally nonnegative, but Bellman-Ford used anyway!
L16.25
Distance Vector Protocol
Distance vector protocol.
Each router maintains a vector of shortest path lengths to every
other node (distances) and the first hop on each path (directions).
Algorithm: each router performs n separate computations, one for
each potential destination node.
"Routing by rumor."
Ex. RIP, Xerox XNS RIP, Novell's IPX RIP, Cisco's IGRP, DEC's DNA
Phase IV, AppleTalk's RTMP.
Caveat. Edge costs may change during algorithm (or fail completely).
tv 1s 1
1
deleted
"counting to infinity"
2 1
L16.26
Path Vector Protocols
Link state routing.
Each router also stores the entire path.
Based on Dijkstra's algorithm.
Avoids "counting-to-infinity" problem and related difficulties.
Requires significantly more storage.
Ex. Border Gateway Protocol (BGP), Open Shortest Path First (OSPF).
not just the distance and first hop
L16.27
Detecting negative cycles in a graph
10/19/2016S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.28
Detecting Negative Cycles
Bellman-Ford is guaranteed to work if there are no
negative-cost cycles.
How can we tell if a negative-cost cycle exists?
• We could pick a destination vertex 𝑡 and check whether
cost estimates in Bellman-Ford converge
• What is wrong with it?
– What if the cycle isn’t on any path to 𝑡?
10/19/2016
v
18
2
5-23
-15-11
6
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.29
Detecting Negative Cycles
Theorem. Can detect a negative cost cycle in O(mn) time.
• Add new node 𝑡 and connect all nodes to 𝑡 with 0-cost edge.
• Check if OPT(𝑛, 𝑣) = OPT(𝑛 − 1, 𝑣) for all nodes 𝑣.
– if yes, then no negative cycles
– if no, then extract cycle from shortest path from 𝑣 to 𝑡
10/19/2016
v
18
2
5-23
-15-11
6
t
0
0
0 0
0
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.30
Detecting Negative CyclesLemma. If OPT(𝑛, 𝑣) = OPT(𝑛 − 1, 𝑣) for all 𝑣, then no negative
cycles are connected to 𝑡.
Proof. If OPT(𝑛, 𝑣)=OPT(𝑛 − 1, 𝑣) for all 𝑣, then distance estimates
won’t change again even with many executions of the for loop. So
there are no negative cost cycles on any path from 𝑣 to 𝑡, for all 𝑣.
Lemma. If OPT(𝑛, 𝑣) < OPT(𝑛 − 1, 𝑣) for some node 𝑣, then some
path from 𝑣 to 𝑡 contains a cycle W of negative cost.
Proof. (by contradiction)
– Since OPT(𝑛, 𝑣) < OPT(𝑛 − 1, 𝑣), current path P from 𝑣 to 𝑡 has n edges.
– By pigeonhole principle, P must contain a directed cycle W.
– Deleting W yields a v-t path with < n edges W has negative cost.
v t
W
c(W) < 010/19/2016
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.31
Detecting Negative Cycles: Application
Currency conversion. Given n currencies and exchange rates between
pairs of currencies, is there an arbitrage opportunity?
Remark. Fastest algorithm very valuable!
Question. What should we use as edge costs?
F$
£ ¥DM
1/7
3/102/3 2
170 56
3/504/3
8
IBM
1/10000
800
L16.32
Detecting Negative Cycles: Summary
Bellman-Ford. O(mn) time, O(m + n) space.
Run Bellman-Ford for n iterations (instead of n-1).
Upon termination, Bellman-Ford successor variables trace a negative
cycle if one exists.
See p. 304 for improved version and early termination rule.
L16.33
Network Flow and
Linear Programming
10/19/2016S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L16.34
Soviet Rail Network, 1955
Reference: On the history of the transportation and maximum flow problems.Alexander Schrijver in Math Programming, 91: 3, 2002.
L16.35
Maximum Flow and Minimum Cut
Max flow and min cut.
Two very rich algorithmic problems.
Cornerstone problems in combinatorial optimization.
Beautiful mathematical duality.
Nontrivial applications / reductions.
Data mining.
Open-pit mining.
Project selection.
Airline scheduling.
Bipartite matching.
Baseball elimination.
Image segmentation.
Network connectivity.
Network reliability.
Distributed computing.
Egalitarian stable matching.
Security of statistical data.
Network intrusion detection.
Multi-camera scene reconstruction.
Many many more . . .
L16.36
Flow network.
Abstraction for material flowing through the edges.
G = (V, E) = directed graph, no parallel edges.
Two distinguished nodes: s = source, t = sink.
c(e) = capacity of edge e.
Minimum Cut Problem
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4
capacity
source sink
L16.37
Def. An s-t cut is a partition (A, B) of V with s A and t B.
Def. The capacity of a cut (A, B) is:
Cuts
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4
Capacity = 10 + 5 + 15= 30
A
cap(A, B) c(e)e out of A
L16.38
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4A
Cuts
Def. An s-t cut is a partition (A, B) of V with s A and t B.
Def. The capacity of a cut (A, B) is:
cap(A, B) c(e)e out of A
Capacity = 9 + 15 + 8 + 30= 62
we don’t count edges
into A
L16.39
Min s-t cut problem. Find an s-t cut of minimum capacity.
Minimum Cut Problem
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4A
Capacity = 10 + 8 + 10= 28
L16.40
Def. An s-t flow is a function that satisfies:
For each e E: (capacity)
For each v V – {s, t}: (conservation)
Def. The value of a flow f is:
Flows
4
0
0
0
0 0
0 4 4
0
0
0
Value = 40
f (e)e in to v
f (e)e out of v
0 f (e) c(e)
capacity
flow
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4 0
v( f ) f (e) e out of s
.
4
Water flowing in
and out of 𝑣
L16.41
Def. An s-t flow is a function that satisfies:
For each e E: (capacity)
For each v V – {s, t}: (conservation)
Def. The value of a flow f is:
Flows
10
6
6
11
1 10
3 8 8
0
0
0
11
capacity
flow
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4 0
Value = 24
f (e)e in to v
f (e)e out of v
0 f (e) c(e)
v( f ) f (e) e out of s
.
4
L16.42
Max flow problem. Find s-t flow of maximum value.
Maximum Flow Problem
10
9
9
14
4 10
4 8 9
1
0 0
0
14
capacity
flow
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4 0
Value = 28
L16.43
Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then
the net flow sent across the cut is equal to the amount leaving s.
Flows and Cuts
10
6
6
11
1 10
3 8 8
0
0
0
11
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4 0
Value = 24
f (e)e out of A
- f (e)e in to A
v( f )
4
A
L16.44
Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then
the net flow sent across the cut is equal to the amount leaving s.
Flows and Cuts
10
6
6
1 10
3 8 8
0
0
0
11
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4 0
f (e)e out of A
- f (e)e in to A
v( f )
Value = 6 + 0 + 8 - 1 + 11= 24
4
11
A
L16.45
Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then
the net flow sent across the cut is equal to the amount leaving s.
Flows and Cuts
10
6
6
11
1 10
3 8 8
0
0
0
11
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4 0
f (e)e out of A
- f (e)e in to A
v( f )
Value = 10 - 4 + 8 - 0 + 10= 24
4
A
L16.46
Flows and Cuts
Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then
Proof.
f (e)e out of A
- f (e) v( f )e in to A
.
v( f ) f (e)e out of s
v A
f (e)e out of v
- f (e)e in to v
f (e)e out of A
- f (e).e in to A
by flow conservation, all termsexcept v = s are 0
L16.47
Question
Two problems
• Min Cut
• Max Flow
•How do they relate?
L16.48
Flows and Cuts
Weak duality. Let f be any flow, and let (A, B) be any s-t cut. Then the
value of the flow is at most the capacity of the cut.
Cut capacity = 30 Flow value 30
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4
Capacity = 30
A
Big Optimization Idea #1: Look for structural constraints, e.g.
max flow min-cut
L16.49
Weak duality. Let f be any flow. Then, for any s-t cut (A, B),
v(f) cap(A, B).
Proof.
▪
Flows and Cuts
v( f ) f (e)e out of A
- f (e)e in to A
f (e)e out of A
c(e)e out of A
cap(A, B)
s
t
A B
7
6
8
4
L16.50
Certificate of Optimality
Corollary. Let f be any flow, and let (A, B) be any cut.
If v(f) = cap(A, B), then f is a max flow and (A, B) is a min cut.
Value of flow = 28Cut capacity = 28 Flow value 28
10
9
9
14
4 10
4 8 9
1
0 0
0
14
s
2
3
4
5
6
7
t
15
5
30
15
10
8
15
9
6 10
10
10154
4 0A
L16.51
Review Questions
True/False
Let G be an arbitrary flow network, with a source s, and sink t, and a positive integer capacity ce on every edge e.
1) If f is a maximum s-t flow in G, then f saturates every edge out of s with flow (i.e., for all edges e out of s, we have f(e) = ce).
2) Let (A,B) be a minimum s-t cut with respect to these capacities . If we add 1 to every capacity, then (A,B) is still a minimum s-t cut with respect to the new capacities.
L16.52