Browse papers
A

Section A: Long Answer Questions

Attempt all / any as specified.

4 questions
1long12 marks

(a) Define a Non-deterministic Finite Automaton (NFA) formally as a 5-tuple and explain the role of each component. State how an NFA differs from a DFA.

(b) Consider the NFA with ε\varepsilon-transitions over the alphabet Σ={a,b}\Sigma = \{a, b\} that accepts all strings containing the substring aba. Using the subset construction algorithm, convert the given NFA into an equivalent DFA. Show the transition table and draw the resulting DFA, clearly marking the start and final states.

(a) Formal definition of an NFA

A Non-deterministic Finite Automaton (NFA) is a 5-tuple

M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F)

where:

  • QQ — a finite, non-empty set of states.
  • Σ\Sigma — a finite input alphabet (the symbols the machine reads).
  • δ\delta — the transition function δ:Q×(Σ{ε})2Q\delta : Q \times (\Sigma \cup \{\varepsilon\}) \rightarrow 2^{Q}. For a given state and input symbol it returns a set of possible next states (possibly empty), and ε\varepsilon-moves allow changing state without reading input.
  • q0Qq_0 \in Q — the start (initial) state.
  • FQF \subseteq Q — the set of final (accepting) states.

A string ww is accepted if at least one computation path ends in a state of FF.

NFA vs DFA

FeatureDFANFA
Transitionδ:Q×ΣQ\delta: Q\times\Sigma \to Q (exactly one next state)δ:Q×(Σ{ε})2Q\delta: Q\times(\Sigma\cup\{\varepsilon\}) \to 2^{Q} (a set of states)
ε\varepsilon-movesNot allowedAllowed
ChoiceDeterministic (one path)Non-deterministic (many paths)
AcceptanceThe single path ends in FFSome path ends in FF

Both accept exactly the regular languages — they are equivalent in power.

(b) NFA for strings containing aba, then subset construction

NFA NN with states {A,B,C,D}\{A,B,C,D\}, start AA, final DD:

Stateaabb
A\to A{A,B}\{A,B\}{A}\{A\}
BB\emptyset{C}\{C\}
CC{D}\{D\}\emptyset
D*D{D}\{D\}{D}\{D\}

State AA loops on every symbol (still searching); AaBbCaDA\xrightarrow{a}B\xrightarrow{b}C\xrightarrow{a}D recognises aba; DD is a trap-accept state. (No ε\varepsilon-transitions are actually needed, so ε\varepsilon-closure of each state is the state itself.)

Subset construction

Start subset ={A}= \{A\}.

DFA stateSubsetaabb
S0S_0 (start){A}\{A\}{A,B}=S1\{A,B\}=S_1{A}=S0\{A\}=S_0
S1S_1{A,B}\{A,B\}{A,B}=S1\{A,B\}=S_1{A,C}=S2\{A,C\}=S_2
S2S_2{A,C}\{A,C\}{A,B,D}=S3\{A,B,D\}=S_3{A}=S0\{A\}=S_0
S3S_3*{A,B,D}\{A,B,D\}{A,B,D}=S3\{A,B,D\}=S_3{A,C,D}=S4\{A,C,D\}=S_4
S4S_4*{A,C,D}\{A,C,D\}{A,B,D}=S3\{A,B,D\}=S_3{A,D}=S5\{A,D\}=S_5
S5S_5*{A,D}\{A,D\}{A,B,D}=S3\{A,B,D\}=S_3{A,D}=S5\{A,D\}=S_5

Final states are all subsets containing DD: S3,S4,S5S_3, S_4, S_5.

Resulting DFA (described)

  • Start state: S0={A}S_0=\{A\}.
  • Accepting states (double circle): S3,S4,S5S_3,S_4,S_5.
  • Transitions as in the table. Once any accepting state is reached (substring aba seen) the machine stays among accepting states forever, since every subsequent subset still contains DD.

This DFA accepts exactly the strings over {a,b}\{a,b\} that contain the substring aba.

finite-automatanfa-to-dfadfa-design
2long12 marks

(a) Consider the context-free grammar GG with productions:

SaSbSSεS \rightarrow aSb \mid SS \mid \varepsilon

Show the leftmost derivation and draw the parse tree for the string aabbab. State the language L(G)L(G) generated by this grammar.

(b) Construct a Pushdown Automaton (PDA) that accepts the language L={anbnn1}L = \{ a^n b^n \mid n \geq 1 \} by final state. Describe its transition function and trace the moves of your PDA on the input string aaabbb.

(a) Derivation, parse tree and L(G)L(G) for SaSbSSεS \to aSb \mid SS \mid \varepsilon

Leftmost derivation of aabbab:

SSSaSbSaaSbbSaaεbbSaabbSaabbaSbaabbaεbaabbabS \Rightarrow SS \Rightarrow aSbS \Rightarrow aaSbbS \Rightarrow aa\varepsilon bbS \Rightarrow aabbS \Rightarrow aabb\,aSb \Rightarrow aabb\,a\varepsilon b \Rightarrow aabbab

So aabbab =(aabb)(ab)= (aabb)(ab), two balanced blocks concatenated.

Parse tree (described):

            S
           / \
          S   S
         /|\   /|\
        a S b a S b
          |     |
          S     S
         /|\    |
        a S b   ε
          |
          ε
  • Root SSSS \to S\,S.
  • Left SaSba(aSb)baabbS \Rightarrow aSb \Rightarrow a(aSb)b \Rightarrow aabb (innermost SεS\to\varepsilon).
  • Right SaSbabS \to aSb \Rightarrow ab (with SεS\to\varepsilon).

Language generated:

L(G)={w{a,b}w is a string of balanced parentheses with a=‘(’ and b=‘)’}L(G) = \{\, w \in \{a,b\}^* \mid w \text{ is a string of balanced parentheses with } a=\text{`(' and } b=\text{`)'}\,\}

i.e. the set of all strings in which every prefix has #a#b\#a \ge \#b and the whole string has #a=#b\#a = \#b — the balanced / Dyck language over {a,b}\{a,b\}.

(b) PDA for L={anbnn1}L = \{a^n b^n \mid n \ge 1\} (acceptance by final state)

M=(Q,Σ,Γ,δ,q0,Z0,F)M = (Q,\Sigma,\Gamma,\delta,q_0,Z_0,F)

with Q={q0,q1,q2}Q=\{q_0,q_1,q_2\}, Σ={a,b}\Sigma=\{a,b\}, Γ={A,Z0}\Gamma=\{A,Z_0\}, start state q0q_0, start stack symbol Z0Z_0, F={q2}F=\{q_2\}.

Transition function:

  1. δ(q0,a,Z0)={(q0,AZ0)}\delta(q_0,a,Z_0) = \{(q_0,AZ_0)\} — push first AA.
  2. δ(q0,a,A)={(q0,AA)}\delta(q_0,a,A) = \{(q_0,AA)\} — push an AA for each aa.
  3. δ(q0,b,A)={(q1,ε)}\delta(q_0,b,A) = \{(q_1,\varepsilon)\} — on first bb, pop one AA, go to q1q_1.
  4. δ(q1,b,A)={(q1,ε)}\delta(q_1,b,A) = \{(q_1,\varepsilon)\} — pop one AA per bb.
  5. δ(q1,ε,Z0)={(q2,Z0)}\delta(q_1,\varepsilon,Z_0) = \{(q_2,Z_0)\} — stack back to Z0Z_0 (equal aa's and bb's): accept.

Trace on aaabbb (notation: (state, remaining input, stack), top of stack on the left):

StepConfigurationRule
0(q0, aaabbb, Z0)(q_0,\ aaabbb,\ Z_0)start
1(q0, aabbb, AZ0)(q_0,\ aabbb,\ AZ_0)(1)
2(q0, abbb, AAZ0)(q_0,\ abbb,\ AAZ_0)(2)
3(q0, bbb, AAAZ0)(q_0,\ bbb,\ AAAZ_0)(2)
4(q1, bb, AAZ0)(q_1,\ bb,\ AAZ_0)(3)
5(q1, b, AZ0)(q_1,\ b,\ AZ_0)(4)
6(q1, ε, Z0)(q_1,\ \varepsilon,\ Z_0)(4)
7(q2, ε, Z0)(q_2,\ \varepsilon,\ Z_0)(5)

Input consumed and machine in final state q2q_2aaabbb is accepted.

context-free-grammarspushdown-automatacfg-to-pda
3long16 marks

(a) Design a Turing Machine that accepts the language L={anbncnn1}L = \{ a^n b^n c^n \mid n \geq 1 \}. Give the formal definition (states, tape symbols, transition function) and show the sequence of configurations (instantaneous descriptions) when the machine processes the input aabbcc.

(b) State the Halting Problem and prove that it is undecidable. Explain the significance of this result in the theory of computation.

(a) Turing Machine for L={anbncnn1}L = \{a^n b^n c^n \mid n \ge 1\}

Idea: repeatedly cross off the leftmost unmarked aa (write XX), then the leftmost bb (write YY), then the leftmost cc (write ZZ); return to the left and repeat until none remain. Accept if everything is matched.

Formal definition M=(Q,Σ,Γ,δ,q0,B,F)M=(Q,\Sigma,\Gamma,\delta,q_0,B,F):

  • Q={q0,q1,q2,q3,q4,qacc,qrej}Q=\{q_0,q_1,q_2,q_3,q_4,q_{acc},q_{rej}\}
  • Σ={a,b,c}\Sigma=\{a,b,c\}
  • Γ={a,b,c,X,Y,Z,B}\Gamma=\{a,b,c,X,Y,Z,B\} (BB = blank)
  • start q0q_0, F={qacc}F=\{q_{acc}\}

Transition function δ\delta (Right = R, Left = L):

q0 a -> q1, X, R     (mark an a, go find a b)
q0 Y -> q4, Y, R     (no a's left; verify only Y,Z remain)
q1 a -> q1, a, R     (skip a's)
q1 Y -> q1, Y, R     (skip already-marked Y)
q1 b -> q2, Y, R     (mark a b, go find a c)
q2 b -> q2, b, R     (skip b's)
q2 Z -> q2, Z, R     (skip marked Z)
q2 c -> q3, Z, L     (mark a c, turn around)
q3 (a|b|Y|Z) -> q3, same, L   (walk left)
q3 X -> q0, X, R     (back at left boundary, repeat)
q4 Y -> q4, Y, R     (scan remaining Y's)
q4 Z -> q4, Z, R     (scan remaining Z's)
q4 B -> qacc, B, R   (all matched -> accept)

Any missing transition (e.g. seeing aa when expecting only Y,ZY,Z, or a cc before a bb) goes to qrejq_{rej}, ensuring strings like a2b2c3a^2b^2c^3 or abccabcc are rejected.

Sequence of configurations (IDs) for input aabbcc (current state shown just left of the scanned symbol):

q0 a a b b c c
X q1 a b b c c
X a q1 b b c c
X a Y q2 b c c
X a Y b q2 c c
X a Y q3 b Z c       (mark c->Z, move L)
X a q3 Y b Z c
X q3 a Y b Z c
q3 X a Y b Z c
X q0 a Y b Z c
X X q1 Y b Z c       (mark a->X)
X X Y q1 b Z c
X X Y Y q2 Z c       (mark b->Y)
X X Y Y Z q2 c
X X Y Y q3 Z Z       (mark c->Z, move L)
X X Y q3 Y Z Z
X X q3 Y Y Z Z
X q3 X Y Y Z Z
X X q0 Y Y Z Z       (no a's left)
X X Y q4 Y Z Z
X X Y Y q4 Z Z
X X Y Y Z q4 Z
X X Y Y Z Z q4 B
X X Y Y Z Z B qacc   -> ACCEPT

(b) The Halting Problem and its undecidability

Statement. Given the encoding M\langle M\rangle of an arbitrary Turing machine MM and an input ww, decide whether MM halts on ww. Formally, decide membership in

HALT={M,wM halts on input w}.HALT = \{\,\langle M,w\rangle \mid M \text{ halts on input } w\,\}.

The Halting Problem asks for an algorithm (a TM HH that always halts) deciding HALTHALT.

Proof of undecidability (diagonalization / contradiction).

Assume, for contradiction, that a TM HH decides HALTHALT:

H(M,w)={acceptif M halts on wrejectif M loops on wH(\langle M\rangle, w) = \begin{cases} \text{accept} & \text{if } M \text{ halts on } w \\ \text{reject} & \text{if } M \text{ loops on } w \end{cases}

Construct a TM DD that takes a single input M\langle M\rangle and runs H(M,M)H(\langle M\rangle,\langle M\rangle):

  • If HH says "MM halts on M\langle M\rangle", then DD loops forever.
  • If HH says "MM loops on M\langle M\rangle", then DD halts.

Now run DD on its own description D\langle D\rangle:

  • If DD halts on D\langle D\rangle, then by construction HH said it loops, so DD must loop — contradiction.
  • If DD loops on D\langle D\rangle, then HH said it halts, so DD must halt — contradiction.

Either way we reach a contradiction, so the assumed decider HH cannot exist. Hence the Halting Problem is undecidable. \blacksquare

Significance.

  • It is the first concrete, natural problem proven algorithmically unsolvable, establishing a hard limit on what computers can do.
  • It shows the class of recursively enumerable languages strictly contains the recursive (decidable) languages (HALTHALT is r.e. but not recursive).
  • Via reduction, undecidability of HALTHALT propagates to many practical problems (program verification, equivalence of programs, Rice's theorem properties), proving no general algorithm can decide them.
turing-machinesdecidabilityhalting-problem
4long10 marks

Explain the Chomsky hierarchy of grammars. For each of the four types (Type 0, Type 1, Type 2, Type 3), describe the form of the production rules, the class of language generated, and the corresponding accepting automaton. Illustrate each grammar type with one suitable example.

The Chomsky Hierarchy of Grammars

Proposed by Noam Chomsky (1956), it classifies grammars (and the languages/automata they correspond to) into four nested types by restricting the form of productions αβ\alpha \to \beta. Each class strictly contains the next:

Type 3Type 2Type 1Type 0.\text{Type 3} \subset \text{Type 2} \subset \text{Type 1} \subset \text{Type 0}.

Type 0 — Unrestricted (Recursively Enumerable)

  • Productions: αβ\alpha \to \beta with α,β(VT)\alpha,\beta \in (V\cup T)^* and α\alpha containing at least one variable. No restriction.
  • Language class: Recursively enumerable languages.
  • Automaton: Turing Machine.
  • Example: Any TM-recognisable language, e.g. {anbncndnn1}\{a^n b^n c^n d^n \mid n\ge 1\} via context-sensitive-style rules with deletion; e.g. SaSBC, , CBBC, aBab,S \to aSBC,\ \dots,\ CB \to BC,\ aB\to ab,\dots (with extra erasing rules).

Type 1 — Context-Sensitive

  • Productions: αβ\alpha \to \beta with αβ|\alpha| \le |\beta| (non-contracting); equivalently αAβαγβ\alpha A \beta \to \alpha\gamma\beta with γε\gamma\ne\varepsilon. (SεS\to\varepsilon allowed if SS unused on RHS.)
  • Language class: Context-sensitive languages.
  • Automaton: Linear Bounded Automaton (LBA) — a TM whose tape is bounded by the input length.
  • Example: L={anbncnn1}L=\{a^n b^n c^n \mid n\ge 1\}, generated by a context-sensitive grammar such as SabcaSBc, cBBc, bBbbS\to abc \mid aSBc,\ cB\to Bc,\ bB\to bb.

Type 2 — Context-Free

  • Productions: AγA \to \gamma where AVA\in V (single variable on the left) and γ(VT)\gamma\in(V\cup T)^*.
  • Language class: Context-free languages.
  • Automaton: Pushdown Automaton (PDA).
  • Example: {anbnn1}\{a^n b^n \mid n\ge 1\}, generated by SaSbabS \to aSb \mid ab.

Type 3 — Regular

  • Productions: right-linear AaBA \to aB or AaA\to a (or all left-linear), aT, A,BVa\in T,\ A,B\in V.
  • Language class: Regular languages.
  • Automaton: Finite Automaton (DFA/NFA).
  • Example: aba^*b (strings of aa's ending in one bb), generated by SaSbS \to aS \mid b.

Summary table

TypeGrammarProduction formLanguageAutomaton
0Unrestrictedαβ\alpha\to\betaRecursively enumerableTuring Machine
1Context-sensitive$\alpha\le
2Context-freeAγA\to\gammaContext-freePushdown Automaton
3RegularAaB, AaA\to aB,\ A\to aRegularFinite Automaton
chomsky-hierarchygrammar-typesautomata-equivalence
B

Section B: Short Answer Questions

Attempt all / any as specified.

7 questions
5short6 marks

(a) Write a regular expression for the language over Σ={0,1}\Sigma = \{0, 1\} consisting of all strings that have an even number of 0's and end with 1.

(b) State Arden's theorem and use it to find the regular expression accepted by a finite automaton with states q1,q2q_1, q_2 where q1q_1 is the start state, q1aq1q_1 \xrightarrow{a} q_1, q1bq2q_1 \xrightarrow{b} q_2, and q2q_2 is the final state with q2a,bq2q_2 \xrightarrow{a,b} q_2.

(a) Even number of 0's and ending in 1

A block containing an even number of 0's (any number of 1's interspersed) is described by (10101)(1^*0\,1^*0\,1^*)^*, i.e. 0's come in pairs. The string must then end in 1, so append at least one 1:

(1010)11\boxed{\,(\,1^*0\,1^*0\,)^* \, 1^* 1\,}

Equivalently 1(0101)11^*(0\,1^*0\,1^*)^*1. This guarantees an even count of 0's and a final symbol 1.

(b) Arden's Theorem

Statement. For regular expressions PP and QQ where PP does not contain (does not generate) the empty string ε\varepsilon, the equation

R=Q+RPR = Q + RP

has the unique solution

R=QP.R = Q P^*.

Applying it to the given automaton

States: start q1q_1, final q2q_2. Transitions: q1aq1q_1\xrightarrow{a}q_1, q1bq2q_1\xrightarrow{b}q_2, q2aq2q_2\xrightarrow{a}q_2, q2bq2q_2\xrightarrow{b}q_2.

Write equations for strings reaching each state (each variable = set of strings ending in that state):

q1=ε+q1aq2=q1b+q2(a+b)q_1 = \varepsilon + q_1 a \qquad q_2 = q_1 b + q_2(a+b)

Solve q1q_1 with Arden (Q=ε, P=aQ=\varepsilon,\ P=a):

q1=εa=a.q_1 = \varepsilon\, a^* = a^*.

Solve q2q_2 (Q=q1b=ab, P=a+bQ=q_1 b = a^*b,\ P=a+b):

q2=(ab)(a+b).q_2 = (a^* b)(a+b)^*.

Since q2q_2 is the only final state, the regular expression accepted by the automaton is

ab(a+b)\boxed{\,a^* b\,(a+b)^*\,}

i.e. all strings consisting of zero or more aa's, then a bb, then any string over {a,b}\{a,b\}.

regular-expressionsregular-languages
6short6 marks

State the pumping lemma for regular languages. Using the pumping lemma, prove that the language L={anbnn0}L = \{ a^n b^n \mid n \geq 0 \} is not regular.

Pumping Lemma for Regular Languages

If LL is regular, then there exists a constant p1p\ge 1 (the pumping length) such that every string wLw\in L with wp|w|\ge p can be written as

w=xyzw = xyz

satisfying:

  1. y1|y| \ge 1 (the middle part is non-empty),
  2. xyp|xy| \le p (the pumped part lies within the first pp symbols),
  3. xyizLx y^i z \in L for all i0i \ge 0 (pumping keeps the string in LL).

Proof that L={anbnn0}L = \{a^n b^n \mid n \ge 0\} is not regular

Assume LL is regular, and let pp be the pumping length.

Choose the string

w=apbpL,w=2pp.w = a^{p} b^{p} \in L, \qquad |w| = 2p \ge p.

By the lemma, w=xyzw = xyz with xyp|xy|\le p and y1|y|\ge 1. Since the first pp symbols of ww are all aa's, both xx and yy lie entirely within the aa-block. So

x=aj,y=ak (k1),z=apjkbp.x = a^{j},\quad y = a^{k}\ (k\ge 1),\quad z = a^{\,p-j-k} b^{p}.

Pump with i=2i = 2:

xy2z=aja2kapjkbp=ap+kbp.x y^{2} z = a^{j} a^{2k} a^{\,p-j-k} b^{p} = a^{\,p+k} b^{p}.

Since k1k\ge 1, this string has more aa's than bb's, so ap+kbpLa^{p+k}b^{p} \notin L. This contradicts condition 3 of the pumping lemma.

Therefore our assumption is false, and L={anbnn0}L = \{a^n b^n \mid n\ge 0\} is not regular. \blacksquare

pumping-lemmaregular-languages
7short6 marks

(a) Define an ambiguous grammar. Show that the grammar EE+EEE(E)idE \rightarrow E + E \mid E * E \mid (E) \mid id is ambiguous by giving two distinct parse trees for the string id + id * id.

(b) Briefly explain how the ambiguity in this grammar can be removed.

(a) Ambiguous grammar

A context-free grammar GG is ambiguous if there exists at least one string wL(G)w \in L(G) that has two or more distinct parse (derivation) trees — equivalently, two distinct leftmost (or two distinct rightmost) derivations.

Showing EE+EEE(E)idE \to E + E \mid E * E \mid (E) \mid id is ambiguous using the string id + id * id:

Parse tree 1 (addition at the root — interprets as id + (id * id)):

        E
      / | \
     E  +  E
     |    / | \
    id   E  *  E
         |     |
        id    id

Parse tree 2 (multiplication at the root — interprets as (id + id) * id):

          E
        / | \
       E  *  E
      /|\    |
     E + E  id
     |   |
    id  id

Both trees derive the same string id + id * id but have different structure (and different evaluation: id+(idid)id+(id\cdot id) vs (id+id)id(id+id)\cdot id). Two distinct parse trees exist, hence the grammar is ambiguous.

(b) Removing the ambiguity

The ambiguity is removed by encoding operator precedence and associativity into the grammar using a hierarchy of non-terminals (one level per precedence) and making them left-recursive for left associativity:

E -> E + T | T        (+ has lowest precedence, left-associative)
T -> T * F | F        (* binds tighter than +)
F -> ( E ) | id       (highest precedence: parentheses / atoms)

Now id + id * id has a unique parse tree: FF/* (the TT level) must be built before ++ (the EE level), forcing the multiplication to be grouped first (id+(idid)id + (id*id)). Since every string now has exactly one parse tree, the grammar is unambiguous.

context-free-grammarsambiguitygrammar-simplification
8short6 marks

Convert the following context-free grammar into Chomsky Normal Form (CNF), showing each step of the conversion:

SASAaBS \rightarrow ASA \mid aB ABSA \rightarrow B \mid S BbεB \rightarrow b \mid \varepsilon

Convert to Chomsky Normal Form

Given grammar:

SASAaB,ABS,BbεS \to ASA \mid aB, \quad A \to B \mid S, \quad B \to b \mid \varepsilon

CNF requires every production to be of the form ABCA \to BC (two variables) or AaA \to a (single terminal); only the start symbol may derive ε\varepsilon, and it must not appear on any right-hand side.

Step 1 — Add a new start symbol

S0S,SASAaB,ABS,BbεS_0 \to S, \quad S \to ASA \mid aB, \quad A \to B \mid S, \quad B \to b \mid \varepsilon

Step 2 — Remove ε\varepsilon-productions

Nullable variables: BB (directly), and AA (via ABA\to B). Remove BεB\to\varepsilon and add versions of rules with nullable symbols omitted:

  • SASAS\to ASA: omitting nullable AA's gives SSA, AS, SS\to SA,\ AS,\ S.
  • SaBS\to aB: omitting BB gives SaS\to a.
  • ABA\to B: BB nullable but dropping it gives empty, not added (would be AεA\to\varepsilon, discard).

Result:

S0S,SASASAASSaBa,ABS,BbS_0\to S,\quad S\to ASA\mid SA\mid AS\mid S\mid aB\mid a,\quad A\to B\mid S,\quad B\to b

Step 3 — Remove unit productions

Unit pairs and substitution:

  • SSS\to S: delete (trivial).
  • S0SS_0\to SS0S_0 inherits SS's rules: S0ASASAASaBaS_0\to ASA\mid SA\mid AS\mid aB\mid a.
  • ABA\to BAbA\to b; ASA\to SAA inherits SS's rules: AASASAASaBaA\to ASA\mid SA\mid AS\mid aB\mid a.

Now:

S0ASASAASaBaSASASAASaBaAASASAASaBabBb\begin{aligned} S_0 &\to ASA \mid SA \mid AS \mid aB \mid a\\ S &\to ASA \mid SA \mid AS \mid aB \mid a\\ A &\to ASA \mid SA \mid AS \mid aB \mid a \mid b\\ B &\to b \end{aligned}

Step 4 — Replace terminals in long rules and break up bodies of length > 2

Introduce TaaT_a \to a and use it inside mixed/long bodies. Replace aBaB with TaBT_a B. Break the ternary ASAASA by introducing CSAC \to SA, so ASAACASA \to A\,C.

Let CSAC \to SA.

Final CNF grammar:

S0ACSAASTaBaSACSAASTaBaAACSAASTaBabBbCSATaa\begin{aligned} S_0 &\to AC \mid SA \mid AS \mid T_aB \mid a\\ S &\to AC \mid SA \mid AS \mid T_aB \mid a\\ A &\to AC \mid SA \mid AS \mid T_aB \mid a \mid b\\ B &\to b\\ C &\to SA\\ T_a &\to a \end{aligned}

Every production is now XYZX\to YZ or XaX\to a, so the grammar is in Chomsky Normal Form.

context-free-grammarschomsky-normal-form
9short5 marks

Differentiate between a Mealy machine and a Moore machine with respect to output behaviour. Design a Moore machine over Σ={0,1}\Sigma = \{0, 1\} that outputs the residue (remainder) of the input binary number when divided by 3.

Mealy vs Moore machines (output behaviour)

AspectMoore machineMealy machine
Output depends onthe state onlythe state and current input
Output writtenλ:QΔ\lambda: Q \to \Delta (one symbol per state)λ:Q×ΣΔ\lambda: Q\times\Sigma \to \Delta (one symbol per transition)
Output for input of length nnn+1n+1 symbols (includes the start state's output)nn symbols
Reaction to input changedelayed by one clock (state must change first)immediate (same clock)
Typical sizeusually more statesusually fewer states

Moore machine: residue of a binary number mod 3

Reading the binary input most-significant bit first, the value-so-far vv updates as v2v+bitv \to 2v + \text{bit}, so the remainder mod 3 updates as r(2r+bit)mod3r \to (2r+\text{bit}) \bmod 3. Use three states, one per residue, and output the residue of the state.

States / outputs: q0q_0 (residue 0, output 0), q1q_1 (residue 1, output 1), q2q_2 (residue 2, output 2). Start state q0q_0 (empty input ⇒ residue 0).

Transition table (next state =(2r+b)mod3=(2r+b)\bmod 3):

State (residue)Outputinput 0input 1
q0q_0 (0)0q0q_0 (00)q1q_1 (11)
q1q_1 (1)1q2q_2 (22)q0q_0 (00)
q2q_2 (2)2q1q_1 (11)q2q_2 (22)

Reasoning for q1q_1 on input 0: r=121+0=22q2r=1\Rightarrow 2\cdot1+0=2\equiv2\Rightarrow q_2. On input 1: 21+1=30q02\cdot1+1=3\equiv0\Rightarrow q_0. Similarly for the others.

Example: input 110 (decimal 6). q01q11q00q0q_0\xrightarrow{1}q_1\xrightarrow{1}q_0\xrightarrow{0}q_0, final output 0 = 6mod36 \bmod 3. Correct. Because it is a Moore machine, the output is simply the label of the state reached after reading the whole string.

finite-automatamealy-moore-machines
10short5 marks

(a) Distinguish between a recursive language and a recursively enumerable language.

(b) State Rice's theorem and give one example of a non-trivial property of recursively enumerable languages that is undecidable as a consequence of it.

(a) Recursive vs Recursively Enumerable languages

Recursive (decidable)Recursively enumerable (r.e.)
Deciding TMA TM that halts on every input, accepting members and rejecting non-membersA TM that halts and accepts members, but may loop forever on non-members
MembershipAlways decidable (algorithm gives yes/no)Semi-decidable (yes is detectable; no may never be reported)
Closure under complementClosed (recursive languages are closed under complement)Not closed under complement
RelationshipEvery recursive language is r.e.r.e. strictly contains recursive (e.g. HALTHALT is r.e. but not recursive)

Key fact: LL is recursive iff both LL and its complement Lˉ\bar L are recursively enumerable.

(b) Rice's Theorem

Statement. Any non-trivial property of the language recognised by a Turing machine is undecidable. A property PP of r.e. languages is non-trivial if it is true for at least one r.e. language and false for at least one other (i.e. neither always true nor always false). Formally, the set

{ML(M) has property P}\{\langle M\rangle \mid L(M) \text{ has property } P\}

is undecidable whenever PP is non-trivial.

Example of an undecidable property (by Rice's theorem):

  • "Is L(M)=L(M) = \emptyset?" (the emptiness problem) — non-trivial, hence undecidable.

Other consequences: "Is L(M)L(M) regular?", "Is L(M)L(M) finite?", "Does L(M)L(M) contain a particular string ww?" — all undecidable, since each is a non-trivial property of the recognised language.

decidabilityundecidabilityrices-theorem
11short6 marks

(a) Explain the difference between a deterministic PDA (DPDA) and a non-deterministic PDA (NPDA), and state which class of languages each accepts.

(b) State the pumping lemma for context-free languages and explain how it is used to show that a language is not context-free.

(a) DPDA vs NPDA

Deterministic PDA (DPDA)Non-deterministic PDA (NPDA)
TransitionAt most one move possible in any configuration; no choice. (At most one of an input-move or a corresponding ε\varepsilon-move applies.)A configuration may allow several moves (a set of next configurations).
Languages acceptedDeterministic context-free languages (DCFL) — a proper subset of CFLsAll context-free languages (CFL)
PowerStrictly weakerStrictly more powerful
Example only NPDA handles{wwR}\{ww^R\} (even-length palindromes) needs non-determinism

Unlike finite automata (where DFA = NFA in power), for PDAs NPDA is strictly more powerful than DPDA: e.g. the language of even-length palindromes {wwRwΣ}\{ww^R \mid w\in\Sigma^*\} is context-free but not deterministic context-free.

(b) Pumping Lemma for Context-Free Languages

If LL is context-free, there is a constant p1p\ge1 (pumping length) such that every zLz\in L with zp|z|\ge p can be written as

z=uvwxyz = uvwxy

satisfying:

  1. vwxp|vwx| \le p,
  2. vx1|vx| \ge 1 (i.e. vv and xx are not both empty),
  3. uviwxiyLu v^{i} w x^{i} y \in L for all i0i \ge 0.

How it is used (proof by contradiction). To show a language LL is not context-free:

  1. Assume LL is context-free with pumping length pp.
  2. Choose a specific string zLz\in L with zp|z|\ge p (chosen cleverly).
  3. For every allowed decomposition z=uvwxyz=uvwxy with vwxp|vwx|\le p and vx1|vx|\ge1, find some ii (usually i=0i=0 or i=2i=2) such that uviwxiyLuv^iwx^iy \notin L.
  4. This contradicts condition 3, so the assumption fails and LL is not context-free.

Illustration: for L={anbncnn1}L=\{a^n b^n c^n\mid n\ge1\}, take z=apbpcpz=a^pb^pc^p; since vwxp|vwx|\le p, vwxvwx spans at most two of the three symbol blocks, so pumping (i=2i=2) unbalances the counts and the result leaves LL — proving LL is not context-free.

pushdown-automatacontext-free-languagesdpda

Frequently asked questions

Where can I find the BE Computer Engineering (IOE, TU) Theory of Computation (IOE, CT 502) question paper 2078?
The full BE Computer Engineering (IOE, TU) Theory of Computation (IOE, CT 502) 2078 (regular) question paper is available free on Kekkei. You can read every question online and attempt the paper under timed exam conditions.
Does the Theory of Computation (IOE, CT 502) 2078 paper come with solutions?
Yes. Every question on this Theory of Computation (IOE, CT 502) past paper includes a step-by-step solution, plus instant AI feedback when you attempt it on Kekkei.
How many marks is the BE Computer Engineering (IOE, TU) Theory of Computation (IOE, CT 502) 2078 paper?
The BE Computer Engineering (IOE, TU) Theory of Computation (IOE, CT 502) 2078 paper carries 80 full marks and is meant to be completed in 180 minutes, across 11 questions.
Is practising this Theory of Computation (IOE, CT 502) past paper free?
Yes — reading and attempting this Theory of Computation (IOE, CT 502) past paper on Kekkei is completely free.