Theory of Computation
1. Introduction
1.1 Why Theory Matters
The theory of computation provides the mathematical foundations for computer science. It answers Fundamental questions: What can be computed? What cannot? How efficiently? These answers guide the Design of algorithms, programming languages, compilers, and hardware.
1.2 Alphabets, Strings, and Languages
An alphabet is a finite, non-empty set of symbols (e.g., ).
A string (or word) over is a finite sequence of symbols from . The empty string Is denoted .
Notation:
- : The set of all strings over (including ).
- : The set of all non-empty strings over .
- : The length of string . .
- : The reversal of .
- : Strings of length over .
A language over is any subset of . The empty language is (distinct from Which contains one string).
Operations on languages:
- Union: .
- Intersection: .
- Concatenation: .
- Kleene star: .
- Complement: .
1.3 Examples of Formal Languages
Formal languages arise throughout computer science. Here are representative examples Organised by complexity class.
Finite languages (always regular):
- — the set of Boolean literals.
- — all binary strings of length at most 3.
Regular languages (decidable by finite automata):
- .
- .
- .
Context-free but not regular:
- — matching counts of two symbols.
- — even-length palindromes.
- — equal numbers of
aS andbS.
Decidable but not context-free:
- .
- .
Undecidable (Turing-recognisable):
- — the acceptance problem.
- \mathrm{HALT_}{\mathrm{TM} = \{\langle M, w \rangle : M \mathrm{ halts} on w\}}.
Not even Turing-recognisable:
- — the complement of the acceptance problem.
This hierarchy illustrates the central theme of the course: as we move to more expressive language Classes, the corresponding machines become more powerful, but certain properties (decidability, Closure under complement) may be lost.
1.4 Countability and Cardinality
An important foundational observation is that the set of all languages over is uncountable, while the set of all Turing machines (and hence the set of all decidable Languages) is countable.
Theorem 1.1. The set of all languages over a non-empty alphabet is uncountable.
Proof. The set is countable (enumerate strings by length, then lexicographically). The set of all languages is Which is uncountable by Cantor”s theorem (since for any set ).
Theorem 1.2. The set of all Turing machines is countable.
Proof. Each TM has a finite description (its states, alphabet, and transition function). Encode This as a string over a finite alphabet. The set of all finite strings is countable.
Corollary 1.3. There exist languages that are not Turing-recognisable (in fact, uncountably Many such languages).
This follows because there are uncountably many languages but only countably many TMs. The set Of Turing-recognisable languages is a countable subset of the uncountable set of all languages.
Cantor’s diagonalisation. The classic proof of Theorem 1.1 uses diagonalisation: assume the Set of all languages is countable, list them as And construct a language that differs from each on the -th string. Then is not in the list — contradiction. This technique reappears in the proof of undecidability of (Section 5.2).
2. Regular Languages
2.1 Finite Automata
A deterministic finite automaton (DFA) is a 5-tuple where:
- is a finite set of states.
- is the input alphabet.
- is the transition function.
- is the start state.
- is the set of accepting (final) states.
accepts a string if the sequence of states ends in a state in .
Example. DFA for strings over containing the substring 01:
| State | ||
|---|---|---|
A nondeterministic finite automaton (NFA) allows multiple transitions from a state on the same Symbol, and -transitions (transitions without consuming input). Formally, .
accepts if there exists some path of transitions consuming that ends in a state in .
Worked Example: DFA for binary numbers divisible by 3
Design a DFA over that accepts exactly those strings (interpreted as binary Numbers, most significant bit first) that are divisible by 3.
We track the value of the number read so far, modulo 3. After reading prefix Let . Reading a new bit appends to the right: the new value is .
States: (remainder 0), (remainder 1), (remainder 2). Start state: (the empty prefix has value 0). Accept state: .
| State | ||
|---|---|---|
Correctness. By induction on input length. Base: , DFA is in . Step: if after the DFA is in (where ), Then reading moves to Which equals .
Worked Example: NFA for strings ending in `01`
Construct an NFA over that accepts strings ending in the substring 01.
States: (start), (have seen a 0), (accept, have seen 01).
| State | ||
|---|---|---|
Accept: . The NFA is nondeterministic because from on input 0It can either stay In or move to .
2.2 Equivalence of DFA and NFA
Theorem 2.1. For every NFA There exists a DFA such that .
Proof (subset construction). Given NFA Construct DFA where:
- (each state of is a subset of states of ).
- .
- for .
- .
Every string accepted by has some path through ‘s states. The subset construction tracks all Possible states could be in after reading each symbol. accepts exactly when at least one Possibility in leads to acceptance.
Corollary 2.2. NFA, DFA, and NFA with -transitions all recognise exactly the class of Regular languages.
Worked Example: Subset construction (NFA to DFA)
Convert the NFA from the “strings ending in 01” example to a DFA via the subset construction.
NFA states: , , No -transitions.
Start state: .
| DFA State | Accept? | ||
|---|---|---|---|
| No | |||
| No | |||
| Yes |
The DFA has 3 reachable states. , And are unreachable.
2.3 Regular Expressions
Definition. Regular expressions over are defined inductively:
- (empty set), (empty string), and for each are regex.
- If and are regex, then , And are regex.
- Nothing else is a regex.
Shorthand: means . means .
Examples:
- : strings containing
00. - : strings with exactly two
0S. - : all strings over .
- : the empty language.
Regex identities. The following algebraic laws hold for regular expressions and can be used To simplify or reason about them:
| Identity | Description |
|---|---|
| Annihilator for concatenation | |
| Identity for concatenation | |
| Identity for union | |
| Kleene star of empty set | |
| Kleene star of empty string | |
| Idempotence of Kleene star | |
| Associativity of concatenation | |
| Commutativity of union | |
| Associativity of union | |
| Idempotence of union | |
| Distributivity |
These identities can be used to prove that two regex describe the same language, analogous to Simplifying algebraic expressions. Two regex and are equivalent (written ) If .
2.4 Equivalence of Regular Expressions and Finite Automata
Theorem 2.3 (Kleene). A language is regular if and only if it is described by some regular Expression.
Proof (sketch).
Regex to NFA: Induction on the structure of the regex.
- Base cases: , , each have trivial NFAs.
- Union: Combine two NFAs with a new start state and -transitions to each.
- Concatenation: Connect the accept states of the first NFA to the start state of the second via -transitions.
- Kleene star: Add -transitions from a new start state to the NFA’s start and from the NFA’s accept states back to the start state.
NFA to regex: State elimination. Systematically eliminate states of the NFA one at a time, Updating the transition labels (which are regex) between remaining states. The final label from start To accept is the equivalent regex.
Thompson’s construction. The regex-to-NFA translation can be made fully explicit. For each Sub-expression of the regex, we build a small NFA fragment with exactly one entry and one exit State, connected by -transitions. The construction guarantees that the NFA has at most One accept state, no transitions into the start state, and no transitions out of the accept state.
Theorem 2.3a (Thompson’s construction correctness). For every regular expression over Thompson’s construction produces an NFA with And has States and transitions.
Proof. By structural induction on .
Base cases:
: has a start state and an accept state with no transitions. .
: has a start state, an accept state, and an -transition between them. .
for : has a start state, an accept state, and a transition on . .
Inductive cases:
: By IH, and . Thompson adds a new start and accept with -transitions to the start states of and And -transitions from their accept states to . Any accepting path goes through exactly one sub-NFA, so .
: Thompson connects the accept state of to the start state of via -transitions. A string iff where and I.e., .
: Thompson adds a new start and accept With -transitions from to (allowing zero repetitions) and from to the start of And from the accept of back to . Any accepting path corresponds to zero or more traversals of So .
In all cases, the number of states added is a constant per operator, so .
2.5 DFA Minimisation and the Myhill-Nerode Theorem
Theorem 2.4 (Myhill-Nerode). The following are equivalent for a language :
- is regular.
- The Myhill-Nerode equivalence relation has finitely many equivalence classes.
- is the union of some of the equivalence classes.
Definition. Two strings are distinguishable with respect to if there exists Such that exactly one of is in . The Myhill-Nerode equivalence is: iff and are not distinguishable.
Proof of Theorem 2.4.
(1) (2): Let be a DFA for . Define iff (i.e., reaches the same state on and ). Then has at most equivalence classes. We show . If Then for All , So iff Meaning . Conversely, if There exists with and (or vice versa), so Hence .
(2) (3): Trivial, since consists of all strings whose equivalence class is one That contains at least one string in .
(3) (1): Suppose has finitely many equivalence classes . Construct a DFA with one state per equivalence class, start state for Transition And accept states = those classes contained in . This DFA is Well-defined because is a right congruence: if Then for all .
Worked Example: Myhill-Nerode classes for $L = \{0^n 1^n : n \geq 0\}$
We show is not regular by exhibiting infinitely many pairwise distinguishable strings.
Claim: the strings are pairwise distinguishable with respect to .
Proof. For with Consider the suffix . Then:
- .
- (since ).
Therefore . Since there are infinitely many such strings, has Infinitely many equivalence classes, so is not regular by the Myhill-Nerode theorem.
This proof technique is often cleaner than the pumping lemma, as it avoids case analysis over Decompositions.
Corollary 2.5. The minimum-state DFA for has exactly as many states as there are Equivalence classes of .
Minimisation algorithm:
1. Start with two partitions: F (accepting states) and Q \ F (non-accepting).2. Repeatedly refine: for each pair (p, q) in the same partition, if delta(p, a) and delta(q, a) are in different partitions for some a, split p and q into different partitions.3. Repeat until no further splitting occurs.4. Each partition becomes a state in the minimised DFA.This algorithm runs in time where is the number of states and .
2.6 Pumping Lemma for Regular Languages
Theorem 2.6 (Pumping Lemma). If is regular, then there exists a constant (the pumping Length) such that for every string with , can be decomposed as satisfying:
- (the pumped part is non-empty).
- (the pumpable part is within the first symbols).
- for all .
Proof. Let be a DFA for with states. Set . For any string of length The path through visits at least states, so by the pigeonhole principle, some state is Visited twice within the first symbols. The substring between the two visits can be repeated Or removed () without affecting acceptance.
Using the pumping lemma to prove non-regularity. To show is not regular, assume it is, let Be the pumping length, and exhibit a string with such that every Decomposition satisfying (1) and (2) has some with .
Example. is not regular.
Proof. Assume is regular with pumping length . Let . By (2), So consists only of 0S. Let . Then (since ). Contradiction.
Example. is not regular.
Proof. Assume pumping length . Let . Since , for Some . Then (the two halves have different lengths).
Example. is not regular.
Proof. Assume is regular with pumping length . Since regular languages are closed under Complement, would also be regular. Then would be regular (since is regular And regular languages are closed under intersection). But is not regular — Contradiction.
Worked Example: Proving $\{w : n_0(w) = n_1(w)\}$ is not regular
Let .
Proof. Assume is regular with pumping length . Let . By (2), So for some . Then Which has zeros and ones. Since So . Contradiction.
Worked Example: $L = \{a^{n!} : n \geq 0\}$ is not regular
Proof. Assume pumping length . Let with . By (2), So for some . Choose (an integer since ). Then . But is not a factorial for (since for And for any ). Hence .
:::caution Common Pitfall The pumping lemma says that for every decomposition satisfying (1) and (2), there exists a Pumping that fails. To prove non-regularity, you must show that all valid decompositions lead To a contradiction. A single decomposition that works is insufficient to disprove the lemma. The converse of the pumping lemma is false: if a language satisfies the pumping condition, it is Not necessarily regular. :::
2.7 Closure Properties of Regular Languages
Regular languages are closed under:
| Operation | Proof technique |
|---|---|
| Union | NFA union construction |
| Intersection | DFA product construction |
| Complement | Swap accepting and non-accepting states |
| Concatenation | NFA concatenation construction |
| Kleene star | NFA star construction |
| Reversal | Reverse all transitions, swap start/F |
| Difference | |
| Homomorphism | Apply the homomorphism to each symbol |
| Inverse homomorphism | Pre-image construction |
Theorem 2.7 (Intersection via product construction). If and are regular, then is regular.
Proof. Let and . Construct where . Then accepts iff both and accept I.e., .
Theorem 2.7. If is regular and is not regular, then may or may not be Regular. Closure properties do not apply when one operand is non-regular.
3. Context-Free Languages
3.1 Context-Free Grammars
A context-free grammar (CFG) is a 4-tuple where:
- is a finite set of variables (non-terminals).
- is a finite set of terminals.
- is a finite set of production rules.
- is the start variable.
A production means variable can be replaced by string .
Example. CFG for :
Example. CFG for balanced parentheses:
Example. CFG for (palindromes):
Example. CFG for :
The variable generates the part, and generates the part. Since The total number Of bS and cS equals the total number of aS.
Example. CFG for strings with equal numbers of 0S and 1S:
The first two rules add a matched pair (in either order); the third concatenates two balanced Strings; the fourth handles the empty string.
Example. CFG for the language of all strings over that are not palindromes:
Here generates non-palindromes and generates arbitrary strings of length .
Worked Example: CFG for $\{a^i b^j : 2i = j \mathrm{ or} 2j = i\}$
generates and generates . Their union is the desired Language.
Proof of correctness for . By induction: . So . Similarly . Hence Which is exactly the target language.
3.2 Parse Trees and Ambiguity
A parse tree (derivation tree) for a string according to grammar is a tree where:
- The root is labelled .
- Internal nodes are labelled with variables.
- Leaves are labelled with terminals (or ).
- Children of a node labelled are the symbols of the right-hand side of a production in left-to-right order.
A CFG is ambiguous if some string in its language has two or more distinct parse trees (equivalently, Two or more leftmost derivations or two or more rightmost derivations).
Example (ambiguous). . The string has two parse trees: and .
Removing ambiguity. Some ambiguous grammars can be made unambiguous by rewriting the productions To enforce a particular evaluation order. For the arithmetic expression grammar:
This grammar is unambiguous and enforces the standard precedence ( before ) and left Associativity.
Worked Example: Proving a grammar is unambiguous
Consider the grammar: . We prove it is unambiguous.
Proof. Any string in has the form for some . We show by induction On that has exactly one parse tree.
Base (): has only the parse tree using .
Inductive step: must use (the rule produces only Strings of length 2). The inner must derive Which by IH has a unique parse tree. Hence has exactly one parse tree.
Example. is inherently ambiguous. (Proof omitted; Any grammar must have two competing mechanisms for the two conditions, and these Interfere.)
3.3 Chomsky Normal Form
A CFG is in Chomsky Normal Form (CNF) if every production is of the form:
- (two variables), or
- (single terminal), or
- (only the start variable, and does not appear on the right of any rule).
Theorem 3.1. Every CFG has an equivalent CFG in Chomsky Normal Form.
Proof (conversion algorithm).
- Add a new start variable with rule (ensures does not appear on any right side if it generates ).
- Remove all -productions (except possibly ). For each occurrence of in a right-hand side, add a variant without .
- Remove all unit productions . For each chain Add for each .
- Convert remaining rules: replace terminals in right-hand sides of length with new variables and rules . Replace right-hand sides of length by introducing intermediate variables.
Worked Example: Converting a CFG to CNF
Convert the following grammar to CNF:
Step 1: Add new start variable. .
Step 2: Remove -productions. Both and are nullable. For each production Containing or Add variants with nullable symbols removed:
Step 3: Remove unit productions. is a unit production. Replace with all -productions:
Step 4: Convert long right-hand sides and terminals.
Introduce and :
Handle (length 3): introduce Then and .
Final CNF grammar:
3.4 Pushdown Automata
A pushdown automaton (PDA) is a 6-tuple where:
- is a finite set of states.
- is the input alphabet.
- is the stack alphabet.
- is the transition function.
- is the start state.
- is the set of accepting states.
A PDA can read input, push symbols onto the stack, pop symbols from the stack, and make -moves (without consuming input or changing the stack).
Example. PDA for :
- Push a marker \text{\}$ onto the stack.
- For each
aPushXonto the stack. - For each
bPopXfrom the stack. - Accept if the stack is empty (just the marker) and all input is consumed.
Formally: states (start), (pushing), (popping), (accept).
Worked Example: PDA for palindromes $\{ww^R : w \in \{0,1\}^*\}$
Design a PDA for .
Idea: Push the first half of the input onto the stack. Nondeterministically guess when the Middle is reached, then pop and compare with the remaining input.
States: (pushing), (popping/comparing), (accept).
Transitions:
Push phase ():
— push
0.— push
1.— guess the midpoint.
Pop phase ():
— match
0.— match
1.— accept if stack empty and input consumed.
Accept: .
Correctness. If The PDA pushes Guesses the midpoint, then pops and matches . If No sequence of guesses leads to acceptance.
3.5 Equivalence of CFG and PDA
Theorem 3.2. A language is context-free if and only if some PDA recognises it.
Proof (sketch).
CFG to PDA: The PDA simulates a leftmost derivation. It maintains the current sentential form On the stack. At each step, it pops a variable from the stack and pushes the right-hand side of a Production for that variable.
PDA to CFG: Given PDA Construct a grammar whose variables encode pairs of states Meaning “the PDA can go from state to state Popping everything pushed onto the stack.” The productions simulate the PDA’s transitions.
Theorem 3.2a (CFG to PDA construction). Let be a CFG. Then there exists a PDA with .
Proof. Construct M = (\{q_0, q_1, q_2\}, \Sigma, V \cup \Sigma \cup \{\}, \delta, q_0, {q_2})$:
- (q_0, \varepsilon, \varepsilon) \to (q_1, S\)$ — push the start variable and bottom marker.
- For each : — replace a variable with its production.
- For each : — match a terminal on input with stack.
- (q_1, \varepsilon, \) \to (q_2, $)$ — accept when only the marker remains.
The PDA maintains the current sentential form (minus terminals already matched) on the stack. When only \text{\}\blacksquare$
3.6 Pumping Lemma for Context-Free Languages
Theorem 3.3. If is context-free, there exists such that for every with , can be decomposed as satisfying:
- .
- .
- for all .
Proof. Let be a CFG in CNF with variables. Any parse tree of height generates a string Of length at most . Set . For The parse tree has height So some path repeats a variable. The substring generated between the two occurrences can be pumped.
Example. is not context-free.
Proof. Assume pumping length . Let . Since The substring cannot span all three letter types. Case analysis:
- If is within the
aS orbS orcS: pumping changes only one count, breaking the equality. - If spans
aS andbS: pumping changes theaandbcounts but notcBreaking equality. - If spans
bS andcS: analogous.
In all cases, .
Example. is not context-free.
Proof. Assume pumping length . Let . Since :
- If lies entirely within one block: pumping up () increases only one count, but the gap between adjacent counts is only 1 or 2, so doubling the pumped count violates the strict inequalities.
- If spans two blocks: pumping changes two adjacent counts by the same additive amount. The gap between those counts is 1, so increasing both by the same positive amount makes them equal, violating .
In all cases, .
Worked Example: $\{a^n b^n a^n : n \geq 0\}$ is not context-free
Proof. Assume pumping length . Let . Since The substring cannot span all three blocks. Case analysis:
- within the first block: pumping down () reduces the first count only. for .
- within the block: pumping changes the count only, breaking equality.
- within the last block: analogous.
- spans the first and : pumping changes first and counts but not the last count.
- spans and last : analogous.
In all cases, pumping produces a string not in .
3.7 The CYK Parsing Algorithm
The Cocke—Younger—Kasami (CYK) algorithm determines membership in a context-free language When the grammar is in Chomsky Normal Form.
Theorem 3.4. Given a CFG in CNF and a string of length The CYK algorithm decides Whether in time.
Algorithm. Construct a table for Where is the set Of variables that can derive the substring .
- Base case (): T[i, i] = \{A : A \to w_i \mathrm{ is a rule in G\}.
- Recursive case (): For each split with :
- Answer: iff .
Proof of correctness. In CNF, every derivation of a string of length involves exactly binary productions. The table considers every possible “root” of the derivation tree For each substring, and every possible split of that substring into two parts. By induction on the Substring length, contains exactly those variables that derive .
Time complexity. The table has entries. Each entry considers at most split points And checks all rules, giving total time.
Worked Example: CYK on a small grammar
Grammar (CNF): , , , .
String: .
Length 1:
- : So (since ).
- : So (since and ).
Length 2:
- : split at . Check all pairs :
- : ? No. ? No. ? No. ? No.
- : ? Yes — add .
- : already checked. So .
Since The string is in . The parse tree is Where and .
4. Turing Machines
4.1 Definition
A Turing machine (TM) is a 7-tuple where:
- is a finite set of states.
- is the input alphabet (does not contain the blank symbol ).
- is the tape alphabet, , .
- is the transition function.
- is the start state.
- q_{\mathrm{accept} \in Q is the accept state.
- q_{\mathrm{reject} \in Q is the reject state (q_{\mathrm{accept} \neq q_{\mathrm{reject}).
The TM has an infinite tape (initially containing the input followed by blanks), a read/write head That moves left or right, and a finite control. At each step, based on the current state and symbol Under the head, it writes a symbol, moves the head, and changes state.
accepts input if the computation halts in q_{\mathrm{accept}. rejects if It halts in q_{\mathrm{reject}. loops if it never halts.
The language recognised by is L(M) = \{w : M \mathrm{ accepts w\}.
A language is Turing-recognisable (recursively enumerable) if some TM recognises it. A language Is decidable if some TM decides it (halts on all inputs, accepting or rejecting).
4.2 TM Variants
Multitape TMs. Multiple tapes, each with its own head. The transition function maps to .
Theorem 4.1. Every multitape TM has an equivalent single-tape TM.
Proof. Simulate tapes on a single tape using tracks (or interleaving symbols with Delimiters). Simulating one step of the multitape TM requires scanning the single tape to read all heads and update them, costing steps per simulated step.
Nondeterministic TMs. . The TM accepts if some computation path accepts.
Theorem 4.2. Every nondeterministic TM has an equivalent deterministic TM.
Proof. Simulate the NTM using breadth-first search on the computation tree. Each level of the tree Has at most branches (where is the maximum number of choices). After steps, the tree has At most nodes. The simulation visits nodes in order, using a 3-tape DTM: tape 1 stores the Input, tape 2 simulates the current branch, tape 3 tracks the address of the current node in the Tree. The simulation runs in times the NTM’s time, which is exponential overhead.
Enumerators. A TM with a printer that outputs a list of strings. A language is Turing-recognisable If and only if some enumerator enumerates it.
Theorem 4.2a. A language is Turing-recognisable if and only if some enumerator enumerates it.
Proof. () Given TM recognising Construct an enumerator that dovetails: Runs on for 1 step, then on and on for 2 steps, then on for 3 steps, and so on. Whenever accepts, prints that String. Every string in is eventually printed.
() Given enumerator for Construct TM that on input runs and checks Each printed string against . If is printed, accept. If It will eventually be Printed, so recognises .
4.3 Church-Turing Thesis
Theorem 4.3 (Church-Turing Thesis). A function is computable (can be computed by any reasonable Model of computation) if and only if it is computable by a Turing machine.
This is a thesis, not a theorem — it cannot be formally proved because “reasonable model” is not Formally defined. However, all proposed models of computation (lambda calculus, recursive functions, Post systems, cellular automata, modern programming languages) have been shown equivalent to Turing Machines.
Implication: To show a problem is uncomputable, it suffices to show no TM can solve it.
Evidence supporting the thesis:
- Robustness. Every “reasonable” model of computation proposed since the 1930s has been shown to compute exactly the Turing-computable functions.
- Invariance. Adding resources (more tapes, nondeterminism, random access) does not increase the class of computable functions.
- Empirical adequacy. No physically realisable process has been shown to compute a non-Turing-computable function.
- Universality. The existence of universal Turing machines (Section 4.5) shows that a single fixed machine can simulate any other, reinforcing the notion of a canonical model.
Limitations. The Church-Turing thesis concerns computability in principle (unlimited time and Space). It does not address feasibility, which is the domain of complexity theory (Section 6). Quantum computers do not compute non-Turing-computable functions; they may offer speedups for Specific problems but remain within the Turing-computable class.
4.4 TM Construction Examples
Worked Example: TM for $L = \{0^n 1^n : n \geq 0\}$
Design a single-tape TM that decides .
Idea: Repeatedly cross off one 0 and one 1. If all symbols are crossed off, accept.
M = (Q, \{0, 1\}, \{0, 1, \mathrm{x, \sqcup\}, \delta, q_0, q_{\mathrm{accept}, q_{\mathrm{reject}).
Key transitions:
- In Read
0: writexMove right, go to . (Cross off a0.) - In Read
1: reject. (A1before any0.) - In Read : accept. (Nothing left.)
- In Read
0: move right, stay in . (Skip remaining0S.) - In Read
1: writexMove left, go to . (Cross off a1.) - In Read : reject. (No
1to match.) - In Read
0orx: move left, stay in . (Scan back.) - In Read : move right, go to . (Return to start.)
Correctness. Each iteration crosses off exactly one 0 and one 1. If the input is The machine performs iterations and accepts. If counts differ or the pattern is violated, The machine rejects.
Worked Example: TM that decides $A_{\mathrm{DFA}$
Design a TM that decides A_{\mathrm{DFA} = \{\langle B, w \rangle : B \mathrm{ accepts w\}.
Construction. On input where :
- Simulate on . Maintain the current state and position in .
- At each step, look up in ‘s transition table (encoded on the tape).
- Update and . If when Accept.
- If and Reject.
The simulation takes steps and always halts.
4.5 The Universal Turing Machine
A universal Turing machine (UTM) is a Turing machine that can simulate any other Turing Machine on any input . The input to is an encoding .
Theorem 4.4. A universal Turing machine exists.
Proof (construction sketch). Fix an encoding scheme for TMs and their inputs as strings over a Finite alphabet. The UTM uses multiple tapes:
- Tape 1: Stores the description of the simulated TM (the “program”).
- Tape 2: Stores the current contents of ‘s tape (the “memory”).
- Tape 3: Stores the current state of and the position of ‘s head.
At each step, reads the current state and tape symbol from tapes 3 and 2, scans tape 1 to Find the matching transition in ‘s description, updates tape 2 (write symbol), tape 3 (state And head position), and repeats. Since ‘s description is finite and each step requires a finite Scan, correctly simulates .
Significance. The UTM is the theoretical foundation for the stored-program computer: hardware (the UTM) is fixed, and software (the encoded TM) provides the specific computation.
Simulation overhead. If runs in time Then simulates in time Where is the size of ‘s description.
Worked Example: TM for $L = \{w\#w^R : w \in \{0,1\}^*\}$
Design a TM that decides .
Idea: Repeatedly check the first and last non- symbols, then cross them off. When only remains, accept.
Algorithm:
- Scan right to find the rightmost non-Non-\mathrm{x symbol (call it ). If we cross on the way, note its position.
- Return to the leftmost non-Non-\mathrm{x symbol (call it ).
- If Reject.
- Cross off both and (write \mathrm{x).
- Repeat until only (and \mathrm{xS) remain, then accept.
Correctness. If the input is The first symbol of equals the last symbol of (which is the first symbol of ), the second equals the second-to-last, etc. Each Iteration verifies one pair. If any pair mismatches, the string is not of the form .
5. Decidability
5.1 Decidable Languages
Theorem 5.1. The following languages are decidable:
- A_{\mathrm{DFA} = \{\langle B, w \rangle : B \mathrm{ is a DFA that accepts w\}.
- E_{\mathrm{DFA} = \{\langle B \rangle : B \mathrm{ is a DFA with L(B) = \emptyset\}.
- \mathrm{EQ_{\mathrm{DFA} = \{\langle A, B \rangle : A, B \mathrm{ are DFAs with L(A) = L(B)\}.
- A_{\mathrm{CFG} = \{\langle G, w \rangle : G \mathrm{ is a CFG that generates w\}.
Proof (for A_{\mathrm{DFA}). Simulate on input . This takes steps and always halts.
Proof (for E_{\mathrm{DFA}). Mark the start state. Repeatedly mark states reachable by Transitions from already-marked states. After no more states can be marked, check if any accept state Is marked. If not, .
Proof (for \mathrm{EQ_{\mathrm{DFA}). Use the product construction to build a DFA for . Test if this DFA accepts any string (using the algorithm for E_{\mathrm{DFA}).
Additional decidable problems:
- A_{\mathrm{REX} = \{\langle R, w \rangle : R \mathrm{ is a regex and w \in L(R)\} — convert to a DFA, then decide A_{\mathrm{DFA}.
- E_{\mathrm{CFG} = \{\langle G \rangle : L(G) = \emptyset\} — test all derivations up to length .
- \mathrm{INF_{\mathrm{CFL} = \{\langle G \rangle : L(G) \mathrm{ is infinite\} — check if any variable has a self-embedding derivation.
5.2 The Halting Problem
Theorem 5.2. A_{\mathrm{TM} = \{\langle M, w \rangle : M \mathrm{ is a TM and M \mathrm{ accepts w\} Is Turing-recognisable but undecidable.
Proof of recognisability. Simulate on . If accepts, accept. If rejects, reject. This recognises A_{\mathrm{TM}.
Proof of undecidability (by contradiction). Assume a decider for A_{\mathrm{TM} exists. Construct a TM that on input :
- Run on .
- If accepts, reject.
- If rejects, accept.
Consider on input :
- If accepts Then by construction rejects . Contradiction.
- If rejects Then by construction accepts . Contradiction.
Therefore cannot exist.
Theorem 5.2a. \overline{A_{\mathrm{TM}} is not Turing-recognisable.
Proof. If \overline{A_{\mathrm{TM}} were Turing-recognisable, then since A_{\mathrm{TM} is Also Turing-recognisable, A_{\mathrm{TM} would be decidable (run both recognisers in parallel; one Must accept). But A_{\mathrm{TM} is undecidable. Contradiction.
5.3 Reductions and Undecidability
A reduction from language to language is a computable function that maps instances of to instances of such that .
Theorem 5.3. If and is decidable, then is decidable.
Proof. To decide on input : compute Then decide on . Since both steps are Computable, is decidable.
Corollary 5.4. If and is undecidable, then is undecidable.
Applications. Using reductions from A_{\mathrm{TM}We can prove many problems undecidable:
| Language | Description | Reduction from |
|---|---|---|
| \mathrm{HALT_{\mathrm{TM} | \{M, w : M \mathrm{ halts on w\} | A_{\mathrm{TM} |
| E_{\mathrm{TM} | A_{\mathrm{TM} | |
| \mathrm{REGULAR_{\mathrm{TM} | \{M : L(M) \mathrm{ is regular\} | A_{\mathrm{TM} |
| \mathrm{EQ_{\mathrm{TM} | E_{\mathrm{TM} |
Example reduction. A_{\mathrm{TM} \leq_m \mathrm{HALT_{\mathrm{TM}.
Proof. Given Construct a TM that on input : simulates on . If accepts, accept. If rejects, loop. Then \langle M, w \rangle \in A_{\mathrm{TM} iff halts on some input (any input), iff \langle M' \rangle \in \mathrm{HALT_{\mathrm{TM}.
Worked Example: $A_{\mathrm{TM} \leq_m E_{\mathrm{TM}$
Proof. Given Construct a TM that on input :
- Simulate on .
- If accepts Accept .
- If rejects Reject .
Then if accepts And if does not accept .
Therefore: \langle M, w \rangle \in A_{\mathrm{TM} iff Iff \langle M_w \rangle \notin E_{\mathrm{TM}.
The reduction is computable. So if E_{\mathrm{TM} Were decidable, \overline{E_{\mathrm{TM}} would be decidable, and hence A_{\mathrm{TM} Would be decidable — contradiction.
Worked Example: $E_{\mathrm{TM} \leq_m \mathrm{EQ_{\mathrm{TM}$
Proof. Given Construct two TMs:
- : on any input, immediately rejects. So .
- : on any input, simulates and accepts iff accepts. So .
Then iff .
Therefore \langle M \rangle \in E_{\mathrm{TM} iff \langle M_1, M_2 \rangle \in \mathrm{EQ_{\mathrm{TM}. If \mathrm{EQ_{\mathrm{TM} were decidable, E_{\mathrm{TM} would be decidable — contradiction.
5.4 Rice’s Theorem
Theorem 5.5 (Rice’s Theorem). Every non-trivial property of the language recognised by a Turing Machine is undecidable.
A property is a set of Turing-recognisable languages. It is non-trivial if is neither Empty nor the set of all Turing-recognisable languages.
Proof (sketch). Let be a non-trivial property. Since is non-trivial, there exists a TM with and a TM with . Given an arbitrary TM and input Construct that on input : first simulates on Then simulates on . If accepts Then ; if does not accept , . If Then iff accepts So deciding would decide . The case is similar.
Corollary. The following are undecidable: “Does accept at least one string?”, “Is Finite?”, “Is regular?”, “Is context-free?”
:::caution Common Pitfall Rice’s theorem applies only to properties of the language Not properties of the machine itself. For example, “Does halt within 100 steps on input ?” is a property Of ‘s behaviour, not of And is in fact decidable (just simulate for 100 steps). :::
5.5 Post Correspondence Problem
Definition. An instance of the Post Correspondence Problem (PCP) consists of two lists of Strings and over some Alphabet . A solution is a non-empty sequence of indices such That:
The PCP language is .
Example. , . The sequence gives and — not equal. The sequence gives and — not equal. This instance may or may not have a solution; determining this is undecidable .
Worked Example: A solvable PCP instance
, .
Try the sequence :
- Top:
- Bottom:
Not equal.
Try :
- Top:
- Bottom:
Not equal. Finding solutions to PCP instances can be very difficult — there is no general algorithm.
Theorem 5.6. PCP is undecidable.
Proof (sketch). Reduce from . Given TM and input Construct a PCP instance Whose tiles encode the computation history of on . The tiles are designed so that a matching Sequence corresponds to a valid accepting computation: the first tile starts the computation, middle Tiles enforce that each configuration follows from the previous by a valid transition, and the last Tile allows termination only if an accept state is reached. Thus the PCP instance has a solution iff accepts . The construction is computable, so if PCP were decidable, would Be decidable — contradiction.
Modified PCP (MPCP). In the modified version, the first tile used must be tile 1. MPCP is also Undecidable, and the reduction from PCP to MPCP adds a “prefix” tile that forces tile 1 to be used First.
5.6 Oracle Machines and the Arithmetical Hierarchy
An oracle machine is a Turing machine with access to an oracle for a language . In addition to its ordinary transitions, may enter a special “query state,” write a string on a query tape, and enter an “answer state” where the tape Contains 1 if and 0 if . The oracle answers in one step.
Definition. for a fixed oracle TM and oracle .
Theorem 5.7. There exists an oracle such that And an oracle such That .
This result (Baker—Gill—Solovay, 1975) shows that resolving will require Non-relativising techniques — proof methods that do not carry over in the presence of oracles.
The Turing jump. Given a language Define the halting problem relative to :
A' = \{\langle M^A, w \rangle : M^A \mathrm{ accepts w\}
Theorem 5.8. (i.e., is strictly more difficult than under Turing Reductions).
The arithmetical hierarchy is defined by iterating the jump: . Each jump produces a strictly more difficult problem, Yielding an infinite hierarchy of undecidability.
:::caution Common Pitfall A common mistake when using reductions is confusing the direction. To prove is undecidable Using a reduction from a known undecidable problem You need Not . Remember: if and is undecidable, then is undecidable (contrapositive of “if is decidable then is decidable”). Reversing the direction gives a valid implication (“if and is undecidable, then…”) that tells us nothing about . :::
6. 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 proof 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). :::
7. Problem Set
7.1 Regular Languages
Problem 1. Construct a DFA over that accepts exactly those strings whose Length is a multiple of 3. Prove your DFA is correct.
Problem 2. Let . Give a DFA with the minimum number of states for .
Problem 3. Use the Myhill-Nerode theorem to prove that is not regular.
Problem 4. Prove or disprove: if is regular, then both and are regular.
7.2 Context-Free Languages
Problem 5. Give a CFG for . Prove your grammar generates Exactly this language.
Problem 6. Convert the following grammar to CNF: . Show all steps of the conversion.
Problem 7. Use the CFL pumping lemma to prove that is not context-free.
Problem 8. Construct a PDA for . Give the formal definition Of the PDA and explain why it is correct.
7.3 Turing Machines and Decidability
Problem 9. Design a TM that decides the language . Describe the algorithm and prove it always halts.
Problem 10. Prove that the language is undecidable.
Problem 11. Use Rice’s theorem to prove that is undecidable. Explain why Rice’s theorem applies.
Problem 12. Show that the PCP instance with and Has a solution by finding one, or prove it has no solution.
7.4 Complexity Theory
Problem 13. Show that if Then .
Problem 14. A 3-colouring of a graph is a function Such that for every edge . Show that 3-SAT 3-Colouring By describing the reduction construction.
Problem 15. Prove that is self-reducible: given an oracle for Describe a polynomial-time algorithm to find an actual clique of size (if one exists).
Problem 16. Using Savitch’s theorem, prove that . What is the time complexity of your algorithm?
Problem 17. Define the language . Show that is NP-complete.
Problem 18. A language is in DP (difference of two NP sets) if there exist such that . Show that is in DP. Is DP contained in ? Justify.
7.5 Comprehensive
Problem 19. Consider the language . (a) Prove is not regular using the pumping lemma. (b) Give a CFG for and prove it is correct. (c) Is decidable? Justify.
Problem 20. For each of the following languages, state the smallest complexity class (from , \mathrm{CFL, , \mathrm{NP \mathrm{PSPACE, Or “undecidable”) that is known to contain it. Justify each answer briefly.
(a) (b) (c) (d) (e)
7.6 Selected Solutions and Hints
Problem 1. States with transitions for All . Accept state: . Proof by induction on the number of symbols read.
Problem 3. The strings are pairwise distinguishable: for The suffix distinguishes from since but (because ).
Problem 4. Disproof: let (not regular) and (regular). Then is regular, but is not.
Problem 7. Let with pumping length . Since The Substring cannot span all four blocks. Case analysis shows that pumping any valid Decomposition produces a string not in .
Problem 10. Reduce from . Given Construct two TMs (accepts only) and (accepts what accepts). Then iff accepts iff (after adjusting for the specific reduction).
Problem 13. If Then for any We have . Since is closed under complement, . So for every , meaning . By symmetry, .
Problem 19. (a) Let . Since , is in the first block. Pumping down gives . (b) (generate matched pairs on both sides of ). (c) Yes — a TM can check the symbol and verify both halves are reverses of each other.
Problem 20. (a) Decidable but not CFL (pumping lemma for CFLs). (b) NP-complete (Hamiltonian cycle is NP-complete). (c) NP-complete (vertex cover is NP-complete). (d) Decidable (in fact, in P — simulate for 100 steps). (e) PSPACE-complete (TQBF is PSPACE-complete).
Common Pitfalls
Neglecting to normalise database designs, leading to data redundancy and update anomalies.
Confusing an algorithm with a program — an algorithm is a step-by-step procedure, not its implementation in code.
Writing pseudocode that is too language-specific rather than using standard algorithmic constructs.
Mixing up Big O, Big , and Big notation — Big O is an upper bound, not necessarily tight.
Worked Examples
Example 1: DFA Construction
Problem. Design a DFA that accepts all binary strings over where the number of 1s is divisible by 3.
Solution. Three states: (0 mod 3, accept), (1 mod 3), (2 mod 3).
- ,
- ,
- ,
Accept state: . Strings like , , are accepted.
Example 2: Reducing 3-SAT to Independent Set
Problem. Show that Independent Set is NP-complete by reducing from 3-SAT.
Solution. Given a 3-CNF formula with clauses, construct a graph:
- For each clause, create a triangle of 3 vertices (one per literal).
- Add edges between contradictory literals ( and ) across different clauses.
The formula is satisfiable iff the graph has an independent set of size (pick one true literal per clause; no two contradict). Polynomial-time reduction, so Independent Set is NP-hard. Since it’s also in NP, it’s NP-complete.
Summary
- Finite automata: DFA (deterministic, recognition) and NFA (non-deterministic); equivalent in power via subset construction ( states worst case).
- Regular expressions: describe regular languages; pumping lemma proves non-regularity for languages like .
- Context-free grammars: pushdown automata add a stack; CFLs are closed under union but not intersection or complement.
- Turing machines: model general computation; Church-Turing thesis — any “effectively computable” function is TM-computable.
- Complexity: P (polynomial time), NP (polynomial verification), NP-complete (hardest in NP); P vs NP remains open.
Cross-References
| Topic | Site | Link |
|---|---|---|
| Discrete Mathematics | WyattsNotes | View |
| Algorithms and Data Structures | WyattsNotes | View |
| Advanced Algorithms | WyattsNotes | View |
| Theory of Computation — MIT 18.404 | MIT | View |