Lecture 5

Boolean satisfiability

Johannes Korbmacher Lecture slides

Logical methods for AI

Boolean satisfiability

Lecture 5

What you’ll be able to do

Read chapter 5 first; exercise sheet 5 comes after the lecture.

Automating the hard work

In chapter 4 we did it all by hand: list the models, calculate the values, hunt for a countermodel.

  • Let the machine do it.
  • SAT asks one question: can one valuation make all of these true?
  • Two reasoning problems reduce to it.

Reduce the problem to SAT, then run an algorithm on it.

sat_cpu_bugCPU2 + 2 = 5

Satisfiability

SUN ∧ ¬RAIN, with v(SUN) = 1, v(RAIN) = 0:

1 AND (NOT 0) = 1

Satisfiable: one valuation settles it.

SUN ∧ ¬SUN:

1 AND (NOT 1) = 0 0 AND (NOT 0) = 0

Unsatisfiable: every valuation must fail.

The SAT problem

  • SUN ∨ RAIN, ¬SUN: satisfiable, by v(SUN) = 0, v(RAIN) = 1.
  • Add ¬RAIN: unsatisfiable — though each can be satisfied on its own.

Reduction 1: circuit verification

The interactive example is loading.

What the circuit does:

C = ¬(INPUT₁ ∧ INPUT₂)

What it should do, excluding the row with output 0:

S = ¬INPUT₁ ∨ ¬INPUT₂

Where do they disagree? SAT? ¬(S ↔ C)

  • Satisfiable: that valuation is an input where the circuit is wrong.
  • Unsatisfiable: the circuit implements the specification.

Reduction 2: deductive validity

(SUN ∨ RAIN) ∧ ¬SUN ∧ ¬RAIN: no countermodel, so valid.

(SUN ∨ RAIN) ∧ SUN ∧ ¬¬RAIN: v(SUN) = v(RAIN) = 1 is a countermodel, so invalid.

Truth-tables: brute force

  • n variables: 2ⁿ assignments.
  • Count from 0 in binary to list them all.
  • Evaluate the formula in each row.
  • No preparation: the formula goes in as it stands.
sat_truth-table-3varSUNRAIN00011WIND001101110011000110101234567

Evaluating a row

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def evaluate(tree, v):
    if is_variable(tree):
        return v(variable(tree))

    if operator(tree) == "¬":
        return NOT(evaluate(child(tree), v))

    left_value = evaluate(left_child(tree), v)
    right_value = evaluate(right_child(tree), v)
    if operator(tree) == "∧":
        return AND(left_value, right_value)
    if operator(tree) == "∨":
        return OR(left_value, right_value)
    return XNOR(left_value, right_value)

Every call gets a smaller tree, so evaluation terminates.

Searching the table

Ex.

The interactive example is loading.

A decision procedure

  • Finitely many rows, so the search finishes.
  • A returned valuation makes the formula true.
  • “Unsatisfiable” means every valuation was checked.

So SAT is decidable. The finished table also shows a tautology (glossary, opens in a new tab) (all 1s) or a contradiction (glossary, opens in a new tab) (all 0s).

The cost of brute force

For n variables and m operators, worst-case time complexity (glossary, opens in a new tab) :

O(2ⁿ(m + 1))
VariablesRows
38
101 024
201 048 576
40over a trillion
sat_exponential123455101520xy#variables#valuations

Combinatorial explosion (glossary, opens in a new tab) : one more variable doubles the work.

Normal forms

A literal (glossary, opens in a new tab) is a propositional variable or the negation of a propositional variable.

FormulaDNF?CNF?
SUN ∨ ¬RAINyesyes
SUN ∧ ¬RAINyesyes
SUN ∨ (RAIN ∧ WIND)yesno
(SUN ∨ RAIN) ∧ WINDnoyes
¬(SUN ∧ RAIN)nono

A DNF lists alternatives; a CNF lists requirements. Resolution needs CNF.

Rewriting into normal form

Each rule replaces a subformula by an equivalent one. First remove every ↔:

(A ↔ B) ⟹ ((¬A ∨ B) ∧ (¬B ∨ A))

Push negations inward:

r₁: ¬¬A ⟹ A r₂: ¬(A ∧ B) ⟹ ¬A ∨ ¬B r₃: ¬(A ∨ B) ⟹ ¬A ∧ ¬B

The result is in negation normal form (glossary, opens in a new tab) .

For CNF, distribute ∨ over ∧:

r₄: A ∨ (B ∧ C) ⟹ (A ∨ B) ∧ (A ∨ C) r₅: (A ∧ B) ∨ C ⟹ (A ∨ C) ∧ (B ∨ C)

For DNF, distribute ∧ over ∨:

r₆: A ∧ (B ∨ C) ⟹ (A ∧ B) ∨ (A ∧ C) r₇: (A ∨ B) ∧ C ⟹ (A ∧ C) ∨ (B ∧ C)

The rewriting algorithm

Remove ↔, push negations inward, then distribute — one rule at a time.

Ex.

The interactive example is loading.

Preprocessing is the expensive part

Distribution copies subformulas: a compact input can produce exponentially many clauses.

(SUN ∧ WARM) ∨ (RAIN ∧ WIND)

becomes

(SUN ∨ RAIN) ∧ (SUN ∨ WIND) ∧ (WARM ∨ RAIN) ∧ (WARM ∨ WIND)

Each further pair doubles the clauses: n pairs give 2ⁿ.

sat_naive_approach

Truth-tables pay at search time; resolution pays before the search starts.

The resolution rule

Remove only one complementary pair at a time.

The empty clause (glossary, opens in a new tab) ⊥ is false under every valuation.

Ex.

The interactive example is loading.

Tseytin: cheap preprocessing

Give each subformula a fresh name and constrain its value, instead of copying it. Name SUN ∧ RAIN by a fresh u:

(¬u ∨ SUN) ∧ (¬u ∨ RAIN) ∧ (u ∨ ¬SUN ∨ ¬RAIN)

One fresh variable and a few clauses per connective: the CNF grows linearly.

Worked-out example: the NAND circuit

Ex.

The interactive example is loading.