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 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 is a finite object: a sequence of configurations, each a tape of 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 on as a tableau: a table with rows, one per time step, and columns. Row is the configuration at time : 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 of states, tape symbols and the border.
One variable per cell and symbol, , means "cell holds ". There are of them, since depends only on . The formula has four groups of clauses:
One symbol per cell. At least one symbol, and no two:
The start. Unit clauses fix row 0 to the start configuration: the border, the start state , the input , the separator and blanks, except for the certificate cells, which are only required to hold , or a blank. These free cells are where a satisfying assignment chooses the certificate.
Acceptance. Some cell holds the accepting state (one long clause):
Legal moves. For every window of the tableau, the six symbols in it form a legal window: one consistent with the transition function . Each window's condition involves only variables, a number fixed by , so it can be written as a CNF of constant size.
Accepting runs give satisfying assignments. If accepts , fill row with 's configuration at time and set the variables accordingly: every group of clauses holds.
Satisfying assignments give accepting runs. gives one symbol per cell; makes row 0 a start configuration for some certificate ; the window lemma, by induction on , makes every row the next configuration of ; and says that appears. So accepts .
Size. There are cells and windows, each contributing a constant number of constant-size clauses, plus the accepting clause with literals. Writing them all takes time polynomial in , hence in .
In the tableau formula, which cells does deliberately leave undetermined, and why?
- The cells of the last row, so that any final configuration is allowed
- The certificate cells of row 0: a satisfying assignment chooses the certificate, so satisfiable means "some certificate is accepted"
- The cells holding the input , so that the formula works for every input
- 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, , and long clauses are split with new variables.
If some is true, set true exactly for . The clauses before the one containing end with a true ; the clauses after it begin with for a false ; and the clause containing is satisfied by .
If every is false, the chain fails. The first clause forces , each middle clause then forces the next , and the last clause is left with false and both of its literals false.
Size. A clause of literals becomes clauses with new variables, so the total size grows by a constant factor.
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 over can explode: has clauses in CNF. Tseitin (1968) instead gives every gate its own variable and three clauses (two for a NOT gate) saying that 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.
| Gate | Clauses for |
|---|---|
| , , | |
| , , | |
| , |
The tableau spends cells on a run that touches only of them per row. Schnorr (1978) and Cook (1988) brought the formula down to quasi-linear size, : first simulate by an oblivious machine, whose head position at every step depends only on the input length (Pippenger & Fischer, 1979, at a cost of a 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 variables cannot be solved in time. Combined with the sparsification lemma and reductions of linear size, it rules out -time algorithms for 3-coloring, independent set, vertex cover and Hamiltonian cycle on graphs with vertices and edges.
Run every program, briefly. In phase , run every program of length on the input for steps, and check each output with .
Cost of a phase. There are at most programs of length , each run for steps:
When succeeds. gets steps once , and the phases up to then cost
Levin's sharper schedule, which weights programs from a prefix-free code by , removes the logarithmic factor. The constant 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 , then 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 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 , the formula has 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 windows. Locality of a Turing machine step is why windows suffice.
- Clause splitting turns a -literal clause into 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 .
- Consequences: SAT solvers are universal NP engines, hardness spreads by single reductions, ETH rules out 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.
Why do constraints on windows suffice to force every row of the tableau to follow from the row above?
- Because a window contains a whole configuration
- Because one step of a Turing machine changes only cells next to the head, so legality can be checked locally
- Because any window size would work equally well
- 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.
Clause splitting turns into 3-literal clauses. How many clauses and new variables does it create?
- 5 clauses, 5 new variables
- 3 clauses, 2 new variables
- 2 clauses, 1 new variable
- 4 clauses, 3 new variables
Show the answer
B. 3 clauses, 2 new variables
: a clause of literals becomes clauses with new variables.
Why does Tseitin's transformation introduce a new variable per gate instead of converting a circuit to CNF by distributing over ?
- Distribution can blow up exponentially, while new variables keep the CNF linear in the circuit's size and preserve satisfiability
- Distribution changes which assignments satisfy the formula
- SAT solvers cannot read formulas produced by distribution
- 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
has 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).