Sub topic 10
Cook-Levin: SAT Is NP-Complete
This chapter assumes you know the definitions of NP, polynomial-time many-one reductions, and NP-completeness from earlier chapters. It also assumes you can read Boolean formulas and simple Python. Here we look at the theorem that showed NP-complete problems exist at all, and at why its proof works.
The Bootstrapping Problem
To prove a problem B is NP-hard, you usually reduce a known NP-hard problem A to B. That method needs a first NP-hard problem to start from. Someone has to show, directly, that every problem in NP reduces to one specific problem.
Stephen Cook did this in 1971, and Leonid Levin did it independently around the same time. Their result:
Cook-Levin theorem. The Boolean satisfiability problem (SAT) is NP-complete.
Every NP-completeness proof since then traces back to this one theorem through a chain of reductions.
SAT and CNF
A Boolean formula is built from variables, AND, OR, and NOT. SAT asks: given a formula, is there an assignment of true/false values to its variables that makes the formula true?
Most statements of the theorem use conjunctive normal form (CNF). A CNF formula is an AND of clauses. Each clause is an OR of literals, and a literal is a variable or its negation. For example:
(x1 ∨ ¬x2) ∧ (x2 ∨ x3)
The theorem holds for CNF-SAT directly. That matters because CNF is the input format for every modern SAT solver, and it makes later reductions easier.
Part One: SAT Is in NP
This half is easy. The certificate is an assignment, and checking it takes time linear in the size of the formula:
from itertools import combinations, product
def verify_cnf(clauses, assignment):
"""clauses: list of clauses, each a list of nonzero ints (DIMACS style:
3 means x3, -3 means NOT x3). assignment: dict mapping variable number to bool."""
for clause in clauses:
if not any(assignment[abs(lit)] == (lit > 0) for lit in clause):
return False
return True
def exactly_one(variables):
"""CNF clauses forcing exactly one of the given variables to be true."""
clauses = [list(variables)] # at least one
for a, b in combinations(variables, 2):
clauses.append([-a, -b]) # no two together
return clauses
def count_models(clauses, num_vars):
"""Brute force: try all 2^num_vars assignments."""
count = 0
for bits in product([False, True], repeat=num_vars):
assignment = {i + 1: bits[i] for i in range(num_vars)}
if verify_cnf(clauses, assignment):
count += 1
return count
# One tableau cell, three possible symbols: variables 1, 2, 3.
cell = exactly_one([1, 2, 3])
print(cell) # [[1, 2, 3], [-1, -2], [-1, -3], [-2, -3]]
print(count_models(cell, 3)) # 3: the cell holds exactly one symbol
# Add a "start condition": the cell must hold symbol 1.
print(count_models(cell + [[1]], 3)) # 1verify_cnf is the polynomial-time verifier that puts SAT in NP. exactly_one is the main building block of the hardness proof below. count_models runs in exponential time, and you should read it as a reminder that checking an assignment is cheap while searching for one is not.
Part Two: Every NP Problem Reduces to SAT
Take any language L in NP. By definition, a nondeterministic Turing machine N decides L in time n^k for some constant k. Equivalently, a deterministic verifier V accepts (x, c) within polynomial time for some certificate c exactly when x is in L. Given an input x of length n, we must build in polynomial time a CNF formula φ_x such that:
φ_x is satisfiable ⟺ N has an accepting computation on x.
The Tableau
An accepting computation of N on x is a sequence of at most n^k configurations. Each configuration records the tape contents, the head position, and the state. Each configuration fits in n^k tape cells, because the machine cannot visit more cells than it has steps. Stack the configurations as rows to get an n^k × n^k grid called a tableau.
For each cell (i, j) and each symbol s (a tape symbol, or a state marking where the head is), create a variable x_{i,j,s}. It means "cell (i, j) holds s." The alphabet is fixed by N and does not grow with n, so there are O(n^{2k}) variables.
The Four Constraint Groups
| Subformula | What it forces | Size |
|---|---|---|
| φ_cell | Every cell holds exactly one symbol (`exactly_one` per cell) | O(n^{2k}) |
| φ_start | Row 1 is the start configuration on input x | O(n^k) |
| φ_accept | Some cell holds the accept state | O(n^{2k}) |
| φ_move | Every 2×3 window of cells matches a legal transition of N | O(n^{2k}) |
Then φx = φcell ∧ φstart ∧ φaccept ∧ φ_move.
The key idea is in φ_move. A Turing machine step changes the tape only near the head, so you can check whether row i+1 legally follows row i by looking at small fixed-size windows. There is a constant number of legal window contents, which depends only on N. Each window becomes a constant-size CNF constraint. Local checks are enough to enforce a global property, a legal computation, because the machine itself acts only locally.
Why Both Directions Hold
- If N accepts x, write down the accepting tableau. Setting each variable true exactly when its cell holds that symbol satisfies all four groups.
- If φx has a satisfying assignment, φcell lets you read off one symbol per cell, which gives a tableau. φstart and φmove make it a legal computation from x, and φ_accept makes it accepting.
The nondeterministic choices end up as the free variables of the formula. A SAT solver that "chooses" values is in effect choosing a computation branch.
Why It Runs in Polynomial Time
The formula has O(n^{2k}) clauses of constant size, and each clause follows a fixed pattern, so a simple program can print them all in polynomial time. The constant k comes from L's machine and does not depend on x.
Common Errors
- Reversing the direction. Cook-Levin reduces every NP problem to SAT. To show a new problem B is NP-hard, you reduce SAT (or any known NP-complete problem) to B. Reducing B to SAT only shows B is in NP.
- Forgetting membership in NP. NP-hardness alone does not give NP-completeness. Part one, the verifier, is required.
- Assuming SAT is hard on every instance. The theorem is about the worst case. Industrial SAT solvers routinely handle formulas with millions of variables, because real-world instances have structure.
- Treating the tableau as practical. The construction is a proof device. Real encodings into SAT are designed by hand and are far smaller.
Exercises
Exercise
How many clauses does exactlyone produce for m variables, and why does this keep φcell polynomial in the Cook-Levin construction?
Show answer
It produces 1 + m(m-1)/2 clauses: one "at least one" clause plus one clause for each pair. In the tableau, m is the size of N's fixed symbol set, a constant that does not depend on n. So each cell costs a constant number of clauses, and φ_cell has O(n^{2k}) clauses in total.
Exercise
A student shows that their new problem B reduces to SAT in polynomial time and concludes that B is NP-complete. What is wrong?
Show answer
The student used the wrong direction. A reduction from B to SAT shows only that B is no harder than SAT, which places B in NP. To show NP-hardness, they must reduce SAT (or another NP-complete problem) to B.
Exercise
Why does φ_move check 2×3 windows rather than comparing whole rows at once?
Show answer
A Turing machine step changes only the cell under the head and moves the head by one position. So a legal step can be verified locally. Each window has a constant number of allowed contents, so each check is a constant-size CNF constraint. Comparing whole rows directly would not give constant-size clauses.
Exercise
Run countmodels(exactlyone([1, 2, 3, 4]), 4). What does it print, and how many clauses does the formula have?
Show answer
It prints 4, since exactly one of the four variables can be true. The formula has 1 + 4·3/2 = 7 clauses.