Documentation

CslibFrontier.Algorithms.WordRAM.HopcroftKarp.DFSStep

Hopcroft–Karp iterative DFS frame and single-step transitions #

Pure accumulator/potential lemmas for hkDfs and hkDfsRoots, augmenting-path copy helper copyAugCode, stack-frame invariant DfsFrame, and single-step symbolic-execution theorems (exec_dfsStep_pop, exec_dfsStep_skip, exec_dfsStep_accept, exec_dfsStep_push) for the explicit-stack depth-first search loop dfsLoop ([HK73]; [CLRS09], Problem 26-6).

Main definitions #

Main results #

References #

Pure DFS accumulator, coverage, and potential lemmas #

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.hkDfs_acc {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) (f i : ℕ) (u : Fin m) (vis : Vector Bool r) (acc₀ : List (Fin m × Fin r)) :
Lean.MaxFlow.hkDfs G adj mate dist f i u vis acc₀ = ((Lean.MaxFlow.hkDfs G adj mate dist f i u vis []).1, Option.map (fun (x : List (Fin m × Fin r)) => x ++ acc₀) (Lean.MaxFlow.hkDfs G adj mate dist f i u vis []).2)
def Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.dfsNext {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))

Remaining-depth continuation supplied to hkDfsLevel for the recursive hkDfs call.

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.hkDfs_eq_dfsLevel {m r : ℕ} (G : Fin m × Fin r → Bool) (adj : Fin m → List (Fin r)) (mate : Vector (Option (Fin m)) r) (dist : Vector (Option ℕ) m) (f i : ℕ) (u : Fin m) (vis : Vector Bool r) (acc₀ : List (Fin m × Fin r)) :
Lean.MaxFlow.hkDfs G adj mate dist f i u vis acc₀ = Lean.MaxFlow.hkDfsLevel G mate dist (decide (f = 0)) i (dfsNext G adj mate dist f i) u (adj u) vis acc₀

ps has pairwise distinct right endpoints, each marked true in vis.

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.VisCovers.mono {m r : ℕ} {vis vis' : Vector Bool r} {ps : List (Fin m × Fin r)} (h : VisCovers vis ps) (hle : ∀ (x : Fin r), Lean.MaxFlow.vget vis x = true → Lean.MaxFlow.vget vis' x = true) :
VisCovers vis' ps
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.VisCovers.snoc {m r : ℕ} {vis : Vector Bool r} {ps : List (Fin m × Fin r)} (h : VisCovers vis ps) (u : Fin m) {v : Fin r} (hv : Lean.MaxFlow.vget vis v = false) :

Per-vertex instruction-potential weight assigned to an unvisited right vertex v.

Remaining DFS work potential summed over unvisited (false) entries of vis.

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.dfsPot_vset_true {inst : SparseInstance} {mate : Vector (Option (Fin inst.m)) inst.r} {vis : Vector Bool inst.r} {v : Fin inst.r} (hv : Lean.MaxFlow.vget vis v = false) :
dfsPot inst mate (Lean.MaxFlow.vset vis v true) + dfsVertWeight inst mate v = dfsPot inst mate vis
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.hkDfsLevel_pure_props {inst : SparseInstance} {mate : Vector (Option (Fin inst.m)) inst.r} {dist : Vector (Option ℕ) inst.m} (f d : ℕ) (hnext : ∀ (w₀ : Fin inst.m) (vis₀ : Vector Bool inst.r) (ps : List (Fin inst.m × Fin inst.r)), VisCovers vis₀ ps → (∀ (x : Fin inst.r), Lean.MaxFlow.vget vis₀ x = true → Lean.MaxFlow.vget (dfsNext inst.graph inst.adj mate dist f d w₀ vis₀ []).1 x = true) ∧ dfsPot inst mate (dfsNext inst.graph inst.adj mate dist f d w₀ vis₀ []).1 ≤ dfsPot inst mate vis₀ ∧ match (dfsNext inst.graph inst.adj mate dist f d w₀ vis₀ []).2 with | some qw => VisCovers (dfsNext inst.graph inst.adj mate dist f d w₀ vis₀ []).1 (ps ++ qw) ∧ qw.length = f | none => VisCovers (dfsNext inst.graph inst.adj mate dist f d w₀ vis₀ []).1 ps) (u : Fin inst.m) (vs : List (Fin inst.r)) (vis : Vector Bool inst.r) (ps : List (Fin inst.m × Fin inst.r)) (hcovers : VisCovers vis ps) :
(∀ (x : Fin inst.r), Lean.MaxFlow.vget vis x = true → Lean.MaxFlow.vget (Lean.MaxFlow.hkDfsLevel inst.graph mate dist (decide (f = 0)) d (dfsNext inst.graph inst.adj mate dist f d) u vs vis []).1 x = true) ∧ dfsPot inst mate (Lean.MaxFlow.hkDfsLevel inst.graph mate dist (decide (f = 0)) d (dfsNext inst.graph inst.adj mate dist f d) u vs vis []).1 ≤ dfsPot inst mate vis ∧ match (Lean.MaxFlow.hkDfsLevel inst.graph mate dist (decide (f = 0)) d (dfsNext inst.graph inst.adj mate dist f d) u vs vis []).2 with | some q => VisCovers (Lean.MaxFlow.hkDfsLevel inst.graph mate dist (decide (f = 0)) d (dfsNext inst.graph inst.adj mate dist f d) u vs vis []).1 (ps ++ q) ∧ q.length = f + 1 | none => VisCovers (Lean.MaxFlow.hkDfsLevel inst.graph mate dist (decide (f = 0)) d (dfsNext inst.graph inst.adj mate dist f d) u vs vis []).1 ps
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.hkDfs_pure_props {inst : SparseInstance} {mate : Vector (Option (Fin inst.m)) inst.r} {dist : Vector (Option ℕ) inst.m} (f d : ℕ) (u : Fin inst.m) (vis : Vector Bool inst.r) (ps : List (Fin inst.m × Fin inst.r)) :
VisCovers vis ps → (∀ (x : Fin inst.r), Lean.MaxFlow.vget vis x = true → Lean.MaxFlow.vget (Lean.MaxFlow.hkDfs inst.graph inst.adj mate dist f d u vis []).1 x = true) ∧ dfsPot inst mate (Lean.MaxFlow.hkDfs inst.graph inst.adj mate dist f d u vis []).1 ≤ dfsPot inst mate vis ∧ match (Lean.MaxFlow.hkDfs inst.graph inst.adj mate dist f d u vis []).2 with | some q => VisCovers (Lean.MaxFlow.hkDfs inst.graph inst.adj mate dist f d u vis []).1 (ps ++ q) ∧ q.length = f + 1 | none => VisCovers (Lean.MaxFlow.hkDfs inst.graph inst.adj mate dist f d u vis []).1 ps
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.hkDfsRoots_nodup_snd {inst : SparseInstance} (mate : Vector (Option (Fin inst.m)) inst.r) (dist : Vector (Option ℕ) inst.m) (k : ℕ) (us : List (Fin inst.m)) (vis : Vector Bool inst.r) (acc₀ : List (Fin inst.m × Fin inst.r)) :
VisCovers vis acc₀ → (List.map Prod.snd (Lean.MaxFlow.hkDfsRoots inst.graph inst.adj mate dist k us vis acc₀)).Nodup

DFS memory predicates and copyAugCode #

In-memory DFS visited-flag table vis[0..r) starting at bVis.

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.VisMem.of_outside {inst : SparseInstance} {bOff : ℕ} {vis : Vector Bool inst.r} {mem mem' : ℕ → ℕ} (h : VisMem inst bOff vis mem) (hf : ∀ (a : ℕ), bOff + inst.m + 1 + inst.numEdges + inst.r + 3 * inst.m ≤ a ∧ a < bOff + inst.m + 1 + inst.numEdges + 2 * inst.r + 3 * inst.m → mem' a = mem a) :
VisMem inst bOff vis mem'
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.VisMem.vset_true {inst : SparseInstance} {bOff : ℕ} {vis : Vector Bool inst.r} {mem mem' : ℕ → ℕ} (h : VisMem inst bOff vis mem) (v : Fin inst.r) (hself : mem' (bOff + inst.m + 1 + inst.numEdges + inst.r + 3 * inst.m + ↑v) = 1) (hother : ∀ (v₀ : Fin inst.r), v₀ ≠ v → mem' (bOff + inst.m + 1 + inst.numEdges + inst.r + 3 * inst.m + ↑v₀) = mem (bOff + inst.m + 1 + inst.numEdges + inst.r + 3 * inst.m + ↑v₀)) :
VisMem inst bOff (Lean.MaxFlow.vset vis v true) mem'
def Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.AugMem (inst : SparseInstance) (bOff : ℕ) (ps : List (Fin inst.m × Fin inst.r)) (mem : ℕ → ℕ) :

In-memory phase rematch pairs augU[0..ps.length) and augV[0..ps.length).

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.AugMem.of_outside {inst : SparseInstance} {bOff : ℕ} {ps : List (Fin inst.m × Fin inst.r)} {mem mem' : ℕ → ℕ} (h : AugMem inst bOff ps mem) (hlen : ps.length ≤ inst.r) (hf : ∀ (a : ℕ), bOff + inst.m + 1 + inst.numEdges + 2 * inst.r + 6 * inst.m + 6 ≤ a ∧ a < bOff + inst.m + 1 + inst.numEdges + 4 * inst.r + 6 * inst.m + 6 → mem' a = mem a) :
AugMem inst bOff ps mem'

Explicit-stack DFS step and frame invariant #

Single explicit-stack DFS loop body advancing or backtracking one stack frame.

Explicit-stack DFS iteration loop running dfsStepBody while active = 1.

structure Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.DfsFrame (inst : SparseInstance) (bOff k : ℕ) (mate : Vector (Option (Fin inst.m)) inst.r) (dist : Vector (Option ℕ) inst.m) (augPairs : List (Fin inst.m × Fin inst.r)) (d : ℕ) (path : List (Fin inst.m × Fin inst.r)) (u : Fin inst.m) (pre : List (Fin inst.r)) (vis : Vector Bool inst.r) (x : WordRAM.Data 34) :

Bundled state invariant at stack depth d during dfsLoop.

Address a lies outside vis and the active DFS stack frames above depth d.

theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.exec_dfsStep_pop {w bOff k d : ℕ} {inst : SparseInstance} {mate : Vector (Option (Fin inst.m)) inst.r} {dist : Vector (Option ℕ) inst.m} {augPairs path : List (Fin inst.m × Fin inst.r)} {u : Fin inst.m} {pre : List (Fin inst.r)} {vis : Vector Bool inst.r} {x : WordRAM.Data 34} (hd_le : d ≤ k) (hk_le : k ≤ inst.m + 1) (hsplit : inst.adj u = pre) (hfr : DfsFrame inst bOff k mate dist augPairs d path u pre vis x) (hfit : WordRAM.Fits w (bOff + 7 * inst.m + 5 * inst.r + inst.numEdges + 8)) :
∃ (y : WordRAM.Data 34), WordRAM.ExecWithin w dfsStepBody x y 12 ∧ (∀ (i : Fin 34), ↑i < 25 → y.regs i = x.regs i) ∧ y.regs augLen = augPairs.length ∧ (if d = 0 then y.regs active = 0 else y.regs active = 1 ∧ y.regs rd = d - 1) ∧ y.mem = x.mem
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.exec_dfsStep_skip {w bOff k d : ℕ} {inst : SparseInstance} {mate : Vector (Option (Fin inst.m)) inst.r} {dist : Vector (Option ℕ) inst.m} {augPairs path : List (Fin inst.m × Fin inst.r)} {u : Fin inst.m} {pre vs : List (Fin inst.r)} {v : Fin inst.r} {vis : Vector Bool inst.r} {x : WordRAM.Data 34} (hd_le : d ≤ k) (hk_le : k ≤ inst.m + 1) (hsplit : inst.adj u = pre ++ v :: vs) (hfr : DfsFrame inst bOff k mate dist augPairs d path u pre vis x) (hcond : Lean.MaxFlow.vget vis v = true ∨ Lean.MaxFlow.vget vis v = false ∧ Lean.MaxFlow.vget mate v = none ∧ d ≠ k ∨ Lean.MaxFlow.vget vis v = false ∧ ∃ (w₀ : Fin inst.m), Lean.MaxFlow.vget mate v = some w₀ ∧ ¬(d < k ∧ Lean.MaxFlow.vget dist w₀ = some (d + 1))) (hfit : WordRAM.Fits w (bOff + 7 * inst.m + 5 * inst.r + inst.numEdges + 8)) :
∃ (x₁ : WordRAM.Data 34), WordRAM.ExecWithin w dfsStepBody x x₁ 27 ∧ (∀ (i : Fin 34), ↑i < 25 → x₁.regs i = x.regs i) ∧ DfsFrame inst bOff k mate dist augPairs d path u (pre ++ [v]) vis x₁ ∧ (∀ (a : ℕ), DfsSubtreeOutside inst bOff d a → x₁.mem a = x.mem a) ∧ ∀ (a : ℕ), ¬(bOff + inst.m + 1 + inst.numEdges + inst.r + 3 * inst.m ≤ a ∧ a < bOff + inst.m + 1 + inst.numEdges + 4 * inst.r + 6 * inst.m + 6) → x₁.mem a = x.mem a
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.exec_dfsStep_accept {w bOff k : ℕ} {inst : SparseInstance} {mate : Vector (Option (Fin inst.m)) inst.r} {dist : Vector (Option ℕ) inst.m} {augPairs path : List (Fin inst.m × Fin inst.r)} {u : Fin inst.m} {pre vs : List (Fin inst.r)} {v : Fin inst.r} {vis : Vector Bool inst.r} {x : WordRAM.Data 34} (hk_le : k ≤ inst.m + 1) (hsplit : inst.adj u = pre ++ v :: vs) (hfr : DfsFrame inst bOff k mate dist augPairs k path u pre vis x) (hvv : Lean.MaxFlow.vget vis v = false) (hmv : Lean.MaxFlow.vget mate v = none) (hfit : WordRAM.Fits w (bOff + 7 * inst.m + 5 * inst.r + inst.numEdges + 8)) :
∃ (z : WordRAM.Data 34), WordRAM.ExecWithin w dfsLoop x z (12 * (k + 1) + 33) ∧ (∀ (i : Fin 34), ↑i < 25 → z.regs i = x.regs i) ∧ z.regs augLen = (augPairs ++ (path ++ [(u, v)])).length ∧ VisMem inst bOff (Lean.MaxFlow.vset vis v true) z.mem ∧ AugMem inst bOff (augPairs ++ (path ++ [(u, v)])) z.mem ∧ ∀ (a : ℕ), ¬(bOff + inst.m + 1 + inst.numEdges + inst.r + 3 * inst.m ≤ a ∧ a < bOff + inst.m + 1 + inst.numEdges + 4 * inst.r + 6 * inst.m + 6) → z.mem a = x.mem a
theorem Cslib.Algorithms.WordRAM.HopcroftKarp.Internal.exec_dfsStep_push {w bOff k d : ℕ} {inst : SparseInstance} {mate : Vector (Option (Fin inst.m)) inst.r} {dist : Vector (Option ℕ) inst.m} {augPairs path : List (Fin inst.m × Fin inst.r)} {u w₀ : Fin inst.m} {pre vs : List (Fin inst.r)} {v : Fin inst.r} {vis : Vector Bool inst.r} {x : WordRAM.Data 34} (hd_lt_k : d < k) (hk_le : k ≤ inst.m + 1) (hsplit : inst.adj u = pre ++ v :: vs) (hfr : DfsFrame inst bOff k mate dist augPairs d path u pre vis x) (hvv : Lean.MaxFlow.vget vis v = false) (hmv : Lean.MaxFlow.vget mate v = some w₀) (hdw : Lean.MaxFlow.vget dist w₀ = some (d + 1)) (hfit : WordRAM.Fits w (bOff + 7 * inst.m + 5 * inst.r + inst.numEdges + 8)) :
∃ (x_push : WordRAM.Data 34), WordRAM.ExecWithin w dfsStepBody x x_push 36 ∧ (∀ (i : Fin 34), ↑i < 25 → x_push.regs i = x.regs i) ∧ DfsFrame inst bOff k mate dist augPairs (d + 1) (path ++ [(u, v)]) w₀ [] (Lean.MaxFlow.vset vis v true) x_push ∧ x_push.mem (bOff + inst.m + 1 + inst.numEdges + 2 * inst.r + 5 * inst.m + 4 + d) = inst.offset ↑u + (pre ++ [v]).length ∧ (∀ (a : ℕ), DfsSubtreeOutside inst bOff d a → x_push.mem a = x.mem a) ∧ ∀ (a : ℕ), ¬(bOff + inst.m + 1 + inst.numEdges + inst.r + 3 * inst.m ≤ a ∧ a < bOff + inst.m + 1 + inst.numEdges + 4 * inst.r + 6 * inst.m + 6) → x_push.mem a = x.mem a