P vs NP
P vs NP asks one question: if a solution can be checked quickly, can it also be found quickly? Nobody knows. It is the most famous open problem in computer science, and it comes up in interviews as a reasoning question — “is this problem hard?” — far more often than as a formal proof.
The 60-second answer
Section titled “The 60-second answer”P is the set of decision problems solvable in polynomial time. NP is the set of decision problems whose yes answers can be verified in polynomial time. P ⊆ NP, and whether P = NP is open. A problem is NP-complete if it is in NP and every problem in NP reduces to it in polynomial time. NP-hard problems are at least as hard as NP-complete ones but need not be in NP. If a problem is NP-complete I stop looking for an exact polynomial algorithm and use constraints, approximation or a solver.
Decision problems first
Section titled “Decision problems first”The classes are defined over decision problems: yes/no questions. Optimisation problems convert to them — “is there a tour shorter than k?” rather than “find the shortest tour”. This matters because “solvable in polynomial time” is only well-defined when the output is one bit.
| Problem | Decision version |
|---|---|
| Shortest path | Is there a path from s to t of length ≤ k? |
| Travelling salesman | Is there a tour of length ≤ k? |
| Knapsack | Is there a subset with value ≥ v and weight ≤ w? |
| Graph colouring | Can this graph be coloured with k colours? |
The classes
Section titled “The classes”P — solvable in O(n^k) for fixed k. In practice: sorting, shortest path, maximum flow, primality testing, linear programming, string matching.
NP — given a certificate (a proposed solution), correctness is verifiable in polynomial time. The N is for non-deterministic, not “not polynomial”. Verification is not solving: a TSP tour can be checked in O(n), but finding one is a different matter. P ⊆ NP, because anything you can solve quickly you can obviously verify.
co-NP — problems whose no answers have polynomial certificates. Whether NP = co-NP is also open, and if they differ then P ≠ NP.
NP-complete
Section titled “NP-complete”A problem is NP-complete if it is in NP and every problem in NP reduces to it in polynomial time. Consequence: a polynomial algorithm for any NP-complete problem gives one for all of NP and proves P = NP. That is why “this is NP-complete” is a legitimate answer to “can you do better?”.
The first was SAT (Cook–Levin, 1971): is a boolean formula in conjunctive normal form satisfiable? 3-SAT, where every clause has exactly three literals, is the standard starting point for reductions.
| Problem | Shape |
|---|---|
| SAT / 3-SAT | Boolean satisfiability |
| Hamiltonian cycle | Visit every vertex exactly once |
| Travelling salesman (decision) | Cycle of bounded length |
| Subset sum | Subset with an exact total |
| Graph colouring | k-colouring |
| Clique / vertex cover | Structural graph properties |
NP-hard
Section titled “NP-hard”NP-hard means “at least as hard as every problem in NP” under polynomial reductions — but the problem itself need not be in NP. The halting problem is NP-hard and undecidable. TSP as an optimisation problem is NP-hard; its decision version is NP-complete.
NP-hard ⊇ NP ⊇ P, NP-complete = NP ∩ NP-hard(Schematic. Whether P = NP would collapse the P and NP regions.)
Reductions
Section titled “Reductions”A reduction from A to B is a polynomial-time transformation turning every instance of A into an instance of B with the same answer. If A reduces to B and B is easy, A is easy. Contrapositive: if A is hard and A reduces to B, B is hard — that is how hardness propagates.
To prove X is NP-complete: show X ∈ NP (a certificate verifies in polynomial time), then reduce a known NP-complete problem to X. The direction matters: reducing X to 3-SAT proves nothing about X’s hardness.
Why it matters in an interview
Section titled “Why it matters in an interview”You will not prove NP-completeness in a coding round. You will face questions where recognising hardness stops you burning thirty minutes hunting for a polynomial solution. Practical responses:
- Check the constraints. Subset sum with n ≤ 20 is fine as O(2ⁿ); knapsack with capacity ≤ 10⁴ is fine as O(n·capacity), which is pseudo-polynomial.
- Approximate. Metric TSP has the Christofides 1.5-approximation.
- Use a solver. SAT, ILP or constraint-programming solvers are often the right engineering answer.
- Exploit structure. Trees, interval graphs and planar graphs often make NP-hard problems polynomial.
Follow-up questions
Section titled “Follow-up questions”- What does it mean for a problem to be in NP but not NP-complete, assuming P ≠ NP?
- Why does a reduction go from the known-hard problem to the new one?
- Is the halting problem in NP? Why not?
- What makes a DP pseudo-polynomial, and how does that relate to NP-hardness?
- If someone proved P = NP, what would change in practice?
Common traps
Section titled “Common traps”- Saying NP means “not polynomial”. It means non-deterministic polynomial time; P ⊆ NP.
- Saying NP-hard problems are undecidable. Most real ones are decidable.
- Confusing NP-complete with NP-hard. Complete also requires membership of NP.
- Reversing the reduction. To show X is hard, reduce a hard problem to X.
- Ignoring the constraints. A bounded parameter can make the practical problem easy.