CS161 Lecture 17
CS161 Lecture 17
Scribe: Chuanqi Shen (2015), Virginia Williams, Luke Johnston (2016), G. Valiant (2017), M.
Wootters (2017)
Date: June 5, 2017
1. (Capacity Constraint)
∀(u, v) ∈ E, 0 ≤ f (u, v) ≤ c(u, v)
2. (Flow Conservation Constraint)
X X
∀v ∈ V \{s, t}, f (x, v) = f (v, y)
x∈Nin (v) y∈Nout (v)
Here Nin (v) denotes the set of nodes with an edge that points to v and Nout (v) denotes the set of
nodes that v points to.
Suppose that there
P are no edges going Pinto s and no edges coming out of t.PFrom the above, you can
verify yourself that x∈Nout (s) f (s, x) = y∈Nin (t) f (y, t). We define the value x∈Nout (s) f (s, x) to be the
value of the flow f . We usually denote the value of a flow f as |f |. If there are edges going into s and out
of t, then the value of f is X X
|f | = f (s, x) − f (y, s).
x∈Nout (s) y∈Nin (s)
The max flow problem is to find some flow f such that |f | is maximized.
Remark 1. In the analysis below we consider graphs with a single source s and a single sink t. However, if
we need to work with a graph with multiple sources, we can do so by adding a new source node, and then
adding edges with capacity infinity from it to each of the multiple sources. Similarly, if we want to have
multiple sinks, we add a new sink node and add edges from the multiple sinks to that sink with capacity
infinity.
1
5 5
a b a b
10 100 5 6
3 1
s 12 t s 0 t
11 10 11 10
c c
Figure 1: (Left) Graph G with edge capacities (Right) Graph G with a sample flow
3 Example
In Figure 1, we have a graph G and a sample flow f . Observe that the two constraints for a flow are satisfied.
There can be multiple other flows possible that can satisfy the constraints. For our given flow, |f | = 16. The
max flow for this graph is actually 18, as we will see shortly.
For our graph G shown above, if we set S = {s, a, c} and T = {b, t}, then the cost of the cut is c(a, b) +
c(c, b) + c(c, t) = 5 + 3 + 10 = 18. If we take another cut S 0 = {s, c}, T 0 = {a, b, t}, then kS 0 , T 0 k =
c(s, a) + c(c, b) + c(c, t) = 10 + 3 + 10 = 23. Note that we do not consider the edge {a, c} as it is in the
wrong direction (we only consider edges from S 0 to T 0 ).
2
X X X
= f (v, x) − f (y, v) [first term sums to 0]
v∈S x∈Nout (v)∩T y∈Nin (v)∩T
X X
≤ f (v, x) ≤ c(v, x) = kS, T k
v∈S,x∈T,x∈Nout (v) v∈S,x∈T,x∈Nout (v)
P P P
In the proof, v∈S x∈Nout (v)∩S f (v, x) − y∈Nin (v)∩S f (y, v) = 0 since we add and subtract the flow
f (u, v) for every u, v ∈ S such that (u, v) ∈ E.
We get the following consequence.
Corollary 5.1. If we can find f and (S, T ) such that |f | = kS, T k, then f is a max flow and (S, T ) is a min
cut.
It turns out that we can always find such f and (S, T ) for any graph.
Theorem 5.1 (Max-flow min-cut theorem). For any graph G, source s and destination t, the value of the
max flow is equal to the cost of the min cut.
We will show this by coming up with an algorithm. The algorithm will take the graph G and some flow
f that has already been constructed, and create a new graph that is called the residual graph. In this new
graph, the algorithm will try to find a path from s to t. If no such path exists, we will show that the value
of the flow we started with is the value of the maximum flow. If not, we show how to increase the value of
our flow by pushing some flow on that path.
Basically, what this does is that, if there is any flow through the edge, you remove the flow from the
capacity and add an edge in the opposite direction with the value of the flow. The reason we do this is
because the flow we picked thus far might not be the correct flow, and this formulation allows us to undo
changes that we have done. The residual capacity cf (u, v) represents how much flow we can send from u to
v in addition to the flow f .
We define Gf to be a residual network defined with respect to f , where V (Gf ) = V (G) and (u, v) ∈ E(Gf )
if cf (u, v) > 0. Figure 2 shows G with the residual edges.
We will show that, if there is a path from s to t in Gf , then f is not a max flow. If no such path exists,
that f is a max flow.
Lemma 6.1. If t is not reachable from s is Gf , then f is a maximum flow.
Proof. Let S be the set of nodes reachable from s in Gf and T = V \ S. There are no edges in Gf from S
to T since the nodes in T are not reachable from s. Note that (S, T ) defines an s − t cut. Now consider any
v ∈ S, w ∈ T . We have cf (v, w) = 0 since (v, w) is not an edge in Gf . There are three cases:
3
a b
5 5 94
5
s 6
12 t
2
1
11 c 10
= kS, T k
Thus, we show that the flow is equal to the cut. From Corollary 5.1 we know that f is a maximum flow,
and kS, T k is a min cut.
Lemma 6.2. If Gf has a path from s to t, we can modify f to f 0 such that |f | < |f 0 |.
Proof. Pick a path P from s to t in Gf , and consider the edge of minimum capacity on the path. Let that
capacity be F . Then we can increase our flow by F . For each edge in P , if cf (v, w) is the right direction
(i.e there is an edge (v, w) ∈ E(G)), then we can increase our flow on this edge by F . If cf (v, w) is in the
opposite direction (i.e. (w, v) ∈ E(G)), then we can decrease the flow on this edge by F . In effect, we are
“undoing” the flow on this edge. By doing so, we have increased our flow by F .
As an example, consider Figure 2 again. The path s → a → c → b → t is a path with minimum capacity
2. Therefore, we can update our flow and push additional 2 units of flow, resulting in a flow of 18.
Formally, Let s = x0 → x1 → ... → xk = t be a simple path P in Gf , and let F = mini cf (xi , xi+1 ).
Define a new flow f 0 where
f (u, v) + F if (u, v) ∈ P
0
f (u, v) = f (u, v) − F if (v, u) ∈ P
f (u, v) otherwise
We now need to show that f 0 is a flow. The capacity constraints are satisfied because for every (u, v) ∈ E,
1. If (u, v) ∈ P , then 0 ≤ f (u, v) + F ≤ f (u, v) + cf (u, v) = f (u, v) + c(u, v) − f (u, v) = c(u, v)
4
2. If (v, u) ∈ P , then f (u, v) − F ≤ f (u, v) ≤ c(u, v) and f (u, v) − F ≥ f (u, v) − cf (v, u) = 0.
3. Otherwise, f (u, v) is from the original flow f .
The conservation constraints are also satisfied: Recall that P is a simple path. Thus, for every v ∈
V \ {s, t}, P uses 0 or two edges incident on v. If P uses 0 edges on v, then flow values of the edges
incident on v have not changed when going from f to f 0 . Thus, suppose that P uses two edges (x, v) and
(v, y) incident on v. Because in Gf some edges appear in the opposite direction compared to G, we need to
consider a few cases.
1. (x, v) and (v, y) are both in the same direction (an edge into v and an edge out of v); the flow into v
increases by F and the flow out of it also increases by F .
2. (x, v) and (v, y) are both in the opposite direction ((v, x), (y, v) ∈ E); the flow into v decreases by F
and the flow out of it also decreases by F .
3. (x, v) is in the correct direction and (v, y) is in the opposite direction. Then the flow into V changes
by F − F = 0.
4. (x, v) is in the opposite direction and (v, y) is in the correct direction. Then the flow out of V changes
by F − F = 0.
Finally, note that we increase our flow by F . Consider the edge (s, x1 ) in P . If (s, x1 ) ∈ E, the flow out
of s increases by F . If (x1 , s) ∈ E, the flow into s decreases by F . By our definition of Gf , it must be that
F > 0, and we get that |f 0 | > |f |.
From this, we can construct an algorithm to find the maximum flow. Starting with some arbitrary flow
of the graph, construct the residual network, and check if there is a path from s to t. If there is a path,
update the flow, construct the new residual graph and repeat. Otherwise, we have found the max flow.
A path from s to t in the residual graph is called an augmenting path, and pushing flow through it to
modify the current flow is referred to as augmenting along the path.
Algorithm 1: maxflow(G, s, t)
f ← all zeroes flow;
Gf ← G;
while t is reachable from s in Gf (check using DFS) do
P ← path in Gf from s to t;
F ←min capacity on P ;
f ← f 0 as defined in Lemma 6.2;
Update Gf to correspond to new flow;
return f ;
The run time of this algorithm is bounded by the number of times we update our flow. If the edge
capacities are all integers, we can increase the flow by at least 1 each time we update our flow. Therefore,
the runtime is O(|f |m) where |f | is the value of the max flow. If we have rational edge capacities, then
we can multiply all edge capacities by a factor to make them all integers. However, the runtime blows up
by that factor as well. If we have irrational edge capacities, then the algorithm is no longer guaranteed to
terminate. So we have a problem.
We will save the day in the next sections. Algorithm 1 is called the Ford-Fulkerson method. It is actually
part of a family of algorithms that depend on how the path P between s and t in Gf is selected. One
can obtain P via DFS, BFS, or any other method for selecting paths. It turns out that two methods work
particularly well: the shortest path method and the fattest path method. The shortest path method is
known as the Edmonds-Karp algorithm or Dinic’s algorithm. In this class you are only required to know
the above implementation and the running time of the Edmonds-Karp algorithm, but we have included the
details of these two improvements for the interested student.
5
The fattest path method This method finds a path between s and t that maximizes mine∈P cf (e)
among all s − t paths P . Finding such a path can be done in O(m + n) time by a clever mix of linear time
median-finding and DFS.
The shortest path method (the Edmonds-Karp algorithm/Dinic’s algorithm) This method picks
the path between s and t using BFS, thus picking a path that minimizes the number of edges. Finding such
a path also runs in O(m) time: BFS takes O(m + n) to explore the whole graph, but since we only care
about the vertices reachable from s this is O(m) time. The total run time is O(nm2 ).
Since both methods of selecting a path run in linear time, the main question becomes, how many iterations
does Ford-Fulkerson perform? We will answer these questions below in the next section.
Since all the capacities are integers, all the residual capacities are also integers, and so the max flow value
in Gf is an integer. Since it is < 1, it must be 0. Hence after m ln |f | iterations, the max flow value in Gf is
zero, s and t are disconnected and the computed flow in G is maximum. The runtime is O((m + n)m log |f |).
6
7.2 The shortest path version of Ford-Fulkerson
Here we analyze running Ford-Fulkerson using BFS to find a path between s and t in Gf .
With each augmentation along a path P in Gf , at least one edge is removed from Gf , namely the edge
with residual capacity F = mine∈P cf (e). The main claim that we need to prove the runtime is that the
number of times an edge can be removed from Gf is small. Since each iteration of the algorithm causes at
least one removal, the main lemma will show that the number of iterations is small and hence the runtime
is small as well.
Claim 2 (Main). Fix any (u, v) that is ever an edge in Gf . Then the number of times that (u, v) can disappear
from Gf is at most n/2.
Once this claim is proven, we would get that the total number of edge disappearances is at most mn/2
and hence the number of iterations of the algorithm is also ≤ mn/2. Because of this, the algorithm runtime
is O((m + n)mn).
To prove the claim, we will need a useful lemma (see below) that shows that as Gf evolves through the
iterations, for any v, the (unweighted) distance from s to v in Gf cannot go down. Let’s begin with some
notation. Let Gif be the residual network after the ith iteration of the algorithm; G0f = G. For a vertex v,
let di (v) be the (unweighted) distance from s to v in Gif .
/ Gi−1
Case 2: (u, v) ∈ i
f . Then, since (u, v) ∈ Gf , we must have that (v, u) was on the (i − 1)st augmenting
path. Hence di−1 (u) = di−1 (v) + 1. Hence:
7
Thus, between (u, v)’s disappearance and its next reappearance, the distance from s to u increased by +2.
Hence between any two consective disappearances the distance to u increases by ≥ 2. The distance starts
as ≥ 0 and can be ≤ n − 1 before becoming ∞. Thus the total number of disappearances of (u, v) is ≤ n/2.
This completes the proof of the main claim and the proof of the runtime.
8 Applications
We wrap up by talking about some applications of the Ford-Fulkerson algorithm.