Part VII · Selected Topics Chapter 34

NP-Completeness

The theory that lets you prove a problem is hard — not that you failed to find an algorithm, but that thousands of people have failed, and for a shared reason.

Chapter 1 mentioned NP-complete problems in passing. Chapter 34 makes the idea precise, and it is the most consequential piece of theory in the book. Its practical value is negative and enormous: it lets you stop looking. Proving a problem NP-complete means an efficient algorithm for it would give efficient algorithms for thousands of problems that have resisted fifty years of effort. That is not a proof of impossibility, but it is the strongest evidence available, and it redirects you from finding an exact fast algorithm to the alternatives in Chapter 35.

4th edition note. Renumbered from 34 to 34 within the reordered Part VII. Content is essentially unchanged: the complexity classes, reductions, Cook-Levin, and the standard chain of NP-completeness proofs.

Contents

  1. Decision problems and encodings
  2. P, NP, and NP-complete
  3. Verification is the key idea
  4. Reducibility
  5. How to prove NP-completeness
  6. Cook-Levin and the chain
  7. A catalogue of hard problems
  8. What to do about it
  9. Recap

Decision problems and encodings

The theory is built on decision problems — those with a yes/no answer — rather than optimisation problems, because the machinery is cleaner.

This is no loss of generality. Any optimisation problem has a decision version obtained by adding a bound: instead of “what is the shortest path?” ask “is there a path of length at most k?” If the decision version is hard, the optimisation version is at least as hard, since an optimiser would answer the decision question immediately. Hardness results for decision problems therefore carry over.

Formally a decision problem is a language: the set of binary strings encoding yes-instances. CLRS is careful about encodings, because a problem can change complexity if you encode it wastefully — the classic case being numbers written in unary rather than binary. A polynomial-time reduction between reasonable encodings preserves complexity, so any sensible encoding will do.

P, NP, and NP-complete

ClassDefinitionIntuition
PDecidable in polynomial timeYou can solve it fast
NPVerifiable in polynomial time given a certificateYou can check a proposed answer fast
NP-hardEvery problem in NP reduces to it in polynomial timeAt least as hard as everything in NP
NP-completeIn NP and NP-hardThe hardest problems in NP
P NP NP- complete NP-hard includes problems not even in NP drawn assuming P ≠ NP — if P = NP the green and blue regions coincide
Figure 34.1 — The class landscape. NP-complete problems sit at the far edge of NP; NP-hard extends beyond it to problems that are not even verifiable in polynomial time.
P ⊆ NP is obvious — if you can solve it fast you can verify it fast by ignoring the certificate and solving. Whether P = NP is the central open question in computer science, unresolved since 1971 and carrying a million-dollar Millennium Prize. Nearly everyone believes P ≠ NP, and nobody can prove it.

Verification is the key idea

NP is not “non-polynomial”. It stands for nondeterministic polynomial time, and the useful definition is about verification.

A problem is in NP if a yes-answer has a certificate — a short proof — that can be checked in polynomial time. You do not have to find the certificate quickly; you only have to recognise a correct one quickly.
ProblemCertificate for a yes-instanceVerification
Hamiltonian cycleThe cycle itselfCheck it visits each vertex once and every step is an edge: O(V)
3-CNF satisfiabilityA satisfying assignmentEvaluate every clause: O(n)
Subset sumThe subsetAdd it up: O(n)
Composite numberA non-trivial factorOne division
The asymmetry is deliberate. A certificate proves a yes. There is no requirement that no-answers have short proofs, and for these problems nobody knows short proofs of no. That asymmetry is captured by the separate class co-NP, and whether NP = co-NP is another open question.

Reducibility

Language L₁ is polynomial-time reducible to L₂, written L₁ ≤ᴵ L₂, if there is a polynomial-time computable function f such that for every input x: x ∈ L₁ if and only if f(x) ∈ L₂. Informally: L₂ is at least as hard as L₁, because a solver for L₂ plus the translation gives a solver for L₁.
Lemma 34.3: If L₁ ≤ᴵ L₂ and L₂ ∈ P, then L₁ ∈ P. Contrapositive: if L₁ is hard, then L₂ is hard too.
The direction of the reduction is the thing people get backwards. To prove that your problem Y is hard, reduce a known hard problem X to it: X ≤ᴵ Y. Reducing Y to something known hard proves nothing about Y. Mnemonic: reduce FROM a known-hard problem TO your problem. If you can turn any instance of a hard problem into an instance of yours, yours must be at least as hard.

How to prove NP-completeness

To show that problem Y is NP-complete: 1. Show Y ∈ NP. Exhibit a certificate and a polynomial-time verifier. Usually easy. 2. Choose a known NP-complete problem X. Pick one structurally similar to Y — this is where the skill is. 3. Construct a polynomial-time reduction f from X to Y. Describe how to build an instance of Y from any instance of X. 4. Prove f is correct, in both directions: x is a yes-instance of X ⇒ f(x) is a yes-instance of Y f(x) is a yes-instance of Y ⇒ x is a yes-instance of X 5. Verify f runs in polynomial time.
Step 4 is where proofs fail. The forward direction is usually straightforward; the reverse — showing that a solution to the constructed instance must correspond to a solution of the original — is where the construction has to be tight. A reduction that leaks, allowing solutions to f(x) that do not encode solutions to x, is wrong.

Cook-Levin and the chain

Every reduction needs a known-hard problem to start from. The first one had to be proved from the definition itself.

Theorem 34.9 (Cook-Levin). CIRCUIT-SAT is NP-complete. The proof shows that any polynomial-time verifier for any NP problem can be encoded as a boolean circuit that is satisfiable exactly when a valid certificate exists. It reduces every problem in NP at once, by construction rather than by comparison, and it is the foundation the entire theory stands on.

With one anchor established, everything else follows by chaining reductions:

CIRCUIT-SAT SAT 3-CNF-SAT CLIQUE VERTEX-COVER SUBSET-SUM HAM-CYCLE TSP PARTITION Cook-Levin proves the red anchor from first principles; every arrow after it is one reduction.
Figure 34.2 — The classic chain. Each arrow X → Y is a proof that X ≤ᴵ Y, so Y inherits hardness from X.

CLRS proves each link. Two worth remembering: CLIQUE ≤ᴵ VERTEX-COVER is almost trivial once you notice a clique in G is exactly an independent set in the complement of G; and HAM-CYCLE ≤ᴵ TSP is the simplest reduction in the chapter — give existing edges weight 0 and missing edges weight 1, then ask for a tour of total weight 0.

A catalogue of hard problems

NP-completeIn P — the near neighbourThe difference
Hamiltonian cycle: visit every vertex onceEuler tour: use every edge onceVertices instead of edges
Longest simple pathShortest pathOne word
3-CNF-SAT2-CNF-SATOne literal per clause
0-1 knapsackFractional knapsackWhether items can be split
Graph 3-colouringGraph 2-colouring (bipartiteness)One colour
Vertex coverBipartite vertex coverRestricting the graph class
Integer linear programmingLinear programmingIntegrality
Subset sum, TSP, clique, set cover
Read the middle column. The boundary between tractable and intractable is razor-thin, and it does not follow intuition. This is the single most practically useful takeaway from the chapter: when a problem resembles something easy, that resemblance is not evidence that it is easy.

What to do about it

Proving your problem NP-complete is not the end of the engineering; it is the start of a different phase.

StrategyGive up onKeepWhere
ApproximationOptimalityA proven bound on how far off you areChapter 35
Special casesGeneralityExactness on the inputs you actually gete.g. trees, planar graphs, bounded width
HeuristicsGuaranteesGood behaviour on real dataSimulated annealing, genetic algorithms
Exponential but smartPolynomial timeExactness, with aggressive pruningBranch-and-bound, SAT and ILP solvers
ParameterisedPolynomial in everythingf(k)·nO(1) for a small parameter kFixed-parameter tractability
NP-complete does not mean unsolvable in practice. Modern SAT solvers routinely dispatch industrial instances with millions of variables, and commercial ILP solvers handle enormous scheduling and routing problems daily. Worst-case hardness is a statement about the worst input, and real inputs have structure that solvers exploit. The theorem tells you no algorithm is fast on everything; it does not tell you your instance is hard.

Recap

The eight things to carry forward

Where this goes next

Chapter 35, the last, takes the first of those strategies seriously. Approximation algorithms run in polynomial time and return a solution provably within a bounded factor of optimal — turning “this problem is hard” into “here is a solution, and here is exactly how far off it can be”.


Ch 33 — Machine-Learning Algorithms Ch 35 — Approximation Algorithms