Documentation

CslibFrontier.Algorithms.Lean.Arborescence.Table

Materialized-table Chu–Liu/Edmonds #

Whereas Cslib.Algorithms.Lean.Arborescence.edmonds passes contracted adjacency predicates and cost functions across recursive calls as unmemoised closures, this module materializes per-level n × n Vector tables (AdjTable, CostTable) indexed through FinEnum.equiv and runs the contraction algorithm over flat Fin n-indexed Vector arrays (edmondsTable, edmondsOutTable).

Each contraction level stores its contracted graph, costs, cycle membership mask, other-cycle-cost sum, and best-entry / best-exit tables in flat Fin n-indexed Vector arrays before recursing, so no level re-evaluates earlier levels. Transfer lemmas identify the returned parent map with ChuLiuEdmonds.edmonds (edmondsTable_eq_edmonds, edmondsOutTable_eq_edmondsOut), transferring all correctness and optimality theorems (edmondsTable_spec, edmondsTable_optimal, edmondsOutTable_spec).

Machine-checked running-time bounds live in sibling modules: CslibFrontier.Algorithms.WordRAM.Arborescence proves O(n³) Word-RAM running time on materialized matrices, and CslibFrontier.Algorithms.Lean.Arborescence.PolyTime proves multi-tape Turing-machine polynomial time (P) on binary integer weights.

Main definitions #

Main results #

References #

Materialized tables and helpers #

@[reducible, inline]

An n × n materialized adjacency matrix over Fin n.

@[reducible, inline]

An n × n materialized arc-cost table over Fin n.

@[reducible, inline]

A materialized parent vector over Fin n.

Materialize the n × n Boolean adjacency matrix of a relation on Fin n.

def Cslib.Algorithms.Lean.Arborescence.initCostTable {β : Type u_1} (n : ℕ) (cost : Fin n → Fin n → β) :

Materialize the n × n cost matrix on Fin n.

@[simp]
theorem Cslib.Algorithms.Lean.Arborescence.getElem_initAdjTable (n : ℕ) (adj : Fin n → Fin n → Bool) (u w : Fin n) :
(initAdjTable n adj)[↑u][↑w] = adj u w
@[simp]
theorem Cslib.Algorithms.Lean.Arborescence.getElem_initCostTable {β : Type u_1} (n : ℕ) (cost : Fin n → Fin n → β) (u w : Fin n) :
(initCostTable n cost)[↑u][↑w] = cost u w
def Cslib.Algorithms.Lean.Arborescence.argMinFin {β : Type u_1} [LinearOrder β] (n : ℕ) (pred : Fin n → Bool) (val : Fin n → β) :

Scan List.finRange n for the first minimizer of val satisfying pred.

Per-level subroutines #

Result of selecting optimal parents.

  • feasible : Bool

    True if every non-root node has at least one incoming edge, so a valid parent was found.

  • parents : ParentTable n

    The selected parents.

Select the cheapest outgoing candidate parent for every non-root vertex (r maps to some r), check feasibility, and materialize the parent vector f.

Test whether iterating f starting from x returns to x within n steps.

theorem Cslib.Algorithms.Lean.Arborescence.hasCycleFrom_eq {n : ℕ} (x : Fin n) (f : ParentTable n) :
hasCycleFrom x f = (List.range n).any fun (k : ℕ) => (fun (i : Fin n) => f[↑i])^[k + 1] x == x

Find the first vertex x ≠ r lying on a cycle of f, if any.

Materialize the cycle indicator vector inCycle of the orbit cycleOf f x0.

theorem Cslib.Algorithms.Lean.Arborescence.getElem_cycleMask {n : ℕ} (f : ParentTable n) (x0 y : Fin n) :
(cycleMask f x0)[↑y] = true ↔ ∃ k < n, (fun (i : Fin n) => f[↑i])^[k] x0 = y
def Cslib.Algorithms.Lean.Arborescence.bestEntryTable {β : Type u_1} [LinearOrder β] {n : ℕ} (a : AdjTable n) (c : CostTable β n) (inCycle : Vector Bool n) :
Vector (Option (Fin n)) n

Materialize bestEntry for every vertex u : Fin n.

def Cslib.Algorithms.Lean.Arborescence.otherCycleCost {β : Type u_1} {n : ℕ} [AddCommMonoid β] (c : CostTable β n) (f : ParentTable n) (inCycle : Vector Bool n) :
Vector β n

Tabulate for every x : Fin n the sum ∑ y ∈ C.erase x, c y (f y) when x ∈ C, without using subtraction.

def Cslib.Algorithms.Lean.Arborescence.bestExitTable {β : Type u_1} [LinearOrder β] {n : ℕ} [AddCommMonoid β] (a : AdjTable n) (c : CostTable β n) (inCycle : Vector Bool n) (other : Vector β n) :
Vector (Option (Fin n)) n

Materialize bestExit for every vertex w : Fin n.

def Cslib.Algorithms.Lean.Arborescence.contractAdjTable {n : ℕ} (a : AdjTable n) (inCycle : Vector Bool n) (bestEntry bestExit : Vector (Option (Fin n)) n) (x0 r : Fin n) :

Materialize the contracted n × n adjacency table using O(1) table lookups per cell.

@[simp]
theorem Cslib.Algorithms.Lean.Arborescence.getElem_contractAdjTable {n : ℕ} (a : AdjTable n) (inCycle : Vector Bool n) (bestEntry bestExit : Vector (Option (Fin n)) n) (x0 r u w : Fin n) :
(contractAdjTable a inCycle bestEntry bestExit x0 r)[↑u][↑w] = if inCycle[↑u] = true then if (u == x0) = true then !inCycle[↑w] && bestExit[↑w].isSome else w == r else if inCycle[↑w] = true then w == x0 && bestEntry[↑u].isSome else a[↑u][↑w]
def Cslib.Algorithms.Lean.Arborescence.contractCostTable {β : Type u_1} {n : ℕ} [AddCommMonoid β] (c : CostTable β n) (inCycle : Vector Bool n) (other : Vector β n) (bestEntry bestExit : Vector (Option (Fin n)) n) (x0 : Fin n) :

Materialize the contracted n × n cost table using O(1) table lookups per cell.

@[simp]
theorem Cslib.Algorithms.Lean.Arborescence.getElem_contractCostTable {β : Type u_1} {n : ℕ} [AddCommMonoid β] (c : CostTable β n) (inCycle : Vector Bool n) (other : Vector β n) (bestEntry bestExit : Vector (Option (Fin n)) n) (x0 u w : Fin n) :
(contractCostTable c inCycle other bestEntry bestExit x0)[↑u][↑w] = if inCycle[↑u] = true then if (u == x0) = true then match bestExit[↑w] with | some x => c[↑x][↑w] + other[↑x] | none => c[↑u][↑w] else 0 else if (w == x0) = true then match bestEntry[↑u] with | some y => c[↑u][↑y] | none => c[↑u][↑w] else c[↑u][↑w]
def Cslib.Algorithms.Lean.Arborescence.expandParentTable {n : ℕ} (f : ParentTable n) (inCycle : Vector Bool n) (bestEntry bestExit : Vector (Option (Fin n)) n) (x0 : Fin n) (q : ParentTable n) :

Expand a contracted parent vector q using the precomputed level tables.

@[simp]
theorem Cslib.Algorithms.Lean.Arborescence.getElem_expandParentTable {n : ℕ} (f : ParentTable n) (inCycle : Vector Bool n) (bestEntry bestExit : Vector (Option (Fin n)) n) (x0 : Fin n) (q : ParentTable n) (v : Fin n) :
(expandParentTable f inCycle bestEntry bestExit x0 q)[↑v] = if inCycle[↑v] = true then match bestExit[↑q[↑x0]] with | some x => if (v == x) = true then q[↑x0] else f[↑v] | none => f[↑v] else if (q[↑v] == x0) = true then bestEntry[↑v].getD v else q[↑v]

Full contraction solver #

Materialized-table Chu–Liu/Edmonds recursion with fuel fuel on Fin n.

Equivalence with edmondsAux #

theorem Cslib.Algorithms.Lean.Arborescence.edmondsTableAux_eq_edmondsAux {β : Type u_1} [LinearOrder β] [AddCommMonoid β] {V : Type u_2} [FinEnum V] (fuel : ℕ) (a : AdjTable (FinEnum.card V)) (c : CostTable β (FinEnum.card V)) (aV : V → V → Bool) (cV : V → V → β) (rV : V) :
(∀ (u w : Fin (FinEnum.card V)), a[↑u][↑w] = aV (FinEnum.equiv.symm u) (FinEnum.equiv.symm w)) → (∀ (u w : Fin (FinEnum.card V)), c[↑u][↑w] = cV (FinEnum.equiv.symm u) (FinEnum.equiv.symm w)) → Option.map (fun (p : ParentTable (FinEnum.card V)) (v : V) => FinEnum.equiv.symm p[↑(FinEnum.equiv v)]) (edmondsTableAux (FinEnum.equiv rV) fuel a c) = edmondsAux rV fuel aV cV

Exact equivalence with ChuLiuEdmonds.edmondsAux: at every recursion depth fuel, the materialized table solver edmondsTableAux computes the exact same parent map (up to FinEnum.equiv) as ChuLiuEdmonds.edmondsAux.

Top-level materialized solvers for [FinEnum V] #

def Cslib.Algorithms.Lean.Arborescence.edmondsTable {β : Type u_1} [LinearOrder β] [AddCommMonoid β] {V : Type u_2} [FinEnum V] (G : Digraph V) [DecidableRel G.Adj] (c : V → V → β) (r : V) :
Option (V → V)

Materialized-table Chu–Liu/Edmonds algorithm for spanning in-arborescences rooted at r, transporting G and c onto flat Fin (FinEnum.card V) tables via FinEnum.equiv.

theorem Cslib.Algorithms.Lean.Arborescence.edmondsTable_eq_edmonds {β : Type u_1} [LinearOrder β] [AddCommMonoid β] {V : Type u_2} [FinEnum V] (G : Digraph V) [DecidableRel G.Adj] (c : V → V → β) (r : V) :
edmondsTable G c r = edmonds G c r

Equivalence with ChuLiuEdmonds.edmonds: edmondsTable returns the exact same parent map as edmonds.

theorem Cslib.Algorithms.Lean.Arborescence.edmondsTable_spec {β : Type u_1} [LinearOrder β] [AddCommMonoid β] [IsOrderedAddMonoid β] {V : Type u_2} [FinEnum V] {G : Digraph V} [DecidableRel G.Adj] {c : V → V → β} {r : V} :
(∀ (p : V → V), edmondsTable G c r = some p → IsInArborescenceParent G.Adj r p ∧ ∀ (t : V → V), IsInArborescenceParent G.Adj r t → parentCost c r p ≤ parentCost c r t) ∧ (edmondsTable G c r = none → ∀ (t : V → V), ¬IsInArborescenceParent G.Adj r t)

Materialized-table correctness for spanning in-arborescences, transferred from edmonds_spec.

theorem Cslib.Algorithms.Lean.Arborescence.edmondsTable_optimal {β : Type u_1} [LinearOrder β] [AddCommMonoid β] [IsOrderedAddMonoid β] {V : Type u_2} [FinEnum V] {G : Digraph V} [DecidableRel G.Adj] {c : V → V → β} {r : V} {p : V → V} (hp : edmondsTable G c r = some p) :

Materialized-table optimality over Digraph.IsInArborescence, transferred from edmonds_optimal.

def Cslib.Algorithms.Lean.Arborescence.edmondsOutTable {β : Type u_1} [LinearOrder β] [AddCommMonoid β] {V : Type u_2} [FinEnum V] (G : Digraph V) [DecidableRel G.Adj] (c : V → V → β) (r : V) :
Option (V → V)

Materialized-table Chu–Liu/Edmonds algorithm for out-arborescences (arcs directed away from r).

theorem Cslib.Algorithms.Lean.Arborescence.edmondsOutTable_eq_edmondsOut {β : Type u_1} [LinearOrder β] [AddCommMonoid β] {V : Type u_2} [FinEnum V] (G : Digraph V) [DecidableRel G.Adj] (c : V → V → β) (r : V) :

edmondsOutTable computes the exact same parent map as edmondsOut.

theorem Cslib.Algorithms.Lean.Arborescence.edmondsOutTable_spec {β : Type u_1} [LinearOrder β] [AddCommMonoid β] [IsOrderedAddMonoid β] {V : Type u_2} [FinEnum V] {G : Digraph V} [DecidableRel G.Adj] {c : V → V → β} {r : V} :
(∀ (p : V → V), edmondsOutTable G c r = some p → IsInArborescenceParent (fun (u w : V) => G.Adj w u) r p ∧ ∀ (t : V → V), IsInArborescenceParent (fun (u w : V) => G.Adj w u) r t → parentCost (fun (u w : V) => c w u) r p ≤ parentCost (fun (u w : V) => c w u) r t) ∧ (edmondsOutTable G c r = none → ∀ (t : V → V), ¬IsInArborescenceParent (fun (u w : V) => G.Adj w u) r t)

Materialized-table correctness for out-arborescences, transferred from edmondsOut_spec.