0% found this document useful (0 votes)
2 views

CS161 Lecture 17

A&D part 1

Uploaded by

minhb2203567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS161 Lecture 17

A&D part 1

Uploaded by

minhb2203567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CS 161 Lecture 17 Max Flow, Ford-Fulkerson Algorithm

Scribe: Chuanqi Shen (2015), Virginia Williams, Luke Johnston (2016), G. Valiant (2017), M.
Wootters (2017)
Date: June 5, 2017

1 History of Flows and Cuts


Today we will continue the theme of studying cuts in graphs. In particular we will be studying a very
interesting problem called the max flow problem. Before that, a short history lesson. During the Cold
War, the US Air Force at that time was very interested in the Soviet train networks. In reports that were
declassified in 1999, it was revealed that the Air Force collected enough information about the train network
that they were able to determine how resources were shipped from the Soviet Union to Europe. The Air
Force was very interested in determining how much resources can be transported from the Soviet Union
to Europe, and what needed to be done to destroy this movement of resources. What this translates to is
the min cut problem, i.e., cut the minimum number of train tracks so that nothing goes to Europe. Here,
cutting an edge means dropping a bomb. Nowadays, however, there are much milder (but still important!)
applications of this problem, for instance, understanding the flow of information through the Internet.

2 Formulation of the Maximum Flow Problem


You are given an input graph G = (V, E), where the edges are directed. There is a function c : E → R+
that defines the capacity of each edge. We also label two nodes, s and t in G, as the source and destination,
respectively. The task is to output a flow of maximum value. We will shortly define what a flow is and what
a flow of maximum value means.
A flow f is a function f : E → R+0 such that

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.

4 Formulation of the Minimum Cut Problem


Now, we give a formulation of the min cut problem defined for directed graphs with source and destination
nodes s and t. (There is also a version of the Min-Cut problem without a source and sink node, though we
won’t discuss that now.) S
An s − t cut is a partition V = S T where S and T are disjoint and s ∈ S, t ∈ T , and the size/cost of
an s − t cut is X
kS, T k = c(x, y)
x∈S,y∈T

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 ).

5 The Max-Flow Min-Cut Theorem


Lemma 5.1. For any flow f and any s − t cut (S, T ) of G, we have |f | ≤ kS, T k. In particular, the value of
the max flow is at most the value of the min cut.
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

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.

6 The Ford-Fulkerson Max-Flow 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 edges (u, v) and (v, u) in E. We can make
this condition hold by modifying the original graph in the following way. If (u, v), (v, u) ∈ E, 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

cf (u, v) = f (v, u) if (v, u) ∈ E

0 otherwise

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

Figure 2: The residual network given the flow presented in Figure 1

1. If (v, w) ∈ E, then by definition cf (v, w) = c(v, w) − f (v, w) = 0 =⇒ c(v, w) = f (v, w).


2. If (w, v) ∈ E, then cf (v, w) = f (w, v) = 0.
3. If (v, w) ∈
/ E and (w, v) ∈
/ E, we can disregard (v, w) and (w, v) since they do not appear in any flow
or cut.

Using this, and the proof in Lemma 5.1, we have


 
X X X
|f | =  f (v, x) − f (y, v)
v∈S x∈Nout (v)∩T y∈Nin (v)∩T
X X
= f (v, x) [from case 2 the second term is 0]
v∈S x∈Nout (v)∩T
X X
= c(v, x) [from case 1]
v∈S x∈Nout (v)∩T

= 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.

7 Running time of various implementations of Ford-Fulkerson


NOTE: we did not discuss the details of this section in class, but it’s in the notes for the
interested reader.

7.1 The fattest path version of Ford-Fulkerson


In this section we will show that the fattest path method results in a runtime of O(m(m+n) log |f |) when run
on a graph with integer capacities. Thus, when rational capacities are converted to integers by multiplying
by N , we get a runtime of O(m(m + n)(log |f | + log N )) for rational capacities. Thus the effect of large N
is mitigated. This method does not solve the issues when the capacities can be irrational.
To show the runtime, we prove a main claim that states that after each iteration of the algorithm, the
maximum flow value in Gf goes down by a factor of (1 − 1/m). This max flow value starts as |f | since
Gf = G in the beginning of the algorithm, and ends at 0 as in the end s and t are disconnected.
Claim 1 (Main). Let f 0 be the max flow in Gf . Then after one iteration of Ford-Fulkerson on Gf , the max
flow value becomes ≤ |f 0 |(1 − 1/m)..
Proof. Let P be the fattest path from s to t in Gf . Let F = mine∈P cf (e). Let S be the nodes reachable
from s in Gf via paths composed of edges with residual capacities > F . Thus, any edge (x, y) of Gf with
x ∈ S, y ∈
P / S must have cf (x, y) ≤ F . In particular, this means that the size of the cut between S and V § is
x∈S,y∈V \S cf (x, y) ≤ mF . Thus, the size of the min s-t cut in Gf is at most mF .
By the max-flow-min-cut theorem from last lecture, the size of the min s − t cut is at least the size of
the max-flow |f 0 | in Gf , and so |f 0 | ≤ mF . Thus F ≥ |f 0 |/m.
Now, when we augment (push flow) along P , the flow in G increases by F , while the flow in Gf decreases
by F . Thus, the new flow in Gf after augmenting along P becomes |f 0 | − F ≤ |f 0 |(1 − 1/m). 
Now that the main claim has been proven, we can conclude with a discussion of the runtime. Consider
how the max flow value in Gf evolves after t iterations. It starts as |f | (where f is the max flow in G) and
then after t iterations is
t
≤ |f | (1 − 1/m) .
If t = m ln |f |, we het that the max flow value in Gf is
ln |f |
≤ |f | ((1 − 1/m)m ) < |f |(1/e)ln |f | = 1.

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 .

Lemma 7.1. For all i ≥ 1, and all v ∈ V , di−1 (v) ≤ di (v).


Proof. Fix i. We will prove the statement for i by induction on d = di (v).
The inductive hypothesis is that for all d and all v with di (v) = d, di−1 (v) ≤ di (v). The base case is
d = 0. We note that if di (v) = 0, then v = s since we view Gif as an unweighted graph. But then we also
have di−1 (s) = 0 ≤ di (s).
For the induction, let’s assume that the inductive hypothesis holds for d − 1, i.e. that for all x with
di (x) = d − 1, di−1 (x) ≤ di (x). We want to show that for all v with di (v) = d, we also have di−1 (v) ≤ di (v).
Consider some v with di (v) = d. Let u be the node just before v on a shortest s − v path in Gif . Then,
di (u) = di (v) − 1 = d − 1 and the inductive hypothesis applies to it so that di−1 (u) ≤ di (u).
We consider two cases.
Case 1: (u, v) ∈ Gi−1 i−1
f . Then, by the triangle inequality in Gf , we have that di−1 (v) ≤ di−1 (u) + 1.
Since di−1 (u) ≤ di (u), we get that

di−1 (v) ≤ di (u) + 1 = (di (v) − 1) + 1 = di (v).

/ 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:

di−1 (v) = di−1 (u) − 1 ≤ di (u) − 1 = di (v) − 2 ≤ di (v).

In both cases di−1 (v) ≤ di (v) and the induction is complete. 


Now we are ready to prove the main claim.
Fix some (u, v) that is an edge in Gf at some point. Let’s consider two consecutive disappearences of
(u, v). Suppose that (u, v) ∈ Gi but (u, v) ∈ / Gi+1 . If after this disappearance (u, v) had another one later
on, then at some point (u, v) must have appeared in Gf again. Let j be the first iteration after i so that the
jth augmenting path made (u, v) appear in Gj+1 f .
/ Gi+1
Because (u, v) ∈ Gif but (u, v) ∈ f , (u, v) must have been in the ith augmenting path Pi .
j j+1
Because (u, v) ∈
/ Gf but (u, v) ∈ Gf , (v, u) must have been in the jth augmenting path Pj .
From this we obtain that di (v) = di (u) + 1 and dj (u) = dj (v) + 1. Using the fact that j > i and the key
lemma from above we obtain

dj (u) = dj (v) + 1 ≥ di (v) + 1 = di (u) + 2.

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.

8.1 Bipartite perfect matching


Let G = (V, E) be an undirected, unweighted bipartite graph: the set of vertices is partitioned into V1 and V2
so that there are no edges with two endpoints entirely in V1 or entirely in V2 . A matching in G is a collection
of edges, no two of which share an end point. A perfect matching is a matching M such that every node in
V has exactly one incident edge in M . In order for G to have a perfect matching, we need that |V1 | = |V2 |.
The perfect matching problem is, given a bipartite graph G with |V1 | = |V2 | = n and on m edges, determine
whether G has a perfect matching.
We will solve the bipartite perfect matching problem by creating an instance of max flow and using
Ford-Fulkerson’s algorithm.
Given G = (V1 ∪ V2 , E), direct all the edges in E from V1 to V2 . Add two extra nodes s and t. Add
(directed) edges from s to every node in V1 and from every node of V2 to t. In this new graph H, let all the
edge capacities be 1 and then run Ford-Fulkerson’s algorithm to compute the max flow.
Suppose that G has a perfect matching M . Then, H has max flow value n = |V1 | = |V2 |. This is because
we can set f (e) = 1 for every e ∈ M , ell the edges out of s and all the edges out of t. All other flow values
are 0. The capacity constraints are trivially satisfied. The flow conservation constraints are satisfied since
for every x ∈ V1 there is exactly one edge (s, x) into x that has flow 1, and exactly one edge (x, y) ∈ M with
flow 1; similarly for every x ∈ V2 there is exactly one edge (x, t) out of x that has flow 1, and exactly one
edge (y, x) ∈ M with flow 1.
Suppose now that Ford-Fulkerson returns a flow f of value n. Hence f (s, x) = f (y, t) = 1 for all
x ∈ V1 , y ∈ V2 . Because Ford-Fulkerson causes all flow values on the edges to be integers, the flow values on
all edges are either 1 or 0. Because of this, every node x ∈ V1 gets flow of 1 going into it and a flow of 1
needs to come out so that 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. The edges
in V1 ∪ V2 with positive flow through them must hence form a perfect matching.

8.2 More applications


There are many applications of max-flow and min-cut! We may talk about a few more in class if time (check
the slides), and also check out Section 7.7 of Kleinberg and Tardos.

You might also like