THEORY OF COMPUTATION / 5. P, NP & COMPLEXITY
Complexity Theory — P, NP, and NP-Completeness
The hardest open problem in mathematics — and why it matters for every algorithm you write
EXPLANATION
Complexity Theory asks: among all decidable problems, which ones can be solved EFFICIENTLY? "Efficiently" means polynomial time — O(nᵏ) for some constant k. Time Complexity Classes: P (Polynomial Time): Problems solvable by a deterministic TM in O(nᵏ) time. These are the "tractable" problems — practically solvable even for large inputs. Examples: sorting, shortest path, primality testing (AKS algorithm, 2002), matrix multiplication, most graph algorithms. NP (Nondeterministic Polynomial Time): Two equivalent definitions: ① Problems solvable by a NONDETERMINISTIC TM in polynomial time (the machine "guesses" the solution) ② Problems where a given solution can be VERIFIED in polynomial time Key insight: verifying is often much easier than finding. Given a proposed Hamiltonian cycle, you can verify it in O(n). Finding one might take exponential time. Examples: Boolean Satisfiability (SAT), Traveling Salesman, Graph Coloring, Subset Sum, Knapsack. P ⊆ NP (trivially: if you can solve it, you can verify it). The million-dollar question: P = NP? NP-Hard: problems at least as hard as the hardest NP problems. Solving any NP-Hard problem in poly-time would solve ALL NP problems. NP-Complete: NP-Hard AND in NP. The hardest problems IN NP. - Cook-Levin Theorem (1971): SAT is NP-Complete. FIRST proof. - After SAT, proving other problems NP-Complete: reduce SAT to your problem in poly time. - If your problem X is NP-Complete: don't search for poly-time algorithm (likely doesn't exist). Use approximation, heuristics, or exact algorithms for small inputs. Polynomial Reduction (A ≤p B): "A reduces to B in poly time" means: solve A by transforming input to B's input, solve B, transform output. If B ∈ P and A ≤p B, then A ∈ P. Reductions prove relative hardness. Important NP-Complete problems: - SAT: given boolean formula, is there an assignment making it true? - 3-SAT: SAT where formula is in 3-CNF (at most 3 literals per clause). Everything reduces to 3-SAT. - Vertex Cover: find minimum set of vertices covering all edges - Clique: does graph have clique of size k? - Hamiltonian Path/Cycle: visit all vertices exactly once - TSP (decision): is there a tour of cost ≤ k? - Graph Coloring: color graph with k colors, no adjacent same color - Subset Sum: does subset sum to target T? - Partition: can set be divided into two equal-sum subsets? Beyond NP: - co-NP: complements of NP problems (UNSAT — prove formula has no solution) - PSPACE: decidable using polynomial SPACE (may use exponential time) - EXPTIME: decidable in exponential time - Undecidable: no algorithm at all Why P≠NP is believed (but unproven): - Thousands of smart people tried for 50+ years — no poly-time algorithm found for any NP-Complete problem - Cryptography depends on it (RSA, factoring assumed hard) - But absence of evidence is not evidence of absence — it could be proven tomorrow
DIAGRAM
COMPLEXITY HIERARCHY: ┌────────────────────────────────────────────────┐ │ EXPTIME │ │ ┌──────────────────────────────────────────┐ │ │ │ PSPACE │ │ │ │ ┌────────────────────────────────────┐ │ │ │ │ │ NP │ │ │ │ │ │ ┌──────────────────────────────┐ │ │ │ │ │ │ │ NP-Complete │ │ │ │ │ │ │ │ SAT, TSP, Clique │ │ │ │ │ │ │ └──────────────────────────────┘ │ │ │ │ │ │ ┌──────────┐ │ │ │ │ │ │ │ P │ ← sorting, Dijkstra │ │ │ │ │ │ └──────────┘ │ │ │ │ │ └────────────────────────────────────┘ │ │ │ └──────────────────────────────────────────┘ │ └────────────────────────────────────────────────┘ P vs NP (the question): ┌────────────────┐ ┌──────────────────────┐ │ If P = NP: │ │ If P ≠ NP (believed)│ │ NP │ │ NP │ │ ┌──────────┐ │ │ ┌───────────────┐ │ │ │ P = NP │ │ │ │ NP-Complete │ │ │ └──────────┘ │ │ └───────────────┘ │ │ Crypto broken │ │ ┌────────┐ │ │ AI trivial │ │ │ P │ │ │ Everything │ │ └────────┘ │ │ efficiently │ │ Crypto secure │ │ solvable! │ │ Hard problems exist │ └────────────────┘ └──────────────────────┘
CODE