Skip to content

2-satisfiability

Definition2-CNF formulas#✓fully proved

A CNF formula is a 2-CNF formula when each of its clauses, viewed as a list of literals, has length at most \(2\) (repeated literals are counted with multiplicity).

def IsTwoCNF (φ : CNF) : Prop := ∀ C ∈ φ, C.length ≤ 2
Uses 2 · Used by 3

AbbreviationComplement of a literal#✓fully proved

The complement of a literal \((x, b)\), with variable index \(x \in \mathbb{N}\) and polarity \(b\), is the literal \((x, \lnot b)\): same variable, opposite polarity.

abbrev neg (l : Nat × Bool) : Nat × Bool := (l.1, !l.2)
Used by 10

TheoremClauses with at most two literals#✓fully proved

In a clause with at most two literals, any two different members \(a\) and \(b\) are all of its members: every member \(c\) of the clause equals \(a\) or \(b\).

theorem eq_or_eq_of_length_le_two {C : CNFClause} (hC : C.length ≤ 2) {a b c : Nat × Bool}
    (ha : a ∈ C) (hb : b ∈ C) (hab : a ≠ b) (hc : c ∈ C) : c = a ∨ c = b
Uses 1 · Used by 1
22 fully proved

The implication graph on literals

DefinitionImplication edge#✓fully proved

For a CNF formula \(\varphi\) and literals \(l\) and \(m\), there is an implication edge from \(l\) to \(m\) when some clause of \(\varphi\) contains both \(\neg l\) and \(m\), either as two different literals or with \(m\) as its only literal (so a unit clause \(m\) gives the edge from \(\neg m\) to \(m\)).

def ImpEdge (φ : CNF) (l m : Nat × Bool) : Prop :=
  ∃ C ∈ φ, neg l ∈ C ∧ m ∈ C ∧ (neg l ≠ m ∨ ∀ c ∈ C, c = m)
Uses 3 · Used by 9

DefinitionReachability in the implication graph#✓fully proved

For a CNF formula \(\varphi\), a literal \(l\) reaches a literal \(m\) when there is a path of zero or more implication edges of \(\varphi\) from \(l\) to \(m\); that is, reachability is the reflexive-transitive closure of the implication edge relation.

def ImpReach (φ : CNF) : Nat × Bool → Nat × Bool → Prop :=
  Relation.ReflTransGen (ImpEdge φ)
Uses 2 · Used by 6

TheoremSkew symmetry of the implication graph#✓fully proved

For a CNF formula \(\varphi\) and literals \(l\) and \(m\), if the implication graph of \(\varphi\) has an edge from \(l\) to \(m\), then it also has the contrapositive edge from \(\lnot m\) to \(\lnot l\), where \(\lnot\) denotes the complementary literal.

theorem ImpEdge.skew {φ : CNF} {l m : Nat × Bool} (h : ImpEdge φ l m) :
    ImpEdge φ (neg m) (neg l)
Uses 4 · Used by 1

TheoremSatisfying assignments respect implication edges#✓fully proved

Let \(\varphi\) be a 2-CNF formula and \(\sigma\) an assignment of truth values to the variables that satisfies \(\varphi\). If the implication graph of \(\varphi\) has an edge from a literal \(l\) to a literal \(m\) and \(\sigma\) satisfies \(l\), then \(\sigma\) satisfies \(m\).

theorem satisfies_of_impEdge {φ : CNF} (h2 : IsTwoCNF φ) {σ : Nat → Bool}
    (hσ : φ.Satisfies σ) {l m : Nat × Bool} (h : ImpEdge φ l m)
    (hl : CNFLiteral.Satisfies σ l) : CNFLiteral.Satisfies σ m
Uses 9 · Used by 1

Resolution derivations from a 2-CNF formula

DefinitionResolution invariant of the implication graph#✓fully proved

A clause \(C\) satisfies the invariant with respect to a CNF formula \(\varphi\) when for every literal \(a\) of \(C\) there is a literal \(b\) of \(C\) such that the complementary literal \(\lnot a\) reaches \(b\) in the implication graph of \(\varphi\) (by a path of zero or more edges).

Every clause derivable from \(\varphi\) by resolution satisfies this invariant.

def Inv (φ : CNF) (C : CNFClause) : Prop := ∀ a ∈ C, ∃ b ∈ C, ImpReach φ (neg a) b
Uses 4 · Used by 1

TheoremResolution refutations give contradictory cycles#✓fully proved

If a CNF formula \(\varphi\) does not contain the empty clause and the empty clause is derivable from \(\varphi\) by resolution, then there is a variable \(x\) such that the literals \(x\) and \(\neg x\) reach each other in the implication graph of \(\varphi\).

theorem exists_contradictory_of_resolutionDerivable_nil {φ : CNF} (hnil : [] ∉ φ)
    (h : φ.ResolutionDerivable []) :
    ∃ x, ImpReach φ (x, true) (x, false) ∧ ImpReach φ (x, false) (x, true)
Uses 9 · Used by 1

Theorem2-SAT characterization#✓fully proved

2-SAT characterization (Aspvall, Plass, Tarjan): a 2-CNF formula is satisfiable iff it has no empty clause and no variable x such that x and ¬x reach each other in the implication graph.

theorem cnfSat_iff_of_isTwoCNF {φ : CNF} (h2 : IsTwoCNF φ) :
    CNFSat φ ↔ [] ∉ φ ∧
      ∀ x, ¬ (ImpReach φ (x, true) (x, false) ∧ ImpReach φ (x, false) (x, true))
Uses 13 · Used by 1

The implication graph as a finite directed graph, and the SCC decision procedure

AbbreviationLiterals over the variables of a formula#✓fully proved

For a CNF formula \(\varphi\), a literal over \(\varphi\) is a pair \((x, b)\) of a variable index \(x \in \mathbb{N}\) and a polarity \(b \in \{\mathrm{true}, \mathrm{false}\}\) such that the variable \(x\) occurs in \(\varphi\). These literals are the vertices of the implication graph of \(\varphi\).

abbrev Lit (φ : CNF) := {l : Nat × Bool // l.1 ∈ φ.vars}
Uses 2 · Used by 8

DefinitionComplement of a literal over a formula#✓fully proved

For a CNF formula \(\varphi\), the complement of a literal \((x, b)\) over the variables of \(\varphi\) is the literal \((x, \lnot b)\), which is again a literal over the variables of \(\varphi\).

def Lit.neg {φ : CNF} (l : Lit φ) : Lit φ := ⟨TwoSAT.neg l.val, l.prop⟩
Uses 4 · Used by 2

DefinitionDuplicate-free list of the variables of a CNF formula#✓fully proved

For a CNF formula \(\varphi\), this is the list of the variables occurring in the literals of \(\varphi\), each exactly once, in the order of their last occurrences when the clauses of \(\varphi\) and their literals are read from first to last.

def varList (φ : CNF) : List Nat := (φ.flatMap fun C ↦ C.map Prod.fst).dedup
Uses 2 · Used by 3

DefinitionList of the literals of a CNF formula#✓fully proved

For a CNF formula \(\varphi\), this is the list of all literals over the variables of \(\varphi\), each appearing exactly once, arranged variable by variable: for each variable \(x\) of \(\varphi\), the positive literal \((x, \mathrm{true})\) is immediately followed by the negative literal \((x, \mathrm{false})\).

def litList (φ : CNF) : List (Lit φ) :=
  (varList φ).attach.flatMap fun x ↦
    [⟨(x.1, true), mem_varList.mp x.2⟩, ⟨(x.1, false), mem_varList.mp x.2⟩]
Uses 6 · Used by 4

DefinitionSuccessors in the implication graph#✓fully proved

Let \(\varphi\) be a CNF formula and \(l\) a literal over the variables of \(\varphi\), and write \(\neg l\) for the complementary literal. The successors of \(l\) are the literals \(m\) over the variables of \(\varphi\) such that the implication graph of \(\varphi\) has an edge from \(l\) to \(m\): some clause of \(\varphi\) contains \(\neg l\) and \(m\), either as two different literals or as its only literal. They are listed without repetition, in the order of the list of all literals over the variables of \(\varphi\).

def successors (φ : CNF) (l : Lit φ) : List (Lit φ) :=
  (litList φ).filter fun m ↦ decide (ImpEdge φ l.val m.val)
Uses 7 · Used by 2

DefinitionAdjacency table of the implication graph#✓fully proved

For a CNF formula \(\varphi\), the hash map that sends each literal over the variables of \(\varphi\) to the list of its implication-graph successors, that is, of the literals \(m\) over the variables of \(\varphi\) with an implication edge from it to \(m\).

The table is computed once so that the SCC traversal looks successor lists up in expected constant time.

def adjacencyTable (φ : CNF) : Std.HashMap (Lit φ) (List (Lit φ)) :=
  Std.HashMap.ofList ((litList φ).map fun l ↦ (l, successors φ l))
Uses 5 · Used by 2

DefinitionImplication graph as a finite directed graph#✓fully proved

For a CNF formula \(\varphi\), the finite directed graph whose vertices are the literals over the variables of \(\varphi\), listed without repetition, and whose out-neighbours of a literal \(l\) are the literals \(m\) over the variables of \(\varphi\) with an implication edge from \(l\) to \(m\), read from the adjacency table of \(\varphi\).

def implicationGraph (φ : CNF) : DirectedAdjacency (Lit φ) :=
  let table := adjacencyTable φ
  { vertices := litList φ
    vertices_nodup := nodup_litList φ
    vertices_complete := mem_litList φ
    adjacency := fun l ↦ table.getD l [] }
Uses 9 · Used by 3

TheoremImplication edges end over variables of the formula#✓fully proved

For a CNF formula \(\varphi\) and literals \(l\) and \(m\), if the implication graph of \(\varphi\) has an edge from \(l\) to \(m\), then the variable of \(m\) occurs in \(\varphi\).

theorem mem_vars_of_impEdge {φ : CNF} {l m : Nat × Bool} (h : ImpEdge φ l m) :
    m.1 ∈ φ.vars
Uses 6 · Used by 1

TheoremReachability in the finite implication graph#✓fully proved

For a CNF formula \(\varphi\) and literals \(l\) and \(m\) over the variables of \(\varphi\), \(m\) is reachable from \(l\) in the implication graph of \(\varphi\), viewed as a finite directed graph on these literals, if and only if \(l\) reaches \(m\) by implication edges of \(\varphi\).

theorem implicationGraph_reachable_iff {φ : CNF} {l m : Lit φ} :
    (implicationGraph φ).Reachable l m ↔ ImpReach φ l.val m.val
Uses 17 · Used by 1

DefinitionSCC-based 2-SAT decision procedure#✓fully proved

For a CNF formula \(\varphi\), the procedure returns false if \(\varphi\) contains the empty clause; otherwise it returns true if and only if no block of the strongly connected components computed for the implication graph of \(\varphi\) contains a literal together with its complementary literal.

def decide2SAT (φ : CNF) : Bool :=
  !φ.contains [] &&
    (components (implicationGraph φ)).all fun block ↦ block.all fun l ↦ !block.contains l.neg
Uses 7 · Used by 1

TheoremA positive literal reaching its complement lies over a variable of the formula#✓fully proved

For a CNF formula \(\varphi\) and a variable \(x\), if the positive literal \((x, \mathrm{true})\) reaches the negative literal \((x, \mathrm{false})\) in the implication graph of \(\varphi\), then \(x\) occurs in \(\varphi\).

theorem mem_vars_of_impReach_true_false {φ : CNF} {x : Nat}
    (h : ImpReach φ (x, true) (x, false)) : x ∈ φ.vars
Uses 7 · Used by 1

TheoremCorrectness of the SCC-based 2-SAT procedure#✓fully proved

For every CNF formula \(\varphi\) in which every clause has at most two literals, the SCC-based 2-SAT decision procedure returns true on \(\varphi\) if and only if \(\varphi\) is satisfiable.

theorem decide2SAT_eq_true_iff {φ : CNF} (h2 : IsTwoCNF φ) :
    decide2SAT φ = true ↔ CNFSat φ
Uses 17

The dependency explorer needs JavaScript and its graph data.

Download the complete graph data.