Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

The Cook–Levin Theorem

Why satisfiability is NP-complete: a whole computation encoded as a formula.

How can one problem be as hard as every problem in NP, including problems nobody has thought of yet? Cook and Levin's answer: whatever a polynomial-time verifier does, a Boolean formula can describe step by step. Asking whether some certificate makes the verifier accept is then asking whether a formula is satisfiable.

What you’ll learn

  • State the Cook–Levin theorem precisely, with the definition of NP it relies on, and the size of the formula it produces.
  • Build the tableau formula for a polynomial-time verifier: its variables, its four groups of clauses, and why legal 2×32 \times 3 windows force a legal computation.
  • Reduce SAT to 3-SAT by clause splitting, and circuits to CNF by Tseitin's transformation, proving both equisatisfiable.
  • Explain the consequences of the theorem: chains of reductions, the exponential time hypothesis, and Levin's universal search.

The statement and the plan

SAT is in NP, since a satisfying assignment is a certificate checked in linear time. The work is the other half, NP-hardness, and the plan is to translate computation into logic. A run of VV is a finite object: a sequence of t+1t + 1 configurations, each a tape of O(t)O(t) cells (the head moves one cell per step, so it never sees more). Boolean variables can describe such a table, clauses can say that it is a correct accepting run, and the certificate becomes the part of the table that the formula leaves free.

Cook stated his theorem for polynomial-time reductions that may consult the target problem many times, and phrased it for tautologies; his construction is in fact a single translation, the many-one kind that Karp used in 1972. Levin, working independently in the Soviet Union on search problems, identified several "universal" problems with the same property. Sipser's textbook proves the theorem with a nondeterministic machine instead of a verifier; the two views are equivalent, because the machine's guesses are a certificate.

Writing a computation as a formula

Write the run of VV on x#cx\#c as a tableau: a table with t+1t + 1 rows, one per time step, and w=O(t)w = O(t) columns. Row ii is the configuration at time ii: the tape contents with the state symbol written just left of the cell under the head, and a #\# at each end. Every cell holds one symbol from the finite set C=Q∪Γ∪{#}C = Q \cup \Gamma \cup \{\#\} of states, tape symbols and the border.

One variable per cell and symbol, xi,j,sx_{i,j,s}, means "cell (i,j)(i, j) holds ss". There are (t+1)⋅w⋅∣C∣=O(t2)(t+1) \cdot w \cdot \abs{C} = O(t^2) of them, since ∣C∣\abs{C} depends only on VV. The formula φx=φcell∧φstart∧φaccept∧φmove\varphi_x = \varphi_{\text{cell}} \land \varphi_{\text{start}} \land \varphi_{\text{accept}} \land \varphi_{\text{move}} has four groups of clauses:

DerivationThe four groups of clauses
  1. φcell  =  ⋀i,j[(⋁s∈Cxi,j,s)∧⋀s≠s′(¬xi,j,s∨¬xi,j,s′)]\varphi_{\text{cell}} \;=\; \bigwedge_{i,j}\Big[\Big(\bigvee_{s \in C} x_{i,j,s}\Big) \land \bigwedge_{s \ne s'} \big(\neg x_{i,j,s} \lor \neg x_{i,j,s'}\big)\Big]

    One symbol per cell. At least one symbol, and no two:

  2. φstart  =  ⋀fixed jx0,j,sj  ∧⋀certificate j(x0,j,0∨x0,j,1∨x0,j,⊔)\varphi_{\text{start}} \;=\; \bigwedge_{\text{fixed } j} x_{0,j,s_j} \;\land \bigwedge_{\text{certificate } j} \big(x_{0,j,0} \lor x_{0,j,1} \lor x_{0,j,\sqcup}\big)

    The start. Unit clauses fix row 0 to the start configuration: the border, the start state q0q_0, the input x1⋯xnx_1 \cdots x_n, the separator and blanks, except for the p(n)p(n) certificate cells, which are only required to hold 00, 11 or a blank. These free cells are where a satisfying assignment chooses the certificate.

  3. φaccept  =  ⋁i,jxi,j,qacc\varphi_{\text{accept}} \;=\; \bigvee_{i,j} x_{i,j,q_{\text{acc}}}

    Acceptance. Some cell holds the accepting state (one long clause):

  4. φmove  =  ⋀0≤i<t, 1<j<wlegal(i,j),∣legal(i,j)∣=O(1)\varphi_{\text{move}} \;=\; \bigwedge_{0 \le i < t,\ 1 < j < w} \text{legal}(i, j), \qquad \abs{\text{legal}(i,j)} = O(1)

    Legal moves. For every 2×32 \times 3 window of the tableau, the six symbols in it form a legal window: one consistent with the transition function δ\delta. Each window's condition involves only 6∣C∣6\abs{C} variables, a number fixed by VV, so it can be written as a CNF of constant size.

DerivationCorrectness and size
  1. x∈L  ⟹  φx satisfiablex \in L \;\Longrightarrow\; \varphi_x \text{ satisfiable}

    Accepting runs give satisfying assignments. If VV accepts x#cx\#c, fill row ii with VV's configuration at time ii and set the variables accordingly: every group of clauses holds.

  2. φx satisfiable  ⟹  ∃c: V accepts x#c  ⟹  x∈L\varphi_x \text{ satisfiable} \;\Longrightarrow\; \exists c:\ V \text{ accepts } x\#c \;\Longrightarrow\; x \in L

    Satisfying assignments give accepting runs. φcell\varphi_{\text{cell}} gives one symbol per cell; φstart\varphi_{\text{start}} makes row 0 a start configuration for some certificate cc; the window lemma, by induction on ii, makes every row the next configuration of VV; and φaccept\varphi_{\text{accept}} says that qaccq_{\text{acc}} appears. So VV accepts x#cx\#c.

  3. ∣φx∣  =  O(t(n)2)  =  nO(1)\abs{\varphi_x} \;=\; O\big(t(n)^2\big) \;=\; n^{O(1)}

    Size. There are O(t2)O(t^2) cells and O(t2)O(t^2) windows, each contributing a constant number of constant-size clauses, plus the accepting clause with O(t2)O(t^2) literals. Writing them all takes time polynomial in tt, hence in nn.

Check your understanding

In the tableau formula, which cells does φstart\varphi_{\text{start}} deliberately leave undetermined, and why?

  1. The cells of the last row, so that any final configuration is allowed
  2. The certificate cells of row 0: a satisfying assignment chooses the certificate, so satisfiable means "some certificate is accepted"
  3. The cells holding the input xx, so that the formula works for every input
  4. The state symbols, so that the machine may behave nondeterministically
Show the answer

B. The certificate cells of row 0: a satisfying assignment chooses the certificate, so satisfiable means "some certificate is accepted"

The verifier's question is "does some certificate work?", an existential question, and satisfiability is existential too. Fixing the input and leaving the certificate free makes the two match exactly.

From SAT to 3-SAT, and from circuits to CNF

The tableau formula has clauses of many lengths. To show that 3-SAT is NP-complete, rewrite every clause with exactly three literals without changing satisfiability. Short clauses are padded by repeating a literal, (a)→(a∨a∨a)(a) \to (a \lor a \lor a), and long clauses are split with new variables.

DerivationProof
  1. yj=[ j≤r−2 ]⟹every clause of the chain is satisfiedy_j = [\,j \le r - 2\,] \quad\Longrightarrow\quad \text{every clause of the chain is satisfied}

    If some ℓr\ell_r is true, set yjy_j true exactly for j≤r−2j \le r - 2. The clauses before the one containing ℓr\ell_r end with a true yjy_j; the clauses after it begin with ¬yj\neg y_j for a false yjy_j; and the clause containing ℓr\ell_r is satisfied by ℓr\ell_r.

  2. y1⇒y2⇒⋯⇒yk−3,(¬yk−3∨ℓk−1∨ℓk)=falsey_1 \Rightarrow y_2 \Rightarrow \cdots \Rightarrow y_{k-3}, \qquad (\neg y_{k-3} \lor \ell_{k-1} \lor \ell_k) = \text{false}

    If every ℓr\ell_r is false, the chain fails. The first clause forces y1y_1, each middle clause then forces the next yy, and the last clause is left with ¬yk−3\neg y_{k-3} false and both of its literals false.

  3. k literals  ⟼  k−2 clauses,  k−3 new variablesk \text{ literals} \;\longmapsto\; k - 2 \text{ clauses},\ \ k - 3 \text{ new variables}

    Size. A clause of kk literals becomes k−2k - 2 clauses with k−3k - 3 new variables, so the total size grows by a constant factor.

python
import itertools, random

def to_3cnf(clauses, n):
    """Equisatisfiable 3-CNF. Clauses are lists of nonzero ints over variables 1..n."""
    out = []
    for c in clauses:
        c = list(c)
        if len(c) < 3:
            c = (c * 3)[:3]                    # (a) -> (a a a); (a b) -> (a b a)
        while len(c) > 3:                      # (l1 l2 rest) -> (l1 l2 y) and (not-y rest)
            n += 1
            out.append([c[0], c[1], n])
            c = [-n] + c[2:]
        out.append(c)
    return out, n

def satisfiable(clauses, n):
    return any(all(any((l > 0) == b[abs(l) - 1] for l in c) for c in clauses)
               for b in itertools.product([False, True], repeat=n))

print(to_3cnf([[1, 2, 3, 4, 5]], 5))           # ([[1, 2, 6], [-6, 3, 7], [-7, 4, 5]], 7)
random.seed(4)
for _ in range(1000):
    n = random.randint(1, 4)
    F = [[random.choice([1, -1]) * random.randint(1, n) for _ in range(random.randint(1, 5))]
         for _ in range(random.randint(1, 4))]
    G, N = to_3cnf(F, n)
    assert all(len(c) == 3 for c in G) and satisfiable(F, n) == satisfiable(G, N)
print("equisatisfiable on 1,000 random formulas")

The random formulas include 60 unsatisfiable ones, so both directions of the proof are exercised.

The same idea converts Boolean circuits to CNF. Converting by distributing ∨\lor over ∧\land can explode: (x1∧y1)∨⋯∨(xn∧yn)(x_1 \land y_1) \lor \cdots \lor (x_n \land y_n) has 2n2^n clauses in CNF. Tseitin (1968) instead gives every gate gg its own variable and three clauses (two for a NOT gate) saying that gg equals its gate's output, then adds the unit clause "the output gate is true". The result has linear size and is satisfiable exactly when the circuit is: equisatisfiable rather than equivalent, which is all a reduction needs.

GateClauses for g↔gateg \leftrightarrow \text{gate}
g=a∧bg = a \land b(¬g∨a)(\neg g \lor a), (¬g∨b)(\neg g \lor b), (g∨¬a∨¬b)(g \lor \neg a \lor \neg b)
g=a∨bg = a \lor b(g∨¬a)(g \lor \neg a), (g∨¬b)(g \lor \neg b), (¬g∨a∨b)(\neg g \lor a \lor b)
g=¬ag = \neg a(g∨a)(g \lor a), (¬g∨¬a)(\neg g \lor \neg a)
Tseitin's clauses, checked equisatisfiable against brute force on 400 random circuits. This is how verification tools hand circuits to SAT solvers.

The tableau spends O(t2)O(t^2) cells on a run that touches only O(t)O(t) of them per row. Schnorr (1978) and Cook (1988) brought the formula down to quasi-linear size, O(tlog⁡t)O(t \log t): first simulate VV by an oblivious machine, whose head position at every step depends only on the input length (Pippenger & Fischer, 1979, at a cost of a log⁡t\log t factor in time); then the formula only has to describe the cell under the head at each step.

Consequences, and Levin’s universal search

Three consequences follow from the theorem.

  • SAT is a universal NP language. Every NP problem can be translated into SAT, which is why SAT solvers are used for hardware and software verification, planning and scheduling: encode, then solve.
  • Hardness spreads by reductions. With one NP-complete problem in hand, every further proof is a single reduction: Karp's 21 problems in 1972, thousands since.
  • Fine-grained hardness. The exponential time hypothesis (Impagliazzo & Paturi, 2001) says that 3-SAT on nn variables cannot be solved in 2o(n)2^{o(n)} time. Combined with the sparsification lemma and reductions of linear size, it rules out 2o(n)2^{o(n)}-time algorithms for 3-coloring, independent set, vertex cover and Hamiltonian cycle on graphs with nn vertices and O(n)O(n) edges.
DerivationA simple schedule that almost achieves it
  1. phase i:run p for 2 i−∣p∣ steps,∣p∣≤i\text{phase } i:\quad \text{run } p \text{ for } 2^{\,i - \abs{p}} \text{ steps}, \quad \abs{p} \le i

    Run every program, briefly. In phase i=1,2,3,…i = 1, 2, 3, \dots, run every program pp of length ∣p∣≤i\abs{p} \le i on the input for 2 i−∣p∣2^{\,i - \abs{p}} steps, and check each output with VV.

  2. ∑ℓ≤i2ℓ⋅2 i−ℓ  =  i⋅2i\sum_{\ell \le i} 2^{\ell} \cdot 2^{\,i - \ell} \;=\; i \cdot 2^{i}

    Cost of a phase. There are at most 2ℓ2^{\ell} programs of length ℓ\ell, each run for 2 i−ℓ2^{\,i-\ell} steps:

  3. ∑i≤i∗i 2i  ≤  2 i∗ 2i∗  =  O(2∣A∣ T (∣A∣+log⁡T))\sum_{i \le i^*} i\, 2^i \;\le\; 2\, i^*\, 2^{i^*} \;=\; O\big(2^{\abs{A}}\, T\,(\abs{A} + \log T)\big)

    When AA succeeds. AA gets TT steps once i∗=∣A∣+⌈log⁡2T⌉i^* = \abs{A} + \lceil \log_2 T \rceil, and the phases up to then cost

  4. TU(x)  =  O(2∣A∣ TA(x))  for every AT_U(x) \;=\; O\big(2^{\abs{A}}\, T_A(x)\big) \ \text{ for every } A

    Levin's sharper schedule, which weights programs from a prefix-free code by 2−∣p∣2^{-\abs{p}}, removes the logarithmic factor. The constant 2∣A∣2^{\abs{A}} does not depend on the input, which is all "optimal up to a constant" means, but for any real program it is astronomically large.

So there is an explicit algorithm about which we can say: if P=NP\mathrm{P} = \mathrm{NP}, then UU finds satisfying assignments of satisfiable formulas in polynomial time. The statement is less useful than it sounds. The hidden constant is beyond astronomical, and on unsatisfiable formulas UU simply never stops: it searches for a certificate that does not exist.

Key takeaways

  • Cook–Levin: SAT, and with clause splitting 3-SAT, is NP-complete. For a verifier running in time tt, the formula has O(t2)O(t^2) clauses of constant size plus one long clause.
  • The tableau formula has one variable per cell and symbol, and four groups of clauses: one symbol per cell, the start row with free certificate cells, acceptance, and legal 2×32 \times 3 windows. Locality of a Turing machine step is why windows suffice.
  • Clause splitting turns a kk-literal clause into k−2k - 2 three-literal clauses, and Tseitin's transformation turns a circuit into an equisatisfiable CNF of linear size, avoiding the exponential blow-up of distribution; with oblivious simulation the whole reduction has size O(tlog⁡t)O(t \log t).
  • Consequences: SAT solvers are universal NP engines, hardness spreads by single reductions, ETH rules out 2o(n)2^{o(n)} algorithms for many graph problems, and Levin's universal search is optimal up to a constant factor that is astronomically large.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Why do constraints on 2×32 \times 3 windows suffice to force every row of the tableau to follow from the row above?

  1. Because a window contains a whole configuration
  2. Because one step of a Turing machine changes only cells next to the head, so legality can be checked locally
  3. Because any window size would work equally well
  4. Because the verifier is nondeterministic
Show the answer

B. Because one step of a Turing machine changes only cells next to the head, so legality can be checked locally

Locality is the key property of Turing machines: a step reads and writes one cell and moves the head by one. Every possible change is visible inside a window of constant size, so constant-size clauses describe it.

Question 2

Clause splitting turns (x1∨x2∨x3∨x4∨x5)(x_1 \lor x_2 \lor x_3 \lor x_4 \lor x_5) into 3-literal clauses. How many clauses and new variables does it create?

  1. 5 clauses, 5 new variables
  2. 3 clauses, 2 new variables
  3. 2 clauses, 1 new variable
  4. 4 clauses, 3 new variables
Show the answer

B. 3 clauses, 2 new variables

(x1∨x2∨y1)∧(¬y1∨x3∨y2)∧(¬y2∨x4∨x5)(x_1 \lor x_2 \lor y_1) \land (\neg y_1 \lor x_3 \lor y_2) \land (\neg y_2 \lor x_4 \lor x_5): a clause of kk literals becomes k−2k - 2 clauses with k−3k - 3 new variables.

Question 3

Why does Tseitin's transformation introduce a new variable per gate instead of converting a circuit to CNF by distributing ∨\lor over ∧\land?

  1. Distribution can blow up exponentially, while new variables keep the CNF linear in the circuit's size and preserve satisfiability
  2. Distribution changes which assignments satisfy the formula
  3. SAT solvers cannot read formulas produced by distribution
  4. The new variables make the formula equivalent to the circuit, not just equisatisfiable
Show the answer

A. Distribution can blow up exponentially, while new variables keep the CNF linear in the circuit's size and preserve satisfiability

(x1∧y1)∨⋯∨(xn∧yn)(x_1 \land y_1) \lor \cdots \lor (x_n \land y_n) has 2n2^n clauses in CNF. Tseitin's version has three clauses per gate and is satisfiable exactly when the circuit is, which is all a reduction needs.

Further reading

  • S. A. Cook, "The complexity of theorem-proving procedures," Proceedings of STOC (1971), 151–158.
  • L. A. Levin, "Universal sequential search problems," Problems of Information Transmission 9(3) (1973).
  • M. Sipser, Introduction to the Theory of Computation, 3rd ed. (Cengage, 2012), §7.4.
  • S. Arora & B. Barak, Computational Complexity: A Modern Approach (Cambridge University Press, 2009), chapter 2.
  • C. P. Schnorr, "Satisfiability is quasilinear complete in NQL," Journal of the ACM 25(1) (1978).