2-CNF Formulas and the Language 2-SAT
Lax117284.TwoSatCNF · concepts/Lax117284/TwoSatCNF.lean · lax-117284
No public endorsements yet.
Loading review…
Sign in with ORCIDNatural Language Statement
Definition
A CNF formula is in 2-CNF when every clause has at most two literals. The language 2-SAT consists of the binary encodings of the satisfiable 2-CNF formulas: it is the language SAT of , restricted to the encodings of formulas in 2-CNF.
Concept map
Lean source view on GitHub
| 1 | import Lax429075.Satisfiability |
| 2 | import Mathlib.Data.Finset.Card |
| 3 | |
| 4 | /-! |
| 5 | --- |
| 6 | title: 2-CNF Formulas and the Language 2-SAT |
| 7 | type: definition |
| 8 | --- |
| 9 | A CNF formula is in *2-CNF* when every clause has at most two literals. The language 2-SAT |
| 10 | consists of the binary encodings of the satisfiable 2-CNF formulas: it is the language SAT of |
| 11 | `lax-429075`, restricted to the encodings of formulas in 2-CNF. |
| 12 | |
| 13 | # Formalization Notes |
| 14 | |
| 15 | Nothing about formulas, literals, satisfiability or encodings is defined here. A formula is a |
| 16 | formula of `lax-429075` — a list of clauses, a clause a list of literals, a literal a variable |
| 17 | index with a sign — its satisfiability is that submission's `Satisfiable`, and its binary word is |
| 18 | that submission's `encodeCNF`. The only new notion is the width condition, so that 2-SAT is |
| 19 | literally a subset of SAT, with membership in the two languages witnessed by the same formula. |
| 20 | |
| 21 | A clause is allowed to have fewer than two literals. A unit clause is a clause of one literal; |
| 22 | the empty clause is a clause no assignment satisfies, so a formula containing one is not |
| 23 | satisfiable. Both are handled by the algorithm, and the classical results hold for them: the |
| 24 | restriction to clauses of exactly two literals is a convenience that this development does not |
| 25 | need. |
| 26 | |
| 27 | The variables of a formula are the indices its literals mention, however large; a formula on |
| 28 | the single variable `1000` has one variable. The count of distinct variables is what the running |
| 29 | time of the algorithm is measured by, besides the length of the word. |
| 30 | -/ |
| 31 | |
| 32 | namespace Lax117284.TwoSatCNF |
| 33 | |
| 34 | open Lax429075.CNF Lax429075.Encoding Lax429075.Satisfiability Lax434930.PolynomialTime |
| 35 | |
| 36 | /-- A CNF formula is in **2-CNF** when every clause has at most two literals. -/ |
| 37 | def IsTwoCNF (F : Formula) : Prop := ∀ C ∈ F, C.length ≤ 2 |
| 38 | |
| 39 | /-- The literals of a formula, in order of occurrence. -/ |
| 40 | def literals (F : Formula) : List Literal := F.flatMap id |
| 41 | |
| 42 | /-- The variables a formula mentions. -/ |
| 43 | def vars (F : Formula) : Finset ℕ := ((literals F).map Literal.index).toFinset |
| 44 | |
| 45 | /-- The number of distinct variables of a formula. -/ |
| 46 | def varCount (F : Formula) : ℕ := (vars F).card |
| 47 | |
| 48 | /-- **2-SAT**: the encodings of the satisfiable formulas in 2-CNF. It is `SAT` restricted to |
| 49 | the encodings of 2-CNF formulas. -/ |
| 50 | def TwoSAT : Language := {w | ∃ F : Formula, encodeCNF F = w ∧ IsTwoCNF F ∧ Satisfiable F} |
| 51 | |
| 52 | end Lax117284.TwoSatCNF |
| 53 |
Formalization Notes
Nothing about formulas, literals, satisfiability or encodings is defined here. A formula is a formula of — a list of clauses, a clause a list of literals, a literal a variable index with a sign — its satisfiability is that submission's , and its binary word is that submission's . The only new notion is the width condition, so that 2-SAT is literally a subset of SAT, with membership in the two languages witnessed by the same formula.
A clause is allowed to have fewer than two literals. A unit clause is a clause of one literal; the empty clause is a clause no assignment satisfies, so a formula containing one is not satisfiable. Both are handled by the algorithm, and the classical results hold for them: the restriction to clauses of exactly two literals is a convenience that this development does not need.
The variables of a formula are the indices its literals mention, however large; a formula on the single variable has one variable. The count of distinct variables is what the running time of the algorithm is measured by, besides the length of the word.
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above.
0 comments