Part III · trunk B · combinatorial algorithms · spine

15

Matching

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

The route

1 · bipartite cardinality

Berge's augmenting paths, König and Hall, Hopcroft–Karp's $O(E\sqrt V)$.

2 · assignment

The LP and its dual; the Hungarian method as primal–dual; the certificate.

3 · general graphs

Tutte's theorem, blossoms, the matching polytope and its odd-set inequalities.

4 · stable matching

Gale–Shapley: a different question with the same word.

getting around

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

Bipartite cardinality

One augmenting path at a time, then many at once.

15 · bipartite

Augmenting paths: Berge's theorem

ℓ₀ℓ₁ℓ₂ r₀r₁r₂ thick: matched · dashed: the path's free edges

ℓ₂ 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.

definitions

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.

Berge 1957

$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

König and Hall: min–max theorems

König 1931

In a bipartite graph, maximum matching size = minimum vertex cover size.

how to get the cover · lab step 2

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)

Hall 1935

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

through the course's lens

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

König's cover, on a small graph

ℓ₀ ℓ₁ ℓ₂ r₀ r₁ r₂ thick: matching · filled: cover

Maximum matching $\{\ell_0 r_0, \ell_2 r_1\}$, size 2. Filled: the cover $\{\ell_2, r_0\}$, size 2.

the construction, step by step

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.

and a Hall violator

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

Hopcroft–Karp: many shortest paths per phase

  1. BFS from all free left vertices at once, alternating edges, to get layers. Stop at the first layer that reaches a free right vertex.
  2. DFS from each free left vertex along layer-increasing edges only, taking a maximal set of vertex-disjoint shortest augmenting paths. Remove dead ends.
  3. Flip them all. Repeat.

Each phase costs $O(E)$. It is Dinic (unit 14) on the unit-capacity network.

Hopcroft & Karp 1973

$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

Assignment

Where unit 03's duality first pays off outside LP.

15 · assignment

The assignment LP and its dual

primal: the assignment problem $$\begin{aligned} \min\ & \textstyle\sum_{i,j} c_{ij} x_{ij} \\ \text{s.t.}\ & \textstyle\sum_j x_{ij} = 1 \ \ \forall i \\ & \textstyle\sum_i x_{ij} = 1 \ \ \forall j, \quad x \ge 0 \end{aligned}$$
dual $$\max \sum_i u_i + \sum_j v_j \quad \text{s.t.}\ u_i + v_j \le c_{ij}$$

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

the certificate · lab step 4

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 is primal–dual

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

  1. Grow an alternating tree from $i$ over tight edges.
  2. If it reaches a free column, augment. Done.
  3. Else let $\delta$ = the smallest reduced cost from a tree row to a non-tree column. Raise $u$ on tree rows by $\delta$ and lower $v$ on tree columns by $\delta$. Tree edges stay tight, all duals stay feasible, and at least one new edge becomes tight. Go to 1.
why this is cubic time

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.

the same thing, seen from unit 14

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

The Hungarian method, on a 3×3 matrix

$$c = \begin{pmatrix} 4 & 1 & 3 \\ 2 & 0 & 5 \\ 3 & 2 & 2 \end{pmatrix}$$
add rowwhat happens$u$$v$
1cheapest entry 1, column 2: raise $u_1$ by 1, assign$(1, 0, 0)$$(0, 0, 0)$
2wants 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)$
3best 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$.

the certificate, checked

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

How much faster is production code?

Lab then, costs uniform in $0..10^6$, seconds. All four totals are equal.

nyoursscipyOR-ToolsHiGHS LP
500.0020.00010.00140.051
1000.0120.00030.00560.046
2000.0850.00160.0220.233
4000.5050.00630.0891.332
  • scipy (linear_sum_assignment) is a C shortest-augmenting-path code, the same asymptotics as yours, about 80× faster.
  • OR-Tools uses cost-scaling push–relabel (Goldberg–Kennedy), designed for large sparse graphs rather than dense matrices.
  • The general LP is slowest, but is exact and integral here.

section 3

General graphs

Odd cycles, and why "polynomial" means "good".

15 · general graphs

Tutte's theorem, and why the bipartite search fails

root base

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.

Tutte 1947

$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

Edmonds 1965: shrink the blossom

the key lemma

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

"Paths, trees, and flowers", §2

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

One blossom, shrunk and expanded

0 1 2 3 4 5 6 7 free free base → 0 1 B 7 G / B: path 0 – 1 = B – 7
shrink

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

expand, then flip

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 matching polytope

Edmonds 1965

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

  • There are exponentially many of them, yet you can optimise over the polytope in polynomial time. Separation (Padberg–Rao 1982) is a minimum odd cut, via a Gomory–Hu tree. It's unit 09's pattern again: subtour elimination with an odd-parity twist.
  • No compact formulation exists. Rothvoss (2014) proved every extended formulation of the perfect matching polytope needs $2^{\Omega(n)}$ inequalities.
  • The weighted blossom algorithm keeps a dual variable on every odd set, and it's the primal–dual method on this LP.

section 4

Stable matching

A different question with the same word.

15 · stable matching

Gale–Shapley

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

  1. A free proposer proposes to the best receiver not yet tried.
  2. The receiver keeps the better of the current holder and the proposer.
Gale & Shapley 1962

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.

what is different
  • No weights, nothing summed: stability is a local, strategic condition.
  • Who proposes matters: the algorithm is not symmetric.
  • Proposing truthfully is a dominant strategy for proposers (strategy-proof) (Dubins–Freedman, Roth), but not for receivers.

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

Gale–Shapley, round by round

prefers, best first
AX, Y, Z
BX, Y, Z
CY, X, Z
XB, A, C
YA, B, C
ZA, B, C

A, B, C propose; X, Y, Z receive.

#proposalanswerheld
1A → XacceptX:A
2B → Xaccept, drop AX:B
3C → YacceptX:B, Y:C
4A → Yaccept, drop CX:B, Y:A
5C → Xreject (holds B)X:B, Y:A
6C → ZacceptX:B, Y:A, Z:C
why nobody wants to break it

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

Lab 15 — what you build

about 3 hours + a stretch session · uv run co lab 15
  1. Hopcroft–Karp.
  2. König's cover from your maximum matching.
  3. The Hungarian method, returning its potentials.
  4. The certificate: permutation + dual feasibility + tightness.
  5. Gale–Shapley and a stability checker.
  6. Stretch: Edmonds' blossom algorithm.
uv run co test 15
uv run co then 15     # vs scipy, OR-Tools, HiGHS · networkx for bipartite and general

15 · checkpoint

Done when

checkpoint
  • Lab steps 1–5 are ✓ (6 if you took the stretch).
  • You can verify your Hungarian algorithm's answer is optimal by checking complementary slackness against its dual potentials, without re-running the algorithm.
  • You can explain why König fails on a triangle and which inequality repairs the LP.

Reading

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

Next

16 dynamic programming · 18 Régin's alldifferent filter is a matching algorithm · 23 Christofides needs a minimum-weight perfect matching.