Technopedia

The technologist’s encyclopedia

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 M=(Q,Σ,Γ,δ,q0,qaccept,qreject)M = (Q, \Sigma, \Gamma, \delta, q_0, q_{accept}, q_{reject}) is a 7-tuple where QQ is a finite set of states, Σ\Sigma is the input alphabet, Γ\Gamma is the tape alphabet (Σ⊆Γ\Sigma \subseteq \Gamma), δ:Q×Γ→Q×Γ×{L,R}\delta: Q \times \Gamma \to Q \times \Gamma \times \{L, R\} is the transition function, and q0,qaccept,qreject∈Qq_0, q_{accept}, q_{reject} \in Q are special states.

A language L⊆Σ∗L \subseteq \Sigma^* is decidable (recursive) if some Turing machine halts on every input and accepts exactly the strings in LL.

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 MM and input ww, does M(w)M(w) halt? Turing proved this is undecidable by diagonalization: if a decider HH existed, one could construct a machine DD that does the opposite of H(D,D)H(D, D), a contradiction.

Key consequences:

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 L={w∈{0,1}∗:w contains an even number of 1s}L = \{w \in \{0,1\}^* : w \text{ contains an even number of } 1\text{s}\}.

Solution

Use two states: qevenq_{even} (start state, also accepting) and qoddq_{odd}. The transition function is:

  • δ(qeven,1)=(qodd,1,R)\delta(q_{even}, 1) = (q_{odd}, 1, R) and δ(qodd,1)=(qeven,1,R)\delta(q_{odd}, 1) = (q_{even}, 1, R) — each 1 toggles the parity.
  • δ(qeven,0)=(qeven,0,R)\delta(q_{even}, 0) = (q_{even}, 0, R) and δ(qodd,0)=(qodd,0,R)\delta(q_{odd}, 0) = (q_{odd}, 0, R) — 0s leave the parity unchanged.
  • On the blank symbol ⊔\sqcup: δ(qeven,⊔)=qaccept\delta(q_{even}, \sqcup) = q_{accept} and δ(qodd,⊔)=qreject\delta(q_{odd}, \sqcup) = q_{reject}.

The machine scans left to right once, so it runs in O(n)O(n) time and uses O(n)O(n) 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 LL is in P if a deterministic Turing machine decides it in O(nk)O(n^k) time for some constant kk. 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 <1/3< 1/3 on every input. The constant 1/31/3 is arbitrary: by running the machine O(log⁡(1/ε))O(\log(1/\varepsilon)) times and taking a majority vote, the error can be reduced to any desired ε>0\varepsilon > 0 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. BPP⊆BQP⊆PSPACE\text{BPP} \subseteq \text{BQP} \subseteq \text{PSPACE} (both inclusions proven; Bernstein–Vazirani 1997 for BQP⊆PSPACE\text{BQP} \subseteq \text{PSPACE}); whether BQP⊆P\text{BQP} \subseteq \text{P} is open. See Quantum Mechanics.

PSPACE: languages decidable by a deterministic Turing machine using O(nk)O(n^k) tape space for some constant kk, regardless of time. Since a polynomial-space machine has only polynomially many configurations, it can be simulated in exponential time, which gives P⊆NP⊆PH⊆PSPACE\text{P} \subseteq \text{NP} \subseteq \text{PH} \subseteq \text{PSPACE}.

co-NP: the class of complements of NP languages — languages whose “no” instances have polynomially checkable certificates. It is unknown whether NP=co-NP\text{NP} = \text{co}\text{-NP}; 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). NP⊆PH⊆PSPACE\text{NP} \subseteq \text{PH} \subseteq \text{PSPACE}. 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

PSPACE PH (Polynomial Hierarchy) NP P BPP (BPP ⊆ PSPACE proven; BPP ⊆ P open) BQP (open: BQP ⊆ P?) NC P ⊆ NP ⊆ PH ⊆ PSPACE BPP ⊆ BQP ⊆ PSPACE Solid = proven inclusion · Dashed = open question · Colors distinguish 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: A≤pBA \le_p B means there is a polynomial-time computable function ff such that x∈A  ⟺  f(x)∈Bx \in A \iff f(x) \in B. If AA is NP-complete and A≤pBA \le_p B, then BB is NP-hard.

Worked example: 3-SAT ≤ₚ CLIQUE

Take the formula φ=(x1∨¬x2∨x3)∧(¬x1∨x2∨x3)\varphi = (x_1 \lor \lnot x_2 \lor x_3) \land (\lnot x_1 \lor x_2 \lor x_3).

Step 1 — Create one vertex per literal per clause:

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 φ\varphi is satisfiable.

x₁ ¬x₂ x₃ ¬x₁ x₂ x₃ Clause 1: (x₁ ∨ ¬x₂ ∨ x₃) Clause 2: (¬x₁ ∨ x₂ ∨ x₃) No edges within a clause; no edge between complementary literals Clique {x₁, x₃}: x₁=T, x₃=T satisfies φ

The clique {x1,x3}\{x_1, x_3\} (one vertex from each clause, connected by an edge) corresponds to the satisfying assignment x1=T,x3=Tx_1 = T, x_3 = T.

PCP theorem

The PCP theorem (Arora, Lund, Motwani, Sudan, Szegedy, 1998) states that NP=PCP(O(log⁡n),O(1))\text{NP} = \text{PCP}(O(\log n), O(1)): every NP language has a probabilistically checkable proof where the verifier reads only O(1)O(1) bits of the proof and uses O(log⁡n)O(\log n) random bits. This is the foundation of inapproximability results: for example, MAX-3SAT is not approximable within 7/8+ε7/8 + \varepsilon unless P=NP\text{P} = \text{NP} (Håstad, 1997).

Authoritative sources

Practical implications

Example problem: show that the language L={w∈{0,1}∗:w has an even number of 1s}L = \{w \in \{0,1\}^* : w \text{ has an even number of } 1\text{s}\} is in P.

Solution

A one-pass algorithm decides LL in linear time: initialize a bit p=0p = 0; scan the input left to right, and whenever the current symbol is 1, flip pp (p←1−pp \leftarrow 1 - p); when the scan ends, accept iff p=0p = 0. Flipping a bit is exactly addition modulo 2, so pp tracks the parity of the number of 1s seen so far. The scan performs nn symbol inspections and at most nn bit flips, which is O(n)O(n) time and O(1)O(1) extra space — polynomial, so L∈PL \in \text{P}. (In fact LL 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:

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 φ\varphi whose clauses may have any length, build in polynomial time a 3-CNF formula φ′\varphi' that is satisfiable exactly when φ\varphi is.

Solution

Construction. Process each clause C=(ℓ1∨ℓ2∨⋯∨ℓk)C = (\ell_1 \lor \ell_2 \lor \cdots \lor \ell_k) independently, using fresh variables z1,…,zk−3z_1, \dots, z_{k-3} that appear nowhere else:

  • k=1k = 1: replace ℓ1\ell_1 by (ℓ1∨z)∧(ℓ1∨¬z)(\ell_1 \lor z) \land (\ell_1 \lor \lnot z) for a fresh zz — the two clauses together force ℓ1\ell_1 to be true.
  • k=2k = 2: keep (ℓ1∨ℓ2)(\ell_1 \lor \ell_2) as is.
  • k≥3k \ge 3: replace CC by the chain (ℓ1∨ℓ2∨z1)∧(¬z1∨ℓ3∨z2)∧⋯∧(¬zk−3∨ℓk−1∨ℓk)(\ell_1 \lor \ell_2 \lor z_1) \land (\lnot z_1 \lor \ell_3 \lor z_2) \land \cdots \land (\lnot z_{k-3} \lor \ell_{k-1} \lor \ell_k).

Correctness. If an assignment satisfies CC, some ℓj\ell_j is true; set the ziz_i before jj to make each earlier clause true via its ¬z\lnot z literal, and the clause containing ℓj\ell_j is satisfied by ℓj\ell_j itself. Conversely, restrict any satisfying assignment of the chain to the original variables: if all ℓj\ell_j were false, the first clause forces z1z_1 true, the second forces z2z_2 true, …, and the last clause (¬zk−3∨ℓk−1∨ℓk)(\lnot z_{k-3} \lor \ell_{k-1} \lor \ell_k) would be false — a contradiction. So φ′\varphi' is satisfiable iff φ\varphi is.

Polynomial time. A clause of length kk becomes max⁡(1,k−2)\max(1, k-2) clauses of size ≤ 3, so ∣φ′∣=O(∣φ∣)|\varphi'| = O(|\varphi|) 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.