Documentation

CslibFrontier.Algorithms.WordRAM.HopcroftKarp

Hopcroft–Karp maximum bipartite matching on the word RAM #

This file implements and proves machine-level running-time bounds on the standard-basis word RAM (Basis.standard) for:

  1. Sparse Hopcroft–Karp (O((m + r + |E|) √ν)): given a bipartite graph on Fin m × Fin r represented in compressed sparse row (CSR) format (SparseInstance), coreCode alternates queue-based breadth-first layering (O(m + r + |E|) instructions per round, CslibFrontier.Algorithms.WordRAM.HopcroftKarp.BFS) and iterative explicit-stack depth-first augmentation (O(m + r + |E|) instructions per phase, CslibFrontier.Algorithms.WordRAM.HopcroftKarp.DFS), matching the pure specification Cslib.Algorithms.Lean.MaxFlow.hopcroftKarp (IsMaxMatching, at most 2⌊√ν⌋ augmenting phases by IsPhaseRun.le_two_mul_sqrt). This yields end-to-end State.init programs for maximum matching (sparseMatchingProgram), matching cardinality (sparseCardProgram), and balanced perfect-matching decision (sparsePerfectProgram).
  2. Dense Hopcroft–Karp (O((m + r)² √ν)): given an m × r 0/1 adjacency matrix (DenseInstance), converts the input matrix into exact CSR neighbour lists (exactBipAdj) in O(m + m r) instructions (buildCSRCode / matrixSetupCode, CslibFrontier.Algorithms.WordRAM.HopcroftKarp.MatrixCSR) and invokes coreCode, agreeing with hopcroftKarpDense.

The O(n^{5/2}) Dilworth minimum chain cover and maximum antichain endpoints built on top of matrixSetupCode and coreCode live in CslibFrontier.Algorithms.WordRAM.ChainCover.

Main definitions #

Main results #

References #

Core Hopcroft–Karp phase loop (coreCode) #

In-memory Hopcroft–Karp solver on a CSR bipartite graph inst stored at bOff. Initializes colMate[0..r) to m (none), then alternates bfsCode and dfsPhaseCode until no augmenting path remains, leaving the maximum matching in colMate[0..r) and the closed alternating-layer distances in dist[0..m).

Instruction bound O((m + r + E) (√ν + 1)) for coreCode on a CSR graph with sides m, r, E edge entries, and maximum matching cardinality ν.

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.core_exec {w bOff : ℕ} (inst : SparseInstance) (x : WordRAM.Data 34) (hb : BaseRegs inst bOff x) (hcsr : CSRMem inst bOff x.mem) (hfit : WordRAM.Fits w (bOff + 7 * inst.m + 5 * inst.r + inst.numEdges + 8)) :
have res := Lean.MaxFlow.hkLoop inst.graph inst.adj (inst.m + 1) 0 (Vector.replicate inst.r none); ∃ (y : WordRAM.Data 34), WordRAM.ExecWithin w coreCode x y (coreTime inst.m inst.r inst.numEdges inst.maxMatchingCard) ∧ BaseRegs inst bOff y ∧ CSRMem inst bOff y.mem ∧ (∀ (v : Fin inst.r), y.mem (bOff + inst.m + 1 + inst.numEdges + ↑v) = match Lean.MaxFlow.vget res.mate v with | some u => ↑u | none => inst.m) ∧ (∀ (u : Fin inst.m), y.mem (bOff + inst.m + 1 + inst.numEdges + inst.r + inst.m + ↑u) = match Lean.MaxFlow.vget res.dist u with | none => 0 | some j => j + 1) ∧ ∀ (a : ℕ), ¬(bOff + inst.m + 1 + inst.numEdges ≤ a ∧ a < bOff + inst.m + 1 + inst.numEdges + 4 * inst.r + 6 * inst.m + 6) → y.mem a = x.mem a

In-memory O((m + r + |E|) (√ν + 1)) execution of Hopcroft–Karp (coreCode). Starting from any state x with BaseRegs inst bOff x and CSRMem inst bOff x.mem, coreCode terminates within coreTime inst.m inst.r inst.numEdges inst.maxMatchingCard instructions, leaving the maximum matching res.mate in colMate[0..r) and the closed alternating-layer distances res.dist in dist[0..m), while preserving BaseRegs, CSRMem, and all memory outside the workspace [bOff + m + 1 + numEdges, outBase).

Matching output extraction (emitMatchingCode) #

End-to-end State.init sparse CSR endpoints #

Standard-basis word-RAM program returning inst.matchingWords (ExecutesList) on a CSR bipartite graph inst : SparseInstance.

Standard-basis word-RAM program returning the maximum matching cardinality inst.maxMatchingCard (Executes) on inst : SparseInstance.

Standard-basis word-RAM program deciding SparseInstance.HasPerfectMatching (1 iff balanced and admitting a perfect matching, 0 otherwise).

Instance-only O((m + r + E) √(min m r)) upper bound on sparseTime.

O((m + r + |E|) √ν) Word-RAM maximum bipartite matching (ExecutesList).

O((m + r + |E|) √ν) Word-RAM maximum matching cardinality (Executes).

O((m + r + |E|) √ν) Word-RAM perfect-matching decision (Executes).

End-to-end State.init dense endpoints #

Standard-basis word-RAM program returning inst.matchingWords (ExecutesList) on a dense m × r 0/1 adjacency matrix inst : DenseInstance.

Standard-basis word-RAM program returning inst.maxMatchingCard (Executes) on inst : DenseInstance.

Instruction bound O((m + r)² √ν) for denseMatchingProgram and denseCardProgram.

O((m + r)² √ν) Word-RAM dense maximum bipartite matching (ExecutesList).

O((m + r)² √ν) Word-RAM dense maximum matching cardinality (Executes).