NP, NP-Complete, NP-Hard
You've just finished a Sudoku puzzle. Checking whether you solved it correctly? Easy — scan each row, column, and box. Takes seconds. But solving a blank puzzle? That can take minutes, hours, or forever depending on the puzzle.
This gap — between checking a solution and finding one — is at the heart of the most important unsolved problem in mathematics and computer science: P vs NP.
P — problems with fast solutions
P (Polynomial time) is the class of problems solvable by a computer in time \(O(n^k)\) for some fixed k, where n is the input size. These are the "efficiently solvable" problems.
Examples: sorting a list, finding shortest paths (Dijkstra), checking if a number is prime (AKS algorithm), solving a linear system.
NP — problems with fast verification
NP (Nondeterministic Polynomial time) is the class of problems where a proposed "yes" solution — called a certificate — can be verified in polynomial time.
Think of it like a padlock. Trying all combinations takes forever. But if someone hands you the combination, checking it opens the lock takes one second.
Examples: Sudoku (verify a completed grid in \(O(n^2)\)), factoring (verify that p × q = N in \(O(n^2)\)), Hamiltonian path (verify a proposed path visits every vertex once).
Every problem in P is also in NP — if you can solve it fast, you can verify it fast. The open question is whether the reverse holds: is every NP problem also in P?
Reductions — comparing problem hardness
Problem A reduces to problem B (written A ≤p B) if any instance of A can be transformed into an instance of B in polynomial time, with the answer preserved. If you can solve B, you can solve A. This means B is "at least as hard" as A.
Reductions are the tool for proving NP-hardness: show that a known hard problem reduces to your problem.
NP-Completeness — the hardest problems in NP
A problem X is NP-complete if: (1) X is in NP, and (2) every other NP problem reduces to X. NP-complete problems are the "hardest" in NP — solving any one of them efficiently would solve all of NP.
Stephen Cook (1971) proved the first NP-complete problem: SAT — can a Boolean formula be satisfied by some assignment of true/false to its variables? From SAT, a chain of reductions proved hundreds of other problems NP-complete.
Famous NP-complete problems
3-SAT: Satisfiability with 3 literals per clause — the workhorse of NP-completeness proofs.
Graph Coloring: Can you color a graph's vertices with k ≤ 3 colors so no adjacent pair shares a color?
Hamiltonian Path: Does a path exist that visits every vertex exactly once?
TSP (decision): Is there a tour visiting all cities with total cost ≤ k?
Subset Sum: Do some elements of a set add up to exactly T?
Clique: Does the graph contain a complete subgraph of size k?
NP-Hard — outside NP
NP-hard means "at least as hard as NP-complete" but the problem doesn't need to be in NP itself — it might not even have a polynomial-time verifiable certificate.
The optimization version of TSP ("find the minimum cost tour") is NP-hard but not NP-complete — it's not a yes/no decision problem. The Halting Problem is NP-hard and undecidable — even harder.
Why P ≠ NP is unproven
Almost every computer scientist believes P ≠ NP. But proof has been elusive for 50+ years. To prove P ≠ NP, you'd need to show that no algorithm — including algorithms not yet invented — can solve an NP-complete problem in polynomial time. Every known proof technique hits fundamental barriers. The answer may require entirely new mathematics.