Lecture 16
Lecture 16
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.
The max flow problem is to find some flow f such that |f | is maximized.
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.
Here in this case, there is no edge into s. However, when there can be edges into s, we can define |f | more
generally as X X
|f | = f (s, x) − f (y, s)
x∈Nout y∈Nin (s)
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
Proof.
X X
|f | = f (s, x) − f (y, s)
x∈Nout (s) y∈Nin (s)
X X X
= f (v, x) − f (y, v) by the Flow Conservation Constraint all added terms sum to 0
v∈S x∈Nout (v) y∈Nin (v)
X X X X X X
= f (v, x) − f (y, v) + f (v, x) − f (y, v)
v∈S x∈Nout (v)∩S y∈Nin (v)∩S v∈S x∈Nout (v)∩T y∈Nin (v)∩T
X X X
= f (v, x) − f (y, v) first term sum to 0 because everything cancels out
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)
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 maximum. If not, we show how to increase the value of our flow by pushing
some flow on that path.
6 Algorithm
We will make an assumption on our graph. The assumption can be removed, but it will make our lives
easier. We will assume that for all u, v ∈ V , G does not have both (u, v) and (v, u) ∈ E(G). We can make
this condition hold by modifying the original graph. If (u, v), (v, u) ∈ E(G), we split the edge (u, v) to two
edges (u, x) and (x, v), where x is a new node we introduce into the graph. This makes the number of nodes
at most m + n.
Now, let f be a flow given to us. We will try to see if we can improve this flow. We will define the residual
capacity cf : V × V → R+ 0 as follows.
c(u, v) − f (u, v) if (u, v) ∈ E(G)
cf (u, v) = f (v, u) if (v, u) ∈ E(G)
0 otherwise
3
a b
5 5 94
5
s 6
12 t
2
1
11 c 10
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.
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 max 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 t is 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
= 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 |.
4
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 v. Then we can increase our flow by v. 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 v. 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 v. In effect, we are
”undoing” the flow on this edge. By doing so, we have increased our flow by v.
As an example, 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 an additional 2 units of flow, resulting in a flow of 18.
Formally, Let s = x0 → x1 → ... → xk = t be a 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
f 0 (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
The conservation constraints are also satisfied: Suppose that P is simple. Thus, for every v ∈ V , P uses 0
or two edges incident on v. If P uses 0 edges on v, then the edge flow values 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 (an edge opposite to one into v and an edge opposite
to one out of v); 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.
3. (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, |f 0 | > |f | because we just increase our flow by F , and by our definition of Gf , it must be that F > 0.
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 yes, 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.
Here is the max flow algorithm attributed to Ford-Fulkerson.
The runtime of this algorithm is bounded by the number of times we update our flow. If edge capacities are
all integers, then we can increase the flow by at least 1 each time we update our flow. Therefore, the runtime
if 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 a factor as well.
5
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;
return f ;
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 lecture.