Complexity Theory
Intuition
Section titled “Intuition”Complexity theory classifies problems by how much time or space they require to solve. The key insight is that even among decidable problems, some are tractable (solvable efficiently, class P) and some are intractable (no known efficient solution, class NP). The boundary between these classes is the deepest open question in computer science.
P vs NP intuition: P contains problems solvable in polynomial time (e.g., sorting, shortest paths). NP contains problems whose solutions can be verified in polynomial time (e.g., SAT, travelling salesman). The question is whether being able to verify quickly implies being able to solve quickly. Almost everyone believes P ≠ NP, but no one has proven it.
NP-completeness intuition: NP-complete problems are the hardest problems in NP. If any NP-complete problem has a polynomial-time algorithm, then P = NP (and every problem in NP becomes efficiently solvable). The Cook-Levin theorem proved SAT is NP-complete by directly encoding a TM’s computation as a Boolean formula. Other NP-complete problems are proved by reducing from known NP-complete problems.
6.1 Time Complexity
Section titled “6.1 Time Complexity”The running time of a deterministic TM on input is the number of steps takes before Halting. If never halts, the running time is .
runs in time if for every input of length , halts within steps.
Theorem 6.1. Let be a function with . Every TM that runs in time has An equivalent single-tape TM that runs in time .
Proof. A -tape TM running in time uses at most tape cells on each tape. Simulating One step of the -tape machine requires scanning the single-tape simulation from left to right To read all heads, then left to right again to update them. This costs per simulated Step. Over steps, the total is .
Theorem 6.1a (Time Hierarchy Theorem). If are time-constructible functions with Then .
Proof (idea). Use diagonalisation. Construct a TM that on input of length :
- Compute (possible since is time-constructible).
- Simulate all TMs in parallel for steps on input .
- If any accepts within steps, does the opposite (reject).
- Otherwise, accept.
runs in time and differs from every TM that runs in time on at least One input.
Corollary. .
Definition. .
Definition. .
6.2 The Class P
Section titled “6.2 The Class P”is the class of languages decidable in polynomial time by a deterministic TM. This Captures the notion of “efficiently solvable.”
Examples of problems in P:
- Path connectivity (BFS/DFS): .
- Shortest paths (Dijkstra): .
- Sorting: .
- 2-SAT: .
- Primality testing: (AKS algorithm).
6.3 The Class NP
Section titled “6.3 The Class NP”Equivalent definition. A language is in NP if there exists a polynomial-time verifier And a polynomial such that:
The string is called a certificate (or witness).
Examples of problems in NP:
- SAT: certificate is a satisfying assignment.
- Clique: certificate is the set of vertices forming the clique.
- Travelling Salesman (decision): certificate is the tour.
- Subset Sum: certificate is the subset.
- Graph 3-Colouring: certificate is the colouring.
- Integer factorisation (decision version): certificate is a factor.
Theorem 6.2. .
Proof. Every deterministic polynomial-time algorithm is a special case of a nondeterministic one (with exactly one choice at each step). Alternatively, the certificate can be empty.
Theorem 6.2a. If and Then .
Proof. Let be the polynomial-time verifier for and be the polynomial-time reduction. Then is a polynomial-time verifier for .
Open question: ? This is the most important open problem in computer Science. Most researchers believe .
Consequences of P = NP. If Then every problem in NP (including SAT, Travelling Salesman, graph colouring, protein folding, etc.) would have polynomial-time algorithms. This would revolutionise optimisation, cryptography (RSA and most public-key systems would be broken), And artificial intelligence. However, after decades of research, no polynomial-time algorithm has Been found for any NP-complete problem, providing strong empirical evidence for .
6.4 NP-Completeness
Section titled “6.4 NP-Completeness”A language is NP-complete if:
- And
- for every (polynomial-time mapping reduction).
A language is NP-hard if condition (2) holds (it need not be in NP).
Theorem 6.3 (Cook-Levin, 1971). is NP-complete.
Proof (detailed sketch).
: a satisfying assignment is a polynomial-size certificate that can be verified in polynomial time.
For any There is a polynomial-time NTM that decides in time for some . Given input of length Construct a Boolean formula that is satisfiable iff accepts .
The formula encodes the tableau of on : a table of size where each cell records the symbol at tape cell after step of the computation.
Variables: For each position in the tableau and each symbol in the combined state-tape alphabet A variable indicating that cell contains .
Constraints:
- Well-formedness: Each cell contains exactly one symbol. For each : exactly one of \{x_{i,j,s} : s \in \Gamma} is true.
- Start configuration: Row 0 encodes at position 0, at position 1, etc.
- Transition correctness: For every window of the tableau, the bottom row must be a valid successor of the top row according to ‘s transition function.
- Acceptance: Some cell in the last row contains .
Each constraint can be expressed as a polynomial-size CNF formula (using standard encodings of “exactly one” and “window” constraints). The total formula has size Which is polynomial in .
Worked Example: Cook-Levin tableau construction (simplified)
Consider an NTM that decides a language in time . On input (length ), The tableau is a grid (since ).
The formula has variables for each cell. For instance, indicates That position contains the start state. The constraints enforce:
- Row 0: at position 0,
0at position 1,1at position 2, at position 3. - Transition windows: each block must be consistent with .
- Row 3: at least one cell contains .
If accepts Then the accepting computation path provides a satisfying assignment (the tableau records that path). If is satisfiable, the satisfying assignment Encodes a valid accepting computation.
Corollary 6.4. If any NP-complete problem is in P, then P = NP.
Theorem 6.5. If and Then .
Proof. To decide on input : compute in polynomial time (the reduction), then decide on in polynomial time. Total: polynomial time.
6.5 Classic NP-Complete Problems
Section titled “6.5 Classic NP-Complete Problems”3-SAT. Given a CNF formula with exactly 3 literals per clause, is it satisfiable?
Reduction: SAT 3-SAT. Replace each clause with more or fewer than 3 literals by Equivalent clauses with exactly 3 literals using auxiliary variables.
Theorem 6.5a. SAT 3-SAT.
Proof. Given a CNF formula Convert each clause to exactly 3 literals:
- Clause (1 literal): replace with where are new variables. This is satisfiable iff is true.
- Clause (2 literals): replace with where is new. Satisfiable iff is true.
- Clause : leave as is.
- Clause for : replace with where are new variables. Satisfiable iff the original clause is satisfiable.
Each transformation is polynomial-size and preserves satisfiability.
Vertex Cover. Given a graph and integer Is there a vertex cover of size ?
Theorem 6.5b. 3-SAT Vertex Cover.
Proof (sketch). Given a 3-CNF formula with clauses and variables, construct a graph :
- For each variable Create a variable gadget: two vertices and connected by an edge. Selecting into the cover corresponds to setting to true (removing from consideration).
- For each clause Create a clause gadget: a triangle of three vertices connected to the corresponding literal vertices in the variable gadgets.
- Set the target: .
The cover must include at least one endpoint of each variable-gadget edge ( vertices) and at least two vertices from each clause triangle ( vertices). Selecting a literal vertex in the Cover removes it from clause consideration; the remaining two triangle vertices must be in the Cover. The formula is satisfiable iff we can choose literal vertices such that each clause triangle has at most one vertex already excluded.
Clique. Given and integer Does contain a clique of size ?
Reduction: Vertex Cover Clique. has a vertex cover of size iff has a clique of size .
Proof. If is a vertex cover of size in Then every edge of has at Least one endpoint in . So is an independent set in Meaning every pair in is an edge in . Hence has a clique of size . The converse is analogous.
Hamiltonian Path. Given a graph Does have a path visiting every vertex exactly Once?
Theorem 6.5c. Vertex Cover Hamiltonian Path (via Hamiltonian Cycle).
Proof (sketch). Given for Vertex Cover, construct a graph such that has a Vertex cover of size iff has a Hamiltonian cycle. The construction uses selection gadgets That choose vertices (the cover), verification gadgets that check every edge is covered, and Connecting gadgets that string the selections together into a single cycle. The construction is Polynomial.
Subset Sum. Given integers and target Is there a subset summing To ?
Theorem 6.5d. 3-SAT Subset Sum.
Proof (sketch). Given a 3-CNF formula with variables and clauses Construct a set of numbers and target in decimal.
For each variable Create two numbers and . In the “variable digits” (first columns), has a 1 in column and 0 elsewhere; also has a 1 in column and 0 elsewhere. This forces choosing exactly one of .
For each clause Add a “clause digit” (column ): in (resp. ), This digit is 1 iff (resp. ) appears in . The target has 1 in Every digit. Choosing or contributes to satisfying the clauses that contain That literal.
Partition. Given integers Can be partitioned into two subsets of Equal sum?
Reduction chain:
Worked Example: Reducing 3-SAT to Independent Set
Given a 3-CNF formula with clauses, construct a graph :
- For each clause Create a group of 3 vertices (one per literal).
- Within each group, add all three edges (forming a triangle). At most one vertex per group can be in an independent set.
- For each pair of contradictory literals ( and ) in different groups, add an edge. They cannot both be selected.
Set the target to (one vertex per clause). An independent set of size exists iff Is satisfiable: selecting one literal per clause without contradiction gives a consistent Satisfying assignment.
6.6 Space Complexity
Section titled “6.6 Space Complexity”Definition. is the class of languages decidable by a deterministic TM Using tape cells. is the nondeterministic analogue.
Key classes:
- , logarithmic space.
- , nondeterministic logarithmic space.
- .
Theorem 6.6 (Savitch, 1970). For .
Proof (sketch). To decide whether an NTM using space accepts, we check reachability in the Configuration graph. The graph has at most nodes, where is the tape alphabet size and the number of states. A deterministic TM can decide Reachability using the recursive algorithm (can reach in at most steps?), which uses space and recurses to depth . Total space: .
Corollary 6.7. .
Proof. by Savitch’s theorem.
NL-completeness. A language is NL-complete if and every is log-space reducible to .
Theorem 6.8 (Immerman—Szelepcsényi, 1987). .
This is surprising because it is not known whether . The …/1-number-and-algebra/3_proof-and-logic uses An inductive counting argument: given an NTM for Construct an NTM for that Counts the number of reachable configurations.
PSPACE-completeness. A language is PSPACE-complete if it is in PSPACE and every PSPACE problem Reduces to it. Key PSPACE-complete problems:
- TQBF (True Quantified Boolean Formula): Given a fully quantified Boolean formula where and is a CNF formula, is true?
Theorem 6.9. TQBF is PSPACE-complete.
Proof (membership). Evaluate the quantifiers recursively. For Try both values Of and recurse. For Similarly. At depth Evaluate . Each level Uses space to store the current assignment, giving total.
Proof (hardness). Reduce from any using the configuration graph. A Computation of a PSPACE TM on input of length uses at most cells for some Polynomial . The number of distinct configurations is at most which is exponential. The statement ” accepts ” can be expressed as: “there exists a Configuration reachable from the start configuration in steps such that for all Configurations reachable from in one step, there exists a configuration …” This alternating reachability formula can be encoded as a quantified Boolean formula of Polynomial size.
Worked Example: Evaluating a QBF formula
Evaluate .
- For : need such that . So we need (which exists).
- For : need such that . So we need (which exists).
Since both values of lead to a satisfying assignment for , is true.
6.7 The Polynomial Hierarchy
Section titled “6.7 The Polynomial Hierarchy”The polynomial hierarchy (PH) generalises the notions of NP and coNP through alternating Quantifiers.
Definition. Define the classes and inductively:
- .
- (NP with a oracle).
- (coNP with a oracle).
- .
Equivalent characterisation. A language is in iff there exist polynomial-time Computable relations and polynomials such that:
Where each and the quantifiers alternate, starting with .
Examples:
- : “there exists a certificate.”
- : “for all certificates.”
- contains problems like “does there exist a strategy for player 1 such that for all strategies of player 2, player 1 wins?” (for polynomial-size games).
- contains the complement of such problems.
Relationships:
Theorem 6.10. If for some Then (the polynomial hierarchy collapses to level ).
Proof. If Then the oracle Provides no additional power. By induction, for all So .
It is widely believed that does not collapse.
6.8 Beyond NP
Section titled “6.8 Beyond NP”coNP. The class of languages whose complements are in NP. A problem is in coNP if every “no” Instance has a polynomial-time verifiable certificate.
- Example: “Is this formula a tautology?” (the certificate for “no” would be a failing assignment).
- .
- It is unknown whether . If Then .
Theorem 6.11. If Then .
Proof. If Then (since Is closed under complement), so . The contrapositive gives the Result.
PSPACE. The class of languages decidable in polynomial space:
- .
- (space hierarchy theorem).
- PSPACE-complete problems: TQBF, generalised geography, determining the winner of a position in certain games.
EXPTIME. The class of languages decidable in exponential time:
- .
- (time hierarchy theorem).
- EXPTIME-complete problems: Generalised chess, Go (on sufficiently large boards), determining the winner of a two-player game with exponential game tree.
Hierarchy summary:
| Inclusion | Known to be proper? | Theorem used |
|---|---|---|
| Yes | Pumping lemma | |
| Yes | CYK algorithm | |
| Yes | Diagonalisation | |
| Yes | Time hierarchy | |
| Yes | Space hierarchy | |
| Yes | Savitch’s corollary | |
| Unknown | ||
| Unknown | Open problem | |
| Unknown | Open problem |
Both inclusions and are Known to be proper (), but the status of vs. remains open.
6.9 Ladner’s Theorem: NP-Intermediate Problems
Section titled “6.9 Ladner’s Theorem: NP-Intermediate Problems”If P ≠ NP, are there languages between them; in NP but neither in P nor NP-complete? Or does every problem in NP sit at one extreme? Ladner’s theorem answers: there are NP-intermediate problems, and they can be constructed explicitly.
Theorem 6.12 (Ladner, 1975). If P ≠ NP, then there exists a language that is not NP-complete.
Proof idea. Construct by diagonalisation over the polynomial-time deciders. The construction “squashes” SAT: contains exactly the strings in SAT whose length is a perfect square; but with the length threshold adjusted so that is neither trivially easy nor NP-complete. The construction alternates between two modes:
- Mode 1 (make it easy): On inputs of length , decide with the -th polynomial-time machine . If decides correctly, is falsified; so switch to Mode 2.
- Mode 2 (make it hard): Ensure differs from the -th NP-complete language on some input, by padding SAT instances; so if were NP-complete, SAT would reduce to , which the padding prevents; so switch back to Mode 1.
The alternation is self-limiting: each switch happens later than the last, so is neither in P (Mode 1 always fails eventually) nor NP-complete (Mode 2 always blocks the reduction). The formal proof requires the delay trick to ensure the diagonalisation doesn’t outpace the polynomial bounds.
Ladner’s theorem tells us that the P vs. NP question is maximally fine-grained: either P = NP, or there is a genuine hierarchy of intermediate difficulty. The natural candidates; integer factorisation, graph isomorphism; are believed to be NP-intermediate.
6.10 Randomised Computation: The Class BPP
Section titled “6.10 Randomised Computation: The Class BPP”Definition. (Bounded-error Probabilistic Polynomial time) is the class of languages decidable by a probabilistic polynomial-time TM that, on every input, gives the correct answer with probability .
The constant is arbitrary; amplification (repeating the computation times and taking the majority) raises the success probability to for a constant by the Chernoff bound, at a multiplicative cost of in running time.
Theorem 6.13 (Adleman, 1978). .
Proof. Let . Amplify the machine to error by running it times. For each input of length , the machine uses random bits. Call a random string bad if it causes a wrong answer on some input of length . By the union bound, the number of bad strings is . So if , the bad strings are at most a fraction of ; most strings are good. Fix a good : the deterministic machine that runs the randomised machine with hardwired is correct on every input of length . The circuit family hardwires for each length.
This means BPP machines can be derandomised at the cost of non-uniform advice; and if (widely believed), the randomisation was never doing real work.
6.11 Oracle Separations: Why Relativisation Is Not Enough
Section titled “6.11 Oracle Separations: Why Relativisation Is Not Enough”Definition. An oracle is a set of strings queryable in one step by a Turing machine. and are the classes definable with oracle access to .
Theorem 6.14 (Baker—Gill—Solovay, 1975). There exists an oracle such that , and there exists an oracle such that .
Proof idea. For : let (PSPACE-complete). Then (the oracle answers any PSPACE query). For : define by diagonalisation over all polynomial-time oracle machines, ensuring the constructed oracle answers queries to separate from .
The Baker—Gill—Solovay theorem is a caution: the techniques available to complexity theorists (relativising arguments) cannot resolve P vs. NP, because those techniques would apply equally well in the oracle worlds where the answer goes both ways. This is why the resolution of P vs. NP requires non-relativising techniques; which the circuit-complexity approach (circuit lower bounds) and the algebraic approach (arithmetisation in IP = PSPACE) exemplify.
6.12 The PCP Theorem: Proofs You Can Check by Glance
Section titled “6.12 The PCP Theorem: Proofs You Can Check by Glance”Suppose a proof of a statement is written down, but you are allowed to randomly inspect only a handful of its positions – a constant number of characters – and, from that glance alone, accept correct proofs always and reject bogus ones with high probability. For most proof systems this is impossible: skip one step of a long calculation and you might miss the error. The PCP theorem says that for NP statements, such proofs exist – after rewriting the proof into a special redundant form.
Theorem 6.13 (Feige–Goldwasser–Lund–Safra–Szegedy 1991; Arora–Safra 1992; Arora–Lund–Motwani–Sudan–Szegedy 1992).
A language is in NP if and only if there is a verifier that, given an input of length and oracle access to a proof :
- flips random coins (so at most possible inspection patterns),
- queries constant many positions of ,
- accepts or rejects – with completeness ≥ when (some proof makes accept with probability 1 in the standard formulation), and soundness ≤ when (every proof fools on at most a third of its random choices).
The proof may be exponentially long in – the verifier never reads it all; it only probes.
Why “rewritten” matters. A vanilla NP witness (a satisfying assignment) has no such robust property: changing one variable’s value can be detected only by checking a constraint that mentions it. The PCP construction encodes the witness into a locally testable object where every error contaminates a constant fraction of the proof, so any inconsistent assignment corrupts enough positions that a constant-sample check catches it. Building such encodings is the entire difficulty of the theorem.
The engine: gap amplification. Start from a constraint-satisfaction view of NP: SAT compiles to a system of constant-size constraints where a satisfying assignment satisfies all of them. The PCP construction repeatedly applies a transformation that:
- keeps satisfiable systems perfectly satisfiable, and
- drives unsatisfiable systems’ maximum satisfiable fraction down by a constant factor,
while increasing the proof size only polynomially. After amplification rounds, an unsatisfiable instance has no assignment satisfying more than a constant fraction below 1 – and at that point a verifier can sample a single random constraint, check it with a constant number of proof probes, and get soundness . That constant gap is what makes queries possible.
Dinur’s proof (2006). The original proofs were long and algebraic. Irit Dinur’s combinatorial proof reformulates the amplification as a graph powering step on constraint graphs: expand the constraint graph so that violations spread, then reduce alphabet size with a local test – repeat times. This version is short enough to teach and is the standard reference for a first encounter.
The consequence that reshaped a field: hardness of approximation. Because the PCP reduction preserves a gap, it maps optimisation problems to themselves with a gap: MAX-3SAT compiles so that satisfiable instances stay fully satisfiable while unsatisfiable ones have no assignment satisfying more than a fraction of clauses. Any polynomial-time algorithm guaranteeing better than would distinguish the two cases – solving NP-complete SAT exactly. This yields optimal inapproximability constants:
| Problem | Best polynomial approximation | Hardness (via PCP) |
|---|---|---|
| MAX-3SAT | (random assignment) | better than is NP-hard |
| Vertex cover | better than is NP-hard (UG-hard at 2) | |
| Set cover | is NP-hard | |
| Chromatic number | is NP-hard |
Before the PCP theorem these gaps had no explanation; after it, “the best known approximation ratio equals random assignment” stopped being an embarrassment and became a theorem’s fingerprint.
Connection to interactive proofs. The PCP theorem is the compiled form of the IP = PSPACE result: an interactive proof protocol between a verifier and a (possibly untrusted) prover can be converted, by committing the prover to answers via Merkle-tree-style cryptographic hashing, into a non-interactive oracle proof inspectable in constant queries. This lineage – interactive proofs → probabilistically checkable proofs → delegation of computation – is the foundation of modern proofs of proximity, SNARKs, and verifiable delegation in blockchain systems.
In one sentence. Every NP statement has proofs that certify themselves: if the claim is true, some proof survives arbitrary probing; if false, every proof is caught by a constant-size glance – and this impossible-sounding object is exactly what makes approximation hardness and modern verifiable computation possible.
Where to go next
Section titled “Where to go next”- Computability; the undecidability results that motivate the complexity classes.
- Algorithm Analysis; the concrete algorithms whose running times populate the complexity classes.
Cross-References
Section titled “Cross-References”Turing Machines — Complexity classes are defined in terms of Turing machine resources, connecting computational models to complexity.
Algorithm Analysis — Complexity theory classifies the computational difficulty of problems that algorithms attempt to solve.
Regular Languages — The Chomsky hierarchy places regular languages at the simplest level of the complexity hierarchy.
Common Mistakes
Section titled “Common Mistakes”Confusing NP-hard with NP-complete. NP-hard means “at least as hard as every problem in NP” (under polynomial-time reductions). NP-complete means NP-hard and in NP. The halting problem is NP-hard but not NP-complete because it is not in NP (it is not even decidable). A problem can be NP-hard without being verifiable in polynomial time.
Assuming “NP” means “Not Polynomial.” NP stands for Nondeterministic Polynomial time. A problem is in NP if a proposed solution can be verified in polynomial time by a deterministic TM, regardless of how long it takes to find the solution. This is about verifiability, not solvability.
Believing that polynomial reductions prove a problem is “hard” in an absolute sense. Polynomial-time reductions show relative difficulty within the polynomial hierarchy. A problem reduced from SAT is “hard” only in the sense that solving it would solve SAT. It says nothing about the problem’s difficulty for small input sizes or with respect to other resource bounds.
Confusing the classes P and NP. P ⊆ NP is known (a deterministic TM is a special case of a non-deterministic one). The open question is whether P = NP, whether every problem verifiable in polynomial time is also solvable in polynomial time. Most experts believe P ≠ NP, but this is unproven.
Assuming NP ⊆ coNP is false. It is unknown whether NP = coNP. If it were true, then every problem whose “no” instances are efficiently verifiable would also have “yes” instances efficiently verifiable. This is considered unlikely but remains open, and is considered a harder question than P vs. NP by some researchers.