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 be a bipartite graph, with every edge between and . A matching is a set of edges without common ends. An -alternating path is a path whose edges are alternately outside and inside . An -augmenting path is an -alternating path of at least one edge whose two ends are both exposed (covered by no edge of ). If is -augmenting, then 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, is the matching given by the arrays and , which stay consistent: exactly when . Line L1 makes .
Lemma 1 (one search). Consider a call of TRY at line L4, where is exposed and all marks are false. While the call runs, does not change until the first time line L10 runs. The call returns true if and only if has an augmenting path that starts at , and in that case, on return, the new matching is for an augmenting path from .
Proof. Termination. Each call of TRY that is made from line L9 is made just after a right vertex 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 calls of TRY in total, and each call scans a finite list.
The calls follow alternating paths. While no call has returned true, does not change. At each moment the active calls are TRY, TRY, ..., TRY with , where TRY was called by TRY at line L9 for a right vertex with . The vertices are distinct, as each was marked once. So the mates are distinct, and none is , which is exposed. Hence is a path whose edges are not in (since is matched to or exposed) and whose edges are in : an -alternating path from .
If the call returns true. The first true value comes from TRY at line L9 for a vertex that is not marked before and has $\text{mate}_R[v_k] = $ nil. Then is an alternating path with both ends exposed, so it is -augmenting. Each TRY then runs line L10 with and returns true to its caller. After all of them, and for . The pairs of are replaced by the pairs , and no other pair changes. This is .
If the call returns false. Then no line L10 ran, so did not change. Let be the set of marked right vertices and . Each is matched: an exposed marked vertex makes the test of line L9 true. TRY was called on each vertex of : on at line L4, and on right after was marked. Each of these calls returned false, so it ran its loop to the end, and each neighbour of its vertex was marked, by this call or earlier. So . Now let be any -alternating path from . Its first edge is not in , as is exposed, so . Since is bipartite, the vertices of alternate between and ; the edges from to are in , and the edges from to are not. By induction along : if , then is matched and , and then . So every right vertex of is in and is matched, and every left vertex of other than is matched as well. So does not end at an exposed vertex other than , and it is not augmenting.
Lemma 2 (dead vertices stay dead). Let be a matching, a vertex exposed by such that has no augmenting path with as an end, and an -augmenting path. Then is exposed by , and has no augmenting path with as an end.
Proof. The ends of are exposed by and are not , by the hypothesis. The inner vertices of are covered by , so is not on , and stays exposed. Every vertex of is covered by by an edge of .
Assume that is an -augmenting path with the end . If has no vertex on , then no edge of is in , and and agree on the edges of , so is -augmenting with end , a contradiction. Otherwise, let be the first vertex of , counted from , that is on , and let be the part of from to . The edges of have at most the end on , so none is an edge of , and and agree on them. The last edge of ends at and is not in ; the -edge at is in ; so the last edge of is not in , and not in . So is an -alternating path from whose first and last edges are outside .
If is an end of , it is exposed by , and is -augmenting with end , a contradiction. Otherwise is an inner vertex of , and the -edge at is on . Let be the part of from that starts with this -edge, up to the end of in that direction. is -alternating, starts with an edge of and ends at an end of , which is exposed by . The paths and share only , because meets only at . So is a path from to an -exposed vertex other than , and it alternates at , because the last edge of is outside and the first edge of is in . It is -augmenting with end , a contradiction.
Theorem. KUHN stops and returns a maximum matching of .
Proof. Each round stops by Lemma 1, and there are rounds. Each round changes only by an augmentation (Lemma 1), so 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 , every left vertex handled so far is matched or dead. Before the round of , is exposed: the augmentation of the round of an earlier vertex newly covers only and one right vertex. By Lemma 1, the round either matches , or it fails, and then 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 .
At the end every left vertex is matched or dead. Assume that has an augmenting path . Its two ends are exposed, and it has an odd number of edges, so, as is bipartite, one end is in . That end is an exposed left vertex, so it is dead, and no augmenting path has it as an end, a contradiction. So has no augmenting path, and by Berge's lemma it is a maximum matching.