Documentation

CslibFrontier.Algorithms.Lean.TreeDecomposition.CSP

Dynamic programming for CSPs on bounded treewidth #

For a constraint satisfaction problem F : CSP Rel V over a finite inhabited domain Fin q (0 < q), and a valid nice tree decomposition covering every constraint scope of F (from F.primalGraph or F.activePrimalGraph), this module implements executable pure dynamic programming procedures for both exact model counting (cspCountNice, cspCountRooted) and satisfiability (cspDecideNice, cspDecideRooted).

Using SimpleGraph.RootedTreeDecomposition.toNiceTreePruned, any supplied rooted tree decomposition R of width w is converted to a nice tree decomposition of width at most w and size at most (3 * w + 5) * (card V + 1). Machine-level running-time bounds over pre-serialized nice tree decompositions and pre-materialized constraint truth tables (logarithmic-word decision and single-word wide-word exact counting) live in CslibFrontier.Algorithms.WordRAM.TreeDecomposition.CSP.

Main definitions #

Main results #

Canonical variable listing of each bag in a NiceTree.

Shared DP engine and correctness against specDPCount and specDPDecide #

Semiring-like operations used by dpTree for both counting and decision.

  • zero : β

    Additive identity (0 for counting, false for decision).

  • one : β

    Multiplicative identity (1 for counting, true for decision).

  • add : β → β → β

    Aggregation across values of a forgotten variable.

  • mul : β → β → β

    Combination of two join-branch table entries.

def Cslib.Algorithms.Lean.TreeDecomposition.dpAlgFold {β : Type} (z : β) (add : β → β → β) :
ℕ → (ℕ → β) → β

Fold g 0, …, g (k - 1) from the left using add and initial value z.

theorem Cslib.Algorithms.Lean.TreeDecomposition.dpAlgFold_congr {β : Type} {z : β} {add : β → β → β} {k : ℕ} {g₁ g₂ : ℕ → β} (h : ∀ d < k, g₁ d = g₂ d) :
dpAlgFold z add k g₁ = dpAlgFold z add k g₂
theorem Cslib.Algorithms.Lean.TreeDecomposition.dpAlgFold_nat (k : ℕ) (g : ℕ → ℕ) :
dpAlgFold 0 (fun (x1 x2 : ℕ) => x1 + x2) k g = ∑ d ∈ Finset.range k, g d
theorem Cslib.Algorithms.Lean.TreeDecomposition.dpAlgFold_bool (k : ℕ) (g : ℕ → Bool) :
dpAlgFold false (fun (x1 x2 : Bool) => x1 || x2) k g = decide (∃ d < k, g d = true)
def Cslib.Algorithms.Lean.TreeDecomposition.dpTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {β : Type} (A : DPAlg β) (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (vars : List V) :

Generic bottom-up DP engine over a NiceTree, parameterized by DPAlg.

theorem Cslib.Algorithms.Lean.TreeDecomposition.size_dpTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {β : Type} (A : DPAlg β) (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (vars : List V) (T : SimpleGraph.NiceTree V) :
(dpTree A q hq sem F vars T).size = q ^ (bagList T).length

DPAlg instance over ℕ for exact model counting.

DPAlg instance over Bool for satisfiability decision.

def Cslib.Algorithms.Lean.TreeDecomposition.cspCountTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (vars : List V) (T : SimpleGraph.NiceTree V) :

Compute the bottom-up exact-count DP table on a nice tree decomposition over Fin q.

def Cslib.Algorithms.Lean.TreeDecomposition.cspDecideTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (vars : List V) (T : SimpleGraph.NiceTree V) :

Compute the bottom-up satisfiability DP table on a nice tree decomposition over Fin q.

theorem Cslib.Algorithms.Lean.TreeDecomposition.getD_cspCountTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) {T : SimpleGraph.NiceTree V} (hval : T.LocalValid) {m : ℕ} (hm : m < q ^ (bagList T).length) :
(cspCountTree q hq sem F F.vars T).getD m 0 = Logic.PL.CSP.specDPCount sem F F.vars.toFinset T (ofRadixFin q hq (bagList T) m)
theorem Cslib.Algorithms.Lean.TreeDecomposition.getD_cspDecideTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) {T : SimpleGraph.NiceTree V} (hval : T.LocalValid) {m : ℕ} (hm : m < q ^ (bagList T).length) :

Top-level CSP solvers on nice and rooted tree decompositions #

def Cslib.Algorithms.Lean.TreeDecomposition.cspCountNiceTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (T : SimpleGraph.NiceTree V) :

Exact model count computed from a supplied nice tree T.

def Cslib.Algorithms.Lean.TreeDecomposition.cspDecideNiceTree {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (T : SimpleGraph.NiceTree V) :

Satisfiability decision computed from a supplied nice tree T.

theorem Cslib.Algorithms.Lean.TreeDecomposition.cspCountNiceTree_eq_modelCount {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) {F : Logic.PL.CSP Rel V} {T : SimpleGraph.NiceTree V} (hval : T.LocalValid) (hbag : T.bag = ∅) (hsub : F.vars.toFinset ⊆ T.subtreeVerts) (hcov : ∀ c ∈ F, SimpleGraph.NiceTree.CoversFinset c.scope.toFinset T) :
theorem Cslib.Algorithms.Lean.TreeDecomposition.cspDecideNiceTree_eq_true_iff {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) {F : Logic.PL.CSP Rel V} {T : SimpleGraph.NiceTree V} (hval : T.LocalValid) (hbag : T.bag = ∅) (hsub : F.vars.toFinset ⊆ T.subtreeVerts) (hcov : ∀ c ∈ F, SimpleGraph.NiceTree.CoversFinset c.scope.toFinset T) :
def Cslib.Algorithms.Lean.TreeDecomposition.cspCountNice {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (N : F.primalGraph.NiceTreeDecomposition) :

Exact model count on a nice tree decomposition of F.primalGraph.

def Cslib.Algorithms.Lean.TreeDecomposition.cspDecideNice {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (N : F.primalGraph.NiceTreeDecomposition) :

Satisfiability decision on a nice tree decomposition of F.primalGraph.

theorem Cslib.Algorithms.Lean.TreeDecomposition.cspCountNice_eq_modelCount {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) {F : Logic.PL.CSP Rel V} (N : F.primalGraph.NiceTreeDecomposition) :
theorem Cslib.Algorithms.Lean.TreeDecomposition.cspDecideNice_eq_true_iff {Rel : Type u_1} {V : Type u_2} [DecidableEq V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) {F : Logic.PL.CSP Rel V} (N : F.primalGraph.NiceTreeDecomposition) :
def Cslib.Algorithms.Lean.TreeDecomposition.cspCountActiveNice {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (N : F.activePrimalGraph.NiceTreeDecomposition) :

Exact model count on a nice tree decomposition of F.activePrimalGraph.

def Cslib.Algorithms.Lean.TreeDecomposition.cspDecideActiveNice {Rel : Type u_1} {V : Type u_2} [DecidableEq V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) (N : F.activePrimalGraph.NiceTreeDecomposition) :

Satisfiability decision on a nice tree decomposition of F.activePrimalGraph.

def Cslib.Algorithms.Lean.TreeDecomposition.cspCountRooted {Rel : Type u_1} {V : Type u_2} [DecidableEq V] [LinearOrder V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) {n : ℕ} (R : F.primalGraph.RootedTreeDecomposition n) :

Exact model count on a rooted tree decomposition of F.primalGraph via toNiceTreePruned.

def Cslib.Algorithms.Lean.TreeDecomposition.cspDecideRooted {Rel : Type u_1} {V : Type u_2} [DecidableEq V] [LinearOrder V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) {n : ℕ} (R : F.primalGraph.RootedTreeDecomposition n) :

Satisfiability decision on a rooted tree decomposition of F.primalGraph via toNiceTreePruned.

theorem Cslib.Algorithms.Lean.TreeDecomposition.cspCountRooted_eq_modelCount {Rel : Type u_1} {V : Type u_2} [DecidableEq V] [LinearOrder V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) {F : Logic.PL.CSP Rel V} {n : ℕ} (R : F.primalGraph.RootedTreeDecomposition n) :
theorem Cslib.Algorithms.Lean.TreeDecomposition.cspDecideRooted_eq_true_iff {Rel : Type u_1} {V : Type u_2} [DecidableEq V] [LinearOrder V] {q : ℕ} (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) {F : Logic.PL.CSP Rel V} {n : ℕ} (R : F.primalGraph.RootedTreeDecomposition n) :
def Cslib.Algorithms.Lean.TreeDecomposition.cspCountActiveRooted {Rel : Type u_1} {V : Type u_2} [DecidableEq V] [LinearOrder V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) {n : ℕ} (R : F.activePrimalGraph.RootedTreeDecomposition n) :

Exact model count on a rooted tree decomposition of F.activePrimalGraph via toNiceTreePruned.

def Cslib.Algorithms.Lean.TreeDecomposition.cspDecideActiveRooted {Rel : Type u_1} {V : Type u_2} [DecidableEq V] [LinearOrder V] (q : ℕ) (hq : 0 < q) (sem : Rel → List (Fin q) → Bool) (F : Logic.PL.CSP Rel V) {n : ℕ} (R : F.activePrimalGraph.RootedTreeDecomposition n) :

Satisfiability decision on a rooted tree decomposition of F.activePrimalGraph via toNiceTreePruned.

Finite inhabited domain equivalence bridge #

def Cslib.Algorithms.Lean.TreeDecomposition.semFin {Rel : Type u_1} {D : Type u_3} {q : ℕ} (sem : Rel → List D → Bool) (e : D ≃ Fin q) :
Rel → List (Fin q) → Bool

Pull back constraint semantics across e : D ≃ Fin q.

theorem Cslib.Algorithms.Lean.TreeDecomposition.eval_semFin {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {q : ℕ} (sem : Rel → List D → Bool) (e : D ≃ Fin q) (σ : V → Fin q) (F : Logic.PL.CSP Rel V) :
Logic.PL.CSP.eval (semFin sem e) σ F = Logic.PL.CSP.eval sem (fun (v : V) => e.symm (σ v)) F
theorem Cslib.Algorithms.Lean.TreeDecomposition.satisfiable_semFin_iff {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {q : ℕ} (sem : Rel → List D → Bool) (e : D ≃ Fin q) (F : Logic.PL.CSP Rel V) :
theorem Cslib.Algorithms.Lean.TreeDecomposition.modelCount_semFin {Rel : Type u_1} {V : Type u_2} {D : Type u_3} [DecidableEq V] [Fintype D] [Inhabited D] {q : ℕ} (hq : 0 < q) (sem : Rel → List D → Bool) (e : D ≃ Fin q) (F : Logic.PL.CSP Rel V) :
theorem Cslib.Algorithms.Lean.TreeDecomposition.cspCountNice_equiv {Rel : Type u_1} {V : Type u_2} {D : Type u_3} [DecidableEq V] [Fintype D] [Inhabited D] (sem : Rel → List D → Bool) {F : Logic.PL.CSP Rel V} (N : F.primalGraph.NiceTreeDecomposition) (e : D ≃ Fin (Fintype.card D)) :