Sub topic 10
SAT and the Cook-Levin Theorem
This chapter assumes you already know three things from earlier chapters. You should know the class NP, defined as the problems whose yes-instances have certificates that can be checked in polynomial time. You should know polynomial-time (Karp) reductions. You should also know the definitions of NP-hard and NP-complete. With those in place, there is one gap left: to prove a problem NP-complete by reduction, you first need some problem already known to be NP-complete. The Cook-Levin theorem fills that gap.
The Bootstrapping Problem
The standard recipe for NP-completeness says: show B is in NP, then reduce a known NP-complete problem A to B. The recipe only works once you have a first A. For that first problem there is nothing to reduce from. You must show directly that every problem in NP reduces to it.
Stephen Cook proved this for Boolean satisfiability in 1971. Leonid Levin proved it independently, publishing in 1973. Together their results are called the Cook-Levin theorem.
Satisfiability, Precisely
A Boolean formula is built from variables, NOT, AND, and OR. A literal is a variable x or its negation ¬x. A clause is an OR of literals. A formula in conjunctive normal form (CNF) is an AND of clauses. An assignment gives each variable the value true or false. It satisfies a formula if the formula evaluates to true under it.
- SAT: given a CNF formula φ, is there an assignment that satisfies φ?
- 3-SAT: the same question, restricted to formulas where every clause has exactly three literals.
SAT is in NP because the certificate is the assignment itself. Checking it takes one pass over the formula:
def satisfies(clauses, assignment):
# clauses: list of tuples of nonzero ints; -3 means NOT x3
# assignment: dict mapping variable number -> bool
return all(any(assignment[abs(lit)] == (lit > 0) for lit in clause)
for clause in clauses)
phi = [(1, -2), (2, 3), (-1, -3)]
print(satisfies(phi, {1: True, 2: True, 3: False})) # True
print(satisfies(phi, {1: True, 2: False, 3: True})) # FalseThis check runs in time linear in the size of the formula. That covers the "in NP" half of NP-completeness. The hard half is NP-hardness.
The Theorem
Cook-Levin theorem. SAT is NP-complete. In particular, for every language L in NP there is a polynomial-time function that maps each string w to a CNF formula φw such that w ∈ L if and only if φw is satisfiable.
The reduction is generic. It does not use anything special about L. It uses only the fact that L has a polynomial-time verifier, or equivalently a nondeterministic Turing machine M that decides L in time p(n) for some polynomial p.
Proof Sketch: Encoding a Computation as a Formula
The tableau
An accepting run of M on w, with |w| = n, takes at most p(n) steps. Each step uses at most p(n) tape cells. Write the run as a grid of p(n) rows. Row i is the configuration after step i: the tape contents, with the machine's current state written into the cell where the head is. If the machine halts early, repeat the final row so that the grid always has p(n) rows.
Let C be the finite set of states and tape symbols. For each row i, column j, and symbol s in C, create a variable x(i,j,s). It means "cell (i,j) holds s." That gives p(n)² · |C| variables. Since |C| is a constant that depends only on M, this is O(p(n)²) variables.
The four constraint groups
| Group | What it forces | Clause size per cell or window | Column | Column |
|---|---|---|---|---|
| Cell | Each cell holds exactly one symbol: one "at least one" clause plus pairwise "not both" clauses | O(\ | C\ | ²), a constant |
| Start | Row 0 is the start configuration: start state, then w, then blanks | O(1) per cell | ||
| Accept | Some cell in the tableau holds the accepting state | One clause of O(p(n)²) literals | ||
| Move | Every 2×3 window of adjacent rows is consistent with M's transition function | Constant, because only finitely many windows are legal |
The Move group carries the main idea. A Turing machine step changes the tape only near the head. So a pair of rows is a legal step exactly when every 2×3 window of the two rows is legal. The legality of a window depends only on M, so the list of legal windows is a fixed finite table. For each window position, "this window is one of the legal ones" is a constant-size Boolean condition. You can rewrite it in CNF, and the size is still a constant.
Size and running time
The four groups together contain O(p(n)²) clauses, each of constant size except the single Accept clause. Writing them out takes polynomial time in n. The reduction can be computed in polynomial time even though it describes a nondeterministic computation. The formula does not simulate M. It only states the rules that a valid run must obey.
Correctness in both directions
- If w ∈ L: some run of M accepts w. Fill in the tableau from that run and set each x(i,j,s) to match it. Every clause holds, so φ_w is satisfiable.
- If φ_w is satisfiable: the Cell clauses make the assignment describe exactly one symbol per cell. Start fixes row 0. Move makes each row a legal successor of the row before it. Accept puts the accepting state somewhere. So the assignment describes an accepting run, and w ∈ L.
From SAT to 3-SAT
Most later reductions, including the gadget reduction in the next chapter, start from 3-SAT because its clauses have a fixed shape. SAT reduces to 3-SAT clause by clause:
- A clause with one or two literals is padded by repeating a literal.
- A clause (ℓ₁ ∨ ℓ₂ ∨ … ∨ ℓk) with k > 3 is split into a chain using fresh variables y₁ … y{k−3}: (ℓ₁ ∨ ℓ₂ ∨ y₁), (¬y₁ ∨ ℓ₃ ∨ y₂), …, (¬y{k−3} ∨ ℓ{k−1} ∨ ℓ_k).
If some ℓt is true, set the y variables before it to true and the ones after it to false, and every chain clause is satisfied. If every ℓ is false, the y variables would have to satisfy a chain of implications y₁, y₁→y₂, …, ¬y{k−3}, and no assignment can do that.
import random
from itertools import product
def satisfies(clauses, assignment):
return all(any(assignment[abs(l)] == (l > 0) for l in c) for c in clauses)
def brute_force_sat(clauses):
vs = sorted({abs(l) for c in clauses for l in c})
for bits in product([False, True], repeat=len(vs)):
a = dict(zip(vs, bits))
if satisfies(clauses, a):
return a
return None
def to_3sat(clauses):
next_var = max((abs(l) for c in clauses for l in c), default=0) + 1
out = []
for c in clauses:
c = list(c)
if not c:
raise ValueError("empty clause: formula is trivially unsatisfiable")
if len(c) == 1:
out.append((c[0], c[0], c[0]))
elif len(c) == 2:
out.append((c[0], c[0], c[1]))
elif len(c) == 3:
out.append(tuple(c))
else:
y = next_var; next_var += 1
out.append((c[0], c[1], y))
for lit in c[2:-2]:
z = next_var; next_var += 1
out.append((-y, lit, z))
y = z
out.append((-y, c[-2], c[-1]))
return out
random.seed(1)
for _ in range(300):
f = [tuple(random.choice([1, -1]) * random.randint(1, 4)
for _ in range(random.randint(1, 5)))
for _ in range(random.randint(1, 4))]
g = to_3sat(f)
assert all(len(c) == 3 for c in g)
assert (brute_force_sat(f) is None) == (brute_force_sat(g) is None)
print("SAT -> 3-SAT preserved satisfiability on all tests")The output size is linear in the input size, so the reduction runs in polynomial time. 3-SAT is in NP for the same reason SAT is, so 3-SAT is NP-complete.
Edge Cases and Common Confusions
- Formula size must be polynomial. You can convert an arbitrary formula to CNF by distributing OR over AND, but that can blow up exponentially. Cook-Levin avoids this because each window constraint has constant size. When you need to convert a general formula, the Tseitin transformation adds fresh variables and keeps the output linear in size.
- Circuit-SAT is a common alternative starting point. Many textbooks first encode the verifier as a Boolean circuit and then translate gates to clauses. The idea is the same: local consistency checks over a polynomial-size record of the computation.
- Satisfiable and equivalent are different properties. φ_w and its 3-CNF version are equisatisfiable, not logically equivalent, because the new variables change the set of models. Reductions only need equisatisfiability.
Exercise
In the Cook-Levin construction, why is it enough to check 2×3 windows instead of whole pairs of rows when making sure each row follows legally from the previous one?
Show answer
In one step a Turing machine changes only the cell under its head and moves the head by at most one cell. Every change between consecutive rows therefore fits inside some window that is 3 cells wide. Cells away from the head must stay the same, and windows check that too. If every window is legal, the whole pair of rows is a legal step. Because there are only finitely many legal windows, each check has constant size.
Exercise
Apply to_3sat to the single clause (1, 2, 3, 4, 5). List the output clauses, and give values for the fresh variables that satisfy them when only x4 is true.
Show answer
Fresh variables are 6 and 7. The output is (1, 2, 6), (-6, 3, 7), (-7, 4, 5). With only x4 true, set x6 = true and x7 = true. Then (1,2,6) holds through x6, (-6,3,7) holds through x7, and (-7,4,5) holds through x4.
Exercise
A classmate argues: "Cook-Levin shows SAT is NP-hard, and SAT reduces to Integer Linear Programming, so ILP is NP-hard. Therefore ILP is NP-complete." What is missing?
Show answer
NP-completeness also requires membership in NP. The reduction shows only that ILP is NP-hard. To show ILP is in NP, you must also prove that every feasible ILP has a solution whose bit length is polynomial in the input size, which is a known but nontrivial result.