Documentation

CslibFrontier.Algorithms.Lean.MaxFlow.HopcroftKarp

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:

  1. Breadth-first layering (hkBfs): computes the exact alternating layers IsLayer of CslibFrontier.Algorithms.Lean.MaxFlow.AugmentingPath in FIFO queue order up to the first layer k adjacent 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).
  2. 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 an IsLayeredFamily admitting no further layered path, hence an IsMaximalLayeredAugmentation; the mate table is then rematched along it (hkPhase_spec). The phase lemma IsMaximalLayeredAugmentation.noShortAug_augment and the phase bound IsPhaseRun.le_two_mul_sqrt give at most 2⌊√ν⌋ rounds that augment. This is slightly sharper than the 2⌊√s⌋ + 2 bound 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 #

Main results #

References #

Graph inputs and mate tables #

def Cslib.Algorithms.Lean.MaxFlow.BipAdjValid {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) :

Candidate neighbour lists adj cover every edge of G.

Total length of the candidate neighbour lists (|E| for exact adjacency lists).

Dense candidate neighbour lists enumerating every right vertex.

def Cslib.Algorithms.Lean.MaxFlow.exactBipAdj {m r : ℕ} (G : Fin m × Fin r → Bool) (u : Fin m) :
List (Fin r)

Exact neighbour lists of G, enumerating the right neighbours of u in ascending order.

Matching encoded by a right-vertex mate table: (u, v) is matched iff mate[v] = some u.

@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.mem_mateMatching {m r : ℕ} {mate : Vector (Option (Fin m)) r} {e : Fin m × Fin r} :
e ∈ mateMatching mate ↔ vget mate e.2 = some e.1

Breadth-first layering #

def Cslib.Algorithms.Lean.MaxFlow.hkMarkStep {m r : ℕ} (mate : Vector (Option (Fin m)) r) (b : Vector Bool m) (v : Fin r) :

Mark the mate of the right vertex v.

Mark the left vertices matched by mate.

State of the expansion of one breadth-first layer.

  • dist : Vector (Option ℕ) m

    Layer index of each discovered left vertex.

  • next : List (Fin m)

    Left vertices discovered for the next layer (in FIFO order).

  • found : Bool

    Whether a free right vertex is adjacent to the current layer.

def Cslib.Algorithms.Lean.MaxFlow.hkBfsStep {m r : ℕ} (G : Fin m × Fin r → Bool) (mate : Vector (Option (Fin m)) r) (j : ℕ) (u : Fin m) (s : HKBfsState m) (v : Fin r) :

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.

def Cslib.Algorithms.Lean.MaxFlow.hkBfsLayer {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (j : ℕ) (us : List (Fin m)) (s : HKBfsState m) :

Expand the frontier us of layer j.

Result of a breadth-first layering: layer indices, the last layer k, and whether a free right vertex is adjacent to layer k.

  • dist : Vector (Option ℕ) m

    Layer index of each reached left vertex.

  • k : ℕ

    Index of the last expanded layer.

  • found : Bool

    Whether layer k reaches a free right vertex.

def Cslib.Algorithms.Lean.MaxFlow.hkBfsLoop {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) :
ℕ → ℕ → Vector (Option ℕ) m → List (Fin m) → HKBfsResult m

Expand layers j, j + 1, … until a free right vertex is found or no new vertex appears.

Put a free left vertex into layer 0 (in ascending FIFO order).

def Cslib.Algorithms.Lean.MaxFlow.hkBfs {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) :

Breadth-first layering of the alternating graph of the matching encoded by mate.

Depth-first search for vertex-disjoint shortest augmenting paths #

def Cslib.Algorithms.Lean.MaxFlow.hkDfsLevel {m r : ℕ} (G : Fin m × Fin r → Bool) (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) (accept : Bool) (i : ℕ) (next : Fin m → Vector Bool r → List (Fin m × Fin r) → Vector Bool r × Option (List (Fin m × Fin r))) (u : Fin m) :
List (Fin r) → Vector Bool r → List (Fin m × Fin r) → Vector Bool r × Option (List (Fin m × Fin r))

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.

def Cslib.Algorithms.Lean.MaxFlow.hkDfs {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) :
ℕ → ℕ → Fin m → Vector Bool r → List (Fin m × Fin r) → Vector Bool r × Option (List (Fin m × Fin r))

Depth-first search from u in layer i with f layers remaining.

def Cslib.Algorithms.Lean.MaxFlow.hkDfsRoots {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) (k : ℕ) :
List (Fin m) → Vector Bool r → List (Fin m × Fin r) → List (Fin m × Fin r)

Run the depth-first search from every layer-0 vertex of us, sharing the visited table.

def Cslib.Algorithms.Lean.MaxFlow.hkApplyEdges {m r : ℕ} (mate : Vector (Option (Fin m)) r) (es : List (Fin m × Fin r)) :
Vector (Option (Fin m)) r

Rematch the right end of every edge of es to its left end.

def Cslib.Algorithms.Lean.MaxFlow.hkPhase {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) (k : ℕ) :
Vector (Option (Fin m)) r

One augmenting phase for the layering dist with shortest augmenting parameter k.

Final state of the phase loop.

def Cslib.Algorithms.Lean.MaxFlow.hkLoop {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) :
ℕ → ℕ → Vector (Option (Fin m)) r → HKLoopResult m r

Alternate breadth-first layering and augmenting phases until no augmenting path remains.

def Cslib.Algorithms.Lean.MaxFlow.hkMatchingStep {m r : ℕ} (mate : Vector (Option (Fin m)) r) (a : Finset (Fin m × Fin r)) (v : Fin r) :

Collect the matched edge at the right vertex v.

Collect the left vertex u into the cover when it is unreached.

def Cslib.Algorithms.Lean.MaxFlow.hkCoverRightStep {m r : ℕ} (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) (a : Finset (Fin r)) (v : Fin r) :

Collect the right vertex v into the cover when its mate is reached.

Output of hopcroftKarp.

  • matching : Finset (Fin m × Fin r)

    Maximum-cardinality matching.

  • cover : VertexCover (Fin m) (Fin r)

    Minimum-cardinality König vertex cover.

  • phases : ℕ

    Number of augmenting phases.

def Cslib.Algorithms.Lean.MaxFlow.hopcroftKarp {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) :

Hopcroft–Karp on G : Fin m × Fin r → Bool with candidate neighbour lists adj.

Hopcroft–Karp with dense candidate lists denseBipAdj m r.

Potentials for round cost analysis #

def Cslib.Algorithms.Lean.MaxFlow.vecPotential {α : Type u_1} {n : ℕ} (p : α → Prop) [DecidablePred p] (c : Fin n → ℕ) (t : Vector α n) :

Total cost of the entries of t satisfying p.

theorem Cslib.Algorithms.Lean.MaxFlow.vecPotential_vset_add {α : Type u_1} {n : ℕ} {p : α → Prop} [DecidablePred p] {c : Fin n → ℕ} {t : Vector α n} {w : Fin n} {x : α} (hw : p (vget t w)) (hx : ¬p x) :
vecPotential p c (vset t w x) + c w = vecPotential p c t
theorem Cslib.Algorithms.Lean.MaxFlow.vecPotential_le_sum {α : Type u_1} {n : ℕ} {p : α → Prop} [DecidablePred p] (c : Fin n → ℕ) (t : Vector α n) :
vecPotential p c t ≤ ∑ u : Fin n, c u
def Cslib.Algorithms.Lean.MaxFlow.hkDfsCost {m r : ℕ} (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (v : Fin r) :

Cost charged to a right vertex during a DFS phase: the candidate list length of its mate.

theorem Cslib.Algorithms.Lean.MaxFlow.vecPotential_replicate_le {m r : ℕ} {G : Fin m × Fin r → Bool} {adj : Fin m → List (Fin r)} {mate : Vector (Option (Fin m)) r} (hM : IsMatching G (mateMatching mate)) :
vecPotential (fun (x : Bool) => x = false) (hkDfsCost adj mate) (Vector.replicate r false) ≤ bipAdjSteps adj

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 #

structure Cslib.Algorithms.Lean.MaxFlow.HKBfsSpec {m r : ℕ} (G : Fin m × Fin r → Bool) (M : Finset (Fin m × Fin r)) (b : HKBfsResult m) :

Specification of a breadth-first layering result.

theorem Cslib.Algorithms.Lean.MaxFlow.hkBfs_spec {m r : ℕ} {G : Fin m × Fin r → Bool} {adj : Fin m → List (Fin r)} {mate : Vector (Option (Fin m)) r} (hadj : BipAdjValid G adj) :
HKBfsSpec G (mateMatching mate) (hkBfs G adj mate)
theorem Cslib.Algorithms.Lean.MaxFlow.hkBfs_k_le {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) :
(hkBfs G adj mate).k ≤ m + 1
theorem Cslib.Algorithms.Lean.MaxFlow.bfsInit_potential {m : ℕ} (ml : Vector Bool m) (c : Fin m → ℕ) (l : List (Fin m)) (p : Vector (Option ℕ) m × List (Fin m)) :
vecPotential (fun (x : Option ℕ) => x = none) c (List.foldl (hkBfsInitStep ml) p l).1 + (List.map c (List.foldl (hkBfsInitStep ml) p l).2).sum ≤ vecPotential (fun (x : Option ℕ) => x = none) c p.1 + (List.map c p.2).sum + (List.map c l).sum
theorem Cslib.Algorithms.Lean.MaxFlow.layerFold_potential {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (c : Fin m → ℕ) (j : ℕ) (us : List (Fin m)) (s : HKBfsState m) :
vecPotential (fun (x : Option ℕ) => x = none) c (hkBfsLayer G adj mate j us s).dist + (List.map c (hkBfsLayer G adj mate j us s).next).sum = vecPotential (fun (x : Option ℕ) => x = none) c s.dist + (List.map c s.next).sum

Depth-first search: correctness #

theorem Cslib.Algorithms.Lean.MaxFlow.vget_foldl_apply {m r : ℕ} (mate : Vector (Option (Fin m)) r) (es : List (Fin m × Fin r)) (hnd : (List.map Prod.snd es).Nodup) (u : Fin m) (v : Fin r) :
vget (List.foldl (fun (mt : Vector (Option (Fin m)) r) (e : Fin m × Fin r) => vset mt e.2 (some e.1)) mate es) v = some u ↔ (u, v) ∈ es ∨ v ∉ List.map Prod.snd es ∧ vget mate v = some u

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.

theorem Cslib.Algorithms.Lean.MaxFlow.hkApplyEdges_eq_of_toFinset_eq {m r : ℕ} (mate : Vector (Option (Fin m)) r) {es₁ es₂ : List (Fin m × Fin r)} (hnd₁ : (List.map Prod.snd es₁).Nodup) (hnd₂ : (List.map Prod.snd es₂).Nodup) (heq : es₁.toFinset = es₂.toFinset) :
hkApplyEdges mate es₁ = hkApplyEdges mate es₂

Two edge lists with distinct right ends and the same edge set induce the same mate table.

theorem Cslib.Algorithms.Lean.MaxFlow.mateMatching_applyEdges {m r : ℕ} (mate : Vector (Option (Fin m)) r) (es : List (Fin m × Fin r)) (hnd : (List.map Prod.snd es).Nodup) (hcompat : ∀ (u : Fin m) (v : Fin r), (u, v) ∈ mateMatching mate → (u ∈ matchedLeft es.toFinset ↔ v ∈ matchedRight es.toFinset)) :
mateMatching (List.foldl (fun (mt : Vector (Option (Fin m)) r) (e : Fin m × Fin r) => vset mt e.2 (some e.1)) mate es) = augment (mateMatching mate) es.toFinset

Rematching a mate table along a compatible edge list realizes augment.

One phase and phase loop #

theorem Cslib.Algorithms.Lean.MaxFlow.hkPhase_spec {m r : ℕ} {G : Fin m × Fin r → Bool} {adj : Fin m → List (Fin r)} {mate : Vector (Option (Fin m)) r} {dist : Vector (Option ℕ) m} {k : ℕ} (hadj : BipAdjValid G adj) (hM : IsMatching G (mateMatching mate)) (hex : ∀ (u : Fin m), ∀ i ≤ k, vget dist u = some i ↔ IsLayer G (mateMatching mate) u i) :
∃ (N : Finset (Fin m × Fin r)), IsMaximalLayeredAugmentation G (mateMatching mate) N k ∧ mateMatching (hkPhase G adj mate dist k) = augment (mateMatching mate) N
structure Cslib.Algorithms.Lean.MaxFlow.HKLoopPost {m r : ℕ} (G : Fin m × Fin r → Bool) (mate : Vector (Option (Fin m)) r) (n : ℕ) (res : HKLoopResult m r) :

Postcondition of the phase loop started with phase counter n from mate.

theorem Cslib.Algorithms.Lean.MaxFlow.hkLoop_spec {m r : ℕ} {G : Fin m × Fin r → Bool} {adj : Fin m → List (Fin r)} (hadj : BipAdjValid G adj) (fuel n : ℕ) (mate : Vector (Option (Fin m)) r) :
IsMatching G (mateMatching mate) → m < fuel + (mateMatching mate).card → HKLoopPost G mate n (hkLoop G adj fuel n mate)

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 #

theorem Cslib.Algorithms.Lean.MaxFlow.mem_foldl_hkMatchingStep {m r : ℕ} (mate : Vector (Option (Fin m)) r) (l : List (Fin r)) (a : Finset (Fin m × Fin r)) (u : Fin m) (v : Fin r) :
(u, v) ∈ List.foldl (hkMatchingStep mate) a l ↔ (u, v) ∈ a ∨ v ∈ l ∧ vget mate v = some u

Membership in the matching extracted from a mate table.

theorem Cslib.Algorithms.Lean.MaxFlow.mem_foldl_hkCoverLeftStep {m : ℕ} (dist : Vector (Option ℕ) m) (l : List (Fin m)) (a : Finset (Fin m)) (u : Fin m) :
u ∈ List.foldl (hkCoverLeftStep dist) a l ↔ u ∈ a ∨ u ∈ l ∧ vget dist u = none
theorem Cslib.Algorithms.Lean.MaxFlow.mem_foldl_hkCoverRightStep {m r : ℕ} (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) (l : List (Fin r)) (a : Finset (Fin r)) (v : Fin r) :
v ∈ List.foldl (hkCoverRightStep mate dist) a l ↔ v ∈ a ∨ v ∈ l ∧ ∃ (w : Fin m), vget mate v = some w ∧ vget dist w ≠ none

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.

theorem Cslib.Algorithms.Lean.MaxFlow.card_hopcroftKarp_matching_eq {m r : ℕ} {G : Fin m × Fin r → Bool} {adj : Fin m → List (Fin r)} {M : Finset (Fin m × Fin r)} (hadj : BipAdjValid G adj) (hM : IsMaxMatching G M) :

Hopcroft–Karp returns a matching of the same cardinality as any maximum matching M of G.

Decide whether G : Fin n × Fin n → Bool has a perfect matching using Hopcroft–Karp.