Documentation

CslibFrontier.Algorithms.Lean.TwoSAT

2-SAT via strongly connected components #

The 2-SAT decision and witness-construction algorithm of Aspvall, Plass and Tarjan, built as a client of Kosaraju's strongly connected components (CslibFrontier.Algorithms.Lean.DFS.SCC) and connected to Cslib.Complexity.TwoSAT (CslibFrontier.Computability.Complexity.Problems.TwoSAT). For the linear-time O(n + |φ|) word-RAM realization on CNF (Fin n), see CslibFrontier.Algorithms.WordRAM.TwoSAT.

Given a list vs covering a finite variable type V and a formula φ : CNF V:

  1. checkClauses φ checks that every clause has length 1 or 2;
  2. implicationAdj φ builds the adjacency lists of φ's implication graph (a ∨ b contributes ¬a → b and ¬b → a; a unit clause a contributes ¬a → a), with at most 2 * |φ| arcs;
  3. DFS.sccs and DFS.indexComponents compute the topological component index compIdx of every literal;
  4. extractAssignment rejects if some variable shares a component with its negation, and otherwise sets σ v := decide (compIdx (v, false) < compIdx (v, true)).

For formulas φ : CNF ℕ with arbitrary binary variable labels, solveNat compacts occurring variables into Fin (φ.vars.length + 1), deciding φ ∈ Cslib.Complexity.TwoSAT and producing a satisfying assignment ℕ → Bool.

Main definitions #

Main results #

References #

Clause-width guard and literal enumeration #

Predicate checking whether a clause has width 1 or 2.

Check that every clause of φ has length 1 or 2.

Enumerate both polarities (v, false) and (v, true) for each v ∈ vs.

theorem Cslib.Algorithms.Lean.TwoSAT.mem_literalList {V : Type u_1} {vs : List V} (hvs : ∀ (v : V), v ∈ vs) (ℓ : Logic.PL.Literal V) :

Implication-graph adjacency lists #

Add the implication arcs of a single clause c of width 1 or 2.

Build the implication-graph adjacency lists of φ.

theorem Cslib.Algorithms.Lean.TwoSAT.edge_implicationAdj_iff {V : Type u_1} [DecidableEq V] {φ : Logic.PL.CNF V} (hw : ∀ c ∈ φ, List.length c ≤ 2) (hnil : [] ∉ φ) (ℓ ℓ' : Logic.PL.Literal V) :
DFS.Edge (implicationAdj φ) ℓ ℓ' ↔ φ.Implies ℓ ℓ'

When φ has no empty clause and all clauses have width at most 2, implicationAdj φ realizes φ.Implies.

Extracting the truth assignment from SCC topological indices #

def Cslib.Algorithms.Lean.TwoSAT.extractAssignment {V : Type u_1} (vs : List V) (compIdx : Logic.PL.Literal V → ℕ) :
Option (V → Bool)

Check that no variable in vs shares an SCC index with its negation, and return the topological truth assignment v ↦ decide (compIdx (v, false) < compIdx (v, true)).

2-SAT solver #

def Cslib.Algorithms.Lean.TwoSAT.solve {V : Type u_1} [DecidableEq V] (vs : List V) (φ : Logic.PL.CNF V) :
Option (V → Bool)

Aspvall–Plass–Tarjan 2-SAT solver: returns some σ satisfying φ when every clause of φ has width at most 2 and φ is satisfiable, and none otherwise.

theorem Cslib.Algorithms.Lean.TwoSAT.solve_eq_some {V : Type u_1} [DecidableEq V] {vs : List V} {φ : Logic.PL.CNF V} (hvs : ∀ (v : V), v ∈ vs) {σ : V → Bool} (h : solve vs φ = some σ) :
(∀ c ∈ φ, List.length c ≤ 2) ∧ Logic.PL.CNF.eval σ φ = true

Soundness of solve: whenever solve vs φ = some σ (with vs covering V), every clause of φ has width at most 2 and σ satisfies φ.

theorem Cslib.Algorithms.Lean.TwoSAT.isSome_solve_iff {V : Type u_1} [DecidableEq V] {vs : List V} {φ : Logic.PL.CNF V} (hvs : ∀ (v : V), v ∈ vs) :
(solve vs φ).isSome = true ↔ (∀ c ∈ φ, List.length c ≤ 2) ∧ φ.Satisfiable

Completeness and characterization of solve: when vs covers V, (solve vs φ).isSome holds if and only if every clause of φ has width at most 2 and φ is satisfiable.

Specialization to CNF (Fin n) and bridge to Cslib.Complexity.TwoSAT #

2-SAT solver on CNF (Fin n).

theorem Cslib.Algorithms.Lean.TwoSAT.solveFin_eq_some {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {σ : Fin n → Bool} (h : solveFin n φ = some σ) :
(∀ c ∈ φ, List.length c ≤ 2) ∧ Logic.PL.CNF.eval σ φ = true

Map a natural-number variable to its compact index in Fin (φ.vars.length + 1).

Relabel φ : CNF ℕ into CNF (Fin (φ.vars.length + 1)).

Solve a CNF ℕ instance of Cslib.Complexity.TwoSAT, returning some σ when φ ∈ TwoSAT and none otherwise.

Soundness of solveNat: any returned assignment witnesses φ ∈ Cslib.Complexity.TwoSAT.