Algorithm Search⌘K

Proofs: Kuhn's algorithm

These are the human proofs of Kuhn's algorithm, at full length.

Kuhn's algorithm returns a maximum matching full

This proof shows: correctness, termination.

Let G=(L∪R,E)G = (L \cup R, E) be a bipartite graph, with every edge between LL and RR. A matching MM is a set of edges without common ends. An MM-alternating path is a path whose edges are alternately outside and inside MM. An MM-augmenting path is an MM-alternating path of at least one edge whose two ends are both exposed (covered by no edge of MM). If PP is MM-augmenting, then M⊕P=(M∖P)∪(P∖M)M \oplus P = (M \setminus P) \cup (P \setminus M) is a matching with one more edge. We use Berge's lemma: a matching is maximum if and only if it has no augmenting path.

Throughout, MM is the matching given by the arrays mateL\text{mate}_L and mateR\text{mate}_R, which stay consistent: mateL[x]=v\text{mate}_L[x] = v exactly when mateR[v]=x\text{mate}_R[v] = x. Line L1 makes M=∅M = \emptyset.

Lemma 1 (one search). Consider a call of TRY(u)(u) at line L4, where u∈Lu \in L is exposed and all marks are false. While the call runs, MM does not change until the first time line L10 runs. The call returns true if and only if MM has an augmenting path that starts at uu, and in that case, on return, the new matching is M⊕PM \oplus P for an augmenting path PP from uu.

Proof. Termination. Each call of TRY that is made from line L9 is made just after a right vertex vv is marked at line L8, and a vertex is marked at most once, because line L7 tests the mark. So the call makes at most ∣R∣+1|R| + 1 calls of TRY in total, and each call scans a finite list.

The calls follow alternating paths. While no call has returned true, MM does not change. At each moment the active calls are TRY(x0)(x_0), TRY(x1)(x_1), ..., TRY(xk)(x_k) with x0=ux_0 = u, where TRY(xi+1)(x_{i+1}) was called by TRY(xi)(x_i) at line L9 for a right vertex vi∈Adj[xi]v_i \in \text{Adj}[x_i] with mateR[vi]=xi+1\text{mate}_R[v_i] = x_{i+1}. The vertices v0,…,vk−1v_0, \ldots, v_{k-1} are distinct, as each was marked once. So the mates x1,…,xkx_1, \ldots, x_k are distinct, and none is uu, which is exposed. Hence x0v0x1v1⋯xkx_0 v_0 x_1 v_1 \cdots x_k is a path whose edges xivix_i v_i are not in MM (since xix_i is matched to vi−1v_{i-1} or exposed) and whose edges vixi+1v_i x_{i+1} are in MM: an MM-alternating path from uu.

If the call returns true. The first true value comes from TRY(xk)(x_k) at line L9 for a vertex vk∈Adj[xk]v_k \in \text{Adj}[x_k] that is not marked before and has $\text{mate}_R[v_k] = $ nil. Then P=x0v0x1v1⋯xkvkP = x_0 v_0 x_1 v_1 \cdots x_k v_k is an alternating path with both ends exposed, so it is MM-augmenting. Each TRY(xi)(x_i) then runs line L10 with viv_i and returns true to its caller. After all of them, mateL[xi]=vi\text{mate}_L[x_i] = v_i and mateR[vi]=xi\text{mate}_R[v_i] = x_i for 0≤i≤k0 \le i \le k. The pairs xi+1vix_{i+1} v_i of MM are replaced by the pairs xivix_i v_i, and no other pair changes. This is M⊕PM \oplus P.

If the call returns false. Then no line L10 ran, so MM did not change. Let ZZ be the set of marked right vertices and S={u}∪{mateR[v]:v∈Z}S = \{u\} \cup \{\text{mate}_R[v] : v \in Z\}. Each v∈Zv \in Z is matched: an exposed marked vertex makes the test of line L9 true. TRY was called on each vertex of SS: on uu at line L4, and on mateR[v]\text{mate}_R[v] right after vv was marked. Each of these calls returned false, so it ran its loop to the end, and each neighbour vv of its vertex was marked, by this call or earlier. So N(S)⊆ZN(S) \subseteq Z. Now let Q=u w1y1w2y2…Q = u\,w_1 y_1 w_2 y_2 \ldots be any MM-alternating path from uu. Its first edge is not in MM, as uu is exposed, so w1∈N(u)⊆Zw_1 \in N(u) \subseteq Z. Since GG is bipartite, the vertices of QQ alternate between RR and LL; the edges from RR to LL are in MM, and the edges from LL to RR are not. By induction along QQ: if wi∈Zw_i \in Z, then wiw_i is matched and yi=mateR[wi]∈Sy_i = \text{mate}_R[w_i] \in S, and then wi+1∈N(yi)⊆Zw_{i+1} \in N(y_i) \subseteq Z. So every right vertex of QQ is in ZZ and is matched, and every left vertex of QQ other than uu is matched as well. So QQ does not end at an exposed vertex other than uu, and it is not augmenting. □\square

Lemma 2 (dead vertices stay dead). Let MM be a matching, uu a vertex exposed by MM such that MM has no augmenting path with uu as an end, and PP an MM-augmenting path. Then uu is exposed by M′=M⊕PM' = M \oplus P, and M′M' has no augmenting path with uu as an end.

Proof. The ends of PP are exposed by MM and are not uu, by the hypothesis. The inner vertices of PP are covered by MM, so uu is not on PP, and uu stays exposed. Every vertex of PP is covered by M′M' by an edge of PP.

Assume that QQ is an M′M'-augmenting path with the end uu. If QQ has no vertex on PP, then no edge of QQ is in PP, and MM and M′M' agree on the edges of QQ, so QQ is MM-augmenting with end uu, a contradiction. Otherwise, let xx be the first vertex of QQ, counted from uu, that is on PP, and let Q1Q_1 be the part of QQ from uu to xx. The edges of Q1Q_1 have at most the end xx on PP, so none is an edge of PP, and MM and M′M' agree on them. The last edge of Q1Q_1 ends at xx and is not in PP; the M′M'-edge at xx is in PP; so the last edge of Q1Q_1 is not in M′M', and not in MM. So Q1Q_1 is an MM-alternating path from uu whose first and last edges are outside MM.

If xx is an end of PP, it is exposed by MM, and Q1Q_1 is MM-augmenting with end uu, a contradiction. Otherwise xx is an inner vertex of PP, and the MM-edge at xx is on PP. Let P1P_1 be the part of PP from xx that starts with this MM-edge, up to the end of PP in that direction. P1P_1 is MM-alternating, starts with an edge of MM and ends at an end of PP, which is exposed by MM. The paths Q1Q_1 and P1P_1 share only xx, because Q1Q_1 meets PP only at xx. So Q1P1Q_1 P_1 is a path from uu to an MM-exposed vertex other than uu, and it alternates at xx, because the last edge of Q1Q_1 is outside MM and the first edge of P1P_1 is in MM. It is MM-augmenting with end uu, a contradiction. □\square

Theorem. KUHN stops and returns a maximum matching of GG.

Proof. Each round stops by Lemma 1, and there are ∣L∣|L| rounds. Each round changes MM only by an augmentation (Lemma 1), so MM is always a matching. Call a left vertex dead when it is exposed and the current matching has no augmenting path with it as an end.

We show by induction on the rounds: after the round of uu, every left vertex handled so far is matched or dead. Before the round of uu, uu is exposed: the augmentation of the round of an earlier vertex xx newly covers only xx and one right vertex. By Lemma 1, the round either matches uu, or it fails, and then uu is dead. A matched left vertex stays matched, because an augmentation only adds covered vertices. A dead vertex stays dead after an augmentation, by Lemma 2, and a failed round does not change MM.

At the end every left vertex is matched or dead. Assume that MM has an augmenting path PP. Its two ends are exposed, and it has an odd number of edges, so, as GG is bipartite, one end is in LL. That end is an exposed left vertex, so it is dead, and no augmenting path has it as an end, a contradiction. So MM has no augmenting path, and by Berge's lemma it is a maximum matching. ■\blacksquare

Back to Kuhn's algorithm