Theory of Computation
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 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 consisting of all strings that end in . (4 marks)
Q4. Consider the NFA with states , start , accept , over , with transitions and . 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) all strings containing at least one ; (b) all strings of even length; (c) all strings that start with and end with . (3 marks)
Q6. State the Pumping Lemma for regular languages. Then use it to prove that is not regular. (5 marks)
Q7. Give a context-free grammar for the language and show a leftmost derivation of the string . (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 is a finite, non-empty set of symbols. (1)
- (b) A string (word) over is a finite sequence of symbols from . (1)
- (c) A language over is any set of strings, i.e. a subset . (1)
- Empty string = string of length ; is the set of all finite strings over (including ). (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 : (1 for tuple)
- : finite set of states; (½)
- : input alphabet; (½)
- : transition function (total, single next state); (1)
- : start state; (½)
- : set of accepting states. (½)
Why: determinism = exactly one transition per (state, symbol) pair; a string is accepted iff .
Q3. (4 marks) States , start , accept . (1) Transitions: (2)
- : on , on
- : on , on
- : on , on
= "just saw "; = "just saw ". (1 for correct accept condition)
Why: the machine tracks the longest suffix so far matching a prefix of "01"; only reaching (ending in ) accepts.
Q4. (5 marks) Subsets reachable from :
- on (1)
- on (1)
DFA: states (start), (accepting, since contains ). (2)
- .
Accepted language: — all non-empty strings of 's. (1)
Why: subset construction tracks the set of NFA states; a DFA state is accepting iff it contains an NFA accepting state. is rejected because start subset has no accept state.
Q5. (3 marks)
- (a) (1)
- (b) (1)
- (c) (1)
Why: (a) force at least one ; (b) length multiple of 2 via pairs; (c) fix first and last symbols.
Q6. (5 marks) Pumping Lemma: If is regular, there exists (pumping length) such that every with can be written with (i) , (ii) , (iii) for all . (2)
Proof of non-regularity of : Assume regular with pumping length . Take , . (1) By (ii), lies within the first symbols, all 's, so with . (1) Pump : with , so it has more 's than 's , contradicting (iii). Hence is not regular. (1)
Why: the constraint forces the pumped part into the -block, breaking the equal-count condition.
Q7. (4 marks) Grammar: . (2) Leftmost derivation of : (2)
Why: each application adds one matching pair; termination via guarantees equal counts.
Q8. (3 marks) (a) Every production is of the form: (2, ~⅔ each)
- (two non-terminals), or
- (single terminal), and
- optionally (only start symbol may derive ), with not on any RHS.
(b) Useful reason (any one): CNF gives parse trees that are binary, enabling the CYK parsing algorithm ( membership testing) and simplifying inductive proofs. (1)
Q9. (4 marks)
- P: the class of languages decidable by a deterministic Turing machine in time for some constant (polynomial time). (1.5)
- NP: the class of languages for which there is a polynomial-time verifier and polynomial such that accepts. ( = certificate/witness.) (1.5)
- P vs NP: Is ? 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 and input , does halt on ? The language 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"}
]