Part III · trunk B · combinatorial algorithms · spine
König and Hall, Hopcroft–Karp, the Hungarian method as primal–dual with its certificate in plain sight, Edmonds' blossoms, and stable matching for contrast.
needs 14~2 h slides + 3 h lab (+ blossom stretch)lab: uv run co lab 15
15 · overview
Berge's augmenting paths, König and Hall, Hopcroft–Karp's $O(E\sqrt V)$.
The LP and its dual; the Hungarian method as primal–dual; the certificate.
Tutte's theorem, blossoms, the matching polytope and its odd-set inequalities.
Gale–Shapley: a different question with the same word.
Words with a dotted underline are glossary terms: click one for its definition. G lists every term in the unit, N shows the notes. Optional extra: interactive figures, linked from the slides they support by a small ▸ chip; skip them freely.
section 1
One augmenting path at a time, then many at once.
15 · bipartite
ℓ₂ and r₀ are free. The path r₀–ℓ₀–r₁–ℓ₁–r₂–ℓ₂ alternates free, matched, free, matched, free. Flip it and the matching grows from 2 to 3.
For a matching $M$, an alternating path alternates edges outside and inside $M$. It is augmenting if both its ends are free. Then the symmetric difference $M \mathbin{\triangle} P$ is a matching with one more edge.
$M$ is maximum if and only if it has no augmenting path.
Why: overlay $M$ with a bigger matching. The overlay falls into paths and even cycles, and the extra edges of the bigger matching must sit on some path, which is augmenting for $M$.
full proof in the notes (N)
A maximal matching (nothing can be added) need not be maximum: the figure's thick edges are maximal. Berge's theorem holds in every graph. Only finding the path is harder when the graph is not bipartite (section 3).
15 · bipartite
In a bipartite graph, maximum matching size = minimum vertex cover size.
König's construction: from the free left vertices, follow alternating paths. Take the left vertices you didn't reach and the right vertices you did. That set covers every edge and has exactly one vertex per matched edge. The next slide does it.
full proof in the notes (N)
There is a matching that saturates $L$ iff $|N(S)| \ge |S|$ for every $S \subseteq L$, with $N(S)$ the neighbours of $S$.
From König: a cover $A \cup B$ with $A \subseteq L$, $B \subseteq R$ and $|A| + |B| < |L|$ would leave $S = L \setminus A$ with $N(S) \subseteq B$, so $|N(S)| \le |B| < |S|$.
Matching LP: $\max \mathbf 1^\top x$, $x(\delta(v)) \le 1$. Its dual is the fractional vertex cover. The incidence matrix is TU (unit 06), so both LPs have integral optima, and LP strong duality is König.
15 · bipartite · example
Maximum matching $\{\ell_0 r_0, \ell_2 r_1\}$, size 2. Filled: the cover $\{\ell_2, r_0\}$, size 2.
1. Free left vertices: $\ell_1$. Start there.
2. Unmatched edge $\ell_1 \to r_0$. Matched edge $r_0 \to \ell_0$. From $\ell_0$ there is no other edge. Reached: $Z = \{\ell_1, r_0, \ell_0\}$.
3. Cover $=$ left vertices not reached $\{\ell_2\}$ $\cup$ right vertices reached $\{r_0\}$.
Check: $r_0$ covers $\ell_0 r_0, \ell_1 r_0, \ell_2 r_0$; $\ell_2$ covers $\ell_2 r_1, \ell_2 r_2$. Size 2 = matching size, so both are optimal.
The reached left vertices $S = \{\ell_0, \ell_1\}$ have $N(S) = \{r_0\}$: two vertices, one neighbour. No matching saturates the left side, and this set proves it.
15 · bipartite
Each phase costs $O(E)$. It is Dinic (unit 14) on the unit-capacity network.
$O(\sqrt V)$ phases, so $O(E\sqrt V)$ total.
Why: each phase makes the shortest augmenting path longer. After $\sqrt V$ phases the missing paths are all long, and long disjoint paths can't be numerous, so few phases remain.
full proof in the notes (N)
Lab then, degree 4: yours 0.070 s at 16 000 vertices a side, networkx 0.317 s.
section 2
Where unit 03's duality first pays off outside LP.
15 · assignment
$u, v$: the dual potentials.
Integral vertices (Birkhoff–von Neumann: the doubly stochastic matrices are the convex hull of the permutation matrices), so the LP optimum is an assignment.
If $\sigma$ is a permutation, $u_i + v_j \le c_{ij}$ everywhere, and $u_i + v_{\sigma(i)} = c_{i\sigma(i)}$ for every $i$, then $\sigma$ is optimal.
For any permutation $\tau$: $\ c(\tau) = \sum_i c_{i\tau(i)} \ge \sum_i (u_i + v_{\tau(i)}) = \sum u + \sum v = c(\sigma)$. $\square$
Checking takes $O(n^2)$ and needs no solver. The tests never call scipy for step 4.
Lab then: HiGHS's simplex vertex had zero fractional entries at every n up to 400.
15 · assignment
The Hungarian method, a primal–dual method. Keep dual-feasible $(u, v)$ and a partial assignment using only tight edges ($c_{ij} = u_i + v_j$). To add row $i$:
Keep $\text{minv}[j]$, the smallest reduced cost into column $j$ from the tree. Each dual step then costs $O(n)$, not $O(n^2)$. There are at most $n$ steps per row and $n$ rows.
It is successive shortest paths for min-cost flow: Dijkstra on reduced costs, with $(u, v)$ as the potentials. $\text{minv}$ is Dijkstra's tentative distance, and step 3 is "settle the nearest node".
15 · assignment · example
| add row | what happens | $u$ | $v$ |
|---|---|---|---|
| 1 | cheapest entry 1, column 2: raise $u_1$ by 1, assign | $(1, 0, 0)$ | $(0, 0, 0)$ |
| 2 | wants column 2 too (reduced cost 0), which row 1 holds. Tree $\{2, 1\}$: next tight edge costs 2, to column 1. Shift by 2, reroute | $(3, 2, 0)$ | $(0, -2, 0)$ |
| 3 | best reduced cost 2, column 3 free: raise $u_3$ by 2, assign | $(3, 2, 2)$ | $(0, -2, 0)$ |
Result: row 1 → column 2, row 2 → column 1, row 3 → column 3. Cost $1 + 2 + 2 = 5$.
Reduced costs $c_{ij} - u_i - v_j$ at the end:
$$\begin{pmatrix} 1 & \mathbf 0 & 0 \\ \mathbf 0 & 0 & 3 \\ 1 & 2 & \mathbf 0 \end{pmatrix}$$All $\ge 0$: dual feasible. Zero on every assigned entry (bold): tight. And $\sum u + \sum v = 7 - 2 = 5$ = the cost. So 5 is optimal, without looking at the other 5 permutations.
Brute force over all 6 permutations agrees: the minimum is 5.
15 · assignment
Lab then, costs uniform in $0..10^6$, seconds. All four totals are equal.
| n | yours | scipy | OR-Tools | HiGHS LP |
|---|---|---|---|---|
| 50 | 0.002 | 0.0001 | 0.0014 | 0.051 |
| 100 | 0.012 | 0.0003 | 0.0056 | 0.046 |
| 200 | 0.085 | 0.0016 | 0.022 | 0.233 |
| 400 | 0.505 | 0.0063 | 0.089 | 1.332 |
linear_sum_assignment) is a C shortest-augmenting-path code, the same asymptotics as yours, about 80× faster.section 3
Odd cycles, and why "polynomial" means "good".
15 · general graphs
From the free root, an alternating walk reaches the base, then goes round an odd cycle. Each cycle vertex is reachable at both parities, so "left/right" labels break.
$G$ has a perfect matching iff $\ \mathrm{odd}(G - U) \le |U|$ for every $U \subseteq V$.
$\mathrm{odd}(G - U)$ is the number of odd components after deleting $U$. Each needs a vertex of $U$ to match out. The Tutte–Berge formula generalises this: $\nu(G) = \min_U \tfrac12\bigl(|V| + |U| - \mathrm{odd}(G - U)\bigr)$.
In a bipartite graph every vertex has a fixed side, so the BFS labels it once. In the figure, a BFS that labels each vertex once can miss augmenting paths that go round the cycle.
15 · general graphs
Let $B$ be a blossom (an odd alternating cycle reached at its base). The contraction $G / B$ is $G$ with $B$ shrunk to a single vertex. Then $M$ has an augmenting path in $G$ iff $M / B$ has one in $G / B$.
In words: shrinking a blossom never hides or invents an augmenting path. The next slide shrinks one.
The algorithm: grow alternating trees. When an edge joins two even vertices of the same tree, find their lowest common ancestor (the base) and contract the cycle. When you reach a free vertex, expand the blossoms back along the path and augment.
$O(V^3)$ as in the lab (contraction as base[v] relabelling). $O(E\sqrt V)$ by Micali–Vazirani (1980).
Edmonds argued that an algorithm whose running time is bounded by a polynomial in the input size is the right formal notion of a good algorithm. His evidence: perfect matching has exponentially many candidates, no totally unimodular LP, and yet a good algorithm.
Cobham (1965) made the same proposal independently. Their thesis is why P became the dividing line of unit 22.
15 · general graphs · example
Matching $\{12, 34, 56\}$; free vertices 0 and 7. Search from 0: $0 - 1 = 2$, then $2 - 3 = 4$ and $2 - 6 = 5$ both reach the cycle, and edge $45$ joins two even vertices: an odd cycle $2,3,4,5,6$ with base 2. Shrink it to $B$.
In $G/B$: $0 - 1 = B - 7$ is an augmenting path (7 is adjacent to $B$ through 5).
Inside $B$, enter at the base 2 and leave at 5 by the even side: $2 - 6 = 5$. The path in $G$ is $0 - 1 = 2 - 6 = 5 - 7$.
Flip it: matching $\{01, 26, 57, 34\}$, size 4, perfect on 8 vertices. Going round the other way, $2 - 3 = 4 - 5$, arrives at 5 on a free edge; the next edge would have to be matched ($5 = 6$), and 7 is out of reach.
15 · general graphs
The convex hull of matchings of $G$, the matching polytope, is (with $E[S]$ the edges inside $S$)
$$\begin{aligned} x(\delta(v)) &\le 1 && \forall v \\ x(E[S]) &\le \tfrac{|S| - 1}{2} && \forall S \subseteq V,\ |S| \text{ odd} \\ x &\ge 0 \end{aligned}$$The odd-set inequalities cut off $x_e = \tfrac12$ on a triangle: $x(E[S]) = \tfrac32 > 1$.
section 4
A different question with the same word.
15 · stable matching
$n$ proposers and $n$ receivers, each ranking the other side. A matching is stable if no pair $(p, r)$ both prefer each other to their partners (a blocking pair).
It ends after at most $n^2$ proposals with a stable perfect matching. Every proposer gets their best stable partner (proposer-optimal), and every receiver their worst.
Used for the US medical residency match (NRMP, after Roth–Peranson 1999) and school choice. Roth and Shapley won the 2012 Nobel prize in economics.
15 · stable matching · example
| prefers, best first | |
|---|---|
| A | X, Y, Z |
| B | X, Y, Z |
| C | Y, X, Z |
| X | B, A, C |
| Y | A, B, C |
| Z | A, B, C |
A, B, C propose; X, Y, Z receive.
| # | proposal | answer | held |
|---|---|---|---|
| 1 | A → X | accept | X:A |
| 2 | B → X | accept, drop A | X:B |
| 3 | C → Y | accept | X:B, Y:C |
| 4 | A → Y | accept, drop C | X:B, Y:A |
| 5 | C → X | reject (holds B) | X:B, Y:A |
| 6 | C → Z | accept | X:B, Y:A, Z:C |
A would rather have X, but X holds B, whom X ranks higher. C would rather have Y or X, but each holds someone they rank above C. No blocking pair: stable.
15 · lab
uv run co test 15
uv run co then 15 # vs scipy, OR-Tools, HiGHS · networkx for bipartite and general
15 · checkpoint
Korte & Vygen, ch. 10–11.
Schrijver, A Course in Combinatorial Optimization, ch. 3, 5.
Edmonds, "Paths, trees, and flowers" (1965).
Gale & Shapley, Amer. Math. Monthly 69 (1962).
16 dynamic programming · 18 Régin's alldifferent filter is a matching algorithm · 23 Christofides needs a minimum-weight perfect matching.