Applied and computational mathematics
Computational complexity: P and NP
Classifies problems by how the time to solve them grows, centered on whether P equals NP.
IntuitionWhy some puzzles are easy to check but hard to solve
Solving a Sudoku puzzle from scratch can take a long time, trying possibility after possibility. But if a friend hands you a completed grid and claims it solves the puzzle, checking their claim is fast: just scan every row, column, and box for repeats. Sorting a shuffled deck of cards is different — it is fast to solve (a few passes of any sorting method) and just as fast to check. Computational complexity theory makes this everyday distinction — "easy to solve" versus merely "easy to check a solution" — mathematically precise, and asks whether that distinction is real or illusory.
SchoolMeasuring running time: polynomial versus exponential growth
An algorithm's running time is usually measured as a function of the input size , using big-O notation: means the running time grows no faster than a constant multiple of once is large. Searching an unsorted list of items one by one takes steps; binary search on a sorted list of items takes only steps. Both are polynomial (in fact sub-linear or linear) in . Trying every subset of items, by contrast, takes steps — exponential in , and vastly slower once grows past a few dozen.
| Growth rate | |||
|---|---|---|---|
UndergraduateThe classes P and NP
Definition: The class P
P (polynomial time) is the set of yes/no problems that a standard computer can solve in time bounded by some polynomial in the input size — that is, in time for some fixed constant . Sorting, primality testing, and finding shortest paths in a graph are all in P.
Definition: The class NP
NP (nondeterministic polynomial time) is the set of yes/no problems for which a proposed "yes" answer comes with a certificate (a witness, like the Hamiltonian cycle above or a satisfying assignment to a formula) that can be checked for correctness in polynomial time, even though no polynomial-time way to find such a certificate is known in general. Every problem in P is also in NP (if you can solve it quickly, checking is trivially quick too), so ; whether the reverse containment holds is exactly the P versus NP question.
Formally, a language is in NP if there is a polynomial and a polynomial-time verifier such that if and only if there exists a certificate with and accepts. This verifier-based definition is equivalent to the more common "nondeterministic Turing machine" definition, and is usually easier to reason about in practice.
UndergraduatePolynomial-time reductions and NP-completeness
Definition: Polynomial-time reduction
Problem reduces to problem in polynomial time, written , if there is a function , computable in polynomial time, that transforms any instance of into an instance of such that is a yes-instance of if and only if is a yes-instance of . Intuitively, means " is at least as hard as ": a fast algorithm for immediately gives a fast algorithm for , by translating and calling it.
Definition: NP-complete
A problem is NP-complete if and every problem satisfies . NP-complete problems are the "hardest" problems in NP: an efficient algorithm for any one of them would translate, via reductions, into an efficient algorithm for every problem in NP.
The Boolean satisfiability problem (SAT) — given a Boolean formula in variables , decide whether some assignment of true/false values makes it evaluate to true — is NP-complete.
Why is it true?
SAT is clearly in NP: a satisfying assignment is a certificate that can be checked in polynomial time by simply plugging in the values. The deep part is showing every NP problem reduces to SAT — this works because a Boolean formula is expressive enough to describe, step by step, the entire run of any polynomial-time verifier, cell by cell and moment by moment, so "does a certificate exist" becomes "is this giant formula satisfiable".
Proof
Let with polynomial-time verifier running in time at most on inputs of length paired with a certificate of length at most . Fix an input of length ; we build a Boolean formula , satisfiable exactly when some certificate makes accept.
Imagine the entire computation of on , for an unknown certificate , laid out as a tableau: an grid where row records the verifier's full tape contents and head position at time step . Introduce a Boolean variable for every (cell, time-step, possible symbol) triple, recording what symbol sits in that cell at that time — since the grid has polynomially many cells and time steps, this is a polynomial number of variables.
The formula is built as a conjunction (AND) of clauses enforcing four local, easily-checkable conditions: (1) every cell holds exactly one symbol at each time step; (2) row correctly encodes the fixed input followed by a blank placeholder for the yet-to-be-guessed certificate ; (3) each small window of adjacent cells across two consecutive time steps is consistent with 's transition rules (this is where the certificate bits, left free in row , get to influence the rest of the computation); (4) some cell at the final time step records the accept state.
Each of these conditions only constrains a small, fixed-size neighborhood of variables, so each translates into a constant number of clauses, and there are only polynomially many neighborhoods to constrain — so has polynomial size and is computable from in polynomial time. By construction, is satisfiable exactly when there is a consistent tableau, i.e. exactly when some certificate makes accept, i.e. exactly when . This exhibits the reduction for an arbitrary , and combined with shown above, proves SAT is NP-complete.
If is NP-complete and , then .
Why is it true?
NP-completeness of means every NP problem can be rewritten, in polynomial time, as an instance of . If itself can then be solved in polynomial time, chaining the rewriting step and the solving step together solves the original NP problem in polynomial time too — so a single tractable NP-complete problem drags every NP problem down into P with it.
Proof
Suppose is NP-complete and there is an algorithm solving in time . Take any ; by NP-completeness of , , meaning there is a reduction function computable in time that maps instances of to instances of while preserving yes/no answers.
On an input of length for , first compute : this takes time , and in particular the output has length at most (a polynomial-time algorithm cannot write more output than time it runs). Then run the polynomial-time algorithm for on : since , this takes time .
The total running time is , still a polynomial in (the composition of two polynomials is a polynomial). By the correctness of the reduction, is a yes-instance of if and only if is a yes-instance of , so this combined procedure correctly decides in polynomial time.
Since was arbitrary, every NP problem has a polynomial-time algorithm, i.e. . Combined with the always-true containment , this gives .
| Problem | Known status |
|---|---|
| Sorting a list | In P: comparisons suffice |
| Primality testing | In P since 2002 (AKS algorithm) |
| Boolean satisfiability (3-SAT) | NP-complete (Cook–Levin, 1971) |
| Traveling salesman (decision version) | NP-complete |
| Graph isomorphism | In NP; quasi-polynomial algorithm since 2015 (Babai); not known to be in P or NP-complete |
| Integer factoring | In NP and co-NP; not known to be in P — hardness assumption behind RSA |
UndergraduateReal-World Applications and Worked Examples
Recognizing that a problem is NP-complete has immediate practical value: it tells engineers to stop searching for an exact, always-fast algorithm and instead reach for heuristics, approximation algorithms, or special-case structure. It also underlies modern cryptography (RSA's security rests on factoring being hard), logistics and scheduling (vehicle routing, exam timetabling), bioinformatics (protein structure prediction, sequence alignment variants), and compiler optimization (register allocation is graph coloring, which is NP-complete).
Example: Turning a vertex cover into an independent set
Consider the path graph on vertices with edges . Using the fact that a graph with vertices has a vertex cover of size if and only if it has an independent set of size , and given that is a maximum independent set of this graph, find the minimum vertex cover size.
Solution
First verify really is independent: none of the edges has both endpoints in , so no two chosen vertices are adjacent, confirming it is a valid independent set of size ; it is maximum since a -vertex path cannot have pairwise non-adjacent vertices (some two would have to be adjacent along the single path).
Apply the reduction formula with and independent set size : minimum vertex cover size .
Verify directly: the complement set should be a vertex cover. Check every edge has at least one endpoint in : edge touches ; edge touches ; edge touches ; edge touches . All four edges are covered by just vertices, confirming the answer.
This equivalence between vertex cover and independent set is exactly the kind of polynomial-time reduction (in fact, here a very simple one, computable in linear time and reversible) discussed above: both Vertex Cover and Independent Set are NP-complete decision problems, and this reduction shows they are, in a precise sense, the same problem viewed from two different angles.
Example: Why brute force fails even at modest size
A brute-force algorithm for a satisfiability problem with Boolean variables checks all possible truth assignments, spending about seconds (one nanosecond) per assignment on a fast computer. Estimate, to the nearest power of ten, how many seconds this brute-force search takes for variables.
Solution
The number of assignments to check is . Since , we get ; more precisely .
Multiplying by the per-assignment cost of seconds gives a total time of roughly seconds.
Rounded to the nearest power of ten, this is about seconds — roughly to days of continuous computation, just for variables, a size that would be considered small in real applications (industrial SAT instances routinely have thousands or millions of variables).
This is exactly why the distinction between polynomial time and exponential time matters in practice, not just in theory: a hypothetical polynomial algorithm running in, say, steps would need only steps — a fraction of a millisecond — showing why the P versus NP question has such enormous practical stakes.
For , which quantity is larger: or ?
What does "NP" actually stand for?
According to the Cook–Levin theorem, which problem was the first ever proven to be NP-complete?
If someone discovered a polynomial-time algorithm for a single NP-complete problem such as SAT, what would follow?
References
- Stephen A. Cook (1971). The Complexity of Theorem-Proving Procedures · DOI:10.1145/800157.805047
- Michael Sipser (2012). Introduction to the Theory of Computation
- Michael R. Garey, David S. Johnson (1979). Computers and Intractability: A Guide to the Theory of NP-Completeness
- Clay Mathematics Institute (2000). P vs NP Problem