Beta. Content is under active construction and has not been peer-reviewed. Report errors on GitHub.Disclaimer

Algorithms Foundations

P vs NP

A central open problem in computer science: is every problem whose solution can be verified in polynomial time also solvable in polynomial time? Covers P, NP, NP-completeness, reductions, Cook-Levin theorem, and relevance for ML.

CoreTier 3Stable~50 min
0

Why This Matters

The P vs NP question is not just the biggest open problem in computer science. it is one of the seven Millennium Prize Problems, with a one million dollar bounty from the Clay Mathematics Institute. If someone proved P=NPP = NP, public-key cryptography would break, most optimization problems would become tractable, and the entire landscape of AI would shift overnight.

For ML practitioners specifically: many core ML problems are NP-hard. Feature selection, optimal clustering, neural architecture search, finding the global minimum of a non-convex loss. Understanding P vs NP tells you why these problems are hard and why we rely on heuristics and approximations.

Mental Model

Think of it this way. Solving a Sudoku puzzle from scratch is hard. But if someone hands you a completed Sudoku, checking that it is valid takes a quick scan. P vs NP asks: is there a fundamental gap between finding solutions and checking solutions? Or is checking always just as hard as finding, once you are clever enough?

Formal Setup and Notation

We work with decision problems: problems whose answer is YES or NO. The input is encoded as a binary string of length nn.

Definition

The Class P

PP is the class of decision problems solvable by a deterministic Turing machine in time O(nk)O(n^k) for some constant kk.

Informally: problems where you can find the answer efficiently. Examples: sorting, shortest paths, linear programming, primality testing.

Definition

The Class NP

NPNP is the class of decision problems where a YES answer can be verified in polynomial time given a certificate (witness) of polynomial length.

Formally, LNPL \in NP if there exists a polynomial-time verifier VV and a polynomial pp such that:

xL    w with wp(x) such that V(x,w)=1x \in L \iff \exists w \text{ with } |w| \leq p(|x|) \text{ such that } V(x, w) = 1

Informally: problems where you can check a proposed solution efficiently. Examples: SAT, graph coloring, Hamiltonian cycle, subset sum.

Definition

NP-Hard and NP-Complete

A problem QQ is NP-hard if every problem in NPNP can be reduced to QQ in polynomial time. NP-hard problems are at least as hard as the hardest problems in NP.

A problem QQ is NP-complete if it is both in NPNP and NP-hard. These are the hardest problems within NP. If any NP-complete problem has a polynomial time algorithm, then P=NPP = NP.

Definition

Polynomial-Time Reduction

A polynomial-time reduction from problem AA to problem BB is a polynomial-time computable function ff such that:

xA    f(x)Bx \in A \iff f(x) \in B

If APBA \leq_P B and BPB \in P, then APA \in P. Reductions transfer hardness upward: if AA is hard and APBA \leq_P B, then BB is at least as hard.

The P vs NP Question

The question is simple to state:

P=?NPP \stackrel{?}{=} NP

We know PNPP \subseteq NP because any problem you can solve in polynomial time you can certainly verify in polynomial time (just solve it again and check). The question is whether NPPNP \subseteq P: can every problem with efficiently checkable solutions also be efficiently solved?

Core Definitions

The satisfiability problem (SAT) asks: given a Boolean formula ϕ(x1,,xn)\phi(x_1, \ldots, x_n) in conjunctive normal form (CNF), is there an assignment of truth values that makes ϕ\phi true?

3-SAT restricts each clause to exactly 3 literals. Despite this restriction, 3-SAT is NP-complete.

The certificate (or witness) for a SAT instance is a satisfying assignment. Given the assignment, you can check each clause in linear time.

Main Theorems

Theorem

Cook-Levin Theorem

Statement

SAT is NP-complete. That is, SAT is in NP and every problem in NP is polynomial-time reducible to SAT.

Intuition

The proof encodes the entire computation of a nondeterministic Turing machine as a Boolean formula. If the machine accepts (has some accepting path), the formula is satisfiable. The formula has polynomial size because it only needs to describe polynomially many time steps. This is the "grandfather" reduction: it reduces every NP problem to SAT in one shot.

Proof Sketch

Let LNPL \in NP with verifier VV running in time p(n)p(n). For input xx of length nn, create Boolean variables encoding: (1) the contents of each tape cell at each time step, (2) the head position at each time step, (3) the state at each time step. Write clauses ensuring: the initial configuration matches (x,w)(x, w), each step follows the transition function, and the machine reaches an accept state. The resulting formula ϕx(w)\phi_x(w) has size O(p(n)2)O(p(n)^2) and is satisfiable if and only if xLx \in L.

Why It Matters

Cook-Levin is the foundation of NP-completeness theory. Once you know SAT is NP-complete, you can prove other problems NP-complete by reducing SAT to them. This launched the entire field: Karp immediately showed 21 problems were NP-complete. Today thousands of NP-complete problems are known.

Failure Mode

The Cook-Levin theorem does not tell us whether P=NPP = NP or PNPP \neq NP. It only tells us that SAT is a "representative" hard problem: solving SAT efficiently would solve everything in NP efficiently.

Proof Ideas and Templates Used

The core technique is polynomial-time reduction. To show problem BB is NP-complete:

  1. Show BNPB \in NP (exhibit a polynomial-time verifier)
  2. Pick a known NP-complete problem AA
  3. Construct a polynomial-time function ff such that xA    f(x)Bx \in A \iff f(x) \in B

This template has been used thousands of times. Classic reductions:

  • SAT P\leq_P 3-SAT (clause splitting)
  • 3-SAT P\leq_P Independent Set (gadget construction)
  • 3-SAT P\leq_P Subset Sum (number encoding)

Canonical Examples

Example

Reduction from 3-SAT to Clique

Given a 3-SAT formula with kk clauses, build a graph: one node per literal per clause, edges between all nodes in different clauses whose literals are compatible (not a variable and its negation). The formula is satisfiable if and only if the graph has a clique of size kk. Each node in the clique gives a true literal from each clause.

Example

Why feature selection is NP-hard

Given a dataset with dd features, find the subset of kk features that minimizes prediction error. This can be shown NP-hard by reduction from Set Cover. In practice, we use greedy forward/backward selection, L1 regularization, or random search. these are all heuristics that avoid the exponential search over (dk)\binom{d}{k} subsets.

Common Confusions

Watch Out

NP does not mean exponential time

NP stands for "nondeterministic polynomial," not "non-polynomial." NP is a class of problems defined by verifiability. Many NP problems are in P (every problem in P is also in NP). The hard ones are the NP-complete problems, which might require exponential time. But nobody has proved that.

Watch Out

NP-hard is not the same as NP-complete

NP-hard means "at least as hard as the hardest problems in NP." An NP-hard problem does not need to be in NP itself. The halting problem is NP-hard but not in NP (it is undecidable). NP-complete means NP-hard and in NP.

Watch Out

Worst case vs average case

NP-hardness is a worst-case statement. SAT is NP-complete, but random SAT instances are often easy. Many NP-hard optimization problems have good average case algorithms or approximation algorithms. This is exactly why ML heuristics work in practice despite the problems being NP-hard in theory.

Connection to Machine Learning

Many ML problems are NP-hard in the worst case:

  • Feature selection: finding the optimal subset of kk features
  • Optimal clustering: kk-means is NP-hard for general kk and dd
  • Neural architecture search: finding the optimal architecture
  • Training neural networks: even training a 2-node network is NP-hard
  • MAP inference in graphical models

This does not mean we give up. It means we use:

  • Approximation algorithms with provable guarantees
  • Heuristics that work well in practice (SGD, greedy methods)
  • Relaxations (convex relaxation of combinatorial problems)
  • Structural assumptions that make instances tractable

Summary

  • PP = efficiently solvable; NPNP = efficiently verifiable
  • PNPP \subseteq NP is trivial; whether P=NPP = NP is open
  • Cook-Levin: SAT is NP-complete (the first and foundational result)
  • NP-complete problems are equivalent to each other under polynomial reductions
  • Most researchers believe PNPP \neq NP, but no proof exists
  • Many ML problems are NP-hard, which is why we use heuristics

Exercises

ExerciseCore

Problem

Show that the Clique problem is in NP. That is, given a graph GG and integer kk, describe a certificate and a polynomial-time verification procedure for deciding whether GG contains a clique of size kk.

ExerciseCore

Problem

If someone proves that P=NPP = NP, why would RSA encryption break?

ExerciseAdvanced

Problem

Explain why proving PNPP \neq NP is so difficult. What proof techniques have been shown to be insufficient, and why?

References

Canonical:

  • Sipser, Introduction to the Theory of Computation, Chapters 7-8
  • Arora & Barak, Computational Complexity: A Modern Approach (2009), Chapters 2-3

Current:

  • Aaronson, "P vs NP" survey (2017)
  • Wigderson, Mathematics and Computation (2019), Chapters 1-6

Next Topics

The natural next steps from P vs NP:

Last reviewed: April 2026

Prerequisites

Foundations this topic depends on.

Builds on This

Next Topics