You are on page 1of 30

COL703: Logic for Computer Science

(Aug-Nov 2022)
Lectures 11 & 12 (Horn-SAT, 2-SAT, DPLL)

Kumar Madhukar
madhukar@cse.iitd.ac.in

September 15th and 19th, 2022

1 / 30
Horn formulas

• a literal is a boolean variable or its negation

• for a variable x, we have a positive literal (x) and a negative literal (¬x)

• a horn clause is a finite disjunction of literals with at most one positive literal

• a horn formula is a finite conjunction of horn clauses

• example
(x ∨¬y ∨¬z ∨¬w )∧(¬x ∨¬y ∨¬w )∧(¬x ∨¬z ∨w )∧(¬x ∨y )∧(x)∧(¬z)∧(¬x ∨¬y ∨w )

2 / 30
Horn-SAT

• if the formula contains a unit clause, say (l )


• all clauses containing (l ) is removed
• from all clauses containing (¬l ) have (¬l ) removed

• this may generate new unit clauses, which are propagated similarly

• if there are no unit clauses left, the formula can be satisfied by setting every remaining
variable to false

• formula is unsat if propagation generates an empty clause

3 / 30
2-SAT

• given a 2-CNF formula, is it satisfiable or not

• every clause has 2 literals

• example
(¬x ∨ y ) ∧ (¬y ∨ z) ∧ (x ∨ ¬z) ∧ (z ∨ y )

4 / 30
2-SAT satisfiability check

• create a graph with 2n vertices (for a formula with n variables)

• corresponding to positive and negative literals for every variable

• for every clause (a ∨ b), create directed edges ¬a → b and ¬b → a

5 / 30
2-SAT satisfiability check

• create a graph with 2n vertices (for a formula with n variables)

• corresponding to positive and negative literals for every variable

• for every clause (a ∨ b), create directed edges ¬a → b and ¬b → a

• claim: if the graph contains a path from α to β, then it also contains a path from ¬β to
¬α

6 / 30
2-SAT satisfiability check

• create a graph with 2n vertices (for a formula with n variables)

• corresponding to positive and negative literals for every variable

• for every clause (a ∨ b), create directed edges ¬a → b and ¬b → a

• claim: if the graph contains a path from α to β, then it also contains a path from ¬β to
¬α
• claim: a 2-CNF formula is unsat iff there exists a variable x such that:
• there is a path from x to ¬x
• there is a path from ¬x to x

7 / 30
2-SAT satisfying assignment

• pick an unassigned literal l , with no path from l to ¬l

• assign true to l and all vertices reachable from l (and assign false to their negations)

• repeat until all vertices are assigned

8 / 30
Example
(¬x ∨ y ) ∧ (¬y ∨ z) ∧ (x ∨ ¬z) ∧ (z ∨ y )

image source: https://www.iitg.ac.in/deepkesh/CS301/assignment-2/2sat.pdf

9 / 30
Another example

(x1 ∨ ¬x2 ) ∧ (¬x1 ∨ ¬x3 ) ∧ (x1 ∨ x2 ) ∧ (x4 ∨ ¬x3 ) ∧ (x4 ∨ ¬x1 )

10 / 30
Tseitin transformation

• we know that an arbitrary boolean formula can be converted to CNF

• using De Morgan’s law and distributivity property

• but this may result in an exponential explosion of the formula

• example: (x1 ∧ y1 ) ∨ (x2 ∧ y2 ) ∨ . . . ∨ (xn ∧ yn )

• Tseitin transformation is guaranteed to only linearly increase the size of the formula

11 / 30
Tseitin transformation: Example (source: Wikipedia)

12 / 30
1-SAT to 3-SAT

c = (l )

c 0 = (l ∨ u ∨ v ) ∧ (l ∨ ¬u ∨ v ) ∧ (l ∨ u ∨ ¬v ) ∧ (l ∨ ¬u ∨ ¬v )

c 0 is satisfiable iff c is satisfiable.

13 / 30
2-SAT to 3-SAT

c = (l1 ∨ l2 )

c 0 = (l1 ∨ l2 ∨ u ) ∧ (l1 ∨ l2 ∨ ¬u )

c 0 is satisfiable iff c is satisfiable.

14 / 30
k(>3)-SAT to (k-1)-SAT

c = (l1 ∨ l2 ∨ . . . ∨ lk )

c 0 = (l1 ∨ l2 ∨ . . . lk−2 ∨ u ) ∧ (lk−1 ∨ lk ∨ ¬u )

c 0 is satisfiable iff c is satisfiable.

15 / 30
breaking 3SAT similarly to get 2SAT fails!

c = (l1 ∨ l2 ∨ l3 )

c 0 = (l1 ∨ l2 ∨ u ) ∧ (l3 ∨ ¬u ) (still 3!)

c 0 = (l1 ∨ u ) ∧ (l2 ∨ l3 ∨ ¬u ) (still 3!)

16 / 30
Davis-Putnam Algorithm

• unit-clause – if there is a unit clause l , delete all clauses containing l , and delete all
occurrences of ¬l from other clauses

• pure-literal – if there is a pure literal l , delete all clauses containing l

• eliminate a variable by resolution – choose an atom p and perform all possible resolutions
on clauses that clash on p and ¬p. Add these resolvents to the set of clauses and then
delete all the clauses containing p or ¬p.

• use these repeatedly; but use resolution only if the the first two rules do not apply

17 / 30
Davis-Putnam Algorithm

• if empty clause is produced, the formula is unsat

• if no more rules are applicable, report sat

• why does this terminate?

• why is this correct?

• example: (p) ∧ (¬p ∨ q) ∧ (¬q ∨ r ) ∧ (¬r ∨ s ∨ t)

18 / 30
DPLL Algorithm

• creating all possible resolvents is very inefficient

• DPLL improves on the DP algorithm by replacing the variable elimination with a search
for a model of the formula

19 / 30
DPLL

• Davis-Putnam-Logemann-Loveland algorithm (about 60 years old)

• combines search and deduction to decide satisfiability of CNF formulas

• based around backtrack search for a satisfying valuation

20 / 30
DPLL Algorithm

• the state of the algorithm is a pair (F, A)

• a state is successful if A sets some literal in each clause of F to true

• a conflict state is one where A sets every literal in some clause to false

• let F|A denote the formula that we get after simplifying F using A

• (F, A) is a conflict state if F|A contains the empty clause 

• (F, A) is a successful state if F|A is the empty set of clause

21 / 30
DPLL Algorithm

1. initialize A to be an empty assignment

2. while there are unit clauses {l }, add l 7→ 1 to A

3. if (F, A) is a successful then stop and output A

4. if (F, A) is a conflict state then apply clause learning to get a new clause C
• if C is  then stop and output unsat
• add C to F; backtrack to the highest level at which C is a unit clause; goto 2

5. add a new decision assignment pi 7→ 1 to A; goto 2

22 / 30
Example1

C1 : {¬p1 , ¬p4 , p5 }
C2 : {¬p1 , p6 , ¬p5 }
C3 : {¬p1 , ¬p6 , p7 }
C4 : {¬p1 , ¬p7 , ¬p5 }
C5 : {p1 , p4 , p6 }

A: hp1 7→ 1, p2 7→ 0, p3 7→ 0, p4 7→ 1i
C
1 C2 C 3
unit propagation generates a sequence of implied assignments: hp5 7−→ 1, p6 7−→ 1, p7 7−→ 1i
conflict: C4 becomes false!

1
https://www.cs.ox.ac.uk/people/james.worrell/lec7-2015.pdf
23 / 30
Learned clause

if clause learning gives a clause C , then we would want

• F ≡F∪C

• C should be a conflict clause

• all variables in C should be decision variables (fixed using decision assignments)

24 / 30
Correctness

• termination – a sequence of decisions leading to a conflict cannot be repeated

• correctness – if empty clause is learned, then F is unsatisfiable (because F ≡ F ∪ C )

25 / 30
Clause learning

C C C
A: hp1 7→ 1, p2 7→ 0, p3 7→ 0, p4 7→ 1, p5 7−→
1 2
1, p6 7−→ 3
1, p7 7−→ 1i

26 / 30
Clause learning

C C C
A: hp1 7→ 1, p2 7→ 0, p3 7→ 0, p4 7→ 1, p5 7−→
1 2
1, p6 7−→ 3
1, p7 7−→ 1i

what about the things that were desirable from a learned clause?

27 / 30
Syllabus for Minor exam

Everything that has been taught till (including) today!

28 / 30
Next week

• Binary Decision Diagrams or First-Order Logic

29 / 30
Thank you!

30 / 30

You might also like