CSC262 · TU past paper
Theory of Computation 2080 question paper
The complete TU 2080 exam paper for Theory of Computation (CSC262), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- 110 marksNumericalMethod for reduction of NFA to DFAHideAnswer
What is NFA? How is it different from DFA? How is NFA to DFA conversion done? Convert the following NFA into DFA.[10]
NFA to DFA: Definition, Differences, Conversion, and Worked Example
STEP 1 - EXTRACT: Given Data
The question asks for definitions and a conversion. The specific NFA to be converted was not provided in the question text (the diagram or table is missing), so the worked example below uses this standard NFA:
State a b →q₀ {q₀, q₁} {q₀} q₁ ∅ {q₂} *q₂ ∅ ∅ Start state: $q_0$; Final state: $q_2$; Alphabet $\Sigma = {a, b}$; no ε-transitions.
Since the original NFA is missing, the theory below is given in full and the conversion is worked on this assumed NFA. In the exam, apply the same subset-construction steps to the NFA printed on your paper.
STEP 2 - SOLVE
1. What is an NFA?
A Non-deterministic Finite Automaton (NFA) is a finite automaton where, for a given state and input symbol, there may be zero, one, or more next states, and ε-moves (transitions without consuming input) are permitted.
Formally, $M = (Q, \Sigma, \delta, q_0, F)$ where:
Symbol Meaning $Q$ Finite set of states $\Sigma$ Input alphabet $\delta$ $Q \times (\Sigma \cup {\varepsilon}) \to 2^{Q}$ $q_0$ Start state $F$ Set of final states A string is accepted if at least one computation path ends in a final state.
2. Difference Between NFA and DFA
Feature DFA NFA Transitions per (state, symbol) Exactly one Zero, one, or more ε-moves Not allowed Allowed Transition function $\delta: Q \times \Sigma \to Q$ $\delta: Q \times (\Sigma \cup {\varepsilon}) \to 2^{Q}$ Acceptance Unique path Any accepting path States needed Usually more Usually fewer Ease of design Harder Easier Language class Regular Regular (same power) Every DFA is an NFA; every NFA has an equivalent DFA.
3. NFA to DFA Conversion (Subset Construction)
- Step 1: DFA start state = $\varepsilon\text{-closure}(q_0)$.
- Step 2: For each DFA state $T$ and symbol $a$: compute $\varepsilon\text{-closure}(\text{move}(T,a))$ = a new DFA state.
- Step 3: Repeat until no new states appear.
- Step 4: A DFA state is final if it contains any NFA final state.
- Step 5: Missing transitions lead to a dead state $\emptyset$.
4. Worked Example (using the assumed NFA above)
Start state: $\varepsilon\text{-closure}(q_0) = {q_0} = A$
State $A = {q_0}$:
- On $a$: ${q_0, q_1} = B$
- On $b$: ${q_0} = A$
State $B = {q_0, q_1}$:
- On $a$: ${q_0, q_1} \cup \emptyset = {q_0, q_1} = B$
- On $b$: ${q_0} \cup {q_2} = {q_0, q_2} = C$
State $C = {q_0, q_2}$:
- On $a$: ${q_0, q_1} \cup \emptyset = {q_0, q_1} = B$
- On $b$: ${q_0} \cup \emptyset = {q_0} = A$
No new states. Final DFA Transition Table:
DFA State NFA States On $a$ On $b$ Final? →A ${q_0}$ B A No B ${q_0, q_1}$ B C No C ${q_0, q_2}$ B A Yes (contains $q_2$) Start state = $A$; Final state = $C$ (since it contains the NFA final state $q_2$). This DFA accepts all strings over ${a,b}$ ending in "$ab$".
Note on Missing Data
The actual NFA diagram or table intended by the exam was not included in the question, so the worked conversion above uses the assumed NFA. The theory portions (definition, differences, algorithm) are complete and correct regardless.
Check: the transition computations are consistent ($A\to B/A$, $B\to B/C$, $C\to B/A$), and the final state is $C$, the only subset containing $q_2$.
- 210 marksNumericalAcceptance of a string by a Turing MachineHideAnswer
How does Turing machine accept a string? Design a Turing Machine over the alphabet {0,1,a} that processes the string defined by L = {a01a,a10a,a0101a}. Show both transition diagram and table. Show acceptance of a0101a.[10]
Turing Machine for L = {a01a, a10a, a0101a}
STEP 1 - EXTRACT: Given Data
- Alphabet (Σ): ${0, 1, a}$
- Language: $L = {a01a,\ a10a,\ a0101a}$ (a finite language of exactly 3 strings)
- Tape symbols (Γ): ${0, 1, a, B}$ where $B$ = blank
- Required: definition of acceptance, TM design, transition diagram, transition table, trace of
a0101a
All data is present; nothing missing.
STEP 2 - SOLVE
1. How a Turing Machine Accepts a String
A Turing Machine is a 7-tuple: $$M = (Q, \Sigma, \Gamma, \delta, q_0, B, F)$$
- $Q$: finite set of states
- $\Sigma$: input alphabet
- $\Gamma$: tape alphabet ($\Sigma \subseteq \Gamma$, includes $B$)
- $\delta: Q \times \Gamma \rightarrow Q \times \Gamma \times {L, R}$: transition function
- $q_0$: initial state
- $B$: blank symbol
- $F \subseteq Q$: set of final (accepting) states
Acceptance process:
- The input string $x$ is written on the tape, surrounded by blanks $B$.
- The head starts at the leftmost symbol of $x$; machine begins in $q_0$.
- At each step the machine reads the current cell, uses $\delta$ to write a symbol, move (L/R), and change state.
- A string is accepted if the machine halts in a final state ($q \in F$).
- It is rejected if it halts in a non-final state or never halts.
$$L(M) = { x \in \Sigma^* \mid q_0 x \vdash^* \alpha, q_f, \beta,\ q_f \in F}$$
2. Design Strategy
Since $L$ is finite (3 exact strings), the TM scans left-to-right, tracking progress through the shared prefix
a, then branching. Since we only recognize, we write back the same symbol and move R.States:
State Meaning $q_0$ expect leading a$q_1$ after a; expect0or1$q_2$ after a0; expect1$q_3$ after a01; expect finalaor continue0$q_4$ after a1; expect0$q_5$ after a10; expect finala$q_6$ after a010; expect1$q_7$ after a0101; expect finala$q_8,q_9,q_{10}$ after trailing a; verify blank end$q_a$ accept (final) $q_r$ reject 3. Transition Table
$\delta(\text{state},\text{read}) = (\text{write},\text{move},\text{next})$
State Read Write Move Next $q_0$ a a R $q_1$ $q_0$ 0,1,B - - $q_r$ $q_1$ 0 0 R $q_2$ $q_1$ 1 1 R $q_4$ $q_1$ a,B - - $q_r$ $q_2$ 1 1 R $q_3$ $q_2$ 0,a,B - - $q_r$ $q_3$ a a R $q_8$ $q_3$ 0 0 R $q_6$ $q_3$ 1,B - - $q_r$ $q_4$ 0 0 R $q_5$ $q_4$ 1,a,B - - $q_r$ $q_5$ a a R $q_9$ $q_5$ 0,1,B - - $q_r$ $q_6$ 1 1 R $q_7$ $q_6$ 0,a,B - - $q_r$ $q_7$ a a R $q_{10}$ $q_7$ 0,1,B - - $q_r$ $q_8$ B B - $q_a$ $q_8$ 0,1,a - - $q_r$ $q_9$ B B - $q_a$ $q_9$ 0,1,a - - $q_r$ $q_{10}$ B B - $q_a$ $q_{10}$ 0,1,a - - $q_r$ 4. Transition Diagram
a 0 1 a B ->(q0) --------> (q1) ----> (q2) ----> (q3) ----> (q8) ----> ((qa)) | 1 a01a path | v | 0 (q4) v | 0 (q6) v | 1 (q5) v | a (q7) v | a (q9) v | B (q10) v | B ((qa)) v ((qa))(All undefined/unexpected inputs go to $q_r$, reject.)
5. Acceptance of
a0101aTape: $B\ a\ 0\ 1\ 0\ 1\ a\ B$
Step State Head reads Action Next 1 $q_0$ a write a, R $q_1$ 2 $q_1$ 0 write 0, R $q_2$ 3 $q_2$ 1 write 1, R $q_3$ 4 $q_3$ 0 write 0, R $q_6$ 5 $q_6$ 1 write 1, R $q_7$ 6 $q_7$ a write a, R $q_{10}$ 7 $q_{10}$ B write B $q_a$ The machine halts in $q_a$ (final state), so the string
a0101ais ACCEPTED. ✓ - 310 marksNumericalChomsky Normal FormHideAnswer
Define context free grammar with an example. Explain with example, how context free grammar is converted to Chomsky Normal Form.[10]
Context Free Grammar and Conversion to Chomsky Normal Form
STEP 1 - EXTRACT (Given data)
This is a theory and derivation question. The only structural data needed is the grammar chosen for the worked example, which is:
$$ \begin{aligned} S &\to ASB \mid \varepsilon\ A &\to aAS \mid a\ B &\to SbS \mid A \mid bb \end{aligned} $$
No data is missing; the question is fully answerable.
STEP 2 - SOLVE
Part 1: Context Free Grammar (CFG)
A Context Free Grammar is a 4-tuple $G = (V, T, P, S)$ where:
- $V$ = finite set of non-terminals (variables)
- $T$ = finite set of terminals, with $V \cap T = \varnothing$
- $P$ = finite set of productions of the form $A \to \alpha$, where $A \in V$ and $\alpha \in (V \cup T)^*$
- $S \in V$ = start symbol
Every production has exactly one non-terminal on the left, so it can be replaced regardless of surrounding context - hence "context free."
Example: $$S \to 0S1 \mid \varepsilon$$
- $V={S},\ T={0,1},\ S=S$
- Generates ${0^n1^n \mid n \ge 0} = {\varepsilon, 01, 0011, \dots}$
CFGs describe the syntax of programming languages and drive parser/compiler design.
Part 2: Chomsky Normal Form (CNF)
A CFG is in CNF if every production is of the form:
- $A \to BC$ (two non-terminals), or
- $A \to a$ (single terminal)
with the sole exception $S \to \varepsilon$ allowed if $\varepsilon$ is in the language, and $S$ must not appear on any RHS.
Conversion steps:
- Add a new start symbol.
- Eliminate $\varepsilon$-productions.
- Eliminate unit productions.
- Reduce RHS length to 2 (introduce variables).
- Replace terminals in long productions with new variables.
Worked Example
Original grammar: $$ S \to ASB \mid \varepsilon,\quad A \to aAS \mid a,\quad B \to SbS \mid A \mid bb $$
Step 1: New start symbol $S_0$
$$ S_0 \to S,\quad S \to ASB \mid \varepsilon,\quad A \to aAS \mid a,\quad B \to SbS \mid A \mid bb $$
Step 2: Eliminate $\varepsilon$-productions
Nullable variables: $S$ (since $S\to\varepsilon$). Insert versions with $S$ deleted:
- $S \to ASB \Rightarrow S \to ASB \mid AB$
- $A \to aAS \Rightarrow A \to aAS \mid aA$
- $B \to SbS \Rightarrow B \to SbS \mid bS \mid Sb \mid b$
- $S_0 \to S$ keeps $S_0 \to \varepsilon$ (since $\varepsilon$ is in the language)
$$ \begin{aligned} S_0 &\to S \mid \varepsilon\ S &\to ASB \mid AB\ A &\to aAS \mid aA \mid a\ B &\to SbS \mid bS \mid Sb \mid b \mid A \mid bb \end{aligned} $$
Step 3: Eliminate unit productions
Unit pairs: $S_0 \to S$, $B \to A$.
- $S_0 \to S$: $S_0 \to ASB \mid AB$
- $B \to A$: $B \to aAS \mid aA \mid a$
$$ \begin{aligned} S_0 &\to ASB \mid AB \mid \varepsilon\ S &\to ASB \mid AB\ A &\to aAS \mid aA \mid a\ B &\to SbS \mid bS \mid Sb \mid b \mid aAS \mid aA \mid a \mid bb \end{aligned} $$
Step 4: Replace terminals in multi-symbol productions
Introduce $X_a \to a$, $X_b \to b$. Replace terminals wherever they sit with other symbols:
$$ \begin{aligned} S_0 &\to ASB \mid AB \mid \varepsilon\ S &\to ASB \mid AB\ A &\to X_aAS \mid X_aA \mid a\ B &\to SX_bS \mid X_bS \mid SX_b \mid b \mid X_aAS \mid X_aA \mid a \mid X_bX_b\ X_a &\to a\ X_b &\to b \end{aligned} $$
Step 5: Reduce RHS length to 2
Introduce helper variables:
- $ASB$: let $C_1 \to SB$, so $\to A C_1$
- $X_aAS$: let $C_2 \to AS$, so $\to X_a C_2$
- $SX_bS$: let $C_3 \to X_bS$, so $\to S C_3$
Final CNF grammar: $$ \begin{aligned} S_0 &\to AC_1 \mid AB \mid \varepsilon\ S &\to AC_1 \mid AB\ A &\to X_aC_2 \mid X_aA \mid a\ B &\to SC_3 \mid X_bS \mid SX_b \mid b \mid X_aC_2 \mid X_aA \mid a \mid X_bX_b\ C_1 &\to SB\ C_2 &\to AS\ C_3 &\to X_bS\ X_a &\to a,\qquad X_b \to b \end{aligned} $$
Every production is now $A \to BC$ or $A \to a$ (plus the permitted $S_0 \to \varepsilon$), so the grammar is in Chomsky Normal Form.
- 45 marksStringsHideAnswer
Define string, substring, empty string, and empty language over alphabet {a,b}. [5]
String, Substring, Empty String, and Empty Language over {a, b}
1. String
A string (also called a word) is a finite sequence of symbols taken from an alphabet.
Over the alphabet Σ = {a, b}, a string is any finite arrangement of the symbols a and b.
Examples over {a, b}:
ababaabbbaabab
Formally, if Σ = {a, b}, then Σ* (Kleene closure) gives the set of all possible strings over {a, b}, including the empty string:
Σ* = {ε, a, b, aa, ab, ba, bb, aaa, aab, ...}
2. Substring
A string v is called a substring of string w if v appears as a contiguous sequence of symbols within w (obtained by removing zero or more leading symbols and zero or more trailing symbols from w).
Example over {a, b}:
Let w =
ababSubstrings of w include:
a,b,ab,ba,aba,bab,abab,ε
Note: Every string is a substring of itself, and the empty string ε is a substring of every string.
3. Empty String
The empty string (denoted ε or sometimes λ) is a string that contains no symbols at all. Its length is 0.
Key properties of ε over {a, b}:
Property Result Length of ε |ε| = 0 Concatenation with any string x ε x = x ε = x ε belongs to Σ* for any alphabet Σ Example:
If x =
ab, then ε · x =aband x · ε =abThe empty string acts as the identity element for string concatenation.
4. Empty Language
A language over an alphabet Σ is a set of strings, i.e., a subset of Σ*.
The empty language (denoted ∅) is a language that contains no strings at all.
Over {a, b}:
∅ = { } (a set with no members)
Important distinction:
Concept Notation Contains Empty string ε A string of length 0 Empty language ∅ No strings whatsoever Language with only empty string {ε} Exactly one string (ε) Note: ∅ ≠ {ε}. The empty language has no strings, whereas {ε} is a language containing one string (the empty string).
The empty language ∅ is a valid language over any alphabet, including {a, b}.
Summary Table
Term Definition Example over {a,b} String Finite sequence of symbols from Σ ab,bba,aabSubstring Contiguous part of a string abis substring ofaabEmpty String (ε) String with no symbols, length = 0 ε Empty Language (∅) Language containing no strings ∅ = { } - 55 marksNumericalDeterministic Finite AutomataHideAnswer
Design a DFA that accepts single line and multi-line comments of the C-Language. [5]
- Single-line comment: begins with //, terminates at newline \n. - Multi-line comment: begins with /, terminates at first /. - Alphabet symbols needed: /, , \n, and other (any character that is not /, , or \n). No numeric matrices are in...
- 65 marksNumericalRegular ExpressionsHideAnswer
Write regular expression over {a,b} that represents a. Strings having exactly two a's and at least two b's. b. Strings having an even number of a's and each a followed by at least one b. [5]
Regular Expressions over {a, b}
Given data
- Alphabet: $\Sigma = {a, b}$
- Part (a): strings with exactly two a's AND at least two b's.
- Part (b): strings with an even number of a's, and each a immediately followed by at least one b.
Part (a): Exactly two a's and at least two b's
Structure. With exactly two a's, every string has the shape:
$$b^,a,b^,a,b^*$$
The three $b^*$ blocks (before, between, after) hold all the b's. The constraint is that the total number of b's across all three blocks is at least 2.
Deriving a compact expression. The plain shape $b^*ab^ab^$ allows 0 or 1 b's, which we must forbid. So we subtract those cases by forcing the b-count $\ge 2$. This is cleanly expressed by casing on where the "guaranteed" b's sit. A correct enumeration (each covers b-total $\ge 2$, union covers all):
$$R = b^{2+}ab^ab^ ;+; b^+ab^+ab^* ;+; b^+ab^*ab^+ ;+; b^ab^{2+}ab^ ;+; b^*ab^+ab^+ ;+; b^*ab^*ab^{2+}$$
where $b^{2+} = bbb^$ (at least two b's) and $b^+ = bb^$.
A shorter equivalent that is fully acceptable in an exam:
$$\boxed{R = b^*a,b^a,b^\ \text{restricted to total } b\text{'s} \ge 2}$$
expressed formally as the union above. In terms of "at least one block has $bb$, OR two blocks each have a $b$":
$$R = bbb^* a, b^* a, b^* + b^* a, bbb^* a, b^* + b^* a, b^* a, bbb^* + bb^* a, bb^* a, b^* + bb^* a, b^* a, bb^* + b^* a, bb^* a, bb^*$$
Check. Every accepted string must carry exactly two a's, so test $baab$: it has one b before the a's, the two a's adjacent, and one b after, giving a b-total of 2, and it matches the term $b^+ab^*ab^+$. The string $aab$ carries only one b and is correctly rejected. The string $bbaa$ has two b's ahead of the first a and matches the term $b^{2+}ab^ab^$.
Part (b): Even number of a's, each a followed by at least one b
Structure.
- Each a must be immediately followed by at least one b, so the basic unit is $ab^+$ (i.e. $abb^*$).
- The number of a's must be even, so units come in pairs: $(ab^+ab^+)$, repeated zero or more times.
- Extra b's may appear at the start (before any a).
Regular expression:
$$\boxed{R = b^,(ab^+,ab^+)^}$$
Verification:
String Match? Reason $\varepsilon$ Yes zero a's (even) $bb$ Yes $b^*$ only $abab$ Yes one pair $ab,ab$ $abbabb$ Yes $abb,abb$ $babbab$ Yes $b^*=b$, then $abb,ab$ $ab$ No one a (odd) $aab$ No first a not followed by b $abba$ No last a not followed by b
Final Answers
(a) Exactly two a's and at least two b's: $$R = b^{2+}ab^ab^ + b^+ab^+ab^* + b^+ab^*ab^+ + b^ab^{2+}ab^ + b^*ab^+ab^+ + b^*ab^*ab^{2+}$$
(b) Even number of a's, each a followed by at least one b: $$R = b^(ab^+ab^+)^$$
- 75 marksNumericalPumping LemmaHideAnswer
Using pumping lemma, prove that the language is not regular. $L = {a^i b^j c^k \mid j=i+k}$ [5]
- Language: $L = {a^i b^j c^k \mid j = i + k,\ i,k \ge 0}$ - Alphabet: ${a, b, c}$ - Method required: Pumping Lemma for regular languages. --- Suppose $L$ is regular. Then by the Pumping Lemma, there exists a pumping length $p \ge 1$...
- 85 marksNumericalConstruction of PDA by Final StateHideAnswer
Design a PDA over {x,y} which accepts strings defined by the language Show acceptance of xxyy. $L = {x^n y^n xy \mid n \geq 0}$ [5]
- Language: $L = {x^n y^n xy \mid n \geq 0}$ - Alphabet: $\Sigma = {x, y}$ - String to trace for acceptance: xxyy The problem asks to show acceptance of xxyy. Let us check whether "xxyy" is in $L$. A string in $L$ has the form
- 95 marksNumericalTuring Machine as a Computing FunctionHideAnswer
Design a Turing machine that computes a function f(n)=0. [5]
- Function to compute: $f(n) = 0$ for all $n \in \mathbb{N}$. - Input encoding (standard unary): a natural number $n$ is written as $n$ ones, i.e. tape holds $B,1^n,B$. - Output encoding: result $0$ is represented by $0$ ones, i.e. a b...
- 105 marksProblem and its typesHideAnswer
How abstract, decision and optimization problems are different from each other? [5]
An abstract problem is a general, mathematical formulation of a computational problem. It defines a relationship between a set of problem instances (inputs) and a set of solutions (outputs), without restricting the form of the answer. - ...
- 115 marksNumericalConversion of PDA to CFGHideAnswer
How is PDA to CFG conversion done? Consider a PDA that accepts by empty stack, Now construct an equivalent CFG. $P = ((p,q);{0,1},{Z},\delta,p,Z);$ $\delta(p,0,Z)=(p,0z), \delta(p,0,0)=(p,00), \delta(p,1,0)=(p,\varepsilon), \delta(p,\varepsilon,z)=(q,\varepsilon)$ [5]
PDA $P = ({p, q}, {0,1}, {Z, 0}, \delta, p, Z)$ accepting by empty stack. Transitions: - $\delta(p, 0, Z) = (p, 0Z)$ - $\delta(p, 0, 0) = (p, 00)$ - $\delta(p, 1, 0) = (p, \varepsilon)$ -
- 125 marksIntroduction to Context Free GrammarHideAnswer
What is the meaning of the term 'Context Free' in context free grammar? Justify with a suitable example. What is the need of a parse tree? [5]
--- The term "Context Free" means that the production rules can be applied to a non-terminal symbol regardless of the context (surrounding symbols) in which that non-terminal appears. Formally, a Context Free Grammar is defined as: G = (...