Observe that the residual network Gf is similar to a flow network
with capacities given by cf. It does not satisfy the definition of a flow
network, however, because it could contain antiparallel edges. Other
than this difference, a residual network has the same properties as a flow
network, and we can define a flow in the residual network as one that
satisfies the definition of a flow, but with respect to capacities cf in the
residual network Gf.
A flow in a residual network provides a roadmap for adding flow to
the original flow network. If f is a flow in G and f′ is a flow in the
corresponding residual network Gf, we define f ↑ f′, the augmentation of flow f by f ′, to be a function from V × V to ℝ, defined by The intuition behind this definition follows the definition of the
residual network. The flow on ( u, v) increases by f ′( u, v), but decreases by f ′( v, u) because pushing flow on the reverse edge in the residual network signifies decreasing the flow in the original network. Pushing
flow on the reverse edge in the residual network is also known as
cancellation. For example, suppose that 5 crates of hockey pucks go
from u to v and 2 crates go from v to u. That is equivalent (from the perspective of the final result) to sending 3 crates from u to v and none from v to u. Cancellation of this type is crucial for any maximum-flow
algorithm.
The following lemma shows that augmenting a flow in G by a flow in
Gf yields a new flow in G with a greater flow value.
Lemma 24.1
Let G = ( V, E) be a flow network with source s and sink t, and let f be a flow in G. Let Gf be the residual network of G induced by f, and let f ′ be a flow in Gf. Then the function f ↑ f ′ defined in equation (24.4) is a flow in G with value | f ↑ f ′| = | f | + | f ′|.
Proof We first verify that f ↑ f ′ obeys the capacity constraint for each edge in E and flow conservation at each vertex in V − { s, t}.
For the capacity constraint, first observe that if ( u, v) ∈ E, then cf ( v, u) = f ( u, v). Because f ′ is a flow in Gf, we have f ′( v, u) ≤ cf ( v, u), which gives f ′( v, u) ≤ f ( u, v). Therefore,
( f ↑ f′)( u, v) = f ( u, v) + f ′( u, v) − f ′( v, u) (by equation (24.4))
≥ f ( u, v) + f ′( u, v) − f ( u, v) (because f ′( v, u) ≤ f ( u, v))
= f ′( u, v)
≥ 0.
In addition,
( f ↑ f′)( u, v)
= f ( u, v) + f ′( u, v) − f ′( v, u) (by equation (24.4))
≤ f ( u, v) + f ′( u, v)
(because flows are nonnegative)
≤ f ( u, v) + cf ( u, v)
(capacity constraint)
= f ( u, v) + c( u, v) − f ( u, v) (definition of cf)
= c( u, v).
To show that flow conservation holds and that | f ↑ f ′| = | f | + | f ′|, we first prove the claim that for all u ∈ V, we have
Because we disallow antiparallel edges in G (but not in Gf), we know that for each vertex u, there can be an edge ( u, v) or ( v, u) in G, but never both. For a fixed vertex u, define Vl( u) = { v : ( u, v) ∈ E} to be the set of vertices with edges in G leaving u, and define Ve( u) = { v : ( v, u) ∈ E} to be the set of vertices with edges in G entering u. We have Vl( u) ∪ Ve( u)
⊆ V and, because G contains no antiparallel edges, Vl( u) ∩ Ve( u) = ∅ .
By the definition of flow augmentation in equation (24.4), only vertices
v in Vl( u) can have positive ( f ↑ f′)( u, v), and only vertices v in Ve( u) can have positive ( f ↑ f ′)( v, u). Starting from the left-hand side of equation (24.5), we use this fact and then reorder and group terms, giving

In equation (24.6), all four summations can extend to sum over V, since
each additional term has value 0. (Exercise 24.2-1 asks you to prove this
formally.) Taking all four summations over V, instead of just subsets of
V, proves the claim in equation (24.5).
Now we are ready to prove flow conservation for f ↑ f ′ and that | f ↑ f′|
= | f | + | f ′|. For the latter property, let u = s in equation (24.5). Then, we have
For flow conservation, observe that for any vertex u that is neither s nor t, flow conservation for f and f ′ means that the right-hand side of equation (24.5) is 0, and thus Σ v∈ V ( f ↑ f′)( u, v) = Σ v∈ V ( f ↑ f′)( v, u).
▪
Augmenting paths
Given a flow network G = ( V, E) and a flow f, an augmenting path p is a simple path from s to t in the residual network Gf. By the definition of the residual network, the flow on an edge ( u, v) of an augmenting path
may increase by up to cf ( u, v) without violating the capacity constraint on whichever of ( u, v) and ( v, u) belongs to the original flow network G.
The blue path in Figure 24.4(b) is an augmenting path. Treating the residual network Gf in the figure as a flow network, the flow through
each edge of this path can increase by up to 4 units without violating a
capacity constraint, since the smallest residual capacity on this path is cf
( v 2, v 3) = 4. We call the maximum amount by which we can increase the flow on each edge in an augmenting path p the residual capacity of p, given by
cf ( p) = min { cf ( u, v) : ( u, v) is in p}.
The following lemma, which Exercise 24.2-7 asks you to prove, makes
the above argument more precise.
Lemma 24.2
Let G = ( V, E) be a flow network, let f be a flow in G, and let p be an augmenting path in Gf. Define a function fp : V × V → ℝ by Then, fp is a flow in Gf with value | fp| = cf ( p) > 0.
▪
The following corollary shows that augmenting f by fp produces another flow in G whose value is closer to the maximum. Figure 24.4(c)
shows the result of augmenting the flow f from Figure 24.4(a) by the flow fp in Figure 24.4(b), and Figure 24.4(d) shows the ensuing residual network.
Corollary 24.3
Let G = ( V, E) be a flow network, let f be a flow in G, and let p be an augmenting path in Gf. Let fp be defined as in equation (24.7), and suppose that f is augmented by fp. Then the function f ↑ fp is a flow in G
with value | f ↑ fp| = | f| + | fp| > | f|.

Proof Immediate from Lemmas 24.1 and 24.2.
▪
Cuts of flow networks
The Ford-Fulkerson method repeatedly augments the flow along
augmenting paths until it has found a maximum flow. How do we know
that when the algorithm terminates, it has actually found a maximum
flow? The max-flow min-cut theorem, which we will prove shortly, tells
us that a flow is maximum if and only if its residual network contains no
augmenting path. To prove this theorem, though, we must first explore
the notion of a cut of a flow network.
A cut ( S, T) of flow network G = ( V, E) is a partition of V into S and T = V − S such that s ∈ S and t ∈ T. (This definition is similar to the
definition of “cut” that we used for minimum spanning trees in Chapter
21, except that here we are cutting a directed graph rather than an
undirected graph, and we insist that s ∈ S and t ∈ T.) If f is a flow, then the net flow f( S, T) across the cut ( S, T) is defined to be The capacity of the cut ( S, T) is
A minimum cut of a network is a cut whose capacity is minimum over all
cuts of the network.
You probably noticed that the definitions of flow across a cut and
capacity of a cut differ in that flow counts edges going in both directions
across the cut, but capacity counts only edges going from the source
side of the cut toward the sink side. This asymmetry is intentional and
important. The reason for this difference will become apparent later in
this section.
Figure 24.5 shows the cut ({ s, v 1, v 2}, { v 3, v 4, t}) in the flow network of Figure 24.1(b). The net flow across this cut is
f ( v 1, v 3) + f ( v 2, v 4) − f ( v 3, v 2) = 12 + 11 − 4


= 19,
and the capacity of this cut is
Figure 24.5 A cut ( S, T) in the flow network of Figure 24.1(b), where S = { s, v 1, v 2} and T =
{ v 3, v 4, t}. The vertices in S are orange, and the vertices in T are tan. The net flow across ( S, T) is f ( S, T) = 19, and the capacity is c( S, T) = 26.
c( v 1, v 3) + c( v 2, v 4) = 12 + 14
= 26.
The following lemma shows that, for a given flow f, the net flow
across any cut is the same, and it equals | f|, the value of the flow.
Lemma 24.4
Let f be a flow in a flow network G with source s and sink t, and let ( S, T) be any cut of G. Then the net flow across ( S, T) is f ( S, T) =| f|.
Proof For any vertex u ∈ V − { s, t}, rewrite the flow-conservation condition as
Taking the definition of| f| from equation (24.1) and adding the left-hand side of equation (24.10), which equals 0, summed over all vertices
in S − { s}, gives
Expanding the right-hand summation and regrouping terms yields


Because V = S ∪ T and S ∩ T = ∅ , splitting each summation over V
into summations over S and T gives
The two summations within the parentheses are actually the same, since
for all vertices x, y ∈ S, the term f ( x, y) appears once in each summation. Hence, these summations cancel, yielding
▪
A corollary to Lemma 24.4 shows how cut capacities bound the
value of a flow.
Corollary 24.5
The value of any flow f in a flow network G is bounded from above by
the capacity of any cut of G.
Proof Let ( S, T) be any cut of G and let f be any flow. By Lemma 24.4
and the capacity constraint,
▪
Corollary 24.5 yields the immediate consequence that the value of a
maximum flow in a network is bounded from above by the capacity of a
minimum cut of the network. The important max-flow min-cut
theorem, which we now state and prove, says that the value of a
maximum flow is in fact equal to the capacity of a minimum cut.
Theorem 24.6 (Max-flow min-cut theorem)
If f is a flow in a flow network G = ( V, E) with source s and sink t, then the following conditions are equivalent:
1. f is a maximum flow in G.
2. The residual network Gf contains no augmenting paths.
3. | f| = c( S, T) for some cut ( S, T) of G.
Proof (1) ⇒ (2): Suppose for the sake of contradiction that f is a maximum flow in G but that Gf has an augmenting path p. Then, by Corollary 24.3, the flow found by augmenting f by fp, where fp is given by equation (24.7), is a flow in G with value strictly greater than | f|, contradicting the assumption that f is a maximum flow.
(2) ⇒ (3): Suppose that Gf has no augmenting path, that is, that Gf
contains no path from s to t. Define
S = { v ∈ V : there exists a path from s to v in Gf }
and T = V − S. The partition ( S, T) is a cut: we have s ∈ S trivially and t ∉ S because there is no path from s to t in Gf. Now consider a pair of vertices u ∈ S and v ∈ T. If ( u, v) ∈ E, we must have f ( u, v) = c( u, v),
since otherwise ( u, v) ∈ Ef, which would place v in set S. If ( v, u) ∈ E, we must have f ( v, u) = 0, because otherwise cf ( u, v) = f ( v, u) would be positive and we would have ( u, v) ∈ Ef, which again would place v in S.
Of course, if neither ( u, v) nor ( v, u) belongs to E, then f ( u, v) = f ( v, u)
= 0. We thus have
By Lemma 24.4, therefore, | f| = f ( S, T) = c( S, T).
(3) ⇒ (1): By Corollary 24.5, | f| ≤ c( S, T) for all cuts ( S, T). The condition | f| = c( S, T) thus implies that f is a maximum flow.
▪
The basic Ford-Fulkerson algorithm
Each iteration of the Ford-Fulkerson method finds some augmenting
path p and uses p to modify the flow f. As Lemma 24.2 and Corollary 24.3 suggest, replacing f by f ↑ fp produces a new flow whose value is | f|
+ | fp|. The procedure FORD-FULKERSON on the next page
implements the method by updating the flow attribute ( u, v). f for each edge ( u, v) ∈ E. 1 It assumes implicitly that ( u, v). f = 0 if ( u, v) ∉ E. The procedure also assumes that the capacities c( u, v) come with the flow network, and that c( u, v) = 0 if ( u, v) ∉ E. The procedure computes the residual capacity cf ( u, v) in accordance with the formula (24.2). The expression cf ( p) in the code is just a temporary variable that stores the residual capacity of the path p.
FORD-FULKERSON ( G, s, t)
1 for each edge ( u, v) ∈ G. E
2
( u, v). f = 0
3 while there exists a path p from s to t in the residual network Gf 4
cf ( p) = min { cf ( u, v) : ( u, v) is in p}
for each edge ( u, v) in p
6
if ( u, v) ∈ G. E
7
( u, v). f = ( u, v). f + cf ( p) 8
else ( v, u). f = ( v, u). f − cf ( p) 9 return f
The FORD-FULKERSON procedure simply expands on the
FORD-FULKERSON-METHOD pseudocode given earlier. Figure
24.6 shows the result of each iteration in a sample run. Lines 1–2
initialize the flow f to 0. The while loop of lines 3–8 repeatedly finds an
augmenting path p in Gf and augments flow f along p by the residual capacity cf ( p). Each residual edge in path p is either an edge in the original network or the reversal of an edge in the original network.
Lines 6–8 update the flow in each case appropriately, adding flow when
the residual edge is an original edge and subtracting it otherwise. When
no augmenting paths exist, the flow f is a maximum flow.
Analysis of Ford-Fulkerson
The running time of FORD-FULKERSON depends on the
augmenting path p and how it’s found in line 3. If the edge capacities are
irrational numbers, it’s possible to choose the augmenting path so that
the algorithm never terminates: the value of the flow increases with
successive augmentations, but never converges to the maximum flow
value. The good news is that if the algorithm finds the augmenting path
by using a breadth-first search (which we saw in Section 20.2), it runs in polynomial time. Before proving this result, we obtain a simple bound
for the case in which all capacities are integers and the algorithm finds
any augmenting path.
Figure 24.6 The execution of the basic Ford-Fulkerson algorithm. (a)–(e) Successive iterations of the while loop. The left side of each part shows the residual network Gf from line 3 with a blue augmenting path p. The right side of each part shows the new flow f that results from augmenting f by fp. The residual network in (a) is the input flow network G. (f) The residual network at the last while loop test. It has no augmenting paths, and the flow f shown in (e) is therefore a maximum flow. The value of the maximum flow found is 23.
Figure 24.7 (a) A flow network for which FORD-FULKERSON can take Θ( E | f*|) time, where f* is a maximum flow, shown here with | f*| = 2,000,000. The blue path is an augmenting path with residual capacity 1. (b) The resulting residual network, with another augmenting path whose residual capacity is 1. (c) The resulting residual network.
In practice, the maximum-flow problem often arises with integer
capacities. If the capacities are rational numbers, an appropriate scaling
transformation can make them all integers. If f* denotes a maximum
flow in the transformed network, then a straightforward implementation
of FORD-FULKERSON executes the while loop of lines 3–8 at most
| f*| times, since the flow value increases by at least 1 unit in each iteration.
A good implementation should perform the work done within the
while loop efficiently. It should represent the flow network G = ( V, E) with the right data structure and find an augmenting path by a linear-time algorithm. Let’s assume that the implementation keeps a data
structure corresponding to a directed graph G′ = ( V, E′), where E′ = {( u, v) : ( u, v) ∈ E or ( v, u) ∈ E}. Edges in the network G are also edges in G′, making it straightforward to maintain capacities and flows in this data structure. Given a flow f on G, the edges in the residual network Gf consist of all edges ( u, v) of G′ such that cf ( u, v) > 0, where cf conforms to equation (24.2). The time to find a path in a residual network is
therefore O( V + E′) = O( E) using either depth-first search or breadth-first search. Each iteration of the while loop thus takes O( E) time, as does the initialization in lines 1–2, making the total running time of the
FORD-FULKERSON algorithm O( E | f*|).
When the capacities are integers and the optimal flow value | f*| is
small, the running time of the Ford-Fulkerson algorithm is good.
Figure 24.7(a) shows an example of what can happen on a simple flow
network for which | f*| is large. A maximum flow in this network has value 2,000,000: 1,000,000 units of flow traverse the path s → u → t, and another 1,000,000 units traverse the path s → v → t. If the first augmenting path found by FORD-FULKERSON is s → u → v → t, shown in Figure 24.7(a), the flow has value 1 after the first iteration.
The resulting residual network appears in Figure 24.7(b). If the second
iteration finds the augmenting path s → v → u → t, as shown in Figure
24.7(b), the flow then has value 2. Figure 24.7(c) shows the resulting
residual network. If the algorithm continues alternately choosing the
augmenting paths s → u → v → t and s → v → u → t, it performs a total of 2,000,000 augmentations, increasing the flow value by only 1 unit in
each.
The Edmonds-Karp algorithm
In the example of Figure 24.7, the algorithm never chooses the augmenting path with the fewest edges. It should have. By using
breadth-first search to find an augmenting path in the residual network,
the algorithm runs in polynomial time, independent of the maximum
flow value. We call the Ford-Fulkerson method so implemented the
Edmonds-Karp algorithm.
Let’s now prove that the Edmonds-Karp algorithm runs in O( VE 2)
time. The analysis depends on the distances to vertices in the residual
network Gf. The notation δf ( u, v) denotes the shortest-path distance from u to v in Gf, where each edge has unit distance.
Lemma 24.7
If the Edmonds-Karp algorithm is run on a flow network G = ( V, E) with source s and sink t, then for all vertices v ∈ V − { s, t}, the shortest-path distance δf ( s, v) in the residual network Gf increases monotonically with each flow augmentation.
Proof We’ll suppose that a flow augmentation occurs that causes the
shortest-path distance from s to some vertex v ∈ V − { s, t} to decrease and then derive a contradiction. Let f be the flow just before an
augmentation that decreases some shortest-path distance, and let f′ be

the flow just afterward. Let v be a vertex with the minimum δf′ ( s, v) whose distance was decreased by the augmentation, so that δf′ ( s, v) < δf ( s, v). Let p = s ⇝ u → v be a shortest path from s to v in Gf′, so that ( u, v) ∈ Ef′ and
Because of how we chose v, we know that the distance of vertex u from
the source s did not decrease, that is,
We claim that ( u, v) ∉ Ef. Why? If we have ( u, v) ∈ Ef, then we also have
δf ( s, v) ≤ δf ( s, u) + 1 (by Lemma 22.10, the triangle inequality)
≤ δf′ ( s, u) + 1 (by inequality (24.12))
= δf′ ( s, v)
(by equation (24.11)),
which contradicts our assumption that δf′ ( s, v) < δf ( s, v).
How can we have ( u, v) ∉ Ef and ( u, v) ∈ Ef′? The augmentation must have increased the flow from v to u, so that edge ( v, u) was in the augmenting path. The augmenting path was a shortest path from s to t
in Gf, and since any subpath of a shortest path is itself a shortest path,
this augmenting path includes a shortest path from s to u in Gf that has ( v, u) as its last edge. Therefore,
δf ( s, v) = δf ( s, u) − 1
≤ δf′ ( s, u) − 1 (by inequality (24.12))
= δf′ ( s, v) − 2 (by equation (24.11)),
so that δf′ ( s, v) > δf ( s, v), contradicting our assumption that δf′ ( s, v) < δf ( s, v). We conclude that our assumption that such a vertex v exists is incorrect.
▪
The next theorem bounds the number of iterations of the Edmonds-
Karp algorithm.
Theorem 24.8
If the Edmonds-Karp algorithm is run on a flow network G = ( V, E) with source s and sink t, then the total number of flow augmentations
performed by the algorithm is O( VE).
Proof We say that an edge ( u, v) in a residual network Gf is critical on an augmenting path p if the residual capacity of p is the residual capacity of ( u, v), that is, if cf ( p) = cf ( u, v). After flow is augmented along an augmenting path, any critical edge on the path disappears
from the residual network. Moreover, at least one edge on any
augmenting path must be critical. We’ll show that each of the | E| edges
can become critical at most | V|/2 times.
Let u and v be vertices in V that are connected by an edge in E. Since augmenting paths are shortest paths, when ( u, v) is critical for the first time, we have
δf ( s, v) = δf ( s, u) + 1.
Once the flow is augmented, the edge ( u, v) disappears from the residual network. It cannot reappear later on another augmenting path until
after the flow from u to v is decreased, which occurs only if ( v, u) appears on an augmenting path. If f ′ is the flow in G when this event
occurs, then we have
δf′ ( s, u) = δf′ ( s, v) + 1.
Since δf ( s, v) ≤ δf′ ( s, v) by Lemma 24.7, we have δf′ ( s, u) = δf′ ( s, v) + 1
≥ δf ( s, v) + 1
= δf ( s, u) + 2.
Consequently, from the time ( u, v) becomes critical to the time when
it next becomes critical, the distance of u from the source increases by at
least 2. The distance of u from the source is initially at least 0. Because edge ( u, v) is on an augmenting path, and augmenting paths end at t, we know that u cannot be t, so that in any residual network that has a path from s to u, the shortest such path has at most | V| − 2 edges. Thus, after the first time that ( u, v) becomes critical, it can become critical at most (| V| − 2)/2 = | V|/2 − 1 times more, for a total of at most | V|/2 times. Since there are O( E) pairs of vertices that can have an edge between them in a residual network, the total number of critical edges during the entire
execution of the Edmonds-Karp algorithm is O( VE). Each augmenting
path has at least one critical edge, and hence the theorem follows.
▪
Because each iteration of FORD-FULKERSON takes O( E) time
when it uses breadth-first search to find the augmenting path, the total
running time of the Edmonds-Karp algorithm is O( VE 2).
Exercises
24.2-1
Prove that the summations in equation (24.6) equal the summations on
the right-hand side of equation (24.5).
24.2-2
In Figure 24.1(b), what is the net flow across the cut ({ s, v 2, v 4}, { v 1, v 3, t})? What is the capacity of this cut?
24.2-3
Show the execution of the Edmonds-Karp algorithm on the flow
network of Figure 24.1(a).
24.2-4
In the example of Figure 24.6, what is the minimum cut corresponding to the maximum flow shown? Of the augmenting paths appearing in the
example, which one cancels flow?
24.2-5
The construction in Section 24.1 to convert a flow network with multiple sources and sinks into a single-source, single-sink network adds
edges with infinite capacity. Prove that any flow in the resulting network
has a finite value if the edges of the original network with multiple
sources and sinks have finite capacity.
24.2-6
Suppose that each source si in a flow network with multiple sources and
sinks produces exactly pi units of flow, so that Σ v∈ V f ( si, v) = pi.
Suppose also that each sink tj consumes exactly qj units, so that Σ v∈ V f ( v, tj) = qj, where Σ i pi = Σ j qj. Show how to convert the problem of finding a flow f that obeys these additional constraints into the problem
of finding a maximum flow in a single-source, single-sink flow network.
24.2-7
Prove Lemma 24.2.
24.2-8
Suppose that we redefine the residual network to disallow edges into s.
Argue that the procedure FORD-FULKERSON still correctly
computes a maximum flow.
24.2-9
Suppose that both f and f ′ are flows in a flow network. Does the augmented flow f ↑ f ′ satisfy the flow conservation property? Does it satisfy the capacity constraint?
24.2-10
Show how to find a maximum flow in a flow network G = ( V, E) by a sequence of at most | E| augmenting paths. ( Hint: Determine the paths
after finding the maximum flow.)
24.2-11
The edge connectivity of an undirected graph is the minimum number k
of edges that must be removed to disconnect the graph. For example,
the edge connectivity of a tree is 1, and the edge connectivity of a cyclic
chain of vertices is 2. Show how to determine the edge connectivity of
an undirected graph G = ( V, E) by running a maximum-flow algorithm on at most | V| flow networks, each having O( V + E) vertices and O( E) edges.
24.2-12
You are given a flow network G, where G contains edges entering the source s. Let f be a flow in G with | f| ≥ 0 in which one of the edges ( v, s) entering the source has f ( v, s) = 1. Prove that there must exist another flow f ′ with f ′( v, s) = 0 such that | f| = | f′|. Give an O( E)-time algorithm to compute f′, given f and assuming that all edge capacities are integers.
24.2-13
Suppose that you wish to find, among all minimum cuts in a flow
network G with integer capacities, one that contains the smallest
number of edges. Show how to modify the capacities of G to create a
new flow network G′ in which any minimum cut in G′ is a minimum cut
with the smallest number of edges in G.
24.3 Maximum bipartite matching
Some combinatorial problems can be cast as maximum-flow problems,
such as the multiple-source, multiple-sink maximum-flow problem from
Section 24.1. Other combinatorial problems seem on the surface to have little to do with flow networks, but they can in fact be reduced to
maximum-flow problems. This section presents one such problem:
finding a maximum matching in a bipartite graph. In order to solve this
problem, we’ll take advantage of an integrality property provided by the
Ford-Fulkerson method. We’ll also see how to use the Ford-Fulkerson
method to solve the maximum-bipartite-matching problem on a graph
G = ( V, E) in O( VE) time. Section 25.1 will present an algorithm specifically designed to solve this problem.
The maximum-bipartite-matching problem
Given an undirected graph G = ( V, E), a matching is a subset of edges M ⊆ E such that for all vertices v ∈ V, at most one edge of M is
incident on v. We say that a vertex v ∈ V is matched by the matching M
if some edge in M is incident on v, and otherwise, v is unmatched. A maximum matching is a matching of maximum cardinality, that is, a
matching M such that for any matching M′, we have | M| ≥ | M′|. In this section, we restrict our attention to finding maximum matchings in
bipartite graphs: graphs in which the vertex set can be partitioned into
V = L ∪ R, where L and R are disjoint and all edges in E go between L
and R. We further assume that every vertex in V has at least one incident edge. Figure 24.8 illustrates the notion of a matching in a bipartite graph.
The problem of finding a maximum matching in a bipartite graph
has many practical applications. As an example, consider matching a set
L of machines with a set R of tasks to be performed simultaneously. An
edge ( u, v) in E signifies that a particular machine u ∈ L is capable of performing a particular task v ∈ R. A maximum matching provides work for as many machines as possible.
Figure 24.8 A bipartite graph G = ( V, E) with vertex partition V = L ∪ R. (a) A matching with cardinality 2, indicated by blue edges. (b) A maximum matching with cardinality 3. (c) The corresponding flow network G′ with a maximum flow shown. Each edge has unit capacity. Blue edges have a flow of 1, and all other edges carry no flow. The blue edges from L to R correspond to those in the maximum matching from (b).
Finding a maximum bipartite matching
The Ford-Fulkerson method provides a basis for finding a maximum
matching in an undirected bipartite graph G = ( V, E) in time
polynomial in | V| and | E|. The trick is to construct a flow network in which flows correspond to matchings, as shown in Figure 24.8(c). We define the corresponding flow network G′ = ( V′, E′) for the bipartite graph G as follows. Let the source s and sink t be new vertices not in V, and let V′ = V ∪ { s, t}. If the vertex partition of G is V = L ∪ R, the directed edges of G′ are the edges of E, directed from L to R, along with
| V | new directed edges:
E′ = {( s, u) : u ∈ L}
∪ {( u, v) : u ∈ L, v ∈ R, and ( u, v) ∈ E}
∪ {( v, t) : v ∈ R}.
To complete the construction, assign unit capacity to each edge in E′.
Since each vertex in V has at least one incident edge, | E| ≥ | V|/2. Thus, | E|
≤ | E′| = | E| + | V| ≤ 3 | E|, and so | E′| = Θ( E).
The following lemma shows that a matching in G corresponds
directly to a flow in G’s corresponding flow network G′. We say that a
flow f on a flow network G = ( V, E) is integer-valued if f ( u, v) is an integer for all ( u, v) ∈ V × V.
Lemma 24.9
Let G = ( V, E) be a bipartite graph with vertex partition V = L ∪ R, and let G′ = ( V′, E′) be its corresponding flow network. If M is a matching in G, then there is an integer-valued flow f in G′ with value | f|
= | M|. Conversely, if f is an integer-valued flow in G′, then there is a matching M in G with cardinality | M| = | f| consisting of edges ( u, v) ∈ E
such that f ( u, v) > 0.
Proof We first show that a matching M in G corresponds to an integer-valued flow f in G′. Define f as follows. If ( u, v) ∈ M, then f ( s, u) = f ( u, v) = f ( v, t) = 1. For all other edges ( u, v) ∈ E′, define f ( u, v) = 0. It is simple to verify that f satisfies the capacity constraint and flow
conservation.
Intuitively, each edge ( u, v) ∈ M corresponds to 1 unit of flow in G′
that traverses the path s → u → v → t. Moreover, the paths induced by edges in M are vertex-disjoint, except for s and t. The net flow across cut
( L ∪ { s}, R ∪ { t}) is equal to | M|, and thus, by Lemma 24.4, the value of the flow is | f| = | M|.
To prove the converse, let f be an integer-valued flow in G′ and, as in the statement of the lemma, let
M = {( u, v) : u ∈ L, v ∈ R, and f ( u, v) > 0}.
Each vertex u ∈ L has only one entering edge, namely ( s, u), and its capacity is 1. Thus, each u ∈ L has at most 1 unit of flow entering it, and if 1 unit of flow does enter, by flow conservation, 1 unit of flow must
leave. Furthermore, since the flow f is integer-valued, for each u ∈ L, the 1 unit of flow can enter on at most one edge and can leave on at most
one edge. Thus, 1 unit of flow enters u if and only if there is exactly one
vertex v ∈ R such that f ( u, v) = 1, and at most one edge leaving each u
∈ L carries positive flow. A symmetric argument applies to each v ∈ R.
The set M is therefore a matching.
To see that | M| = | f|, observe that of the edges ( u, v) ∈ E′ such that u
∈ L and v ∈ R,
Consequently, f ( L ∪ { s}, R ∪ { t}), the net flow across cut ( L ∪ { s}, R ∪
{ t}), is equal to | M|. Lemma 24.4 gives that | f| = f ( L ∪ { s}, R ∪ { t}) =
| M|.
▪
Based on Lemma 24.9, we would like to conclude that a maximum
matching in a bipartite graph G corresponds to a maximum flow in its
corresponding flow network G′, and therefore running a maximum-flow
algorithm on G′ provides a maximum matching in G. The only hitch in
this reasoning is that the maximum-flow algorithm might return a flow
in G′ for which some f ( u, v) is not an integer, even though the flow value
| f| must be an integer. The following theorem shows that the Ford-
Fulkerson method cannot produce a solution with this problem.
Theorem 24.10 (Integrality theorem)
If the capacity function c takes on only integer values, then the maximum flow f produced by the Ford-Fulkerson method has the
property that | f| is an integer. Moreover, for all vertices u and v, the value of f ( u, v) is an integer.
Proof Exercise 24.3-2 asks you to provide the proof by induction on
the number of iterations.
▪
We can now prove the following corollary to Lemma 24.9.
Corollary 24.11
The cardinality of a maximum matching M in a bipartite graph G
equals the value of a maximum flow f in its corresponding flow network
G′.
Proof We use the nomenclature from Lemma 24.9. Suppose that M is a
maximum matching in G and that the corresponding flow f in G′ is not maximum. Then there is a maximum flow f′ in G′ such that | f′| > | f|.
Since the capacities in G′ are integer-valued, by Theorem 24.10, we can
assume that f′ is integer-valued. Thus, f′ corresponds to a matching M′
in G with cardinality | M′| = | f′| > | f| = | M|, contradicting our assumption that M is a maximum matching. In a similar manner, we can show that
if f is a maximum flow in G′, its corresponding matching is a maximum
matching on G.
▪
Thus, to find a maximum matching in a bipartite undirected graph
G, create the flow network G′, run the Ford-Fulkerson method on G′, and convert the integer-valued maximum flow found into a maximum
matching for G. Since any matching in a bipartite graph has cardinality
at most min {| L|, | R|} = O( V), the value of the maximum flow in G′ is O( V). Therefore, finding a maximum matching in a bipartite graph takes O( VE′) = O( VE) time, since | E′| = Θ( E).
Exercises
24.3-1
Run the Ford-Fulkerson algorithm on the flow network in Figure
24.8(c) and show the residual network after each flow augmentation.
Number the vertices in L top to bottom from 1 to 5 and in R top to bottom from 6 to 9. For each iteration, pick the augmenting path that is
lexicographically smallest.
24.3-2
Prove Theorem 24.10. Use induction on the number of iterations of the
Ford-Fulkerson method.
24.3-3
Let G = ( V, E) be a bipartite graph with vertex partition V = L ∪ R, and let G′ be its corresponding flow network. Give a good upper bound
on the length of any augmenting path found in G′ during the execution
of FORD-FULKERSON.
Problems
24-1 Escape problem
An n× n grid is an undirected graph consisting of n rows and n columns of vertices, as shown in Figure 24.9. We denote the vertex in the i th row and the j th column by ( i, j). All vertices in a grid have exactly four neighbors, except for the boundary vertices, which are the points ( i, j) for which i = 1, i = n, j = 1, or j = n.
Given m ≤ n 2 starting points ( x 1, y 1), ( x 2, y 2), …, ( xm, ym) in the grid, the escape problem is to determine whether there are m vertex-disjoint paths from the starting points to any m different points on the
boundary. For example, the grid in Figure 24.9(a) has an escape, but the grid in Figure 24.9(b) does not.
Figure 24.9 Grids for the escape problem. Starting points are blue, and other grid vertices are tan. (a) A grid with an escape, shown by blue paths. (b) A grid with no escape.
a. Consider a flow network in which vertices, as well as edges, have
capacities. That is, the total positive flow entering any given vertex is
subject to a capacity constraint. Show how to reduce the problem of
determining the maximum flow in a network with edge and vertex
capacities to an ordinary maximum-flow problem on a flow network
of comparable size.
b. Describe an efficient algorithm to solve the escape problem, and
analyze its running time.
24-2 Minimum path cover
A path cover of a directed graph G = ( V, E) is a set P of vertex-disjoint paths such that every vertex in V is included in exactly one path in P.
Paths may start and end anywhere, and they may be of any length,
including 0. A minimum path cover of G is a path cover containing the
fewest possible paths.
a. Give an efficient algorithm to find a minimum path cover of a
directed acyclic graph G = ( V, E). ( Hint: Assuming that V = {1, 2, …, n}, construct a flow network based on the graph G′ = ( V′, E′), where V′ = { x 0, x 1, …, xn} ∪ { y 0, y 1, …, yn}, E′ = {( x 0, xi) : i ∈ V } ∪ {( yi, y 0) : i ∈ V } ∪ {( xi, yj) : ( i, j) ∈ E}, and run a maximum-flow algorithm.)
b. Does your algorithm work for directed graphs that contain cycles?
Explain.
24-3 Hiring consulting experts
Professor Fieri wants to open a consulting company for the food
industry. He has identified n important food categories, which he
represents by the set C = { C 1, C 2, …, Cn}. In each category Ck, he can hire an expert in that category for ek > 0 dollars. The consulting company has lined up a set J = { J 1, J 2, …, Jm} of potential jobs. In order to perform job Ji, the company needs to have hired experts in a
subset Ri ⊆ C of categories. Each expert can work on multiple jobs simultaneously. If the company chooses to accept job Ji, it must have
hired experts in all categories in Ri, and it takes in revenue of pi > 0
dollars.
Professor Fieri’s job is to determine which categories to hire experts
in and which jobs to accept in order to maximize the net revenue, which
is the total income from jobs accepted minus the total cost of employing
the experts.
Consider the following flow network G. It contains a source vertex s,
vertices C 1, C 2, …, Cn, vertices J 1, J 2, …, Jm, and a sink vertex t. For k = 1, 2 …, n, the flow network contains an edge ( s, Ck) with capacity c( s, Ck) = ek, and for i = 1, 2, …, m, the flow network contains an edge ( Ji, t) with capacity c( Ji, t) = pi. For k = 1, 2, …, n and i = 1, 2, …, m, if Ck ∈ Ri, then G contains an edge ( Ck, Ji) with capacity c( Ck, Ji) = ∞.
a. Show that if Ji ∈ T for a finite-capacity cut ( S, T) of G, then Ck ∈ T
for each Ck ∈ Ri.
b. Show how to determine the maximum net revenue from the capacity
of a minimum cut of G and the given pi values.
c. Give an efficient algorithm to determine which jobs to accept and
which experts to hire. Analyze the running time of your algorithm in
terms of m, n, and
.
Let G = ( V, E) be a flow network with source s, sink t, and integer capacities. Suppose that you are given a maximum flow in G.
a. Suppose that the capacity of a single edge ( u, v) ∈ E increases by 1.
Give an O( V + E)-time algorithm to update the maximum flow.
b. Suppose that the capacity of a single edge ( u, v) ∈ E decreases by 1.
Give an O( V + E)-time algorithm to update the maximum flow.
24-5 Maximum flow by scaling
Let G = ( V, E) be a flow network with source s, sink t, and an integer capacity c( u, v) on each edge ( u, v) ∈ E. Let C = max { c( u, v) : ( u, v) ∈
E}.
a. Argue that a minimum cut of G has capacity at most C | E|.
b. For a given number K, show how to find an augmenting path of
capacity at least K in O( E) time, if such a path exists.
The procedure MAX-FLOW-BY-SCALING appearing on the
following page modifies the basic FORD-FULKERSON-METHOD
procedure to compute a maximum flow in G.
c. Argue that MAX-FLOW-BY-SCALING returns a maximum flow.
d. Show that the capacity of a minimum cut of the residual network Gf
is less than 2 K | E| each time line 4 executes.
e. Argue that the inner while loop of lines 5–6 executes O( E) times for each value of K.
MAX-FLOW-BY-SCALING ( G, s, t)
1 C = max { c( u, v) : ( u, v) ∈ E}
2 initialize flow f to 0
3 K = 2⌊lg C⌋
4 while K ≥ 1
5
while there exists an augmenting path p of capacity at least K
6
augment flow f along p
K = K/2
8 return f
f. Conclude that MAX-FLOW-BY-SCALING can be implemented so
that it runs in O( E 2 lg C) time.
24-6 Widest augmenting path
The Edmonds-Karp algorithm implements the Ford-Fulkerson
algorithm by always choosing a shortest augmenting path in the
residual network. Suppose instead that the Ford-Fulkerson algorithm
chooses a widest augmenting path: an augmenting path with the greatest
residual capacity. Assume that G = ( V, E) is a flow network with source s and sink t, that all capacities are integer, and that the largest capacity is C. In this problem, you will show that choosing a widest augmenting
path results in at most | E| ln | f*| augmentations to find a maximum flow f*.
a. Show how to adjust Dijkstra’s algorithm to find the widest
augmenting path in the residual network.
b. Show that a maximum flow in G can be formed by successive flow
augmentations along at most | E| paths from s to t.
c. Given a flow f, argue that the residual network Gf has an augmenting path p with residual capacity cf ( p) ≥ (| f*| − | f|)/| E|.
d. Assuming that each augmenting path is a widest augmenting path, let
fi be the flow after augmenting the flow by the i th augmenting path,
where f 0 has f ( u, v) = 0 for all edges ( u, v). Show that | f*| − | fi| ≤ | f*| (1
− 1/| E|) i.
e. Show that | f* | − | fi| < | f*| e− i/| E|.
f. Conclude that after the flow is augmented at most | E| ln | f*| times, the flow is a maximum flow.
24-7 Global minimum cut

A global cut in an undirected graph G = ( V, E) is a partition (see page 1156) of V into two nonempty sets V 1 and V 2. This definition is like the definition of cut that we have used in this chapter, except that we no
longer have distinguished vertices s and t. Any edge ( u, v) with u ∈ V 1
and v ∈ V 2 is said to cross the cut.
We can extend this definition of a cut to a multigraph G = ( V, E) (see page 1167), and we denote by c( u, v) the number of edges in the multigraph with endpoints u and v. A global cut in a multigraph is still a partition of the vertices, and the value of a global cut ( V 1, V 2) is
. A solution to the global-minimum-cut
problem is a cut ( V 1, V 2) such that c( V 1, V 2) is minimum. Let μ( G) denote the value of a global minimum cut in a graph or multigraph G.
a. Show how to find a global minimum cut of a graph G = ( V, E) by solving
maximum-flow problems, each with a different pair of
vertices as the source and sink, and taking the mininum value of the
cuts found.
b. Give an algorithm to find a global minimum cut by solving only Θ( V) maximum-flow problems. What is the running time of your algorithm?
The remainder of this problem develops an algorithm for the global-
minimum-cut problem that does not use any maximum-flow
computations. It uses the notion of an edge contraction, defined on
page 1168, with one crucial difference. The algorithm maintains a
multigraph, so that upon contracting an edge ( u, v), it creates a new vertex x, and for any other vertex y ∈ V, the number of edges between x and y is c( u, y) + c( v, y). The algorithm does not maintain self-loops, and so it sets c( x, x) to 0. Denote by G/( u, v) the multigraph that results from contracting edge ( u, v) in multigraph G.
Consider what can happen to the minimum cut when an edge is
contracted. Assume that, at all points, the minimum cut in a multigraph
G is unique. We’ll remove this assumption later.
c. Show that for any edge ( u, v), we have μ( G/( u, v)) ≤ μ( G). Under what conditions is μ( G/( u, v)) < μ( G)?


Next, you will show that if you pick an edge uniformly at random,
the probability that it belongs to the minimum cut is small.
d. Show that for any multigraph G = ( V, E), the value of the global minimum cut is at most the average degree of a vertex: that μ( G) ≤ 2
| E|/| V|, where | E| denotes the total number of edges in the multigraph.
e. Using the results from parts (c) and (d), show that, if we pick an edge
( u, v) uniformly at random, then the probability that ( u, v) belongs to the minimum cut is at most 2/ V.
Consider the algorithm that repeatedly chooses an edge at random
and contracts it until the multigraph has exactly two vertices, say u and
v. At that point, the multigraph corresponds to a cut in the original graph, with vertex u representing all the nodes in one side of the original
graph, and v representing all the vertices on the other side. The number
of edges given by c( u, v) corresponds exactly to the number of edges crossing the corresponding cut in the original graph. We call this
algorithm the contraction algorithm.
f. Suppose that the contraction algorithm terminates with a multigraph
whose only vertices are u and v. Show that
.
g. Prove that if the contraction algorithm repeats
times, then the
probability that at least one of the runs returns the minimum cut is at
least 1 − 1/| V|.
h. Give a detailed implementation of the contraction algorithm that
runs in O( V 2) time.
i. Combine the previous parts and remove the assumption that the
minimum cut must be unique, to conclude that running the
contraction algorithm
times yields an algorithm that runs in
O( V 4 lg V) time and returns a minimum cut with probability at least 1
− 1/ V.
Chapter notes
Ahuja, Magnanti, and Orlin [7], Even [137], Lawler [276], Papadimitriou and Steiglitz [353], Tarjan [429], and Williamson [458]
are good references for network flows and related algorithms. Schrijver
[399] has written an interesting review of historical developments in the field of network flows.
The Ford-Fulkerson method is due to Ford and Fulkerson [149], who originated the formal study of many of the problems in the area of
network flow, including the maximum-flow and bipartite-matching
problems. Many early implementations of the Ford-Fulkerson method
found augmenting paths using breadth-first search. Edmonds and Karp
[132], and independently Dinic [119], proved that this strategy yields a polynomial-time algorithm. A related idea, that of using “blocking
flows,” was also first developed by Dinic [119].
A class of algorithms known as push-relabel algorithms, due to
Goldberg [185] and Goldberg and Tarjan [188], takes a different approach from the Ford-Fulkerson method. Push-relabel algorithms
allow flow conservation to be violated at vertices other than the source
and sink as they execute. Using an idea first developed by Karzonov
[251], they allow a preflow in which the flow into a vertex may exceed the flow out of the vertex. Such a vertex is said to be overflowing. Initially,
every edge leaving the source is filled to capacity, so that all neighbors of
the source are overflowing. In a push-relabel algorithm, each vertex is
assigned an integer height. An overflowing vertex may push flow to a
neighboring vertex to which it has a residual edge provided that it is
higher than the neighbor. If all residual edges from an overflowing
vertex go to neighbors with equal or greater heights, then the vertex
may increase its height. Once all vertices other than the sink are no
longer overflowing, the preflow is not only a legal flow, but also a
maximum flow.
Goldberg and Tarjan [188] gave an O( V 3)-time algorithm that uses a queue to maintain the set of overflowing vertices, as well as an
algorithm that uses dynamic trees to achieve a running time of O( VE
lg( V 2/ E + 2)). Several other researchers developed improved variants and implementations [9, 10, 15, 86, 87, 255, 358], the fastest of which, by King, Rao, and Tarjan [255], runs in O( VE log E/( V lg V) V) time.
Another efficient algorithm for maximum flow, by Goldberg and
Rao [187], runs in O (min{ V 2/3, E 1/2} E lg ( V 2/ E + 2) lg C) time, where C is the maximum capacity any edge. Orlin [350] gave an algorithm in the same spirit as this algorithm that runs in O( VE + E 31/16 lg2 V) time. Combining it with the algorithm of King, Rao, and Tarjan results
in an O( VE)-time algorithm.
A different approach to maximum flows and related problems is to
use techniques from continuous optimization including electrical flows
and interior-point methods. The first breakthrough in this line of work
is due to Madry [308], who gave an Õ( E 10/7)-time algorithm for unit-capacity maximum flow and bipartite maximum matching. (See
Problem 3-6 on page 73 for a definition of Õ.) There has been a series of
papers in this area for matchings, maximum flows, and minimum-cost
flows. The fastest algorithm to date in this line of work for maximum
flow is due to Lee and Sidford [285], taking
time. If the
capacities are not too large, this algorithm is faster than the O( VE)-time algorithm mentioned above. Another algorithm, due to Liu and Sidford
[303] runs in Õ( E 11/8 C 1/4) time, where C is the maximum capacity of any edge. This algorithm does not run in polynomial time, but for small
enough capacities, it is faster than the previous ones.
In practice, push-relabel algorithms currently dominate algorithms
based on augmenting paths, continuous-optimization, and linear
programming for the maximum-flow problem [88].
1 Recall from Section 20.1 that we represent an attribute f for edge ( u, v) with the same style of notation—( u, v). f—that we use for an attribute of any other object.
25 Matchings in Bipartite Graphs
Many real-world problems can be modeled as finding matchings in an
undirected graph. For an undirected graph G = ( V, E), a matching is a subset of edges M ⊆ E such that every vertex in V has at most one incident edge in M.
For example, consider the following scenario. You have one or more
positions to fill and several candidates to interview. According to your
schedule, you are able to interview candidates at certain time slots. You
ask the candidates to indicate the subsets of time slots at which they are
available. How can you schedule the interviews so that each time slot
has at most one candidate scheduled, while maximizing the number of
candidates that you can interview? You can model this scenario as a
matching problem on a bipartite graph in which each vertex represents
either a candidate or a time slot, with an edge between a candidate and
a time slot if the candidate is available then. If an edge is included in the
matching, that means you are scheduling a particular candidate for a
particular time slot. Your goal is to find a maximum matching: a
matching of maximum cardinality. One of the authors of this book was
faced with exactly this situation when hiring teaching assistants for a
large class. He used the Hopcroft-Karp algorithm in Section 25.1 to schedule the interviews.
Another application of matching is the U.S. National Resident
Matching Program, in which medical students are matched to hospitals
where they will be stationed as medical residents. Each student ranks
the hospitals by preference, and each hospital ranks the students. The
goal is to assign students to hospitals so that there is never a student
and a hospital that both have regrets because the student was not assigned to the hospital, yet each ranked the other higher than who or
where they were assigned. This scenario is perhaps the best-known real-
world example of the “stable-marriage problem,” which Section 25.2
examines.
Yet another instance where matching comes into play occurs when
workers must be assigned to tasks in order to maximize the overall
effectiveness of the assignment. For each worker and each task, the
worker has some quantified effectiveness for that task. Assuming that
there are equal numbers of workers and tasks, the goal is to find a
matching with the maximum total effectiveness. Such a situation is an
example of an assignment problem, which Section 25.3 shows how to solve.
The algorithms in this chapter find matchings in bipartite graphs. As
in Section 24.3, the input is an undirected graph G = ( V, E), where V =
L ∪ R, the vertex sets L and R are disjoint, and every edge in E is incident on one vertex in L and one vertex in R. A matching, therefore, matches vertices in L with vertices in R. In some applications, the sets L
and R have equal cardinality, and in other applications they need not be
the same size.
An undirected graph need not be bipartite for the concept of
matching to apply. Matching in general undirected graphs has
applications in areas such as scheduling and computational chemistry. It
models problems in which you want to pair up entities, represented by
vertices. Two vertices are adjacent if they represent compatible entities,
and you need to find a large set of compatible pairs. Maximum-
matching and maximum-weight matching problems on general graphs
can be solved by polynomial-time algorithms whose running times are
similar to those for bipartite matching, but the algorithms are
significantly more complicated. Exercise 25.2-5 discusses the general
version of the stable-marriage problem, known as the “stable-
roommates problem.” Although matching applies to general undirected
graphs, this chapter deals only with bipartite graphs.
25.1 Maximum bipartite matching (revisited)
Section 24.3 demonstrated one way to find a maximum matching in a
bipartite graph, by finding a maximum flow. This section provides a
more efficient method, the Hopcroft-Karp algorithm, which runs in
time. Figure 25.1(a) shows a matching in an undirected
bipartite graph. A vertex that has an incident edge in matching M is matched under M, and otherwise, it is unmatched. A maximal matching is a matching M to which no other edges can be added, that is, for every
edge e ∈ E − M, the edge set M ∪ { e} fails to be a matching. A maximum matching is always maximal, but the reverse does not always
hold.
Many algorithms to find maximum matchings, the Hopcroft-Karp
algorithm included, work by incrementally increasing the size of a
matching. Given a matching M in an undirected graph G = ( V, E), an M-alternating path is a simple path whose edges alternate between being
in M and being in E − M. Figure 25.1(b) depicts an M-augmenting path (sometimes called an augmenting path with respect to M): an M-
alternating path whose first and last edges belong to E − M. Since an
M-augmenting path contains one more edge in E − M than in M, it must consist of an odd number of edges.
Figure 25.1 A bipartite graph, where V = L ∪ R, L = { l 1, l 2, … , l 7}, and R = { r 1, r 2, … , r 8}.
(a) A matching M with cardinality 4, highlighted in blue. Matched vertices are blue, and unmatched vertices are tan. (b) The five edges highlighted in orange form an M-augmenting path P going between vertices l 6 and r 8. (c) The set of edges M′ = M ⊕ P highlighted in blue is a matching containing one more edge than M and adding l 6 and r 8 to the matched vertices. This matching is not a maximum matching (see Exercise 25.1-1).
Figure 25.1(c) demonstrates the following lemma, which shows that
by removing from matching M the edges in an M-augmenting path that
belong to M and adding to M the edges in the M-augmenting path that are not in M, the result is a new matching with one more edge than M.
Since a matching is a set of edges, the lemma relies on the notion of the
symmetric difference of two sets: X ⊕ Y = ( X − Y) ∪ ( Y − X), that is, the elements that belong to X or Y, but not both. Alternatively, you can think of X ⊕ Y as ( X ∪ Y)−( X ∩ Y). The operator ⊕ is commutative and associative. Furthermore, X ⊕ X = Ø and X ⊕ Ø = Ø ⊕ X = X for any set X, so that the empty set is the identity for ⊕.
Lemma 25.1
Let M be a matching in any undirected graph G = ( V, E), and let P be an M-augmenting path. Then the set of edges M′ = M ⊕ P is also a
matching in G with | M′| = | M| + 1.
Proof Let P contain q edges, so that ⌈q/2⌉ edges belong to E − M and
⌊q/2⌊ edges belong to M, and let these q edges be ( v 1, v 2), ( v 2, v 3), … , ( vq, vq+1). Because P is an M-augmenting path, vertices v 1 and vq+1
are unmatched under M and all other vertices in P are matched. Edges
( v 1, v 2), ( v 3, v 4), … , ( vq, vq+1) belong to E − M, and edges ( v 2, v 3), ( v 4, v 5), … , ( vq−1, vq) belong to M. The symmetric difference M′ = M
⊕ P reverses these roles, so that edges ( v 1, v 2), ( v 3, v 4), … , ( vq, vq+1) belong to M′ and ( v 2, v 3), ( v 4, v 5), … , ( vq−1, vq) belong to E − M′.
Each vertex v 1, v 2, … , vq, vq+1 is matched under M′, which gains one additional edge relative to M, and no other vertices or edges in G are affected by the change from M to M′. Hence, M′ is a matching in G, and
| M′| = | M| + 1.
▪
Since taking the symmetric difference of a matching M with an M-
augmenting path increases the size of the matching by 1, the following
corollary shows that taking the symmetric difference of M with k
vertex-disjoint M-augmenting paths increases the size of the matching
by k.
Corollary 25.2
Let M be a matching in any undirected graph G = ( V, E) and P 1, P 2, …
, Pk be vertex-disjoint M-augmenting paths. Then the set of edges M′ =
M ⊕ ( P 1 ∪ P 2 ∪ … ∪ Pk) is a matching in G with | M′| = | M| + k.
Proof Since the M-augmenting paths P 1, P 2, … , Pk are vertex-disjoint, we have that P 1 ∪ P 2 ∪ ⋯∪ Pk = P 1⊕ P 2 ⊕⋯⊕ Pk. Because the operator ⊕ is associative, we have
M ⊕ ( P 1 ∪ P 2 ∪ ⋯ ∪ = M ⊕ ( P 1 ⊕ P 2 ⊕ ⋯ ⊕ Pk) Pk)
=
(⋯ (( M ⊕ P 1) ⊕ P 2) ⊕ ⋯ ⊕ Pk−1) ⊕
Pk.
A simple induction on i using Lemma 25.1 shows that M ⊕ ( P 1 ∪ P 2 ∪
⋯ ∪ Pi−1) is a matching in G containing | M| + i − 1 edges and that path Pi is an augmenting path with respect to M ⊕ ( P 1 ∪ P 2 ∪ ⋯ ∪
Pi−1). Each of these augmenting paths increases the size of the
matching by 1, and so | M′| = | M| + k.
▪
As the Hopcroft-Karp algorithm goes from matching to matching, it
will be useful to consider the symmetric difference between two
matchings.
Lemma 25.3
Let M and M* be matchings in graph G = ( V, E), and consider the graph G′ = ( V, E′), where E′ = M ⊕ M*. Then, G′ is a disjoint union of simple paths, simple cycles, and/or isolated vertices. The edges in each
such simple path or simple cycle alternate between M and M*. If | M*| >
| M|, then G′ contains at least | M*|−| M| vertex-disjoint M-augmenting paths.
Proof Each vertex in G′ has degree 0, 1, or 2, since at most two edges of E′ can be incident on a vertex: at most one edge from M and at most
one edge from M*. Therefore, each connected component of G′ is either
a singleton vertex, an even-length simple cycle with edges alternately in
M and M*, or a simple path with edges alternately in M and M*. Since E′ = M ⊕ M*
= ( M ∪ M*) − ( M ∩ M*)
and | M*| > | M|, the edge set E′ must contain | M*| − | M| more edges from M* than from M. Because each cycle in G′ has an even number of edges drawn alternately from M and M*, each cycle has an equal number of edges from M and M*. Therefore, the simple paths in G′
account for there being | M*| − | M| more edges from M* than M. Each
path containing a different number of edges from M and M* either starts and ends with edges from M, containing one more edge from M
than from M*, or starts and ends with edges from M*, containing one
more edge from M* than from M. Because E′ contains | M*| − | M| more edges from M* than from M, there are at least | M*| − | M| paths of the latter type, and each one is an M-augmenting path. Because each vertex
has at most two incident edges from E′, these paths must be vertex-
disjoint.
▪
If an algorithm finds a maximum matching by incrementally
increasing the size of the matching, how does it determine when to stop?
The following corollary gives the answer: when there are no augmenting
paths.
Corollary 25.4
Matching M in graph G = ( V, E) is a maximum matching if and only if G contains no M-augmenting path.
Proof We prove the contrapositive of both directions of the lemma statement. The contrapositive of the forward direction is
straightforward. If there is an M-augmenting path P in G, then by Lemma 25.1, the matching M ⊕ P contains one more edge than M, meaning that M could not be a maximum matching.
To show the contrapositive of the backward direction—if M is not a
maximum matching, then G contains an M-augmenting path—let M*
be a maximum matching in Lemma 25.3, so that | M*| > | M|. Then G
contains at least | M*| − | M| > 0 vertex-disjoint M-augmenting paths.
▪
We already have learned enough to create a maximum-matching
algorithm that runs in O( VE) time. Start with the matching M empty.
Then repeatedly run a variant of either breadth-first search or depth-
first search from an unmatched vertex that takes alternating paths until
you find another unmatched vertex. Use the resulting M-augmenting
path to increase the size of M by 1.
The Hopcroft-Karp algorithm


The Hopcroft-Karp algorithm improves the running time to
.
The procedure HOPCROFT-KARP is given an undirected bipartite
graph, and it uses Corollary 25.2 to repeatedly increase the size of the
matching M it finds. Corollary 25.4 proves that the algorithm is correct,
since it terminates once there are no M-augmenting paths. It remains to
show that the algorithm does run in
time. We’ll see that the
repeat loop of lines 2–5 iterates
times and how to implement line
3 so that it runs in O( E) time in each iteration.
HOPCROFT-KARP ( G)
1 M = Ø
2 repeat
3
let P = { P 1, P 2, … , Pk} be a maximal set of vertex-disjoint shortest M-augmenting paths
4
M = M ⊕ ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk)
5 until P == Ø
6 return M
Let’s first see how to find a maximal set of vertex-disjoint shortest
M-augmenting paths in O( E) time. There are three phases. The first phase forms a directed version GM of the undirected bipartite graph G.
The second phase creates a directed acyclic graph H from GM via a variant of breadth-first search. The third phase finds a maximal set of
vertex-disjoint shortest M-augmenting paths by running a variant of
depth-first search on the transpose H T of H. (Recall that the transpose of a directed graph reverses the direction of each edge. Since H is acyclic, so is H T.)
Given a matching M, you can think of an M-augmenting path P as
starting at an unmatched vertex in L, traversing an odd number of
edges, and ending at an unmatched vertex in R. The edges in P traversed from L to R must belong to E − M, and the edges in P traversed from R
to L must belong to M. The first phase, therefore, creates the directed graph GM by directing the edges accordingly: GM = ( V, EM), where
Figure 25.2 (a) The directed graph GM created in the first phase for the undirected bipartite graph G and matching M in Figure 25.1(a). Breadth-first distances from any unmatched vertex in L appear next to each vertex. (b) The dag H created from GM in the second phase. Because the smallest distance to an unmatched vertex in R is 3, vertices l 7 and r 8, with distances greater than 3, are not in H.
EM = {( l, r) : l ∈ L, r ∈ R, and ( l, r) ∈ E − M } (edges from L to R)
∪ {( r, l) : r ∈ R, l ∈ L, and ( l, r) ∈ M } (edges from R to L).
Figure 25.2(a) shows the graph GM for the graph G and matching M in
The dag H = ( VH, EH) created by the second phase has layers of
vertices. Figure 25.2(b) shows the dag H corresponding to the directed graph GM in part (a) of the figure. Each layer contains only vertices from L or only vertices from R, alternating from layer to layer. The layer that a vertex resides in is given by that vertex’s minimum breadth-
first distance in GM from any unmatched vertex in L. Vertices in L
appear in even-numbered layers, and vertices in R appear in odd-
numbered layers. Let q denote the smallest distance in GM of any unmatched vertex in R. Then, the last layer in H contains the vertices in R with distance q. Vertices whose distance exceeds q do not appear in VH. (The graph H in Figure 25.2(b) omits vertices l 7 and r 8 because their distances from any unmatched vertex in L exceed q = 3.) The edges in EH form a subset of EM:
EH = {( l, r) ∈ EM : r. d ≤ q and r. d = l. d + 1} ∪ {( r, l) ∈ EM : l. d ≤ q}, where the attribute d of a vertex gives the vertex’s breadth-first distance
in GM from any unmatched vertex in L. Edges that do not go between
two consecutive layers are omitted from EH.
To determine the breadth-first distances of vertices, run breadth-first
search on the graph GM, but starting from all the unmatched vertices in
L. (In the BFS procedure on page 556, replace the root vertex s by the
set of unmatched vertices in L.) The predecessor attributes π computed
by the BFS procedure are not needed here, since H is a dag and not necessarily a tree.
Every path in H from a vertex in layer 0 to an unmatched vertex in
layer q corresponds to a shortest M-augmenting path in the original bipartite graph G. Just use the undirected versions of the directed edges
in H. Moreover, every shortest M-augmenting path in G is present in H.
The third phase identifies a maximal set of vertex-disjoint shortest
M-augmenting paths. As Figure 25.3 shows, it starts by creating the transpose H T of H. Then, for each unmatched vertex r in layer q, it performs a depth-first search starting from r until it either reaches a vertex in layer 0 or has exhausted all possible paths without reaching a
vertex in layer 0. Instead of maintaining discovery and finish times, the
depth-first search just needs to keep track of the predecessor attributes π
in the depth-first tree of each search. Upon reaching a vertex in layer 0,
tracing back along the predecessors identifies an M-augmenting path.
Each vertex is searched from only when it is first discovered in any
search. If the search from a vertex r in layer q cannot find a path of
undiscovered vertices to an undiscovered vertex in layer 0, then no M-
augmenting path including r goes into the maximal set.
Figure 25.3 shows the result of the third phase. The first depth-first search starts from vertex r 1. It identifies the M-augmenting path 〈( r 1, l 3), ( l 3, r 3), ( r 3, l 1)〉, which is highlighted in orange, and discovers vertices r 1, l 3, r 3, and l 1. The next depth-first search starts from vertex r 4. This search first examines the edge ( r 4, l 3), but because l 3 was already discovered, it backtracks and examines edge ( r 4, l 5). From there, it continues and identifies the M-augmenting path 〈( r 4, l 5), ( l 5, r 7), ( r 7, l 6)〉, which is highlighted in yellow, and discovers vertices r 4, l 5, r 7, and l 6. The depth-first search from vertex r 6 gets stuck at vertices l 3
and l 5, which have already been discovered, and so this search fails to
find a path of undiscovered vertices to a vertex in layer 0. There is no
depth-first search from vertex r 5 because it is matched, and depth-first
searches start from unmatched vertices. Therefore, the maximal set of
vertex-disjoint shortest M-augmenting paths found contains just the
two M-augmenting paths (〈 r 1, l 3), ( l 3, r 3), ( r 3, l 1)〉 and 〈( r 4, l 5), ( l 5, r 7), ( r 7, l 6)〉.
You might have noticed that in this example, this maximal set of two
vertex-disjoint shortest M-augmenting paths is not a maximum set. The
graph contains three vertex-disjoint shortest M-augmenting paths: 〈( r 1, l 2), ( l 2, r 2), ( r 2, l 1)〉, 〈( r 4, l 3), ( l 3, r 3), ( r 3, l 4)〉, and 〈( r 6, l 5), ( l 5, r 7), ( r 7, l 6)〉. No matter: the algorithm requires the set of vertex-disjoint shortest
M-augmenting paths found in line 3 of HOPCROFT-KARP to be only
maximal, not necessarily maximum.
Figure 25.3 The transpose H T of the dag H created in the third phase. The first depth-first search, starting from vertex r 1, identifies the M-augmenting path 〈( r 1, l 3), ( l 3, r 3), ( r 3, l 1)〉
highlighted in orange, and it discovers vertices r 1, l 3, r 3, l 1. The second depth-first search, starting from vertex r 4, identifies the M-augmenting path 〈( r 4, l 5), ( l 5, r 7), ( r 7, l 6)〉 highlighted in yellow, discovering vertices r 4, l 5, r 7, l 6.
It remains to show that all three phases of line 3 take O( E) time. We
assume that in the original bipartite graph G, each vertex has at least
one incident edge so that | V| = O( E), which in turn implies that | V| + | E|
= O( E). The first phase creates the directed graph GM by simply directing each edge of G, so that | VM| = | V| and | EM = | E|. The second phase performs a breadth-first search on GM, taking O( VM + EM) =
O( EM) = O( E) time. In fact, it can stop once the first distance in the queue within the breadth-first search exceeds the shortest distance q to
an unmatched vertex in R. The dag H has | VH| ≤ | VM| and | EH| ≤ | EM|, so that it takes O( VH + EH) = O( E) time to construct. Finally, the third phase performs depth-first searches from the unmatched vertices in
layer q. Once a vertex is discovered, it is not searched from again, and so
the analysis of depth-first search from Section 20.3 applies here: O( VH
+ EH) = O( E). Hence, all three phases take just O( E) time.
Once the maximal set of vertex-disjoint shortest M-augmenting
paths have been found in line 3, updating the matching in line 4 takes
O( E) time, as it is just a matter of going through the edges of the M-
augmenting paths and adding edges to and removing edges from the
matching M. Thus, each iteration of the repeat loop of lines 2–5 can run
in O( E) time.
It remains to show that the repeat loop iterates
times. We
start with the following lemma, which shows that after each iteration of
the repeat loop, the length of an augmenting path increases.
Lemma 25.5
Let G = ( V, E) be an undirected bipartite graph with matching M, and let q be the length of a shortest M-augmenting path. Let P = { P 1, P 2,
… , Pk} be a maximal set of vertex-disjoint M-augmenting paths of length q. Let M′ = M ⊕ ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk), and suppose that P is a shortest M′-augmenting path. Then P has more than q edges.
Proof We consider separately the cases in which P is vertex-disjoint from the augmenting paths in P and in which it is not vertex-disjoint.
First, assume that P is vertex-disjoint from the augmenting paths in
P. Then, P contains edges that are in M but are not in any of P 1, P 2, …
, Pk, so that P is also an M-augmenting path. Since P is disjoint from P 1, P 2, … , Pk but is also an M-augmenting path, and since P is a maximal set of shortest M-augmenting paths, P must be longer than any of the augmenting paths in P, each of which has length q.
Therefore, P has more than q edges.
Now, assume that P visits at least one vertex from the M-augmenting
paths in P. By Corollary 25.2, M ′ is a matching in G with | M′| = | M| +
k. Since P is an M′-augmenting path, by Lemma 25.1, M′ ⊕ P is a matching with | M′ ⊕ P| = | M′| + 1 = | M| + k + 1. Now let A = M ⊕ M′
⊕ P. We claim that A = ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk) ⊕ P: A = M ⊕ M′ ⊕ P
=
M ⊕ (M ⊕ ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk)) ⊕ P
= ( M ⊕ M) ⊕ ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk) ⊕ P (associativity of ⊕)
= Ø ⊕ ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk) ⊕ P
( X ⊕ X = Ø for all X)
= ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk) ⊕ P
(Ø ⊕ X = X for all X).
Lemma 25.3 with M* = M′ ⊕ P gives that A contains at least | M′ ⊕ P|
− | M| = k + 1 vertex-disjoint M-augmenting paths. Since each such M-
augmenting path has at least q edges, we have | A| ≥ ( k + 1) q = kq + q.
Now we claim that P shares at least one edge with some M-
augmenting path in P. Under the matching M′, every vertex in each M-
augmenting path in P is matched. (Only the first and last vertex in each
M-augmenting path Pi is unmatched under M, and under M ⊕ Pi, all vertices in Pi are matched. Because the M-augmenting paths in P are
vertex-disjoint, no other path in P can affect whether the vertices in Pi
are matched. That is, the vertices in Pi are matched under ( M ⊕ Pi) ⊕
Pj if and only if they are matched under M ⊕ Pi, for any other path Pj
∈ P.) Suppose that P shares a vertex v with some path Pi ∈ P. Vertex v cannot be an endpoint of P, because the endpoints of P are unmatched
under M′. Therefore, v has an incident edge in P that belongs to M′.
Since any vertex has at most one incident edge in a matching, this edge
must also belong to Pi, thus proving the claim.
Because A = ( P 1 ∪ P 2 ∪ ⋯ ∪ Pk) ⊕ P and P shares at least one edge with some Pi ∈ P, we have that | A| < | P 1 ∪ P 2 ∪ ⋯ ∪ Pk| + | P|.
Thus, we have
kq + q ≤ | A|
< | P 1 ∪ P 2 ∪ ⋯ ∪ Pk| + | P|
= kq + | P|,
so that q < | P|. We conclude that P contains more than q edges.
▪





The next lemma bounds the size of a maximum matching, based on
the length of a shortest augmenting path.
Lemma 25.6
Let M be a matching in graph G = ( V, E), and let a shortest M-
augmenting path in G contain q edges. Then the size of a maximum matching in G is at most | M| + | V| / ( q + 1).
Proof Let M* be a maximum matching in G. By Lemma 25.3, G
contains at least | M*| − | M| vertex-disjoint M-augmenting paths. Each of these paths contains at least q edges, and hence at least q + 1 vertices.
Because these paths are vertex-disjoint, we have (| M*|−| M|)( q+1) ≤ | V|, so that | M*| ≤ | M| + | V|/( q+1).
▪
The final lemma bounds the number of iterations of the repeat loop
of lines 2–5.
Lemma 25.7
When the HOPCROFT-KARP procedure runs on an undirected
bipartite graph G = ( V, E), the repeat loop of lines 2–5 iterates times.
Proof By Lemma 25.5, the length q of the shortest M-augmenting paths found in line 3 increases from iteration to iteration. After
iterations, therefore, we must have
. Consider the situation
after the first time line 4 executes with M-augmenting paths whose
length is at least
. Since the size of a matching increases by at
least one edge per iteration, Lemma 25.6 implies that the number of
additional iterations before achieving a maximum matching is at most
Hence, the total number of loop iterations is less than
.
▪


Thus, we have the following bound on the running time of the
HOPCROFT-KARP procedure.
Theorem 25.8
The procedure HOPCROFT-KARP runs in
time on an
undirected bipartite graph G = ( V, E).
Proof By Lemma 25.7 the repeat loop iterates
times, and we
have seen how to implement each iteration in O( E) time.
▪
Exercises
25.1-1
Use the Hopcroft-Karp algorithm to find a maximum matching for the
graph in Figure 25.1.
25.1-2
How are M-augmenting paths and augmenting paths in flow networks
similar? How do they differ?
25.1-3
What is the advantage of searching in the transpose H T from
unmatched vertices in layer q (the first layer that contains an unmatched
vertex in R) to layer 0 versus searching in the dag H from layer 0 to layer q?
25.1-4
Show how to bound the number of iterations of the the repeat loop of
lines 2–5 of HOPCROFT-KARP by
.
★ 25.1-5
A perfect matching is a matching under which every vertex is matched.
Let G = ( V, E) be an undirected bipartite graph with vertex partition V
= L ∪ R, where | L| = | R|. For any X ⊆ V, define the neighborhood of X
as
N( X) = { y ∈ V : ( x, y) ∈ E for some x ∈ X}, that is, the set of vertices adjacent to some member of X. Prove Hall’s
theorem: there exists a perfect matching in G if and only if | A| ≤ | N( A)|
for every subset A ⊆ L.
25.1-6
In a d-regular graph, every vertex has degree d. If G = ( V, E) is bipartite with vertex partition V = L ∪ R and also d-regular, then | L| = | R|. Use Hall’s theorem (see Exercise 25.1-5) to prove that every d-regular
bipartite graph contains a perfect matching. Then use that result to
prove that every d-regular bipartite graph contains d disjoint perfect matchings.
25.2 The stable-marriage problem
In Section 25.1, the goal was to find a maximum matching in an undirected bipartite graph. If you know that the graph G = ( V, E) with vertex partition V = L ∪ R is a complete bipartite graph1—containing an edge from every vertex in L to every vertex in R—then you can find a maximum matching by a simple greedy algorithm.
When a graph can have several matchings, you might want to decide
which matchings are most desirable. In Section 25.3, we’ll add weights to the edges and find a matching of maximum weight. In this section,
we will instead add some information to each vertex in a complete
bipartite graph: a ranking of the vertices in the other side. That is, each
vertex in L has an ordered list of all the vertices in R, and vice-versa. To keep things simple, let’s assume that L and R each contain n vertices.
The goal here is to match each vertex in L with a vertex in R in a
“stable” way.
This problem derives its name, the stable-marriage problem, from the
notion of heterosexual marriage, viewing L as a set of women and R as
a set of men.2 Each woman ranks all the men in terms of desirability, and each man does the same with all the women. The goal is to pair up
women and men (a matching) so that if a woman and a man are not
matched to each other, then at least one of them prefers their assigned partner.
If a woman and a man are not matched to each other but each
prefers the other over their assigned partner, they form a blocking pair.
A blocking pair has incentive to opt out of the assigned pairing and get
together on their own. If that were to occur, then this pair would block
the matching from being “stable.” A stable matching, therefore, is a
matching that has no blocking pair. If there is a blocking pair, then the
matching is unstable.
Let’s look at an example with four women—Wanda, Emma, Lacey,
and Karen—and four men—Oscar, Davis, Brent, and Hank—having
the following preferences:
Wanda:Brent, Hank, Oscar, Davis
Emma: Davis, Hank, Oscar, Brent
Lacey: Brent, Davis, Hank, Oscar
Karen: Brent, Hank, Davis, Oscar
Oscar: Wanda, Karen, Lacey, Emma
Davis: Wanda, Lacey, Karen, Emma
Brent: Lacey, Karen, Wanda, Emma
Hank: Lacey, Wanda, Emma, Karen
A stable matching comprises the following pairs:
Lacey and Brent
Wanda and Hank
Karen and Davis
Emma and Oscar
You can verify that this matching has no blocking pair. For example,
even though Karen prefers Brent and Hank to her partner Davis, Brent
prefers his partner Lacey to Karen, and Hank prefers his partner
Wanda to Karen, so that neither Karen and Brent nor Karen and Hank
form a blocking pair. In fact, this stable matching is unique. Suppose
instead that the last two pairs were
Emma and Davis
Then Karen and Davis would be a blocking pair, because they were not
paired together, Karen prefers Davis to Oscar, and Davis prefers Karen
to Emma. Therefore, this matching is not stable.
Stable matchings need not be unique. For example, suppose that
there are three women—Monica, Phoebe, and Rachel—and three men
—Chandler, Joey, and Ross—with these preferences:
Monica: Chandler, Joey, Ross
Phoebe: Joey, Ross, Chandler
Rachel:
Ross, Chandler, Joey
Chandler: Phoebe, Rachel, Monica
Joey:
Rachel, Monica, Phoebe
Ross:
Monica, Phoebe, Rachel
In this case, there are three stable matchings:
Matching 1
Matching 2
Matching 3
Monica and Chandler Phoebe and Chandler Rachel and Chandler
Phoebe and Joey
Rachel and Joey
Monica and Joey
Rachel and Ross
Monica and Ross
Phoebe and Ross
In matching 1, all women get their first choice and all men get their last
choice. Matching 2 is the opposite, with all men getting their first choice
and all women getting their last choice. When all the women or all the
men get their first choice, there plainly cannot be a blocking pair. In
matching 3, everyone gets their second choice. You can verify that there
are no blocking pairs.
You might wonder whether it is always possible to come up with a
stable matching no matter what rankings each participant provides. The
answer is yes. (Exercise 25.2-3 asks you to show that even in the scenario
of the National Resident Matching Program, where each hospital takes
on multiple students, it is always possible to devise a stable assignment.)
A simple algorithm known as the Gale-Shapley algorithm always finds
a stable matching. The algorithm has two variants, which mirror each
other: “woman-oriented” and “man-oriented.” Let’s examine the
woman-oriented version. Each participant is either “free” or “engaged.”
Everyone starts out free. Engagements occur when a free woman
proposes to a man. When a man is first proposed to, he goes from free
to engaged, and he always stays engaged, though not necessarily to the
same woman. If an engaged man receives a proposal from a woman
whom he prefers to the woman he’s currently engaged to, that
engagement is broken, the woman to whom he had been engaged
becomes free, and the man and the woman whom he prefers become
engaged. Each woman proposes to the men in her preference list, in
order, until the last time she becomes engaged. When a woman is
engaged, she temporarily stops proposing, but if she becomes free again,
she continues down her list. Once everyone is engaged, the algorithm
terminates. The procedure GALE-SHAPLEY on the next page makes
this process more concrete. The procedure allows for some choice: any
free woman may be selected in line 2. We’ll see that the procedure
produces a stable matching regardless of the order in which line 2
chooses free women. For the man-oriented version, just reverse the roles
of men and women in the procedure.
Let’s see how the GALE-SHAPLEY procedure executes on the
example with Wanda, Emma, Lacey, Karen, Oscar, Davis, Brent, and
Hank. After everyone is initialized to free, here is one possible version
of what can occur in successive iterations of the while loop of lines 2–9:
1. Wanda proposes to Brent. Brent is free, so that Wanda and
Brent become engaged and no longer free.
2. Emma proposes to Davis. Davis is free, so that Emma and Davis
become engaged and no longer free.
3. Lacey proposes to Brent. Brent is engaged to Wanda, but he
prefers Lacey. Brent breaks the engagement to Wanda, who
becomes free. Lacey and Brent become engaged, with Lacey no
longer free.
4. Karen proposes to Brent. Brent is engaged to Lacey, whom he
prefers to Karen. Brent rejects Karen, who remains free.
5. Karen proposes to Hank. Hank is free, so that Karen and Hank
become engaged and no longer free.
6. Wanda proposes to Hank. Hank is engaged to Karen, but he
prefers Wanda. Hank breaks the engagement with Karen, who
becomes free. Wanda and Hank become engaged, with Wanda
no longer free.
7. Karen proposes to Davis. Davis is engaged to Emma, but he
prefers Karen. Davis breaks the engagement to Emma, who
becomes free. Karen and Davis become engaged, with Karen no
longer free.
8. Emma proposes to Hank. Hank is engaged to Wanda, whom he
prefers to Emma. Hank rejects Emma, who remains free.
9. Emma proposes to Oscar. Oscar is free, so that Emma and Oscar
become engaged and no longer free.
GALE-SHAPLEY ( men, women, rankings)
1 assign each woman and man as free
2 while some woman w is free
3
let m be the first man on w’s ranked list to whom she has not proposed
4
if m is free
5
w and m become engaged to each other (and not free)
6
elseif m ranks w higher than the woman w′ he is currently engaged to
7
m breaks the engagement to w′, who becomes free
8
w and m become engaged to each other (and not free)
9
else m rejects w, with w remaining free
10 return the stable matching consisting of the engaged pairs
At this point, everyone is engaged and nobody is free, so the while loop
terminates. The procedure returns the stable matching we saw earlier.
The following theorem shows that not only does GALE-SHAPLEY
terminate, but that it always returns a stable matching, thereby proving
that a stable matching always exists.
The procedure GALE-SHAPLEY always terminates and returns a
stable matching.
Proof Let’s first show that the while loop of lines 2–9 always
terminates, so that the procedure terminates. The proof is by
contradiction. If the loop fails to terminate, it is because some woman
remains free. In order for a woman to remain free, she must have
proposed to all the men and been rejected by each one. In order for a
man to reject a woman, he must be already engaged. Therefore, all the
men are engaged. Once engaged, a man stays engaged (though not
necessarily to the same woman). There are an equal number n of women
and men, however, which means that every woman is engaged, leading
to the contradiction that no women are free. We must also show that the
while loop makes a bounded number of iterations. Since each of the n
women goes through her ranking of the n men in order, possibly not
reaching the end of her list, the total number of iterations is at most n 2.
Therefore, the while loop always terminates, and the procedure returns a
matching.
We need to show that there are no blocking pairs. We first observe
that once a man m is engaged to a woman w, all subsequent actions for
m occur in lines 6–8. Therefore, once a man is engaged, he stays
engaged, and any time he breaks an engagement to a woman w, it’s for a
woman whom he prefers to w. Suppose that a woman w is matched with
a man m, but she prefers man m′. We’ll show that w and m′ is not a blocking pair, because m′ does not prefer w to his partner. Because w ranks m′ higher than m, she must have proposed to m′ before proposing to m, and m′ either rejected her proposal or accepted it and later broke the engagement. If m′ rejected the proposal from w, it is because he was already engaged to some woman he prefers to w. If m′ accepted and later broke the engagement, he was at some point engaged to w but later
accepted a proposal from a woman he prefers to w. In either case, he
ultimately ends up with a partner whom he prefers to w. We conclude
that even though w might prefer m′ to her partner m, it is not also the case that m′ prefers w to his partner. Therefore, the procedure returns a matching containing no blocking pairs.
Exercise 25.2-1 asks you to provide the proof of the following
corollary.
Corollary 25.10
Given preference rankings for n women and n men, the Gale-Shapley algorithm can be implemented to run in O( n 2) time.
▪
Because line 2 can choose any free woman, you might wonder
whether different choices can produce different stable matchings. The
answer is no: as the following theorem shows, every execution of the
GALE-SHAPLEY produces exactly the same result. Moreover, the
stable matching returned is optimal for the women.
Theorem 25.11
Regardless of how women are chosen in line 2 of GALE-SHAPLEY,
the procedure always returns the same stable matching, and in this
stable matching, each woman has the best partner possible in any stable
matching.
Proof The proof that each woman has the best partner possible in any
stable matching is by contradiction. Suppose that the GALE-
SHAPLEY procedure returns a stable matching M, but that there is a
different stable matching M′ in which some woman w prefers her partner m′ to the partner m she has in M. Because w ranks m′ higher than m, she must have proposed to m′ before proposing to m. Then there is a woman w′ whom m′ prefers to w, and m′ was already engaged to w′ when w proposed or m′ accepted the proposal from w and later broke the engagement in favor of w′. Either way, there is a moment
when m′ decided against w in favor of w′. Now suppose, without loss of generality, that this moment was the first time that any man rejected a
partner who belongs to some stable matching.
We claim that w′ cannot have a partner m″ in a stable matching whom she prefers to m′. If there were such a man m″, then in order for
w′ to propose to m′, she would have proposed to m″ and been rejected at
some point before proposing to m′. If m′ accepted the proposal from w and later broke it to accept w′, then since this was the first rejection in a
stable matching, we get the contradiction that m″ could not have
rejected w′ beforehand. If m″ was already engaged to w′ when w proposed, then again, m″ could not have rejected w′ beforehand, thus proving the claim.
Since w′ does not prefer anyone to m′ in a stable matching and w′ is not matched with m′ in M′ (because m′ is matched with w in M′), w′
prefers m′ to her partner in M′. Since w′ prefers m′ over her partner in M′ and m′ prefers w′ over his partner w in M′, the pair w′ and m′ is a blocking pair in M′. Because M′ has a blocking pair, it cannot be a stable matching, thereby contradicting the assumption that there exists
some stable matching in which each woman has the best partner
possible other than the matching M returned by GALE-SHAPLEY.
We put no condition on the execution of the procedure, which means
that all possible orders in which line 2 selects women result in the same
stable matching being returned.
▪
Corollary 25.12
There can be stable matchings that the GALE-SHAPLEY procedure
does not return.
Proof Theorem 25.11 says that for a given set of rankings, GALE-
SHAPLEY returns just one matching, no matter how it chooses women
in line 2. The earlier example of three women and three men with three
different stable matchings shows that there can be multiple stable
matchings for a given set of rankings. A call of GALE-SHAPLEY is
capable of returning only one of these stable matchings.
▪
Although the GALE-SHAPLEY procedure gives the best possible
outcome for the women, the following corollary shows that it also
produces the worst possible outcome for the men.
Corollary 25.13
In the stable matching returned by the procedure GALE-SHAPLEY, each man has the worst partner possible in any stable matching.
Proof Let M be the matching returned by a call to GALE-SHAPLEY.
Suppose that there is another stable matching M′ and a man m who prefers his partner w in M to his partner w′ in M′. Let the partner of w in M′ be m′. By Theorem 25.11, m is the best partner that w can have in any stable matching, which means that w prefers m to m′. Since m prefers w to w′, the pair w and m is a blocking pair in M′, contradicting the assumption that M′ is a stable matching.
▪
Exercises
25.2-1
Describe how to implement the Gale-Shapley algorithm so that it runs
in O( n 2) time.
25.2-2
Is it possible to have an unstable matching with just two women and two
men? If so, provide and justify an example. If not, argue why not.
25.2-3
The National Resident Matching Program differs from the scenario for
the stable-marriage problem set out in this section in two ways. First, a
hospital may be matched with more than one student, so that hospital h
takes rh ≥ 1 students. Second, the number of students might not equal
the number of hospitals. Describe how to modify the Gale-Shapley
algorithm to fit the requirements of the National Resident Matching
Program.
25.2-4
Prove the following property, which is known as weak Pareto optimality:
Let M be the stable matching produced by the GALE-
SHAPLEY procedure, with women proposing to men. Then,
for a given instance of the stable-marriage problem there is no
matching—stable or unstable—such that every woman has a
partner whom she prefers to her partner in the stable matching
M.
25.2-5
The stable-roommates problem is similar to the stable-marriage problem,
except that the graph is a complete graph, not bipartite, with an even
number of vertices. Each vertex represents a person, and each person
ranks all the other people. The definitions of blocking pairs and stable
matching extend in the natural way: a blocking pair comprises two
people who both prefer each other to their current partner, and a
matching is stable if there are no blocking pairs. For example, consider
four people—Wendy, Xenia, Yolanda, and Zelda—with the following
preference lists:
Wendy: Xenia, Yolanda, Zelda
Xenia: Wendy, Zelda, Yolanda
Yolanda: Wendy, Zelda, Xenia
Zelda:
Xenia, Yolanda, Wendy
You can verify that the following matching is stable:
Wendy and Xenia
Yolanda and Zelda
Unlike the stable-marriage problem, the stable-roommates problem can
have inputs for which no stable matching exists. Find such an input and
explain why no stable matching exists.
25.3 The Hungarian algorithm for the assignment problem
Let us once again add some information to a complete bipartite graph
G = ( V, E), where V = L ∪ R. This time, instead of having the vertices of each side rank the vertices on the other side, we assign a weight to
each edge. Again, let’s assume that the vertex sets L and R each contain n vertices, so that the graph contains n 2 edges. For l ∈ L and r ∈ R,
denote the weight of edge ( l, r) by w( l, r), which represents the utility gained by matching vertex l with vertex r.