Theory
P vs NP
Is verifying a solution as easy as finding one?
P vs NP asks whether every problem whose solution can be verified in polynomial time can also be solved in polynomial time. P is the class of problems a computer can solve quickly (in time bounded by n^k); NP is the class of problems whose proposed answers can be checked quickly. Since Stephen Cook's 1971 theorem introduced NP-completeness, this question has been the single biggest open problem in computer science — one of the seven Clay Millennium Prize Problems, worth $1,000,000. Almost everyone believes P ≠ NP, but no one has proved it.
- StatusOpen since 1971
- PSolvable in poly time — O(n^k)
- NPVerifiable in poly time given a certificate
- First NP-complete problemBoolean SAT (Cook–Levin, 1971)
- Prize$1,000,000 Clay Millennium
- Consensus≈ 80% of theorists expect P ≠ NP
Interactive visualization
Press play, or step through manually. The visualization is yours to drive — try it before reading on.
Watch the 60-second explainer
A condensed visual walkthrough — narrated, captioned, under a minute.
Why P vs NP matters
Almost every hard problem worth solving — routing trucks, packing containers, scheduling flights, folding proteins, verifying chips, cracking codes — shares a curious asymmetry. If someone hands you a candidate answer, checking it is fast. Finding that answer from scratch seems to require searching an exponential haystack. P vs NP asks whether that gap is real or an illusion born of our own ignorance.
Formally, P (deterministic polynomial time) is the set of decision problems solvable by a Turing machine in time O(n^k) for some fixed constant k — sorting, shortest paths, primality testing (Agrawal–Kayal–Saxena, 2002), linear programming. NP (nondeterministic polynomial time) is the set of decision problems whose "yes" instances have a certificate — a short witness — that a deterministic verifier can check in polynomial time. Sudoku is the canonical intuition: verifying a filled grid is trivial; filling a blank one on an n×n board is not known to be easy.
The stakes are enormous. If P = NP (with a practical algorithm), then optimal logistics, automated mathematical proof, drug discovery, and machine learning all become tractable — and essentially all public-key cryptography collapses. If P ≠ NP, we get a formal guarantee that some problems are inherently intractable, cementing the security assumptions the modern internet runs on. Either way, the answer reshapes what "computable in practice" means.
P, NP, and verification, step by step
The cleanest way to see NP is through the verifier definition. A language L is in NP if there is a polynomial-time verifier V(x, c) and a polynomial p such that:
- If
x ∈ L, there exists a certificatecwith|c| ≤ p(|x|)andV(x, c) = 1. - If
x ∉ L, thenV(x, c) = 0for every candidate certificatec.
Concretely, for the subset-sum problem "is there a subset of these integers summing to zero?", the certificate is the subset itself; the verifier adds it up in O(n) time. For Hamiltonian cycle, the certificate is the ordering of vertices; the verifier checks each consecutive pair is an edge. The magic is entirely in the guess — the verification is boring and fast.
The equivalent nondeterministic-machine definition says a nondeterministic Turing machine may branch into many parallel guesses at each step and accepts if any branch accepts. Such a machine "guesses" the certificate and then verifies it. P is what the deterministic version can do; NP is what the guessing version can do. So P ⊆ NP is immediate: a machine that never needs to guess is a special case of one that can.
The whole difficulty is the other direction. Does allowing free guesses actually buy exponential power, or can a clever deterministic algorithm always simulate the guessing efficiently? That is P vs NP in one sentence.
NP-complete: the hardest problems in NP
A problem B is NP-hard if every problem in NP reduces to it in polynomial time. It is NP-complete if it is both NP-hard and a member of NP. NP-complete problems are the load-bearing wall of the theory: if any one of them has a polynomial-time algorithm, then all of NP does, and P = NP.
The founding result is the Cook–Levin theorem (Stephen Cook, 1971; independently Leonid Levin, 1973): Boolean satisfiability (SAT) is NP-complete. Cook showed how to encode the entire computation of an arbitrary polynomial-time nondeterministic Turing machine as a Boolean formula that is satisfiable exactly when the machine accepts. That single reduction plants the flag; every subsequent NP-completeness proof stands on it.
In 1972, Richard Karp published "Reducibility Among Combinatorial Problems," reducing SAT to 21 classic problems — clique, vertex cover, Hamiltonian cycle, 3-coloring, knapsack, and more — establishing them all as NP-complete. Today the list runs into the thousands. Crucially, they are all the "same" problem in disguise: a fast algorithm for any one solves them all.
How reductions spread NP-completeness
To prove a new problem X is NP-complete you do exactly two things:
- Show
X ∈ NP— exhibit a polynomial-size certificate and a polynomial-time verifier. - Show
Xis NP-hard — pick a known NP-complete problemY(usually 3-SAT) and give a polynomial-time many-one reductionY ≤ₚ X: a functionfmapping each instanceyto an instancef(y)ofXsuch thatyis a "yes" ifff(y)is a "yes".
The direction matters and trips people up constantly. To prove X is hard, you reduce a hard problem to X — you must not reduce X to something hard. Because ≤ₚ is transitive and every NP problem reduces to Y, chaining (\text{any NP}) ≤ₚ Y ≤ₚ X shows every NP problem reduces to X.
P vs NP vs the neighboring classes
| Class | Informal meaning | Solve time (det.) | Verify a "yes" | Example |
|---|---|---|---|---|
| P | Solvable quickly | Polynomial | Polynomial | Shortest path, primality |
| NP | "Yes" verifiable quickly | Exponential (best known) | Polynomial (with certificate) | SAT, Hamiltonian cycle |
| co-NP | "No" verifiable quickly | Exponential (best known) | Poly for "no" instances | Tautology, non-3-colorability |
| NP-complete | Hardest in NP | Exponential (best known) | Polynomial | 3-SAT, TSP (decision) |
| NP-hard | At least as hard as NP | ≥ NP; may be undecidable | Not necessarily in NP | Halting problem, TSP (optimization) |
| PSPACE | Solvable in poly space | Contains NP and co-NP | — | QBF, generalized geography |
The known containments are P ⊆ NP ⊆ PSPACE ⊆ EXPTIME, and P ⊆ co-NP ⊆ PSPACE. We know P ≠ EXPTIME (by the time hierarchy theorem), so at least one of the inner containments is strict — but we cannot prove which. Whether NP = co-NP is a separate open question; a proof that they differ would immediately imply P ≠ NP.
How to verify an NP certificate in Python
The defining feature of NP is not solving but checking. Here is a polynomial-time verifier for 3-SAT: given a formula and a proposed assignment (the certificate), confirm every clause is satisfied. This is the boring, fast half of the P vs NP asymmetry.
# A 3-CNF formula: list of clauses, each clause a list of literals.
# A positive int v means variable v is true; -v means variable v is false.
formula = [[1, -2, 3], [-1, 2, 4], [-3, -4, 2], [1, 3, -4]]
def verify_sat(formula, assignment):
"""assignment[v] is True/False. Runs in O(#clauses * clause_width)."""
for clause in formula:
satisfied = False
for lit in clause:
v = abs(lit)
want_true = lit > 0
if assignment[v] == want_true: # this literal is satisfied
satisfied = True
break
if not satisfied: # a whole clause failed
return False
return True
# The certificate — a proposed satisfying assignment.
certificate = {1: True, 2: True, 3: False, 4: False}
print(verify_sat(formula, certificate)) # -> True, in linear time
# There are 2**n possible certificates. Verifying one is O(n);
# FINDING a satisfying one is the NP-hard part — no poly-time
# algorithm is known, and one would prove P = NP.
Notice the shape of the argument: the verifier is unambiguously polynomial, but the space of certificates is 2^n. A deterministic solver that tried them all would take exponential time. The million-dollar question is whether some fundamentally cleverer solver avoids that search.
If P = NP, cryptography breaks
Modern public-key cryptography is, in effect, a bet that P ≠ NP. Consider RSA: verifying that a number d is the correct private exponent is easy, and recovering the private key reduces to a problem whose solution is checkable in polynomial time — so key recovery lives in (or very near) NP. A general polynomial-time NP solver would let an attacker recover keys about as fast as legitimate parties verify them. The same logic dissolves the hardness assumptions behind discrete-log, lattice, and code-based schemes to varying degrees.
Two important caveats. First, the exponent matters: an O(n^{100}) algorithm would prove P = NP yet leave real-world crypto essentially untouched, while an O(n^2) algorithm would be apocalyptic. Second, symmetric primitives like AES and hash functions like SHA-256 do not immediately fall, because their security rests on structural and statistical assumptions rather than a clean NP-hardness reduction — though confidence in them would certainly erode. The contrapositive is quieter: proving P ≠ NP would not automatically prove any particular cipher secure; cryptography needs average-case hardness, and NP-completeness is a worst-case notion.
Common misconceptions and pitfalls
- "NP means non-polynomial." No — NP is nondeterministic polynomial time. Every problem in P is also in NP. The class of provably super-polynomial problems is different (e.g., EXPTIME-complete problems).
- "NP-complete problems are unsolvable." They are perfectly solvable — just not known to be solvable quickly. SAT solvers routinely crack instances with millions of variables; the exponential blow-up is a worst-case, not a universal, phenomenon.
- "NP-hard = NP-complete." NP-hard means "at least as hard as everything in NP" and need not be in NP at all — the halting problem is NP-hard but undecidable. NP-complete adds the requirement of membership in NP.
- Reducing in the wrong direction. To prove
Xhard, reduce a known-hard problem toX, not the reverse. Getting the arrow backwards proves nothing. - Confusing optimization with decision. P vs NP is stated for decision problems (yes/no). "Find the shortest tour" is an optimization problem; "is there a tour of length ≤ k?" is its decision version. NP-completeness is defined on the decision form.
- Assuming a proof is around the corner. Relativization (Baker–Gill–Solovay, 1975), natural proofs (Razborov–Rudich, 1994), and algebrization (Aaronson–Wigderson, 2008) each rule out broad classes of techniques, which is why fifty-plus years of effort have not cracked it.
A brief history
The informal question predates the theory: in a now-famous 1956 letter, Kurt Gödel asked John von Neumann whether a certain search problem could be solved in time linear or quadratic in the proof length — essentially P vs NP avant la lettre. The formal framework arrived with Stephen Cook's 1971 paper "The Complexity of Theorem-Proving Procedures," which defined NP-completeness and proved SAT complete. Leonid Levin reached equivalent results independently in the Soviet Union (published 1973), which is why the theorem bears both names.
Richard Karp's 1972 list of 21 NP-complete problems showed the phenomenon was pervasive, not a curiosity. In 2000 the Clay Mathematics Institute named P vs NP one of seven Millennium Prize Problems, attaching a $1,000,000 reward to a correct resolution. A widely cited 2019 poll of complexity theorists found roughly 80% expect the answer to be P ≠ NP — but expectation is not proof, and the prize remains unclaimed.
Frequently asked questions
What is the difference between P and NP?
P is the class of decision problems a deterministic Turing machine can solve in polynomial time — time bounded by n^k for some constant k. NP is the class of decision problems whose 'yes' answers can be verified in polynomial time given a certificate (a proposed solution). Every problem in P is trivially in NP: if you can solve it fast, you can verify it fast by ignoring the certificate and re-solving. The open question is whether the reverse holds — whether efficient verification always implies efficient solving.
What does NP stand for?
NP stands for 'nondeterministic polynomial time' — not 'non-polynomial'. It is the set of problems solvable in polynomial time by a nondeterministic Turing machine, a hypothetical machine that can guess a solution and verify it. Equivalently, NP is the set of problems whose solutions can be verified in polynomial time by an ordinary deterministic machine given a certificate. The common misreading 'non-polynomial' is wrong and reverses the meaning.
What does it mean for a problem to be NP-complete?
A problem is NP-complete if it is in NP and every other problem in NP reduces to it in polynomial time. NP-complete problems are the hardest in NP: a polynomial-time algorithm for any single one would give a polynomial-time algorithm for all of NP, proving P = NP. Boolean satisfiability (SAT) was the first, via the 1971 Cook–Levin theorem. Thousands of problems — the traveling salesman decision problem, graph coloring, subset sum, clique — are now known to be NP-complete.
What is a polynomial-time reduction?
A polynomial-time reduction transforms instances of problem A into instances of problem B, using a polynomial-time algorithm, such that A's answer is 'yes' exactly when B's answer is 'yes'. If A reduces to B and you can solve B efficiently, you can solve A efficiently. Reductions are how NP-completeness spreads: to prove a new problem is NP-complete, you reduce a known NP-complete problem (like 3-SAT) to it, then show the new problem is itself in NP.
What happens to cryptography if P equals NP?
If P = NP with a practical, small-degree algorithm, most modern public-key cryptography would collapse. Breaking RSA, finding a private key, or forging a signature are problems whose correct answer is easy to verify — meaning they sit in NP. A fast general solver for NP would let an attacker recover keys as quickly as legitimate users verify them. Symmetric ciphers like AES weaken but do not automatically break, since their security rests on other assumptions. The reverse — proving P ≠ NP — would give cryptography a firmer theoretical foundation but would not by itself prove any specific cipher secure.
Is P vs NP actually solved?
No. P vs NP is unsolved and is one of the seven Clay Mathematics Institute Millennium Prize Problems, each carrying a $1,000,000 reward. Most computer scientists believe P ≠ NP, but no proof exists in either direction. Barriers like relativization (Baker–Gill–Solovay, 1975), natural proofs (Razborov–Rudich, 1994), and algebrization (2008) rule out entire families of proof techniques, which is part of why the problem has resisted attack for over five decades.
If someone found a polynomial-time algorithm for one NP-complete problem, what would happen?
It would prove P = NP outright. Because every NP problem reduces in polynomial time to any NP-complete problem, a single polynomial-time algorithm for one NP-complete problem — say 3-SAT — could be composed with those reductions to solve all of NP in polynomial time. The practical impact depends on the exponent: an O(n^100) algorithm proves the theorem but changes little in practice, whereas an O(n^2) algorithm would transform optimization, logistics, protein folding, theorem proving, and cryptography overnight.