P, NP, and Reductions
Most problems we have met so far have fast algorithms. A vast and important family seemingly does not.
╌╌╌╌
Until now this course has been a catalog of triumphs: sorting in , shortest paths in near-linear time, spanning trees almost for free. It is tempting to believe that every problem yields to a clever enough algorithm. It does not. There is a large, eminently practical family of problems (scheduling, routing, packing, constraint satisfaction) for which, after more than half a century of effort, nobody has found an algorithm that is fast on every instance, and for which we have strong reasons to suspect none exists.
The theory of intractability is how we make that suspicion precise. Its central insight, due to Cook, Levin, and Karp, is that thousands of these stubborn problems are really the same problem in disguise. Solve any one of them quickly and you solve them all; prove any one of them hard and you have proved them all hard. This lesson assembles the three ideas needed to state that claim: decision problems, the classes and , and reductions.
Decision problems
To classify difficulty cleanly we restrict attention to problems with a yes-or-no answer. A decision problem asks, of each input, a single question whose answer is yes or no.
This seems like a severe restriction; surely we usually want to find a
shortest tour, not merely learn whether a short one exists. But the two are
rarely far apart. The optimization problem find the cheapest tour
has a
decision twin: is there a tour of cost at most ?
If we can answer the
decision question quickly for every , a binary search over pins down
the optimal cost, and a little more work recovers the tour itself. Decision
problems lose almost nothing and gain a clean theory, so they are the objects
we classify.
A word on encoding and size. An input is a string of bits; the size
of an instance is the length of that string. We always assume a reasonable
encoding (integers in binary, graphs as adjacency lists) because an
artificially bloated encoding (say, integers in unary) could make a slow
algorithm look fast. With reasonable encodings fixed, polynomial in the input size
is a stable, machine-independent notion.
The class P
Some decision problems have algorithms whose running time is bounded by a polynomial in the input size. These are the problems we regard as tractable.
Why polynomial, and not, say, or better
? Because polynomials are
closed under the operations we constantly perform: addition,
multiplication, and especially composition. If a polynomial-time algorithm
calls a polynomial-time subroutine a polynomial number of times, the whole
thing is still polynomial. This closure is what makes stable: it
does not depend on the particular machine model, the programming language, or
whether we count as fast.
Everything we have called efficient so far
(sorting, shortest paths, matching, linear programming) lives in .
The class is our formal stand-in for feasible.
1
The class NP
Now consider a problem like Hamiltonian Cycle: given a graph , is there a cycle that visits every vertex exactly once? We know of no polynomial-time algorithm to decide this. But notice an asymmetry. If a benevolent oracle hands us a cycle and claims it is Hamiltonian, we can check the claim in linear time: walk the cycle, confirm it visits each vertex once and uses real edges. Finding the cycle seems hard; verifying a proposed cycle is easy.
This is the defining feature of the class : a problem is in if every yes-instance has a short proof, a certificate, that can be checked quickly.
The name stands for nondeterministic polynomial time: one can equivalently picture a machine that guesses the certificate and then verifies it. The verifier definition avoids speaking of magical guessing; it asks only that correct yes-answers come with checkable evidence.2
Three points deserve emphasis.
- The certificate must be short (polynomial length) and the check must be fast (polynomial time). For Hamiltonian Cycle the certificate is the cycle; for SAT (is a boolean formula satisfiable?) it is a satisfying assignment; for it is the subset that hits the target.
- The asymmetry between yes and no is real. guarantees a
certificate only for yes-instances. The problem of certifying a no
(
there is no Hamiltonian cycle
) need not be in ; that belongs to a companion class, . - Every problem in is in . If we can solve a problem in polynomial time, we can ignore any offered certificate and just decide the answer directly; an empty certificate suffices. So .
Reductions: comparing difficulty
We want to say problem is at least as hard as problem
without knowing
how hard either one actually is. The device that makes this possible is a
reduction: a way to convert any instance of into an instance of
that has the same answer, so that an algorithm for becomes an algorithm for
.
Read as is no harder than .
3 Picture this as a
transform–solve–transform pipeline: take the input, transform it into an
input for the other problem, hand that to a solver for , then transform
the solver's output back into an answer for . The solver for is used as a
black-box subroutine, and we never look inside it.
The dashed box is the punchline: a fast solver for , wrapped in the fast transformers on either side, is a fast solver for . For a decision problem the right-hand transformer is trivial (pass the yes/no answer through unchanged), and the pipeline collapses to the textbook definition above: apply , ask the -question, report the answer. For a search problem (the warm-up below) the right-hand transformer does real work, turning 's witness back into a witness for . Either way, the diagram encodes the two ways every reduction is used, which are mirror images of each other.
- Upper bound (good news flows forward). If and , then . Tractability of the harder problem drags the easier one along: run , then the fast solver for ; the composition of two polynomials is a polynomial.
- Lower bound (bad news flows backward). If and is known to be hard, then is hard too. For if had a fast algorithm, the pipeline above would give one as well, a contradiction.
It is the second direction that powers the theory of intractability. To show a new problem is hard, we reduce a known-hard problem to it. The direction is a notorious source of error: we reduce from the hard problem to the new one. Getting the arrow backwards proves nothing.
Two structural facts make behave like an ordering of difficulty.
- Reflexivity is trivial and, crucially, transitivity holds: if and , then , since we just compose the two
translators, and a polynomial of a polynomial is still a polynomial. Chains
of reductions are the raw material of the next lesson's
reduction web.
A warm-up reduction: matching reduces to flow
Reductions are not a tool reserved for hardness arguments; we have already been using them to design algorithms. The cleanest example, motivating the whole idea, is bipartite matching. We are given a set of tasks , a set of workers , and a compatibility table, where is true when task can be done by worker . A matching assigns tasks to workers so that no task and no worker is used twice; we want a matching of maximum cardinality. There is a slogan worth remembering here:
We do not write a matching algorithm from scratch. Instead we reduce to , a problem we already know how to solve, and reuse that solver. This is the pipeline of the previous section made concrete, and because matching is a search problem, the output transformer is no longer trivial.
Transform the input. Build a flow network: add a source and a sink . Direct an edge for every task, an edge for every worker, and an edge whenever is true. Give every edge capacity . The unit capacities out of and into are exactly the budget constraints: each task and each worker can carry at most one unit.
Solve with the black box. Run any max-flow algorithm (Ford–Fulkerson or Edmonds–Karp) and let be the maximum flow. The value is the size of the largest matching.
Transform the output. A flow value is just a number; the matching is recovered by reading off which compatibility edges carry one unit of flow. Those edges are the assignment.
Two claims make the reduction correct, and it's worth separating them:
- Easy direction. If some tasks can be assigned, then pushing one unit along each chosen path gives a flow of value . So the max flow is at least as large as the best matching.
- Less easy direction. If the max flow value is , then tasks can be assigned. This needs the integrality theorem: when every edge capacity is an integer, some maximum flow is integral (every ) and Ford–Fulkerson actually finds such a flow. With unit capacities, integral means each edge carries or , and an integral flow decomposes into a collection of edge-disjoint – path flows. Each path uses one task and one worker, so the paths are a matching of size .
Both directions together give (size of maximum matching), so the reduction is valid. The transform–solve–transform diagram now does real work: build the network (), call the flow solver (), decompose the integral flow into paths (the output transformer). We obtained a matching algorithm without writing one, purely because .
A small reduction between decision problems
The matching reduction reused an easy solver to solve another easy problem. The reductions that drive intractability go the other way, relating two decision problems with no known fast solver, but the mechanism is identical. Here is a textbook example. asks: given a graph and integer , is there a set of vertices no two of which are adjacent? Clique asks: is there a set of vertices that are all pairwise adjacent? These are the same question asked of complementary graphs.
Given an instance of , build the complement graph on the same vertices, where is an edge of exactly when it is not an edge of . A set is independent in (no edges inside it) precisely when is a clique in (all edges inside it). So Constructing takes time polynomial in the size of , so this is a valid reduction , and, since complementation is its own inverse, the reverse reduction holds too. The two problems are equivalent in difficulty: a fast algorithm for either yields one for the other.4
The P versus NP question
We now have two classes with . Problems in can be solved quickly; problems in can at least have their solutions checked quickly. The defining open question of theoretical computer science is whether checking is genuinely easier than solving.
Almost everyone believes the answer is no: that , and that finding a needle is fundamentally harder than recognizing one once found. But no proof is known in either direction, and the question carries a million-dollar Clay Millennium Prize.5 What makes it more than a curiosity is the next lesson's discovery: there are problems in , the -complete problems, that are universally hardest. Every problem in reduces to each of them. A polynomial-time algorithm for even one would collapse and into a single class; a proof that even one requires super-polynomial time would settle the question the other way.
The sketch shows the conjectured world: and the -complete problems sit as disjoint regions inside . If , all three regions coincide and the entire picture collapses to a single blob. We do not know which world we live in, but we will see that the two regions, if separate, can never overlap.
Takeaways
- We study decision problems (yes/no answers); optimization problems reduce to them by binary search, so little is lost.
- is the class of problems solvable in polynomial time; is the class of problems whose yes-answers admit a short, polynomial-time checkable certificate. Always .
- A polynomial-time reduction is a transform–solve–transform
pipeline: transform the input, call a black-box solver for , transform its
output back. It means
is no harder than
: tractability flows forward ( easy easy), hardness flows backward ( hard hard). To prove hard, reduce a known hard problem to. - Reductions also build algorithms: solves matching by routing it through a flow solver, with the integrality theorem guaranteeing the flow decomposes back into a matching.
- Reductions compose, so chains, building a web of equivalent difficulty.
- Whether , that is, whether checking is as easy as solving, is the central open question, and the hardest problems in hold the key.
Footnotes
- CLRS, Ch. 34 — NP-Completeness (§34.1): the class of polynomial-time-solvable decision problems and its closure-based robustness. ↩
- CLRS, Ch. 34 — NP-Completeness (§34.2): the class defined via a polynomial-time verifier and short certificates. ↩
- CLRS, Ch. 34 — NP-Completeness (§34.3): polynomial-time reductions as a measure of relative difficulty. ↩
- Erickson, Ch. 12 — NP-Hardness: the Independent-Set / Clique equivalence via graph complementation. ↩
- Skiena, §11 — NP-Completeness: the question and the belief that verifying is easier than solving. ↩
╌╌ END ╌╌