Hopcroft–Karp Maximum Bipartite Matching #
Pure executable specification and correctness proofs for the Hopcroft–Karp algorithm
([HK73]; [CLRS09], Problem 26-6) on G : Fin m × Fin r → Bool with candidate
neighbour lists adj : Fin m → List (Fin r). Returns a maximum matching together with a minimum
König vertex cover after at most 2⌊√ν⌋ augmenting phases (ν the maximum matching size).
Machine-checked O((m + r + |E|) √ν) sparse and O((m + r)² √ν) dense running-time bounds on the
word RAM live in CslibFrontier.Algorithms.WordRAM.HopcroftKarp.
Algorithm and source mapping #
The matching is stored as a right-indexed mate table mate : Vector (Option (Fin m)) r
(mateMatching). Each round of hkLoop is:
- Breadth-first layering (
hkBfs): computes the exact alternating layersIsLayerofCslibFrontier.Algorithms.Lean.MaxFlow.AugmentingPathin FIFO queue order up to the first layerkadjacent to a free right vertex (hkBfs_spec). If there is none, the layers have closed up and the unreached left vertices together with the reached right vertices form a König cover certifying maximality (isMaxMatching_isMinVertexCover_of_stable). - Depth-first phase (
hkPhase): a depth-first search from every free left vertex along the layers, sharing one visited table over right vertices, collects vertex-disjoint shortest augmenting paths (hkDfsRoots). The collected edge list is anIsLayeredFamilyadmitting no further layered path, hence anIsMaximalLayeredAugmentation; the mate table is then rematched along it (hkPhase_spec). The phase lemmaIsMaximalLayeredAugmentation.noShortAug_augmentand the phase boundIsPhaseRun.le_two_mul_sqrtgive at most2⌊√ν⌋rounds that augment. This is slightly sharper than the2⌊√s⌋ + 2bound of [HK73], Theorem 3, which counts distinct shortest augmenting path lengths.
The phase bound and Berge's theorem are proved through the layered counting lemma
card_mul_le_of_noShortAug rather than the textbook decomposition of M ⊕ M* into
vertex-disjoint augmenting paths; see CslibFrontier.Algorithms.Lean.MaxFlow.AugmentingPath.
Main definitions #
BipAdjValid,bipAdjSteps,denseBipAdj,exactBipAdj: candidate neighbour lists and their total length.mateMatching: the matching encoded by a mate table.hkBfs,hkDfs,hkDfsRoots,hkPhase,hkLoop: layering, depth-first search, one phase, and the phase loop.HopcroftKarpOutput,hopcroftKarp,hopcroftKarpDense,decidePerfectMatchingHK.vecPotential,hkDfsCost: potential functions bounding total vertex/edge inspections per round (reused by the word-RAM cost analysis).
Main results #
hopcroftKarp_correct: maximum matching, minimum vertex cover, equal sizes (König), and at most2⌊√ν⌋phases.hopcroftKarpDense_correct,hopcroftKarp_exactBipAdj: the dense instance and its agreement with exact filtered neighbour lists.card_hopcroftKarp_matching_eq: agreement in matching size with any maximum matching ofG.decidePerfectMatchingHK_eq_true_iff: perfect-matching decision characterization (HasPerfectMatching).
References #
Graph inputs and mate tables #
Breadth-first layering #
State of the expansion of one breadth-first layer.
Layer index of each discovered left vertex.
Left vertices discovered for the next layer (in FIFO order).
- found : Bool
Whether a free right vertex is adjacent to the current layer.
Inspect the candidate edge (u, v) from layer j: a free v sets found; the mate w of a
matched v is discovered in layer j + 1 unless already discovered.
Depth-first search for vertex-disjoint shortest augmenting paths #
Scan the candidates vs of u in layer i. With accept, take a free unvisited v;
otherwise descend through an unvisited matched v whose mate lies in layer i + 1 using
next. Visited right vertices are never revisited. Returns the visited table and, on success,
the found path's non-matching edges prepended to acc.
Output of hopcroftKarp.
Maximum-cardinality matching.
Minimum-cardinality König vertex cover.
- phases : ℕ
Number of augmenting phases.
Hopcroft–Karp with dense candidate lists denseBipAdj m r.
Potentials for round cost analysis #
Total cost of the entries of t satisfying p.
The initial DFS potential is at most bipAdjSteps adj whenever mate encodes a matching.
Matched left vertices #
Layer 0 #
Breadth-first search: one layer #
Breadth-first search: correctness #
Specification of a breadth-first layering result.
- noShort : NoShortAug G M b.k
Depth-first search: correctness #
Rematching along an edge list with distinct right ends: v is matched to u afterwards iff
(u, v) is in the list, or v is not rematched and was matched to u before.
Two edge lists with distinct right ends and the same edge set induce the same mate table.
Rematching a mate table along a compatible edge list realizes augment.
One phase and phase loop #
Postcondition of the phase loop started with phase counter n from mate.
- closed : ∃ (k : ℕ), altLeft G (mateMatching res.mate) (k + 1) = altLeft G (mateMatching res.mate) k ∧ altRight G (mateMatching res.mate) k ⊆ matchedRight (mateMatching res.mate) ∧ ∀ (u : Fin m), vget res.dist u = none ↔ u ∉ altLeft G (mateMatching res.mate) k
Exact filtered adjacency equivalence #
Running hopcroftKarp on exact filtered neighbour lists exactBipAdj G produces the exact
same output as hopcroftKarpDense G.
Extraction and main theorems #
Correctness of Hopcroft–Karp. hopcroftKarp G adj returns a maximum matching and a
minimum König vertex cover of equal size, after at most 2⌊√ν⌋ augmenting phases, where ν is
the maximum matching size.
Correctness of hopcroftKarpDense.
Hopcroft–Karp returns a matching of the same cardinality as any maximum matching M of G.