Lecture 16
Lecture 16
1 Max flow
Consider a directed graph G with positive edge weights c that define the “capacity” of each
edge. We can identify two special nodes in G: the source node s and the sink node t. We
want to find a max flow from s to t (we will explain what that is in a bit).
Real-life example: Suppose we want to send trucks from city s to city t, and the capacity
of each edge represents the number of trucks that can go on that road every hour. What is
the maximum number of trucks that we can send from s to t every hour?
where Nin is the set of nodes that have edges pointing to v, and Nout is the set of nodes
that v points to.
In the truck example, this means that we cannot have any more trucks on a road than the
road can hold, and the number of trucks going into a city has to equal the number of trucks
coming out of the city (which makes sense because otherwise the city would run out of trucks
or the trucks would build up).
1
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
The value |f | of the flow is defined as the total flow coming out of the source (which is the
same as the total flow coming into the sink). That is,
X X
|f | = f (s, x) − f (y, s)
x∈Nout (s) y∈Nin (s)
In the max-flow problem, our goal is to find the flow with the maximum value. That is, we
want to find the best way to send trucks from s to t.
Note that when computing the size of a cut, we only consider edges going from S to T , not
the other way around.
The min s-t cut is the s-t cut with the smallest size.
Theorem: For any graph G, source s, and destination t, the value of the max flow is equal
to the size of the min cut.
First we will prove that the value of the max flow is less than or equal to the size of the min
cut. Then we will find an algorithm that finds a flow whose value is equal to the size of the
min cut, proving the theorem.
2
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
Lemma: For any flow f and any s-t cut (S, T ), we have |f | ≤ ||S, T ||. (This proves that
the value of the max flow is less than or equal to the size of the min cut.)
Proof: By definition, we have
X X
|f | = f (s, x) − f (y, s)
x∈Nout (s) y∈Nin (s)
To simplify this expression, we can add a bunch of extraneous terms that sum to zero, to
get
X X X
f (v, x) − f (y, v)
v∈S x∈Nout (v) y∈Nin (v)
Observe that the first two sums cancel out, because we are summing over all vertices in S,
and each edgePfromPa vertex in S to another vertex in S is represented
P P exactly twice: once
in the term v∈S x∈Nout (v)∩S f (v, x), and once in the term v∈S x∈Nin (v)∩S f (y, v). So
this actually just equals
X X X
f (v, x) − f (y, v)
v∈S x∈Nout (v)∩T y∈Nin (v)∩T
3 Ford Fulkerson
This is the method we will use to find a flow whose value equals the value of a cut (i.e. the
max flow). Key to this method is the notion of a “residual graph”, which is based on the
flow that is currently going through the graph, and says how much more flow can be pushed
through the graph on each edge.
3
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
The logic behind this definition is that if u → v is an edge in the original graph, and
we are only pushing f (u, v) units of flow through it right now, we can potentially push
c(u, v) − f (u, v) more units through that edge. However, if the edge points from v → u, we
can push f (u, v) more units in the u → v direction by just removing the flow in the v → u
direction.
We define Gf as the graph that contains the same vertices as the original graph, but only
contains the edges where cf > 0.
First we will show that if t is not reachable from s in Gf , then f is a maximum flow. Then,
if Gf has a path from s to t, we will show how to “augment” f to get a flow f 0 with greater
value.
4
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
Based on this, we know that we can find a maximum flow by repeatedly augmenting the
flow until we can no longer find a path in the residual graph from s to t.
Proof: Define an s-t cut S ∪ T in the original graph G, where S is the set of nodes that are
reachable from s in Gf , and T is the set of nodes that are not. Observe that there are no
edges in Gf between nodes in S and nodes in T , because otherwise one of the nodes in T
would be reachable from s. So for all u ∈ S, v ∈ T , we have cf (u, v) = 0.
This means there are three cases:
1. If (u, v) is an edge in the original graph, then cf (u, v) = c(u, v) − f (u, v) = 0, so
c(u, v) = f (u, v).
2. If (v, u) is an edge in the original graph, then cf (u, v) = f (v, u) = 0.
3. If neither (u, v) nor (v, u) are edges, we can completely disregard this pair of vertices
since they do not appear in any flow or cut.
Therefore,
X X X
|f | = f (u, x) − f (y, u) (from the previous proof)
u∈S x∈Nout (u)∩T y∈Nin (u)∩T
X X X
= c(u, x) − 0
u∈S x∈Nout (u)∩T y∈Nin (u)∩T
= ||S, T ||
Since the flow is equal to the cut, by the previous lemma, f must be a max flow.
Let P = x0 → x1 → · · · → xk be the path from s to t in the residual graph (note that this may
not necessarily correspond to a path in the real graph), and consider the edge of minimum
capacity in the residual graph. Let F be the weight of this edge, i.e. mini cf (xi , xi+1 ).
We may increase the overall flow in the graph by augmenting the flow by F . That is, if there
is an edge from xi → xi+1 in the original graph, we increase the flow on that edge by F . If
there is an edge from xi+1 to xi , we decrease the flow on that edge by F . Note that we can
always do this because of the definition of capacity in the residual graph.
5
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
(Note that f 0 is only defined on edges that were in the original graph, so the (v, u) ∈ P case is
the case where the edge in the residual graph points in the opposite direction of the original
edge, and the (u, v) ∈ P case is the case where they are pointing in the same direction.)
We need to show that f 0 is a flow with greater value than f .
Capacity constraints:
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).
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 (u, v) =
0.
3. Otherwise, f 0 (u, v) = f (u, v), which is between 0 and c(u, v).
Conservation constraints: Suppose that P is a simple path. Then for every vertex v
other than s and t, v either touches 0 edges of P or 2 edges of P . If it touches 0 edges, the
6
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
flow is conserved. If it touches 2 edges, then we consider four cases, depending on whether
the edge in Gf is the same direction as the corresponding edge in G, or whether it is in the
opposite direction.
Exercise: Verify that in all these cases, the flow is conserved.
Finally, |f 0 | > f because we are increasing our flow by F , which is greater than 0.
Therefore, we can create a maximum flow by repeatedly augmenting paths in the flow network
until there is no longer any path from s to t in the residual network. We can find the path
in the residual network using some path finding method, like breadth-first search.
Runtime for arbitrary path finding method: If all the capacities are integers, we must
augment the flow by at least 1 every time, so the runtime is bounded by O(m|f |), where |f |
is the value of the maximum flow. However, this is inefficient when the flow is large (see the
figure), or the capacities are not integers. In fact, if the capacities are irrational numbers,
the algorithm is not guaranteed to terminate.
Good path finding method: However, if we choose the path using breadth-first search
(each time choosing the shortest unweighted path from s to t in the residual network), the
algorithm is guaranteed to run in polynomial time. Specifically, it runs in O(nm2 ) time.
When the Ford Fulkerson method is implemented using breadth-first search, it is called the
Edmonds-Karp algorithm.
7
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
8
Lecture 16 – Max Flow Jessica Su (some parts copied from CLRS / last quarter’s notes)
Now suppose Ford-Fulkerson returns a flow of value n, so that f (s, x) = f (y, t) = 1 for all
x ∈ V1 and y ∈ V2 . Since Ford-Fulkerson causes all flow values on the edges to be integers
(when the capacities are all integers), the flow values on all edges are either 1 or 0. Therefore,
every node x ∈ V1 gets a flow of 1 going into it and a flow of 1 needs to come out, so there
is a single edge (x, y) that has flow value 1 and all other edges out of x have flow value 0.
Similarly, for every y ∈ V2 there is a unique edge into y with positive flow value 1. Then the
edges in V1 ∪ V2 with positive flow form a perfect matching.