Part VI · Graph Algorithms Chapter 25

Matchings in Bipartite Graphs

Pairing one set with another — the largest number of pairs, the most stable set of pairs, and the cheapest set of pairs. Three problems, three quite different algorithms.

A matching pairs up elements of two disjoint sets so that nobody is used twice: workers to jobs, students to schools, doctors to hospitals. Chapter 25 covers three versions. Maximum bipartite matching reduces to max flow from Chapter 24 and is sped up by Hopcroft-Karp. The stable-marriage problem asks not for the most pairs but for pairs nobody wants to defect from, and is solved by the Gale-Shapley algorithm. The assignment problem adds costs and is solved by the Hungarian algorithm.

4th edition note. This chapter is new. Maximum bipartite matching was §26.3 of the 3rd edition’s max-flow chapter; the stable-marriage and Hungarian material is newly added, giving matchings a chapter of their own.

Contents

  1. Matchings
  2. Maximum matching via max flow
  3. Augmenting paths, matching version
  4. Hopcroft-Karp
  5. The stable-marriage problem
  6. Gale-Shapley
  7. The assignment problem
  8. Recap

Matchings

Given an undirected graph G = (V,E), a matching is a subset M ⊆ E such that no two edges in M share a vertex. A vertex covered by an edge of M is matched; otherwise it is free. A maximum matching is one of largest cardinality. A perfect matching covers every vertex.

A graph is bipartite if V splits into L and R with every edge running between the two sides. Bipartite matching is far easier than general matching, and this chapter stays bipartite.

Maximum matching via max flow

The reduction is short and exact.

Given bipartite G = (L ∪ R, E), build a flow network G′: - add a source s with an edge s → u for every u ∈ L - direct every original edge from L to R - add a sink t with an edge v → t for every v ∈ R - give every edge capacity 1
s t u₁u₂u₃ v₁v₂v₃ L every capacity is 1, so no vertex can be used twice — max flow = maximum matching
Figure 25.1 — The reduction. Unit capacities on the source and sink edges are exactly the constraint that each vertex is matched at most once.
Why it is exact. The capacity-1 edge from s to each u ∈ L allows at most one unit through u, so at most one of its edges can be used — precisely the matching condition. Symmetrically on the right. And by the integrality theorem from Chapter 24, an integer-capacity network has an integer maximum flow, so each edge carries 0 or 1 and the flow is a set of edges. Without integrality the reduction would allow fractional half-matches and mean nothing.

Cost: the max flow value is at most min(|L|, |R|) = O(V), and each augmentation costs O(E), so Ford-Fulkerson gives O(V E).

Augmenting paths, matching version

The flow language translates into matching language directly, and the direct version is how the problem is usually implemented.

An augmenting path with respect to a matching M is a path that starts at a free vertex in L, ends at a free vertex in R, and alternates between edges not in M and edges in M. Flipping every edge along it — unmatched become matched and vice versa — increases the matching size by exactly one, because the path has one more unmatched edge than matched.
Berge’s theorem. A matching M is maximum if and only if there is no augmenting path with respect to M. This is the matching-language statement of the max-flow min-cut condition from Chapter 24.

The obvious algorithm follows: repeatedly find an augmenting path by DFS or BFS and flip it. At most O(V) augmentations, each O(E), so O(V E).

Hall’s theorem gives the matching-theoretic companion to min-cut: a bipartite graph has a matching saturating every vertex of L if and only if, for every subset S ⊆ L, the neighbourhood N(S) satisfies |N(S)| ≥ |S|. A violating set S is the certificate that no perfect matching exists — the analogue of exhibiting a small cut.

Hopcroft-Karp

Instead of one augmenting path per round, Hopcroft-Karp finds a maximal set of vertex-disjoint shortest augmenting paths by BFS and augments along all of them at once.

The analysis mirrors Edmonds-Karp. The length of the shortest augmenting path strictly increases after each phase, and once it exceeds √V only O(√V) augmentations can remain. So there are O(√V) phases, each costing O(E):

O(√V · E)
AlgorithmTimeIdea
Ford-Fulkerson reductionO(V E)One augmenting path at a time
Hopcroft-KarpO(√V · E)A maximal disjoint set of shortest paths per phase

The stable-marriage problem

A different objective entirely. Here every vertex is matchable and |L| = |R| = n; the question is not how many pairs but which pairs, given that everyone has a full preference ranking of the other side.

A matching is unstable if there is a blocking pair: two people, not matched to each other, who each prefer the other to their current partner. Such a pair would defect. A matching with no blocking pair is stable.
Note what stability is not. It does not maximise anyone’s happiness, nor minimise total dissatisfaction. It only guarantees that no pair has a mutual incentive to break away. A stable matching can leave many people with poor partners.

Gale-Shapley

GALE-SHAPLEY (proposal / deferred-acceptance) 1 while some proposer p is free and has not proposed to everyone 2 r = the highest-ranked receiver on p's list p has not yet asked 3 if r is free 4 tentatively match p and r 5 elseif r prefers p to their current partner p′ 6 match p and r; p′ becomes free // r trades up 7 else 8 r rejects p // p stays free, moves down the list

O(n²): each proposer asks each receiver at most once, so there are at most proposals.

Theorem. Gale-Shapley always terminates with a perfect and stable matching. Termination holds because no proposer ever repeats a proposal. Stability holds because if p prefers r to their partner, then p must already have proposed to r and been rejected or later dropped — which means r has someone they prefer to p. So no blocking pair can exist.
The algorithm is not neutral. Gale-Shapley produces the proposer-optimal stable matching: every proposer gets the best partner they could have in any stable matching, and simultaneously every receiver gets their worst such partner. Which side proposes materially changes the outcome. This is not a curiosity — the algorithm runs the US National Resident Matching Program, and the question of whether hospitals or students propose was the subject of a real reform. Also, receivers can sometimes benefit by misreporting preferences, while proposers cannot.

The assignment problem

The weighted version. Given an n × n cost matrix, find a perfect matching of minimum total cost — assign n workers to n jobs as cheaply as possible.

Brute force is n!. The Hungarian algorithm (Kuhn-Munkres) solves it in polynomial time.

The key observation: subtracting a constant from every entry of a row, or of a column, does not change which perfect matching is optimal — every perfect matching uses exactly one entry from each row and each column, so all of them shift by the same total. This is the same telescoping idea as Johnson’s reweighting in Chapter 23.
Hungarian algorithm, outline 1. Subtract the row minimum from every row. 2. Subtract the column minimum from every column. 3. Cover all zeros with the minimum number of lines. 4. If the number of lines equals n, an optimal assignment exists among the zeros — extract it. 5. Otherwise let m be the smallest uncovered entry. Subtract m from every uncovered entry, add m to every doubly covered entry, and go back to step 3.

Running time is O(n³) in its standard implementation. Modern presentations phrase it as maintaining a dual feasible solution (the row and column potentials) alongside a partial matching, and it is a genuine special case of linear programming — a preview of Chapter 29.

ProblemObjectiveAlgorithmTime
Maximum bipartite matchingMost pairsMax flow / augmenting pathsO(VE)
SameMost pairsHopcroft-KarpO(√V E)
Stable marriageNo blocking pairGale-ShapleyO(n²)
AssignmentMinimum total costHungarianO(n³)

Recap

The seven things to carry forward

Where this goes next

Part VI is complete. Part VII is a tour of selected topics, and it opens with two chapters that change the computational model rather than the problem: Chapter 26 on parallel algorithms, where work and span replace running time, and Chapter 27 on online algorithms, where the input arrives one piece at a time and decisions cannot be revised.


Ch 24 — Maximum Flow Ch 26 — Parallel Algorithms