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:
checkClauses φchecks that every clause has length1or2;implicationAdj φbuilds the adjacency lists ofφ's implication graph (a ∨ bcontributes¬a → band¬b → a; a unit clauseacontributes¬a → a), with at most2 * |φ|arcs;DFS.sccsandDFS.indexComponentscompute the topological component indexcompIdxof every literal;extractAssignmentrejects 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 #
Cslib.Algorithms.Lean.TwoSAT.implicationAdj: implication-graph adjacency lists of a width-at-most-2 formula.Cslib.Algorithms.Lean.TwoSAT.solve: 2-SAT solver over a finite variable listvs.Cslib.Algorithms.Lean.TwoSAT.solveFin: specialization toCNF (Fin n).Cslib.Algorithms.Lean.TwoSAT.solveNat: solver onCNF ℕconnected toCslib.Complexity.TwoSAT.
Main results #
Cslib.Algorithms.Lean.TwoSAT.solve_eq_some,Cslib.Algorithms.Lean.TwoSAT.isSome_solve_iff: soundness and completeness ofsolve.Cslib.Algorithms.Lean.TwoSAT.solveFin_eq_some,Cslib.Algorithms.Lean.TwoSAT.isSome_solveFin_iff: soundness and completeness onCNF (Fin n).Cslib.Algorithms.Lean.TwoSAT.isSome_solveNat_iff,Cslib.Algorithms.Lean.TwoSAT.solveNat_eq_some: exact equivalence withφ ∈ Cslib.Complexity.TwoSATand witness validity.
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.
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 φ.
When φ has no empty clause and all clauses have width at most 2, implicationAdj φ
realizes φ.Implies.
Extracting the truth assignment from SCC topological indices #
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 #
Aspvall–Plass–Tarjan 2-SAT solver: returns some σ satisfying φ when every clause of φ
has width at most 2 and φ is satisfiable, and none otherwise.
Soundness of solve: whenever solve vs φ = some σ (with vs covering V), every clause
of φ has width at most 2 and σ satisfies φ.
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).
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.
Exact equivalence with Cslib.Complexity.TwoSAT.