7. Network Flow - Cs.princeton.edu

Soviet Rail Network, 1955
7. Network Flow
Reference: On the history of the transportation and maximum flow problems.
Alexander Schrijver in Math Programming, 91: 3, 2002.
Algorithm Design by Éva Tardos and Jon Kleinberg •
Copyright © 2005 Addison Wesley
•
Slides by Kevin Wayne
2
Maximum Flow and Minimum Cut
Minimum Cut Problem
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.
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 reliability.
2
9
5
4
15
15
10
3
8
6
10
4
6
15
4
30
7
Distributed computing.
Egalitarian stable matching.
10
Security of statistical data.
Network intrusion detection.
source
Multi-camera scene reconstruction.
s
5
Many many more . . .
Network connectivity.
capacity
3
15
t
sink
10
4
Cuts
Cuts
Def. An s-t cut is a partition (A, B) of V with s ! A and t ! B.
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:
Def. The capacity of a cut (A, B) is:
cap( A, B) =
" c(e)
cap( A, B) =
e out of A
!
10
5
s
" c(e)
e out of A
!
2
9
5
4
15
15
10
3
8
6
10
4
6
15
4
30
7
2
9
5
4
15
15
10
3
8
6
10
4
6
15
4
30
7
10
t
5
s
t
A
15
A
10
15
Capacity = 10 + 5 + 15
= 30
10
Capacity = 9 + 15 + 8 + 30
= 62
5
6
Minimum Cut Problem
Flows
Min s-t cut: find an s-t cut of minimum capacity.
Def. An s-t flow is a function that satisfies:
For each e ! E:
0 " f (e) " c(e)
For each v ! V – {s, t}: " f (e) = " f (e)
(capacity)
(conservation)
!
!
e in to v
e out of v
Def. The value of a !flow f is: v( f ) =
" f (e) .
e out of s
!
0
2
9
5
4
10
9
5
4 4
0
15
15
3
8
4 0
6
4
30
2
4
15
15
10
3
8
6
10
4
6
15
4
30
7
10
!
0
5
s
t
5
s
0
0
4
4
6
0
A
15
10
Capacity = 10 + 8 + 10
= 28
7
capacity
15
flow
0
10
15 0
0
7
10
t
0
10
Value = 4
8
Flows
Maximum Flow Problem
Def. An s-t flow is a function that satisfies:
For each e ! E:
0 " f (e) " c(e)
For each v ! V – {s, t}: " f (e) = " f (e)
Max flow problem: find s-t flow of maximum value.
(capacity)
(conservation)
!
!
e in to v
e out of v
Def. The value of a !flow f is: v( f ) =
" f (e) .
e out of s
!
6
2
10
10
!
4 4
0
15
3
8
4 0
6
4
30
3
5
s
9
9
5
15
0
15
flow
11
6
10
10
10
5
4 0
1
15
15
3
8
4
8
8
6
10
1
capacity
9
2
15 0
t
5
s
capacity
15
flow
14
Value = 24
7
4 0
6
4
30
10
9
8
6
t
10
4
10
10
11
9
0
15 0
10
10
14
Value = 28
7
9
10
Flows and Cuts
Flows and Cuts
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.
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.
" f (e) # " f (e) = v( f )
e out of A
!
e out of A
!
6
2
10
10
5
9
4 4
0
15
3
8
3
s
" f (e) # " f (e) = v( f )
e in to A
0
15
6
4
30
11
6
10
10
10
15 0
11
7
5
4 4
0
15
15
3
8
4 0
6
4
30
3
8
6
1
4 0
9
2
8
A
6
5
15
10
t
s
5
10
11
11
10
8
6
1
15
Value = 24
6
0
8
A
10
e in to A
15 0
11
7
t
10
10
10
Value = 6 + 0 + 8 - 1 + 11
= 24
12
Flows and Cuts
Flows and Cuts
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.
Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then
" f (e) # " f (e) = v( f ) .
e out of A
e in to A
Pf. (by induction on |A|)
Base case: A = { s }.
Inductive hypothesis: assume true for all cuts (A, B) with |A| < k.
!
– consider cut (A', B') with |A'| = k
– A' = A " { v } for some v # {s, t}
– By induction, cap(A, B) = v(f).
– adding v to A increase cut capacity by " f (e) # " f (e) = 0 !
" f (e) # " f (e) = v( f )
e out of A
!
e in to A
!
!
6
9
5
4 4
0
15
15
3
8
2
10
10
3
s
5
6
0
6
1
15
4 0
11
15 0
6
30
t
10
t
10
A
Value = 10 - 4 + 8 - 0 + 10
= 24
7
!
t
v
v
10
11
4
e in to v
8
8
A
e out of v
10
A'
s
s
Before
13
After
Flows and Cuts
14
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.
Weak duality. Let f be any flow. Then, for any s-t cut (A, B) we have
v(f) % cap(A, B).
Pf.
Cut capacity = 30 $
Flow value % 30
v( f ) =
e out of A
2
10
s
5
4
9
15
$
5
15
3
8
6
4
6
15
4
30
7
4
8
e in to A
B
t
" f (e)
e out of A
$
10
" c(e)
s
e out of A
10
= cap( A, B)
!
7
6
t
A
15
A
" f (e) # " f (e)
!
10
Capacity = 30
15
16
Certificate of Optimality
Towards a Max Flow Algorithm
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.
Greedy algorithm.
Start with f(e) = 0 for all edge e ! E.
Find an s-t path P where each edge has f(e) < c(e).
Augment flow along path P.
Repeat until you get stuck.
!
!
Value of flow = 28
!
Cut capacity = 28 $
Flow value % 28
!
1
9
2
9
4
1
15
10
10
0
4
5
s
5
9
15
0
9
8
8
3
6
4
A
15
4 0
14
10
15 0
6
10
30
0
10
s
t
t
30 0
10
10
14
4
0
20
10
20
0
0
Flow value = 0
2
7
17
18
Towards a Max Flow Algorithm
Towards a Max Flow Algorithm
Greedy algorithm.
Start with f(e) = 0 for all edge e ! E.
Find an s-t path P where each edge has f(e) < c(e).
Augment flow along path P.
Repeat until you get stuck.
Greedy algorithm.
Start with f(e) = 0 for all edge e ! E.
Find an s-t path P where each edge has f(e) < c(e).
Augment flow along path P.
Repeat until you get stuck.
!
!
!
!
!
!
!
!
locally optimality $ global optimality
1
20 X
0
0
20
10
30 X
0 20
s
1
20
20
t
10
20
0
X
0 20
s
0
10
greedy = 20
2
19
20
20
t
30 20
10
0
Flow value = 20
1
s
20
20
2
10
10
10
10
opt = 30
t
30 10
20
20
2
20
Residual Graph
Original edge: e = (u, v) ! E.
Flow f(e), capacity c(e).
!
Ford-Fulkerson Algorithm
capacity
u
flow
s
!
4
4
10
2
8
6
10
10
3
9
5
10
capacity
G:
6
Residual edge.
"Undo" flow sent.
e = (u, v) and eR = (v, u).
Residual capacity:
2
v
17
residual capacity
t
!
!
u
$c(e) " f (e) if e # E
c f (e) = %
if e R # E
& f (e)
v
11
6
residual capacity
Residual graph: Gf = (V, Ef ).
!
Residual edges with positive residual capacity.
Ef = {e : f(e) < c(e)} " {eR : c(e) > 0}.
!
!
21
22
Max-Flow Min-Cut Theorem
Proof of Max-Flow Min-Cut Theorem
Augmenting path theorem. Flow f is a max flow iff there are no
augmenting paths.
(iii) $ (i)
Let f be a flow with no augmenting paths.
Let A be set of vertices reachable from s in residual graph.
By definition of A, s ! A.
By definition of f, t & A.
!
!
Max-flow min-cut theorem. [Ford-Fulkerson, 1956] The value of the
max flow is equal to the value of the min cut.
!
!
Proof strategy. We prove both simultaneously by showing the TFAE:
(i) There exists a cut (A, B) such that v(f) = cap(A, B).
(ii) Flow f is a max flow.
(iii) There is no augmenting path relative to f.
v( f ) =
" f (e) # " f (e)
e out of A
=
e in to A
A
" c(e)
B
t
e out of A
= cap( A, B)
(i) $ (ii) This was the corollary to weak duality lemma.
s
!
(ii) $ (iii) We show contrapositive.
Let f be a flow. If there exists an augmenting path, then we can
improve f by sending flow along path.
original network
!
23
24
Augmenting Path Algorithm
Running Time
Assumption. All capacities are integers between 1 and C.
Augment(f, c, P) {
b ' bottleneck(P)
foreach e ! P {
if (e ! E) f(e) ' f(e) + b
else
f(eR) ' f(e) - b
}
return f
}
Invariant. Every flow value f(e) and every residual capacities cf (e)
remains an integer throughout the algorithm.
forward edge
reverse edge
Theorem. The algorithm terminates in at most v(f*) % nC iterations.
Pf. Each augmentation increase value by at least 1.
Corollary. If C = 1, Ford-Fulkerson runs in O(mn) time.
Ford-Fulkerson(G, s, t, c) {
foreach e ! E f(e) ' 0
Gf ' residual graph
Integrality theorem. If all capacities are integers, then there exists a
max flow f for which every flow value f(e) is an integer.
Pf. Since algorithm terminates, theorem follows from invariant.
while (there exists augmenting path P) {
f ' Augment(f, c, P)
update Gf
}
return f
}
25
26
Ford-Fulkerson: Exponential Number of Augmentations
7.3 Choosing Good Augmenting Paths
Q. Is generic Ford-Fulkerson algorithm polynomial in input size?
m, n, and log C
A. No. If max capacity is C, then algorithm can take C iterations.
1
1
0
X
0
C
C
s
Copyright © 2005 Addison Wesley
•
Slides by Kevin Wayne
1
t
1 X
0 1
C
C
0
0 1
X
2
Algorithm Design by Éva Tardos and Jon Kleinberg •
1
X
0
0 1
X
C
C
1 X
0 X
1 0
s
t
C
C
X
0 1
0
1 X
2
28
Choosing Good Augmenting Paths
Capacity Scaling
Use care when selecting augmenting paths.
Some choices lead to exponential algorithms.
Clever choices lead to polynomial algorithms.
If capacities are irrational, algorithm not guaranteed to terminate!
Intuition. Choosing path with highest bottleneck capacity increases
flow by max possible amount.
Don't worry about finding exact highest bottleneck path.
Maintain scaling parameter (.
Let Gf (() be the subgraph of the residual graph consisting of only
arcs with capacity at least (.
!
!
!
!
!
!
Goal: choose augmenting paths so that:
Can find augmenting paths efficiently.
Few iterations.
!
4
4
!
110
Choose augmenting paths with: [Edmonds-Karp, 1972]
Max bottleneck capacity.
Sufficiently large bottleneck capacity.
Fewest number of edges.
102
1
s
t
!
122
!
110
170
102
s
t
122
170
!
2
2
Gf
Gf (100)
29
30
Capacity Scaling
Capacity Scaling: Correctness
Assumption. All edge capacities are integers between 1 and C.
Integrality invariant. All flow and residual capacity values are integral.
Scaling-Max-Flow(G, s, t, c) {
foreach e ! E f(e) ' 0
( ' smallest power of 2 greater than or equal to C
Gf ' residual graph
Correctness. If the algorithm terminates, then f is a max flow.
Pf.
By integrality invariant, when ( = 1 $ Gf(() = Gf.
Upon termination of ( = 1 phase, there are no augmenting paths. !
!
while (( ) 1) {
Gf(() ' (-residual graph
while (there exists augmenting path P in Gf(()) {
f ' augment(f, c, P)
update Gf(()
}
( ' ( / 2
}
return f
!
}
31
32
Capacity Scaling: Running Time
Capacity Scaling: Running Time
Lemma 1. The outer while loop repeats 1 + *log2 C+ times.
Pf. Initially C % ( < 2C. ( decreases by a factor of 2 each iteration. !
Lemma 2. Let f be the flow at the end of a (-scaling phase. Then value
of the maximum flow is at most v(f) + m (.
Pf. (almost identical to proof of max-flow min-cut theorem)
We show that at the end of a (-phase, there exists a cut (A, B)
such that cap(A, B) % v(f) + m (.
Choose A to be the set of nodes reachable from s in Gf(().
By definition of A, s ! A.
By definition of f, t & A.
Lemma 2. Let f be the flow at the end of a (-scaling phase. Then the
proof on next slide
value of the maximum flow is at most v(f) + m (.
!
!
Lemma 3. There are at most 2m augmentations per scaling phase.
Let f be the flow at the end of the previous scaling phase.
L2 $ v(f*) % v(f) + m (2().
Each augmentation in a (-phase increases v(f) by at least (. !
!
!
!
!
v( f ) =
!
e out of A
Theorem. The scaling max-flow algorithm finds a max flow in O(m log C)
augmentations. It can be implemented to run in O(m2 log C) time. !
$
" c(e) #
e out of A
B
t
e in to A
" (c(e) # %) # "
e out of A
=
A
" f (e) # " f (e)
%
e in to A
" % # "%
e out of A
e in to A
s
$ cap(A, B) - m%
original network
33
!
34