SAT Solvers in Practice
DPLL, conflict-driven clause learning, and why “NP-hard” is not “hopeless”.
SAT was the first problem proven NP-complete, and in 2016 a SAT solver settled a decades-old question in Ramsey theory with a proof of about 200 terabytes. Both facts are true at once: the worst case is (believed) exponential, yet the formulas that arise in chip verification, planning and mathematics, with millions of clauses, are routinely solved. This lesson opens the solver.
What you’ll learn
- Encode problems in CNF with the Tseitin transformation and economical cardinality constraints, and prove that parity needs clauses without auxiliary variables.
- Implement DPLL with unit propagation, and relate its refutations to tree-like resolution and Haken's pigeonhole lower bound.
- Perform conflict analysis by hand: resolve back to the first unique implication point, learn a clause and backjump; explain watched literals, VSIDS, restarts and clause deletion.
- Describe the phase transition of random 3-SAT, what is proven about it, and where the hard instances lie.
- Survey what SAT solvers are used for, from hardware verification to mathematics, and the state of worst-case algorithms.
From problems to CNF
A SAT solver takes a formula in conjunctive normal form, an AND of clauses that are each an OR of literals, usually in the DIMACS format: variable is the integer , its negation is , and a clause is a list of integers. Everything else is encoding, and encoding well matters as much as solving well.
Converting a formula into an equivalent CNF by distributing ORs over ANDs can blow up exponentially. The Tseitin transformation (1968) instead introduces a fresh variable for every gate of the formula, viewed as a circuit, and adds a few clauses stating that the variable equals the gate's output. The result is equisatisfiable rather than equivalent: it is satisfiable exactly when the original is, and every model of the original extends uniquely to a model of the new formula. Its size is linear.
| Gate | Clauses for the new variable |
|---|---|
| , , | |
| , , | |
| , , , |
from itertools import product
def xor_gate(y, a, b): # clauses for y <-> (a xor b)
return [[-y, a, b], [-y, -a, -b], [y, -a, b], [y, a, -b]]
def parity_tseitin(n): # x1 ^ ... ^ xn = 1, fresh variables n+1, n+2, ...
clauses, prev = [], 1
for i in range(2, n + 1):
y = n + i - 1 # y = prev ^ x_i
clauses += xor_gate(y, prev, i)
prev = y
return clauses + [[prev]]
def parity_direct(n): # forbid each even-parity assignment
return [[-(i + 1) if bit else i + 1 for i, bit in enumerate(bits)]
for bits in product([0, 1], repeat=n) if sum(bits) % 2 == 0]
print(len(parity_direct(20)), len(parity_tseitin(20))) # 524288 77For , brute force confirms that the Tseitin formula's 8 models project exactly onto the 8 odd-parity assignments, one model each.
Cardinality constraints are everywhere: one color per vertex, one time slot per exam. "At most one of " takes clauses pairwise, while Sinz's sequential counter (2005) needs only clauses with auxiliary variables, a large saving for big groups.
The Tseitin transformation turns a formula into a CNF with extra variables. Which statement is correct?
- is logically equivalent to
- is satisfiable exactly when is, and each model of extends to exactly one model of
- is always satisfiable, since the new variables can be chosen freely
- is exponentially larger than in the worst case
Show the answer
B. is satisfiable exactly when is, and each model of extends to exactly one model of
The new variables are forced to equal their gates' outputs, so a model of determines them uniquely, and any model of restricted to the original variables satisfies . It is not equivalent: constrains variables that does not mention. Its size is linear in the size of .
DPLL: search with propagation
Davis and Putnam (1960) eliminated variables by resolution, the rule that derives from clauses and ; it can exhaust memory. Davis, Logemann and Loveland (1962) replaced elimination by backtracking search, and their DPLL procedure is the skeleton of every complete solver since. Its key inference is unit propagation: when all literals of a clause but one are false, the last one is forced. (DLL's other rule, eliminating pure literals that occur with one sign only, is dropped by modern solvers as too costly to maintain.)
def dpll(clauses, assignment=None):
"""clauses: lists of nonzero ints (DIMACS literals). Returns a satisfying dict or None."""
assignment = dict(assignment or {})
while True: # unit propagation
open_clauses, unit = [], None
for c in clauses:
if any(assignment.get(abs(l)) == (l > 0) for l in c):
continue # clause already satisfied
rest = [l for l in c if abs(l) not in assignment]
if not rest:
return None # conflict: every literal is false
if len(rest) == 1:
unit = rest[0]
open_clauses.append(rest)
if unit is None:
break
assignment[abs(unit)] = unit > 0 # forced: set it and propagate again
if not open_clauses:
return assignment # every clause is satisfied
x = abs(open_clauses[0][0]) # branch on a variable of an open clause
for value in (True, False):
result = dpll(open_clauses, {**assignment, x: value})
if result is not None:
return result
return None
print(dpll([[1, 2], [-1, 3], [-2, 3], [-3, 4], [-3, -4, 5], [-5, -1]]))
# {1: False, 2: True, 3: True, 4: True, 5: True}Tested against brute force on 1,500 random formulas. Real solvers replace the clause scan by watched literals, as the next step explains.
On this formula DPLL branches on . With true, propagation forces , then , then , and the clause has every literal false: a conflict. With false, the first clause forces , then , and follow, and every clause is satisfied: three calls in all.
The pigeonhole formula for 11 pigeons and 10 holes uses a variable for "pigeon sits in hole ", a clause per pigeon saying it sits somewhere, and a clause for every hole and every pair of pigeons. How many clauses is that?
Show the answer
561
11 clauses for the pigeons, plus for the holes: 561 clauses over 110 variables. A tiny formula that every resolution-based solver needs exponential time to refute as grows.
CDCL: learning from conflicts
DPLL forgets why a branch failed and may fail the same way again elsewhere. Conflict-driven clause learning (GRASP: Marques-Silva & Sakallah, 1996; Chaff: Moskewicz et al., 2001) records the reason as a new clause. Every assignment is tagged with its decision level, and every propagated literal with its reason, the clause that forced it. Take the clauses , , and , inside some larger formula. The solver decides at level 1 and at level 2. Then forces , forces and forces , all at level 2, and is falsified.
Start from the falsified clause:
Resolve with the reason for , :
Two literals of the conflict level remain; resolve with the reason for , :
Now a single literal, , belongs to level 2: is the first unique implication point (1-UIP), a node on every path from the decision to the conflict. Learn the clause, and backjump to level 1, the highest level among its other literals, where it is unit: it forces , and then forces .
Why the 1-UIP clause rather than the decisions, , which is also entailed? The UIP clause blames however it gets set, so it prunes more of the future search (Zhang, Madigan, Moskewicz & Malik, 2001, found 1-UIP the best scheme in practice). Engineering did the rest:
- Two watched literals (Chaff): each clause watches two literals that are not false and is visited only when one of them becomes false, so propagation touches few clauses and backtracking needs no bookkeeping.
- VSIDS branching (Chaff): bump the activity of variables in recent conflicts and let all activities decay, so the search concentrates where conflicts happen.
- Restarts (Gomes, Selman & Kautz, 1998): restart frequently, keeping learned clauses, to escape heavy-tailed runs; phase saving reuses each variable's last value when it is decided again.
- Clause deletion by literal block distance (Audemard & Simon, 2009) keeps the learned-clause database small.
A learned clause contains literals assigned at decision levels 3, 7 and 12, where 12 is the level of the conflict. To which level does the solver backjump?
- Level 11, undoing only the last decision
- Level 7
- Level 3
- Level 0, restarting from scratch
Show the answer
B. Level 7
At level 7 every literal of the clause except the one from level 12 is still false, so the clause becomes unit and immediately forces its level-12 literal. Levels 8 to 11 had nothing to do with the conflict and are skipped: non-chronological backjumping.
Random 3-SAT and the phase transition
Where are the hard instances? Generate random 3-CNF formulas with variables and clauses, each on three distinct random variables with random signs. With few clauses, a formula is almost surely satisfiable and easy; with many, almost surely unsatisfiable, and contradictions surface after few decisions. In between, near a critical density, satisfiability flips, and that is where DPLL works hardest (Mitchell, Selman & Levesque, 1992).
import random
def random_3sat(n, m, rng):
return [[v if rng.random() < 0.5 else -v for v in rng.sample(range(1, n + 1), 3)]
for _ in range(m)]
rng = random.Random(2025)
for ratio in (3.0, 3.5, 4.0, 4.25, 4.5, 5.0, 6.0):
sat = sum(dpll(random_3sat(40, round(ratio * 40), rng)) is not None for _ in range(200))
print(ratio, sat / 200) # 1.0, 1.0, 0.86, 0.635, 0.44, 0.11, 0.0Uses the dpll function of the previous step. Counting its calls gives the last column of the table below.
| Clause density | Satisfiable, | Satisfiable, | Mean DPLL calls, |
|---|---|---|---|
| 3.0 | 100% | 100% | 21 |
| 3.5 | 98.5% | 100% | 38 |
| 4.0 | 81% | 86% | 99 |
| 4.25 | 74% | 63.5% | 128 |
| 4.5 | 60.5% | 44% | 129 |
| 5.0 | 33.5% | 11% | 115 |
| 6.0 | 4.5% | 0% | 69 |
At almost every formula is unsatisfiable, yet DPLL needs fewer calls than at . Why?
- Because DPLL gives up early on dense formulas
- Because with many clauses, unit propagation reaches a conflict after few decisions, so the search tree that proves unsatisfiability is small
- Because dense formulas have fewer variables
- Because unsatisfiable formulas never require search
Show the answer
B. Because with many clauses, unit propagation reaches a conflict after few decisions, so the search tree that proves unsatisfiability is small
Every decision makes many clauses unit at once, so contradictions appear quickly and each branch is short. Near the threshold, formulas are barely unsatisfiable (or barely satisfiable): branches run deep before failing. For any fixed the work still grows exponentially in , as resolution lower bounds for random formulas show.
SAT in the wild
SAT solvers sit inside many tools. Hardware verification: bounded model checking unrolls a circuit for steps and asks whether a bad state is reachable (Biere, Cimatti, Clarke & Zhu, 1999). Software verification and testing run on SMT solvers ("satisfiability modulo theories"), which combine a CDCL core with decision procedures for arithmetic, arrays and bit-vectors, such as Z3 (de Moura & Bjørner, 2008). Planning as satisfiability encodes a plan of length as a formula (Kautz & Selman, 1992); scheduling, configuration, package dependency resolution and cryptanalysis follow the same recipe.
For satisfiable random instances, local search often beats CDCL: WalkSAT (Selman, Kautz & Cohen, 1994) flips variables of unsatisfied clauses, mixing greedy and random moves. Regular SAT Competitions, held since 2002, keep the field honest, and solvers emit DRAT proofs of unsatisfiability that independent, even formally verified, checkers validate (Wetzler, Heule & Hunt, 2014).
Worst-case theory moves slowly. Schöning's random walk (1999) solves 3-SAT in expected time, and the PPSZ family (Paturi, Pudlák, Saks & Zane, 2005), refined by Hansen, Kaplan, Zamir and Zwick (2019) and Scheder (2021), reaches about . The Strong Exponential Time Hypothesis conjectures that the base must approach 2 as the clause width grows, and the lesson Fine-Grained Complexity shows how much of algorithm design rests on it.
Key takeaways
- Encodings matter: Tseitin's transformation is linear and equisatisfiable, parity needs clauses without auxiliary variables, and the sequential counter encodes "at most one" in clauses.
- DPLL is backtracking with unit propagation; its refutations are tree-like resolution proofs, so the pigeonhole formulas force exponential time (Haken, 1985).
- CDCL resolves each conflict back to the first unique implication point, learns that clause and backjumps; with watched literals, VSIDS, restarts and clause deletion it handles industrial formulas with millions of clauses, and it is as strong as general resolution.
- Random 3-SAT flips from satisfiable to unsatisfiable near (predicted; proven bounds 3.52 and 4.4898), where solvers work hardest.
- SAT and SMT solvers verify chips and software, plan and schedule, and have settled mathematical questions such as the Boolean Pythagorean triples problem, with checkable proofs.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why do SAT encodings use the Tseitin transformation instead of distributing ORs over ANDs?
- Distribution produces a formula that is not equisatisfiable
- Distribution can produce exponentially many clauses, as for parity, while Tseitin's encoding is linear in the formula's size
- Tseitin's encoding has fewer variables
- Solvers only accept clauses of width 2
Show the answer
B. Distribution can produce exponentially many clauses, as for parity, while Tseitin's encoding is linear in the formula's size
Parity on 20 variables needs 524,288 clauses without new variables, but 77 with Tseitin's. The price is extra variables, which solvers handle easily.
The clause is in the formula, and the current assignment has and . What does unit propagation do?
- Nothing: the clause is satisfied
- It reports a conflict
- It sets
- It sets
Show the answer
C. It sets
With and , the literals and are false, so the clause can only be satisfied by : it is unit, and is forced to 1.
Why can adding learned clauses never make CDCL miss a solution?
- Because learned clauses are deleted after each restart
- Because each learned clause is derived by resolution from the formula, so every satisfying assignment satisfies it
- Because learned clauses only mention decision variables
- Because the solver checks every learned clause against all assignments
Show the answer
B. Because each learned clause is derived by resolution from the formula, so every satisfying assignment satisfies it
Resolution is sound: whatever satisfies and satisfies . So learned clauses are entailed by the formula; they prune only assignments that were going to fail anyway.
For random 3-SAT with clauses, where are DPLL's hardest instances?
- At small , where formulas have many solutions to choose from
- Near the satisfiability threshold, about for large , where formulas are about equally likely to be satisfiable or not
- At large , where every formula is unsatisfiable
- Hardness does not depend on
Show the answer
B. Near the satisfiability threshold, about for large , where formulas are about equally likely to be satisfiable or not
Few clauses leave many solutions that propagation finds quickly; many clauses cause conflicts after few decisions, so refutations are short. Near the threshold neither happens, and the work peaks: in the lesson's experiment with , about 128 calls at – against 21 at and 69 at .
Further reading
- A. Biere, M. Heule, H. van Maaren & T. Walsh (eds.), Handbook of Satisfiability, 2nd ed. (IOS Press, 2021).
- M. Davis, G. Logemann & D. Loveland, "A machine program for theorem-proving," Communications of the ACM 5(7) (1962), 394–397.
- J. P. Marques-Silva & K. A. Sakallah, "GRASP: a search algorithm for propositional satisfiability," IEEE Transactions on Computers 48(5) (1999), 506–521.
- M. W. Moskewicz, C. F. Madigan, Y. Zhao, L. Zhang & S. Malik, "Chaff: engineering an efficient SAT solver," Proceedings of the Design Automation Conference (2001).
- D. E. Knuth, The Art of Computer Programming, Volume 4B: Combinatorial Algorithms, Part 2 (Addison-Wesley, 2022), section 7.2.2.2, "Satisfiability".