Level 2 — RecallTheory of Computation

Theory of Computation

30 minutes40 marksprintable — key stays hidden on paper

Level: 2 (Recall — definitions, standard textbook problems, short derivations) Time limit: 30 minutes Total marks: 40


Q1. Define the following formally: (a) an alphabet, (b) a string over an alphabet, (c) a language over an alphabet. State what the empty string and the Kleene star Σ\Sigma^* mean. (4 marks)

Q2. State the formal 5-tuple definition of a Deterministic Finite Automaton (DFA) and explain the meaning of each component. (4 marks)

Q3. Give the DFA (as a 5-tuple or state diagram) that accepts the language over Σ={0,1}\Sigma=\{0,1\} consisting of all strings that end in 0101. (4 marks)

Q4. Consider the NFA with states {q0,q1}\{q_0,q_1\}, start q0q_0, accept {q1}\{q_1\}, over Σ={a}\Sigma=\{a\}, with transitions δ(q0,a)={q0,q1}\delta(q_0,a)=\{q_0,q_1\} and δ(q1,a)=\delta(q_1,a)=\varnothing. Using subset construction, convert it to an equivalent DFA and state which language it accepts. (5 marks)

Q5. Write a regular expression for each language over Σ={a,b}\Sigma=\{a,b\}: (a) all strings containing at least one aa; (b) all strings of even length; (c) all strings that start with aa and end with bb. (3 marks)

Q6. State the Pumping Lemma for regular languages. Then use it to prove that L={anbn:n0}L=\{a^n b^n : n\ge 0\} is not regular. (5 marks)

Q7. Give a context-free grammar for the language L={anbn:n0}L=\{a^n b^n : n\ge 0\} and show a leftmost derivation of the string aabbaabb. (4 marks)

Q8. (a) State the three conditions a grammar must satisfy to be in Chomsky Normal Form (CNF). (b) State one reason CNF is useful. (3 marks)

Q9. Define the complexity classes P and NP (use the verifier definition for NP). State the P vs NP question. (4 marks)

Q10. (a) State the Halting Problem and whether it is decidable. (b) State Rice's theorem in one sentence. (4 marks)


End of paper.

Answer keyMark scheme & solutions

Q1. (4 marks)

  • (a) An alphabet Σ\Sigma is a finite, non-empty set of symbols. (1)
  • (b) A string (word) over Σ\Sigma is a finite sequence of symbols from Σ\Sigma. (1)
  • (c) A language over Σ\Sigma is any set of strings, i.e. a subset LΣL \subseteq \Sigma^*. (1)
  • Empty string ε\varepsilon = string of length 00; Σ=k0Σk\Sigma^* = \bigcup_{k\ge 0}\Sigma^k is the set of all finite strings over Σ\Sigma (including ε\varepsilon). (1)

Why: these are the foundational objects; a language is a set of strings so machines/grammars can "decide/generate" it.


Q2. (4 marks) A DFA is M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F): (1 for tuple)

  • QQ: finite set of states; (½)
  • Σ\Sigma: input alphabet; (½)
  • δ:Q×ΣQ\delta:Q\times\Sigma\to Q: transition function (total, single next state); (1)
  • q0Qq_0\in Q: start state; (½)
  • FQF\subseteq Q: set of accepting states. (½)

Why: determinism = exactly one transition per (state, symbol) pair; a string is accepted iff δ(q0,w)F\delta^*(q_0,w)\in F.


Q3. (4 marks) States {q0,q1,q2}\{q_0,q_1,q_2\}, start q0q_0, accept {q2}\{q_2\}. (1) Transitions: (2)

  • q0q_0: on 0q10\to q_1, on 1q01\to q_0
  • q1q_1: on 0q10\to q_1, on 1q21\to q_2
  • q2q_2: on 0q10\to q_1, on 1q01\to q_0

q2q_2 = "just saw 0101"; q1q_1 = "just saw 00". (1 for correct accept condition)

Why: the machine tracks the longest suffix so far matching a prefix of "01"; only reaching q2q_2 (ending in 0101) accepts.


Q4. (5 marks) Subsets reachable from {q0}\{q_0\}:

  • {q0}\{q_0\} on a{q0,q1}a\to \{q_0,q_1\} (1)
  • {q0,q1}\{q_0,q_1\} on aδ(q0,a)δ(q1,a)={q0,q1}={q0,q1}a\to \delta(q_0,a)\cup\delta(q_1,a)=\{q_0,q_1\}\cup\varnothing=\{q_0,q_1\} (1)

DFA: states {q0}\{q_0\} (start), {q0,q1}\{q_0,q_1\} (accepting, since contains q1q_1). (2)

  • {q0}a{q0,q1}a{q0,q1}\{q_0\}\xrightarrow{a}\{q_0,q_1\}\xrightarrow{a}\{q_0,q_1\}.

Accepted language: {an:n1}=aa\{a^n : n\ge 1\} = a a^* — all non-empty strings of aa's. (1)

Why: subset construction tracks the set of NFA states; a DFA state is accepting iff it contains an NFA accepting state. ε\varepsilon is rejected because start subset {q0}\{q_0\} has no accept state.


Q5. (3 marks)

  • (a) (a+b)a(a+b)(a+b)^* a (a+b)^* (1)
  • (b) ((a+b)(a+b))((a+b)(a+b))^* (1)
  • (c) a(a+b)ba(a+b)^* b (1)

Why: (a) force at least one aa; (b) length multiple of 2 via pairs; (c) fix first and last symbols.


Q6. (5 marks) Pumping Lemma: If LL is regular, there exists p1p\ge 1 (pumping length) such that every wLw\in L with wp|w|\ge p can be written w=xyzw=xyz with (i) y1|y|\ge 1, (ii) xyp|xy|\le p, (iii) xyizLxy^iz\in L for all i0i\ge 0. (2)

Proof of non-regularity of L={anbn}L=\{a^nb^n\}: Assume regular with pumping length pp. Take w=apbpLw=a^p b^p\in L, w=2pp|w|=2p\ge p. (1) By (ii), xyxy lies within the first pp symbols, all aa's, so y=aky=a^k with k1k\ge 1. (1) Pump i=2i=2: xy2z=ap+kbpxy^2z=a^{p+k}b^p with p+kpp+k\ne p, so it has more aa's than bb's xy2zL\Rightarrow xy^2z\notin L, contradicting (iii). Hence LL is not regular. (1)

Why: the constraint xyp|xy|\le p forces the pumped part into the aa-block, breaking the equal-count condition.


Q7. (4 marks) Grammar: SaSbεS\to aSb \mid \varepsilon. (2) Leftmost derivation of aabbaabb: (2) SaSbaaSbbaaεbb=aabb.S\Rightarrow aSb\Rightarrow aaSbb\Rightarrow aa\varepsilon bb = aabb.

Why: each application adds one matching aba\dots b pair; termination via SεS\to\varepsilon guarantees equal counts.


Q8. (3 marks) (a) Every production is of the form: (2, ~⅔ each)

  • ABCA\to BC (two non-terminals), or
  • AaA\to a (single terminal), and
  • optionally SεS\to\varepsilon (only start symbol may derive ε\varepsilon), with SS not on any RHS.

(b) Useful reason (any one): CNF gives parse trees that are binary, enabling the CYK parsing algorithm (O(n3)O(n^3) membership testing) and simplifying inductive proofs. (1)


Q9. (4 marks)

  • P: the class of languages decidable by a deterministic Turing machine in time O(nk)O(n^k) for some constant kk (polynomial time). (1.5)
  • NP: the class of languages LL for which there is a polynomial-time verifier VV and polynomial pp such that xL    c, cp(x), V(x,c)x\in L \iff \exists c,\ |c|\le p(|x|),\ V(x,c) accepts. (cc = certificate/witness.) (1.5)
  • P vs NP: Is P=NPP = NP? I.e., can every problem whose solutions are efficiently verifiable also be efficiently solved? (1)

Why: NP captures "easy to check", P captures "easy to solve"; equality is an open Millennium problem.


Q10. (4 marks) (a) Halting Problem: given a Turing machine MM and input ww, does MM halt on ww? The language ATMhalt={M,w:M halts on w}A_{TM}^{halt}=\{\langle M,w\rangle : M \text{ halts on } w\} is undecidable (recognizable but not recursive), proved by diagonalization. (2) (b) Rice's theorem: every non-trivial semantic property of the language recognized by a Turing machine is undecidable. (2)

Why: diagonalization derives a contradiction from a supposed decider; Rice generalizes undecidability to all non-trivial properties of r.e. languages.


[
  {"claim":"Subset construction of NFA yields only states {q0} and {q0,q1}; language is a a*",
   "code":"from sympy import symbols\n# simulate NFA on a-strings, check accepted set = {n>=1}\ndef nfa_accepts(n):\n    # states reachable after n a's\n    states={'q0'}\n    for _ in range(n):\n        nxt=set()\n        for s in states:\n            if s=='q0': nxt|={'q0','q1'}\n        states=nxt\n    return 'q1' in states\naccepted=[n for n in range(6) if nfa_accepts(n)]\nresult = accepted==[1,2,3,4,5]"},
  {"claim":"Pumping a^p b^p with y=a^k (k>=1), i=2 breaks equality: count a != count b",
   "code":"p=5; k=2\nw_a=p+k; w_b=p\nresult = (w_a != w_b) and (w_a==p+k) and (w_b==p)"},
  {"claim":"CFG S->aSb|eps derives aabb via two aSb applications then eps",
   "code":"steps=['S','aSb','aaSbb','aabb']\nfinal=steps[-1]\nresult = final=='aabb' and final.count('a')==final.count('b')==2"},
  {"claim":"Regex ((a+b)(a+b))* matches exactly even-length strings",
   "code":"import re\npat=re.compile('^((a|b)(a|b))*$')\nstrings=['','a','ab','aba','abab','b']\nres={s:bool(pat.match(s)) for s in strings}\nexpected={'':True,'a':False,'ab':True,'aba':False,'abab':True,'b':False}\nresult = res==expected"}
]