Complexity Theory
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 \mathrm{TIME(t_1(n)) \subsetneq \mathrm{TIME(t_2(n)).
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. \mathrm{P \subsetneq \mathrm{EXPTIME.
Definition. \mathrm{TIME(t(n)) = \{L : L \mathrm{ is decided by a deterministic TM in O(t(n))\}.
Definition. \mathrm{NTIME(t(n)) = \{L : L \mathrm{ is decided by a nondeterministic TM in O(t(n))\}.
6.2 The Class P
\mathrm{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
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. \mathrm{P \subseteq \mathrm{NP.
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 B \in \mathrm{NPThen A \in \mathrm{NP.
Proof. Let be the polynomial-time verifier for and be the polynomial-time reduction. Then is a polynomial-time verifier for .
Open question: \mathrm{P = \mathrm{NP? This is the most important open problem in computer Science. Most researchers believe \mathrm{P \neq \mathrm{NP.
Consequences of P = NP. If \mathrm{P = \mathrm{NPThen 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 \mathrm{P \neq \mathrm{NP.
6.4 NP-Completeness
A language is NP-complete if:
- B \in \mathrm{NPAnd
- for every A \in \mathrm{NP (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). \mathrm{SAT is NP-complete.
Proof (detailed sketch).
\mathrm{SAT \in \mathrm{NP: a satisfying assignment is a polynomial-size certificate that can be verified in polynomial time.
For any L \in \mathrm{NPThere 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 q_{\mathrm{accept}.
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 q_{\mathrm{accept}.
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 B \in \mathrm{PThen A \in \mathrm{P.
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
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
Definition. \mathrm{SPACE(s(n)) is the class of languages decidable by a deterministic TM Using tape cells. \mathrm{NSPACE(s(n)) is the nondeterministic analogue.
Key classes:
- \mathrm{L = \mathrm{SPACE(\log n) — logarithmic space.
- \mathrm{NL = \mathrm{NSPACE(\log n) — nondeterministic logarithmic space.
- \mathrm{PSPACE = \bigcup_{k \geq 1} \mathrm{SPACE(n^k).
Theorem 6.6 (Savitch, 1970). \mathrm{NSPACE(s(n)) \subseteq \mathrm{SPACE(s(n)^2) 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 \mathrm{REACH(u, v, d) (can reach in at most steps?), which uses space and recurses to depth . Total space: .
Corollary 6.7. \mathrm{PSPACE = \mathrm{NPSPACE.
Proof. \mathrm{NPSPACE \subseteq \bigcup_k \mathrm{NSPACE(n^k) \subseteq \bigcup_k \mathrm{SPACE(n^{2k}) = \mathrm{PSPACE by Savitch’s theorem.
NL-completeness. A language is NL-complete if B \in \mathrm{NL and every A \in \mathrm{NL is log-space reducible to .
Theorem 6.8 (Immerman—Szelepcsényi, 1987). \mathrm{NL = \mathrm{coNL.
This is surprising because it is not known whether \mathrm{NP = \mathrm{coNP. 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 L \in \mathrm{PSPACE 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 (0 \lor y) \land (1 \lor y) = y \land \mathrm{true = y. So we need (which exists).
- For : need such that (1 \lor y) \land (0 \lor y) = \mathrm{true \land y = y. So we need (which exists).
Since both values of lead to a satisfying assignment for , is true.
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:
\mathrm{P \subseteq \mathrm{NP \subseteq \Sigma_2^P \subseteq \Sigma_3^P \subseteq \cdots \subseteq \mathrm{PH \subseteq \mathrm{PSPACE
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
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:
\mathrm{PSPACE = \bigcup_{k \geq 1} \mathrm{SPACE(n^k)
- .
- (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:
\mathrm{EXPTIME = \bigcup_{k \geq 1} \mathrm{TIME(2^{n^k})
- .
- (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:
\mathrm{Regular \subsetneq \mathrm{CFL \subsetneq \mathrm{Decidable \subsetneq \mathrm{TM\mathrm{-recognisable}
\mathrm{L \subseteq \mathrm{NL \subseteq \mathrm{P \subseteq \mathrm{NP \subseteq \mathrm{PSPACE \subseteq \mathrm{EXPTIME
\mathrm{P \subseteq \mathrm{NP \subseteq \mathrm{PH \subseteq \mathrm{PSPACE
| 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.
:::caution Common Pitfall NP-completeness refers to decision problems. The optimisation versions (e.g., “find the shortest Tour”) are NP-hard, not necessarily NP-complete. Also, “NP” stands for “Nondeterministic Polynomial Time,” not “Not Polynomial time.” Every problem in NP is verifiable in polynomial time; whether all Such problems are solvable in polynomial time is the P vs. NP question. A common error is confusing “NP-hard” with “NP-complete”: NP-hard means at least as hard as all NP problems, but the problem Itself might not be in NP (e.g., the halting problem is NP-hard but undecidable).
:::