Skip to content

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 Σ\Sigma is a finite, non-empty set of symbols (e.g., Σ={0,1}\Sigma = \{0, 1\}).

A string (or word) over Σ\Sigma is a finite sequence of symbols from Σ\Sigma. The empty string Is denoted ε\varepsilon.

Notation:

  • Σ\Sigma^*: The set of all strings over Σ\Sigma (including ε\varepsilon).
  • Σ+\Sigma^+: The set of all non-empty strings over Σ\Sigma.
  • w|w|: The length of string ww. ε=0|\varepsilon| = 0.
  • wRw^R: The reversal of ww.
  • Σn\Sigma^n: Strings of length nn over Σ\Sigma.

A language LL over Σ\Sigma is any subset of Σ\Sigma^*. The empty language is \emptyset (distinct from {ε}\{\varepsilon\}Which contains one string).

Operations on languages:

  • Union: L1L2={w:wL1orwL2}L_1 \cup L_2 = \{w : w \in L_1 \mathrm{ or} w \in L_2\}.
  • Intersection: L1L2={w:wL1andwL2}L_1 \cap L_2 = \{w : w \in L_1 \mathrm{ and} w \in L_2\}.
  • Concatenation: L1L2={w1w2:w1L1,w2L2}L_1 \cdot L_2 = \{w_1 w_2 : w_1 \in L_1, w_2 \in L_2\}.
  • Kleene star: L={ε}LL2L^* = \{\varepsilon\} \cup L \cup L^2 \cup \cdots.
  • Complement: L=ΣL\overline{L} = \Sigma^* \setminus L.

1.3 Examples of Formal Languages

Formal languages arise throughout computer science. Here are representative examples Organised by complexity class.

Finite languages (always regular):

  • L1={true,false}L_1 = \{\mathrm{true}, \mathrm{false}\} — the set of Boolean literals.
  • L2={w{0,1}:w3}L_2 = \{w \in \{0,1\}^* : |w| \leq 3\} — all binary strings of length at most 3.

Regular languages (decidable by finite automata):

  • L3={w{0,1}:wcontainsthesubstring101}L_3 = \{w \in \{0,1\}^* : w \mathrm{ contains} the substring 101\}.
  • L4={w{0,1}:whasanevennumberof1s}L_4 = \{w \in \{0,1\}^* : w \mathrm{ has} an even number of 1\mathrm{s}\}.
  • L5={w{0,1}:winterpretedinbinaryisdivisibleby3}L_5 = \{w \in \{0,1\}^* : w \mathrm{ interpreted} in binary is divisible by 3\}.

Context-free but not regular:

  • L6={anbn:n0}L_6 = \{a^n b^n : n \geq 0\} — matching counts of two symbols.
  • L7={wwR:w{0,1}}L_7 = \{w w^R : w \in \{0,1\}^*\} — even-length palindromes.
  • L8={w{a,b,c}:na(w)=nb(w)}L_8 = \{w \in \{a,b,c\}^* : n_a(w) = n_b(w)\} — equal numbers of aS and bS.

Decidable but not context-free:

  • L9={anbncn:n0}L_9 = \{a^n b^n c^n : n \geq 0\}.
  • L10={G:Gisaconnectedundirectedgraph}L_{10} = \{\langle G \rangle : G \mathrm{ is} a connected undirected graph\}.

Undecidable (Turing-recognisable):

  • ATM={M,w:Macceptsw}A_{\mathrm{TM} = \{\langle M, w \rangle : M \mathrm{ accepts} w\}} — the acceptance problem.
  • \mathrm{HALT_}{\mathrm{TM} = \{\langle M, w \rangle : M \mathrm{ halts} on w\}}.

Not even Turing-recognisable:

  • ATM\overline{A_{\mathrm{TM}}} — 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 Σ\Sigma 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 Σ\Sigma is uncountable.

Proof. The set Σ\Sigma^* is countable (enumerate strings by length, then lexicographically). The set of all languages is P(Σ)\mathcal{P}(\Sigma^*)Which is uncountable by Cantor”s theorem (since P(S)>S|\mathcal{P}(S)| \gt |S| for any set SS). \blacksquare

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. \blacksquare

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 L1,L2,L3,L_1, L_2, L_3, \ldotsAnd construct a language DD that differs from each LiL_i on the ii-th string. Then DD is not in the list — contradiction. This technique reappears in the proof of undecidability of ATMA_{\mathrm{TM}} (Section 5.2).

2. Regular Languages

2.1 Finite Automata

A deterministic finite automaton (DFA) is a 5-tuple M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) where:

  • QQ is a finite set of states.
  • Σ\Sigma is the input alphabet.
  • δ:Q×ΣQ\delta : Q \times \Sigma \to Q is the transition function.
  • q0Qq_0 \in Q is the start state.
  • FQF \subseteq Q is the set of accepting (final) states.

MM accepts a string w=w1w2wnw = w_1 w_2 \cdots w_n if the sequence of states q0,δ(q0,w1),δ(δ(q0,w1),w2),q_0, \delta(q_0, w_1), \delta(\delta(q_0, w_1), w_2), \ldots ends in a state in FF.

Example. DFA for strings over {0,1}\{0, 1\} containing the substring 01:

M=({q0,q1,q2},{0,1},δ,q0,{q2})M = (\{q_0, q_1, q_2\}, \{0, 1\}, \delta, q_0, \{q_2\})

Stateδ(,0)\delta(\cdot, 0)δ(,1)\delta(\cdot, 1)
q0q_0q1q_1q0q_0
q1q_1q1q_1q2q_2
q2q_2q2q_2q2q_2

A nondeterministic finite automaton (NFA) allows multiple transitions from a state on the same Symbol, and ε\varepsilon-transitions (transitions without consuming input). Formally, δ:Q×(Σ{ε})P(Q)\delta : Q \times (\Sigma \cup \{\varepsilon\}) \to \mathcal{P}(Q).

MM accepts ww if there exists some path of transitions consuming ww that ends in a state in FF.

Worked Example: DFA for binary numbers divisible by 3

Design a DFA over Σ={0,1}\Sigma = \{0, 1\} 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 xxLet r=xmod3r = x \bmod 3. Reading a new bit bb appends bb to the right: the new value is 2r+bmod32r + b \bmod 3.

States: q0q_0 (remainder 0), q1q_1 (remainder 1), q2q_2 (remainder 2). Start state: q0q_0 (the empty prefix has value 0). Accept state: q0q_0.

M=({q0,q1,q2},{0,1},δ,q0,{q0})M = (\{q_0, q_1, q_2\}, \{0, 1\}, \delta, q_0, \{q_0\})

Stateδ(,0)\delta(\cdot, 0)δ(,1)\delta(\cdot, 1)
q0q_0q0q_0q1q_1
q1q_1q2q_2q0q_0
q2q_2q1q_1q2q_2

Correctness. By induction on input length. Base: x=εx = \varepsilon, val(ε)=0\mathrm{val}(\varepsilon) = 0 DFA is in q0q_0. Step: if after xx the DFA is in qrq_r (where r=val(x)mod3r = \mathrm{val}(x) \bmod 3), Then reading bb moves to q(2r+b)mod3q_{(2r+b) \bmod 3}Which equals qval(xb)mod3q_{\mathrm{val}(xb) \bmod 3}. \blacksquare

Worked Example: NFA for strings ending in `01`

Construct an NFA over {0,1}\{0, 1\} that accepts strings ending in the substring 01.

States: q0q_0 (start), q1q_1 (have seen a 0), q2q_2 (accept, have seen 01).

Stateδ(,0)\delta(\cdot, 0)δ(,1)\delta(\cdot, 1)
q0q_0{q0,q1}\{q_0, q_1\}{q0}\{q_0\}
q1q_1\emptyset{q2}\{q_2\}
q2q_2{q1}\{q_1\}\emptyset

Accept: {q2}\{q_2\}. The NFA is nondeterministic because from q0q_0 on input 0It can either stay In q0q_0 or move to q1q_1.

2.2 Equivalence of DFA and NFA

Theorem 2.1. For every NFA NNThere exists a DFA DD such that L(N)=L(D)L(N) = L(D).

Proof (subset construction). Given NFA N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F)Construct DFA D=(Q,Σ,δ,q0,F)D = (Q', \Sigma, \delta', q_0', F') where:

  • Q=P(Q)Q' = \mathcal{P}(Q) (each state of DD is a subset of states of NN).
  • q0=εclosure({q0})q_0' = \varepsilon\mathrm{-closure(\{q_0\})}.
  • δ(S,a)=εclosure(qSδ(q,a))\delta'(S, a) = \varepsilon\mathrm{-closure(\bigcup_{q \in S} \delta(q, a))} for SQS \subseteq Q aΣa \in \Sigma.
  • F={SQ:SF}F' = \{S \subseteq Q : S \cap F \neq \emptyset\}.

Every string accepted by NN has some path through NN‘s states. The subset construction tracks all Possible states NN could be in after reading each symbol. DD accepts exactly when at least one Possibility in NN leads to acceptance. \blacksquare

Corollary 2.2. NFA, DFA, and NFA with ε\varepsilon-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: {q0,q1,q2}\{q_0, q_1, q_2\}, Σ={0,1}\Sigma = \{0, 1\}, F={q2}F = \{q_2\}No ε\varepsilon-transitions.

Start state: {q0}\{q_0\}.

DFA Stateδ(,0)\delta'(\cdot, 0)δ(,1)\delta'(\cdot, 1)Accept?
{q0}\{q_0\}{q0,q1}\{q_0, q_1\}{q0}\{q_0\}No
{q0,q1}\{q_0, q_1\}{q0,q1}\{q_0, q_1\}{q0,q2}\{q_0, q_2\}No
{q0,q2}\{q_0, q_2\}{q0,q1}\{q_0, q_1\}{q0}\{q_0\}Yes

The DFA has 3 reachable states. {q1}\{q_1\}, {q2}\{q_2\}And {q1,q2}\{q_1, q_2\} are unreachable.

2.3 Regular Expressions

Definition. Regular expressions over Σ\Sigma are defined inductively:

  1. \emptyset (empty set), ε\varepsilon (empty string), and aa for each aΣa \in \Sigma are regex.
  2. If R1R_1 and R2R_2 are regex, then (R1R2)(R_1 \cup R_2), (R1R2)(R_1 \cdot R_2)And (R1)(R_1^*) are regex.
  3. Nothing else is a regex.

Shorthand: R+R^+ means RRR \cdot R^*. R?R? means (Rε)(R \cup \varepsilon).

Examples:

  • (01)00(01)(0 \cup 1)^* 00 (0 \cup 1)^*: strings containing 00.
  • 101011^* 01^* 01^*: strings with exactly two 0S.
  • (01)(0 \cup 1)^*: all strings over {0,1}\{0, 1\}.
  • \emptyset: the empty language.

Regex identities. The following algebraic laws hold for regular expressions and can be used To simplify or reason about them:

IdentityDescription
R=R=\emptyset \cdot R = R \cdot \emptyset = \emptysetAnnihilator for concatenation
εR=Rε=R\varepsilon \cdot R = R \cdot \varepsilon = RIdentity for concatenation
R=R=R\emptyset \cup R = R \cup \emptyset = RIdentity for union
=ε\emptyset^* = \varepsilonKleene star of empty set
ε=ε\varepsilon^* = \varepsilonKleene star of empty string
(R)=R(R^*)^* = R^*Idempotence of Kleene star
R(ST)=(RS)TR \cdot (S \cdot T) = (R \cdot S) \cdot TAssociativity of concatenation
RS=SRR \cup S = S \cup RCommutativity of union
R(ST)=(RS)TR \cup (S \cup T) = (R \cup S) \cup TAssociativity of union
RR=RR \cup R = RIdempotence of union
R(ST)=RSRTR(S \cup T) = RS \cup RTDistributivity

These identities can be used to prove that two regex describe the same language, analogous to Simplifying algebraic expressions. Two regex R1R_1 and R2R_2 are equivalent (written R1R2R_1 \equiv R_2) If L(R1)=L(R2)L(R_1) = L(R_2).

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: \emptyset, ε\varepsilon, aa each have trivial NFAs.
  • Union: Combine two NFAs with a new start state and ε\varepsilon-transitions to each.
  • Concatenation: Connect the accept states of the first NFA to the start state of the second via ε\varepsilon-transitions.
  • Kleene star: Add ε\varepsilon-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. \blacksquare

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 ε\varepsilon-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 RR over Σ\SigmaThompson’s construction produces an NFA NRN_R with L(NR)=L(R)L(N_R) = L(R)And NRN_R has O(R)O(|R|) States and transitions.

Proof. By structural induction on RR.

  • Base cases:

  • R=R = \emptyset: NRN_R has a start state and an accept state with no transitions. L(NR)==L()L(N_R) = \emptyset = L(\emptyset).

  • R=εR = \varepsilon: NRN_R has a start state, an accept state, and an ε\varepsilon-transition between them. L(NR)={ε}=L(ε)L(N_R) = \{\varepsilon\} = L(\varepsilon).

  • R=aR = a for aΣa \in \Sigma: NRN_R has a start state, an accept state, and a transition on aa. L(NR)={a}=L(a)L(N_R) = \{a\} = L(a).

  • Inductive cases:

  • R=R1R2R = R_1 \cup R_2: By IH, L(NR1)=L(R1)L(N_{R_1}) = L(R_1) and L(NR2)=L(R2)L(N_{R_2}) = L(R_2). Thompson adds a new start ss and accept ff with ε\varepsilon-transitions to the start states of NR1N_{R_1} and NR2N_{R_2}And ε\varepsilon-transitions from their accept states to ff. Any accepting path goes through exactly one sub-NFA, so L(NR)=L(R1)L(R2)=L(R)L(N_R) = L(R_1) \cup L(R_2) = L(R).

  • R=R1R2R = R_1 \cdot R_2: Thompson connects the accept state of NR1N_{R_1} to the start state of NR2N_{R_2} via ε\varepsilon-transitions. A string wL(NR)w \in L(N_R) iff w=w1w2w = w_1 w_2 where w1L(NR1)w_1 \in L(N_{R_1}) and w2L(NR2)w_2 \in L(N_{R_2})I.e., wL(R1)L(R2)=L(R)w \in L(R_1) \cdot L(R_2) = L(R).

  • R=R1R = R_1^*: Thompson adds a new start ss and accept ffWith ε\varepsilon-transitions from ss to ff (allowing zero repetitions) and from ss to the start of NR1N_{R_1}And from the accept of NR1N_{R_1} back to ss. Any accepting path corresponds to zero or more traversals of NR1N_{R_1}So L(NR)=L(R1)=L(R)L(N_R) = L(R_1)^* = L(R).

In all cases, the number of states added is a constant per operator, so NR=O(R)|N_R| = O(|R|). \blacksquare

2.5 DFA Minimisation and the Myhill-Nerode Theorem

Theorem 2.4 (Myhill-Nerode). The following are equivalent for a language LL:

  1. LL is regular.
  2. The Myhill-Nerode equivalence relation has finitely many equivalence classes.
  3. LL is the union of some of the equivalence classes.

Definition. Two strings x,yx, y are distinguishable with respect to LL if there exists zz Such that exactly one of xz,yzxz, yz is in LL. The Myhill-Nerode equivalence L\equiv_L is: xLyx \equiv_L y iff xx and yy are not distinguishable.

Proof of Theorem 2.4.

(1) \Rightarrow (2): Let D=(Q,Σ,δ,q0,F)D = (Q, \Sigma, \delta, q_0, F) be a DFA for LL. Define xyx \sim y iff δ(q0,x)=δ(q0,y)\delta^*(q_0, x) = \delta^*(q_0, y) (i.e., DD reaches the same state on xx and yy). Then \sim has at most Q|Q| equivalence classes. We show =L\sim = \equiv_L. If xyx \sim yThen for All zz, δ(q0,xz)=δ(q0,yz)\delta^*(q_0, xz) = \delta^*(q_0, yz)So xzLxz \in L iff yzLyz \in LMeaning xLyx \equiv_L y. Conversely, if x̸Lyx \not\equiv_L yThere exists zz with xzLxz \in L and yzLyz \notin L (or vice versa), so δ(q0,xz)δ(q0,yz)\delta^*(q_0, xz) \neq \delta^*(q_0, yz)Hence x≁yx \not\sim y.

(2) \Rightarrow (3): Trivial, since LL consists of all strings whose equivalence class is one That contains at least one string in LL.

(3) \Rightarrow (1): Suppose L\equiv_L has finitely many equivalence classes C1,,CkC_1, \ldots, C_k. Construct a DFA with one state per equivalence class, start state [x]L[x]_{\equiv_L} for x=εx = \varepsilon Transition δ([x],a)=[xa]\delta'([x], a) = [xa]And accept states = those classes contained in LL. This DFA is Well-defined because L\equiv_L is a right congruence: if xLyx \equiv_L yThen xaLyaxa \equiv_L ya for all aΣa \in \Sigma. \blacksquare

Worked Example: Myhill-Nerode classes for $L = \{0^n 1^n : n \geq 0\}$

We show LL is not regular by exhibiting infinitely many pairwise distinguishable strings.

Claim: the strings 00,01,02,03,0^0, 0^1, 0^2, 0^3, \ldots are pairwise distinguishable with respect to LL.

Proof. For iji \neq j with i<ji \lt jConsider the suffix z=1iz = 1^i. Then:

  • 0i1i=0i1iL0^i \cdot 1^i = 0^i 1^i \in L.
  • 0j1i=0j1iL0^j \cdot 1^i = 0^j 1^i \notin L (since j>ij \gt i).

Therefore 0i̸L0j0^i \not\equiv_L 0^j. Since there are infinitely many such strings, L\equiv_L has Infinitely many equivalence classes, so LL is not regular by the Myhill-Nerode theorem. \blacksquare

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 LL has exactly as many states as there are Equivalence classes of L\equiv_L.

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 O(n2k)O(n^2 k) time where nn is the number of states and k=Σk = |\Sigma|.

2.6 Pumping Lemma for Regular Languages

Theorem 2.6 (Pumping Lemma). If LL is regular, then there exists a constant pp (the pumping Length) such that for every string wLw \in L with wp|w| \geq p, ww can be decomposed as w=xyzw = xyz satisfying:

  1. y>0|y| \gt 0 (the pumped part is non-empty).
  2. xyp|xy| \leq p (the pumpable part is within the first pp symbols).
  3. xyizLxy^iz \in L for all i0i \geq 0.

Proof. Let DD be a DFA for LL with nn states. Set p=np = n. For any string ww of length n\geq n The path through DD visits at least n+1n + 1 states, so by the pigeonhole principle, some state is Visited twice within the first pp symbols. The substring yy between the two visits can be repeated Or removed (xyizxy^iz) without affecting acceptance. \blacksquare

Using the pumping lemma to prove non-regularity. To show LL is not regular, assume it is, let pp Be the pumping length, and exhibit a string wLw \in L with wp|w| \geq p such that every Decomposition w=xyzw = xyz satisfying (1) and (2) has some ii with xyizLxy^iz \notin L.

Example. L={0n1n:n0}L = \{0^n 1^n : n \geq 0\} is not regular.

Proof. Assume LL is regular with pumping length pp. Let w=0p1pLw = 0^p 1^p \in L. By (2), xyp|xy| \leq pSo yy consists only of 0S. Let y=k>0|y| = k \gt 0. Then xy0z=0pk1pLxy^0 z = 0^{p-k} 1^p \notin L (since pkpp - k \neq p). Contradiction. \blacksquare

Example. L={ww:w{0,1}}L = \{ww : w \in \{0,1\}^*\} is not regular.

Proof. Assume pumping length pp. Let w=0p10p1Lw = 0^p 1 0^p 1 \in L. Since xyp|xy| \leq p, y=0ky = 0^k for Some k>0k \gt 0. Then xy0z=0pk10p1Lxy^0 z = 0^{p-k} 1 0^p 1 \notin L (the two halves have different lengths). \blacksquare

Example. L={0n1m:nm}L = \{0^n 1^m : n \neq m\} is not regular.

Proof. Assume LL is regular with pumping length pp. Since regular languages are closed under Complement, L={0n1m:n=m}{w:w01}\overline{L} = \{0^n 1^m : n = m\} \cup \{w : w \notin 0^* 1^*\} would also be regular. Then L01={0n1n:n0}\overline{L} \cap 0^* 1^* = \{0^n 1^n : n \geq 0\} would be regular (since 010^* 1^* is regular And regular languages are closed under intersection). But {0n1n:n0}\{0^n 1^n : n \geq 0\} is not regular — Contradiction. \blacksquare

Worked Example: Proving $\{w : n_0(w) = n_1(w)\}$ is not regular

Let L={w{0,1}:n0(w)=n1(w)}L = \{w \in \{0,1\}^* : n_0(w) = n_1(w)\}.

Proof. Assume LL is regular with pumping length pp. Let w=0p1pLw = 0^p 1^p \in L. By (2), xyp|xy| \leq pSo y=0ky = 0^k for some k1k \geq 1. Then xy0z=0pk1pxy^0z = 0^{p-k}1^pWhich has pkp - k zeros and pp ones. Since k1k \geq 1 pkpp - k \neq pSo xy0zLxy^0z \notin L. Contradiction. \blacksquare

Worked Example: $L = \{a^{n!} : n \geq 0\}$ is not regular

Proof. Assume pumping length pp. Let w=ap!Lw = a^{p!} \in L with w=p!p|w| = p! \geq p. By (2), xyp|xy| \leq pSo y=aky = a^k for some 1kp1 \leq k \leq p. Choose i=(p!+k)/k=p!/k+1i = (p! + k) / k = p!/k + 1 (an integer since kpk \leq p). Then xyiz=ap!+(i1)k=ap!+p!=a2p!xy^iz = a^{p! + (i-1)k} = a^{p! + p!} = a^{2 \cdot p!}. But 2p!2 \cdot p! is not a factorial for p2p \geq 2 (since (p+1)!/(p!)2=(p+1)/p!<1(p+1)! / (p!)^2 = (p+1)/p! \lt 1 for p3p \geq 3 And 22!=4n!2 \cdot 2! = 4 \neq n! for any nn). Hence xyizLxy^iz \notin L. \blacksquare

:::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:

OperationProof technique
UnionNFA union construction
IntersectionDFA product construction
ComplementSwap accepting and non-accepting states
ConcatenationNFA concatenation construction
Kleene starNFA star construction
ReversalReverse all transitions, swap start/F
DifferenceABA \cap \overline{B}
HomomorphismApply the homomorphism to each symbol
Inverse homomorphismPre-image construction

Theorem 2.7 (Intersection via product construction). If L1L_1 and L2L_2 are regular, then L1L2L_1 \cap L_2 is regular.

Proof. Let D1=(Q1,Σ,δ1,q1,F1)D_1 = (Q_1, \Sigma, \delta_1, q_1, F_1) and D2=(Q2,Σ,δ2,q2,F2)D_2 = (Q_2, \Sigma, \delta_2, q_2, F_2). Construct D=(Q1×Q2,Σ,δ,(q1,q2),F1×F2)D = (Q_1 \times Q_2, \Sigma, \delta, (q_1, q_2), F_1 \times F_2) where δ((r1,r2),a)=(δ1(r1,a),δ2(r2,a))\delta((r_1, r_2), a) = (\delta_1(r_1, a), \delta_2(r_2, a)). Then DD accepts ww iff both D1D_1 and D2D_2 accept wwI.e., wL1L2w \in L_1 \cap L_2. \blacksquare

Theorem 2.7. If L1L_1 is regular and L2L_2 is not regular, then L1L2L_1 \cap L_2 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 G=(V,Σ,R,S)G = (V, \Sigma, R, S) where:

  • VV is a finite set of variables (non-terminals).
  • Σ\Sigma is a finite set of terminals.
  • RV×(VΣ)R \subseteq V \times (V \cup \Sigma)^* is a finite set of production rules.
  • SVS \in V is the start variable.

A production AαA \to \alpha means variable AA can be replaced by string α\alpha.

Example. CFG for L={anbn:n0}L = \{a^n b^n : n \geq 0\}:

SaSbεS \to aSb \mid \varepsilon

Example. CFG for balanced parentheses:

S(S)SSεS \to (S) \mid SS \mid \varepsilon

Example. CFG for {w{0,1}:w=wR}\{w \in \{0,1\}^* : w = w^R\} (palindromes):

S0S01S101εS \to 0S0 \mid 1S1 \mid 0 \mid 1 \mid \varepsilon

Example. CFG for L={aibjck:i=j+k}L = \{a^i b^j c^k : i = j + k\}:

SaSbT,TaTcεS \to aSb \mid T, \quad T \to aTc \mid \varepsilon

The variable SS generates the aibia^i b^i part, and TT generates the akcka^k c^k part. Since SaiSbiaiTbiai+kckbiS \Rightarrow^* a^i S b^i \Rightarrow a^i T b^i \Rightarrow^* a^{i+k} c^k b^iThe total number Of bS and cS equals the total number of aS.

Example. CFG for strings with equal numbers of 0S and 1S:

S0S11S0SSεS \to 0S1 \mid 1S0 \mid SS \mid \varepsilon

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 {a,b}\{a, b\} that are not palindromes:

SaAbbAaaSabSbab,AaAaaAbbAabAbabS \to aAb \mid bAa \mid aSa \mid bSb \mid a \mid b, \quad A \to aAa \mid aAb \mid bAa \mid bAb \mid a \mid b

Here SS generates non-palindromes and AA generates arbitrary strings of length 1\geq 1.

Worked Example: CFG for $\{a^i b^j : 2i = j \mathrm{ or} 2j = i\}$

SS1S2,S1aS1bbε,S2aaS2bεS \to S_1 \mid S_2, \quad S_1 \to aS_1bb \mid \varepsilon, \quad S_2 \to aaS_2b \mid \varepsilon

S1S_1 generates {aib2i}\{a^i b^{2i}\} and S2S_2 generates {a2jbj}\{a^{2j} b^j\}. Their union is the desired Language.

Proof of correctness for S1S_1. By induction: S1nanS1b2nanb2nS_1 \Rightarrow^n a^n S_1 b^{2n} \Rightarrow a^n b^{2n}. So L(S1)={anb2n:n0}L(S_1) = \{a^n b^{2n} : n \geq 0\}. Similarly L(S2)={a2nbn:n0}L(S_2) = \{a^{2n} b^n : n \geq 0\}. Hence L(S)=L(S1)L(S2)L(S) = L(S_1) \cup L(S_2)Which is exactly the target language. \blacksquare

3.2 Parse Trees and Ambiguity

A parse tree (derivation tree) for a string ww according to grammar GG is a tree where:

  • The root is labelled SS.
  • Internal nodes are labelled with variables.
  • Leaves are labelled with terminals (or ε\varepsilon).
  • Children of a node labelled AA are the symbols of the right-hand side of a production AαA \to \alpha 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). SS+SS×SidS \to S + S \mid S \times S \mid \mathrm{id}. The string id+id×id\mathrm{id} + \mathrm{id} \times \mathrm{id} has two parse trees: S+(S×S)S + (S \times S) and (S+S)×S(S + S) \times S.

Removing ambiguity. Some ambiguous grammars can be made unambiguous by rewriting the productions To enforce a particular evaluation order. For the arithmetic expression grammar:

EE+TT,TT×FF,F(E)idE \to E + T \mid T, \quad T \to T \times F \mid F, \quad F \to (E) \mid \mathrm{id}

This grammar is unambiguous and enforces the standard precedence (×\times before ++) and left Associativity.

Worked Example: Proving a grammar is unambiguous

Consider the grammar: SaSbabS \to aSb \mid ab. We prove it is unambiguous.

Proof. Any string in L(G)L(G) has the form anbna^n b^n for some n1n \geq 1. We show by induction On nn that anbna^n b^n has exactly one parse tree.

Base (n=1n = 1): abab has only the parse tree using SabS \to ab.

Inductive step: an+1bn+1a^{n+1} b^{n+1} must use SaSbS \to aSb (the rule SabS \to ab produces only Strings of length 2). The inner SS must derive anbna^n b^nWhich by IH has a unique parse tree. Hence an+1bn+1a^{n+1} b^{n+1} has exactly one parse tree. \blacksquare

Example. L={aibjck:i=jorj=k}L = \{a^i b^j c^k : i = j \mathrm{ or} j = k\} 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:

  • ABCA \to BC (two variables), or
  • AaA \to a (single terminal), or
  • SεS \to \varepsilon (only the start variable, and SS 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).

  1. Add a new start variable S0S_0 with rule S0SS_0 \to S (ensures SS does not appear on any right side if it generates ε\varepsilon).
  2. Remove all ε\varepsilon-productions AεA \to \varepsilon (except possibly S0εS_0 \to \varepsilon). For each occurrence of AA in a right-hand side, add a variant without AA.
  3. Remove all unit productions ABA \to B. For each chain ABA \Rightarrow^* BAdd AαA \to \alpha for each BαB \to \alpha.
  4. Convert remaining rules: replace terminals aa in right-hand sides of length 2\geq 2 with new variables TaT_a and rules TaaT_a \to a. Replace right-hand sides of length 3\geq 3 by introducing intermediate variables. \blacksquare
Worked Example: Converting a CFG to CNF

Convert the following grammar to CNF:

SAbB,AaAε,BbBεS \to AbB, \quad A \to aA \mid \varepsilon, \quad B \to bB \mid \varepsilon

Step 1: Add new start variable. S0SS_0 \to S.

Step 2: Remove ε\varepsilon-productions. Both AA and BB are nullable. For each production Containing AA or BBAdd variants with nullable symbols removed:

  • SAbBAbaBabS \to AbB \mid Ab \mid aB \mid ab
  • AaAaA \to aA \mid a
  • BbBbB \to bB \mid b
  • S0SS_0 \to S

Step 3: Remove unit productions. S0SS_0 \to S is a unit production. Replace with all SS-productions:

  • S0AbBAbaBabS_0 \to AbB \mid Ab \mid aB \mid ab

Step 4: Convert long right-hand sides and terminals.

Introduce TaaT_a \to a and TbbT_b \to b:

  • SATbBATbTaBTaTbS \to AT_bB \mid AT_b \mid T_aB \mid T_aT_b
  • S0ATbBATbTaBTaTbS_0 \to AT_bB \mid AT_b \mid T_aB \mid T_aT_b

Handle ATbBAT_bB (length 3): introduce C1ATbC_1 \to AT_bThen SC1BS \to C_1B and S0C1BS_0 \to C_1B.

Final CNF grammar:

S0C1BATbTaBTaTbS_0 \to C_1B \mid AT_b \mid T_aB \mid T_aT_b SC1BATbTaBTaTbS \to C_1B \mid AT_b \mid T_aB \mid T_aT_b ATaATaA \to T_aA \mid T_a BTbBTbB \to T_bB \mid T_b C1ATbC_1 \to AT_b Taa,TbbT_a \to a, \quad T_b \to b

3.4 Pushdown Automata

A pushdown automaton (PDA) is a 6-tuple M=(Q,Σ,Γ,δ,q0,F)M = (Q, \Sigma, \Gamma, \delta, q_0, F) where:

  • QQ is a finite set of states.
  • Σ\Sigma is the input alphabet.
  • Γ\Gamma is the stack alphabet.
  • δ:Q×(Σ{ε})×(Γ{ε})P(Q×(Γ{ε}))\delta : Q \times (\Sigma \cup \{\varepsilon\}) \times (\Gamma \cup \{\varepsilon\}) \to \mathcal{P}(Q \times (\Gamma \cup \{\varepsilon\})) is the transition function.
  • q0Qq_0 \in Q is the start state.
  • FQF \subseteq Q is the set of accepting states.

A PDA can read input, push symbols onto the stack, pop symbols from the stack, and make ε\varepsilon-moves (without consuming input or changing the stack).

Example. PDA for L={anbn:n0}L = \{a^n b^n : n \geq 0\}:

  1. Push a marker \text{\}$ onto the stack.
  2. For each aPush X onto the stack.
  3. For each bPop X from the stack.
  4. Accept if the stack is empty (just the marker) and all input is consumed.

Formally: states q0q_0 (start), q1q_1 (pushing), q2q_2 (popping), q3q_3 (accept).

Worked Example: PDA for palindromes $\{ww^R : w \in \{0,1\}^*\}$

Design a PDA for L={wwR:w{0,1}}L = \{ww^R : w \in \{0,1\}^*\}.

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: q0q_0 (pushing), q1q_1 (popping/comparing), q2q_2 (accept).

Transitions:

  • Push phase (q0q_0):

  • (q0,0,ε)(q0,0)(q_0, 0, \varepsilon) \to (q_0, 0) — push 0.

  • (q0,1,ε)(q0,1)(q_0, 1, \varepsilon) \to (q_0, 1) — push 1.

  • (q0,ε,ε)(q1,ε)(q_0, \varepsilon, \varepsilon) \to (q_1, \varepsilon) — guess the midpoint.

  • Pop phase (q1q_1):

  • (q1,0,0)(q1,ε)(q_1, 0, 0) \to (q_1, \varepsilon) — match 0.

  • (q1,1,1)(q1,ε)(q_1, 1, 1) \to (q_1, \varepsilon) — match 1.

  • (q1,ε,ε)(q2,ε)(q_1, \varepsilon, \varepsilon) \to (q_2, \varepsilon) — accept if stack empty and input consumed.

Accept: {q2}\{q_2\}.

Correctness. If w=uuRw = uu^RThe PDA pushes uuGuesses the midpoint, then pops and matches uRu^R. If wuuRw \neq uu^RNo sequence of guesses leads to acceptance. \blacksquare

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 MMConstruct a grammar whose variables encode pairs of states (p,q)(p, q) Meaning “the PDA can go from state pp to state qqPopping everything pushed onto the stack.” The productions simulate the PDA’s transitions. \blacksquare

Theorem 3.2a (CFG to PDA construction). Let G=(V,Σ,R,S)G = (V, \Sigma, R, S) be a CFG. Then there exists a PDA MM with L(M)=L(G)L(M) = L(G).

Proof. Construct M = (\{q_0, q_1, q_2\}, \Sigma, V \cup \Sigma \cup \{\}, \delta, q_0, {q_2})$:

  1. (q_0, \varepsilon, \varepsilon) \to (q_1, S\)$ — push the start variable and bottom marker.
  2. For each AαRA \to \alpha \in R: (q1,ε,A)(q1,α)(q_1, \varepsilon, A) \to (q_1, \alpha) — replace a variable with its production.
  3. For each aΣa \in \Sigma: (q1,a,a)(q1,ε)(q_1, a, a) \to (q_1, \varepsilon) — match a terminal on input with stack.
  4. (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{\}remains,thederivationiscompleteandallinputhasbeenconsumed.remains, the derivation is complete and all input has been consumed.\blacksquare$

3.6 Pumping Lemma for Context-Free Languages

Theorem 3.3. If LL is context-free, there exists pp such that for every wLw \in L with wp|w| \geq p, ww can be decomposed as w=uvxyzw = uvxyz satisfying:

  1. vy>0|vy| \gt 0.
  2. vxyp|vxy| \leq p.
  3. uvixyizLuv^ixy^iz \in L for all i0i \geq 0.

Proof. Let GG be a CFG in CNF with kk variables. Any parse tree of height hh generates a string Of length at most 2h12^{h-1}. Set p=2kp = 2^k. For wp|w| \geq pThe parse tree has height >k\gt k So some path repeats a variable. The substring generated between the two occurrences can be pumped. \blacksquare

Example. L={anbncn:n0}L = \{a^n b^n c^n : n \geq 0\} is not context-free.

Proof. Assume pumping length pp. Let w=apbpcpw = a^p b^p c^p. Since vxyp|vxy| \leq pThe substring vxyvxy cannot span all three letter types. Case analysis:

  • If vxyvxy is within the aS or bS or cS: pumping changes only one count, breaking the equality.
  • If vxyvxy spans aS and bS: pumping changes the a and b counts but not cBreaking equality.
  • If vxyvxy spans bS and cS: analogous.

In all cases, uv2xy2zLuv^2xy^2z \notin L. \blacksquare

Example. L={aibjck:i<j<k}L = \{a^i b^j c^k : i \lt j \lt k\} is not context-free.

Proof. Assume pumping length pp. Let w=apbp+1cp+2w = a^p b^{p+1} c^{p+2}. Since vxyp|vxy| \leq p:

  • If vxyvxy lies entirely within one block: pumping up (i=2i = 2) 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 vxyvxy 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 i<j<ki \lt j \lt k.

In all cases, uv2xy2zLuv^2xy^2z \notin L. \blacksquare

Worked Example: $\{a^n b^n a^n : n \geq 0\}$ is not context-free

Proof. Assume pumping length pp. Let w=apbpapw = a^p b^p a^p. Since vxyp|vxy| \leq pThe substring vxyvxy cannot span all three blocks. Case analysis:

  • vxyvxy within the first apa^p block: pumping down (i=0i = 0) reduces the first count only. uv0xy0z=apkbpapLuv^0xy^0z = a^{p-k}b^pa^p \notin L for k>0k \gt 0.
  • vxyvxy within the bpb^p block: pumping changes the bb count only, breaking equality.
  • vxyvxy within the last apa^p block: analogous.
  • vxyvxy spans the first apa^p and bpb^p: pumping changes first aa and bb counts but not the last aa count.
  • vxyvxy spans bpb^p and last apa^p: analogous.

In all cases, pumping produces a string not in LL. \blacksquare

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 GG in CNF and a string ww of length nnThe CYK algorithm decides Whether wL(G)w \in L(G) in O(n3G)O(n^3 \cdot |G|) time.

Algorithm. Construct a table T[i,j]T[i, j] for 1ijn1 \leq i \leq j \leq nWhere T[i,j]T[i, j] is the set Of variables that can derive the substring wiwi+1wjw_i w_{i+1} \cdots w_j.

  1. Base case (j=1j = 1): T[i, i] = \{A : A \to w_i \mathrm{ is a rule in G\}.
  2. Recursive case (j>1j \gt 1): For each split kk with ik<ji \leq k \lt j: T[i,j]:=T[i,j]{A:ABCR,BT[i,k],CT[k+1,j]}T[i, j] \mathrel{{:}{=}} T[i, j] \cup \{A : A \to BC \in R, B \in T[i, k], C \in T[k+1, j]\}
  3. Answer: wL(G)w \in L(G) iff ST[1,n]S \in T[1, n].

Proof of correctness. In CNF, every derivation of a string of length \ell involves exactly 1\ell - 1 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, T[i,j]T[i, j] contains exactly those variables that derive wiwjw_i \cdots w_j. \blacksquare

Time complexity. The table has O(n2)O(n^2) entries. Each entry considers at most nn split points And checks all G|G| rules, giving O(n3G)O(n^3 \cdot |G|) total time.

Worked Example: CYK on a small grammar

Grammar (CNF): SABBCS \to AB \mid BC, ABAaA \to BA \mid a, BCCbB \to CC \mid b, CABaC \to AB \mid a.

String: w=baw = ba.

Length 1:

  • T[1,1]T[1,1]: w1=bw_1 = bSo T[1,1]={B}T[1,1] = \{B\} (since BbB \to b).
  • T[2,2]T[2,2]: w2=aw_2 = aSo T[2,2]={A,C}T[2,2] = \{A, C\} (since AaA \to a and CaC \to a).

Length 2:

  • T[1,2]T[1,2]: split at k=1k = 1. Check all pairs (XT[1,1],YT[2,2])(X \in T[1,1], Y \in T[2,2]):
  • X=B,Y=AX = B, Y = A: SBAS \to BA? No. BBAB \to BA? No. ABAA \to BA? No. CBAC \to BA? No.
  • X=B,Y=CX = B, Y = C: SBCS \to BC? Yes — add SS.
  • X=B,Y=AX = B, Y = A: already checked. So T[1,2]={S}T[1,2] = \{S\}.

Since ST[1,2]S \in T[1,2]The string baba is in L(G)L(G). The parse tree is SBCS \to BC Where BbB \to b and CaC \to a.

4. Turing Machines

4.1 Definition

A Turing machine (TM) is a 7-tuple M=(Q,Σ,Γ,δ,q0,qaccept,Qreject)M = (Q, \Sigma, \Gamma, \delta, q_0, q_{\mathrm{accept}, Q_{\mathrm{reject})}} where:

  • QQ is a finite set of states.
  • Σ\Sigma is the input alphabet (does not contain the blank symbol \sqcup).
  • Γ\Gamma is the tape alphabet, Γ\sqcup \in \Gamma, ΣΓ\Sigma \subseteq \Gamma.
  • δ:Q×ΓQ×Γ×{L,R}\delta : Q \times \Gamma \to Q \times \Gamma \times \{L, R\} is the transition function.
  • q0Qq_0 \in Q 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.

MM accepts input ww if the computation halts in q_{\mathrm{accept}. MM rejects ww if It halts in q_{\mathrm{reject}. MM loops if it never halts.

The language recognised by MM 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 (q,a1,,ak)(q, a_1, \ldots, a_k) to (q,b1,,bk,d1,,dk)(q', b_1, \ldots, b_k, d_1, \ldots, d_k).

Theorem 4.1. Every multitape TM has an equivalent single-tape TM.

Proof. Simulate kk tapes on a single tape using kk tracks (or interleaving symbols with Delimiters). Simulating one step of the multitape TM requires scanning the single tape to read all kk heads and update them, costing O(kn)O(k \cdot n) steps per simulated step. \blacksquare

Nondeterministic TMs. δ:Q×ΓP(Q×Γ×{L,R})\delta : Q \times \Gamma \to \mathcal{P}(Q \times \Gamma \times \{L, R\}). 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 bb branches (where bb is the maximum number of choices). After nn steps, the tree has At most bnb^n 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 O(bn)O(b^n) times the NTM’s time, which is exponential overhead. \blacksquare

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. (\Rightarrow) Given TM MM recognising LLConstruct an enumerator EE that dovetails: Runs MM on ε\varepsilon for 1 step, then MM on ε\varepsilon and MM on 00 for 2 steps, then on ε,0,1,00,01,10,11\varepsilon, 0, 1, 00, 01, 10, 11 for 3 steps, and so on. Whenever MM accepts, EE prints that String. Every string in LL is eventually printed.

(\Leftarrow) Given enumerator EE for LLConstruct TM MM that on input ww runs EE and checks Each printed string against ww. If ww is printed, accept. If wLw \in LIt will eventually be Printed, so MM recognises LL. \blacksquare

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:

  1. Robustness. Every “reasonable” model of computation proposed since the 1930s has been shown to compute exactly the Turing-computable functions.
  2. Invariance. Adding resources (more tapes, nondeterminism, random access) does not increase the class of computable functions.
  3. Empirical adequacy. No physically realisable process has been shown to compute a non-Turing-computable function.
  4. 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 L={0n1n:n0}L = \{0^n 1^n : n \geq 0\}.

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:

  1. In q0q_0Read 0: write xMove right, go to q1q_1. (Cross off a 0.)
  2. In q0q_0Read 1: reject. (A 1 before any 0.)
  3. In q0q_0Read \sqcup: accept. (Nothing left.)
  4. In q1q_1Read 0: move right, stay in q1q_1. (Skip remaining 0S.)
  5. In q1q_1Read 1: write xMove left, go to q2q_2. (Cross off a 1.)
  6. In q1q_1Read \sqcup: reject. (No 1 to match.)
  7. In q2q_2Read 0 or x: move left, stay in q2q_2. (Scan back.)
  8. In q2q_2Read \sqcup: move right, go to q0q_0. (Return to start.)

Correctness. Each iteration crosses off exactly one 0 and one 1. If the input is 0n1n0^n 1^n The machine performs nn iterations and accepts. If counts differ or the pattern is violated, The machine rejects. \blacksquare

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 B,w\langle B, w \rangle where B=(Q,Σ,δ,q0,F)B = (Q, \Sigma, \delta, q_0, F):

  1. Simulate BB on ww. Maintain the current state qq and position ii in ww.
  2. At each step, look up δ(q,wi)\delta(q, w_i) in BB‘s transition table (encoded on the tape).
  3. Update qq and ii. If qFq \in F when i=w+1i = |w| + 1Accept.
  4. If i=w+1i = |w| + 1 and qFq \notin FReject.

The simulation takes O(w)O(|w|) steps and always halts. \blacksquare

4.5 The Universal Turing Machine

A universal Turing machine (UTM) UU is a Turing machine that can simulate any other Turing Machine MM on any input ww. The input to UU is an encoding M,w\langle M, w \rangle.

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 UU uses multiple tapes:

  1. Tape 1: Stores the description M\langle M \rangle of the simulated TM (the “program”).
  2. Tape 2: Stores the current contents of MM‘s tape (the “memory”).
  3. Tape 3: Stores the current state of MM and the position of MM‘s head.

At each step, UU reads the current state and tape symbol from tapes 3 and 2, scans tape 1 to Find the matching transition in MM‘s description, updates tape 2 (write symbol), tape 3 (state And head position), and repeats. Since MM‘s description is finite and each step requires a finite Scan, UU correctly simulates MM. \blacksquare

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 MM runs in time t(n)t(n)Then UU simulates MM in time O(t(n)M)O(t(n) \cdot |M|)Where M|M| is the size of MM‘s description.

Worked Example: TM for $L = \{w\#w^R : w \in \{0,1\}^*\}$

Design a TM that decides L={w#wR:w{0,1}}L = \{w\#w^R : w \in \{0,1\}^*\}.

Idea: Repeatedly check the first and last non-#\# symbols, then cross them off. When only #\# remains, accept.

Algorithm:

  1. Scan right to find the rightmost non-\sqcupNon-\mathrm{x symbol (call it aa). If we cross #\# on the way, note its position.
  2. Return to the leftmost non-\sqcupNon-\mathrm{x symbol (call it bb).
  3. If aba \neq bReject.
  4. Cross off both aa and bb (write \mathrm{x).
  5. Repeat until only #\# (and \mathrm{xS) remain, then accept.

Correctness. If the input is w#wRw\#w^RThe first symbol of ww equals the last symbol of wRw^R (which is the first symbol of ww), the second equals the second-to-last, etc. Each Iteration verifies one pair. If any pair mismatches, the string is not of the form w#wRw\#w^R. \blacksquare

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 BB on input ww. This takes O(w)O(|w|) 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, L(B)=L(B) = \emptyset.

Proof (for \mathrm{EQ_{\mathrm{DFA}). Use the product construction to build a DFA for L(A)L(B)=(L(A)L(B))(L(A)L(B))L(A) \triangle L(B) = (L(A) \cap \overline{L(B)}) \cup (\overline{L(A)} \cap L(B)). Test if this DFA accepts any string (using the algorithm for E_{\mathrm{DFA}). \blacksquare

Additional decidable problems:

  • A_{\mathrm{REX} = \{\langle R, w \rangle : R \mathrm{ is a regex and w \in L(R)\} — convert RR to a DFA, then decide A_{\mathrm{DFA}.
  • E_{\mathrm{CFG} = \{\langle G \rangle : L(G) = \emptyset\} — test all derivations up to length 2V2^{|V|}.
  • \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 MM on ww. If MM accepts, accept. If MM rejects, reject. This recognises A_{\mathrm{TM}. \blacksquare

Proof of undecidability (by contradiction). Assume a decider HH for A_{\mathrm{TM} exists. Construct a TM DD that on input M\langle M \rangle:

  1. Run HH on M,M\langle M, \langle M \rangle \rangle.
  2. If HH accepts, reject.
  3. If HH rejects, accept.

Consider DD on input D\langle D \rangle:

  • If DD accepts D\langle D \rangleThen by construction DD rejects D\langle D \rangle. Contradiction.
  • If DD rejects D\langle D \rangleThen by construction DD accepts D\langle D \rangle. Contradiction.

Therefore HH cannot exist. \blacksquare

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. \blacksquare

5.3 Reductions and Undecidability

A reduction from language AA to language BB is a computable function ff that maps instances of AA to instances of BB such that wA    f(w)Bw \in A \iff f(w) \in B.

Theorem 5.3. If AmBA \leq_m B and BB is decidable, then AA is decidable.

Proof. To decide AA on input ww: compute f(w)f(w)Then decide BB on f(w)f(w). Since both steps are Computable, AA is decidable. \blacksquare

Corollary 5.4. If AmBA \leq_m B and AA is undecidable, then BB is undecidable.

Applications. Using reductions from A_{\mathrm{TM}We can prove many problems undecidable:

LanguageDescriptionReduction from
\mathrm{HALT_{\mathrm{TM}\{M, w : M \mathrm{ halts on w\}A_{\mathrm{TM}
E_{\mathrm{TM}{M:L(M)=}\{M : L(M) = \emptyset\}A_{\mathrm{TM}
\mathrm{REGULAR_{\mathrm{TM}\{M : L(M) \mathrm{ is regular\}A_{\mathrm{TM}
\mathrm{EQ_{\mathrm{TM}{M1,M2:L(M1)=L(M2)}\{M_1, M_2 : L(M_1) = L(M_2)\}E_{\mathrm{TM}

Example reduction. A_{\mathrm{TM} \leq_m \mathrm{HALT_{\mathrm{TM}.

Proof. Given M,w\langle M, w \rangleConstruct a TM MM' that on input xx: simulates MM on ww. If MM accepts, accept. If MM rejects, loop. Then \langle M, w \rangle \in A_{\mathrm{TM} iff MM' halts on some input (any input), iff \langle M' \rangle \in \mathrm{HALT_{\mathrm{TM}. \blacksquare

Worked Example: $A_{\mathrm{TM} \leq_m E_{\mathrm{TM}$

Proof. Given M,w\langle M, w \rangleConstruct a TM MwM_w that on input xx:

  1. Simulate MM on ww.
  2. If MM accepts wwAccept xx.
  3. If MM rejects wwReject xx.

Then L(Mw)=ΣL(M_w) = \Sigma^* if MM accepts wwAnd L(Mw)=L(M_w) = \emptyset if MM does not accept ww.

Therefore: \langle M, w \rangle \in A_{\mathrm{TM} iff L(Mw)L(M_w) \neq \emptyset Iff \langle M_w \rangle \notin E_{\mathrm{TM}.

The reduction f(M,w)=Mwf(\langle M, w \rangle) = \langle M_w \rangle 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. \blacksquare

Worked Example: $E_{\mathrm{TM} \leq_m \mathrm{EQ_{\mathrm{TM}$

Proof. Given M\langle M \rangleConstruct two TMs:

  • M1M_1: on any input, immediately rejects. So L(M1)=L(M_1) = \emptyset.
  • M2M_2: on any input, simulates MM and accepts iff MM accepts. So L(M2)=L(M)L(M_2) = L(M).

Then L(M)=L(M) = \emptyset iff L(M1)=L(M2)L(M_1) = L(M_2).

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. \blacksquare

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 PP is a set of Turing-recognisable languages. It is non-trivial if PP is neither Empty nor the set of all Turing-recognisable languages.

Proof (sketch). Let PP be a non-trivial property. Since PP is non-trivial, there exists a TM M0M_0 with L(M0)PL(M_0) \in P and a TM M1M_1 with L(M1)PL(M_1) \notin P. Given an arbitrary TM MM and input wwConstruct MwM_w that on input xx: first simulates MM on wwThen simulates M0M_0 on xx. If MM accepts wwThen L(Mw)=L(M0)PL(M_w) = L(M_0) \in P; if MM does not accept ww, L(Mw)=L(M_w) = \emptyset. If P\emptyset \notin PThen MwPM_w \in P iff MM accepts wwSo deciding PP would decide ATMA_{\mathrm{TM}}. The case P\emptyset \in P is similar. \blacksquare

Corollary. The following are undecidable: “Does MM accept at least one string?”, “Is L(M)L(M) Finite?”, “Is L(M)L(M) regular?”, “Is L(M)L(M) context-free?”

:::caution Common Pitfall Rice’s theorem applies only to properties of the language L(M)L(M)Not properties of the machine MM itself. For example, “Does MM halt within 100 steps on input ww?” is a property Of MM‘s behaviour, not of L(M)L(M)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 α=(α1,,αk)\alpha = (\alpha_1, \ldots, \alpha_k) and β=(β1,,βk)\beta = (\beta_1, \ldots, \beta_k) over some Alphabet Σ\Sigma. A solution is a non-empty sequence of indices i1,i2,,imi_1, i_2, \ldots, i_m such That:

αi1αi2αim=βi1βi2βim\alpha_{i_1} \alpha_{i_2} \cdots \alpha_{i_m} = \beta_{i_1} \beta_{i_2} \cdots \beta_{i_m}

The PCP language is PCP={α,β:α,βhaveasolution}\mathrm{PCP} = \{\langle \alpha, \beta \rangle : \alpha, \beta \mathrm{ have} a solution\}.

Example. α=(a,ab,bba)\alpha = (a, ab, bba), β=(ba,aa,bb)\beta = (ba, aa, bb). The sequence (2,1,1,3)(2, 1, 1, 3) gives abaabba=abaabbaab \cdot a \cdot a \cdot bba = abaabba and aabababb=aabababbaa \cdot ba \cdot ba \cdot bb = aabababb — not equal. The sequence (1,3,1)(1, 3, 1) gives abbaa=abbaaa \cdot bba \cdot a = abbaa and babbba=babbaba \cdot bb \cdot ba = babba — not equal. This instance may or may not have a solution; determining this is undecidable .

Worked Example: A solvable PCP instance

α=(b,ba,aab)\alpha = (b, ba, aab), β=(bb,aa,ba)\beta = (bb, aa, ba).

Try the sequence (2,3,1)(2, 3, 1):

  • Top: α2α3α1=baaabb=baaabb\alpha_2 \alpha_3 \alpha_1 = ba \cdot aab \cdot b = baaab b
  • Bottom: β2β3β1=aababb=aababb\beta_2 \beta_3 \beta_1 = aa \cdot ba \cdot bb = aababb

Not equal.

Try (3,2,1,3,2,1)(3, 2, 1, 3, 2, 1):

  • Top: aabbabaabbab=aabbaabaabbaab \cdot ba \cdot b \cdot aab \cdot ba \cdot b = aabbaabaabb
  • Bottom: baaabbbaaabb=baabbbbaabbba \cdot aa \cdot bb \cdot ba \cdot aa \cdot bb = baabbbbaabb

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 ATMA_{\mathrm{TM}}. Given TM MM and input wwConstruct a PCP instance Whose tiles encode the computation history of MM on ww. 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 MM accepts ww. The construction is computable, so if PCP were decidable, ATMA_{\mathrm{TM}} would Be decidable — contradiction. \blacksquare

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 MOM^O is a Turing machine with access to an oracle OO for a language OΣO \subseteq \Sigma^*. In addition to its ordinary transitions, MOM^O may enter a special “query state,” write a string qq on a query tape, and enter an “answer state” where the tape Contains 1 if qOq \in O and 0 if qOq \notin O. The oracle answers in one step.

Definition. AO={w:MOacceptsw}A^O = \{w : M^O \mathrm{ accepts} w\} for a fixed oracle TM MM and oracle OO.

Theorem 5.7. There exists an oracle AA such that PANPAP^A \neq NP^AAnd an oracle BB such That PB=NPBP^B = NP^B.

This result (Baker—Gill—Solovay, 1975) shows that resolving P=?NPP \stackrel{?}{=} NP will require Non-relativising techniques — proof methods that do not carry over in the presence of oracles.

The Turing jump. Given a language AADefine the halting problem relative to AA:

A' = \{\langle M^A, w \rangle : M^A \mathrm{ accepts w\}

Theorem 5.8. A̸TAA' \not\leq_T A (i.e., AA' is strictly more difficult than AA under Turing Reductions).

The arithmetical hierarchy is defined by iterating the jump: (0)=\emptyset^{(0)} = \emptyset (n+1)=((n))\emptyset^{(n+1)} = (\emptyset^{(n)})'. 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 BB is undecidable Using a reduction from a known undecidable problem AAYou need AmBA \leq_m BNot BmAB \leq_m A. Remember: if AmBA \leq_m B and AA is undecidable, then BB is undecidable (contrapositive of “if BB is decidable then AA is decidable”). Reversing the direction gives a valid implication (“if BmAB \leq_m A and AA is undecidable, then…”) that tells us nothing about BB. :::

6. Complexity Theory

6.1 Time Complexity

The running time of a deterministic TM MM on input ww is the number of steps MM takes before Halting. If MM never halts, the running time is \infty.

MM runs in time t(n)t(n) if for every input ww of length nn, MM halts within t(n)t(n) steps.

Theorem 6.1. Let t(n)t(n) be a function with t(n)nt(n) \geq n. Every TM that runs in time t(n)t(n) has An equivalent single-tape TM that runs in time O(t2(n))O(t^2(n)).

Proof. A kk-tape TM running in time t(n)t(n) uses at most t(n)t(n) tape cells on each tape. Simulating One step of the kk-tape machine requires scanning the single-tape simulation from left to right To read all kk heads, then left to right again to update them. This costs O(t(n))O(t(n)) per simulated Step. Over t(n)t(n) steps, the total is O(t(n)2)O(t(n)^2). \blacksquare

Theorem 6.1a (Time Hierarchy Theorem). If t1,t2t_1, t_2 are time-constructible functions with t1(n)logt1(n)=o(t2(n))t_1(n) \log t_1(n) = o(t_2(n))Then \mathrm{TIME(t_1(n)) \subsetneq \mathrm{TIME(t_2(n)).

Proof (idea). Use diagonalisation. Construct a TM DD that on input xx of length nn:

  1. Compute t2(n)t_2(n) (possible since t2t_2 is time-constructible).
  2. Simulate all TMs M1,M2,M_1, M_2, \ldots in parallel for t1(n)t_1(n) steps on input xx.
  3. If any MiM_i accepts xx within t1(n)t_1(n) steps, DD does the opposite (reject).
  4. Otherwise, accept.

DD runs in time O(t2(n))O(t_2(n)) and differs from every TM that runs in time O(t1(n))O(t_1(n)) on at least One input. \blacksquare

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

P=k1TIME(nk)\mathrm{P} = \bigcup_{k \geq 1} \mathrm{TIME}(n^k)

\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): O(V+E)O(V + E).
  • Shortest paths (Dijkstra): O((V+E)logV)O((V + E) \log V).
  • Sorting: O(nlogn)O(n \log n).
  • 2-SAT: O(n+m)O(n + m).
  • Primality testing: O(log6n)O(\log^6 n) (AKS algorithm).

6.3 The Class NP

NP=k1NTIME(nk)\mathrm{NP} = \bigcup_{k \geq 1} \mathrm{NTIME}(n^k)

Equivalent definition. A language LL is in NP if there exists a polynomial-time verifier VV And a polynomial pp such that:

L={w:cwithcp(w)andV(w,c)=accept}L = \{w : \exists c \mathrm{ with} |c| \leq p(|w|) \mathrm{ and} V(w, c) = \mathrm{accept}\}

The string cc 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. \blacksquare

Theorem 6.2a. If ApBA \leq_p B and B \in \mathrm{NPThen A \in \mathrm{NP.

Proof. Let VBV_B be the polynomial-time verifier for BB and ff be the polynomial-time reduction. Then VA(w,c)=VB(f(w),c)V_A(w, c) = V_B(f(w), c) is a polynomial-time verifier for AA. \blacksquare

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 BB is NP-complete if:

  1. B \in \mathrm{NPAnd
  2. ApBA \leq_p B 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).

  1. \mathrm{SAT \in \mathrm{NP: a satisfying assignment is a polynomial-size certificate that can be verified in polynomial time.

  2. For any L \in \mathrm{NPThere is a polynomial-time NTM NN that decides LL in time nkn^k for some kk. Given input ww of length nnConstruct a Boolean formula ϕN,w\phi_{N,w} that is satisfiable iff NN accepts ww.

The formula encodes the tableau of NN on ww: a table of size nk×nkn^k \times n^k where each cell T[i,j]T[i, j] records the symbol at tape cell jj after step ii of the computation.

Variables: For each position (i,j)(i, j) in the tableau and each symbol ss in the combined state-tape alphabet Γ=Q×Γ\Gamma' = Q \times \GammaA variable xi,j,sx_{i,j,s} indicating that cell (i,j)(i, j) contains ss.

Constraints:

  • Well-formedness: Each cell contains exactly one symbol. For each (i,j)(i, j): exactly one of \{x_{i,j,s} : s \in \Gamma} is true.
  • Start configuration: Row 0 encodes q0q_0 at position 0, w1w_1 at position 1, etc.
  • Transition correctness: For every 2×32 \times 3 window of the tableau, the bottom row must be a valid successor of the top row according to NN‘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 O(n2k)O(n^{2k})Which is polynomial in w|w|. \blacksquare

Worked Example: Cook-Levin tableau construction (simplified)

Consider an NTM NN that decides a language LL in time n2n^2. On input w=01w = 01 (length n=2n = 2), The tableau is a 4×44 \times 4 grid (since n2=4n^2 = 4).

The formula ϕN,w\phi_{N,w} has variables for each cell. For instance, x0,0,q0x_{0,0,q_0} indicates That position (0,0)(0,0) contains the start state. The constraints enforce:

  • Row 0: q0q_0 at position 0, 0 at position 1, 1 at position 2, \sqcup at position 3.
  • Transition windows: each 2×32 \times 3 block must be consistent with δ\delta.
  • Row 3: at least one cell contains q_{\mathrm{accept}.

If NN accepts wwThen the accepting computation path provides a satisfying assignment (the tableau records that path). If ϕN,w\phi_{N,w} 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 ApBA \leq_p B and B \in \mathrm{PThen A \in \mathrm{P.

Proof. To decide AA on input ww: compute f(w)f(w) in polynomial time (the reduction), then decide BB on f(w)f(w) in polynomial time. Total: polynomial time. \blacksquare

6.5 Classic NP-Complete Problems

3-SAT. Given a CNF formula with exactly 3 literals per clause, is it satisfiable?

Reduction: SAT p\leq_p 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 p\leq_p 3-SAT.

Proof. Given a CNF formula ϕ\phiConvert each clause to exactly 3 literals:

  • Clause (l1)(l_1) (1 literal): replace with (l1ab)(l1abˉ)(l1aˉb)(l1aˉbˉ)(l_1 \lor a \lor b) \land (l_1 \lor a \lor \bar{b}) \land (l_1 \lor \bar{a} \lor b) \land (l_1 \lor \bar{a} \lor \bar{b}) where a,ba, b are new variables. This is satisfiable iff l1l_1 is true.
  • Clause (l1l2)(l_1 \lor l_2) (2 literals): replace with (l1l2a)(l1l2aˉ)(l_1 \lor l_2 \lor a) \land (l_1 \lor l_2 \lor \bar{a}) where aa is new. Satisfiable iff l1l2l_1 \lor l_2 is true.
  • Clause (l1l2l3)(l_1 \lor l_2 \lor l_3): leave as is.
  • Clause (l1lk)(l_1 \lor \cdots \lor l_k) for k>3k \gt 3: replace with (l1l2z1)(zˉ1l3z2)(zˉk4lk1lk)(l_1 \lor l_2 \lor z_1) \land (\bar{z}_1 \lor l_3 \lor z_2) \land \cdots \land (\bar{z}_{k-4} \lor l_{k-1} \lor l_k) where ziz_i are new variables. Satisfiable iff the original clause is satisfiable.

Each transformation is polynomial-size and preserves satisfiability. \blacksquare

Vertex Cover. Given a graph G=(V,E)G = (V, E) and integer kkIs there a vertex cover of size k\leq k?

Theorem 6.5b. 3-SAT p\leq_p Vertex Cover.

Proof (sketch). Given a 3-CNF formula ϕ\phi with kk clauses and nn variables, construct a graph GG:

  1. For each variable xix_iCreate a variable gadget: two vertices xix_i and xˉi\bar{x}_i connected by an edge. Selecting xix_i into the cover corresponds to setting xix_i to true (removing xˉi\bar{x}_i from consideration).
  2. For each clause Cj=(lalblc)C_j = (l_a \lor l_b \lor l_c)Create a clause gadget: a triangle of three vertices connected to the corresponding literal vertices in the variable gadgets.
  3. Set the target: k=n+2kk' = n + 2k.

The cover must include at least one endpoint of each variable-gadget edge (nn vertices) and at Least two vertices from each clause triangle (2k2k 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. \blacksquare

Clique. Given G=(V,E)G = (V, E) and integer kkDoes GG contain a clique of size kk?

Reduction: Vertex Cover p\leq_p Clique. G=(V,E)G = (V, E) has a vertex cover of size kk iff G=(V,E)\overline{G} = (V, \overline{E}) has a clique of size Vk|V| - k.

Proof. If CVC \subseteq V is a vertex cover of size kk in GGThen every edge of GG has at Least one endpoint in CC. So VCV \setminus C is an independent set in GGMeaning every pair in VCV \setminus C is an edge in G\overline{G}. Hence G\overline{G} has a clique of size Vk|V| - k. The converse is analogous. \blacksquare

Hamiltonian Path. Given a graph G=(V,E)G = (V, E)Does GG have a path visiting every vertex exactly Once?

Theorem 6.5c. Vertex Cover p\leq_p Hamiltonian Path (via Hamiltonian Cycle).

Proof (sketch). Given (G,k)(G, k) for Vertex Cover, construct a graph GG' such that GG has a Vertex cover of size kk iff GG' has a Hamiltonian cycle. The construction uses selection gadgets That choose kk 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. \blacksquare

Subset Sum. Given integers S={s1,,sn}S = \{s_1, \ldots, s_n\} and target TTIs there a subset summing To TT?

Theorem 6.5d. 3-SAT p\leq_p Subset Sum.

Proof (sketch). Given a 3-CNF formula with variables x1,,xnx_1, \ldots, x_n and clauses C1,,CkC_1, \ldots, C_kConstruct a set of numbers SS and target TT in decimal.

For each variable xix_iCreate two numbers viv_i and vˉi\bar{v}_i. In the “variable digits” (first nn columns), viv_i has a 1 in column ii and 0 elsewhere; vˉi\bar{v}_i also has a 1 in column ii and 0 elsewhere. This forces choosing exactly one of vi,vˉiv_i, \bar{v}_i.

For each clause CjC_jAdd a “clause digit” (column n+jn + j): in viv_i (resp. vˉi\bar{v}_i), This digit is 1 iff xix_i (resp. xˉi\bar{x}_i) appears in CjC_j. The target TT has 1 in Every digit. Choosing viv_i or vˉi\bar{v}_i contributes to satisfying the clauses that contain That literal. \blacksquare

Partition. Given integers S={s1,,sn}S = \{s_1, \ldots, s_n\}Can SS be partitioned into two subsets of Equal sum?

Reduction chain:

SAT3SATVertexCoverClique\mathrm{SAT} \to \mathrm{3}\mathrm{-SAT} \to \mathrm{VertexCover} \to \mathrm{Clique}

SAT3SATHamiltonianPath\mathrm{SAT} \to \mathrm{3}\mathrm{-SAT} \to \mathrm{HamiltonianPath}

SAT3SATSubsetSumPartition\mathrm{SAT} \to \mathrm{3}\mathrm{-SAT} \to \mathrm{SubsetSum} \to \mathrm{Partition}

SAT3SATSubsetSumPartition\mathrm{SAT} \to \mathrm{3}\mathrm{-SAT} \to \mathrm{SubsetSum} \to \mathrm{Partition}

Worked Example: Reducing 3-SAT to Independent Set

Given a 3-CNF formula ϕ\phi with kk clauses, construct a graph GG:

  1. For each clause CjC_jCreate a group of 3 vertices (one per literal).
  2. Within each group, add all three edges (forming a triangle) — at most one vertex per group can be in an independent set.
  3. For each pair of contradictory literals (xix_i and xˉi\bar{x}_i) in different groups, add an edge — they cannot both be selected.

Set the target to kk (one vertex per clause). An independent set of size kk exists iff ϕ\phi Is satisfiable: selecting one literal per clause without contradiction gives a consistent Satisfying assignment. \blacksquare

6.6 Space Complexity

Definition. \mathrm{SPACE(s(n)) is the class of languages decidable by a deterministic TM Using O(s(n))O(s(n)) 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 s(n)logns(n) \geq \log n.

Proof (sketch). To decide whether an NTM using space s(n)s(n) accepts, we check reachability in the Configuration graph. The graph has at most N=Γs(n)s(n)QN = |\Gamma|^{s(n)} \cdot s(n) \cdot |Q| nodes, where Γ|\Gamma| is the tape alphabet size and Q|Q| the number of states. A deterministic TM can decide Reachability using the recursive algorithm \mathrm{REACH(u, v, d) (can uu reach vv in at most dd steps?), which uses O(logN)=O(s(n))O(\log N) = O(s(n)) space and recurses to depth O(logN)O(\log N). Total space: O(s(n)logN)=O(s(n)2)O(s(n) \cdot \log N) = O(s(n)^2). \blacksquare

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. \blacksquare

NL-completeness. A language BB is NL-complete if B \in \mathrm{NL and every A \in \mathrm{NL is log-space reducible to BB.

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 LLConstruct an NTM for L\overline{L} 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 ϕ=Q1x1Q2x2Qnxnψ(x1,,xn)\phi = Q_1 x_1 Q_2 x_2 \cdots Q_n x_n \, \psi(x_1, \ldots, x_n) where Qi{,}Q_i \in \{\forall, \exists\} and ψ\psi is a CNF formula, is ϕ\phi true?

Theorem 6.9. TQBF is PSPACE-complete.

Proof (membership). Evaluate the quantifiers recursively. For xiϕ\exists x_i \phiTry both values Of xix_i and recurse. For xiϕ\forall x_i \phiSimilarly. At depth nnEvaluate ψ\psi. Each level Uses O(n)O(n) space to store the current assignment, giving O(n2)O(n^2) total.

Proof (hardness). Reduce from any L \in \mathrm{PSPACE using the configuration graph. A Computation of a PSPACE TM on input ww of length nn uses at most p(n)p(n) cells for some Polynomial pp. The number of distinct configurations is at most N=Γp(n)p(n)QN = |\Gamma|^{p(n)} \cdot p(n) \cdot |Q| Which is exponential. The statement ”MM accepts ww” can be expressed as: “there exists a Configuration c1c_1 reachable from the start configuration in N\leq N steps such that for all Configurations c2c_2 reachable from c1c_1 in one step, there exists a configuration c3c_3…” This alternating reachability formula can be encoded as a quantified Boolean formula of Polynomial size. \blacksquare

Worked Example: Evaluating a QBF formula

Evaluate ϕ=xy[(xy)(xˉy)]\phi = \forall x \exists y \, [(x \lor y) \land (\bar{x} \lor y)].

  • For x=0x = 0: need y\exists y such that (0 \lor y) \land (1 \lor y) = y \land \mathrm{true = y. So we need y=1y = 1 (which exists).
  • For x=1x = 1: need y\exists y such that (1 \lor y) \land (0 \lor y) = \mathrm{true \land y = y. So we need y=1y = 1 (which exists).

Since both values of xx lead to a satisfying assignment for yy, ϕ\phi 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 ΣkP\Sigma_k^P and ΠkP\Pi_k^P inductively:

  • Σ0P=Π0P=P\Sigma_0^P = \Pi_0^P = \mathrm{P}.
  • Σk+1P=NPΣkP\Sigma_{k+1}^P = \mathrm{NP}^{\Sigma_k^P} (NP with a ΣkP\Sigma_k^P oracle).
  • Πk+1P=coNPΣkP{\Pi_{k+1}^P} = \mathrm{coNP}^{\Sigma_k^P} (coNP with a ΣkP\Sigma_k^P oracle).
  • PH=k0ΣkP\mathrm{PH} = \bigcup_{k \geq 0} \Sigma_k^P.

Equivalent characterisation. A language LL is in ΣkP\Sigma_k^P iff there exist polynomial-time Computable relations RR and polynomials pp such that:

L={x:y1y2y3QkykR(x,y1,,yk)}L = \{x : \exists y_1 \forall y_2 \exists y_3 \cdots Q_k y_k \, R(x, y_1, \ldots, y_k)\}

Where each yip(x)|y_i| \leq p(|x|) and the quantifiers alternate, starting with \exists.

Examples:

  • Σ1P=NP\Sigma_1^P = \mathrm{NP}: “there exists a certificate.”
  • Π1P=coNP\Pi_1^P = \mathrm{coNP}: “for all certificates.”
  • Σ2P\Sigma_2^P 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).
  • Π2P\Pi_2^P 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 ΣkP=Σk+1P\Sigma_k^P = \Sigma_{k+1}^P for some kkThen PH=ΣkP\mathrm{PH} = \Sigma_k^P (the polynomial hierarchy collapses to level kk).

Proof. If ΣkP=Σk+1P=NPΣkP\Sigma_k^P = \Sigma_{k+1}^P = \mathrm{NP}^{\Sigma_k^P}Then the ΣkP\Sigma_k^P oracle Provides no additional power. By induction, Σk+iP=ΣkP\Sigma_{k+i}^P = \Sigma_k^P for all i0i \geq 0 So PH=ΣkP\mathrm{PH} = \Sigma_k^P. \blacksquare

It is widely believed that PH\mathrm{PH} 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).
  • PNPcoNP\mathrm{P} \subseteq \mathrm{NP} \cap \mathrm{coNP}.
  • It is unknown whether NP=coNP\mathrm{NP} = \mathrm{coNP}. If P=NP\mathrm{P} = \mathrm{NP}Then NP=coNP\mathrm{NP} = \mathrm{coNP}.

Theorem 6.11. If NPcoNP\mathrm{NP} \neq \mathrm{coNP}Then PNP\mathrm{P} \neq \mathrm{NP}.

Proof. If P=NP\mathrm{P} = \mathrm{NP}Then P=coNP\mathrm{P} = \mathrm{coNP} (since P\mathrm{P} Is closed under complement), so NP=coNP\mathrm{NP} = \mathrm{coNP}. The contrapositive gives the Result. \blacksquare

PSPACE. The class of languages decidable in polynomial space:

\mathrm{PSPACE = \bigcup_{k \geq 1} \mathrm{SPACE(n^k)

  • PNPPSPACE\mathrm{P} \subseteq \mathrm{NP} \subseteq \mathrm{PSPACE}.
  • PPSPACE\mathrm{P} \neq \mathrm{PSPACE} (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})

  • PNPPSPACEEXPTIME\mathrm{P} \subseteq \mathrm{NP} \subseteq \mathrm{PSPACE} \subseteq \mathrm{EXPTIME}.
  • PEXPTIME\mathrm{P} \neq \mathrm{EXPTIME} (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

InclusionKnown to be proper?Theorem used
RegularCFL\mathrm{Regular} \subseteq \mathrm{CFL}YesPumping lemma
CFLDecidable\mathrm{CFL} \subseteq \mathrm{Decidable}YesCYK algorithm
DecidableTMrecognisable\mathrm{Decidable} \subseteq \mathrm{TM}\mathrm{-recognisable}YesDiagonalisation
PEXPTIME\mathrm{P} \subseteq \mathrm{EXPTIME}YesTime hierarchy
PPSPACE\mathrm{P} \subseteq \mathrm{PSPACE}YesSpace hierarchy
NPPSPACE\mathrm{NP} \subseteq \mathrm{PSPACE}YesSavitch’s corollary
LNL\mathrm{L} \subseteq \mathrm{NL}Unknown
PNP\mathrm{P} \subseteq \mathrm{NP}UnknownOpen problem
NPcoNP\mathrm{NP} \subseteq \mathrm{coNP}UnknownOpen problem

Both inclusions PNP\mathrm{P} \subseteq \mathrm{NP} and NPPSPACE\mathrm{NP} \subseteq \mathrm{PSPACE} are Known to be proper (PPSPACE\mathrm{P} \neq \mathrm{PSPACE}), but the status of P\mathrm{P} vs. NP\mathrm{NP} 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 Σ={0,1}\Sigma = \{0, 1\} that accepts exactly those strings whose Length is a multiple of 3. Prove your DFA is correct.

Problem 2. Let L={w{0,1}:wcontainsanevennumberof0sandendswith1}L = \{w \in \{0,1\}^* : w \mathrm{ contains} an even number of 0\mathrm{s} and \mathrm{ ends} with 1\}. Give a DFA with the minimum number of states for LL.

Problem 3. Use the Myhill-Nerode theorem to prove that L={0n12n:n0}L = \{0^n 1^{2n} : n \geq 0\} is not regular.

Problem 4. Prove or disprove: if L1L2L_1 \cdot L_2 is regular, then both L1L_1 and L2L_2 are regular.

7.2 Context-Free Languages

Problem 5. Give a CFG for L={aibjck:i+j=k}L = \{a^i b^j c^k : i + j = k\}. Prove your grammar generates Exactly this language.

Problem 6. Convert the following grammar to CNF: SaSbSεS \to aSbS \mid \varepsilon. Show all steps of the conversion.

Problem 7. Use the CFL pumping lemma to prove that L={aibjaibj:i,j1}L = \{a^i b^j a^i b^j : i, j \geq 1\} is not context-free.

Problem 8. Construct a PDA for L={anbm:n2m}L = \{a^n b^m : n \leq 2m\}. 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 L={02n:n0}L = \{0^{2^n} : n \geq 0\}. Describe the algorithm and prove it always halts.

Problem 10. Prove that the language L={M1,M2:L(M1)L(M2)}L = \{\langle M_1, M_2 \rangle : L(M_1) \cap L(M_2) \neq \emptyset\} is undecidable.

Problem 11. Use Rice’s theorem to prove that L={M:L(M)containsatleasttwostrings}L = \{\langle M \rangle : L(M) \mathrm{ contains} at least two strings\} is undecidable. Explain why Rice’s theorem applies.

Problem 12. Show that the PCP instance with α=(01,0,1)\alpha = (01, 0, 1) and β=(0,10,01)\beta = (0, 10, 01) Has a solution by finding one, or prove it has no solution.

7.4 Complexity Theory

Problem 13. Show that if P=NP\mathrm{P} = \mathrm{NP}Then NP=coNP\mathrm{NP} = \mathrm{coNP}.

Problem 14. A 3-colouring of a graph G=(V,E)G = (V, E) is a function c:V{1,2,3}c : V \to \{1, 2, 3\} Such that c(u)c(v)c(u) \neq c(v) for every edge (u,v)E(u, v) \in E. Show that 3-SAT p\leq_p 3-Colouring By describing the reduction construction.

Problem 15. Prove that CLIQUE\mathrm{CLIQUE} is self-reducible: given an oracle for CLIQUE\mathrm{CLIQUE}Describe a polynomial-time algorithm to find an actual clique of size kk (if one exists).

Problem 16. Using Savitch’s theorem, prove that NLP\mathrm{NL} \subseteq \mathrm{P}. What is the time complexity of your algorithm?

Problem 17. Define the language EXACTCLIQUE={G,k:G\mathrm{EXACT}\mathrm{-CLIQUE} = \{\langle G, k \rangle : G hasacliqueofexactlysizek}\mathrm{ has} a clique of exactly size k\}. Show that EXACTCLIQUE\mathrm{EXACT}\mathrm{-CLIQUE} is NP-complete.

Problem 18. A language LL is in DP (difference of two NP sets) if there exist L1,L2NPL_1, L_2 \in \mathrm{NP} such that L=L1L2L = L_1 \cap \overline{L_2}. Show that SATUNSAT={ϕ,ψ:ϕSATandψSAT}\mathrm{SAT}\mathrm{-UNSAT} = \{\langle \phi, \psi \rangle : \phi \in \mathrm{SAT} \mathrm{ and} \psi \notin \mathrm{SAT}\} is in DP. Is DP contained in Σ2P\Sigma_2^P? Justify.

7.5 Comprehensive

Problem 19. Consider the language L={w#w:w{0,1}}L = \{w \# w : w \in \{0,1\}^*\}. (a) Prove LL is not regular using the pumping lemma. (b) Give a CFG for LL and prove it is correct. (c) Is LL decidable? Justify.

Problem 20. For each of the following languages, state the smallest complexity class (from Regular\mathrm{Regular}, \mathrm{CFL, Decidable\mathrm{Decidable}, \mathrm{NP \mathrm{PSPACE, EXPTIME\mathrm{EXPTIME}Or “undecidable”) that is known to contain it. Justify each answer briefly.

(a) {0n1n0n:n0}\{0^n 1^n 0^n : n \geq 0\} (b) {G:GhasaHamiltoniancycle}\{\langle G \rangle : G \mathrm{ has} a Hamiltonian cycle\} (c) {G,k:Ghasavertexcoverofsizek}\{\langle G, k \rangle : G \mathrm{ has} a vertex cover of size \leq k\} (d) {M:Mrunsforatmost100stepsonε}\{\langle M \rangle : M \mathrm{ runs} for at most 100 \mathrm{ steps} on \varepsilon\} (e) {ϕ:ϕisatruequantifiedBooleanformula}\{\langle \phi \rangle : \phi \mathrm{ is} a true quantified Boolean formula\}

7.6 Selected Solutions and Hints

Problem 1. States q0,q1,q2q_0, q_1, q_2 with transitions δ(qi,a)=q(i+1)mod3\delta(q_i, a) = q_{(i+1) \bmod 3} for All a{0,1}a \in \{0, 1\}. Accept state: q0q_0. Proof by induction on the number of symbols read.

Problem 3. The strings 01,02,03,0^1, 0^2, 0^3, \ldots are pairwise distinguishable: for i<ji \lt j The suffix 12i1^{2i} distinguishes 0i0^i from 0j0^j since 0i12iL0^i 1^{2i} \in L but 0j12iL0^j 1^{2i} \notin L (because 2i2j2i \neq 2j).

Problem 4. Disproof: let L1={0n1n:n0}L_1 = \{0^n 1^n : n \geq 0\} (not regular) and L2=L_2 = \emptyset (regular). Then L1L2=L_1 \cdot L_2 = \emptyset is regular, but L1L_1 is not.

Problem 7. Let w=apbpapbpw = a^p b^p a^p b^p with pumping length pp. Since vxyp|vxy| \leq pThe Substring vxyvxy cannot span all four blocks. Case analysis shows that pumping any valid Decomposition produces a string not in LL.

Problem 10. Reduce from ETME_{\mathrm{TM}}. Given M\langle M \rangleConstruct two TMs M1M_1 (accepts ε\varepsilon only) and M2M_2 (accepts what MM accepts). Then L(M1)L(M2)L(M_1) \cap L(M_2) \neq \emptyset iff MM accepts ε\varepsilon iff METM\langle M \rangle \notin E_{\mathrm{TM}} (after adjusting for the specific reduction).

Problem 13. If P=NP\mathrm{P} = \mathrm{NP}Then for any LNPL \in \mathrm{NP}We have LPL \in \mathrm{P}. Since P\mathrm{P} is closed under complement, LPNP\overline{L} \in \mathrm{P} \subseteq \mathrm{NP}. So LNP\overline{L} \in \mathrm{NP} for every LNPL \in \mathrm{NP}, meaning NPcoNP\mathrm{NP} \subseteq \mathrm{coNP}. By symmetry, coNPNP\mathrm{coNP} \subseteq \mathrm{NP}.

Problem 19. (a) Let w=0p1p#0p1pLw = 0^p 1^p \# 0^p 1^p \in L. Since xyp|xy| \leq p, yy is in the first 0p0^p block. Pumping down gives 0pk1p#0p1pL0^{p-k}1^p\#0^p1^p \notin L. (b) S0S01S1S#SεS \to 0S0 \mid 1S1 \mid S\#S \mid \varepsilon (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

  1. Neglecting to normalise database designs, leading to data redundancy and update anomalies.

  2. Confusing an algorithm with a program — an algorithm is a step-by-step procedure, not its implementation in code.

  3. Writing pseudocode that is too language-specific rather than using standard algorithmic constructs.

  4. Mixing up Big O, Big Ω\Omega, and Big Θ\Theta 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 {0,1}\{0, 1\} where the number of 1s is divisible by 3.

Solution. Three states: q0q_0 (0 mod 3, accept), q1q_1 (1 mod 3), q2q_2 (2 mod 3).

  • δ(q0,0)=q0\delta(q_0, 0) = q_0, δ(q0,1)=q1\delta(q_0, 1) = q_1
  • δ(q1,0)=q1\delta(q_1, 0) = q_1, δ(q1,1)=q2\delta(q_1, 1) = q_2
  • δ(q2,0)=q2\delta(q_2, 0) = q_2, δ(q2,1)=q0\delta(q_2, 1) = q_0

Accept state: {q0}\{q_0\}. Strings like ϵ\epsilon, 111111, 0111001110 are accepted.

\blacksquare

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 kk clauses, construct a graph:

  1. For each clause, create a triangle of 3 vertices (one per literal).
  2. Add edges between contradictory literals (xix_i and ¬xi\neg x_i) across different clauses.

The formula is satisfiable iff the graph has an independent set of size kk (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.

\blacksquare

Summary

  • Finite automata: DFA (deterministic, O(n)O(n) recognition) and NFA (non-deterministic); equivalent in power via subset construction (2n2^n states worst case).
  • Regular expressions: describe regular languages; pumping lemma proves non-regularity for languages like {anbn:n0}\{a^n b^n : n \geq 0\}.
  • 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

TopicSiteLink
Discrete MathematicsWyattsNotesView
Algorithms and Data StructuresWyattsNotesView
Advanced AlgorithmsWyattsNotesView
Theory of Computation — MIT 18.404MITView