The Cook–Levin Theorem

Any checkable problem can be rewritten as one giant logic formula. Watch a machine’s whole computation compiled into clauses, solved, and decoded back into the run

The Cook–Levin Theorem

In 1971 Stephen Cook proved something that sounds impossible: one problem, the satisfiability of logic formulas, is at least as hard as every problem in NP, including ones nobody has thought of yet. The proof is a compiler. It takes a machine that checks answers and writes out a formula that can be satisfied exactly when the machine can be made to accept. This lesson runs it.

Computation Is Local

Point at any cell below the top row: the three cells above it are all that decide it. Then switch to Corrupt a cell, change one anywhere, and watch which 2 × 3 windows fail.

16 × 5
tableau: time steps × tape cells
75
windows checked, one per cell below row 0
60 of 837
of this machine’s legal windows appear in the run
0
illegal windows right now
start011□have011The cell at time 1, position 1 holds have0▸1. The three cells above it read start▸0 1 1, and with that above, the rule table allows only have0▸1 below the middle. This window is legal.
Row 0 is the start configuration and all 75 windows are legal, so this grid is a genuine run: the machine accepts after 15 steps. No check looked at more than six cells.
The 60 different windows this run uses
□start01□□have01start011□have011110have011010□10□0□□0□□□□have01□□1□have011□1have01have01101have01010□have010□□□1□□1□1have01□111have01011have00have010□1have00□0□□have00□□□11□1111have001101have00□10have0□have00□□0have0□□11011check0010have0□1check00□0have0□□check00□□□11□1back111check001back1□1check00□back1□□check00□□□□□□□1□□back1□1back1□back111back1□back11□back1□□1□□□□□□□□□□back1□back□1□back11back□11back11□11□1□□1□□□back□1□□start1back□11□start1111□start11□□□start1□□□□start11□□have11start11□□have11□1□□have11□□□□have11□□1□have11□□1have1□have11□□1have1□□□□□have1□□□□□1□□check11□1have1□□check11□1have1□□check11□□have1□□□□□□□□□□□back□□□check11□back□□□check11□back□□□check11□□□□□□□back□□□□□back□□□□start□back□□□□start□□□□□start□□□□□start□□□accept□□start□□□accept□□start□□□accept□□□

Row t is the tape at time t; time runs downwards. The tinted cell is under the head and names the machine’s state (emerald once it accepts, rose if it rejects); □ is a blank. The sky bar marks the three cells that determine the selected one, and the dashed box is the 2 × 3 window around them. Cells beyond the edge of the grid count as blank. Inputs are capped at 6 symbols so that the whole run fits on screen.

Go deeper:Why a Whole Computation Can Be Checked Through a Keyhole

Write a run of a Turing machine as a grid, the tableau: row t is the tape at time t, and the cell under the head also records the machine’s state. In one step the head rewrites the cell it is on and moves at most one cell. So a cell can only change if the head was on it or next to it a moment before, and what it changes to depends only on those neighbours. Every cell of row t + 1 is settled by the three cells above it.

For a machine that makes choices, three cells above no longer fix the cell below, because the head may have more than one option. That is why the windows are 2 × 3 and not a single cell under three: a window sees the head’s old cell and both places it could go, so it can insist that what was written, where the head went and what state it entered all belong to the same rule. A window is legal if some one step of the machine produces it. The list is finite and depends only on the rule table, not on the input or the length of the run.

The result is a purely local test. A grid is a genuine computation exactly when its first row is the start configuration and every 2 × 3 window is legal: a very large AND of very small checks, each looking at six cells. (The first row has exactly one head; legal windows then keep it that way, row after row.)

Locality is the only fact about Turing machines the theorem uses. Any reasonable model of computation in which one step changes a bounded amount of state near a bounded number of places has it too, which is why the same proof goes through for all of them, and why nothing here depends on the details of this particular kind of machine.

The Compiler

Press Solve and watch a SAT solver fill in the tableau of a machine it never runs. Then flip an input symbol, shorten the time bound, or try the no-instance, and step through the four groups of clauses.

Input
2,430
variables: 9 × 9 cells × 30 contents
41,558
clauses
0
solver decisions (guesses)
0
forced assignments
0
dead ends backed out of
The machine accepts if some symbol equals the one three cells to its right, and it finds the place by guessing. The formula asks whether the grid above can be filled in so that all 41,558 clauses hold. Press Solve, then flip an input symbol and solve again.

Row 0 is the machine’s start configuration on this input: one clause per cell, each a single variable.

At time 0, cell 0 holds what the start configuration puts there.
(x(0, 0, scan▸0))
At time 0, cell 1 holds what the start configuration puts there.
(x(0, 1, 1))
At time 0, cell 2 holds what the start configuration puts there.
(x(0, 2, 0))
Size of the formula as the input grows (logarithmic scale). The dots are this instance; the dashed line is the largest input this demo builds.

The variable x(t, i, c) means “at time t, tape cell i holds c”, where c is a symbol or, written state▸symbol, the head in that state over that symbol; □ is a blank. The head is indigo while it scans, amber once it has guessed, emerald when it accepts. The formula has 30 variables and about 513 clauses for each of the 9 × 9 cells, so it grows like the square of the time bound, with a constant that depends only on the machine. Inputs are capped at 8 symbols (at most 74,303 clauses); the solver is slowed down so that you can watch it, and finishes in a few milliseconds otherwise. This machine never moves left, so the tableau starts at cell 0; a general machine needs columns on both sides.

Go deeper:The Theorem, and How the Clauses Are Written

Theorem (Cook 1971; Levin 1973). Satisfiability is NP-complete. It is in NP, because a satisfying assignment is a certificate that is easy to check. And every problem in NP reduces to it in polynomial time. A problem in NP has a polynomial-time verifier; a verifier is a machine; and the question “does this machine accept this input, together with some certificate, within T steps?” compiles to a formula whose size is polynomial in T. The formula is satisfiable exactly when the answer is yes. In this demo the certificate is the machine’s sequence of guesses, which is the same thing seen from the machine’s side.

The compiler makes one variable for each triple (time, cell, content), where a content is a tape symbol or a state sitting over a symbol. Start pins row 0. One thing per cell is one clause saying “at least one content” and one two-literal clause for each pair of contents saying “not both”. Accept is a single clause listing every place the accepting state could appear. All the work is in Legal moves, which this demo writes as three kinds of implication for every cell: a head does one of the things its rule allows, on its own cell and on the neighbour it steps to; a symbol with no head on it stays; and a head appears only where a rule could have sent it. Each clause mentions cells inside one 2 × 3 window and, below a correct first row, together they hold exactly when every window is legal. Listing the legal windows one by one, as the textbook proof does, gives an equivalent but much longer formula.

The count is what matters. The tableau has (T + 1) rows and, for a head that can travel T cells, on the order of T columns. Each cell contributes a fixed number of variables and clauses that depends on the machine and not on the input. So the formula has on the order of T² variables and clauses, and writing it down takes polynomial time. Nothing in the compiler searches for the accepting run; that job is passed to whoever has to satisfy the formula.

Cook’s paper is “The Complexity of Theorem-Proving Procedures” (1971). Leonid Levin obtained the same result independently in the Soviet Union and published it in 1973. The consequence is a single test case for the whole class: a polynomial-time algorithm for satisfiability would give one for every problem in NP, so P = NP if and only if satisfiability is in P. Whether such an algorithm exists is the open question. The solver in this demo is fast because the instances are tiny and highly structured; that is evidence about these instances and nothing more.

From Any Checker to 3-SAT

Walk through the four stages, tapping gates to see their clauses. Then tap pairs of vertices to add edges, or join vertices 0–3 completely, and watch the formula become unsatisfiable.

012345
34
gates: 6 vertices + 3 × 9 edges + 1
46 → 59
variables: one per wire → after splitting
134 → 147
clauses: after Tseitin → as 3-SAT
solving…
0 decisions, 1 forced assignments
The checker reads two bits per vertex (amber, on the left) and answers one question: is this a proper colouring with three colours? Tap a gate to see what it checks. Selected: NAND gate: vertex 0 does not carry the fourth colour, that is, its two bits are not both 1.

Drag vertices to move them; tap one vertex and then another to add or remove the edge between them. Each vertex has two bits, a and b: 00, 01 and 10 are the three colours and 11 is ruled out. In the clauses, ok is a vertex check, p and q compare the two bits across an edge, d says an edge’s ends differ, and out is the verdict. Wires and gates turn emerald where the solver’s assignment makes them true. Graphs are capped at 8 vertices so the circuit stays readable; the solver runs in slices and finishes in milliseconds at this size, which says nothing about large graphs.

Go deeper:Tseitin’s Encoding, and Why Three Literals

In practice nobody compiles Turing machines. A checker for inputs of a fixed size is a circuit, and a circuit turns into clauses one gate at a time. Give each wire a variable and, for each gate, write the few clauses that say its output equals the gate applied to its inputs. For z = a ∧ b they are (¬z ∨ a), (¬z ∨ b) and (z ∨ ¬a ∨ ¬b). Add one clause saying the output wire is true. The formula is linear in the size of the circuit, and its satisfying assignments are exactly the inputs the circuit accepts, each extended by the values on its wires. This is Tseitin’s encoding (1968), and it is how hardware and software verification problems are handed to SAT solvers today.

A clause with more than three literals can be broken into a chain of three-literal clauses linked by fresh variables, without changing whether the formula can be satisfied. So satisfiability stays NP-complete when every clause has at most three literals. This problem, 3-SAT, appears in Richard Karp’s 1972 list of 21 NP-complete problems, and it has been the usual starting point for hardness proofs ever since. With at most two literals per clause the problem is different in kind: 2-SAT can be solved in polynomial time. The step from two literals to three is where the hardness appears.

Nothing in this demo is special to colouring. Replace the checker by any polynomial-size circuit that verifies a certificate, and the same two steps produce a 3-SAT formula that is satisfiable exactly when a valid certificate exists. That is the Cook–Levin theorem in the form people use.

The phrase “NP-complete”, and the practice of proving a new problem hard by translating a known NP-complete problem into it, both descend from this theorem. Note what it does and does not say: NP-complete problems are the hardest problems in NP, not the hardest problems there are, and many of their instances, like the ones on this page, are easy in practice. See also: The Web of Reductions, and Reductions in the Computational Complexity module.

Key Takeaways

  • A grid of symbols is a genuine computation exactly when its first row is right and every 2 × 3 window is legal.
  • “This machine accepts within T steps” compiles to a formula with on the order of T² variables and clauses.
  • Satisfiability is therefore NP-complete (Cook 1971, Levin 1973): P = NP if and only if it is in P, and that is open.
  • Any checking circuit becomes 3-SAT in linear size: a few clauses per gate, then long clauses split into chains.

Spot a mistake or have an idea?

Search