Algorithm II
7. Network Flow I
WU Xiaokun 吴晓堃
xkun.wu [at] gmail
A flow network is a tuple G = (V, E, s, t, c).
Intuition. Material flowing through a transportation network; material originates at source and is sent to sink.
Def. An st-cut (cut) is a partition (A, B) of the nodes with s \in A and t \in B.
Def. Its capacity is the sum of the capacities of the edges from A to B.
Min-cut problem. Find a cut of minimum capacity.
Def. An st-flow (flow) f is a function that satisfies:
Def. The value of a flow f is: val(f) = \sum_{e\ \text{out}\ s} f(e) - \sum_{e\ \text{into}\ s} f(e)
Max-flow problem. Find a flow of maximum value.
Greedy algorithm.
Greedy algorithm.
Greedy algorithm.
Greedy algorithm.
Ending flow value = 16
Greedy algorithm.
But max-flow value = 19
Q. Why does the greedy algorithm fail?
A. Once greedy algorithm increases flow on an edge, it
never decreases it.
Ex. Consider flow network G.
Bottom line. Need some mechanism to “undo” a bad decision.
Original edge. e = (u, v) \in E.
Reverse edge. e^{-1} = (v, u).
Residual capacity.
c_f(e) = \left\{ \begin{array}{lcl} c(e) - f(e) & \text{if} & e \in E \\ f(e) & \text{if} & e^{-1} \notin E \\ \end{array}\right.
Residual network. G_f = (V, E_f , s, t, c_f ).
Def. An augmenting path is a simple s \leadsto t path in the residual network G_f.
Def. The bottleneck capacity of an augmenting path P is the minimum residual capacity of any edge in P.
Key property. Let f
be a flow and let P be an augmenting
path in G_f. Then, after calling f' = AUGMENT
(f, c, P), the resulting f' is a flow and val(f') = val(f) + bottleneck(G_f,
P).
Key property. Let f
be a flow and let P be an augmenting
path in G_f. Then, after calling f' = AUGMENT
(f, c, P), the resulting f' is a flow and val(f') = val(f) + bottleneck(G_f,
P).
AUGMENT
(f, c, P)
Ford-Fulkerson augmenting path algorithm.
FORD-FULKERSON
(G)
AUGMENT
(f, c, P);Flow value lemma. Let f be any flow and let (A, B) be any cut. Then, the value of the flow f equals the net flow across the cut (A, B).
val(f) = \sum_{e\ \text{out}\ A} f(e) - \sum_{e\ \text{into}\ A} f(e)
Flow value lemma. Let f be any flow and let (A, B) be any cut. Then, the value of the flow f equals the net flow across the cut (A, B).
val(f) = \sum_{e\ \text{out}\ A} f(e) - \sum_{e\ \text{into}\ A} f(e)
Pf.
\begin{aligned} val(f) & = \sum_{e\ \text{out}\ s} f(e) - \sum_{e\ \text{into}\ s} f(e) \\ & = \sum_{v \in A} (\sum_{e\ \text{out}\ v} f(e) - \sum_{e\ \text{into}\ v} f(e)) \\ & = \sum_{e\ \text{out}\ A} f(e) - \sum_{e\ \text{into}\ A} f(e) \\ \end{aligned}
Weak duality. Let f
be any flow and (A, B) be any cut.
Then, val(f) \le cap(A, B).
Pf.
\begin{aligned} val(f) & = \sum_{e\ \text{out}\ A} f(e) - \sum_{e\ \text{into}\ A} f(e) \\ & \le \sum_{e\ \text{out}\ A} f(e) \\ & \le \sum_{e\ \text{out}\ A} c(e) \\ & = cap(A, B) \\ \end{aligned}
Corollary. Let f be
a flow and let (A, B) be any cut. If
val(f) = cap(A, B), then f is a max flow and (A, B) is a min cut.
Pf.
Max-flow min-cut theorem. [strong duality] Value of a max flow = capacity of a min cut.
Augmenting path theorem. A flow f is a max flow iff no augmenting paths.
Pf. The following three conditions are equivalent for any flow f:
[ A \Rightarrow B ]
Max-flow min-cut theorem. [strong duality] Value of a max flow = capacity of a min cut.
Augmenting path theorem. A flow f is a max flow iff no augmenting paths.
Pf. The following three conditions are equivalent for any flow f:
[ B \Rightarrow C ] We prove contrapositive: \lnot C \Rightarrow \lnot B.
[ C \Rightarrow A ]
\begin{aligned} val(f) & = \sum_{e\ \text{out}\ A} f(e) - \sum_{e\ \text{into}\ A} f(e) \\ & = \sum_{e\ \text{out}\ A} c(e) - 0 \\ & = cap(A, B) \\ \end{aligned}
Theorem. Given any max flow f, can compute a min cut (A, B) in O(m) time.
Pf. Let A = set of
nodes reachable from s in residual
network G_f.
Assumption. Every edge capacity c(e) is an integer between 1 and C.
Integrality invariant. Throughout Ford-Fulkerson,
every edge flow f(e) and residual
capacity c_f(e) is an integer.
Pf. By induction on the number of augmenting paths.
Theorem. Ford-Fulkerson terminates after at most
val(f^\ast) \le nC augmenting paths,
where f^\ast is a max flow.
Pf. Each augmentation increases the value of the flow
by at least 1.
Corollary. The running time of Ford-Fulkerson is
O(m nC).
Pf. Can use either BFS or DFS to find an augmenting
path in O(m) time.
Integrality theorem. There exists an integral max
flow f^\ast.
Pf. Since Ford-Fulkerson terminates, theorem follows
from integrality invariant (and augmenting path theorem).
Q. Is generic Ford-Fulkerson algorithm poly-time in
input size (m, n, \log C)?
A. No. If max capacity is C, then algorithm can take \ge C iterations.
The Ford-Fulkerson algorithm is guaranteed to terminate if the edge capacities are …
A. Rational numbers.
B. Real numbers.
C. Both A and B.
D. Neither A nor B.
Rational = integer / integer
Use care when selecting augmenting paths.
Pathology. When edge capacities can be irrational, no guarantee that Ford-Fulkerson terminates (or converges to a maximum flow)!
Goal. Choose augmenting paths so that:
Goal. Choose augmenting paths so that:
Choose augmenting paths with:
Overview. Choosing augmenting paths with “large” bottleneck capacity.
CAPACITY-SCALING
(G)
AUGMENT
(f, c, P);Assumption. All edge capacities are integers between 1 and C.
Invariant. The scaling parameter \Delta is a power of 2.
Pf. Initially a power of 2; each phase divides \Delta by exactly 2.
Integrality invariant. Throughout the algorithm,
every edge flow f(e) and residual
capacity c_f(e) is an integer.
Pf. Same as for generic Ford-Fulkerson.
Theorem. If capacity-scaling algorithm terminates,
then f is a max flow.
Pf.
Lemma 1. There are 1 +
\lfloor \log_2 C \rfloor scaling phases.
Pf. Initially C/ 2 <
\Delta \le C; \Delta decreases
by a factor of 2 in each iteration.
Lemma 2. Let f be
the flow at the end of a \Delta-scaling
phase. Then, the max-flow value \le val(f) + m
\Delta.
Pf. Next slide.
Lemma 3. There are \le
2m augmentations per scaling phase.
Pf.
Lemma 2. Let f be
the flow at the end of a \Delta-scaling
phase. Then, the max-flow value \le val(f) + m
\Delta.
Pf.
\begin{aligned} val(f) & = \sum_{e\ \text{out}\ A} f(e) - \sum_{e\ \text{into}\ A} f(e) \\ & \ge \sum_{e\ \text{out}\ A} (c(e) - \Delta) - \sum_{e\ \text{into}\ A} \Delta \\ & \ge \sum_{e\ \text{out}\ A} c(e) - \sum_{e\ \text{out}\ A} \Delta - \sum_{e\ \text{into}\ A} \Delta \\ & \ge cap(A, B) m\Delta \\ \end{aligned}
Lemma 1. There are 1 + \lfloor \log_2 C \rfloor scaling phases.
Lemma 2. Let f be the flow at the end of a \Delta-scaling phase. Then, the max-flow value \le val(f) + m \Delta.
Lemma 3. There are \le 2m augmentations per scaling phase.
Theorem. The capacity-scaling algorithm takes O(m^2 \log C) time.
Pf.
Q. How to choose next augmenting path in
Ford-Fulkerson?
A. Pick one that uses the fewest edges (via
BFS).
SHORTEST-AUGMENTING-PATH
(G)
BREADTH-FIRST-SEARCH
(G_f);AUGMENT
(f, c, P);Lemma 1. The length (number of edges) of a shortest
augmenting path never decreases.
Pf. Ahead.
Lemma 2. After at most m shortest-path augmentations, the length of
a shortest augmenting path strictly increases.
Pf. Ahead.
Theorem. The shortest-augmenting-path algorithm
takes O(m^2 n) time.
Pf.
Def. Given a digraph G = (V, E) with source s, its level graph is defined by:
Key property. P is a shortest s \leadsto v path in G iff P is an s \leadsto v path in L_G.
Lemma 1. The length of a shortest augmenting path never decreases.
Lemma 2. After at most m shortest-path augmentations, the length of a shortest augmenting path strictly increases.
Lemma 1. The length (number of edges) of a shortest augmenting path never decreases.
Lemma 2. After at most m shortest-path augmentations, the length of a shortest augmenting path strictly increases.
Theorem. The shortest-augmenting-path algorithm takes O(m^2 n) time.
Note. Θ(m n) augmentations necessary for some flow networks.
Two types of augmentations.
Phase of normal augmentations.
INITIALIZE
(G, f)
ADVANCE
(s);RETREAT
(v)
ADVANCE
(u);ADVANCE
(v)
AUGMENT
(P);
ADVANCE
(s);ADVANCE
(w);RETREAT
(v);How to compute the level graph L_G efficiently?
A. Depth-first search.
B. Breadth-first search.
C. Both A and B.
D. Neither A nor B.
Lemma. A phase can be implemented to run in O(m n) time.
Pf.
Theorem. [Dinitz 1970] Dinitz’ algorithm runs in
O(mn^2) time.
Pf.
Push-relabel algorithm. [Goldberg-Tarjan 1988] Increases flow one edge at a time instead of one augmenting path at a time.
Caveat. Worst-case running time is generally not useful for predicting or comparing max-flow algorithm performance in practice.
Best in practice. Push-relabel method with gap relabeling: O(m^{3/2}) in practice.
Computer vision. Different algorithms work better for some dense problems that arise in applications to computer vision.
Which max-flow algorithm to use for bipartite matching?
A. Ford-Fulkerson: O(m n
C).
B. Capacity scaling: O(m^2
log C).
C. Shortest augmenting path: O(m^2 n).
D. Dinitz’ algorithm: O(m
n^2).
D. the graph may be dense.
Def. A flow network is a simple unit-capacity network if:
Ex. Bipartite matching.
Property. Let G be a simple unit-capacity network and let f be a 0-1 flow. Then, residual network G_f is also a simple unit-capacity network.
Shortest-augmenting-path algorithm.
Theorem. [Even-Tarjan 1975] In simple unit-capacity
networks, Dinitz’ algorithm computes a maximum flow in O(m n^{1/2}) time.
Pf.
Lemma 3. After \le
n^{1/2} additional augmentations, flow is optimal.
Pf. Each augmentation increases flow value by at least
1.
Lemma 1 and Lemma 2. Ahead.
Phase of normal augmentations.
Phase of normal augmentations.
Lemma 1. A phase of normal augmentations takes O(m) time.
Pf.
Consider running advance-retreat algorithm in a unit-capacity network (but not necessarily a simple one). What is running time?
A. O(m).
B. O(m^{3/2}).
C. O(m n).
D. May not terminate.
Hint: both indegree and outdegree of a node can be larger than 1.
A. may take m operations per node
Lemma 2. After n^{1/2} phases, val(f) \ge val(f^\ast) - n^{1/2}.
Lemma 2. After n^{1/2} phases, val(f) \ge val(f^\ast) - n^{1/2}.
Theorem. [Even-Tarjan 1975] In simple unit-capacity
networks, Dinitz’ algorithm computes a maximum flow in O(m n^{1/2}) time.
Pf.
Corollary. Dinitz’ algorithm computes max-cardinality bipartite matching in O(m n^{1/2}) time.