Computability and Complexity Classes §
Computability and the fundamental time-based complexity classes (P, NP, and their relatives) answer two questions: can a problem be solved by a mechanical procedure at all, and if so, how fast? This material is relevant whenever you must establish whether a problem is tractable before committing engineering effort, or when you need to prove that no polynomial-time algorithm exists for a given decision problem.
Authoritative sources
Turing machines §
A Turing machine is a 7-tuple where is a finite set of states, is the input alphabet, is the tape alphabet (), is the transition function, and are special states.
A language is decidable (recursive) if some Turing machine halts on every input and accepts exactly the strings in .
Details
The Church–Turing thesis asserts that any effectively computable function can be computed by a Turing machine. This makes the Turing machine the universal model of computation: RAM machines, lambda calculus, and recursive functions all compute exactly the same class of functions.
The halting problem asks: given a description of a Turing machine and input , does halt? Turing proved this is undecidable by diagonalization: if a decider existed, one could construct a machine that does the opposite of , a contradiction.
Key consequences:
- No program can perfectly predict whether another program will terminate.
- No compiler can detect all infinite loops.
- The set of undecidable languages is uncountable (there are countably many machines but uncountably many languages).
Try the interactive simulator below: it runs a two-state Turing machine that decides whether the number of 1s on the tape is even. Load it, then step through the tape cell by cell and watch the head move and the state toggle between “even” and “odd”.
Example problem: design a Turing machine deciding .
Solution
Use two states: (start state, also accepting) and . The transition function is:
- and — each
1toggles the parity. - and —
0s leave the parity unchanged. - On the blank symbol : and .
The machine scans left to right once, so it runs in time and uses tape cells: parity is a regular, linear-time-decidable property. The same “finite memory swept across the input” pattern underlies many deciders; richer languages (like balanced parentheses) need the full tape as a counter or stack instead.
Once we have a formal model of computation, the natural next question is: how much time or space does the machine need? This question gives rise to the complexity classes below.
Complexity classes §
A language is in P if a deterministic Turing machine decides it in time for some constant . A language is in NP if a nondeterministic Turing machine decides it in polynomial time, equivalently if every “yes” instance has a polynomial-length certificate verifiable in polynomial time.
BPP (Bounded-error Probabilistic Polynomial time): a probabilistic Turing machine decides in polynomial time with error probability on every input. The constant is arbitrary: by running the machine times and taking a majority vote, the error can be reduced to any desired while staying polynomial time (this is amplification).
BQP (Bounded-error Quantum Polynomial time): the quantum analogue of BPP, decided by a quantum circuit of polynomial depth. (both inclusions proven; Bernstein–Vazirani 1997 for ); whether is open. See Quantum Mechanics.
PSPACE: languages decidable by a deterministic Turing machine using tape space for some constant , regardless of time. Since a polynomial-space machine has only polynomially many configurations, it can be simulated in exponential time, which gives .
co-NP: the class of complements of NP languages — languages whose “no” instances have polynomially checkable certificates. It is unknown whether ; if they differ, no NP-complete problem can have a short proof for its “no” instances.
Polynomial Hierarchy (PH): generalizes NP and co-NP via oracle machines (or alternating TMs). . PH is the natural next step after P and NP and is relevant to inapproximability results.
See Space and Parallel Complexity for space complexity classes (L, NL, PSPACE, EXPSPACE), hierarchy theorems, and circuit complexity (NC, AC⁰) in detail.
Details
Hierarchy of complexity classes §
Cook–Levin and NP-completeness §
The Cook–Levin theorem (Cook, 1971; Levin, 1973) establishes that SAT is NP-complete: every problem in NP polynomial-time reduces to SAT. Karp (1972) showed 21 combinatorial problems are NP-complete, including 3-SAT, CLIQUE, VERTEX-COVER, HAMILTONIAN CYCLE, and PARTITION.
Reduction: means there is a polynomial-time computable function such that . If is NP-complete and , then is NP-hard.
Worked example: 3-SAT ≤ₚ CLIQUE §
Take the formula .
Step 1 — Create one vertex per literal per clause:
- Clause 1: , ,
- Clause 2: , ,
Step 2 — Connect two vertices with an edge iff they are from different clauses AND are not complementary (i.e., one is not the negation of the other).
Step 3 — The graph has a clique of size 2 (= number of clauses) iff is satisfiable.
The clique (one vertex from each clause, connected by an edge) corresponds to the satisfying assignment .
PCP theorem §
The PCP theorem (Arora, Lund, Motwani, Sudan, Szegedy, 1998) states that : every NP language has a probabilistically checkable proof where the verifier reads only bits of the proof and uses random bits. This is the foundation of inapproximability results: for example, MAX-3SAT is not approximable within unless (Håstad, 1997).
Authoritative sources
Practical implications §
- NP-complete problems have no known polynomial-time algorithm.
- Heuristics, approximation algorithms, and parameterized algorithms are the standard tools (see Graphs for Christofides’ approximation for TSP, and Optimization and Differentiation for continuous optimization).
- In practice, many NP-complete instances are easy; hardness is worst-case.
Example problem: show that the language is in P.
Solution
A one-pass algorithm decides in linear time: initialize a bit ; scan the input left to right, and whenever the current symbol is 1, flip (); when the scan ends, accept iff . Flipping a bit is exactly addition modulo 2, so tracks the parity of the number of 1s seen so far. The scan performs symbol inspections and at most bit flips, which is time and extra space — polynomial, so . (In fact is regular and even decidable by a two-state finite automaton, which is the machine drawn in the simulator above.)
Open problems §
Complexity theory still has famous unresolved questions — statements about whether two of these classes coincide, where decades of research have settled only one direction of the inclusion:
- P vs NP: can every efficiently verifiable certificate also be found efficiently? The most famous open problem in computer science, and one of the Clay Millennium Prize problems.
- NP vs co-NP: whether “no” instances of NP problems also have short certificates. P = NP would imply NP = co-NP, but the converse is open.
- BPP vs P: whether randomness adds power. BPP ⊆ PSPACE is proven, BPP ⊆ P is open, and most researchers conjecture BPP = P.
- BQP vs NP: whether quantum computers can efficiently solve NP-complete problems. BQP ⊆ PSPACE is proven; BQP vs NP is open.
Details
These questions form the lattice of inclusions drawn in the hierarchy diagram above: for each pair of classes, one inclusion is a theorem and the reverse inclusion is open. They are deeply entangled: proving P = NP would collapse the entire polynomial hierarchy PH down to P, and proving BPP = P would show that randomness never helps for decision problems. This is why unconditional results such as the PCP theorem and the inapproximability bounds above are so valuable — they hold regardless of how these open questions resolve.
Quizz §
Quizz
Problem: Show that SAT ≤ₚ 3-SAT: given a CNF formula whose clauses may have any length, build in polynomial time a 3-CNF formula that is satisfiable exactly when is.
Solution
Construction. Process each clause independently, using fresh variables that appear nowhere else:
- : replace by for a fresh — the two clauses together force to be true.
- : keep as is.
- : replace by the chain .
Correctness. If an assignment satisfies , some is true; set the before to make each earlier clause true via its literal, and the clause containing is satisfied by itself. Conversely, restrict any satisfying assignment of the chain to the original variables: if all were false, the first clause forces true, the second forces true, …, and the last clause would be false — a contradiction. So is satisfiable iff is.
Polynomial time. A clause of length becomes clauses of size ≤ 3, so and the construction is linear time.
This is the reverse direction of the worked example above: together, SAT ≤ₚ 3-SAT and 3-SAT ≤ₚ CLIQUE show that SAT, 3-SAT, and CLIQUE are all NP-complete as hard as each other.