Level 3 — ProductionTheory of Computation

Theory of Computation

45 minutes60 marksprintable — key stays hidden on paper

Level 3 — Production (from-scratch derivations, construct-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60

Answer all questions. Show every construction step. Diagrams may be described as transition tables.


Q1. (10 marks) — Subset construction (from scratch) Consider the NFA NN over Σ={a,b}\Sigma=\{a,b\} with states {q0,q1,q2}\{q_0,q_1,q_2\}, start q0q_0, accept {q2}\{q_2\}, and transitions:

  • δ(q0,a)={q0,q1}\delta(q_0,a)=\{q_0,q_1\}, δ(q0,b)={q0}\delta(q_0,b)=\{q_0\}
  • δ(q1,b)={q2}\delta(q_1,b)=\{q_2\}
  • q2q_2 has no outgoing transitions.

(a) Describe in one sentence the language L(N)L(N). (2) (b) Carry out the subset construction to produce an equivalent DFA. Give the full transition table over the reachable subsets and mark start/accept states. (6) (c) State the number of reachable DFA states you obtained. (2)


Q2. (10 marks) — Pumping lemma (full proof from memory) Prove that L={anbnn0}L=\{a^n b^n \mid n\ge 0\} is not regular. (a) State the pumping lemma for regular languages precisely (quantifier structure). (3) (b) Give the complete adversary-style proof, choosing an explicit string in terms of the pumping length pp. (7)


Q3. (12 marks) — CFG and CNF conversion (from scratch) Let grammar GG have start symbol SS and productions: SaSbSSεS \to aSb \mid SS \mid \varepsilon (a) Give a leftmost derivation of the string abababab and draw (as a bracketed structure) its parse tree. (3) (b) Convert GG to Chomsky Normal Form. Show each stage: remove ε\varepsilon-productions, remove unit productions, then convert to CNF. (9)


Q4. (10 marks) — PDA construction (build from memory) Construct a PDA (by empty stack) that accepts L={anbnn1}L=\{a^n b^n \mid n\ge 1\}. (a) Give the formal 7-tuple with full transition function. (7) (b) Trace the accepting computation (sequence of configurations) on input aabbaabb. (3)


Q5. (10 marks) — Undecidability (diagonalization, explain-out-loud) (a) Define the language ATM={M,wM accepts w}A_{TM}=\{\langle M,w\rangle \mid M \text{ accepts } w\} and state precisely what it means for a language to be decidable vs recognizable. (3) (b) Prove that the Halting Problem is undecidable using diagonalization. Structure your answer: assume a decider HH, build DD, and derive the contradiction D(D)D(\langle D\rangle). (7)


Q6. (8 marks) — NP-completeness (reduction + explanation) (a) State what it means for a language to be NP-complete (two conditions). (2) (b) Give a polynomial-time many-one reduction from 3-SAT to CLIQUE, describing the graph construction and why a satisfying assignment corresponds to a clique of the target size. (6)

Answer keyMark scheme & solutions

Q1 (10)

(a) (2) L(N)L(N) = all strings over {a,b}\{a,b\} containing the substring abab; equivalently strings whose penultimate-path requires an aa then bb reaching q2q_2. Since q2q_2 is a dead accept sink reached only via q0aq1bq2q_0\xrightarrow{a}q_1\xrightarrow{b}q_2, and q0q_0 loops on both letters, L(N)=ΣabL(N)=\Sigma^* ab — strings ending in abab? Check: after reaching q2q_2 there are no moves, so any further symbol kills the run. Accept exactly when input ends with abab. (Full 2 marks for "strings ending in abab"; 1 mark for "containing abab".)

(b) (6) No ε\varepsilon-transitions, so subsets are plain. Start ={q0}=\{q_0\}.

State aa bb
{q0}\{q_0\} (start) {q0,q1}\{q_0,q_1\} {q0}\{q_0\}
{q0,q1}\{q_0,q_1\} {q0,q1}\{q_0,q_1\} {q0,q2}\{q_0,q_2\}
{q0,q2}\{q_0,q_2\} (accept) {q0,q1}\{q_0,q_1\} {q0}\{q_0\}

Accept states = those containing q2q_2: {q0,q2}\{q_0,q_2\}. (2 marks per row / correct transitions; deduct for missing accept marking.)

(c) (2) 3 reachable DFA states.


Q2 (10)

(a) (3) Pumping lemma: If LL is regular then p1\exists p\ge 1 such that wL\forall w\in L with wp|w|\ge p, \exists a split w=xyzw=xyz with (i) xyp|xy|\le p, (ii) y1|y|\ge 1, (iii) i0, xyizL\forall i\ge 0,\ xy^iz\in L. (1 mark per quantifier condition set: existence of pp; the three conditions; universal over ii.)

(b) (7)

  • Assume LL regular; let pp be the pumping length. (1)
  • Choose w=apbpLw=a^p b^p\in L, w=2pp|w|=2p\ge p. (2)
  • By the lemma w=xyzw=xyz with xyp|xy|\le p and y1|y|\ge1. Since xyp|xy|\le p, both x,yx,y lie in the first pp symbols, all aa's; so y=aky=a^k with k1k\ge1. (2)
  • Pump i=2i=2: xy2z=ap+kbpxy^2z=a^{p+k}b^p. Since k1k\ge1, p+kpp+k\ne p, so this string L\notin L. (2)
  • Contradiction with (iii); hence LL is not regular. \blacksquare

Q3 (12)

(a) (3) Leftmost derivation of abababab: SSSaSbSabSabaSbababS\Rightarrow SS \Rightarrow aSbS \Rightarrow abS \Rightarrow abaSb \Rightarrow abab (using SSSS\to SS, then first SaSbS\to aSb, inner SεS\to\varepsilon, second SaSbS\to aSb, inner SεS\to\varepsilon). Parse tree (bracketed): S[ S[a S[ε] b] S[a S[ε] b] ]. (1 for valid derivation, 1 for leftmost order, 1 for tree.)

(b) (9) Step 1 — remove ε\varepsilon: SS is nullable. Removing ε\varepsilon from RHS:

  • SaSbS\to aSb gives aSbaSb and abab.
  • SSSS\to SS gives SSSS, SS (either factor nullable). Nullable-removed set (add new start S0SεS_0\to S\mid\varepsilon to keep ε\varepsilon since εL\varepsilon\in L): S0SεS_0\to S\mid\varepsilon; SaSbabSSSS\to aSb\mid ab\mid SS\mid S. (3)

Step 2 — remove unit productions: Units: S0SS_0\to S, SSS\to S. Drop SSS\to S. Replace S0SS_0\to S by SS's rules: S0aSbabSSεS_0\to aSb\mid ab\mid SS\mid \varepsilon; SaSbabSSS\to aSb\mid ab\mid SS. (3)

Step 3 — CNF: Introduce terminals AaA\to a, BbB\to b. Then abABab\to AB, and aSbaSb: introduce CSBC\to SB, so aSbACaSb\to AC. Final CNF: S0ACABSSε,SACABSSS_0\to AC\mid AB\mid SS\mid \varepsilon,\quad S\to AC\mid AB\mid SS CSB,Aa,BbC\to SB,\quad A\to a,\quad B\to b (The lone ε\varepsilon on the start symbol is permitted in CNF because εL\varepsilon\in L and S0S_0 never appears on a RHS.) (3)


Q4 (10)

(a) (7) PDA P=(Q,Σ,Γ,δ,q0,Z0,)P=(Q,\Sigma,\Gamma,\delta,q_0,Z_0,\varnothing) accepting by empty stack:

  • Q={q0,q1}Q=\{q_0,q_1\}, Σ={a,b}\Sigma=\{a,b\}, Γ={A,Z0}\Gamma=\{A,Z_0\}, start state q0q_0, start stack Z0Z_0.
  • δ(q0,a,Z0)={(q0,AZ0)}\delta(q_0,a,Z_0)=\{(q_0,AZ_0)\} — push first aa.
  • δ(q0,a,A)={(q0,AA)}\delta(q_0,a,A)=\{(q_0,AA)\} — push each further aa.
  • δ(q0,b,A)={(q1,ε)}\delta(q_0,b,A)=\{(q_1,\varepsilon)\} — first bb pops an AA, switch to matching mode.
  • δ(q1,b,A)={(q1,ε)}\delta(q_1,b,A)=\{(q_1,\varepsilon)\} — each subsequent bb pops an AA.
  • δ(q1,ε,Z0)={(q1,ε)}\delta(q_1,\varepsilon,Z_0)=\{(q_1,\varepsilon)\} — pop the bottom marker to empty stack (accept). (1 per correct transition, 2 for correct tuple/setup.)

(b) (3) Configurations on aabbaabb (state, remaining input, stack): (q0,aabb,Z0)(q0,abb,AZ0)(q0,bb,AAZ0)(q1,b,AZ0)(q1,ε,Z0)(q1,ε,ε)(q_0,aabb,Z_0)\vdash(q_0,abb,AZ_0)\vdash(q_0,bb,AAZ_0)\vdash(q_1,b,AZ_0)\vdash(q_1,\varepsilon,Z_0)\vdash(q_1,\varepsilon,\varepsilon). Stack empty, input consumed ⇒ accept.


Q5 (10)

(a) (3) ATM={M,w:M is a TM that accepts w}A_{TM}=\{\langle M,w\rangle : M \text{ is a TM that accepts } w\}.

  • Decidable (recursive): some TM halts on every input and decides membership (accepts strings in the language, rejects those not).
  • Recognizable (r.e.): some TM accepts exactly the strings in the language, but may loop on non-members. (1 for definition, 1 each for decidable/recognizable distinction.)

(b) (7)

  • Suppose HALTTMHALT_{TM} (or ATMA_{TM}) is decidable by a decider HH: H(M,w)H(\langle M,w\rangle) accepts iff MM accepts ww, and always halts. (2)
  • Construct DD: on input M\langle M\rangle, run H(M,M)H(\langle M,\langle M\rangle\rangle); if HH accepts (i.e. MM accepts M\langle M\rangle) then DD rejects; if HH rejects then DD accepts. (2)
  • DD is a TM, so consider D(D)D(\langle D\rangle): (1)
    • If DD accepts D\langle D\rangle, then by construction HH said DD does not accept D\langle D\rangle — contradiction.
    • If DD rejects D\langle D\rangle, then HH said DD does accept D\langle D\rangle — contradiction. (1)
  • Either way contradiction ⇒ no such HH exists ⇒ the problem is undecidable. \blacksquare (1)

Q6 (8)

(a) (2) LL is NP-complete iff (i) LL\in NP, and (ii) LL is NP-hard: every LL'\in NP reduces to LL in polynomial time (p\le_p). (1 each.)

(b) (6) Reduction 3-SAT p\le_p CLIQUE. Given a 3-CNF formula ϕ\phi with mm clauses C1,,CmC_1,\dots,C_m, each Ci=(i1i2i3)C_i=(\ell_{i1}\lor\ell_{i2}\lor\ell_{i3}):

  • Vertices: one node per literal occurrence: {(i,j)}\{(i,j)\}, total 3m3m. (1)
  • Edges: connect (i,j)(i,j)(i,j)(i',j') iff iii\ne i' (different clauses) and the literals are not contradictory (ij¬ij\ell_{ij}\ne\neg\ell_{i'j'}). (2)
  • Target: k=mk=m. Construction is polynomial. (1)
  • Correctness: A satisfying assignment picks at least one true literal per clause; those mm chosen literals are in distinct clauses and mutually consistent (no literal and its negation both true), so they are pairwise adjacent ⇒ an mm-clique. Conversely an mm-clique must use one vertex from each of the mm clauses (edges never join same-clause nodes) and contains no contradictory pair, so setting those literals true is a consistent satisfying assignment. (2)

[
  {"claim":"Q1 subset construction yields exactly 3 reachable DFA states",
   "code":"start=frozenset({'q0'}); delta={('q0','a'):{'q0','q1'},('q0','b'):{'q0'},('q1','b'):{'q2'}}\ndef move(S,c):\n    r=set()\n    for s in S: r|=delta.get((s,c),set())\n    return frozenset(r)\nseen={start}; stack=[start]\nwhile stack:\n    S=stack.pop()\n    for c in 'ab':\n        T=move(S,c)\n        if T not in seen: seen.add(T); stack.append(T)\nresult=(len(seen)==3)"},
  {"claim":"Q2 pumping: for w=a^p b^p with y=a^k (k>=1), pumping i=2 leaves unequal counts",
   "code":"p=5; k=2; a_count=p+k; b_count=p; result=(a_count!=b_count)"},
  {"claim":"Q4 PDA trace on aabb ends with empty stack and empty input",
   "code":"config=('q0','aabb',['Z0'])\nsteps=[('q0','abb',['A','Z0']),('q0','bb',['A','A','Z0']),('q1','b',['A','Z0']),('q1','',['Z0']),('q1','',[])]\nfinal=steps[-1]; result=(final[1]=='' and final[2]==[])"},
  {"claim":"Q6 3-SAT to CLIQUE: number of vertices is 3m and clique target is m",
   "code":"m=4; vertices=3*m; target=m; result=(vertices==12 and target==4)"}
]