Simple Algorithm
Algorithms & Data Structures · Optimal · 26 min

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 2n−12^{n-1} 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 ii is the integer ii, its negation is −i-i, 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.

GateClauses for the new variable yy
y↔(a∧b)y \leftrightarrow (a \wedge b)(¬y∨a)(\neg y \vee a), (¬y∨b)(\neg y \vee b), (y∨¬a∨¬b)(y \vee \neg a \vee \neg b)
y↔(a∨b)y \leftrightarrow (a \vee b)(y∨¬a)(y \vee \neg a), (y∨¬b)(y \vee \neg b), (¬y∨a∨b)(\neg y \vee a \vee b)
y↔(a⊕b)y \leftrightarrow (a \oplus b)(¬y∨a∨b)(\neg y \vee a \vee b), (¬y∨¬a∨¬b)(\neg y \vee \neg a \vee \neg b), (y∨¬a∨b)(y \vee \neg a \vee b), (y∨a∨¬b)(y \vee a \vee \neg b)
Each clause forbids one combination of values inconsistent with the gate.
python
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 77

For n=4n = 4, 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 x1,…,xnx_1, \dots, x_n" takes (n2)\binom{n}{2} clauses pairwise, while Sinz's sequential counter (2005) needs only 3n−43n - 4 clauses with n−1n - 1 auxiliary variables, a large saving for big groups.

Check your understanding

The Tseitin transformation turns a formula FF into a CNF T(F)T(F) with extra variables. Which statement is correct?

  1. T(F)T(F) is logically equivalent to FF
  2. T(F)T(F) is satisfiable exactly when FF is, and each model of FF extends to exactly one model of T(F)T(F)
  3. T(F)T(F) is always satisfiable, since the new variables can be chosen freely
  4. T(F)T(F) is exponentially larger than FF in the worst case
Show the answer

B. T(F)T(F) is satisfiable exactly when FF is, and each model of FF extends to exactly one model of T(F)T(F)

The new variables are forced to equal their gates' outputs, so a model of FF determines them uniquely, and any model of T(F)T(F) restricted to the original variables satisfies FF. It is not equivalent: T(F)T(F) constrains variables that FF does not mention. Its size is linear in the size of FF.

DPLL: search with propagation

Davis and Putnam (1960) eliminated variables by resolution, the rule that derives A∨BA \vee B from clauses A∨xA \vee x and B∨¬xB \vee \neg x; 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.)

python
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 x1x_1. With x1x_1 true, propagation forces x3x_3, then x4x_4, then x5x_5, and the clause ¬x5∨¬x1\neg x_5 \vee \neg x_1 has every literal false: a conflict. With x1x_1 false, the first clause forces x2x_2, then x3x_3, x4x_4 and x5x_5 follow, and every clause is satisfied: three calls in all.

Predict before you look

The pigeonhole formula for 11 pigeons and 10 holes uses a variable pijp_{ij} for "pigeon ii sits in hole jj", a clause per pigeon saying it sits somewhere, and a clause ¬pij∨¬pi′j\neg p_{ij} \vee \neg p_{i'j} for every hole and every pair of pigeons. How many clauses is that?

Show the answer

561

11 clauses for the pigeons, plus 10⋅(112)=10⋅55=55010 \cdot \binom{11}{2} = 10 \cdot 55 = 550 for the holes: 561 clauses over 110 variables. A tiny formula that every resolution-based solver needs exponential time to refute as nn 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 c1=¬x2∨x3c_1 = \neg x_2 \vee x_3, c2=¬x1∨¬x3∨x4c_2 = \neg x_1 \vee \neg x_3 \vee x_4, c3=¬x3∨x5c_3 = \neg x_3 \vee x_5 and c4=¬x4∨¬x5c_4 = \neg x_4 \vee \neg x_5, inside some larger formula. The solver decides x1=1x_1 = 1 at level 1 and x2=1x_2 = 1 at level 2. Then c1c_1 forces x3x_3, c2c_2 forces x4x_4 and c3c_3 forces x5x_5, all at level 2, and c4c_4 is falsified.

DerivationConflict analysis is resolution
  1. c4 = ¬x4∨¬x5c_4\ =\ \neg x_4 \vee \neg x_5

    Start from the falsified clause:

  2. ¬x3∨¬x4\neg x_3 \vee \neg x_4

    Resolve with the reason for x5x_5, c3=¬x3∨x5c_3 = \neg x_3 \vee x_5:

  3. ¬x1∨¬x3\neg x_1 \vee \neg x_3

    Two literals of the conflict level remain; resolve with the reason for x4x_4, c2c_2:

  4. learn ¬x1∨¬x3;at level 1: x3←0,  x2←0\text{learn } \neg x_1 \vee \neg x_3; \qquad \text{at level 1: } x_3 \leftarrow 0, \ \ x_2 \leftarrow 0

    Now a single literal, ¬x3\neg x_3, belongs to level 2: x3x_3 is the first unique implication point (1-UIP), a node on every path from the decision x2x_2 to the conflict. Learn the clause, and backjump to level 1, the highest level among its other literals, where it is unit: it forces x3=0x_3 = 0, and then c1c_1 forces x2=0x_2 = 0.

Why the 1-UIP clause rather than the decisions, ¬x1∨¬x2\neg x_1 \vee \neg x_2, which is also entailed? The UIP clause blames x3x_3 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.
Check your understanding

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?

  1. Level 11, undoing only the last decision
  2. Level 7
  3. Level 3
  4. 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 nn variables and m=αnm = \alpha n 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).

python
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.0

Uses the dpll function of the previous step. Counting its calls gives the last column of the table below.

Clause density α\alphaSatisfiable, n=20n = 20Satisfiable, n=40n = 40Mean DPLL calls, n=40n = 40
3.0100%100%21
3.598.5%100%38
4.081%86%99
4.2574%63.5%128
4.560.5%44%129
5.033.5%11%115
6.04.5%0%69
200 random formulas per cell, with the seed 2025 for each nn. Doubling nn makes the drop steeper, and the work peaks where the drop is.
Check your understanding

At α=6\alpha = 6 almost every formula is unsatisfiable, yet DPLL needs fewer calls than at α=4.5\alpha = 4.5. Why?

  1. Because DPLL gives up early on dense formulas
  2. Because with many clauses, unit propagation reaches a conflict after few decisions, so the search tree that proves unsatisfiability is small
  3. Because dense formulas have fewer variables
  4. 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 α\alpha the work still grows exponentially in nn, 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 kk 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 kk 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 O∗((4/3)n)O^*((4/3)^n) 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 1.307n1.307^n. The Strong Exponential Time Hypothesis conjectures that the base must approach 2 as the clause width kk 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 2n−12^{n-1} clauses without auxiliary variables, and the sequential counter encodes "at most one" in 3n−43n - 4 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 α≈4.27\alpha \approx 4.27 (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.

Question 1

Why do SAT encodings use the Tseitin transformation instead of distributing ORs over ANDs?

  1. Distribution produces a formula that is not equisatisfiable
  2. Distribution can produce exponentially many clauses, as for parity, while Tseitin's encoding is linear in the formula's size
  3. Tseitin's encoding has fewer variables
  4. 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.

Question 2

The clause ¬a∨b∨¬c\neg a \vee b \vee \neg c is in the formula, and the current assignment has a=1a = 1 and c=1c = 1. What does unit propagation do?

  1. Nothing: the clause is satisfied
  2. It reports a conflict
  3. It sets b=1b = 1
  4. It sets b=0b = 0
Show the answer

C. It sets b=1b = 1

With a=1a = 1 and c=1c = 1, the literals ¬a\neg a and ¬c\neg c are false, so the clause can only be satisfied by bb: it is unit, and bb is forced to 1.

Question 3

Why can adding learned clauses never make CDCL miss a solution?

  1. Because learned clauses are deleted after each restart
  2. Because each learned clause is derived by resolution from the formula, so every satisfying assignment satisfies it
  3. Because learned clauses only mention decision variables
  4. 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 A∨xA \vee x and B∨¬xB \vee \neg x satisfies A∨BA \vee B. So learned clauses are entailed by the formula; they prune only assignments that were going to fail anyway.

Question 4

For random 3-SAT with m=αnm = \alpha n clauses, where are DPLL's hardest instances?

  1. At small α\alpha, where formulas have many solutions to choose from
  2. Near the satisfiability threshold, about α≈4.27\alpha \approx 4.27 for large nn, where formulas are about equally likely to be satisfiable or not
  3. At large α\alpha, where every formula is unsatisfiable
  4. Hardness does not depend on α\alpha
Show the answer

B. Near the satisfiability threshold, about α≈4.27\alpha \approx 4.27 for large nn, 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 n=40n = 40, about 128 calls at α=4.25\alpha = 4.25–4.54.5 against 21 at α=3\alpha = 3 and 69 at α=6\alpha = 6.

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".