Sub topic 14
The P vs NP Question and Why It Matters
This chapter assumes you already know the definitions of P, NP, polynomial-time reductions, and NP-completeness from earlier chapters. It also assumes you can read short Python programs and know the Cook-Levin theorem's statement that SAT is NP-complete. Here those pieces come together in one open question: does P equal NP?
Stating the Question Precisely
Every problem in P is also in NP. If you can solve a problem in polynomial time, you can verify a "yes" answer in polynomial time too: ignore the certificate and solve the instance directly. So P ⊆ NP is a theorem.
The open question is whether that containment is strict.
- P = NP means every problem whose "yes" answers can be checked in polynomial time can also be solved in polynomial time.
- P ≠ NP means at least one problem in NP has no polynomial-time algorithm at all.
NP-completeness turns this question about a whole class into a question about any single problem. If any one NP-complete problem, such as SAT, 3-SAT, or Vertex Cover, has a polynomial-time algorithm, then every problem in NP does. Every NP problem reduces to that problem in polynomial time, and composing two polynomials gives a polynomial. In the other direction, if P ≠ NP, then no NP-complete problem is in P.
The Two Possible Worlds
If P = NP
The most direct consequence is that finding becomes as easy as checking. For NP-complete problems this holds even for search problems, through a property called self-reducibility. Given a polynomial-time decider for SAT, you can build a satisfying assignment by fixing one variable at a time and asking the decider whether the formula is still satisfiable. That takes n + 1 calls to the decider for n variables.
The program below shows the reduction. It uses a brute-force decider as a stand-in so you can run it. If P = NP, you could swap in a polynomial-time decider, and the search procedure would then run in polynomial time too.
from itertools import product
def sat_decide(clauses, num_vars):
"""Brute-force SAT decider (exponential). Stand-in for a hypothetical fast one.
clauses: list of clauses in DIMACS style, e.g. [1, -2] means x1 OR NOT x2."""
for bits in product([False, True], repeat=num_vars):
assignment = {i + 1: bits[i] for i in range(num_vars)}
if all(any(assignment[abs(lit)] == (lit > 0) for lit in clause)
for clause in clauses):
return True
return False
def sat_search(clauses, num_vars, decide=sat_decide):
"""Find a satisfying assignment using only yes/no calls to `decide`."""
if not decide(clauses, num_vars):
return None
fixed = list(clauses)
assignment = {}
for v in range(1, num_vars + 1):
trial = fixed + [[v]] # force x_v = True with a unit clause
if decide(trial, num_vars):
fixed = trial
assignment[v] = True
else:
fixed = fixed + [[-v]] # x_v = False must then keep it satisfiable
assignment[v] = False
return assignment
# (x1 OR NOT x2) AND (x2 OR x3) AND (NOT x1 OR NOT x3)
print(sat_search([[1, -2], [2, 3], [-1, -3]], 3)) # {1: True, 2: True, 3: False}The search makes n + 1 decider calls, and each call works on a formula only slightly larger than the original. With a polynomial-time decider, the whole search therefore runs in polynomial time.
Other consequences would follow:
- Optimization. Scheduling, routing, packing, and circuit layout problems, whose decision versions are NP-complete, would all have exact polynomial-time algorithms. You get the optimum by binary-searching on the decision version's threshold.
- Cryptography. Public-key schemes depend on some problem that is easy to compute but hard to invert. Inverting such a function is an NP search problem, because a candidate preimage is easy to check. If P = NP, no such one-way functions exist, and those schemes lose their theoretical basis.
- Proofs. "Does this statement have a proof of at most n symbols?" is in NP, because a proof can be checked mechanically. If P = NP, finding short proofs would be roughly as easy as checking them.
If P ≠ NP
Most researchers expect this answer. It would confirm that the exponential gap seen in brute-force Subset Sum or SAT search is unavoidable in the worst case for every NP-complete problem. Several further results would follow:
- NP-intermediate problems exist. Ladner's theorem says that if P ≠ NP, some problems in NP are neither in P nor NP-complete. Later chapters cover this.
- An NP-completeness proof would become a real impossibility result. Today it means "no efficient algorithm is known, and finding one would settle a famous open problem." Under P ≠ NP it would mean "no efficient exact algorithm exists."
- Cryptography would still not be fully secure. Cryptography needs problems that are hard on average, for randomly generated instances, not just hard in the worst case. P ≠ NP does not imply that one-way functions exist. Secure cryptography needs that stronger assumption.
Why the Question Is So Hard to Settle
The Clay Mathematics Institute lists P vs NP as one of its Millennium Prize Problems, with a US$1,000,000 award. Many proof attempts have failed. Researchers have identified barriers, which are proof techniques shown to be unable to settle the question on their own.
| Barrier | Year | What it rules out |
|---|---|---|
| Relativization (Baker, Gill, Solovay) | 1975 | Arguments that still hold when every machine gets access to the same oracle. Some oracles make P = NP and others make P ≠ NP, so such arguments cannot decide the question. |
| Natural proofs (Razborov, Rudich) | 1994 | A broad family of circuit lower-bound arguments. If strong pseudorandom functions exist, these arguments cannot prove P ≠ NP. |
| Algebrization (Aaronson, Wigderson) | 2008 | Extensions of relativizing arguments that use algebraic oracles, which cover techniques that got past the first barrier. |
Any proof must therefore use ideas that avoid all three barriers.
Why It Matters to Working Engineers
Even unresolved, the question shapes daily practice:
- Problem triage. If you show that your problem's decision version is NP-complete, you stop searching for a fast exact algorithm that works in all cases. You switch to approximation, heuristics, parameterized algorithms, or SAT and ILP solvers.
- Honest specifications. "Find the optimal schedule for any input in under a second" is a requirement that would resolve P vs NP. You can recognize it as unrealistic before committing to it.
- Security reasoning. Knowing that cryptography rests on unproven hardness assumptions explains why the field tracks algorithmic advances and plans for algorithms to be replaced.
Common Misconceptions
- "NP means non-polynomial." NP stands for nondeterministic polynomial time. Every problem in P is in NP.
- "NP-hard problems are always slow in practice." Modern SAT solvers routinely handle industrial instances with millions of variables. Worst-case hardness says nothing about the instances you actually see.
- "Quantum computers solve NP-complete problems." No known quantum algorithm solves NP-complete problems in polynomial time. Shor's algorithm factors integers, and factoring is not known to be NP-complete.
- "P ≠ NP has been proven." Claimed proofs appear regularly. None has survived expert review.
Exercise
Suppose someone publishes a correct O(n^3) algorithm for Vertex Cover. Explain why this would put every problem in NP into P, and name one practical caveat.
Show answer
Vertex Cover is NP-complete, so every problem L in NP has a polynomial-time reduction to it. To decide L, apply the reduction, which runs in time p(n) and produces an instance of size at most p(n), then run the O(n^3) algorithm, which takes O(p(n)^3) time. That total is polynomial, so L is in P, and therefore P = NP. One caveat: the degree of the composed polynomial depends on the reduction, so some problems could still need impractically high-degree polynomial time.
Exercise
In satsearch, why is it safe to add the unit clause [-v] without calling the decider again when forcing xv = True fails?
Show answer
Before step v, the formula is satisfiable. That is checked at the start and preserved by every later step. Every satisfying assignment sets xv to either True or False. If no satisfying assignment sets xv = True, then some satisfying assignment must set x_v = False, so adding [-v] keeps the formula satisfiable.
Exercise
Explain why proving P ≠ NP would not by itself guarantee that RSA or similar public-key systems are secure.
Show answer
P ≠ NP is a worst-case statement. It says some instances of NP-complete problems are hard. Cryptography needs average-case hardness, meaning randomly generated keys must be hard to break, and it needs specific problems like factoring to be hard. Factoring is not known to be NP-complete, and P ≠ NP does not imply that one-way functions exist.