Complexity Theory: P, NP, and NP-Completeness
P vs NP asks whether every problem whose solution can be verified quickly can also be solved quickly. P is the class of problems solvable in polynomial time; NP is the class of problems whose proposed solutions are verifiable in polynomial time. An NP-complete problem is a "hardest" problem in NP: if any one of them has a polynomial-time algorithm, then P = NP. The question was crystallized by Stephen Cook (1971) and Leonid Levin (1973); it remains the central open problem of theoretical computer science and one of the seven Clay Millennium Prize Problems.
Verification may be easier than discovery. Finding a solution to a hard problem (a satisfying assignment, a Hamiltonian cycle, a valid Sudoku fill) seems to require searching an exponential space, but checking a proposed solution is trivial. P vs NP asks whether this asymmetry is real or illusory. Reductions are the tool that ties problems together: if problem can be transformed into problem in polynomial time, then is "at least as hard" as . NP-completeness identifies the problems to which all of NP reduces — the apex of difficulty within NP.
Formal Statement
We work with decision problems: problems whose answer is "yes" or "no". A decision problem is formalized as a language over a finite alphabet (e.g. ); a string is in iff the answer for input is "yes". The computational model is the deterministic Turing machine (DTM) or its nondeterministic counterpart.
A language is in P (Polynomial time) if there exists a deterministic Turing machine and a constant such that decides and, on every input , halts within steps:
where is the input length, and is the set of languages decidable by a DTM in time.
Derivation hint: P is the countable union over all polynomial bounds ; closure under composition of polynomials makes the union robust to the choice of (reasonable) machine model.
A language is in NP (Nondeterministic Polynomial time) if there exists a polynomial-time deterministic "verifier" and a polynomial such that:
Here is the certificate (or witness), and runs in time polynomial in . Equivalently, , the class decidable by a nondeterministic TM in polynomial time.
Derivation hint: The two definitions coincide — a nondeterministic machine "guesses" the certificate (of polynomial length) and then deterministically verifies it; conversely a verifier's accepted certificate is exactly an accepting nondeterministic branch.
A language reduces to (written ) if there is a polynomial-time computable function such that:
The function is the reduction. Intuitively, an efficient algorithm for yields an efficient algorithm for via .
Derivation hint: Composition of a polynomial-time with a polynomial-time solver for is still polynomial time, since substituting one polynomial bound into another yields a polynomial; this is why preserves membership in P.
A language is NP-hard if every satisfies . A language is NP-complete if it is both NP-hard and a member of NP:
Derivation hint: NP-hardness places "at least as hard as everything in NP"; adding pins it to be "exactly the hardest in NP." NP-hard problems need not lie in NP (e.g., the halting problem is NP-hard but undecidable).
The Boolean satisfiability problem (SAT) — given a Boolean formula , does there exist an assignment of truth values making true? — is NP-complete. Consequently, the following are equivalent: 1. . 2. SAT P. 3. Some (equivalently, every) NP-complete problem is in P.
We must show (a) SAT NP and (b) every NP reduces to SAT.
Step 1 — SAT NP. Given formula , a certificate is a satisfying assignment . The verifier substitutes and evaluates , which takes time linear in . Thus SAT NP by [2].
Step 2 — Setup for hardness. Let NP, decided by a nondeterministic TM that on input runs in steps, where . The full execution of can be laid out in a tableau: a table of cells, row describing the configuration of at time (tape contents, head position, state).
Step 3 — Variables. Introduce Boolean variables meaning "cell holds symbol ." There are polynomially many variables since the tableau is polynomial in size.
Step 4 — Constraints as clauses. Build a formula :
- : each cell holds exactly one symbol.
- : row 0 encodes the initial configuration on input .
- : some row contains the accept state.
- : every "window" of adjacent cells is consistent with 's transition function. This is the key local-checkability step.
Step 5 — Correctness. is satisfiable iff there is a valid accepting computation of on , i.e. iff . By [3] this is a polynomial-time reduction , since each piece of has polynomial size and is constructible in polynomial time. Hence SAT is NP-hard, and with Step 1, NP-complete.
∎Stephen Arthur Cook, a logician at the University of Toronto, presented "The Complexity of Theorem-Proving Procedures" at the 1971 ACM STOC, defining NP-completeness and proving SAT complete. Independently, Leonid Levin (then in Moscow, later at Boston University) discovered essentially the same notion (his "universal search problems") around 1973. Cook received the 1982 Turing Award for founding computational complexity theory's central question. The pairing is honored in the name Cook–Levin theorem.
Building Intuition
Think of NP as the class of problems where a clever oracle could hand you a proof of a "yes" answer that you can check on the back of an envelope.
- A jigsaw puzzle: assembling it is hard, but if someone shows you the completed picture, verifying it's correct is easy. That's NP.
- A maze: finding the exit may take exponential search; checking a given path is trivial.
- NP-completeness is like discovering that every puzzle in the universe is secretly the same puzzle in disguise — solve one efficiently and you've solved all of them.
The word "nondeterministic" is a historical name; modern intuition prefers the certificate picture. NP is not "exponential time" — it sits between P and EXPTIME and we do not know exactly where. The deep mystery: searching feels exponential, but no one has proven it must be.
A useful mental model is energy landscapes: P problems are like convex bowls (roll downhill to the minimum), while NP-complete problems resemble rugged glassy landscapes with exponentially many local minima — finding the global optimum requires exploring exponentially many valleys, yet recognizing the bottom once you're there is instant. Statistical-mechanics methods (spin glasses, the replica method) have even predicted phase transitions in random SAT: as the clause-to-variable ratio crosses for random 3-SAT, instances flip sharply from almost-always-satisfiable to almost-always-unsatisfiable, with the hardest instances clustered at the threshold.
Worked Examples
Problem (SUBSET-SUM): Given integers and target , is there a subset summing to ?
Given: 5 numbers; the search space has subsets.
As a search: brute force checks up to 32 subsets.
As verification (NP): Suppose a certificate ... wait, . Try instead.
- Step 1: Check : yes, . ✓
- Step 2: Sum the elements: . ✗ — not 30.
- Try : . ✗
- Try ? wait . ✗ Try : actually , .... Let us test the true witness . By exhaustion the achievable sums near 30 are 31 (, ) and 29 (); no subset sums to exactly 30.
Verification cost: each check is one summation of numbers — time, independent of the search.
Given: the 3-CNF formula , with 2 clauses ().
Construction (the reduction ):
- Step 1: For each clause, make a triangle of 3 vertices, one per literal. Two triangles → 6 vertices.
- Step 2: Add edges within each triangle (so at most one literal per clause is chosen).
- Step 3: Add edges between any two vertices labeled by complementary literals (e.g. in clause 1 connects to in clause 2).
- Step 4: Set the target independent-set size .
Claim: is satisfiable the graph has an independent set of size .
Verification of equivalence: picking satisfies both clauses. Choose vertex from clause 1 and vertex from clause 2: not adjacent (different triangles, not complementary), so they form an independent set of size 2. ✓
Cost of : vertices and edges, built in polynomial time.
This shows INDEPENDENT-SET is NP-hard (since 3-SAT is NP-complete), and as it is also in NP, it is NP-complete.
Common Misconceptions
"NP means non-polynomial." False. NP stands for nondeterministic polynomial time. Every problem in P is also in NP (), because a P-algorithm verifies trivially by ignoring the certificate. Problems in NP have polynomial-size, polynomial-time-checkable certificates.
"NP-complete = unsolvable." False. NP-complete problems are decidable; we have algorithms (e.g. exhaustive search in time). They are merely believed not to have polynomial-time algorithms.
"If P ≠ NP, then no NP-complete instance can be solved fast." False. P vs NP is about worst-case asymptotic behavior. Modern SAT solvers handle industrial instances with millions of variables. Hardness is about families that force exponential blowup, not every instance.
"Proving one NP-complete problem hard proves P ≠ NP." Backwards — you'd need to prove a lower bound that no polynomial algorithm exists, which is exactly the open problem.
Not every hard-looking problem is NP-complete. Consider PRIMES (is prime?). For decades it sat in NP and co-NP, suspected hard, and one might guess it NP-complete. But in 2002 Agrawal, Kayal, and Saxena gave the AKS algorithm, a deterministic polynomial-time test, placing PRIMES firmly in P. So a problem can look like a candidate for NP-completeness yet actually be efficiently solvable. Likewise 2-SAT (clauses of 2 literals) is in P via implication-graph SCC analysis, even though its cousin 3-SAT is NP-complete — a sharp jump in complexity from 2 to 3 literals per clause.
Connections
- <span class="wiki-link-missing">Co-NP and the Polynomial Hierarchy</span>: co-NP is the class of problems whose "no"-answers have short certificates (e.g. TAUTOLOGY). Whether NP = co-NP is open and would follow from P = NP. Iterating quantifiers builds the polynomial hierarchy PH, which collapses if P = NP.
- <span class="wiki-link-missing">Space Complexity and PSPACE</span>: ; quantified Boolean formulas (TQBF) are PSPACE-complete, generalizing SAT.
- <span class="wiki-link-missing">Approximation Algorithms</span>: when exact solutions are NP-hard, we seek provably near-optimal answers; the PCP theorem governs which problems are hard to even approximate.
- <span class="wiki-link-missing">Randomized Algorithms and BPP</span>: the class BPP of efficient randomized computation relates to P; many conjecture P = BPP.
- <span class="wiki-link-missing">Reductions and Completeness</span>: the unifying machinery — completeness exists for many classes (P-complete, PSPACE-complete, #P-complete).
Real-World Applications
Historical Development
Subtleties & Limits
Does P = NP? Unresolved. Most researchers believe P ≠ NP, but no proof exists either way. Key obstacles to a proof:
- Relativization (Baker–Gill–Solovay, 1975): There exist oracles with but . So any proof technique that "relativizes" (works relative to all oracles) — like simple diagonalization — cannot settle P vs NP.
- Natural Proofs (Razborov–Rudich, 1994): Broad classes of circuit lower-bound arguments, if successful against strong pseudorandom generators, would themselves break cryptography — a barrier suggesting current combinatorial methods are insufficient.
- Algebrization (Aaronson–Wigderson, 2008): An extension of relativization showing algebraic techniques also fall short.
Ladner's theorem (1975): If P ≠ NP, there exist NP-intermediate problems — in NP, not in P, but not NP-complete. Candidates include graph isomorphism and factoring. So the world of NP is richer than a simple P / NP-complete dichotomy. Related open questions: Is NP = co-NP? Does the polynomial hierarchy collapse? Is graph isomorphism NP-complete (Babai's 2015 quasipolynomial algorithm suggests not)?
See Also
Reductions and Completeness · The Cook–Levin Theorem · Karp's 21 NP-Complete Problems · The PCP Theorem · Ladner's Theorem · Time Hierarchy Theorem · SAT Solvers · The Traveling Salesman Problem