Time Complexity, P, NP, and Polynomial-Time Reductions
Anchor (Master): Sipser 2013 Introduction to the Theory of Computation 3e (Cengage) §7.1-7.4; Arora & Barak 2009 Computational Complexity: A Modern Approach (Cambridge) §1-2.2 and §2.3-2.5 (time hierarchy, Ladner's theorem, the Cook-Levin theorem and the structure of NP-complete problems)
Intuition Beginner
Some problems are easy to solve. Sorting a list of numbers, finding the shortest path between two cities on a map, checking whether a number is prime — all of these can be done by algorithms that finish quickly, even for large inputs. "Quickly" here means the running time grows at most as a polynomial function of the input size: quadratic, cubic, and so on.
Other problems seem hard to solve but easy to check. Given a Sudoku puzzle, finding the solution might take a lot of searching, but if someone hands you a filled-in grid, you can verify it is correct just by scanning the rows, columns, and boxes. This is the essence of the class NP: problems where a proposed solution can be verified in polynomial time.
The question "does P equal NP?" asks whether every problem whose solution can be quickly checked can also be quickly solved. It is one of the seven Clay Millennium Prize Problems, with a one-million-dollar reward for its resolution. Most researchers believe P does not equal NP, but a proof remains elusive.
A polynomial-time reduction is a way of saying "if I could solve problem B quickly, I could also solve problem A quickly." It works by transforming any instance of A into an instance of B using a polynomial-time algorithm. If B is easy, then A becomes easy via the reduction. This is the tool that lets us compare the difficulty of problems without solving them.
Visual Beginner
The landscape of complexity classes:
All decidable problems
┌─────────────────────────────────────────┐
│ │
│ NP │
│ ┌───────────────────────────┐ │
│ │ NP-complete │ │
│ │ ┌────────────┐ │ │
│ │ │ SAT, 3SAT, │ │ │
│ │ │ Clique, │ │ │
│ │ │ TSP (dec) │ │ │
│ │ └────────────┘ │ │
│ │ │ │
│ │ Graph iso? │ │
│ │ Factoring? │ │
│ │ │ │
│ └───────────────────────────┘ │
│ │
│ P │
│ ┌────────────┐ │
│ │ Sorting, │ │
│ │ Shortest │ │
│ │ path, │ │
│ │ Primality │ │
│ └────────────┘ │
│ │
└─────────────────────────────────────────┘| Class | Intuition | Example |
|---|---|---|
| P | Solvable in polynomial time | Is the graph connected? |
| NP | Verifiable in polynomial time | Does this graph have a Hamiltonian cycle? |
| NP-complete | Hardest problems in NP | SAT, 3SAT, Clique |
Worked example Beginner
Consider the Clique problem: given a graph and an integer , does contain a clique (a completely connected subgraph) of size ?
This problem is in NP. Here is why: if someone claims "yes, there is a clique of size " and gives you the set of vertices, you can check by verifying that every pair among those vertices is connected by an edge. There are pairs to check, which is polynomial in the input size. So a proposed solution can be verified quickly.
Is Clique in P? No polynomial-time algorithm is known for general graphs. The best known algorithms take time exponential in (and for large , exponential in the graph size). But nobody has proved that no polynomial-time algorithm exists.
A polynomial-time reduction from 3SAT to Clique would show: if you could solve Clique quickly, you could also solve 3SAT quickly. Since 3SAT is NP-complete (proved in 47.02.02), this would make Clique NP-complete too.
Check your understanding Beginner
Formal definition Intermediate+
A Turing machine (as defined in 42.04.01) decides a language if for every input , halts and accepts when and halts and rejects when . The time complexity of is the function where is the maximum number of steps takes on any input of length .
Definition (TIME and P). For a function :
The class P (polynomial time) is:
Definition (NP, verifier definition). A language is in NP if there exists a polynomial and a polynomial-time Turing machine (the verifier) such that for all :
The string is called a certificate (or witness). The verifier runs in time polynomial in .
Definition (NP, NDTM definition). A language is in NP if there exists a polynomial-time nondeterministic Turing machine that accepts : iff there exists an accepting computation path of on of length at most for some polynomial .
The two definitions are equivalent: the certificate in the verifier definition encodes the sequence of nondeterministic choices in the NDTM definition.
Definition (polynomial-time reduction). A language is polynomial-time reducible to a language , written , if there exists a polynomial-time computable function such that for all :
The relation is reflexive and transitive. If and , then (compose the reduction with the algorithm for ).
Definition (NP-completeness). A language is NP-complete if and for every , .
Counterexamples to common slips
"NP means 'not polynomial.'" NP stands for "nondeterministic polynomial time." It is a class of problems solvable in polynomial time on a nondeterministic machine, not a class of problems that are not polynomial.
"A problem in NP cannot be solved in polynomial time." P is a subset of NP, so every P problem is in NP. NP contains P; the question is whether it contains anything more.
"A polynomial-time reduction from A to B means A and B have the same difficulty." It means A is at most as hard as B (up to polynomial overhead). B could be much harder. Transitivity allows chaining: means .
Key theorem with proof Intermediate+
Theorem (P is closed under complement; NP is not known to be). If , then . The same is not known for NP (i.e., it is unknown whether ).
Proof. Let be a polynomial-time TM deciding . Construct that on input runs on and flips the answer: if accepts, rejects; if rejects, accepts. Then decides in the same time as .
The asymmetry for NP is fundamental: a verifier for checks existence of a good certificate, but requires checking that no certificate exists, which is a universal quantifier. The question is open and is related to, but distinct from, the P vs NP question.
Bridge. The P vs NP question is the foundational reason complexity theory has a structural backbone: the polynomial-time reduction creates a partial order on computational problems, and NP-complete problems sit at the top of this order within NP. This builds toward the Cook-Levin theorem in 47.02.02, where SAT is shown to be NP-complete by encoding arbitrary NP computations as Boolean formulas, and it appears again in 47.02.03 where the reduction chain SAT 3SAT Clique ... classifies dozens of problems as NP-complete at once. The central insight is that polynomial-time reductions transport both algorithms (if B is in P, so is A) and hardness (if A is NP-hard and , then B is NP-hard), and putting these together gives the entire edifice of NP-completeness.
Exercises Intermediate+
Lean formalization Intermediate+
Mathlib has Computability.TuringMachine with basic TM definitions but no time-bounded complexity classes. The definition of TIME(T(n)) as a set of languages, the classes P and NP, polynomial-time reductions, and the transitivity of are absent. A Codex.Complexity.P module defining P via polynomial-bounded TM computations and proving robustness under model changes would be the foundational step; this unit ships without it.
Advanced results Master
Two structural results illuminate the landscape defined by P, NP, and reductions.
Theorem 1 (time hierarchy). For any time-constructible functions with , [Arora & Barak §1.6]. The proof is a diagonalisation argument: a universal machine simulates every -time machine on its own encoding and flips the answer, running in time. This shows that giving a Turing machine more time strictly increases its power — there are problems solvable in time but not in time, problems solvable in time but not in polynomial time, and so on.
Theorem 2 (Cook-Levin theorem, statement). The language is NP-complete [Arora & Barak §2.3]. The proof constructs, for any NP machine and input , a Boolean formula whose satisfying assignments encode accepting computation paths of on . The full proof is developed in 47.02.02.
Theorem 3 (robustness of P). The class P is the same regardless of whether we use single-tape Turing machines, multi-tape Turing machines, or RAM machines. A single-tape TM can simulate a -tape TM with quadratic overhead: . A RAM machine can be simulated by a multi-tape TM with polynomial overhead [Arora & Barak §1.3]. This robustness is why P is a natural class: it does not depend on the specific computational model.
Synthesis. The P vs NP question is the foundational reason complexity theory organises problems by difficulty: polynomial-time reductions create a partial order, NP-complete problems are the maximal elements within NP, and the time hierarchy theorem ensures that this landscape has genuine structure (more time yields more power). The central insight is that the reduction transports both upper bounds (membership in P) and lower bounds (NP-hardness), and putting these together with the Cook-Levin theorem of 47.02.02 and the classical NP-completeness reductions of 47.02.03 classifies hundreds of natural problems into a small number of equivalence classes. This builds toward the polynomial hierarchy of 47.02.04, where the alternation of existential and universal quantifiers creates an infinite hierarchy of complexity classes, and appears again in the randomised classes of 47.03.01 where probabilistic computation potentially extends P.
Full proof set Master
Proposition 1 (equivalence of NP definitions). The verifier definition and the NDTM definition of NP are equivalent.
Proof. (Verifier NDTM): Let be a polynomial-time verifier for with certificate bound . Construct an NDTM that on input nondeterministically guesses a certificate of length and runs . If , some certificate causes to accept, so has an accepting path. If , no certificate is accepted. (NDTM Verifier): Let be a polynomial-time NDTM for . The certificate encodes the sequence of nondeterministic choices (at each branch point, which transition to take). The verifier simulates deterministically using the choices from the certificate and accepts iff the simulation accepts.
Proposition 2 (closure under polynomial-time reductions). If and , then .
Proof. Let reduce to in time , and decide in time . On input , compute in time , then run on in time . Total time: , a polynomial. Accept iff accepts.
Proposition 3 (linear speedup). If and , then .
Proof. Let decide in time . Construct with a larger tape alphabet that encodes symbols of 's tape in one cell of 's tape, for large enough that the overhead of processing symbols is less than steps. The simulation of one step of takes steps of (scan the local -block, compute the new -block, write it back). Choosing sufficiently large gives the speedup factor. The initial encoding of the input takes steps.
Connections Master
42.04.01— The Turing machine model defined there provides the computational framework on which time complexity is measured; the time-bounded classes P and NP are defined in terms of the halting behaviour of TMs within a step budget.50.03.01— Big-O notation and algorithmic complexity analysis give the formalism for measuring and comparing running times; the definition of P uses as the bound.47.02.02— The Cook-Levin theorem proves that SAT is NP-complete, establishing the first NP-complete problem and the foundation for all subsequent NP-completeness results.47.02.03— Classic NP-complete problems are classified by reduction chains from SAT, using the relation defined here.47.02.04— The polynomial hierarchy extends P and NP by alternating quantifiers over polynomial-time predicates, creating and classes.
Historical & philosophical context Master
The class P was first studied by Alan Cobham (1964) and Jack Edmonds (1965), who independently argued that polynomial time captures the intuitive notion of "efficient computation." Cobham's paper "The Intrinsic Computational Difficulty of Functions" defined P using Turing machines and proved its robustness under model changes. Edmonds, in his paper on maximum matching, argued that polynomial-time algorithms represent "good" algorithms.
The class NP was defined by Stephen Cook in his 1971 paper "The Complexity of Theorem-Proving Procedures," where he also proved the NP-completeness of SAT. Richard Karp independently proved NP-completeness for 21 additional problems in his 1972 paper "Reducibility Among Combinatorial Problems," establishing the pervasiveness of NP-completeness across combinatorics, logic, and optimisation [Arora & Barak §2.1].
Leonid Levin, working independently in the Soviet Union, also proved a version of the Cook-Levin theorem around 1971, and the result is sometimes called the Cook-Levin theorem in recognition of both contributions. The P vs NP question was formalised in these papers and has remained the central open problem in computational complexity theory.
Bibliography Master
@book{sipser2013,
author = {Sipser, Michael},
title = {Introduction to the Theory of Computation},
edition = {3rd},
publisher = {Cengage Learning},
year = {2013},
}
@book{arora-barak2009,
author = {Arora, Sanjeev and Barak, Boaz},
title = {Computational Complexity: A Modern Approach},
publisher = {Cambridge University Press},
year = {2009},
}
@article{cook1971complexity,
author = {Cook, Stephen A.},
title = {The Complexity of Theorem-Proving Procedures},
journal = {Proceedings of the 3rd Annual ACM Symposium on Theory of Computing},
pages = {151--158},
year = {1971},
}
@article{karp1972reducibility,
author = {Karp, Richard M.},
title = {Reducibility Among Combinatorial Problems},
journal = {Complexity of Computer Computations},
pages = {85--103},
year = {1972},
publisher = {Plenum Press},
}
@article{cobham1964intrinsic,
author = {Cobham, Alan},
title = {The Intrinsic Computational Difficulty of Functions},
journal = {Proceedings of the 1964 International Congress for Logic, Methodology and Philosophy of Science},
pages = {24--30},
year = {1964},
}