Level 5 — MasteryTheory of Computation

Theory of Computation

90 minutes60 marksprintable — key stays hidden on paper

Time limit: 90 minutes Total marks: 60 Instructions: Answer all three questions. Rigour, precision of definitions, and completeness of proofs are assessed. You may use standard closure results if stated explicitly.


Question 1 — Automata, Subset Construction & Non-Regularity (20 marks)

Consider the language over the alphabet Σ={a,b}\Sigma = \{a, b\}: L={wΣ:w contains aba as a substring}.L = \{\, w \in \Sigma^* : w \text{ contains } aba \text{ as a substring} \,\}.

(a) Design an NFA (with ε\varepsilon-transitions permitted but not required) accepting LL. Give it as a formal 5-tuple (Q,Σ,δ,q0,F)(Q, \Sigma, \delta, q_0, F) and draw its state diagram. (4 marks)

(b) Apply the subset construction to convert your NFA to a DFA. Show the reachable subset table and give the final DFA's state diagram. (6 marks)

(c) Write a regular expression for LL and justify (briefly) why it is equivalent to your automaton. (3 marks)

(d) Now define M={anbnan:n0}M = \{\, a^n b^n a^n : n \geq 0 \,\}. Prove that MM is not regular using the pumping lemma. State the lemma precisely and justify your choice of pumped string. (7 marks)


Question 2 — Grammars, CNF & the CFL Pumping Lemma (20 marks)

(a) Give a context-free grammar GG generating L1={aibjck:i=j or j=k, i,j,k0}L_1 = \{\, a^i b^j c^k : i = j \text{ or } j = k,\ i,j,k \geq 0 \,\}. Show a leftmost derivation of aabbccaabbcc and of abccabcc. (5 marks)

(b) Convert the grammar SaSbεS \to aSb \mid \varepsilon into Chomsky Normal Form. Show every intermediate step (removal of ε\varepsilon, unit productions, terminals, long RHS). (7 marks)

(c) Prove that L2={anbncn:n0}L_2 = \{\, a^n b^n c^n : n \geq 0 \,\} is not context-free using the pumping lemma for CFLs. State the lemma with pumping length pp and analyse all cases of the decomposition uvxyzuvxyz. (8 marks)


Question 3 — Computability, Reductions & Complexity (20 marks)

(a) State the Halting Problem HALTTMHALT_{TM} as a language and prove it is undecidable by diagonalization. Make the contradiction machine explicit. (7 marks)

(b) State Rice's theorem precisely (semantic, non-trivial property). Use it to show that Lempty={M:L(M)=}L_{\text{empty}} = \{\, \langle M \rangle : L(M) = \varnothing \,\} is undecidable, or explain precisely why Rice's theorem does not directly settle it — whichever is correct. (5 marks)

(c) Define a many-one (mapping) reduction AmBA \leq_m B. Prove: if AmBA \leq_m B and BB is decidable, then AA is decidable. State the contrapositive form used to prove undecidability. (4 marks)

(d) State what it means for a language to be NP-complete. Given that 3-SAT is NP-complete, outline the reduction 3-SATpCLIQUE3\text{-SAT} \leq_p \text{CLIQUE}: describe the graph construction and state (with brief justification) the correctness equivalence. (4 marks)

Answer keyMark scheme & solutions

Question 1

(a) (4 marks) NFA: Q={q0,q1,q2,q3}Q=\{q_0,q_1,q_2,q_3\}, Σ={a,b}\Sigma=\{a,b\}, start q0q_0, F={q3}F=\{q_3\}. δ(q0,a)={q0,q1}, δ(q0,b)={q0}, δ(q1,b)={q2}, δ(q2,a)={q3}, δ(q3,a)={q3}, δ(q3,b)={q3}.\delta(q_0,a)=\{q_0,q_1\},\ \delta(q_0,b)=\{q_0\},\ \delta(q_1,b)=\{q_2\},\ \delta(q_2,a)=\{q_3\},\ \delta(q_3,a)=\{q_3\},\ \delta(q_3,b)=\{q_3\}. Idea: stay in q0q_0 (self-loop on a,ba,b) guessing where abaaba starts; q1q_1 after aa, q2q_2 after abab, q3q_3 after abaaba then loop. Marks: correct states/start/final (2), transitions realizing "contains abaaba" (2).

(b) (6 marks) Subset construction from start {q0}\{q_0\}:

Subset on aa on bb
A={q0}A=\{q_0\} {q0,q1}=B\{q_0,q_1\}=B {q0}=A\{q_0\}=A
B={q0,q1}B=\{q_0,q_1\} {q0,q1}=B\{q_0,q_1\}=B {q0,q2}=C\{q_0,q_2\}=C
C={q0,q2}C=\{q_0,q_2\} {q0,q1,q3}=D\{q_0,q_1,q_3\}=D {q0}=A\{q_0\}=A
D={q0,q1,q3}D=\{q_0,q_1,q_3\} {q0,q1,q3}=D\{q_0,q_1,q_3\}=D {q0,q2,q3}=E\{q_0,q_2,q_3\}=E
E={q0,q2,q3}E=\{q_0,q_2,q_3\} {q0,q1,q3}=D\{q_0,q_1,q_3\}=D {q0,q3}=F\{q_0,q_3\}=F
F={q0,q3}F=\{q_0,q_3\} {q0,q1,q3}=D\{q_0,q_1,q_3\}=D {q0,q3}=F\{q_0,q_3\}=F

Accepting DFA states = those containing q3q_3: {D,E,F}\{D,E,F\}. Start AA. Marks: correct start & closure procedure (2), correct table (3), correct accepting set (1). (Once q3q_3 reached, all reachable subsets contain q3q_3 — a dead-accepting sink region, as expected for "contains substring.")

(c) (3 marks) r=(ab)aba(ab)r = (a\mid b)^*\,aba\,(a\mid b)^*. Justification: (ab)(a\mid b)^* before and after matches arbitrary prefix/suffix; the literal abaaba forces the required substring — exactly the strings containing abaaba. Kleene's theorem guarantees RE ⇔ FA equivalence. Marks: RE (2), justification (1).

(d) (7 marks) Pumping lemma (regular): If MM is regular, p1\exists p\ge1 s.t. every wMw\in M with wp|w|\ge p can be written w=xyzw=xyz with (i) xyp|xy|\le p, (ii) y1|y|\ge1, (iii) xyizMxy^iz\in M for all i0i\ge0. (2 marks)

Assume MM regular with pumping length pp. Choose w=apbpapMw=a^p b^p a^p\in M, w=3pp|w|=3p\ge p. (1) By (i)–(ii), xyxy lies within the first pp symbols, all aa's, so y=aky=a^k with 1kp1\le k\le p, and yy is a prefix-block of the first apa^p. (2) Pump i=2i=2: xy2z=ap+kbpapxy^2z=a^{p+k}b^p a^p. Now the count of leading aa's is p+kpp+k\ne p (since k1k\ge1), while the trailing block is still apa^p; a member of MM requires the first and last blocks equal to the middle-count pp. Hence xy2zMxy^2z\notin M. (2) Contradiction ⇒ MM is not regular. \blacksquare (Deduct if wrong string chosen, e.g. one that pumps within a single equal block trivially.)


Question 2

(a) (5 marks) Grammar (union of two CFLs): SS1S2,S1AC, AaAbε, CcCε,S2AB, AaAε, BbBcε.S\to S_1 \mid S_2,\quad S_1\to A\,C',\ A\to aAb\mid\varepsilon,\ C'\to cC'\mid\varepsilon,\quad S_2\to A'\,B,\ A'\to aA'\mid\varepsilon,\ B\to bBc\mid\varepsilon. Here S1S_1 gives i=ji=j (with free kk via CC'), S2S_2 gives j=kj=k (with free ii via AA'). (3 marks for correct grammar)

Leftmost derivation of aabbccaabbcc (use S1S_1, i=j=2,k=2i=j=2,k=2): SS1ACaAbCaaAbbCaabbCaabbcCaabbccCaabbcc.S\Rightarrow S_1\Rightarrow AC'\Rightarrow aAbC'\Rightarrow aaAbbC'\Rightarrow aabbC'\Rightarrow aabb\,cC'\Rightarrow aabb\,ccC'\Rightarrow aabbcc. (1)

Leftmost derivation of abccabcc (use S2S_2, i=1i=1, j=k=...j=k=... wait need j=kj=k; here j=1,k=2j=1,k=2? abccabcc: i=1,j=1,k=2i=1,j=1,k=2, so j=kj=k fails; use S1S_1 with i=j=1i=j=1, k=2k=2): SS1ACaAbCabCabcCabccCabcc.S\Rightarrow S_1\Rightarrow AC'\Rightarrow aAbC'\Rightarrow abC'\Rightarrow ab\,cC'\Rightarrow ab\,ccC'\Rightarrow abcc. (1)

(b) (7 marks) SaSbεS\to aSb\mid\varepsilon. Step 1 — Nullable: SS is nullable. Add new start S0SεS_0\to S\mid\varepsilon; remove ε\varepsilon from SS: from SaSbS\to aSb removing nullable SS gives SaSbabS\to aSb\mid ab. So S0SεS_0\to S\mid\varepsilon, SaSbabS\to aSb\mid ab. (2) Step 2 — Unit productions: S0SS_0\to S replaced by SS's bodies: S0aSbabεS_0\to aSb\mid ab\mid\varepsilon. Keep ε\varepsilon only on start (allowed). (2) Step 3 — Terminals to variables: AaA\to a, BbB\to b. Rewrite: S0ASBABεS_0\to A S B\mid A B\mid\varepsilon, SASBABS\to A S B\mid A B. (1) Step 4 — Break long RHS (ASBASB length 3): introduce CSBC\to SB: S0ACABεS_0\to A C\mid AB\mid\varepsilon, SACABS\to AC\mid AB, CSBC\to SB, AaA\to a, BbB\to b. (2) This is CNF (each production: variable→two variables, or variable→terminal, plus S0εS_0\to\varepsilon).

(c) (8 marks) Pumping lemma (CFL): if L2L_2 is context-free, p\exists p s.t. every wL2w\in L_2, wp|w|\ge p, factors as w=uvxyzw=uvxyz with (i) vxyp|vxy|\le p, (ii) vy1|vy|\ge1, (iii) uvixyizL2uv^ixy^iz\in L_2 for all i0i\ge0. (2)

Take w=apbpcpw=a^p b^p c^p, w=3pp|w|=3p\ge p. Since vxyp|vxy|\le p, the window vxyvxy spans at most two of the three symbol-blocks (it cannot touch both the aa-block and cc-block, which are p\ge p apart). (2) Case analysis on pump i=2i=2 (or i=0i=0): (3)

  • vyvy contains only aa's and/or bb's (no cc): pumping changes aa and/or bb counts but cc-count stays pp; equality of all three counts breaks.
  • vyvy contains only bb's and/or cc's (no aa): aa-count stays pp while others change — breaks.
  • vv or yy straddles a boundary and contains two distinct letters: uv2xy2zuv^2xy^2z has letters out of order (ba\ldots b a\ldots or cb\ldots c b\ldots), leaving abca^*b^*c^* — not in L2L_2. In every case uv2xy2zL2uv^2xy^2z\notin L_2, contradicting (iii). (1) Hence L2L_2 is not context-free. \blacksquare

Question 3

(a) (7 marks) HALTTM={M,w:M halts on w}HALT_{TM}=\{\langle M,w\rangle : M\text{ halts on }w\}. (1) Proof: suppose decider HH decides HALTHALT: H(M,w)H(\langle M,w\rangle) accepts iff MM halts on ww, rejects otherwise (always halts). (1) Build DD on input M\langle M\rangle: run H(M,M)H(\langle M,\langle M\rangle\rangle); if HH says "MM halts on M\langle M\rangle", then DD loops forever; if HH says "does not halt", then DD halts. (2) Now run DD on D\langle D\rangle: (2)

  • If DD halts on D\langle D\rangle, then HH reported "halts", so by construction DD loops — contradiction.
  • If DD loops on D\langle D\rangle, then HH reported "does not halt", so DD halts — contradiction. Both cases contradictory ⇒ no such HH exists ⇒ HALTTMHALT_{TM} undecidable. \blacksquare (1)

(b) (5 marks) Rice's theorem: Let PP be any property of the languages recognized by TMs (a semantic property: depends only on L(M)L(M), not on MM's encoding) that is non-trivial (some TM's language has PP, some doesn't). Then {M:L(M)P}\{\langle M\rangle : L(M)\in P\} is undecidable. (2) Application: "L(M)=L(M)=\varnothing" is a semantic property (depends only on L(M)L(M)) and non-trivial (some machines recognize \varnothing, others recognize Σ\Sigma^*\ne\varnothing). Hence by Rice's theorem LemptyL_{\text{empty}} is undecidable. (3) (Note: Lempty=ETML_{\text{empty}}=E_{TM}; Rice applies since the property is semantic & non-trivial — full credit for correctly invoking it. It is in fact co-recognizable but not decidable.)

(c) (4 marks) AmBA\le_m B: there is a total computable f:ΣΣf:\Sigma^*\to\Sigma^* with xA    f(x)Bx\in A \iff f(x)\in B for all xx. (1.5) Proof: if BB decidable by DBD_B and ff computable, then decide AA by: on xx, compute f(x)f(x), run DB(f(x))D_B(f(x)), and accept iff it accepts. This halts (both stages halt) and is correct by the reduction property. So AA decidable. (1.5) Contrapositive used for undecidability: if AmBA\le_m B and AA is undecidable, then BB is undecidable. (1)

(d) (4 marks) NP-complete: LL is NP-complete if (1) LNPL\in NP and (2) every LNPL'\in NP satisfies LpLL'\le_p L (polynomial-time many-one reduction). (1) 3-SATpCLIQUE3\text{-SAT}\le_p\text{CLIQUE}: Given a 3-CNF ϕ\phi with mm clauses, build graph GG: for each clause create 3 vertices, one per literal. Put an edge between two vertices iff they are in different clauses and their literals are not complementary (not xx and ¬x\lnot x). Set k=mk=m. (2) Correctness: ϕ\phi satisfiable     \iff GG has a kk-clique. A satisfying assignment picks one true literal per clause; these mm literals are pairwise non-contradictory and in distinct clauses ⇒ pairwise adjacent ⇒ mm-clique. Conversely a clique of size mm must use exactly one vertex from each clause (edges only cross clauses) with mutually consistent literals, yielding a satisfying assignment. Construction is polynomial. (1)

[
  {"claim":"Q1 NFA subset construction: transitions from B={q0,q1} on b give C={q0,q2}",
   "code":"B={'q0','q1'}\ndelta={('q0','a'):{'q0','q1'},('q0','b'):{'q0'},('q1','b'):{'q2'}}\nres=set()\nfor s in B:\n    res|=delta.get((s,'b'),set())\nresult = res=={'q0','q2'}"},
  {"claim":"Q1(d) pumping: p=3 sample, pumping y=a^k in first block breaks a-count equality",
   "code":"p=3; k=1\nfirst=p+k; middle_b=p; last=p\nresult = (first!=p) and (first!=last)"},
  {"claim":"Q2 CNF: number of variables after conversion of S->aSb|eps is 5 (S0,S,C,A,B)",
   "code":"vars={'S0','S','C','A','B'}\nresult = len(vars)==5"},
  {"claim