Browse papers
A

Section A: Long Answer Questions

Attempt all / any as specified.

4 questions
1long15 marks

(a) Distinguish between a Deterministic Finite Automaton (DFA) and a Non-deterministic Finite Automaton (NFA), clearly stating the role of the transition function in each. (5)

(b) Consider the NFA with ϵ\epsilon-transitions over the alphabet Σ={0,1}\Sigma = \{0, 1\} that accepts strings containing the substring 01 or ending in 00. Construct an equivalent DFA using the subset (powerset) construction method, showing the resulting state-transition table and the final transition diagram. (10)

(a) DFA vs NFA (5 marks)

AspectDFANFA
Transition functionδ:Q×ΣQ\delta : Q \times \Sigma \to Q — exactly one next state for each (state, symbol) pairδ:Q×(Σ{ϵ})2Q\delta : Q \times (\Sigma \cup \{\epsilon\}) \to 2^{Q} — a set of next states (0, 1 or more), and ϵ\epsilon-moves allowed
DeterminismNext move is uniquely determinedSeveral moves may be possible; machine "guesses"
ϵ\epsilon-movesNot allowedAllowed
AcceptanceSingle computation path; accept if it ends in a final stateAccept if some path ends in a final state
ImplementationDirectly executableNeeds simulation / conversion to DFA

Role of the transition function: In a DFA, δ\delta is a total function returning a single state, so the run is deterministic. In an NFA, δ\delta returns a set of states, so from one configuration the machine may proceed along many branches in parallel. Both recognise exactly the regular languages (Rabin–Scott theorem), and every NFA can be converted to an equivalent DFA by subset construction.

(b) NFA → DFA by subset construction (10 marks)

We want strings over {0,1}\{0,1\} that contain 01 OR end in 00. An NFA NN for this (states for the two patterns merged) has the following ϵ\epsilon-free transitions. Let the start state be AA. We design NN directly:

  • AA: looking for either pattern
  • BB: just saw a 0 (potential start of 01 or 00)
  • CC: substring 01 found — accept (and stay accepting)
  • DD: saw 00 so far — accept, but pattern may be broken by later input unless string ends here

NFA transition relation δN\delta_N:

Stateon 0on 1
AA{A,B}\{A,B\}{A}\{A\}
BB{B,D}\{B,D\}{C}\{C\}
CC (final){C}\{C\}{C}\{C\}
DD (final){B,D}\{B,D\}{C}\{C\}

Final states of NN: C,DC, D (note DD accepts only when the string ends right after 00).

Subset construction. Start = {A}\{A\}.

DFA stateon 0on 1
S0={A}S_0=\{A\}{A,B}=S1\{A,B\}=S_1{A}=S0\{A\}=S_0
S1={A,B}S_1=\{A,B\}{A,B,D}=S2\{A,B,D\}=S_2{A,C}=S3\{A,C\}=S_3
S2={A,B,D}S_2=\{A,B,D\}{A,B,D}=S2\{A,B,D\}=S_2{A,C}=S3\{A,C\}=S_3
S3={A,C}S_3=\{A,C\} (final){A,B,C}=S4\{A,B,C\}=S_4{A,C}=S3\{A,C\}=S_3
S4={A,B,C}S_4=\{A,B,C\} (final){A,B,C,D}=S5\{A,B,C,D\}=S_5{A,C}=S3\{A,C\}=S_3
S5={A,B,C,D}S_5=\{A,B,C,D\} (final)S5S_5S3S_3

Final (accepting) DFA states: every subset containing CC or DD, i.e. S2S_2 (contains DD), S3,S4,S5S_3, S_4, S_5 (contain CC). S2S_2 is accepting because reaching it means 00 was just read.

Transition diagram (described):

  • Start arrow into S0S_0.
  • S00S1S_0 \xrightarrow{0} S_1, S01S0S_0 \xrightarrow{1} S_0.
  • S10S2S_1 \xrightarrow{0} S_2, S11S3S_1 \xrightarrow{1} S_3.
  • S20S2S_2 \xrightarrow{0} S_2, S21S3S_2 \xrightarrow{1} S_3.
  • S30S4S_3 \xrightarrow{0} S_4, S31S3S_3 \xrightarrow{1} S_3 (self-loop).
  • S40S5S_4 \xrightarrow{0} S_5, S41S3S_4 \xrightarrow{1} S_3.
  • S50S5S_5 \xrightarrow{0} S_5 (self-loop), S51S3S_5 \xrightarrow{1} S_3.
  • Double circles (final): S2,S3,S4,S5S_2, S_3, S_4, S_5.

Once the DFA enters S3S_3 (substring 01 seen) it can never leave the accepting region, exactly capturing the "contains 01" condition, while S2/S4/S5S_2/S_4/S_5 capture "ends in 00".

finite-automatadfa-nfasubset-construction
2long15 marks

(a) Define a Context-Free Grammar (CFG) formally as a 4-tuple. Design a CFG that generates the language L={anbmcmdnn1, m0}L = \{ a^n b^m c^m d^n \mid n \ge 1,\ m \ge 0 \} and show a leftmost derivation for the string aabccbb... if it belongs to LL (justify your answer). (7)

(b) Convert the following grammar into Chomsky Normal Form (CNF), showing each step (removal of ϵ\epsilon-productions, unit productions, and useless symbols): (8)

SASBϵ,AaASa,BSbSAbbS \rightarrow ASB \mid \epsilon,\quad A \rightarrow aAS \mid a,\quad B \rightarrow SbS \mid A \mid bb

(a) CFG definition and design (7 marks)

A Context-Free Grammar is a 4-tuple G=(V,T,P,S)G = (V, T, P, S) where:

  • VV = finite set of variables (non-terminals),
  • TT = finite set of terminals, VT=V \cap T = \emptyset,
  • PP = finite set of productions of the form AαA \to \alpha, with AVA \in V and α(VT)\alpha \in (V \cup T)^{*},
  • SVS \in V = start symbol.

Grammar for L={anbmcmdnn1,m0}L = \{a^{n} b^{m} c^{m} d^{n} \mid n \ge 1, m \ge 0\}:

SaSdaBd,BbBcϵS \to a\,S\,d \mid a\,B\,d, \qquad B \to b\,B\,c \mid \epsilon

Here the SS-rules generate matching outer ada\ldots d pairs (n1n \ge 1), and BB generates the matched inner bcb\ldots c pairs (m0m \ge 0).

Is aabccbb in LL? A string of LL must have the strict order anbmcmdna^{n}b^{m}c^{m}d^{n}: all aa's, then bb's, then cc's, then dd's, with #a=#d\#a = \#d and #b=#c\#b = \#c. The string aabccbb has letters in the order a,a,b,c,c,b,ba,a,b,c,c,b,b — a b appears after c, and there is no d. So the order is violated and #d=0#a=2\#d = 0 \ne \#a = 2. Hence aabccbb L\notin L, and no derivation exists.

(For completeness, a valid string such as aabccd has the leftmost derivation: SaSdaaBddS \Rightarrow aSd \Rightarrow aaBdd? — note #a\#a must equal #d\#d. Take aabccd is invalid too since #a=2,#d=1\#a=2,\#d=1. A correct member is aabbccdd:*

SaSdaaBddaabBcddaabbBccddaabbϵccdd=aabbccdd.)S \Rightarrow aSd \Rightarrow aaBdd \Rightarrow aabBcdd \Rightarrow aabbBccdd \Rightarrow aabb\,\epsilon\,ccdd = aabbccdd.)*

(b) Conversion to Chomsky Normal Form (8 marks)

Given SASBϵ,    AaASa,    BSbSAbbS \to ASB \mid \epsilon, \;\; A \to aAS \mid a, \;\; B \to SbS \mid A \mid bb.

Step 1 — Add new start symbol. S0SS_0 \to S (so the start symbol never appears on a RHS).

Step 2 — Remove ϵ\epsilon-productions. Nullable variables: SS (from SϵS\to\epsilon). BSbSB \to SbS does not become nullable (the bb stays). Substitute nullable SS in all RHSs and drop SϵS\to\epsilon:

  • S0SS_0 \to S
  • SASBABS \to ASB \mid AB (drop one or both SS… here only outer/inner SS in ASBASB is the middle SS): ASBASB with the SS removed gives ABAB. So SASBABS \to ASB \mid AB.
  • AaASaAaA \to aAS \mid aA \mid a (remove SS from aASaAS gives aAaA)
  • BSbSSbbSbAbbB \to SbS \mid Sb \mid bS \mid b \mid A \mid bb (delete each nullable SS)

Step 3 — Remove unit productions. Units: S0SS_0 \to S, BAB \to A.

  • S0S_0 inherits SS's rules: S0ASBABS_0 \to ASB \mid AB.
  • BAB \to ABB inherits AA's rules: BaASaAaB \to aAS \mid aA \mid a. Now BSbSSbbSbbbaASaAaB \to SbS \mid Sb \mid bS \mid b \mid bb \mid aAS \mid aA \mid a.

Step 4 — Remove useless symbols. All of S0,S,A,BS_0,S,A,B are reachable and generate terminal strings, so none are removed.

Step 5 — Make all bodies length-2 of variables, or a single terminal. Introduce terminal variables XaaX_a \to a, XbbX_b \to b, and break long bodies with helper variables.

Final CNF grammar:

S0 -> A C1 | A B          (C1 -> S B)
S  -> A C1 | A B
A  -> X_a C2 | X_a A | a   (C2 -> A S)
B  -> S C3 | S X_b | X_b S | b | X_b X_b
       | X_a C2 | X_a A | a
       (C3 -> X_b S)
C1 -> S B
C2 -> A S
C3 -> X_b S
X_a -> a
X_b -> b

Every production now has the form ABCA \to BC (two variables) or AaA \to a (single terminal), so the grammar is in Chomsky Normal Form.

context-free-grammarchomsky-normal-formcfg-design
3long15 marks

(a) Describe the formal model of a standard (single-tape) Turing Machine as a 7-tuple and explain how it accepts, rejects, or loops on an input string. (5)

(b) Design a Turing Machine that accepts the language L={wcwRw{a,b}}L = \{ w c w^{R} \mid w \in \{a, b\}^{*} \}, where wRw^{R} denotes the reverse of ww. Give the complete transition table and trace the computation of the machine on the input string abcba. (10)

(a) Formal model of a single-tape Turing Machine (5 marks)

A standard Turing Machine is a 7-tuple M=(Q,Σ,Γ,δ,q0,qaccept,qreject)M = (Q, \Sigma, \Gamma, \delta, q_0, q_{accept}, q_{reject}) where:

  • QQ = finite set of states,
  • Σ\Sigma = input alphabet (blank Σ\sqcup \notin \Sigma),
  • Γ\Gamma = tape alphabet, ΣΓ\Sigma \subseteq \Gamma and Γ\sqcup \in \Gamma,
  • δ:Q×ΓQ×Γ×{L,R}\delta : Q \times \Gamma \to Q \times \Gamma \times \{L, R\} = transition function (read symbol, write symbol, move head Left/Right),
  • q0Qq_0 \in Q = start state,
  • qaccept,qrejectQq_{accept}, q_{reject} \in Q = accepting and rejecting (halting) states, qacceptqrejectq_{accept} \ne q_{reject}.

Behaviour on input ww: MM starts in q0q_0 with ww on the tape and head on the first symbol. At each step it reads the current cell, writes a symbol, moves L or R, and changes state per δ\delta. There are three outcomes:

  • Accept — it reaches qacceptq_{accept} (halts, wL(M)w \in L(M));
  • Reject — it reaches qrejectq_{reject} (halts, wL(M)w \notin L(M));
  • Loop — it never halts.

A language is recursively enumerable if some TM accepts it; it is recursive (decidable) if some TM halts on every input (never loops).

(b) TM for L={wcwRw{a,b}}L = \{w c w^{R} \mid w \in \{a,b\}^{*}\} (10 marks)

Idea: Match the outermost symbols against the innermost, working from both ends toward the centre c. Read and erase (mark) the first unmarked symbol, then move right to the corresponding last unmarked symbol and check it equals the one remembered. The single center c must remain when all of w,wRw,w^R are matched.

Let XX mark a consumed cell. States: q0q_0 (start/find left symbol), qa,qbq_a,q_b (carry remembered symbol rightward), qa,qbq_{a'},q_{b'} (after matching, return left), qcq_c (check center), qaccq_{acc}, qrejq_{rej}.

Transition table (entry = write, move, next state):

StateabcX\sqcup
q0q_0X,R,qaX,R,q_aX,R,qbX,R,q_bc,R,qcc,R,q_cX,R,q0X,R,q_0,,qrej-,-,q_{rej}
qaq_aa,R,qaa,R,q_ab,R,qab,R,q_ac,R,qac,R,q_aX,L,qaX,L,q_{a'}move L, check a
qbq_ba,R,qba,R,q_bb,R,qbb,R,q_bc,R,qbc,R,q_bX,L,qbX,L,q_{b'}move L, check b
qaq_{a'} (need a at right end)(last sym is a) X,L,qretX,L,q_{ret}(b) qrej\to q_{rej}
qbq_{b'} (need b at right end)(a) qrej\to q_{rej}X,L,qretX,L,q_{ret}
qretq_{ret}a,L,qreta,L,q_{ret}b,L,qretb,L,q_{ret}c,L,qretc,L,q_{ret}X,R,q0X,R,q_0
qcq_c,,qrej-,-,q_{rej},,qrej-,-,q_{rej}c?c?,,qacc\sqcup,-,q_{acc}

(In words: qa/qbq_a/q_b scan right to the rightmost unmarked symbol; states qa/qbq_{a'}/q_{b'} verify that symbol matches the remembered one and mark it; qretq_{ret} rewinds to the left end; qcq_c checks that only the single c remains, then accepts.)

Trace on abcba (tape shown, head position underlined conceptually; XX = matched):

  1. a b c b a, q0q_0: read a → mark, remember a, go right: X b c b a, qaq_a.
  2. qaq_a scans right over b,c,b to last a; matches remembered a → mark: X b c b X, return left.
  3. Back at left, q0q_0 reads b → mark, remember b: X X c b X, qbq_b.
  4. qbq_b scans right to last unmarked b; matches → mark: X X c X X, return left.
  5. q0q_0 reads c → enter center-check qcq_c: tape X X c X X, only the single c is unmatched, surrounding all XXaccept.

Since every aa/bb on the left matched its mirror on the right and exactly one c separates them, abcba (w=abw=ab, wR=baw^R=ba) is accepted.

turing-machinetm-designcomputability
4long15 marks

(a) Explain the formal definition of a Pushdown Automaton (PDA) and distinguish between acceptance by final state and acceptance by empty stack. (5)

(b) Construct a PDA that accepts the language L={anb2nn1}L = \{ a^n b^{2n} \mid n \ge 1 \} by empty stack. Give the transition function and show the sequence of instantaneous descriptions (moves) for the input string aabbbb. (10)

(a) PDA definition; final state vs empty stack (5 marks)

A Pushdown Automaton is a 7-tuple M=(Q,Σ,Γ,δ,q0,Z0,F)M = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F) where:

  • QQ = finite set of states,
  • Σ\Sigma = input alphabet,
  • Γ\Gamma = stack alphabet,
  • δ:Q×(Σ{ϵ})×Γ\delta : Q \times (\Sigma \cup \{\epsilon\}) \times \Gamma \to finite subsets of Q×ΓQ \times \Gamma^{*} (transition function),
  • q0Qq_0 \in Q = start state,
  • Z0ΓZ_0 \in \Gamma = initial stack symbol,
  • FQF \subseteq Q = set of final states.

A PDA augments a finite control with an unbounded stack (LIFO), giving it the extra memory needed to recognise context-free languages.

Acceptance by final state: the input is accepted if, after reading all input, the PDA is in some state of FF (stack contents irrelevant):

L(M)={w(q0,w,Z0)(q,ϵ,γ), qF}.L(M) = \{ w \mid (q_0, w, Z_0) \vdash^{*} (q, \epsilon, \gamma),\ q \in F \}.

Acceptance by empty stack: the input is accepted if, after reading all input, the stack is empty (final state irrelevant):

N(M)={w(q0,w,Z0)(q,ϵ,ϵ)}.N(M) = \{ w \mid (q_0, w, Z_0) \vdash^{*} (q, \epsilon, \epsilon) \}.

The two modes recognise the same class (context-free languages); each can be converted to the other.

(b) PDA for L={anb2nn1}L = \{a^{n} b^{2n} \mid n \ge 1\} by empty stack (10 marks)

Idea: For each a, push two stack symbols; for each b, pop one. Then nn a's push 2n2n markers and 2n2n b's pop them, leaving the stack empty exactly when the count matches.

Let M=({q0,q1},{a,b},{Z0,A},δ,q0,Z0,)M = (\{q_0, q_1\}, \{a,b\}, \{Z_0, A\}, \delta, q_0, Z_0, \emptyset) (empty-stack acceptance).

Transition function:

  1. δ(q0,a,Z0)={(q0,AAZ0)}\delta(q_0, a, Z_0) = \{(q_0, AAZ_0)\} — first a: push two AA's above Z0Z_0.
  2. δ(q0,a,A)={(q0,AAA)}\delta(q_0, a, A) = \{(q_0, AAA)\} — each further a: push two more AA's.
  3. δ(q0,b,A)={(q1,ϵ)}\delta(q_0, b, A) = \{(q_1, \epsilon)\} — first b: pop one AA, switch to q1q_1.
  4. δ(q1,b,A)={(q1,ϵ)}\delta(q_1, b, A) = \{(q_1, \epsilon)\} — each further b: pop one AA.
  5. δ(q1,ϵ,Z0)={(q1,ϵ)}\delta(q_1, \epsilon, Z_0) = \{(q_1, \epsilon)\} — when all AA's gone, pop Z0Z_0 to empty the stack.

Moves (instantaneous descriptions) on aabbbb (n=2n=2, so 2n=42n=4 b's):

(q0, aabbbb, Z0)(q0, abbbb, AAZ0)rule 1(q0, bbbb, AAAAZ0)rule 2(q1, bbb, AAAZ0)rule 3(q1, bb, AAZ0)rule 4(q1, b, AZ0)rule 4(q1, ϵ, Z0)rule 4(q1, ϵ, ϵ)rule 5\begin{aligned} (q_0,\ aabbbb,\ Z_0) &\vdash (q_0,\ abbbb,\ AAZ_0) &&\text{rule 1}\\ &\vdash (q_0,\ bbbb,\ AAAAZ_0) &&\text{rule 2}\\ &\vdash (q_1,\ bbb,\ AAAZ_0) &&\text{rule 3}\\ &\vdash (q_1,\ bb,\ AAZ_0) &&\text{rule 4}\\ &\vdash (q_1,\ b,\ AZ_0) &&\text{rule 4}\\ &\vdash (q_1,\ \epsilon,\ Z_0) &&\text{rule 4}\\ &\vdash (q_1,\ \epsilon,\ \epsilon) &&\text{rule 5} \end{aligned}

All input is consumed and the stack is empty, so aabbbb is accepted. The two a's pushed four AA's, the four b's popped them, and Z0Z_0 was finally popped — confirming a2b4La^{2}b^{4} \in L.

pushdown-automatacfg-to-pdacontext-free-grammar
B

Section B: Short Answer Questions

Attempt all / any as specified.

8 questions
5short7 marks

State the pumping lemma for regular languages. Using it, prove that the language L={aibji>j0}L = \{ a^{i} b^{j} \mid i > j \ge 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 split as w=xyzw = xyz satisfying:

  1. y1|y| \ge 1 (i.e. yϵy \ne \epsilon),
  2. xyp|xy| \le p,
  3. xykzLxy^{k}z \in L for all k0k \ge 0.

Proof that L={aibji>j0}L = \{a^{i} b^{j} \mid i > j \ge 0\} is not regular

Assume, for contradiction, that LL is regular with pumping length pp.

Choose w=ap+1bpw = a^{p+1} b^{p}. Then i=p+1>j=pi = p+1 > j = p, so wLw \in L and wp|w| \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 a's, both xx and yy lie within the aa-block, so y=amy = a^{m} for some m1m \ge 1.

Now pump down with k=0k = 0: xy0z=a(p+1)mbpxy^{0}z = a^{(p+1) - m} b^{p}.

The number of a's is (p+1)m(p+1)1=p(p+1) - m \le (p+1) - 1 = p, while the number of b's is still pp. So i=p+1mp=ji = p+1-m \le p = j, which violates the requirement i>ji > j. Hence xy0zLxy^{0}z \notin L.

This contradicts the pumping lemma. Therefore LL is not regular. \blacksquare

pumping-lemmaregular-languagesnon-regularity
6short7 marks

(a) Write regular expressions over Σ={0,1}\Sigma = \{0, 1\} for: (i) all strings of even length, and (ii) all strings in which every 0 is immediately followed by at least one 1. (4)

(b) State the identities used to simplify regular expressions and use them to simplify (ϵ+0)1(ϵ+0)(\epsilon + 0)^{*} 1 (\epsilon + 0)^{*}. (3)

(a) Regular expressions (4 marks)

(i) All strings of even length over {0,1}\{0,1\} — any two symbols repeated zero or more times:

((0+1)(0+1))\big((0+1)(0+1)\big)^{*}

(The empty string, of length 0, is included.)

(ii) Every 0 is immediately followed by at least one 1. A 0 may never be the last symbol and never be directly followed by another 0. Equivalently, every 0 comes as the block 01. Such strings are made of the blocks 1 and 01 repeated:

(1+01)\big(1 + 01\big)^{*}

(b) Identities for regular expressions and simplification (3 marks)

Useful algebraic identities (with \emptyset = empty set, ϵ\epsilon = empty string):

  • r+r=rr + r = r (idempotence of ++)
  • ϵr=rϵ=r\epsilon\, r = r\, \epsilon = r (identity of concatenation)
  • (ϵ+r)=r(\epsilon + r)^{*} = r^{*}
  • rr=rr^{*} r^{*} = r^{*}, (r)=r(r^{*})^{*} = r^{*}
  • r=r=\emptyset\, r = r\, \emptyset = \emptyset,   +r=r\;\emptyset + r = r

Simplify (ϵ+0)1(ϵ+0)(\epsilon + 0)^{*}\, 1\, (\epsilon + 0)^{*}:

(ϵ+0)1(ϵ+0)=010(\epsilon + 0)^{*}\,1\,(\epsilon + 0)^{*} = 0^{*}\,1\,0^{*}

using the identity (ϵ+0)=0(\epsilon + 0)^{*} = 0^{*} on each factor. The resulting expression 0100^{*} 1 0^{*} denotes all strings over {0,1}\{0,1\} containing exactly one 1.

regular-expressionsregular-languages
7short7 marks

Differentiate between a decidable and an undecidable problem. State the Halting Problem and give an outline of the argument (proof by contradiction) showing that it is undecidable.

Decidable vs Undecidable problems

  • A decidable (recursive) problem is one for which there exists an algorithm — a Turing machine that halts on every input and answers "yes" or "no" correctly. Example: "Does DFA MM accept string ww?"
  • An undecidable problem is one for which no such always-halting algorithm exists; any TM that decides it must loop forever on at least some inputs. Undecidable problems may still be recognisable (semi-decidable) if a TM halts and accepts on "yes" instances but may loop on "no" instances.

The Halting Problem

Statement: Given (an encoding of) an arbitrary Turing machine MM and an input string ww, decide whether MM halts when run on ww. Formally, decide membership in

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

The Halting Problem is undecidable.

Proof outline (by contradiction / diagonalization):

  1. Suppose a TM HH decides HALTHALT: H(M,w)H(\langle M, w\rangle) halts and outputs "halts" if MM halts on ww, and "loops" otherwise.
  2. Construct a new machine DD that takes input M\langle M \rangle and runs HH on M,M\langle M, \langle M\rangle\rangle (i.e. asks whether MM halts on its own description):
    • 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.
  3. Now run DD on its own description D\langle D \rangle:
    • If DD halts on D\langle D\rangle, then by construction DD loops on D\langle D\rangle — contradiction.
    • If DD loops on D\langle D\rangle, then by construction DD halts on D\langle D\rangle — contradiction.
  4. Either way we reach a contradiction, so the assumed decider HH cannot exist.

Therefore the Halting Problem is undecidable. \blacksquare

decidabilitydecidable-problemsturing-machine
8short7 marks

(a) Define the complexity classes P, NP, and NP-Complete, and explain the significance of the P=?NPP \stackrel{?}{=} NP question. (4)

(b) What is a polynomial-time reduction? Explain its role in proving that a problem is NP-complete. (3)

(a) P, NP, NP-Complete and the P =? NP question (4 marks)

  • P (Polynomial time): the class of decision problems solvable by a deterministic Turing machine in time polynomial in the input size, O(nk)O(n^{k}). These are the "efficiently solvable" problems (e.g. sorting, shortest path, primality).
  • NP (Nondeterministic Polynomial time): the class of decision problems for which a proposed solution ("certificate") can be verified by a deterministic TM in polynomial time — equivalently, solvable by a nondeterministic TM in polynomial time. PNPP \subseteq NP.
  • NP-Complete: a problem XX is NP-complete if (i) XNPX \in NP, and (ii) every problem in NP reduces to XX in polynomial time (XX is NP-hard). These are the "hardest" problems in NP; SAT was the first proved NP-complete (Cook–Levin theorem).

Significance of P=?NPP \stackrel{?}{=} NP: It asks whether every problem whose solution can be verified quickly can also be solved quickly. If P=NPP = NP, every NP-complete problem (SAT, TSP, etc.) would have an efficient algorithm, with huge consequences (e.g. cryptography would break). It is one of the major open problems in computer science; it is widely conjectured that PNPP \ne NP.

(b) Polynomial-time reduction and its role (3 marks)

A polynomial-time (many-one) reduction from problem AA to problem BB, written ApBA \le_{p} B, is a function ff computable in polynomial time such that for every input xx:

xA    f(x)B.x \in A \iff f(x) \in B.

It transforms instances of AA into instances of BB while preserving yes/no answers, in polynomial time.

Role in NP-completeness proofs: To prove a new problem BB is NP-complete we (1) show BNPB \in NP, and (2) pick a known NP-complete problem AA and show ApBA \le_{p} B. Because every problem in NP already reduces to AA, and reductions compose, this makes BB NP-hard; together with BNPB \in NP, BB is NP-complete. The reduction "transfers hardness": if BB had a polynomial algorithm, so would AA and hence all of NP.

computational-complexityp-npnp-completeness
9short7 marks

Differentiate between a Moore machine and a Mealy machine. Design a Mealy machine over Σ={0,1}\Sigma = \{0, 1\} that outputs 1 whenever the input sequence ends in the substring 101, and 0 otherwise.

Moore vs Mealy machine

AspectMoore machineMealy machine
Output depends onState onlyState and current input
Output functionλ:QO\lambda : Q \to Oλ:Q×ΣO\lambda : Q \times \Sigma \to O
Output associated withstates (nodes)transitions (edges)
Output for input of length nnn+1n+1 symbols (includes initial-state output)nn symbols
Reaction speedone cycle delayimmediate (same cycle)
Typicallyneeds more statesfewer states

Both are finite-state transducers and are equivalent in expressive power (each can be converted to the other).

Mealy machine that outputs 1 when input ends in 101

We track how much of the suffix 101 has been seen. Output is on each transition (1 only when the move completes 101).

States:

  • S0S_0 — no useful suffix (start)
  • S1S_1 — last symbol matched 1 (prefix 1 of 101)
  • S2S_2 — last symbols matched 10 (prefix 10)

Transition / output table (next state, output):

Stateinput 0input 1
S0S_0S0S_0, 0S1S_1, 0
S1S_1S2S_2, 0S1S_1, 0
S2S_2S0S_0, 0S1S_1, 1

Explanation of key edges:

  • From S2S_2 (10 seen) on input 1 we complete 101 → output 1, and the trailing 1 is itself a fresh prefix, so we go to S1S_1.
  • On any 1 we move to S1S_1 (a 1 always starts a new potential match), except the accepting edge above which also lands in S1S_1.
  • On 0 from S1S_1 we have 10 → go to S2S_2; on 0 from S2S_2 the partial match breaks → S0S_0.

Trace on 1101: S01/0S11/0S10/0S21/1S1S_0 \xrightarrow{1/0} S_1 \xrightarrow{1/0} S_1 \xrightarrow{0/0} S_2 \xrightarrow{1/1} S_1. Output stream 0001 — the final 1 correctly signals that the input ends in 101.

finite-automatamoore-mealysequential-machines
10short7 marks

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.

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 trees (equivalently, two distinct leftmost derivations, or two distinct rightmost derivations). Ambiguity is a property of the grammar, not of the language.

Showing EE+EEE(E)idE \to E + E \mid E * E \mid (E) \mid id is ambiguous

The string id + id * id has two different parse trees because the grammar does not fix the precedence of + and *.

Parse tree 1 — interprets as id+(idid)id + (id * id) (multiplication grouped first):

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

Leftmost derivation: EE+Eid+Eid+EEid+idEid+ididE \Rightarrow E + E \Rightarrow id + E \Rightarrow id + E * E \Rightarrow id + id * E \Rightarrow id + id * id.

Parse tree 2 — interprets as (id+id)id(id + id) * id (addition grouped first):

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

Leftmost derivation: EEEE+EEid+EEid+idEid+ididE \Rightarrow E * E \Rightarrow E + E * E \Rightarrow id + E * E \Rightarrow id + id * E \Rightarrow id + id * id.

Both trees derive the same terminal string id + id * id but have different structures (root operator + vs *). Since one string has two distinct parse trees, the grammar is ambiguous. \blacksquare

(This is usually fixed by introducing precedence/associativity levels: EE+TT,  TTFF,  F(E)idE \to E + T \mid T,\; T \to T * F \mid F,\; F \to (E) \mid id.)

context-free-grammarambiguityparse-tree
11short7 marks

(a) State and briefly justify any three closure properties of regular languages (e.g. under union, concatenation, and complementation). (4)

(b) Define the terms recursive language and recursively enumerable language, and state the relationship between them. (3)

(a) Three closure properties of regular languages (4 marks)

Let L1,L2L_1, L_2 be regular languages over Σ\Sigma.

  1. Union: L1L2L_1 \cup L_2 is regular. Justification: given NFAs (or regular expressions) r1,r2r_1, r_2 for them, r1+r2r_1 + r_2 describes the union; equivalently a new start state with ϵ\epsilon-moves to both NFAs accepts L1L2L_1 \cup L_2.
  2. Concatenation: L1L2L_1 L_2 is regular. Justification: the regular expression r1r2r_1 r_2 denotes it; or link the final states of the first NFA by ϵ\epsilon-moves to the start of the second.
  3. Complementation: L1=ΣL1\overline{L_1} = \Sigma^{*} \setminus L_1 is regular. Justification: take a complete DFA for L1L_1 and swap accepting and non-accepting states; the resulting DFA accepts exactly the strings L1L_1 rejects.

(Other closures: intersection, Kleene star, reversal, difference — all regular.)

(b) Recursive and recursively enumerable languages (3 marks)

  • A language LL is recursive (decidable) if there is a Turing machine that halts on every input and accepts exactly the strings in LL (it answers yes/no for all inputs and always terminates).
  • A language LL is recursively enumerable (RE / Turing-recognisable) if there is a Turing machine that accepts (halts on) every string in LL, but on strings not in LL it may either reject or loop forever.

Relationship: Every recursive language is recursively enumerable, i.e. Recursive \subsetneq RE. The containment is strict: there exist RE languages that are not recursive (e.g. the language of the Halting Problem). Also, LL is recursive iff both LL and its complement L\overline{L} are recursively enumerable.

regular-languagesclosure-propertiesset-theory
12short7 marks

(a) State the Church-Turing thesis and explain its significance in the theory of computation. (4)

(b) Briefly explain how a multi-tape Turing machine is equivalent in computational power to a single-tape Turing machine. (3)

(a) Church–Turing Thesis (4 marks)

Statement: Every function that is effectively computable (computable by any well-defined mechanical/algorithmic procedure) can be computed by a Turing machine. In other words, the intuitive notion of "algorithm" coincides exactly with what Turing machines can compute.

It is a thesis (hypothesis), not a theorem, because "effectively computable" is an informal notion that cannot be proved equal to a formal model. Its strong support comes from the fact that every reasonable model of computation proposed — λ\lambda-calculus, recursive functions, RAM machines, while-programs, modern programming languages — has been shown equivalent in power to the Turing machine.

Significance:

  • It justifies using the Turing machine as the standard definition of computability and algorithm.
  • It lets us prove a problem is unsolvable by any algorithm by showing no Turing machine solves it (e.g. the Halting Problem).
  • It underpins the whole theory of decidability and the classification of problems.

(b) Multi-tape TM equivalence to single-tape TM (3 marks)

A multi-tape TM has kk tapes each with its own independent head; one step reads/writes/moves all kk heads. It is clearly at least as powerful as a single-tape TM.

Single-tape simulation: A single-tape TM SS can simulate a kk-tape TM MM by storing the contents of all kk tapes on its one tape, separated by a delimiter #\#, and marking each tape's head position with a special "dotted" symbol:

# t˙1# t˙2# t˙k#\#\ \dot t_1 \ldots \#\ \dot t_2 \ldots \#\ \dot t_k \ldots \#

To simulate one step of MM, SS sweeps left-to-right over the whole tape to read the kk marked symbols, then sweeps again to write the new symbols and shift the dot markers, expanding the tape if a head runs off its region.

Conclusion: SS recognises exactly the same language as MM, so multi-tape and single-tape TMs are equivalent in computational power (they recognise the same class — the recursively enumerable languages). The only cost is efficiency: tt steps of MM take O(t2)O(t^{2}) steps on SS.

turing-machinechurch-turing-thesisvariants

Frequently asked questions

Where can I find the BE Computer Engineering (Pokhara University) Theory of Computation (PU, CMP 264) question paper 2078?
The full BE Computer Engineering (Pokhara University) Theory of Computation (PU, CMP 264) 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 (PU, CMP 264) 2078 paper come with solutions?
Yes. Every question on this Theory of Computation (PU, CMP 264) 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 (Pokhara University) Theory of Computation (PU, CMP 264) 2078 paper?
The BE Computer Engineering (Pokhara University) Theory of Computation (PU, CMP 264) 2078 paper carries 100 full marks and is meant to be completed in 180 minutes, across 12 questions.
Is practising this Theory of Computation (PU, CMP 264) past paper free?
Yes — reading and attempting this Theory of Computation (PU, CMP 264) past paper on Kekkei is completely free.