Design and Analysis of Algos
Design and Analysis of Algos
edge is not in the residual graph. In the first case, we clearly need to relabel v
before applying a push on this edge. In the latter case, one needs to apply push
to the reverse edge (w, v) to make (v, w) reenter the residual graph. However,
when we apply push to edge (w, v), then w is above v, and so v needs to be
relabeled before one can push flow from v to w again.
Since edges do not have to be considered again for push before relabeling,
we get the following.
(7.32) When the current(v) pointer reaches the end of the edge list for v,
the relabel operation can be applied to node v.
After relabeling node v, we reset current(v) to the first edge on the list and
start considering edges again in the order they appear on v’s list.
Proof. The initial flow and relabeling is set up in O(m) time. Both push and
relabel operations can be implemented in O(1) time, once the operation
has been selected. Consider a node v. We know that v can be relabeled at
most 2n times throughout the algorithm. We will consider the total time the
algorithm spends on finding the right edge on which to push flow out of node v,
between two times that node v gets relabeled. If node v has dv adjacent edges,
then by (7.32) we spend O(dv ) time on advancing the current(v) pointer
between consecutive relabelings of v. Thus the total time spent on advancing
the current pointers throughout the algorithm is O( v∈V ndv ) = O(mn), as
claimed.
The Problem
One of our original goals in developing the Maximum-Flow Problem was to
be able to solve the Bipartite Matching Problem, and we now show how to
do this. Recall that a bipartite graph G = (V , E) is an undirected graph whose
node set can be partitioned as V = X ∪ Y, with the property that every edge
e ∈ E has one end in X and the other end in Y. A matching M in G is a subset
of the edges M ⊆ E such that each node appears in at most one edge in M.
The Bipartite Matching Problem is that of finding a matching in G of largest
possible size.
s t
(a) (b)
Figure 7.9 (a) A bipartite graph. (b) The corresponding flow network, with all capacities
equal to 1.
7.5 A First Application: The Bipartite Matching Problem 369
Proof. To prove this, consider the cut (A, B) in G with A = {s} ∪ X. The value
of the flow is the total flow leaving A, minus the total flow entering A. The
first of these terms is simply the cardinality of M , since these are the edges
leaving A that carry flow, and each carries exactly one unit of flow. The second
of these terms is 0, since there are no edges entering A. Thus, M contains k
edges.
Proof. To prove this, suppose x ∈ X were the tail of at least two edges in M .
Since our flow is integer-valued, this means that at least two units of flow
leave from x. By conservation of flow, at least two units of flow would have
to come into x—but this is not possible, since only a single edge of capacity 1
enters x. Thus x is the tail of at most one edge in M .
(7.37) The size of the maximum matching in G is equal to the value of the
maximum flow in G ; and the edges in such a matching in G are the edges that
carry flow from X to Y in G .
370 Chapter 7 Network Flow
Note the crucial way in which the integrality theorem (7.14) figured in
this construction: we needed to know if there is a maximum flow in G that
takes only the values 0 and 1.
Bounding the Running Time Now let’s consider how quickly we can com-
pute a maximum matching in G. Let n = |X| = |Y|, and let m be the number
of edges of G. We’ll tacitly assume that there is at least one edge incident to
each node in the original problem, and hence m ≥ n/2. The time to compute
a maximum matching is dominated by the time to compute an integer-valued
maximum flow in G , since converting this to a matching in G is simple. For
this flow problem, we have that C = e out of s ce = |X| = n, as s has an edge
of capacity 1 to each node of X. Thus, by using the O(mC) bound in (7.5), we
get the following.
It’s interesting that if we were to use the “better” bounds of O(m2 log2 C) or
O(n3) that we developed in the previous sections, we’d get the inferior running
times of O(m2 log n) or O(n3) for this problem. There is nothing contradictory
in this. These bounds were designed to be good for all instances, even when C
is very large relative to m and n. But C = n for the Bipartite Matching Problem,
and so the cost of this extra sophistication is not needed.
It is worthwhile to consider what the augmenting paths mean in the
network G . Consider the matching M consisting of edges (x2 , y2), (x3, y3),
and (x5 , y5) in the bipartite graph in Figure 7.1; see also Figure 7.10. Let f
be the corresponding flow in G . This matching is not maximum, so f is not
a maximum s-t flow, and hence there is an augmenting path in the residual
graph Gf . One such augmenting path is marked in Figure 7.10(b). Note that
the edges (x2 , y2) and (x3, y3) are used backward, and all other edges are used
forward. All augmenting paths must alternate between edges used backward
and forward, as all edges of the graph G go from X to Y. Augmenting paths
are therefore also called alternating paths in the context of finding a maximum
matching. The effect of this augmentation is to take the edges used backward
out of the matching, and replace them with the edges going forward. Because
the augmenting path goes from s to t, there is one more forward edge than
backward edge; thus the size of the matching increases by one.
7.5 A First Application: The Bipartite Matching Problem 371
x1 y1 x1 y1 x1 y1
x2 y2 x2 y2 x2 y2
x3 y3 x3 y3 x3 y3
x4 y4 x4 y4 x4 y4
x5 y5 x5 y5 x5 y5
Figure 7.10 (a) A bipartite graph, with a matching M. (b) The augmenting path in the
corresponding residual graph. (c) The matching obtained by the augmentation.
that are adjacent to nodes in A. If the graph has a perfect matching, then each
node in A has to be matched to a different node in (A), so (A) has to be at
least as large as A. This gives us the following fact.
(7.40) Assume that the bipartite graph G = (V , E) has two sides X and Y
such that |X| = |Y|. Then the graph G either has a perfect matching or there is
a subset A ⊆ X such that |(A)| < |A|. A perfect matching or an appropriate
subset A can be found in O(mn) time.
Proof. We will use the same graph G as in (7.37). Assume that |X| = |Y| = n.
By (7.37) the graph G has a maximum matching if and only if the value of the
maximum flow in G is n.
We need to show that if the value of the maximum flow is less than n,
then there is a subset A such that |(A)| < |A|, as claimed in the statement.
By the Max-Flow Min-Cut Theorem (7.12), if the maximum-flow value is less
than n, then there is a cut (A , B) with capacity less than n in G . Now the
set A contains s, and may contain nodes from both X and Y as shown in
Figure 7.11. We claim that the set A = X ∩ A has the claimed property. This
will prove both parts of the statement, as we’ve seen in (7.11) that a minimum
cut (A , B) can also be found by running the Ford-Fulkerson Algorithm.
First we claim that one can modify the minimum cut (A , B) so as to
ensure that (A) ⊆ A, where A = X ∩ A as before. To do this, consider a node
y ∈ (A) that belongs to B as shown in Figure 7.11(a). We claim that by moving
y from B to A, we do not increase the capacity of the cut. For what happens
when we move y from B to A? The edge (y, t) now crosses the cut, increasing
the capacity by one. But previously there was at least one edge (x, y) with
x ∈ A, since y ∈ (A); all edges from A and y used to cross the cut, and don’t
anymore. Thus, overall, the capacity of the cut cannot increase. (Note that we
7.6 Disjoint Paths in Directed and Undirected Graphs 373
A
A� A�
s x y t s x y t
(a) (b)
Figure 7.11 (a) A minimum cut in proof of (7.40). (b) The same cut after moving node
y to the A side. The edges crossing the cut are dark.
don’t have to be concerned about nodes x ∈ X that are not in A. The two ends
of the edge (x, y) will be on different sides of the cut, but this edge does not
add to the capacity of the cut, as it goes from B to A.)
Next consider the capacity of this minimum cut (A , B) that has (A) ⊆ A
as shown in Figure 7.11(b). Since all neighbors of A belong to A, we see that
the only edges out of A are either edges that leave the source s or that enter
the sink t. Thus the capacity of the cut is exactly
Notice that |X ∩ B| = n − |A|, and |Y ∩ A| ≥ |(A)|. Now the assumption that
c(A , B) < n implies that
Comparing the first and the last terms, we get the claimed inequality |A| >
|(A)|.
A
A� A�
s x y t s x y t
(a) (b)
Figure 7.11 (a) A minimum cut in proof of (7.40). (b) The same cut after moving node
y to the A side. The edges crossing the cut are dark.
don’t have to be concerned about nodes x ∈ X that are not in A. The two ends
of the edge (x, y) will be on different sides of the cut, but this edge does not
add to the capacity of the cut, as it goes from B to A.)
Next consider the capacity of this minimum cut (A , B) that has (A) ⊆ A
as shown in Figure 7.11(b). Since all neighbors of A belong to A, we see that
the only edges out of A are either edges that leave the source s or that enter
the sink t. Thus the capacity of the cut is exactly
Notice that |X ∩ B| = n − |A|, and |Y ∩ A| ≥ |(A)|. Now the assumption that
c(A , B) < n implies that
Comparing the first and the last terms, we get the claimed inequality |A| >
|(A)|.
the sink. From this more dynamic view of flows, we will arrive at something
called the s-t Disjoint Paths Problem.
The Problem
In defining this problem precisely, we will deal with two issues. First, we will
make precise this intuitive correspondence between units of flow traveling
along paths, and the notion of flow we’ve studied so far. Second, we will
extend the Disjoint Paths Problem to undirected graphs. We’ll see that, despite
the fact that the Maximum-Flow Problem was defined for a directed graph, it
can naturally be used also to handle related problems on undirected graphs.
We say that a set of paths is edge-disjoint if their edge sets are disjoint, that
is, no two paths share an edge, though multiple paths may go through some
of the same nodes. Given a directed graph G = (V , E) with two distinguished
nodes s, t ∈ V, the Directed Edge-Disjoint Paths Problem is to find the maximum
number of edge-disjoint s-t paths in G. The Undirected Edge-Disjoint Paths
Problem is to find the maximum number of edge-disjoint s-t paths in an
undirected graph G. The related question of finding paths that are not only
edge-disjoint, but also node-disjoint (of course, other than at nodes s and t)
will be considered in the exercises to this chapter.
not only give us the maximum number of edge-disjoint paths, but the paths
as well.
(7.42) If f is a 0-1 valued flow of value ν, then the set of edges with flow
value f (e) = 1 contains a set of ν edge-disjoint paths.
Proof. We prove this by induction on the number of edges in f that carry flow.
If ν = 0, there is nothing to prove. Otherwise, there must be an edge (s, u) that
carries one unit of flow. We now “trace out” a path of edges that must also
carry flow: Since (s, u) carries a unit of flow, it follows by conservation that
there is some edge (u, v) that carries one unit of flow, and then there must be
an edge (v, w) that carries one unit of flow, and so forth. If we continue in this
way, one of two things will eventually happen: Either we will reach t, or we
will reach a node v for the second time.
If the first case happens—we find a path P from s to t—then we’ll use this
path as one of our ν paths. Let f be the flow obtained by decreasing the flow
values on the edges along P to 0. This new flow f has value ν − 1, and it has
fewer edges that carry flow. Applying the induction hypothesis for f , we get
ν − 1 edge-disjoint paths, which, along with path P, form the ν paths claimed.
If P reaches a node v for the second time, then we have a situation like
the one pictured in Figure 7.12. (The edges in the figure all carry one unit of
flow, and the dashed edges indicate the path traversed so far, which has just
reached a node v for the second time.) In this case, we can make progress in
a different way.
Consider the cycle C of edges visited between the first and second appear-
ances of v. We obtain a new flow f from f by decreasing the flow values on
the edges along C to 0. This new flow f has value ν, but it has fewer edges that
carry flow. Applying the induction hypothesis for f , we get the ν edge-disjoint
paths as claimed.
376 Chapter 7 Network Flow
t
P
s
Figure 7.12 The edges in the figure all carry one unit of flow. The path P of dashed
edges is one possible path in the proof of (7.42).
Notice also how the proof of (7.42) provides an actual procedure for
constructing the k paths, given an integer-valued maximum flow in G. This
procedure is sometimes referred to as a path decomposition of the flow, since it
“decomposes” the flow into a constituent set of paths. Hence we have shown
that our flow-based algorithm finds the maximum number of edge-disjoint s-t
paths and also gives us a way to construct the actual paths.
Bounding the Running Time For this flow problem, C = e out of s ce ≤
|V| = n, as there are at most |V| edges out of s, each of which has capac-
ity 1. Thus, by using the O(mC) bound in (7.5), we get an integer maximum
flow in O(mn) time.
The path decomposition procedure in the proof of (7.42), which produces
the paths themselves, can also be made to run in O(mn) time. To see this, note
that this procedure, with a little care, can produce a single path from s to t
using at most constant work per edge in the graph, and hence in O(m) time.
Since there can be at most n − 1 edge-disjoint paths from s to t (each must
use a different edge out of s), it therefore takes time O(mn) to produce all the
paths.
In summary, we have shown
A Version of the Max-Flow Min-Cut Theorem for Disjoint Paths The Max-
Flow Min-Cut Theorem (7.13) can be used to give the following characteri-
7.6 Disjoint Paths in Directed and Undirected Graphs 377
zation of the maximum number of edge-disjoint s-t paths. We say that a set
F ⊆ E of edges separates s from t if, after removing the edges F from the graph
G, no s-t paths remain in the graph.
(7.45) In every directed graph with nodes s and t, the maximum number of
edge-disjoint s-t paths is equal to the minimum number of edges whose removal
separates s from t.
Proof. If the removal of a set F ⊆ E of edges separates s from t, then each s-t
path must use at least one edge from F, and hence the number of edge-disjoint
s-t paths is at most |F|.
To prove the other direction, we will use the Max-Flow Min-Cut Theorem
(7.13). By (7.43) the maximum number of edge-disjoint paths is the value ν
of the maximum s-t flow. Now (7.13) states that there is an s-t cut (A, B) with
capacity ν. Let F be the set of edges that go from A to B. Each edge has capacity
1, so |F| = ν and, by the definition of an s-t cut, removing these ν edges from
G separates s from t.
This result, then, can be viewed as the natural special case of the Max-
Flow Min-Cut Theorem in which all edge capacities are equal to 1. In fact,
this special case was proved by Menger in 1927, much before the full Max-
Flow Min-Cut Theorem was formulated and proved; for this reason, (7.45)
is often called Menger’s Theorem. If we think about it, the proof of Hall’s
Theorem (7.40) for bipartite matchings involves a reduction to a graph with
unit-capacity edges, and so it can be proved using Menger’s Theorem rather
than the general Max-Flow Min-Cut Theorem. In other words, Hall’s Theorem
is really a special case of Menger’s Theorem, which in turn is a special case
of the Max-Flow Min-Cut Theorem. And the history follows this progression,
since they were discovered in this order, a few decades apart.2
2 In fact, in an interesting retrospective written in 1981, Menger relates his version of the story of how
he first explained his theorem to König, one of the independent discoverers of Hall’s Theorem. You
might think that König, having thought a lot about these problems, would have immediately grasped
why Menger’s generalization of his theorem was true, and perhaps even considered it obvious. But, in
fact, the opposite happened; König didn’t believe it could be right and stayed up all night searching
for a counterexample. The next day, exhausted, he sought out Menger and asked him for the proof.
378 Chapter 7 Network Flow
(v, u), and in this way create a directed version G of G. (We may delete the
edges into s and out of t, since they are not useful.) Now we want to use the
Ford-Fulkerson Algorithm in the resulting directed graph. However, there is an
important issue we need to deal with first. Notice that two paths P1 and P2 may
be edge-disjoint in the directed graph and yet share an edge in the undirected
graph G: This happens if P1 uses directed edge (u, v) while P2 uses edge (v, u).
However, it is not hard to see that there always exists a maximum flow in any
network that uses at most one out of each pair of oppositely directed edges.
(7.46) In any flow network, there is a maximum flow f where for all opposite
directed edges e = (u, v) and e = (v, u), either f (e) = 0 or f (e ) = 0. If the
capacities of the flow network are integral, then there also is such an integral
maximum flow.
Now we can use the Ford-Fulkerson Algorithm and the path decomposition
procedure from (7.42) to obtain edge-disjoint paths in the undirected graph G.
The undirected analogue of (7.45) is also true, as in any s-t cut, at most
one of the two oppositely directed edges can cross from the s-side to the t-
side of the cut (for if one crosses, then the other must go from the t-side to
the s-side).
(7.48) In every undirected graph with nodes s and t, the maximum number of
edge-disjoint s-t paths is equal to the minimum number of edges whose removal
separates s from t.