Skip to main content

Chapter 5

Theory of Computation

TU BSc CSIT past exam questions

Past questions and answers

69 questions set from this chapter. Most repeated first.

  • 2076 · 10 marks

Define the NFA with ε-transition and ε-closure of a state. Show that for every regular expression r, representing a language L, there is ε-NFA accepting the same language. Also convert regular expression (a+b)ab into equivalent Finite Automata.

Answer

ε-NFA. An NFA with ε-transitions is a 5-tuple M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) where QQ is a finite set of states, Σ\Sigma the input alphabet, q0∈Qq_0\in Q the start state, F⊆QF\subseteq Q the final states and

δ:Q×(Σ∪{ε})→2Q\delta : Q\times(\Sigma\cup\{\varepsilon\})\to 2^{Q}

so the machine may change state without reading input.

ε-closure. ε-closure(q)\varepsilon\text{-closure}(q) is the set of all states reachable from qq using zero or more ε-transitions: qq itself is in it, and if pp is in it and r∈δ(p,ε)r\in\delta(p,\varepsilon) then rr is in it. For a set SS, ε-closure(S)=⋃q∈Sε-closure(q)\varepsilon\text{-closure}(S)=\bigcup_{q\in S}\varepsilon\text{-closure}(q).

Theorem. For every regular expression rr there is an ε-NFA MM with L(M)=L(r)L(M)=L(r).

Proof (induction on the number of operators in rr). We construct MM with exactly one final state, no transition into the start state and no transition out of the final state.

Basis.

  • r=εr=\varepsilon: two states q0→εqfq_0\xrightarrow{\varepsilon}q_f.
  • r=∅r=\varnothing: two states q0,qfq_0,q_f and no transitions.
  • r=ar=a (a∈Σa\in\Sigma): q0→aqfq_0\xrightarrow{a}q_f.

Induction. Let r1,r2r_1,r_2 have ε-NFAs M1=( ⋅ ,q1,f1)M_1=(\,\cdot\,,q_1,f_1) and M2=( ⋅ ,q2,f2)M_2=(\,\cdot\,,q_2,f_2).

  • r=r1+r2r=r_1+r_2: add a new start q0q_0 and new final ff with q0→εq1q_0\xrightarrow{\varepsilon}q_1, q0→εq2q_0\xrightarrow{\varepsilon}q_2, f1→εff_1\xrightarrow{\varepsilon}f, f2→εff_2\xrightarrow{\varepsilon}f. A string is accepted iff it is accepted by M1M_1 or M2M_2.
  • r=r1r2r=r_1r_2: add f1→εq2f_1\xrightarrow{\varepsilon}q_2; start is q1q_1, final is f2f_2. A path goes through M1M_1 (reading a string of L(r1)L(r_1)) and then M2M_2 (a string of L(r2)L(r_2)).
  • r=r1∗r=r_1^{*}: add new start q0q_0 and final ff with q0→εq1q_0\xrightarrow{\varepsilon}q_1, q0→εfq_0\xrightarrow{\varepsilon}f, f1→εq1f_1\xrightarrow{\varepsilon}q_1, f1→εff_1\xrightarrow{\varepsilon}f. This accepts ε\varepsilon and any number of repetitions of strings of L(r1)L(r_1).
  • r=(r1)r=(r_1): same automaton as r1r_1.

Each step preserves the three properties and the language, so every regular expression has an equivalent ε-NFA. ■\blacksquare

Conversion of (a+b)∗ab∗(a+b)^*ab^*. Using the construction:

Stateaabbε\varepsilon
0 (start)--1, 7
1--2, 4
23--
3--6
4-5-
5--6
6--1, 7
7--8
89--
9--10
10--11, 13
11-12-
12--11, 13
13 (final)---

States 0-7 realise (a+b)∗(a+b)^* (union of 2-3 and 4-5 with loop 6 to 1), 8-9 realise aa, and 10-13 realise b∗b^*.

Removing ε-moves and merging gives the compact equivalent automaton. The language is the set of strings that contain at least one aa (take the last aa; everything after it is b∗b^*):

Stateaabb
→q0\to q_0q1q_1q0q_0
∗q1*q_1q1q_1q1q_1

Here q1q_1 is accepting: after the first aa any continuation, being in (a+b)∗ab∗(a+b)^*ab^* form, is accepted.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 10 marks

How can you define the language accepted by a PDA? Explain how a PDA accepting language by empty stack is converted into an equivalent PDA accepting by final state and vice-versa.

Answer

Language accepted by a PDA. Let P=(Q,Σ,Γ,δ,q0,Z0,F)P=(Q,\Sigma,\Gamma,\delta,q_0,Z_0,F) be a PDA. An instantaneous description (ID) is (q,w,γ)(q,w,\gamma): state, remaining input, stack (top on the left). Moves are written ⊢\vdash and ⊢∗\vdash^{*} for zero or more moves. There are two equivalent definitions.

  1. Acceptance by final state: L(P)={ w:(q0,w,Z0)⊢∗(q,ε,γ), q∈F, γ∈Γ∗}L(P)=\{\,w : (q_0,w,Z_0)\vdash^{*}(q,\varepsilon,\gamma),\ q\in F,\ \gamma\in\Gamma^{*}\} The input is consumed and the PDA is in an accepting state; the stack content is irrelevant.

  2. Acceptance by empty stack: N(P)={ w:(q0,w,Z0)⊢∗(q,ε,ε), q∈Q}N(P)=\{\,w : (q_0,w,Z_0)\vdash^{*}(q,\varepsilon,\varepsilon),\ q\in Q\} The input is consumed and the stack is empty; the final-state set is irrelevant.

The two classes of languages are the same (the context-free languages), as shown by the following conversions.

Empty stack to final state. Given PN=(Q,Σ,Γ,δN,q0,Z0)P_N=(Q,\Sigma,\Gamma,\delta_N,q_0,Z_0) with L=N(PN)L=N(P_N), build

PF=(Q∪{p0,pf}, Σ, Γ∪{X0}, δF, p0, X0, {pf})P_F=(Q\cup\{p_0,p_f\},\ \Sigma,\ \Gamma\cup\{X_0\},\ \delta_F,\ p_0,\ X_0,\ \{p_f\})

  • δF(p0,ε,X0)={(q0,Z0X0)}\delta_F(p_0,\varepsilon,X_0)=\{(q_0,Z_0X_0)\}: push the original start symbol above a new bottom marker X0X_0 and start PNP_N.
  • All moves of δN\delta_N are kept in δF\delta_F.
  • For every q∈Qq\in Q: δF(q,ε,X0)∋(pf,ε)\delta_F(q,\varepsilon,X_0)\ni(p_f,\varepsilon).

If PNP_N empties its stack on ww, it exposes X0X_0 and PFP_F jumps to pfp_f and accepts. X0X_0 prevents PFP_F from getting stuck by popping the stack empty prematurely. So L(PF)=N(PN)L(P_F)=N(P_N).

Final state to empty stack. Given PF=(Q,Σ,Γ,δF,q0,Z0,F)P_F=(Q,\Sigma,\Gamma,\delta_F,q_0,Z_0,F) with L=L(PF)L=L(P_F), build

PN=(Q∪{p0,p}, Σ, Γ∪{X0}, δN, p0, X0)P_N=(Q\cup\{p_0,p\},\ \Sigma,\ \Gamma\cup\{X_0\},\ \delta_N,\ p_0,\ X_0)

  • δN(p0,ε,X0)={(q0,Z0X0)}\delta_N(p_0,\varepsilon,X_0)=\{(q_0,Z_0X_0)\}: new bottom marker X0X_0 guards against PFP_F emptying its stack without accepting.
  • All moves of δF\delta_F are kept.
  • For every q∈Fq\in F and every stack symbol Y∈Γ∪{X0}Y\in\Gamma\cup\{X_0\}: δN(q,ε,Y)∋(p,ε)\delta_N(q,\varepsilon,Y)\ni(p,\varepsilon).
  • For every Y∈Γ∪{X0}Y\in\Gamma\cup\{X_0\}: δN(p,ε,Y)={(p,ε)}\delta_N(p,\varepsilon,Y)=\{(p,\varepsilon)\}.

When PFP_F reaches a final state after reading ww, PNP_N moves to pp and pops every symbol, including X0X_0, ending with an empty stack. If PFP_F does not accept, PNP_N never enters pp, and the stack never becomes empty because X0X_0 stays at the bottom. So N(PN)=L(PF)N(P_N)=L(P_F).

Hence languages accepted by final state and by empty stack coincide.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 10 marks

Define a Turing machine. Construct a TM that accept L = {wcw^R | w∈(0, 1) and c is ε or 0 or 1. Show that string 0110 is accepted by this TM with sequence of Instantaneous Description (ID).

Answer

Turing machine. A Turing machine is a 7-tuple

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

where QQ is a finite set of states, Σ\Sigma the input alphabet, Γ⊇Σ\Gamma\supseteq\Sigma the tape alphabet, B∈Γ∖ΣB\in\Gamma\setminus\Sigma the blank, q0q_0 the start state, F⊆QF\subseteq Q the accepting states and

δ:Q×Γ→Q×Γ×{L,R}\delta:Q\times\Gamma\to Q\times\Gamma\times\{L,R\}

the transition function. The machine has an infinite tape with a read/write head; in each move it reads the scanned symbol, writes a symbol, moves left or right and changes state.

Language. L={wcwR∣w∈{0,1}∗, c∈{ε,0,1}}L=\{wcw^R \mid w\in\{0,1\}^*,\ c\in\{\varepsilon,0,1\}\} is the set of all palindromes over {0,1}\{0,1\} (even length when c=εc=\varepsilon, odd length otherwise).

Idea. Repeatedly erase the first symbol (remembering it), run to the right end, compare it with the last symbol, erase that, and return to the left. If all symbols are erased (or one is left in the middle), accept.

M=({q0,q1,q2,q3,q4,q5,qf},{0,1},{0,1,B},δ,q0,B,{qf})M=(\{q_0,q_1,q_2,q_3,q_4,q_5,q_f\},\{0,1\},\{0,1,B\},\delta,q_0,B,\{q_f\})

State01B
q0q_0 (start)(q1,B,R)(q_1,B,R)(q2,B,R)(q_2,B,R)(qf,B,R)(q_f,B,R)
q1q_1 (carry 0, go right)(q1,0,R)(q_1,0,R)(q1,1,R)(q_1,1,R)(q3,B,L)(q_3,B,L)
q2q_2 (carry 1, go right)(q2,0,R)(q_2,0,R)(q2,1,R)(q_2,1,R)(q4,B,L)(q_4,B,L)
q3q_3 (last must be 0)(q5,B,L)(q_5,B,L)-(qf,B,R)(q_f,B,R)
q4q_4 (last must be 1)-(q5,B,L)(q_5,B,L)(qf,B,R)(q_f,B,R)
q5q_5 (return left)(q5,0,L)(q_5,0,L)(q5,1,L)(q_5,1,L)(q0,B,R)(q_0,B,R)

A missing entry means the machine halts without accepting. If q3q_3 or q4q_4 sees BB, the only remaining symbol was the one just erased (odd palindrome), so the string is accepted. If q0q_0 sees BB, everything was matched (even palindrome or empty string).

Acceptance of 0110. An ID is written α q β\alpha\,q\,\beta with the head on the first symbol of β\beta.

q0 0110⊢B q1 110⊢B1 q1 10⊢B11 q1 0⊢B110 q1 B⊢B11 q3 0⊢B1 q5 1B⊢B q5 11B⊢q5 B11B⊢B q0 11B⊢BB q2 1B⊢BB1 q2 B⊢BB q4 1⊢B q5 BB⊢BB q0 B⊢BBB qf\begin{aligned} &q_0\,0110 \vdash B\,q_1\,110 \vdash B1\,q_1\,10 \vdash B11\,q_1\,0 \vdash B110\,q_1\,B\\ &\vdash B11\,q_3\,0 \vdash B1\,q_5\,1B \vdash B\,q_5\,11B \vdash q_5\,B11B \vdash B\,q_0\,11B\\ &\vdash BB\,q_2\,1B \vdash BB1\,q_2\,B \vdash BB\,q_4\,1 \vdash B\,q_5\,BB \vdash BB\,q_0\,B \vdash BBB\,q_f \end{aligned}

The machine reaches the accepting state qfq_f, so 01100110 is accepted.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Give the formal definition of DFA. Construct a DFA accepting all strings of {0, 1} with even number of 0’s and even number of 1’s.

Answer

Formal definition of a DFA. A deterministic finite automaton is a 5-tuple M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) where

  • QQ is a finite set of states,
  • Σ\Sigma is a finite input alphabet,
  • δ:Q×Σ→Q\delta:Q\times\Sigma\to Q is the transition function (exactly one next state for every state and symbol),
  • q0∈Qq_0\in Q is the start state,
  • F⊆QF\subseteq Q is the set of accepting (final) states.

A string ww is accepted if δ^(q0,w)∈F\hat\delta(q_0,w)\in F.

DFA for even number of 0s and even number of 1s. Each state records the parity of the 0s and 1s read so far:

  • q0q_0 = (even 0s, even 1s), start and final
  • q1q_1 = (odd 0s, even 1s)
  • q2q_2 = (even 0s, odd 1s)
  • q3q_3 = (odd 0s, odd 1s)

M=({q0,q1,q2,q3},{0,1},δ,q0,{q0})M=(\{q_0,q_1,q_2,q_3\},\{0,1\},\delta,q_0,\{q_0\})

State01
→∗q0\to *q_0q1q_1q2q_2
q1q_1q0q_0q3q_3
q2q_2q3q_3q0q_0
q3q_3q2q_2q1q_1

Reading a 0 flips the 0-parity and reading a 1 flips the 1-parity. Check with 01100110 (two 0s, two 1s): q0→0q1→1q3→1q1→0q0q_0\xrightarrow{0}q_1\xrightarrow{1}q_3\xrightarrow{1}q_1\xrightarrow{0}q_0, which is final, so it is accepted. The string 010010 goes q0→q1→q3→q2q_0\to q_1\to q_3\to q_2, which is not final, so it is rejected.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Define Chomsky Normal Form and Greibach Normal Form in reference to CFG. Give a suitable example of each.

Answer

Chomsky Normal Form (CNF). A context-free grammar is in CNF if every production has one of the forms

A→BCorA→aA\to BC \quad\text{or}\quad A\to a

where A,B,CA,B,C are variables (B,CB,C not the start symbol) and aa is a terminal. (The rule S→εS\to\varepsilon is allowed only if ε∈L\varepsilon\in L and SS does not appear on any right side.) Every CFG without ε\varepsilon can be converted to CNF by removing ε\varepsilon-productions, unit productions and useless symbols, then replacing terminals inside long right sides by new variables and breaking long right sides into pairs of variables. Derivation trees of CNF grammars are binary trees, and a string of length nn is derived in exactly 2n−12n-1 steps.

Example. The grammar S→aSb∣abS\to aSb\mid ab becomes

S→AC∣AB,C→SB,A→a,B→bS\to AC\mid AB,\quad C\to SB,\quad A\to a,\quad B\to b

Greibach Normal Form (GNF). A CFG is in GNF if every production has the form

A→aαA\to a\alpha

where aa is a terminal and α\alpha is a (possibly empty) string of variables. Every derivation step therefore produces exactly one terminal at the left end, so a string of length nn is derived in nn steps, and the form is useful for building PDAs without ε-moves and for proving that left recursion can be removed.

Example. The same grammar in GNF:

S→aSB∣aB,B→bS\to aSB\mid aB,\quad B\to b

Derivation of aabbaabb: S⇒aSB⇒aaBB⇒aabB⇒aabbS\Rightarrow aSB\Rightarrow aaBB\Rightarrow aabB\Rightarrow aabb.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Give the regular expressions for following language over alphabet {0, 1}.
  • Set of all strings with 2^nd symbol from right is 1.
  • Set of all strings starting with 00 or 11 and ending with 10 or 01.

Answer

(a) Strings over {0,1}\{0,1\} whose 2nd symbol from the right is 1.

The string must end with 1 followed by exactly one more symbol (0 or 1); anything may precede:

(0+1)∗ 1 (0+1)(0+1)^*\,1\,(0+1)

Examples accepted: 1010, 1111, 01100110, 00110011. Rejected: 00, 0101, 100100.

(b) Strings starting with 00 or 11 and ending with 10 or 01.

For strings of length at least 4 the start and end parts do not overlap, so

(00+11) (0+1)∗ (10+01)(00+11)\,(0+1)^*\,(10+01)

generates them (e.g. 00100010, 11011101, 0011000110). The start and end can overlap in the middle symbol for length 3: 001001 (starts with 00, ends with 01) and 110110 (starts with 11, ends with 10) satisfy the condition but are not generated above, and no shorter string works. The complete expression is therefore

(00+11)(0+1)∗(10+01)  +  001  +  110(00+11)(0+1)^*(10+01)\;+\;001\;+\;110

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Show that language L={0^m1^m | m>=1} is not a regular language.

Answer

Claim. L={0m1m∣m≥1}L=\{0^m1^m\mid m\ge1\} is not regular.

Proof by the pumping lemma. Pumping lemma: if LL is regular there is a constant n≥1n\ge1 such that every w∈Lw\in L with ∣w∣≥n|w|\ge n can be written w=xyzw=xyz with (i) ∣xy∣≤n|xy|\le n, (ii) ∣y∣≥1|y|\ge1, (iii) xyiz∈Lxy^iz\in L for all i≥0i\ge0.

Assume LL is regular and let nn be the pumping length. Choose

w=0n1n∈L,∣w∣=2n≥n.w=0^n1^n\in L,\qquad |w|=2n\ge n.

Since ∣xy∣≤n|xy|\le n, the substring xyxy lies inside the leading block of 0s. So

x=0a,y=0b (b≥1),z=0 n−a−b1n.x=0^{a},\quad y=0^{b}\ (b\ge1),\quad z=0^{\,n-a-b}1^n.

Pump with i=2i=2:

xy2z=0a 02b 0n−a−b1n=0 n+b1n.xy^2z=0^{a}\,0^{2b}\,0^{n-a-b}1^n=0^{\,n+b}1^n.

Because b≥1b\ge1, the number of 0s (n+bn+b) is greater than the number of 1s (nn), so xy2z∉Lxy^2z\notin L. This contradicts condition (iii). Hence the assumption is false and LL is not regular. ■\blacksquare

(Intuitively, a finite automaton has only finitely many states and cannot remember the unbounded count of 0s needed to match the 1s.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Describe the Turing machines with multiple tape, multiple track and storage in state.

Answer

These are variants of the basic Turing machine; none of them is more powerful than the standard single-tape TM, they only make programming easier.

1. Multiple-tape Turing machine. The machine has kk tapes, each with its own independent read/write head. The finite control reads the kk scanned symbols together, and in one move it changes state, writes a new symbol on each tape and moves each head left, right or keeps it still:

δ:Q×Γk→Q×Γk×{L,R,S}k\delta:Q\times\Gamma^k\to Q\times\Gamma^k\times\{L,R,S\}^k

Input is placed on tape 1; the other tapes start blank. It is convenient, e.g. for {wcwR}\{wcw^R\} or copying: copy the input to tape 2 and compare in reverse. Equivalence: a one-tape TM simulates a kk-tape machine by storing all tapes on one tape, separated by markers, and marking head positions; each move of the multitape machine takes O(n)O(n) steps, so a computation of T(n)T(n) steps is simulated in O(T(n)2)O(T(n)^2) time. Hence the same languages are accepted.

2. Multiple-track Turing machine. There is one tape and one head, but the tape is divided into kk tracks, so each cell holds a kk-tuple of symbols [a1,a2,…,ak][a_1,a_2,\dots,a_k] that is read and written as a single symbol. The tape alphabet is Γ=Γ1×⋯×Γk\Gamma=\Gamma_1\times\dots\times\Gamma_k. A typical use is marking: the first track holds the input and the second track holds check marks (e.g. primality or divisibility testing, or binary addition with operands on different tracks). It is only a notational convenience, as a standard TM with tape alphabet of tuples is the same.

3. Storage in the finite control (state). The state is made a tuple [q,d1,…,dm][q,d_1,\dots,d_m] where qq is the control state and did_i are stored data items (symbols remembered). Since both QQ and the data are finite, the number of such states is still finite. Example: to test {0n1n}\{0^n1^n\} or the palindrome machine, the first symbol is remembered in the state as [q1,0][q_1,0] while the head travels to the other end, and then compared. The machine is still an ordinary TM with a larger set of states.

All three models accept exactly the recursively enumerable languages.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Construct a NFA accepting language of {0, 1} with each string ending with 01 and convert it into equivalent DFA.

Answer

NFA for strings ending with 01 over {0,1}\{0,1\}.

N=({q0,q1,q2},{0,1},δ,q0,{q2})N=(\{q_0,q_1,q_2\},\{0,1\},\delta,q_0,\{q_2\})

State01
→q0\to q_0{q0,q1}\{q_0,q_1\}{q0}\{q_0\}
q1q_1∅\varnothing{q2}\{q_2\}
∗q2*q_2∅\varnothing∅\varnothing

The NFA loops in q0q_0 while reading anything, and guesses the last two symbols: 0 moves to q1q_1, then 1 moves to q2q_2.

Conversion to DFA (subset construction). Start with {q0}\{q_0\}.

  • {q0}\{q_0\}: on 0 →{q0,q1}\to\{q_0,q_1\}; on 1 →{q0}\to\{q_0\}.
  • {q0,q1}\{q_0,q_1\}: on 0 →{q0,q1}∪∅={q0,q1}\to\{q_0,q_1\}\cup\varnothing=\{q_0,q_1\}; on 1 →{q0}∪{q2}={q0,q2}\to\{q_0\}\cup\{q_2\}=\{q_0,q_2\}.
  • {q0,q2}\{q_0,q_2\}: on 0 →{q0,q1}\to\{q_0,q_1\}; on 1 →{q0}\to\{q_0\}.

No new subsets appear. Final states are those containing q2q_2: {q0,q2}\{q_0,q_2\}. Renaming A={q0}A=\{q_0\}, B={q0,q1}B=\{q_0,q_1\}, C={q0,q2}C=\{q_0,q_2\}:

State01
→A\to ABBAA
BBBBCC
∗C*CBBAA

Check: 11011101: A→A→A→B→CA\to A\to A\to B\to C accepted; 01100110: A→B→C→A→BA\to B\to C\to A\to B rejected.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Construct a PDA accepting language over {0, 1} representing strings with equal no of 0s and1s. Show by sequence of IDs that 0101 is accepted by this PDA.

Answer

PDA for strings over {0,1}\{0,1\} with equal numbers of 0s and 1s.

Idea: the stack holds the surplus symbol. If the top is the same as the input symbol (or the stack holds only Z0Z_0), push; if it is the opposite symbol, pop (cancel one pair). When the input is finished and only Z0Z_0 remains, the counts are equal.

P=({q0,qf},{0,1},{0,1,Z0},δ,q0,Z0,{qf})P=(\{q_0,q_f\},\{0,1\},\{0,1,Z_0\},\delta,q_0,Z_0,\{q_f\})

  1. δ(q0,0,Z0)={(q0,0Z0)}\delta(q_0,0,Z_0)=\{(q_0,0Z_0)\}
  2. δ(q0,1,Z0)={(q0,1Z0)}\delta(q_0,1,Z_0)=\{(q_0,1Z_0)\}
  3. δ(q0,0,0)={(q0,00)}\delta(q_0,0,0)=\{(q_0,00)\}
  4. δ(q0,1,1)={(q0,11)}\delta(q_0,1,1)=\{(q_0,11)\}
  5. δ(q0,0,1)={(q0,ε)}\delta(q_0,0,1)=\{(q_0,\varepsilon)\}
  6. δ(q0,1,0)={(q0,ε)}\delta(q_0,1,0)=\{(q_0,\varepsilon)\}
  7. δ(q0,ε,Z0)={(qf,Z0)}\delta(q_0,\varepsilon,Z_0)=\{(q_f,Z_0)\}

(Acceptance is by final state qfq_f; equivalently, rule 7 could pop Z0Z_0 to accept by empty stack.)

Acceptance of 0101 (top of stack on the left):

(q0,0101,Z0)⊢(q0,101,0Z0)rule 1⊢(q0,01,Z0)rule 6⊢(q0,1,0Z0)rule 1⊢(q0,ε,Z0)rule 6⊢(qf,ε,Z0)rule 7\begin{aligned} (q_0,0101,Z_0)&\vdash(q_0,101,0Z_0) &&\text{rule 1}\\ &\vdash(q_0,01,Z_0) &&\text{rule 6}\\ &\vdash(q_0,1,0Z_0) &&\text{rule 1}\\ &\vdash(q_0,\varepsilon,Z_0) &&\text{rule 6}\\ &\vdash(q_f,\varepsilon,Z_0) &&\text{rule 7} \end{aligned}

The input is consumed and the PDA is in the final state qfq_f, so 01010101 is accepted.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

Define complexity of a Turing machine. Explain about big Oh, big Omega and big Theta notation used for complexity measurement.

Answer

Complexity of a Turing machine. The complexity of a TM MM measures the resources it uses on an input of length nn.

  • Time complexity T(n)T(n): the maximum number of moves MM makes on any input of length nn before halting. MM is said to be of time complexity T(n)T(n) (language in DTIME(T(n))\text{DTIME}(T(n))).
  • Space complexity S(n)S(n): the maximum number of tape cells scanned on any input of length nn.

For a nondeterministic TM the maximum is taken over all branches of the computation tree. Complexity is described for large nn using asymptotic notation.

Big Oh (upper bound). f(n)=O(g(n))f(n)=O(g(n)) if there are constants c>0c>0 and n0n_0 such that

f(n)≤c g(n)for all n≥n0.f(n)\le c\,g(n)\quad\text{for all } n\ge n_0.

It gives an upper bound (worst-case growth). Example: 3n2+5n+2=O(n2)3n^2+5n+2=O(n^2) (take c=4c=4 for n≥6n\ge 6).

Big Omega (lower bound). f(n)=Ω(g(n))f(n)=\Omega(g(n)) if there are constants c>0c>0 and n0n_0 such that

f(n)≥c g(n)for all n≥n0.f(n)\ge c\,g(n)\quad\text{for all } n\ge n_0.

It gives a lower bound. Example: 3n2+5n+2=Ω(n2)3n^2+5n+2=\Omega(n^2) with c=3c=3.

Big Theta (tight bound). f(n)=Θ(g(n))f(n)=\Theta(g(n)) if there are constants c1,c2>0c_1,c_2>0 and n0n_0 such that

c1 g(n)≤f(n)≤c2 g(n)for all n≥n0,c_1\,g(n)\le f(n)\le c_2\,g(n)\quad\text{for all } n\ge n_0,

that is, f=O(g)f=O(g) and f=Ω(g)f=\Omega(g). Example: 3n2+5n+2=Θ(n2)3n^2+5n+2=\Theta(n^2).

Example for a TM: a single-tape machine recognising {0k1k}\{0^k1^k\} by repeatedly crossing off a 0 and a 1 takes about n2/2n^2/2 steps, so T(n)=Θ(n2)T(n)=\Theta(n^2).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2076 · 5 marks

What do you mean by tractable and Intractable problems? Explain with reference to TM.

Answer

Tractable problems. A problem is tractable if it can be solved by a deterministic Turing machine in polynomial time, i.e. in O(nk)O(n^k) steps for some constant kk on inputs of length nn. These problems form the class P:

P=⋃k≥1DTIME(nk)P=\bigcup_{k\ge1}\text{DTIME}(n^k)

Examples: sorting, shortest path, searching, matrix multiplication, testing whether a string is accepted by a DFA. A polynomial time algorithm stays practical as nn grows, and the class is robust: it does not change between reasonable models (multitape TM, RAM), because simulation costs only a polynomial factor.

Intractable problems. A problem is intractable if no polynomial-time algorithm is known, or can exist, so that every algorithm needs super-polynomial (typically exponential) time such as 2n2^n or n!n!. Even for moderate nn (say n=100n=100) these are unusable, since 21002^{100} steps is far beyond any computer.

  • Problems solvable in polynomial time by a nondeterministic TM form NP: NP=⋃kNTIME(nk)NP=\bigcup_k \text{NTIME}(n^k). A solution can be verified in polynomial time. Clearly P⊆NPP\subseteq NP; whether P=NPP=NP is open.
  • NP-complete problems are the hardest in NP: every NP problem reduces to them in polynomial time (Cook's theorem: Satisfiability, SAT). Others: Travelling Salesman (decision form), Hamiltonian circuit, Vertex cover, Clique, 0/1 knapsack. If any NP-complete problem is in P, then P=NPP=NP.
  • Some problems are provably intractable (EXPTIME-complete), and others are undecidable (halting problem), so not solvable at all.

With reference to TM. A problem is tractable if a deterministic TM decides it in at most p(n)p(n) moves for a polynomial pp; it is treated as intractable if the best known deterministic TM needs exponential moves (a nondeterministic TM may still solve it in polynomial time by guessing the solution and verifying it). Example: a deterministic TM trying all 2n2^n assignments for SAT takes exponential time, while a nondeterministic TM guesses an assignment and checks it in O(n2)O(n^2) time.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 10 marks

Give the formal definition of DFA and NFA. How NFA can be converted into eqivalent DFA? Explain with suitable example.

Answer

DFA (formal definition). A deterministic finite automaton is a 5-tuple M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) with a finite set of states QQ, input alphabet Σ\Sigma, start state q0∈Qq_0\in Q, accepting states F⊆QF\subseteq Q and transition function

δ:Q×Σ→Q\delta:Q\times\Sigma\to Q

giving exactly one next state for every state and input symbol.

NFA (formal definition). A nondeterministic finite automaton is a 5-tuple N=(Q,Σ,δ,q0,F)N=(Q,\Sigma,\delta,q_0,F) where everything is as above except

δ:Q×Σ→2Q\delta:Q\times\Sigma\to 2^{Q}

so from a state on a symbol the machine may go to zero, one or several states. A string is accepted if at least one path ends in a state of FF.

Conversion of NFA to DFA (subset construction). Every NFA N=(QN,Σ,δN,q0,FN)N=(Q_N,\Sigma,\delta_N,q_0,F_N) has an equivalent DFA D=(QD,Σ,δD,{q0},FD)D=(Q_D,\Sigma,\delta_D,\{q_0\},F_D):

  1. QD⊆2QNQ_D\subseteq 2^{Q_N}; the start state is {q0}\{q_0\}.
  2. For a DFA state SS and symbol aa: δD(S,a)=⋃p∈SδN(p,a)\delta_D(S,a)=\bigcup_{p\in S}\delta_N(p,a).
  3. Begin with {q0}\{q_0\}, compute the transitions, and add each new subset obtained; repeat until no new subset appears.
  4. FD={S∈QD:S∩FN≠∅}F_D=\{S\in Q_D : S\cap F_N\ne\varnothing\}.

By induction on ∣w∣|w|, δ^D({q0},w)=δ^N(q0,w)\hat\delta_D(\{q_0\},w)=\hat\delta_N(q_0,w), so L(D)=L(N)L(D)=L(N). In the worst case the DFA has 2∣QN∣2^{|Q_N|} states.

Example. NFA over {0,1}\{0,1\} accepting strings whose second symbol from the right is 1.

State01
→q0\to q_0{q0}\{q_0\}{q0,q1}\{q_0,q_1\}
q1q_1{q2}\{q_2\}{q2}\{q_2\}
∗q2*q_2∅\varnothing∅\varnothing

Subset construction:

  • {q0}\{q_0\}: 0 →{q0}\to\{q_0\}, 1 →{q0,q1}\to\{q_0,q_1\}
  • {q0,q1}\{q_0,q_1\}: 0 →{q0}∪{q2}={q0,q2}\to\{q_0\}\cup\{q_2\}=\{q_0,q_2\}, 1 →{q0,q1}∪{q2}={q0,q1,q2}\to\{q_0,q_1\}\cup\{q_2\}=\{q_0,q_1,q_2\}
  • {q0,q2}\{q_0,q_2\}: 0 →{q0}∪∅={q0}\to\{q_0\}\cup\varnothing=\{q_0\}, 1 →{q0,q1}\to\{q_0,q_1\}
  • {q0,q1,q2}\{q_0,q_1,q_2\}: 0 →{q0,q2}\to\{q_0,q_2\}, 1 →{q0,q1,q2}\to\{q_0,q_1,q_2\}

Final states are the subsets containing q2q_2. Naming A={q0}A=\{q_0\}, B={q0,q1}B=\{q_0,q_1\}, C={q0,q2}C=\{q_0,q_2\}, D={q0,q1,q2}D=\{q_0,q_1,q_2\}:

State01
→A\to AAABB
BBCCDD
∗C*CAABB
∗D*DCCDD

Check: 01100110 goes A→A→B→D→CA\to A\to B\to D\to C (final), accepted; the second symbol from the right is 1. 01010101: A→A→B→C→BA\to A\to B\to C\to B, rejected, as the second symbol from the right is 0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 10 marks

Find the minimum state DFA for the given DFA below:
StatesInput
01
AB
BE
CB
*DE
EB
FB

Answer

The DFA has states A,…,FA,\dots,F, start state AA, and the only final state is DD:

State01
→A\to ABBFF
BBEECC
CCBBDD
∗D*DEEFF
EEBBCC
FFBBAA

All six states are reachable from AA (A→B,FA\to B,F; B→E,CB\to E,C; C→DC\to D), so no state is removed first. Use the partition-refinement (table-filling) method.

Step 0. Separate final from non-final: P0={A,B,C,E,F},{D}P_0=\{A,B,C,E,F\},\{D\}.

Step 1. For the non-final states look at the groups reached on 0 and 1 (group 1 = {A,B,C,E,F}\{A,B,C,E,F\}, group 2 = {D}\{D\}):

Stateon 0on 1
AA11
BB11
CC12
EE11
FF11

CC reaches DD on 1, so it is distinguished: P1={A,B,E,F},{C},{D}P_1=\{A,B,E,F\},\{C\},\{D\}.

Step 2. Groups: g1={A,B,E,F}g_1=\{A,B,E,F\}, g2={C}g_2=\{C\}, g3={D}g_3=\{D\}.

Stateon 0on 1
AAB∈g1B\in g_1F∈g1F\in g_1
BBE∈g1E\in g_1C∈g2C\in g_2
EEB∈g1B\in g_1C∈g2C\in g_2
FFB∈g1B\in g_1A∈g1A\in g_1

A,FA,F go to g1g_1 on both symbols; B,EB,E go to g1g_1 on 0 and g2g_2 on 1. So P2={A,F},{B,E},{C},{D}P_2=\{A,F\},\{B,E\},\{C\},\{D\}.

Step 3. Check {A,F}\{A,F\}: on 0 both go to BB; on 1, A→FA\to F and F→AF\to A, both in {A,F}\{A,F\}. Check {B,E}\{B,E\}: on 0, B→EB\to E, E→BE\to B, both in {B,E}\{B,E\}; on 1 both go to CC. No further splitting, so P3=P2P_3=P_2.

Equivalent states: A≡FA\equiv F and B≡EB\equiv E.

Minimum DFA (4 states), with [AF][AF] as start state and [D][D] final:

State01
→[AF]\to [AF][BE][BE][AF][AF]
[BE][BE][BE][BE][C][C]
[C][C][BE][BE][D][D]
∗[D]*[D][BE][BE][AF][AF]

It accepts the strings ending in 011011 (for example [AF]→0[BE]→1[C]→1[D][AF]\xrightarrow{0}[BE]\xrightarrow{1}[C]\xrightarrow{1}[D] on 011011).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 10 marks

Construct a Turing Machine that accepts the language of odd length strings over alphabet {a, b}. Give the complete encoding for this TM as well as its input string w = abb in binary alphabet that is recognized by Universal Turing Machine.

Answer

TM for odd-length strings over {a,b}\{a,b\}.

Idea: the head moves right over the input, toggling between two states q0q_0 (even number of symbols read so far) and q1q_1 (odd number read). At the blank, accept if the machine is in q1q_1.

M=({q0,q1,q2},{a,b},{a,b,B},δ,q0,B,{q2})M=(\{q_0,q_1,q_2\},\{a,b\},\{a,b,B\},\delta,q_0,B,\{q_2\})

TransitionMeaning
δ(q0,a)=(q1,a,R)\delta(q_0,a)=(q_1,a,R)even so far, read a symbol
δ(q0,b)=(q1,b,R)\delta(q_0,b)=(q_1,b,R)
δ(q1,a)=(q0,a,R)\delta(q_1,a)=(q_0,a,R)odd so far, read a symbol
δ(q1,b)=(q0,b,R)\delta(q_1,b)=(q_0,b,R)
δ(q1,B)=(q2,B,R)\delta(q_1,B)=(q_2,B,R)end of input with odd length, accept

There is no move for (q0,B)(q_0,B) (even length, including ε\varepsilon), so the machine halts and rejects.

For w=abbw=abb: q0 abb⊢a q1 bb⊢ab q0 b⊢abb q1 B⊢abbB q2q_0\,abb\vdash a\,q_1\,bb\vdash ab\,q_0\,b\vdash abb\,q_1\,B\vdash abbB\,q_2, accepted.

Encoding for the Universal Turing Machine. Number the states q0,q1,q2q_0,q_1,q_2 as 1,2,31,2,3 (the index is the number of 0s), symbols a=X1a=X_1, b=X2b=X_2, B=X3B=X_3, and directions L=D1L=D_1, R=D2R=D_2. A move δ(qi,Xj)=(qk,Xl,Dm)\delta(q_i,X_j)=(q_k,X_l,D_m) is coded

0i 1 0j 1 0k 1 0l 1 0m0^i\,1\,0^j\,1\,0^k\,1\,0^l\,1\,0^m

(state numbers 1, 2, 3 for q0,q1,q2q_0,q_1,q_2):

MoveCode
δ(1,a)=(2,a,R)\delta(1,a)=(2,a,R)0 1 0 1 00 1 0 1 000\,1\,0\,1\,00\,1\,0\,1\,00 = 01010010100
δ(1,b)=(2,b,R)\delta(1,b)=(2,b,R)0 1 00 1 00 1 00 1 000\,1\,00\,1\,00\,1\,00\,1\,00 = 0100100100100
δ(2,a)=(1,a,R)\delta(2,a)=(1,a,R)00 1 0 1 0 1 0 1 0000\,1\,0\,1\,0\,1\,0\,1\,00 = 00101010100
δ(2,b)=(1,b,R)\delta(2,b)=(1,b,R)00 1 00 1 0 1 00 1 0000\,1\,00\,1\,0\,1\,00\,1\,00 = 0010010100100
δ(2,B)=(3,B,R)\delta(2,B)=(3,B,R)00 1 000 1 000 1 000 1 0000\,1\,000\,1\,000\,1\,000\,1\,00 = 00100010001000100

The code of MM is the moves written one after another, separated by 1111 and enclosed by 111111:

⟨M⟩=111 01010010100 11 0100100100100 11 00101010100 11 0010010100100 11 00100010001000100 111\langle M\rangle=111\,01010010100\,11\,0100100100100\,11\,00101010100\,11\,0010010100100\,11\,00100010001000100\,111

Input w=abbw=abb is encoded by writing each symbol as its number of 0s, separated by single 1s: a=0a=0, b=00b=00, so

⟨w⟩=0 1 00 1 00=0100100\langle w\rangle = 0\,1\,00\,1\,00=0100100

The tape given to the universal TM is ⟨M⟩\langle M\rangle followed by ⟨w⟩\langle w\rangle:

⟨M⟩⟨w⟩=11101010010100110100100100100110010101010011001001010010011001000100010001001110100100\langle M\rangle\langle w\rangle=11101010010100110100100100100110010101010011001001010010011001000100010001001110100100

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Define the term alphabet, prefix and suffix of string, concatenation and Kleen closure with example.

Answer

Alphabet. An alphabet Σ\Sigma is a finite, non-empty set of symbols. Examples: Σ={0,1}\Sigma=\{0,1\} (binary), Σ={a,b,…,z}\Sigma=\{a,b,\dots,z\}.

String. A finite sequence of symbols from Σ\Sigma; ∣w∣|w| is its length and ε\varepsilon is the empty string (∣ε∣=0|\varepsilon|=0). For Σ={a,b}\Sigma=\{a,b\}, abbaabba is a string with ∣abba∣=4|abba|=4.

Prefix. A string obtained by removing zero or more symbols from the end of ww. Prefixes of abbaabba: ε, a, ab, abb, abba\varepsilon,\ a,\ ab,\ abb,\ abba. A proper prefix is a prefix other than ww itself.

Suffix. A string obtained by removing zero or more symbols from the beginning of ww. Suffixes of abbaabba: ε, a, ba, bba, abba\varepsilon,\ a,\ ba,\ bba,\ abba.

Concatenation. For strings xx and yy, xyxy is xx followed by yy, with ∣xy∣=∣x∣+∣y∣|xy|=|x|+|y| and εx=xε=x\varepsilon x=x\varepsilon=x. If x=abx=ab and y=bay=ba then xy=abbaxy=abba. For languages, L1L2={xy∣x∈L1, y∈L2}L_1L_2=\{xy \mid x\in L_1,\ y\in L_2\}; e.g. {a,b}{0,1}={a0,a1,b0,b1}\{a,b\}\{0,1\}=\{a0,a1,b0,b1\}.

Kleene closure. For a language LL (or alphabet Σ\Sigma),

L∗=⋃i≥0Li=L0∪L1∪L2∪⋯ ,L0={ε}L^{*}=\bigcup_{i\ge0}L^{i}=L^{0}\cup L^{1}\cup L^{2}\cup\cdots,\qquad L^{0}=\{\varepsilon\}

It is the set of all strings obtained by concatenating zero or more strings of LL. Example: for Σ={a,b}\Sigma=\{a,b\},

Σ∗={ε,a,b,aa,ab,ba,bb,aaa,… }\Sigma^{*}=\{\varepsilon,a,b,aa,ab,ba,bb,aaa,\dots\}

and for L={ab}L=\{ab\}, L∗={ε,ab,abab,ababab,… }L^{*}=\{\varepsilon,ab,abab,ababab,\dots\}. The positive closure is L+=L∗∖{ε}L^{+}=L^{*}\setminus\{\varepsilon\} (when ε∉L\varepsilon\notin L), e.g. {a}+={a,aa,aaa,… }\{a\}^{+}=\{a,aa,aaa,\dots\}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Give the regular expressions for the following language over alphabet {a, b}.
  • Set of all strings with substring bab or abb.
  • Set of all strings whose 3^rd symbol is ‘a’ and 5^th symbol is ‘b’.

Answer

(a) Strings over {a,b}\{a,b\} containing the substring babbab or abbabb.

Any string, then one of the two substrings, then any string:

(a+b)∗ (bab+abb) (a+b)∗(a+b)^{*}\,(bab+abb)\,(a+b)^{*}

Examples: babbab, aabbaabb, bbabaabbabaa, abbbbabbbb. Rejected: aabaaaabaa (no babbab or abbabb), bbbbbb.

(b) Strings whose 3rd symbol is aa and 5th symbol is bb.

Positions 1, 2 are free, position 3 is aa, position 4 is free, position 5 is bb, and the rest is arbitrary:

(a+b)(a+b) a (a+b) b (a+b)∗(a+b)(a+b)\,a\,(a+b)\,b\,(a+b)^{*}

Examples: ababbababb, bbaabbbaab, aaabbabaaabbab. Strings shorter than 5 symbols are not in the language.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Show that L = { a^n | n is a prime number } is not a regular language.

Answer

Claim. L={ap∣p is a prime number}L=\{a^{p}\mid p \text{ is a prime number}\} is not regular.

Proof by the pumping lemma. If LL were regular, there would be a pumping length nn such that any w∈Lw\in L with ∣w∣≥n|w|\ge n can be written w=xyzw=xyz with ∣xy∣≤n|xy|\le n, ∣y∣≥1|y|\ge1 and xyiz∈Lxy^{i}z\in L for all i≥0i\ge0.

Assume LL is regular and take nn as its pumping length. Choose a prime p≥n+2p\ge n+2 (primes are infinite) and let

w=ap∈L,∣w∣=p≥n.w=a^{p}\in L,\qquad |w|=p\ge n.

Write w=xyzw=xyz with ∣x∣=r|x|=r, ∣y∣=k≥1|y|=k\ge1, ∣z∣=p−r−k|z|=p-r-k. Then ∣xyiz∣=p+(i−1)k|xy^{i}z|=p+(i-1)k.

Pump with i=p+1i=p+1:

∣xyp+1z∣=p+pk=p(1+k).|xy^{p+1}z|=p+pk=p(1+k).

Since k≥1k\ge1, both factors are greater than 1: p≥2p\ge2 and 1+k≥21+k\ge2. So p(1+k)p(1+k) is composite, hence ap(1+k)∉La^{p(1+k)}\notin L. This contradicts the pumping lemma.

Therefore LL is not regular. ■\blacksquare

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Explain about the Chomsky’s Hierarchy about the language and programs.

Answer

The Chomsky hierarchy classifies formal grammars, and therefore the languages they generate, into four nested classes according to the form of their production rules. Each class is recognised by a specific kind of machine, so the hierarchy links languages, grammars and the programs (automata) that process them.

A grammar is G=(V,T,P,S)G=(V,T,P,S) with variables VV, terminals TT, productions PP and start symbol SS.

TypeGrammarProduction formLanguage classRecognising machine
0Unrestricted (phrase-structure)α→β\alpha \to \beta, α\alpha contains at least one variableRecursively enumerableTuring machine
1Context-sensitiveαAβ→αγβ\alpha A\beta \to \alpha\gamma\beta, γ≠ε\gamma\neq\varepsilon (so ∣α∣≤∣β∣\lvert\alpha\rvert\le\lvert\beta\rvert)Context-sensitiveLinear-bounded automaton
2Context-freeA→γA \to \gamma, A∈VA\in VContext-freePushdown automaton
3RegularA→aBA \to aB or A→aA \to a (right linear), or the left-linear mirrorRegularFinite automaton

Containment. Type 3⊂Type 2⊂Type 1⊂Type 0\text{Type 3} \subset \text{Type 2} \subset \text{Type 1} \subset \text{Type 0}. Every regular language is context-free, every context-free language is context-sensitive, and so on, and each inclusion is proper.

Examples that separate the classes

  • Type 3: L={anbm∣n,m≥0}L=\{a^nb^m \mid n,m\ge 0\}, generated by S→aS∣B, B→bB∣εS\to aS\mid B,\ B\to bB\mid\varepsilon. A finite automaton is enough because no counting is needed.
  • Type 2: L={anbn∣n≥0}L=\{a^nb^n \mid n\ge 0\}, generated by S→aSb∣εS\to aSb\mid\varepsilon. It needs one stack to match the counts, so it is not regular.
  • Type 1: L={anbncn∣n≥1}L=\{a^nb^nc^n \mid n\ge 1\}. It needs two counts to be compared and is not context-free.
  • Type 0: the halting language, which is only semi-decidable and needs a full Turing machine.

Relation to programs and programming languages

  • Lexical analysis (identifiers, numbers, keywords) is described by regular expressions and implemented with finite automata.
  • Syntax of a programming language (nested blocks, balanced brackets, expressions) is described by context-free grammars and implemented by parsers, which are pushdown automata.
  • Context-sensitive features such as declaring a variable before use, or matching the number of arguments with the parameters, cannot be captured by a context-free grammar. Compilers check them in the semantic analysis phase.
  • Turing machines model any algorithm that a general-purpose program can carry out.

Moving up the hierarchy increases expressive power, but decision problems such as emptiness, membership and equivalence become harder or undecidable.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Define a Push Down Automata. Construct a PDA that accepts L = {a^nb^n | n > 0}

Answer

Definition. A Push Down Automaton (PDA) is a finite automaton with an extra stack. It is a 7-tuple

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

where QQ is a finite set of states, Σ\Sigma the input alphabet, Γ\Gamma the stack alphabet, q0q_0 the start state, Z0∈ΓZ_0\in\Gamma the initial stack symbol, F⊆QF\subseteq Q the set of final states and

δ:Q×(Σ∪{ε})×Γ→2 Q×Γ∗\delta : Q\times(\Sigma\cup\{\varepsilon\})\times\Gamma \to 2^{\,Q\times\Gamma^*}

is the transition function. A move δ(q,a,X)={(p,γ)}\delta(q,a,X)=\{(p,\gamma)\} means: in state qq, reading aa with XX on top of the stack, go to pp and replace XX by the string γ\gamma.

PDA for L={anbn∣n>0}L=\{a^nb^n \mid n>0\} (acceptance by final state)

P=({q0,q1,q2},{a,b},{A,Z},δ,q0,Z,{q2})P=(\{q_0,q_1,q_2\},\{a,b\},\{A,Z\},\delta,q_0,Z,\{q_2\}) with

  1. δ(q0,a,Z)=(q0,AZ)\delta(q_0,a,Z)=(q_0,AZ) (first aa: push AA)
  2. δ(q0,a,A)=(q0,AA)\delta(q_0,a,A)=(q_0,AA) (further aa's: push AA)
  3. δ(q0,b,A)=(q1,ε)\delta(q_0,b,A)=(q_1,\varepsilon) (first bb: pop AA)
  4. δ(q1,b,A)=(q1,ε)\delta(q_1,b,A)=(q_1,\varepsilon) (further bb's: pop AA)
  5. δ(q1,ε,Z)=(q2,Z)\delta(q_1,\varepsilon,Z)=(q_2,Z) (all AA's matched: accept)

Idea: push one AA for each aa, pop one AA for each bb, and accept when the input is finished and only ZZ remains. Since no rule applies to bb in q0q_0 with ZZ on top, the empty string is rejected, as required by n>0n>0.

Trace for aabbaabb

(q0,aabb,Z)⊢(q0,abb,AZ)⊢(q0,bb,AAZ)⊢(q1,b,AZ)⊢(q1,ε,Z)⊢(q2,ε,Z)(q_0,aabb,Z)\vdash(q_0,abb,AZ)\vdash(q_0,bb,AAZ)\vdash(q_1,b,AZ)\vdash(q_1,\varepsilon,Z)\vdash(q_2,\varepsilon,Z)

The input is consumed and the PDA is in the final state q2q_2, so aabbaabb is accepted.

Rejected strings. For aabaab the PDA ends in q1q_1 with AZAZ on the stack and no move, so it is rejected. For abbabb the second bb meets ZZ on the stack with no rule for (q1,b,Z)(q_1,b,Z), so it is also rejected. For baba there is no rule for (q0,b,Z)(q_0,b,Z).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Construct the following grammer into Chomsky Normal Form. S → abSb | a | aAb A → bS | aAAb | ε

Answer

Given

S→abSb∣a∣aAb,A→bS∣aAAb∣εS\to abSb\mid a\mid aAb,\qquad A\to bS\mid aAAb\mid\varepsilon

Step 1. Remove ε\varepsilon-productions. Only AA is nullable. Replace each occurrence of AA by AA or nothing in every right-hand side:

  • S→abSb∣a∣aAb∣abS\to abSb\mid a\mid aAb\mid ab
  • A→bS∣aAAb∣aAb∣abA\to bS\mid aAAb\mid aAb\mid ab (from aAAbaAAb dropping one or both AA's)

Step 2. Remove unit productions. There are none. All symbols are generating and reachable, so there are no useless symbols.

Step 3. Replace terminals inside longer right-hand sides. Introduce Ca→aC_a\to a and Cb→bC_b\to b:

  • S→CaCbSCb∣a∣CaACb∣CaCbS\to C_aC_bSC_b\mid a\mid C_aAC_b\mid C_aC_b
  • A→CbS∣CaAACb∣CaACb∣CaCbA\to C_bS\mid C_aAAC_b\mid C_aAC_b\mid C_aC_b

Step 4. Break right-hand sides longer than two variables. Use D1→CbD2, D2→SCb, D3→ACb, E→AD3D_1\to C_bD_2,\ D_2\to SC_b,\ D_3\to AC_b,\ E\to AD_3:

  • CaCbSCbC_aC_bSC_b becomes CaD1C_aD_1 with D1→CbD2D_1\to C_bD_2 and D2→SCbD_2\to SC_b
  • CaACbC_aAC_b becomes CaD3C_aD_3 with D3→ACbD_3\to AC_b
  • CaAACbC_aAAC_b becomes CaEC_aE with E→AD3E\to AD_3

Grammar in Chomsky Normal Form

S→CaD1∣CaD3∣CaCb∣aA→CbS∣CaE∣CaD3∣CaCbD1→CbD2D2→SCbD3→ACbE→AD3Ca→aCb→b\begin{aligned} S&\to C_aD_1\mid C_aD_3\mid C_aC_b\mid a\\ A&\to C_bS\mid C_aE\mid C_aD_3\mid C_aC_b\\ D_1&\to C_bD_2\\ D_2&\to SC_b\\ D_3&\to AC_b\\ E&\to AD_3\\ C_a&\to a\\ C_b&\to b \end{aligned}

Every production now has the form X→YZX\to YZ or X→tX\to t.

Check with abababab. In the original grammar, S⇒abSb⇒ababS\Rightarrow abSb\Rightarrow abab. In CNF, S⇒CaD1⇒aD1⇒aCbD2⇒abD2⇒abSCb⇒abaCb⇒ababS\Rightarrow C_aD_1\Rightarrow aD_1\Rightarrow aC_bD_2\Rightarrow abD_2\Rightarrow abSC_b\Rightarrow abaC_b\Rightarrow abab, so the same string is generated.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Define Turing Machine and explain its different variations.

Answer

Definition. A Turing Machine (TM) is a mathematical model of computation consisting of a finite control, and an infinite tape divided into cells, with a read/write head that moves left or right one cell at a time. Formally it is a 7-tuple

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

  • QQ: finite set of states
  • Σ\Sigma: input alphabet (B∉ΣB\notin\Sigma)
  • Γ\Gamma: tape alphabet, Σ⊂Γ\Sigma\subset\Gamma
  • δ:Q×Γ→Q×Γ×{L,R}\delta: Q\times\Gamma\to Q\times\Gamma\times\{L,R\}: transition function
  • q0q_0: start state
  • B∈ΓB\in\Gamma: blank symbol
  • F⊆QF\subseteq Q: set of final (accepting) states

In one move the machine reads the symbol under the head, writes a symbol, moves the head left or right and changes state. The input is accepted if the machine reaches a final state. The class of languages accepted is the recursively enumerable languages.

Variations of the Turing machine

  1. Multitape TM. Has kk tapes, each with its own head. A move depends on the state and the kk scanned symbols, and each head writes and moves independently. It is more convenient to program, but it accepts the same languages as the standard TM. A single-tape machine simulates it with a quadratic slowdown.
  2. Nondeterministic TM (NTM). δ(q,X)\delta(q,X) gives a set of possible moves. A string is accepted if some sequence of choices leads to acceptance. It has the same power as a deterministic TM, though the simulation may take exponential time.
  3. Two-way (bi-infinite) tape TM. The tape extends infinitely in both directions. It can be simulated by a standard TM that folds the tape in two tracks.
  4. Multi-track TM. Each cell holds a tuple of symbols (several tracks). It is a special case of one tape with a larger tape alphabet and is useful for marking.
  5. Multidimensional TM. The tape is a two- or higher-dimensional grid, and the head can also move up and down. It is again equivalent in power to the standard TM.
  6. Multihead TM. Several heads on one tape. This is equivalent in power.
  7. Offline TM and TM with a read-only input tape. Used to study space complexity.
  8. Restricted TMs. A TM whose tape is bounded by the input length is a linear-bounded automaton (context-sensitive languages). A TM with a stack is a PDA.
  9. Universal TM. A single machine that reads the description of any other TM and its input and simulates it. This is the theoretical basis of stored-program computers.

All the extended models recognise exactly the same class of languages as the basic TM. They differ only in convenience and efficiency, which supports the Church-Turing thesis.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Whar do you mean by computational Complexity? Explian about the time and space complexity of a Turing machine.

Answer

Computational complexity is the study of the resources, mainly time and space (memory), that an algorithm or machine needs to solve a problem, expressed as a function of the input size nn. It classifies problems according to how hard they are to solve, rather than just whether they can be solved at all.

Time complexity of a Turing machine. For a TM MM that halts on every input, the time complexity is

TM(n)=max⁡{number of moves M makes on an input of length n}T_M(n)=\max\{\text{number of moves } M \text{ makes on an input of length } n\}

Thus MM is said to run in time T(n)T(n) if on every input of length nn it halts within T(n)T(n) steps. For a nondeterministic TM, T(n)T(n) is the maximum over all inputs of length nn of the length of the longest computation branch. The classes are

DTIME(T(n)),NTIME(T(n)),P=⋃kDTIME(nk),NP=⋃kNTIME(nk)\text{DTIME}(T(n)),\qquad \text{NTIME}(T(n)),\qquad P=\bigcup_{k}\text{DTIME}(n^k),\qquad NP=\bigcup_k\text{NTIME}(n^k)

Space complexity of a Turing machine. The space complexity is

SM(n)=max⁡{number of distinct tape cells M scans on an input of length n}S_M(n)=\max\{\text{number of distinct tape cells } M \text{ scans on an input of length } n\}

For a multi-tape TM with a read-only input tape, only the work-tape cells are counted, which allows sub-linear space such as O(log⁡n)O(\log n). The classes are DSPACE(S(n))\text{DSPACE}(S(n)), NSPACE(S(n))\text{NSPACE}(S(n)), PSPACE=⋃kDSPACE(nk)\text{PSPACE}=\bigcup_k \text{DSPACE}(n^k).

Example. A single-tape TM for {anbn}\{a^nb^n\} that repeatedly crosses off one aa and one bb makes about nn passes of length about 2n2n, so T(n)=O(n2)T(n)=O(n^2). It uses no cells beyond the input, so S(n)=O(n)S(n)=O(n).

Important relations

  • S(n)≤T(n)+1S(n)\le T(n)+1 (each step touches at most one new cell), so DTIME(f)⊆DSPACE(f)\text{DTIME}(f)\subseteq\text{DSPACE}(f).
  • A deterministic machine using space S(n)≥log⁡nS(n)\ge \log n runs in time at most 2O(S(n))2^{O(S(n))}.
  • A kk-tape TM running in time T(n)T(n) can be simulated by a single-tape TM in time O(T(n)2)O(T(n)^2).
  • A nondeterministic TM running in time T(n)T(n) can be simulated by a deterministic TM in time 2O(T(n))2^{O(T(n))}.
  • Savitch's theorem: NSPACE(S(n))⊆DSPACE(S(n)2)\text{NSPACE}(S(n))\subseteq\text{DSPACE}(S(n)^2).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2078 · 5 marks

Explain the term Intractability. Is SAT problem is intractable? Justify

Answer

Intractability. A problem is tractable if it can be solved by an algorithm running in polynomial time, O(nk)O(n^k) for some constant kk (the class PP). A problem is intractable if no polynomial-time algorithm exists for it, or at least none is known, so that the running time grows exponentially (for example 2n2^n or n!n!) and becomes infeasible even for moderate inputs. For n=100n=100 an n3n^3 algorithm needs about 10610^6 steps, while a 2n2^n algorithm needs about 103010^{30} steps. Such problems may be solvable in principle but not in practical time. Examples are the Travelling Salesman Problem, Hamiltonian Circuit, Vertex Cover and the Knapsack decision problem.

The SAT problem. Given a Boolean formula φ\varphi in the variables x1,…,xnx_1,\dots,x_n, decide whether some assignment of truth values makes φ\varphi true.

Is SAT intractable?

  1. Brute force is exponential. Trying all 2n2^n assignments takes O(2n⋅∣φ∣)O(2^n\cdot|\varphi|) time.
  2. SAT is in NP. A proposed assignment (the certificate) can be checked in polynomial time by evaluating the formula.
  3. SAT is NP-complete (Cook-Levin theorem, 1971). Every problem in NP can be reduced to SAT in polynomial time. Therefore, if SAT had a polynomial-time algorithm, then every problem in NP would, and P=NPP=NP.
  4. No polynomial-time algorithm for SAT is known despite decades of effort, and it is widely believed that P≠NPP\neq NP.

Hence SAT is regarded as intractable. This is a conjecture, because it has not been proved that P≠NPP\ne NP. Strictly, SAT is intractable if and only if P≠NPP\neq NP. Restricted versions differ: 2-SAT is solvable in polynomial time (linear time), while 3-SAT remains NP-complete. In practice modern SAT solvers handle many large instances, but worst-case exponential behaviour cannot be ruled out.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 10 marks

Show that, For any NFA N=(Q, ∑, δ, q0, F) accepting language L=∑, There is a DFA D= (Q’, ∑’, q0′, δ’, F’) accepting the same language L.

Answer

Theorem. For every NFA N=(Q,Σ,δ,q0,F)N=(Q,\Sigma,\delta,q_0,F) there is a DFA D=(Q′,Σ,δ′,q0′,F′)D=(Q',\Sigma,\delta',q_0',F') with L(D)=L(N)L(D)=L(N).

Construction (subset construction).

  • Q′=2QQ'=2^{Q}: each DFA state is a set of NFA states.
  • q0′={q0}q_0'=\{q_0\}.
  • δ′(S,a)=⋃p∈Sδ(p,a)\delta'(S,a)=\bigcup_{p\in S}\delta(p,a) for S∈Q′S\in Q' and a∈Σa\in\Sigma.
  • F′={S∈Q′∣S∩F≠∅}F'=\{S\in Q' \mid S\cap F\neq\emptyset\}.

DD is a DFA, since for each SS and aa exactly one set δ′(S,a)\delta'(S,a) is defined. It has at most 2∣Q∣2^{|Q|} states. Only the states reachable from {q0}\{q_0\} need to be built.

Claim. For every string w∈Σ∗w\in\Sigma^*,

δ^′({q0},w)=δ^(q0,w).\hat\delta'(\{q_0\},w)=\hat\delta(q_0,w).

Proof by induction on ∣w∣|w|.

Basis ∣w∣=0|w|=0: w=εw=\varepsilon, and δ^′({q0},ε)={q0}=δ^(q0,ε)\hat\delta'(\{q_0\},\varepsilon)=\{q_0\}=\hat\delta(q_0,\varepsilon).

Induction step. Assume the claim for ww and let a∈Σa\in\Sigma. Then

δ^(q0,wa)=⋃p∈δ^(q0,w)δ(p,a)\hat\delta(q_0,wa)=\bigcup_{p\in\hat\delta(q_0,w)}\delta(p,a)

by the definition of the extended NFA transition. By the induction hypothesis δ^(q0,w)=δ^′({q0},w)=S\hat\delta(q_0,w)=\hat\delta'(\{q_0\},w)=S, so

δ^(q0,wa)=⋃p∈Sδ(p,a)=δ′(S,a)=δ′(δ^′({q0},w),a)=δ^′({q0},wa).\hat\delta(q_0,wa)=\bigcup_{p\in S}\delta(p,a)=\delta'(S,a)=\delta'(\hat\delta'(\{q_0\},w),a)=\hat\delta'(\{q_0\},wa).

This proves the claim.

Language equality. For any ww:

w∈L(D)  ⟺  δ^′({q0},w)∈F′  ⟺  δ^(q0,w)∩F≠∅  ⟺  w∈L(N).w\in L(D)\iff\hat\delta'(\{q_0\},w)\in F'\iff\hat\delta(q_0,w)\cap F\ne\emptyset\iff w\in L(N).

Hence L(D)=L(N)L(D)=L(N). So every language accepted by an NFA is accepted by a DFA, which means NFAs and DFAs have the same power. (If the NFA has ε\varepsilon-moves, use ε\varepsilon-closures in q0′=ECLOSE(q0)q_0'=\text{ECLOSE}(q_0) and δ′(S,a)=⋃p∈SECLOSE(δ(p,a))\delta'(S,a)=\bigcup_{p\in S}\text{ECLOSE}(\delta(p,a)).)

Example. NFA over {0,1}\{0,1\} accepting strings ending in 0101: δ(q0,0)={q0,q1}\delta(q_0,0)=\{q_0,q_1\}, δ(q0,1)={q0}\delta(q_0,1)=\{q_0\}, δ(q1,1)={q2}\delta(q_1,1)=\{q_2\}, F={q2}F=\{q_2\}.

DFA stateon 0on 1
{q0}\{q_0\}{q0,q1}\{q_0,q_1\}{q0}\{q_0\}
{q0,q1}\{q_0,q_1\}{q0,q1}\{q_0,q_1\}{q0,q2}\{q_0,q_2\}
{q0,q2}\{q_0,q_2\} (final){q0,q1}\{q_0,q_1\}{q0}\{q_0\}

The DFA has three reachable states and accepts exactly the strings ending in 0101.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 10 marks

State and prove the Pumping Lemma for regular languages. How can you show with example that pumping lemma is used to prove that a given language is not a regular? Explain.

Answer

Pumping Lemma for regular languages. If LL is a regular language, then there is a constant p≥1p\ge1 (the pumping length) such that every string w∈Lw\in L with ∣w∣≥p|w|\ge p can be written as w=xyzw=xyz with

  1. ∣y∣≥1|y|\ge1,
  2. ∣xy∣≤p|xy|\le p,
  3. xyiz∈Lxy^iz\in L for all i≥0i\ge0.

Proof. Let LL be regular, so it is accepted by a DFA MM with pp states. Let w=a1a2…an∈Lw=a_1a_2\dots a_n\in L with n≥pn\ge p. Let the states visited be q0,q1,…,qnq_0,q_1,\dots,q_n where qi=δ^(q0,a1…ai)q_i=\hat\delta(q_0,a_1\dots a_i) and qnq_n is final.

Among the first p+1p+1 states q0,…,qpq_0,\dots,q_p there are p+1p+1 entries but only pp distinct states, so by the pigeonhole principle qj=qkq_j=q_k for some 0≤j<k≤p0\le j<k\le p. Put

x=a1…aj,y=aj+1…ak,z=ak+1…an.x=a_1\dots a_j,\qquad y=a_{j+1}\dots a_k,\qquad z=a_{k+1}\dots a_n.

Then ∣y∣=k−j≥1|y|=k-j\ge1 and ∣xy∣=k≤p|xy|=k\le p. Reading xx takes MM from q0q_0 to qjq_j. Reading yy takes qjq_j back to qk=qjq_k=q_j, so yy is a loop. Reading zz takes qkq_k to the final state qnq_n. Going around the loop ii times (i=0i=0 skips it) gives the path q0→xqj→yiqj→zqnq_0\xrightarrow{x}q_j\xrightarrow{y^i}q_j\xrightarrow{z}q_n, so xyizxy^iz is accepted for every i≥0i\ge0. ■\blacksquare

Using it to show a language is not regular. The lemma is a necessary condition only, so it can only prove non-regularity. It is used as a game against an adversary:

  1. Assume LL is regular, with pumping length pp (given by the adversary).
  2. Choose a string w∈Lw\in L with ∣w∣≥p|w|\ge p (our choice, depending on pp).
  3. The adversary splits w=xyzw=xyz with ∣xy∣≤p|xy|\le p and ∣y∣≥1|y|\ge1. We must handle every such split.
  4. Choose ii so that xyiz∉Lxy^iz\notin L. This contradicts the lemma, so LL is not regular.

Example. L={0n1n∣n≥0}L=\{0^n1^n\mid n\ge0\} is not regular.

Assume it is regular with pumping length pp and take w=0p1p∈Lw=0^p1^p\in L, ∣w∣=2p≥p|w|=2p\ge p. Since ∣xy∣≤p|xy|\le p, both xx and yy lie entirely in the block of 0's, so y=0my=0^m with 1≤m≤p1\le m\le p. Pump up with i=2i=2:

xy2z=0p+m1p,xy^2z=0^{p+m}1^p,

which has more 0's than 1's and is not in LL. This contradicts condition 3, so LL is not regular.

The same method (choosing i=0i=0 or i=2i=2) shows that {ww}\{ww\}, {an2}\{a^{n^2}\} and the set of balanced parentheses strings are not regular.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 10 marks

Given the following expression grammar for simple arithematic expression with operator + and *. E→ E+T | T T → T+F | F F → (E) | a Remove the left recursion from this grammar then simplify and convert to CNF.

Answer

Grammar as given:

E→E+T∣T,T→T+F∣F,F→(E)∣aE\to E+T\mid T,\qquad T\to T+F\mid F,\qquad F\to(E)\mid a

Step 1. Remove left recursion. A rule A→Aα∣βA\to A\alpha\mid\beta becomes A→βA′, A′→αA′∣εA\to\beta A',\ A'\to\alpha A'\mid\varepsilon.

E→TE′E′→+TE′∣εT→FT′T′→+FT′∣εF→(E)∣a\begin{aligned} E&\to TE' & E'&\to +TE'\mid\varepsilon\\ T&\to FT' & T'&\to +FT'\mid\varepsilon\\ F&\to(E)\mid a \end{aligned}

Step 2. Simplify.

Remove ε\varepsilon-productions. E′E' and T′T' are nullable. Producing the variants with and without them:

E→TE′∣TE′→+TE′∣+TT→FT′∣FT′→+FT′∣+FF→(E)∣a\begin{aligned} E&\to TE'\mid T & E'&\to +TE'\mid +T\\ T&\to FT'\mid F & T'&\to +FT'\mid +F\\ F&\to(E)\mid a \end{aligned}

Remove unit productions E→TE\to T and T→FT\to F. First T→FT\to F is replaced by the productions of FF; then E→TE\to T by those of TT:

F→(E)∣aT→FT′∣(E)∣aE→TE′∣FT′∣(E)∣aE′→+TE′∣+TT′→+FT′∣+F\begin{aligned} F&\to(E)\mid a\\ T&\to FT'\mid(E)\mid a\\ E&\to TE'\mid FT'\mid(E)\mid a\\ E'&\to +TE'\mid +T\\ T'&\to +FT'\mid +F \end{aligned}

All symbols are reachable from EE and generate terminal strings, so there are no useless symbols.

Step 3. Convert to CNF. Introduce P→+P\to +, L→(L\to(, R→)R\to) for terminals inside longer right-hand sides, and break long right-hand sides with X→ERX\to ER, Y1→TE′Y_1\to TE', Y2→FT′Y_2\to FT':

E→TE′∣FT′∣LX∣aT→FT′∣LX∣aF→LX∣aE′→PY1∣PTT′→PY2∣PFX→ERY1→TE′Y2→FT′P→+,L→(,R→)\begin{aligned} E&\to TE'\mid FT'\mid LX\mid a\\ T&\to FT'\mid LX\mid a\\ F&\to LX\mid a\\ E'&\to PY_1\mid PT\\ T'&\to PY_2\mid PF\\ X&\to ER\\ Y_1&\to TE'\\ Y_2&\to FT'\\ P&\to +,\quad L\to(,\quad R\to) \end{aligned}

Every production has the form A→BCA\to BC or A→aA\to a, so the grammar is in Chomsky Normal Form. The start symbol EE does not derive ε\varepsilon and the language is unchanged.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Explain the ε-closure of states on an ε-NFA with suitable examples.

Answer

An ε\varepsilon-NFA is an NFA that can change state without reading any input symbol (ε\varepsilon-transitions): δ:Q×(Σ∪{ε})→2Q\delta:Q\times(\Sigma\cup\{\varepsilon\})\to2^Q.

Definition. The ε\varepsilon-closure of a state qq, written ECLOSE(q)\text{ECLOSE}(q), is the set of all states reachable from qq by following zero or more ε\varepsilon-transitions. It is defined recursively:

  1. q∈ECLOSE(q)q\in\text{ECLOSE}(q).
  2. If p∈ECLOSE(q)p\in\text{ECLOSE}(q) and r∈δ(p,ε)r\in\delta(p,\varepsilon), then r∈ECLOSE(q)r\in\text{ECLOSE}(q).

For a set of states SS, ECLOSE(S)=⋃q∈SECLOSE(q)\text{ECLOSE}(S)=\bigcup_{q\in S}\text{ECLOSE}(q).

Example. Consider the ε\varepsilon-NFA with transitions

q0→εq1,q1→εq2,q1→bq0,q2→aq3.q_0\xrightarrow{\varepsilon}q_1,\quad q_1\xrightarrow{\varepsilon}q_2,\quad q_1\xrightarrow{b}q_0,\quad q_2\xrightarrow{a}q_3.

  • ECLOSE(q0)={q0,q1,q2}\text{ECLOSE}(q_0)=\{q_0,q_1,q_2\} (via q0→q1→q2q_0\to q_1\to q_2)
  • ECLOSE(q1)={q1,q2}\text{ECLOSE}(q_1)=\{q_1,q_2\}
  • ECLOSE(q2)={q2}\text{ECLOSE}(q_2)=\{q_2\}
  • ECLOSE(q3)={q3}\text{ECLOSE}(q_3)=\{q_3\}

The bb-transition from q1q_1 is not followed, because only ε\varepsilon-moves count.

Use. The closure defines the extended transition function: δ^(q,ε)=ECLOSE(q)\hat\delta(q,\varepsilon)=\text{ECLOSE}(q) and δ^(q,wa)=ECLOSE(⋃p∈δ^(q,w)δ(p,a))\hat\delta(q,wa)=\text{ECLOSE}\left(\bigcup_{p\in\hat\delta(q,w)}\delta(p,a)\right). It is also what is used to convert an ε\varepsilon-NFA to a DFA: the start state is ECLOSE(q0)\text{ECLOSE}(q_0), and δD(S,a)=ECLOSE(⋃p∈Sδ(p,a))\delta_D(S,a)=\text{ECLOSE}\big(\bigcup_{p\in S}\delta(p,a)\big). In the example, on aa from ECLOSE(q0)={q0,q1,q2}\text{ECLOSE}(q_0)=\{q_0,q_1,q_2\} we reach ECLOSE(q3)={q3}\text{ECLOSE}(q_3)=\{q_3\}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Convert the following regular expression into equivalent Finite Automata a. (0+1)10(1+0) b. 10(0+1)*1

Answer

(a) (0+1)∗10(1+0)(0+1)^*10(1+0)

The language is all strings of length at least 3 whose third-last symbol is 11 and second-last symbol is 00 (any last symbol). The NFA guesses when the final three symbols begin.

States q0q_0 (start), q1,q2,q3q_1,q_2,q_3 with q3q_3 final.

State01
→q0\to q_0{q0}\{q_0\}{q0,q1}\{q_0,q_1\}
q1q_1{q2}\{q_2\}∅\emptyset
q2q_2{q3}\{q_3\}{q3}\{q_3\}
∗q3*q_3∅\emptyset∅\emptyset

Subset construction gives the equivalent DFA, with A={q0}A=\{q_0\}, B={q0,q1}B=\{q_0,q_1\}, C={q0,q2}C=\{q_0,q_2\}, D={q0,q3}D=\{q_0,q_3\}, E={q0,q1,q3}E=\{q_0,q_1,q_3\}, final states D,ED,E:

State01
→A\to AAABB
BBCCBB
CCDDEE
∗D*DAABB
∗E*ECCBB

Check on 11001100: A→1B→1B→0C→0DA\xrightarrow{1}B\xrightarrow{1}B\xrightarrow{0}C\xrightarrow{0}D, final, accepted. Check on 11011101: …C→1E\ldots C\xrightarrow{1}E, final, accepted. Check on 01100110: A→A→B→B→CA\to A\to B\to B\to C, not final, rejected, since its last three symbols are 110110, not 10x10x.

(b) 1∗0(0+1)∗11^*0(0+1)^*1

The language is strings that, after any number of leading 1's, have a 00, then anything, and end with a 11.

States p0p_0 (start), p1p_1, p2p_2 (final).

State01
→p0\to p_0{p1}\{p_1\}{p0}\{p_0\}
p1p_1{p1}\{p_1\}{p1,p2}\{p_1,p_2\}
∗p2*p_2∅\emptyset∅\emptyset

The equivalent DFA has A={p0}A=\{p_0\}, B={p1}B=\{p_1\}, C={p1,p2}C=\{p_1,p_2\} (final):

State01
→A\to ABBAA
BBBBCC
∗C*CBBCC

Check: 1101111011: A→A→A→B→C→CA\to A\to A\to B\to C\to C, accepted. 0101: A→B→CA\to B\to C, accepted. 110110: ends in BB, rejected. 111111: stays in AA, rejected.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Define the term: Parse Tree, left-most and right-most derivation, sentential form and ambiguity with example.

Answer

Consider the grammar GG: E→E+E∣E∗E∣(E)∣idE\to E+E\mid E*E\mid(E)\mid id.

Parse tree (derivation tree). A tree showing how a string is derived from the start symbol. The root is the start symbol, each interior node is a variable, and its children are the symbols of the right-hand side of the production applied. The leaves, read left to right, form the derived string (the yield). For E⇒E+EE\Rightarrow E+E with the right E⇒E∗EE\Rightarrow E*E, the tree for id+id∗idid+id*id is

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

Left-most derivation. At every step the left-most variable is replaced.

E⇒E+E⇒id+E⇒id+E∗E⇒id+id∗E⇒id+id∗idE\Rightarrow E+E\Rightarrow id+E\Rightarrow id+E*E\Rightarrow id+id*E\Rightarrow id+id*id

Right-most derivation. At every step the right-most variable is replaced.

E⇒E+E⇒E+E∗E⇒E+E∗id⇒E+id∗id⇒id+id∗idE\Rightarrow E+E\Rightarrow E+E*E\Rightarrow E+E*id\Rightarrow E+id*id\Rightarrow id+id*id

Sentential form. Any string of terminals and variables derivable from the start symbol, such as EE, E+EE+E, id+E∗Eid+E*E. If it contains only terminals, it is a sentence of the language. A left-sentential form arises in a left-most derivation, and a right-sentential form in a right-most derivation.

Ambiguity. A grammar is ambiguous if some string in its language has two or more distinct parse trees, or equivalently two or more distinct left-most (or right-most) derivations. The grammar above is ambiguous for id+id∗idid+id*id. The second left-most derivation is

E⇒E∗E⇒E+E∗E⇒id+E∗E⇒id+id∗E⇒id+id∗idE\Rightarrow E*E\Rightarrow E+E*E\Rightarrow id+E*E\Rightarrow id+id*E\Rightarrow id+id*id

Its tree groups the string as (id+id)∗id(id+id)*id, whereas the first tree groups it as id+(id∗id)id+(id*id). The two trees give different meanings, which is why ambiguity is a problem in compilers. It is removed by rewriting the grammar to enforce precedence and associativity, for example E→E+T∣T, T→T∗F∣F, F→(E)∣idE\to E+T\mid T,\ T\to T*F\mid F,\ F\to(E)\mid id.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Give the formal definiton of Push Down Automata. How CFG can be converted into equivalent PDA. Explain with an example.

Answer

Formal definition. A Push Down Automaton is a 7-tuple

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

  • QQ: finite set of states
  • Σ\Sigma: input alphabet
  • Γ\Gamma: stack alphabet
  • δ:Q×(Σ∪{ε})×Γ→\delta:Q\times(\Sigma\cup\{\varepsilon\})\times\Gamma\to finite subsets of Q×Γ∗Q\times\Gamma^*: transition function
  • q0∈Qq_0\in Q: start state
  • Z0∈ΓZ_0\in\Gamma: initial stack symbol
  • F⊆QF\subseteq Q: final states

It accepts either by final state, L(P)={w∣(q0,w,Z0)⊢∗(q,ε,α), q∈F}L(P)=\{w\mid(q_0,w,Z_0)\vdash^*(q,\varepsilon,\alpha),\ q\in F\}, or by empty stack, N(P)={w∣(q0,w,Z0)⊢∗(q,ε,ε)}N(P)=\{w\mid(q_0,w,Z_0)\vdash^*(q,\varepsilon,\varepsilon)\}.

Converting a CFG to an equivalent PDA. Let G=(V,T,P,S)G=(V,T,P,S). Build a PDA with one state that accepts by empty stack:

M=({q},T,V∪T,δ,q,S,∅)M=(\{q\},T,V\cup T,\delta,q,S,\emptyset)

  1. For each variable AA: δ(q,ε,A)={(q,β)∣A→β∈P}\delta(q,\varepsilon,A)=\{(q,\beta)\mid A\to\beta\in P\} (replace the top variable by the right-hand side of a production).
  2. For each terminal aa: δ(q,a,a)={(q,ε)}\delta(q,a,a)=\{(q,\varepsilon)\} (match the input symbol with the top of the stack and pop it).

The stack holds the unprocessed part of a left-most derivation, so MM accepts ww exactly when S⇒lm∗wS\Rightarrow^*_{lm}w, giving N(M)=L(G)N(M)=L(G).

Example. GG: S→aSb∣εS\to aSb\mid\varepsilon, generating {anbn∣n≥0}\{a^nb^n\mid n\ge0\}. The PDA has

  • δ(q,ε,S)={(q,aSb),(q,ε)}\delta(q,\varepsilon,S)=\{(q,aSb),(q,\varepsilon)\}
  • δ(q,a,a)={(q,ε)}\delta(q,a,a)=\{(q,\varepsilon)\}
  • δ(q,b,b)={(q,ε)}\delta(q,b,b)=\{(q,\varepsilon)\}

Accepting aabbaabb (top of stack on the left):

(q,aabb,S)⊢(q,aabb,aSb)⊢(q,abb,Sb)⊢(q,abb,aSbb)⊢(q,bb,Sbb)⊢(q,bb,bb)⊢(q,b,b)⊢(q,ε,ε)\begin{aligned} (q,aabb,S)&\vdash(q,aabb,aSb)\vdash(q,abb,Sb)\vdash(q,abb,aSbb)\vdash(q,bb,Sbb)\\ &\vdash(q,bb,bb)\vdash(q,b,b)\vdash(q,\varepsilon,\varepsilon) \end{aligned}

where the move (q,bb,Sbb)⊢(q,bb,bb)(q,bb,Sbb)\vdash(q,bb,bb) uses S→εS\to\varepsilon. The stack is empty with the input consumed, so aabbaabb is accepted, matching the derivation S⇒aSb⇒aaSbb⇒aabbS\Rightarrow aSb\Rightarrow aaSbb\Rightarrow aabb.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Define regular grammar. Also explain the method of converting right linear grammar into equivalent finite automata.

Answer

Regular grammar. A grammar G=(V,T,P,S)G=(V,T,P,S) is regular if all its productions are of one of the two restricted forms:

  • Right linear: A→aBA\to aB or A→aA\to a (or A→εA\to\varepsilon), with A,B∈VA,B\in V and a∈Ta\in T.
  • Left linear: A→BaA\to Ba or A→aA\to a.

A grammar must use only one of the two forms to be regular. The languages generated by regular grammars are exactly the regular languages, those accepted by finite automata.

Converting a right-linear grammar to a finite automaton. Build an NFA M=(Q,T,δ,S,F)M=(Q,T,\delta,S,F) as follows.

  1. Q=V∪{qf}Q=V\cup\{q_f\}: one state for each variable plus a new final state qfq_f.
  2. The start state is SS.
  3. For each production A→aBA\to aB, add BB to δ(A,a)\delta(A,a).
  4. For each production A→aA\to a, add qfq_f to δ(A,a)\delta(A,a).
  5. For each production A→εA\to\varepsilon, make AA a final state as well.
  6. F={qf}F=\{q_f\} (plus any AA with A→εA\to\varepsilon).

A derivation S⇒a1A1⇒a1a2A2⇒⋯⇒a1…anS\Rightarrow a_1A_1\Rightarrow a_1a_2A_2\Rightarrow\dots\Rightarrow a_1\dots a_n corresponds exactly to a path in the NFA that reads a1…ana_1\dots a_n and ends in qfq_f. The NFA can then be converted to a DFA by the subset construction.

Example. S→aS∣a∣bA,A→bA∣bS\to aS\mid a\mid bA,\quad A\to bA\mid b.

Transitions: δ(S,a)={S,qf}\delta(S,a)=\{S,q_f\}, δ(S,b)={A}\delta(S,b)=\{A\}, δ(A,b)={A,qf}\delta(A,b)=\{A,q_f\}, and δ(A,a)=∅\delta(A,a)=\emptyset.

Stateaabb
→S\to S{S,qf}\{S,q_f\}{A}\{A\}
AA∅\emptyset{A,qf}\{A,q_f\}
∗qf*q_f∅\emptyset∅\emptyset

The language is a++a∗bb+a^+ + a^*bb^+ (a block of aa's, or any number of aa's followed by at least two bb's).

Check on aabbaabb: S→aS→aS→bA→bqfS\xrightarrow{a}S\xrightarrow{a}S\xrightarrow{b}A\xrightarrow{b}q_f, accepted. It matches the derivation S⇒aS⇒aaS⇒aabA⇒aabbS\Rightarrow aS\Rightarrow aaS\Rightarrow aabA\Rightarrow aabb. The string abab ends in the non-final state AA and is rejected, which agrees with the grammar since AA must still produce at least one more bb.

A left-linear grammar can be handled by reversing its productions, building the automaton for the reversed language, and then reversing that automaton.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Construct a Turing machine that accepts the language, L = { a^n b^n | n≥0}

Answer

Idea. Mark one aa with XX and then the matching bb with YY, and repeat. When no aa is left, check that no unmarked bb remains.

M=({q0,q1,q2,q3,qf},{a,b},{a,b,X,Y,B},δ,q0,B,{qf})M=(\{q_0,q_1,q_2,q_3,q_f\},\{a,b\},\{a,b,X,Y,B\},\delta,q_0,B,\{q_f\})

Meaning of states

  • q0q_0: look for the next unmarked aa at the left.
  • q1q_1: move right to find the first unmarked bb.
  • q2q_2: move left back to the last XX.
  • q3q_3: all aa's are marked; verify only YY's remain.
  • qfq_f: accept.

Transitions (written as (state,write,move)(\text{state},\text{write},\text{move}))

StateaabbXXYYBB
q0q_0(q1,X,R)(q_1,X,R),,(q3,Y,R)(q_3,Y,R)(qf,B,R)(q_f,B,R)
q1q_1(q1,a,R)(q_1,a,R)(q2,Y,L)(q_2,Y,L),(q1,Y,R)(q_1,Y,R),
q2q_2(q2,a,L)(q_2,a,L),(q0,X,R)(q_0,X,R)(q2,Y,L)(q_2,Y,L),
q3q_3,,,(q3,Y,R)(q_3,Y,R)(qf,B,R)(q_f,B,R)

A blank entry means no move, so the machine halts and rejects. The empty string (n=0n=0) is accepted directly by q0q_0 on BB.

Trace for aabbaabb (the head position is shown by the state before the scanned symbol):

q0aabb⊢Xq1abb⊢Xaq1bb⊢Xq2aYb⊢q2XaYb⊢Xq0aYb⊢XXq1Yb⊢XXYq1b⊢XXq2YY⊢Xq2XYY⊢XXq0YY⊢XXYq3Y⊢XXYYq3B⊢XXYYBqf\begin{aligned} &q_0aabb\vdash Xq_1abb\vdash Xaq_1bb\vdash Xq_2aYb\vdash q_2XaYb\vdash Xq_0aYb\\ &\vdash XXq_1Yb\vdash XXYq_1b\vdash XXq_2YY\vdash Xq_2XYY\vdash XXq_0YY\\ &\vdash XXYq_3Y\vdash XXYYq_3B\vdash XXYYBq_f \end{aligned}

The machine reaches qfq_f, so aabbaabb is accepted.

Rejection examples. For aabaab: after marking both aa's and the single bb, q2q_2 returns to q0q_0, which finds aa, goes to q1q_1, reaches BB with no rule and halts. For abbabb: after one pair is marked, q0q_0 reads YY, goes to q3q_3, finds an unmarked bb and halts with no rule. For baba: q0q_0 reads bb and halts.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Define Turing machine and its roles.

Answer

Definition. A Turing machine is an abstract computing device made of a finite control, an infinite tape divided into cells, and a read/write head. In each move, depending on the current state and the scanned symbol, it writes a symbol, moves the head one cell left or right, and changes state. Formally it is a 7-tuple

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

where QQ is the set of states, Σ\Sigma the input alphabet, Γ⊇Σ\Gamma\supseteq\Sigma the tape alphabet, δ:Q×Γ→Q×Γ×{L,R}\delta:Q\times\Gamma\to Q\times\Gamma\times\{L,R\} the transition function, q0q_0 the start state, B∈Γ∖ΣB\in\Gamma\setminus\Sigma the blank, and F⊆QF\subseteq Q the accepting states.

Roles of the Turing machine

  1. Language acceptor (recogniser). A TM accepts exactly the recursively enumerable languages. If the string belongs to the language the machine halts in an accepting state. Otherwise it halts and rejects or runs forever.
  2. Language decider. A TM that halts on every input decides a recursive language, so it can answer yes/no problems.
  3. Computer of functions. The TM can compute any function that is computable. For example, it can compute addition, subtraction and multiplication in unary, by leaving the result on the tape. Such functions are called Turing-computable (partial recursive).
  4. Model of an algorithm. By the Church-Turing thesis, anything that can be computed by an algorithm can be computed by a TM, so the TM gives a precise definition of "algorithm" and "computable".
  5. Basis for decidability. It is used to show that some problems, such as the halting problem, are undecidable.
  6. Basis for complexity theory. Time and space are measured as the number of moves and tape cells used, which defines the classes PP, NPNP and PSPACEPSPACE.
  7. Universal machine. A universal TM simulates any other TM, which is the model of the stored-program computer.
  8. Generator (enumerator). A TM can list all the strings of a language.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Explain about the complexity classes p, NP and NP-Complete.

Answer

Complexity classes group decision problems by the resources (time and space) a Turing machine needs to solve them.

Class P. The set of decision problems solvable by a deterministic Turing machine in polynomial time, P=⋃kDTIME(nk)P=\bigcup_k\text{DTIME}(n^k). These are regarded as tractable. Examples are sorting, shortest path, searching, primality testing, and checking whether a graph is connected.

Class NP. The set of decision problems solvable by a nondeterministic Turing machine in polynomial time, NP=⋃kNTIME(nk)NP=\bigcup_k\text{NTIME}(n^k). Equivalently, a "yes" answer has a certificate (a proof) that a deterministic machine can verify in polynomial time. Examples are Satisfiability, Hamiltonian Circuit, Clique, Vertex Cover, Subset Sum and the decision form of the Travelling Salesman Problem. Clearly P⊆NPP\subseteq NP, and whether P=NPP=NP is still an open problem.

NP-hard. A problem HH is NP-hard if every problem in NP reduces to HH in polynomial time (L≤pHL\le_pH for all L∈NPL\in NP).

NP-complete. A problem CC is NP-complete if

  1. C∈NPC\in NP, and
  2. CC is NP-hard.

NP-complete problems are the hardest problems in NP. Cook's theorem (1971) shows that SAT is NP-complete, and other problems are shown to be NP-complete by reducing a known NP-complete problem to them in polynomial time: SAT≤p3-SAT≤pClique≤pVertex Cover≤p…\text{SAT}\le_p3\text{-SAT}\le_p\text{Clique}\le_p\text{Vertex Cover}\le_p\dots. A polynomial-time algorithm for any one NP-complete problem would imply P=NPP=NP.

Relation. P⊆NP⊆PSPACEP\subseteq NP\subseteq PSPACE. If P≠NPP\ne NP (the widely held belief), then NP-complete problems lie in NP∖PNP\setminus P. If P=NPP=NP, then every problem in NP (other than the trivial ones) is NP-complete. NP-hard problems need not be in NP (the halting problem is an example).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2079 · 5 marks

Write short notes ( Any two ) : a) Big Oh, Big Omega and Big Theta b) Tractable and Intractable Problems c) Chomsky Hierarchy

Answer

(a) Big Oh, Big Omega and Big Theta

These notations describe the growth rate of an algorithm's running time f(n)f(n) for large nn.

  • Big Oh (upper bound). f(n)=O(g(n))f(n)=O(g(n)) if there exist constants c>0c>0 and n0n_0 such that f(n)≤c g(n)f(n)\le c\,g(n) for all n≥n0n\ge n_0. It gives the worst-case bound. For example, 3n2+5n=O(n2)3n^2+5n=O(n^2) since 3n2+5n≤4n23n^2+5n\le4n^2 for n≥5n\ge5.
  • Big Omega (lower bound). f(n)=Ω(g(n))f(n)=\Omega(g(n)) if there exist c>0c>0 and n0n_0 such that f(n)≥c g(n)f(n)\ge c\,g(n) for all n≥n0n\ge n_0. For example, 3n2+5n=Ω(n2)3n^2+5n=\Omega(n^2).
  • Big Theta (tight bound). f(n)=Θ(g(n))f(n)=\Theta(g(n)) if there are constants c1,c2>0c_1,c_2>0 and n0n_0 such that c1g(n)≤f(n)≤c2g(n)c_1g(n)\le f(n)\le c_2g(n) for all n≥n0n\ge n_0. This is equivalent to f=O(g)f=O(g) and f=Ω(g)f=\Omega(g). So 3n2+5n=Θ(n2)3n^2+5n=\Theta(n^2).

(b) Tractable and intractable problems

A problem is tractable if it has an algorithm that runs in polynomial time O(nk)O(n^k) in the input size; these problems are in the class PP and are solvable in practice for large inputs (sorting, shortest path, matrix multiplication). A problem is intractable if the best known algorithms need super-polynomial (typically exponential) time, such as O(2n)O(2^n) or O(n!)O(n!), which makes them infeasible for large nn (Travelling Salesman, Hamiltonian Circuit, SAT, Knapsack decision). Most NP-complete problems are considered intractable unless P=NPP=NP. Some problems, like the halting problem, are even undecidable, which is a stronger form of unsolvability than intractability.

(c) Chomsky hierarchy

It classifies grammars into four nested types by the form of their productions: Type 3 regular (A→aB∣aA\to aB\mid a), recognised by finite automata; Type 2 context-free (A→γA\to\gamma), recognised by pushdown automata; Type 1 context-sensitive (αAβ→αγβ\alpha A\beta\to\alpha\gamma\beta), recognised by linear-bounded automata; Type 0 unrestricted (α→β\alpha\to\beta), recognised by Turing machines. Then Regular⊂CFL⊂CSL⊂RE\text{Regular}\subset\text{CFL}\subset\text{CSL}\subset\text{RE}. For example, a∗b∗a^*b^* is regular, anbna^nb^n is context-free but not regular, and anbncna^nb^nc^n is context-sensitive but not context-free.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 10 marks

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.

Answer

How a TM accepts a string. The string is written on the tape, with blanks elsewhere, and the head starts on its first symbol in the start state q0q_0. The machine follows δ\delta. The string is accepted if the machine enters an accepting (final) state, at which point it halts. It is rejected if the machine halts in a non-final state, or reaches a configuration with no defined move. The language of the TM is the set of accepted strings.

Design. The language L={a01a, a10a, a0101a}L=\{a01a,\ a10a,\ a0101a\} is finite, so the TM reads the string left to right and checks it against a tree of the three strings. It never writes anything new (it rewrites the scanned symbol) and always moves right. After the final aa it checks for a blank so that nothing extra follows.

M=({q0,…,q8,qf},{0,1,a},{0,1,a,B},δ,q0,B,{qf})M=(\{q_0,\dots,q_8,q_f\},\{0,1,a\},\{0,1,a,B\},\delta,q_0,B,\{q_f\})

Transition table ((next state, write, move)\text{(next state, write, move)}, blank cell means no move)

State0011aaBB
q0q_0,,(q1,a,R)(q_1,a,R),
q1q_1(q2,0,R)(q_2,0,R)(q5,1,R)(q_5,1,R),,
q2q_2 (read a0a0),(q3,1,R)(q_3,1,R),,
q3q_3 (read a01a01)(q6,0,R)(q_6,0,R),(q4,a,R)(q_4,a,R),
q4q_4 (end aa read),,,(qf,B,R)(q_f,B,R)
q5q_5 (read a1a1)(q8,0,R)(q_8,0,R),,,
q6q_6 (read a010a010),(q7,1,R)(q_7,1,R),,
q7q_7 (read a0101a0101),,(q4,a,R)(q_4,a,R),
q8q_8 (read a10a10),,(q4,a,R)(q_4,a,R),
qfq_f,,,,

Transition diagram (edges labelled read/write,move\text{read}/\text{write},\text{move})

q0 --a/a,R--> q1 --0/0,R--> q2 --1/1,R--> q3 --a/a,R--> q4 --B/B,R--> (qf)
               |                            |              ^  ^
               |                            +--0/0,R--> q6 --1/1,R--> q7 --a/a,R--+
               |                                                                    |
               +--1/1,R--> q5 --0/0,R--> q8 --a/a,R---------------------------------+

(the arrows from q7q_7 and q8q_8 both lead to q4q_4).

The three accepted paths are

  • a01aa01a: q0→q1→q2→q3→q4→qfq_0\to q_1\to q_2\to q_3\to q_4\to q_f
  • a10aa10a: q0→q1→q5→q8→q4→qfq_0\to q_1\to q_5\to q_8\to q_4\to q_f
  • a0101aa0101a: q0→q1→q2→q3→q6→q7→q4→qfq_0\to q_1\to q_2\to q_3\to q_6\to q_7\to q_4\to q_f

Acceptance of a0101aa0101a (the head is on the symbol after the state):

q0a0101a⊢aq10101a⊢a0q2101a⊢a01q301a⊢a010q61a⊢a0101q7a⊢a0101aq4B⊢a0101aBqf\begin{aligned} q_0a0101a&\vdash aq_10101a\vdash a0q_2101a\vdash a01q_301a\vdash a010q_61a\\ &\vdash a0101q_7a\vdash a0101aq_4B\vdash a0101aBq_f \end{aligned}

The machine halts in the final state qfq_f, so a0101aa0101a is accepted. Any other string, for example a011aa011a, reaches a configuration with no move (here q3q_3 reading 11) and is rejected.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 10 marks

Define context free grammar with an example. Explain with example, how context free grammar is converted to Chomsky Normal Form.

Answer

Context-free grammar (CFG). A CFG is a 4-tuple G=(V,T,P,S)G=(V,T,P,S) where VV is a finite set of variables, TT a finite set of terminals, PP a finite set of productions of the form A→αA\to\alpha with A∈VA\in V and α∈(V∪T)∗\alpha\in(V\cup T)^*, and S∈VS\in V the start symbol. The left side is always a single variable, which makes it "context free".

Example. GG: S→aSb∣abS\to aSb\mid ab generates {anbn∣n≥1}\{a^nb^n\mid n\ge1\}. A derivation is S⇒aSb⇒aabbS\Rightarrow aSb\Rightarrow aabb.

Chomsky Normal Form (CNF). Every production is either A→BCA\to BC (two variables) or A→aA\to a (one terminal). Every CFG whose language does not contain ε\varepsilon has an equivalent CNF grammar.

Conversion steps

  1. Add a new start symbol S0→SS_0\to S if SS appears on a right-hand side.
  2. Remove ε\varepsilon-productions: find nullable variables and add the versions of each production with them omitted.
  3. Remove unit productions A→BA\to B by copying the productions of BB to AA.
  4. Remove useless symbols (non-generating and unreachable).
  5. Replace each terminal aa occurring in a right-hand side of length ≥2\ge2 by a new variable CaC_a with Ca→aC_a\to a.
  6. Break right-hand sides of more than two variables: A→B1B2…BkA\to B_1B_2\dots B_k becomes A→B1D1, D1→B2D2, …A\to B_1D_1,\ D_1\to B_2D_2,\ \dots.

Example. Convert S→ASA∣aB,A→B∣S,B→b∣εS\to ASA\mid aB,\quad A\to B\mid S,\quad B\to b\mid\varepsilon.

Step 1. New start: S0→SS_0\to S.

Step 2. Nullable: BB (as B→εB\to\varepsilon), then AA (as A→BA\to B). SS is not nullable. Remove ε\varepsilon:

S0→SS→ASA∣SA∣AS∣S∣aB∣aA→B∣SB→b\begin{aligned} S_0&\to S\\ S&\to ASA\mid SA\mid AS\mid S\mid aB\mid a\\ A&\to B\mid S\\ B&\to b \end{aligned}

The rule S→SS\to S is dropped as trivial.

Step 3. Unit productions S0→SS_0\to S, A→BA\to B, A→SA\to S:

S0→ASA∣SA∣AS∣aB∣aS→ASA∣SA∣AS∣aB∣aA→b∣ASA∣SA∣AS∣aB∣aB→b\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 b\mid ASA\mid SA\mid AS\mid aB\mid a\\ B&\to b \end{aligned}

Step 5 and 6. Add U→aU\to a and D→SAD\to SA (to shorten ASAASA to ADAD):

S0→AD∣SA∣AS∣UB∣aS→AD∣SA∣AS∣UB∣aA→b∣AD∣SA∣AS∣UB∣aB→bD→SAU→a\begin{aligned} S_0&\to AD\mid SA\mid AS\mid UB\mid a\\ S&\to AD\mid SA\mid AS\mid UB\mid a\\ A&\to b\mid AD\mid SA\mid AS\mid UB\mid a\\ B&\to b\\ D&\to SA\\ U&\to a \end{aligned}

All productions are now of the form X→YZX\to YZ or X→tX\to t, so the grammar is in CNF.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

Define string, substring, empty string, and empty language over alphabet {a,b}.

Answer

Let the alphabet be Σ={a,b}\Sigma=\{a,b\}, a finite non-empty set of symbols.

String. A finite sequence of symbols chosen from Σ\Sigma, for example aa, abab, abbaabba, bbbbbb. The number of symbols is its length, ∣abba∣=4|abba|=4. The set of all strings over Σ\Sigma is Σ∗={ε,a,b,aa,ab,ba,bb,aaa,… }\Sigma^*=\{\varepsilon,a,b,aa,ab,ba,bb,aaa,\dots\}.

Substring. A string yy is a substring of ww if w=xyzw=xyz for some strings x,zx,z, that is, yy occurs as a contiguous block in ww. The substrings of abaaba are ε, a, b, ab, ba, aba\varepsilon,\ a,\ b,\ ab,\ ba,\ aba. The string aaaa is not a substring of abaaba (it is only a subsequence). A prefix is a substring at the start (ε,a,ab,aba\varepsilon,a,ab,aba) and a suffix is a substring at the end (ε,a,ba,aba\varepsilon,a,ba,aba).

Empty string. The string with no symbols, written ε\varepsilon (or λ\lambda), with ∣ε∣=0|\varepsilon|=0. It is the identity for concatenation: εw=wε=w\varepsilon w=w\varepsilon=w. It belongs to every Σ∗\Sigma^*.

Empty language. The language containing no strings at all, written ∅\emptyset or {}\{\}. It is different from the language {ε}\{\varepsilon\}, which contains exactly one string, the empty string. Over {a,b}\{a,b\}, ∅\emptyset has 00 strings, whereas {ε}\{\varepsilon\} has 11. For any language LL, L∅=∅L=∅L\emptyset=\emptyset L=\emptyset and L{ε}=LL\{\varepsilon\}=L.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

Design a DFA that accepts single line and multi-line comments of the C-Language.

Answer

Comment forms in C

  • Single line: starts with // and continues to the end of the line (newline).
  • Multi-line: starts with /* and ends at the first */. It may contain newlines, and the body can contain * and / characters.

The DFA accepts a string if it is exactly one complete comment. Input classes: /, *, n (newline) and o (any other character).

States

  • q0q_0: start.
  • q1q_1: read /.
  • q2q_2: inside a single-line comment.
  • q3q_3: inside a multi-line comment.
  • q4q_4: inside a multi-line comment, just read *.
  • q5q_5: read the closing */ (final).
  • q6q_6: the newline ended a single-line comment (final).
  • dd: dead (trap) state.

Transition table

State/*no
→q0\to q_0q1q_1dddddd
q1q_1q2q_2q3q_3dddd
q2q_2q2q_2q2q_2q6q_6q2q_2
q3q_3q3q_3q4q_4q3q_3q3q_3
q4q_4q5q_5q4q_4q3q_3q3q_3
∗q5*q_5dddddddd
∗q6*q_6dddddddd
dddddddddd

Final states: {q5,q6}\{q_5,q_6\}.

Diagram

q0 --/--> q1 --/--> q2 (loop on / * o) --n--> ((q6))
          |
          +--*--> q3 (loop on / n o) --*--> q4 (loop on *) --/--> ((q5))
                   ^                         |
                   +-------- o, n -----------+

Tests.

  • /* hi **/: q0→q1→q3q_0\to q_1\to q_3, loops on the text, then * to q4q_4, * stays q4q_4, / to q5q_5. Accepted.
  • // x*/ y followed by a newline: q0→q1→q2q_0\to q_1\to q_2, stays in q2q_2 on all characters until the newline moves it to q6q_6. Accepted.
  • /* a / : ends in q3q_3, not final, rejected.
  • /*/: q0→q1→q3→q3q_0\to q_1\to q_3\to q_3 (the / loops in q3q_3), not final, so correctly rejected as an unterminated comment.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

Write regular expression over {a,b} that represents a. Strings having exactly two a’s and atleast two b’s. b. Strings having an even number of a’s and each a followed by at least one b.

Answer

(a) Exactly two aa's and at least two bb's

The two aa's split the string into three blocks of bb's: bi a bj a bkb^{i}\,a\,b^{j}\,a\,b^{k} with i+j+k≥2i+j+k\ge2. The condition i+j+k≥2i+j+k\ge2 holds when some block has at least two bb's, or two different blocks each have at least one bb. Taking the union of all these cases:

bbb∗ a b∗ a b∗  +  b∗ a bbb∗ a b∗  +  b∗ a b∗ a bbb∗+  b+ a b+ a b∗  +  b+ a b∗ a b+  +  b∗ a b+ a b+\begin{aligned} &bbb^*\,a\,b^*\,a\,b^*\;+\;b^*\,a\,bbb^*\,a\,b^*\;+\;b^*\,a\,b^*\,a\,bbb^*\\ &+\;b^+\,a\,b^+\,a\,b^*\;+\;b^+\,a\,b^*\,a\,b^+\;+\;b^*\,a\,b^+\,a\,b^+ \end{aligned}

Check: ababbababb has the blocks i=0,j=1,k=2i=0,j=1,k=2, matched by the third term. baabbaab has only 2 bb's in blocks i=1,j=0,k=1i=1,j=0,k=1, matched by the fifth term. aabaab has only one bb and is not matched. abbaabba has aa's at the ends with 2 bb's in the middle, matched by the second term.

(b) Even number of aa's, each aa followed by at least one bb

Each aa must be immediately followed by bb, so the string consists only of the blocks bb and abab (so there is no aaaa and the string does not end with aa). It must contain an even number of abab blocks, so these come in pairs:

b∗ (ab b∗ ab b∗)∗b^*\,(ab\,b^*\,ab\,b^*)^*

Here each abab is followed by any number of further bb's, and the abab blocks are grouped in pairs, which gives an even count of aa's. It also includes ε\varepsilon and all strings of bb's (zero aa's). For example abababab, abbabbabbabb, bababbbababb are accepted, while abab (odd count), aabaab and baba are not.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

Using pumping lemma, prove that the language L = {a^ib^jc^k | j=i+k} is not regular.

Answer

Let L={aibjck∣j=i+k}L=\{a^ib^jc^k\mid j=i+k\}. Assume LL is regular. Then by the pumping lemma there is a pumping length pp such that every w∈Lw\in L with ∣w∣≥p|w|\ge p can be written w=xyzw=xyz with ∣y∣≥1|y|\ge1, ∣xy∣≤p|xy|\le p and xynz∈Lxy^nz\in L for all n≥0n\ge0.

Choice of string. Take i=pi=p and k=0k=0, so j=pj=p:

w=apbp∈L,∣w∣=2p≥p.w=a^pb^p\in L,\qquad |w|=2p\ge p.

Splitting. Since ∣xy∣≤p|xy|\le p, the substring xyxy lies entirely within the first pp symbols, which are all aa's. So y=amy=a^m for some 1≤m≤p1\le m\le p, and

x=ar,y=am,z=ap−r−mbp.x=a^{r},\quad y=a^m,\quad z=a^{p-r-m}b^p.

Pumping. For n=2n=2:

xy2z=ap+mbp.xy^2z=a^{p+m}b^p.

In this string i=p+mi=p+m, j=pj=p and k=0k=0, so i+k=p+m≠p=ji+k=p+m\ne p=j because m≥1m\ge1. Hence xy2z∉Lxy^2z\notin L. (Pumping down with n=0n=0 gives ap−mbpa^{p-m}b^p, which also fails.)

This contradicts the pumping lemma, so the assumption is false. Therefore L={aibjck∣j=i+k}L=\{a^ib^jc^k\mid j=i+k\} is not regular.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

Design a PDA over {x,y} which accepts strings defined by the language L = {x^ny^nxy | n>=0}. Show acceptance of xyxy.

Answer

Language. L={xnynxy∣n≥0}L=\{x^ny^nxy\mid n\ge0\} contains xyxy (n=0n=0), xyxyxyxy (n=1n=1), xxyyxyxxyyxy (n=2n=2), and so on. The idea is to push an XX for each leading xx, pop one for each yy, and then check that the remaining input is exactly xyxy. For n=0n=0 the very first xx is the one in the suffix, so on the first xx the PDA guesses nondeterministically whether it is part of xnx^n or the final xx.

P=({q0,q1,q2,q3},{x,y},{X,Z},δ,q0,Z,{q3})P=(\{q_0,q_1,q_2,q_3\},\{x,y\},\{X,Z\},\delta,q_0,Z,\{q_3\}), accepting by final state.

Transitions

  1. δ(q0,x,Z)={(q0,XZ), (q2,Z)}\delta(q_0,x,Z)=\{(q_0,XZ),\ (q_2,Z)\} (first xx: push, or guess it is the final xx when n=0n=0)
  2. δ(q0,x,X)={(q0,XX)}\delta(q_0,x,X)=\{(q_0,XX)\} (push more xx's)
  3. δ(q0,y,X)={(q1,ε)}\delta(q_0,y,X)=\{(q_1,\varepsilon)\} (first yy: pop)
  4. δ(q1,y,X)={(q1,ε)}\delta(q_1,y,X)=\{(q_1,\varepsilon)\} (pop for each further yy)
  5. δ(q1,x,Z)={(q2,Z)}\delta(q_1,x,Z)=\{(q_2,Z)\} (stack empty of XX's: the number of yy's equals the number of xx's, read the final xx)
  6. δ(q2,y,Z)={(q3,Z)}\delta(q_2,y,Z)=\{(q_3,Z)\} (read the final yy, accept)

Acceptance of xyxyxyxy (n=1n=1; the top of the stack is on the left):

(q0,xyxy,Z)⊢(q0,yxy,XZ)⊢(q1,xy,Z)⊢(q2,y,Z)⊢(q3,ε,Z)(q_0,xyxy,Z)\vdash(q_0,yxy,XZ)\vdash(q_1,xy,Z)\vdash(q_2,y,Z)\vdash(q_3,\varepsilon,Z)

The input is consumed in the final state q3q_3, so xyxyxyxy is accepted. The other branch from the first move, (q0,xyxy,Z)⊢(q2,yxy,Z)⊢(q3,xy,Z)(q_0,xyxy,Z)\vdash(q_2,yxy,Z)\vdash(q_3,xy,Z), has no further move with input left, so it dies and does not affect acceptance.

Other checks. xyxy: (q0,xy,Z)⊢(q2,y,Z)⊢(q3,ε,Z)(q_0,xy,Z)\vdash(q_2,y,Z)\vdash(q_3,\varepsilon,Z), accepted (n=0n=0). xxyxyxxyxy: after pushing XXXX and popping one XX, the next xx meets XX in q1q_1, where there is no move, so it is rejected. xyyxyxyyxy fails because q1q_1 has no move on yy with ZZ on top.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

Design a Turing machine that computes a function f(n)=0.

Answer

Idea. Represent the natural number nn in unary as 1n1^n on the tape (the rest blank BB). The function f(n)=0f(n)=0 must output the unary form of 00, i.e. an empty (all-blank) tape. So the machine simply erases every 11 and halts.

Machine. M=({q0,qf},{1},{1,B},δ,q0,B,{qf})M=(\{q_0,q_f\},\{1\},\{1,B\},\delta,q_0,B,\{q_f\}) with

StateSymbol readMove
q0q_011(q0, B, R)(q_0,\,B,\,R)
q0q_0BB(qf, B, R)(q_f,\,B,\,R)

δ(q0,1)=(q0,B,R),δ(q0,B)=(qf,B,R)\delta(q_0,1)=(q_0,B,R),\qquad \delta(q_0,B)=(q_f,B,R)

Working. In q0q_0 the head overwrites each 11 by BB and moves right. When it meets the first blank all the 11s have been erased, so it enters the halting state qfq_f.

Example, n=3n=3 (input 111111):

q0111⊢Bq011⊢BBq01⊢BBBq0B⊢BBBBqfq_0111\vdash Bq_011\vdash BBq_01\vdash BBBq_0B\vdash BBBBq_f

The tape is now blank, which is the unary representation of 00, so the machine computes f(3)=0f(3)=0. For n=0n=0 the head sees BB immediately and halts with a blank tape, so f(0)=0f(0)=0 as well.

(If the convention is that the output 00 is written as a single symbol, replace the move δ(q0,B)\delta(q_0,B) by (qf,0,R)(q_f,0,R) with 0∈Γ0\in\Gamma; the rest is unchanged.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

How abstract, decision and optimization problems are different from each other?

Answer

Abstract problem. An abstract problem QQ is a binary relation between a set II of problem instances and a set SS of problem solutions. It states what is to be solved without fixing any encoding, e.g. "given a graph GG and vertices u,vu,v, relate it to the paths from uu to vv". An instance may have many solutions.

Decision problem. An abstract problem whose answer is only YES/NO, i.e. Q:I→{0,1}Q:I\to\{0,1\}. Example: PATH\text{PATH}: "Is there a path from uu to vv in GG with at most kk edges?"

Optimization problem. An abstract problem in which every instance has many feasible solutions, each with a value, and we must find one with the best (minimum or maximum) value. Example: "Find a shortest path from uu to vv in GG."

BasisAbstract problemDecision problemOptimization problem
NatureGeneral relation instance →\to solutionSpecial case with answer in {YES,NO}\{\text{YES},\text{NO}\}Special case asking for best solution
OutputAny solution(s)One bitA solution and its optimal value
ExamplePaths in a graphIs there a path of length ≤k\le k?Shortest path
Use in complexityNeeds encoding to be analysedClasses P, NP are defined on theseConverted to decision form for study

Relation. Complexity theory studies decision problems because they fit languages: the set of YES-instances (suitably encoded as strings) is a language. An optimization problem is turned into a decision problem by adding a bound kk ("is there a solution of value ≤k\le k?"). If the decision version is hard, the optimization version is at least as hard.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

How is PDA to CFG conversion done? Consider a PDA that accepts by empty stack, P=({p,q},{0,1},{Z},δ,p,z); with δ defined as δ(p,0,z)=(p,0z), δ(p,0,0)=(p,00), δ(p,1,0)=(p,ε), δ(p,ε,z)=(q,ε), Now construct an equivalent CFG.

Answer

PDA to CFG conversion. Let P=(Q,Σ,Γ,δ,q0,Z0)P=(Q,\Sigma,\Gamma,\delta,q_0,Z_0) accept by empty stack. The grammar G=(V,Σ,R,S)G=(V,\Sigma,R,S) has variables

  • SS (start symbol), and
  • [pXq][pXq] for all p,q∈Qp,q\in Q, X∈ΓX\in\Gamma. The variable [pXq][pXq] generates exactly the strings ww that take PP from state pp with XX on top of the stack to state qq having popped XX (net effect) while reading ww.

Productions:

  1. For every state pp: S→[q0Z0p]S\to[q_0Z_0p].
  2. If δ(q,a,X)\delta(q,a,X) contains (r,Y1Y2…Yk)(r,Y_1Y_2\dots Y_k) with k≥1k\ge1 (a∈Σ∪{ε}a\in\Sigma\cup\{\varepsilon\}), then for all choices of states r1,…,rkr_1,\dots,r_k: [qXrk]→a [rY1r1][r1Y2r2]⋯[rk−1Ykrk][qXr_k]\to a\,[rY_1r_1][r_1Y_2r_2]\cdots[r_{k-1}Y_kr_k]
  3. If δ(q,a,X)\delta(q,a,X) contains (r,ε)(r,\varepsilon) then [qXr]→a[qXr]\to a.

Given PDA. P=({p,q},{0,1},{0,Z},δ,p,Z)P=(\{p,q\},\{0,1\},\{0,Z\},\delta,p,Z) (the stack alphabet must contain 00 and ZZ, since both are used), with

δ(p,0,Z)={(p,0Z)}, δ(p,0,0)={(p,00)}, δ(p,1,0)={(p,ε)}, δ(p,ε,Z)={(q,ε)}\delta(p,0,Z)=\{(p,0Z)\},\ \delta(p,0,0)=\{(p,00)\},\ \delta(p,1,0)=\{(p,\varepsilon)\},\ \delta(p,\varepsilon,Z)=\{(q,\varepsilon)\}

Productions.

Start: S→[pZp]∣[pZq]S\to[pZp]\mid[pZq].

From δ(p,0,Z)=(p,0Z)\delta(p,0,Z)=(p,0Z): [pZp]→0[p0p][pZp]∣0[p0q][qZp][pZp]\to0[p0p][pZp]\mid0[p0q][qZp] [pZq]→0[p0p][pZq]∣0[p0q][qZq][pZq]\to0[p0p][pZq]\mid0[p0q][qZq]

From δ(p,0,0)=(p,00)\delta(p,0,0)=(p,00): [p0p]→0[p0p][p0p]∣0[p0q][q0p][p0p]\to0[p0p][p0p]\mid0[p0q][q0p] [p0q]→0[p0p][p0q]∣0[p0q][q0q][p0q]\to0[p0p][p0q]\mid0[p0q][q0q]

From δ(p,1,0)=(p,ε)\delta(p,1,0)=(p,\varepsilon): [p0p]→1[p0p]\to1.

From δ(p,ε,Z)=(q,ε)\delta(p,\varepsilon,Z)=(q,\varepsilon): [pZq]→ε[pZq]\to\varepsilon.

Simplification. There are no moves out of qq, so [qZp],[qZq],[q0p],[q0q][qZp],[qZq],[q0p],[q0q] have no productions and are non-generating. Removing every production that uses them also makes [pZp][pZp] and [p0q][p0q] non-generating (each needs one of the dead variables or itself only). What remains:

S→[pZq]S\to[pZq] [pZq]→0[p0p][pZq]∣ε[pZq]\to0[p0p][pZq]\mid\varepsilon [p0p]→0[p0p][p0p]∣1[p0p]\to0[p0p][p0p]\mid1

Renaming A=[pZq]A=[pZq], B=[p0p]B=[p0p]:

S→A,A→0BA∣ε,B→0BB∣1S\to A,\qquad A\to0BA\mid\varepsilon,\qquad B\to0BB\mid1

Check. The string 00110011: S⇒A⇒0BA⇒00BBA⇒001BA⇒0011A⇒0011S\Rightarrow A\Rightarrow0BA\Rightarrow00BBA\Rightarrow001BA\Rightarrow0011A\Rightarrow0011. The PDA does it by pushing 00 twice, popping on each 11, then popping ZZ by the ε\varepsilon-move into qq (stack empty). Hence L(P)=L(G)L(P)=L(G).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2080 · 5 marks

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?

Answer

Meaning of "context free". In a context free grammar every production has the form A→αA\to\alpha with a single variable AA on the left. The variable can be replaced by α\alpha wherever it occurs, regardless of the symbols (the context) surrounding it. Contrast this with a context sensitive grammar, whose rules βAγ→βαγ\beta A\gamma\to\beta\alpha\gamma allow AA to be rewritten only when it appears between β\beta and γ\gamma.

Example. S→aSb∣εS\to aSb\mid\varepsilon generates {anbn:n≥0}\{a^nb^n:n\ge0\}. The symbol SS is rewritten in the same way whether it is the first symbol, in the middle of aaSbbaaSbb, or anywhere else:

S⇒aSb⇒aaSbb⇒aabbS\Rightarrow aSb\Rightarrow aaSbb\Rightarrow aabb

The step S→aSbS\to aSb never looks at the neighbours of SS; this independence from context is what "context free" means. Such grammars generate exactly the languages accepted by pushdown automata.

Need for a parse tree. A parse tree is a tree with root SS, internal nodes labelled by variables, and leaves labelled by terminals or ε\varepsilon; the children of a node are the right-hand side of the production applied there. It is needed because

  • it shows the structure of a string (how it is derived) independently of the order in which variables are expanded;
  • the yield (leaves read left to right) is the derived string;
  • it is used to detect ambiguity: two different parse trees for one string means the grammar is ambiguous;
  • compilers use it to check syntax and to attach meaning (code generation, expression evaluation, operator precedence).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 10 marks

Mention the transition function of PDA. List the two ways that PDA accepts the string. Convert the following CFG to PDA. S → AS | ε A → Ab | Bb | ab

Answer

Transition function of a PDA. A PDA is P=(Q,Σ,Γ,δ,q0,Z0,F)P=(Q,\Sigma,\Gamma,\delta,q_0,Z_0,F) and

δ:Q×(Σ∪{ε})×Γ→2 Q×Γ∗\delta:Q\times(\Sigma\cup\{\varepsilon\})\times\Gamma\to 2^{\,Q\times\Gamma^*}

(finite subsets). δ(q,a,X)={(p1,γ1),… }\delta(q,a,X)=\{(p_1,\gamma_1),\dots\} means: in state qq, reading input aa (a=εa=\varepsilon means no input consumed) with XX on top of the stack, the PDA may move to pip_i and replace XX by the string γi\gamma_i (top at the left).

Two ways of acceptance.

  1. Acceptance by final state: L(P)={w:(q0,w,Z0)⊢∗(q,ε,α), q∈F}L(P)=\{w:(q_0,w,Z_0)\vdash^*(q,\varepsilon,\alpha),\ q\in F\}, the input is consumed and the PDA is in a final state, whatever the stack holds.
  2. Acceptance by empty stack: N(P)={w:(q0,w,Z0)⊢∗(q,ε,ε)}N(P)=\{w:(q_0,w,Z_0)\vdash^*(q,\varepsilon,\varepsilon)\}, the input is consumed and the stack is empty. The two methods accept the same class of languages (the context free languages).

CFG to PDA. Grammar:

S→AS∣ε,A→Ab∣Bb∣abS\to AS\mid\varepsilon,\qquad A\to Ab\mid Bb\mid ab

(There is no production for BB, so BB can never be removed from the stack; the production A→BbA\to Bb is therefore never used in a successful run. It is still converted as written.)

Construct the one-state PDA accepting by empty stack: P=({q},{a,b},{S,A,B,a,b},δ,q,S)P=(\{q\},\{a,b\},\{S,A,B,a,b\},\delta,q,S) where

  • For each variable, an ε\varepsilon-move replaces it by the right side of one of its productions:
    • δ(q,ε,S)={(q,AS), (q,ε)}\delta(q,\varepsilon,S)=\{(q,AS),\ (q,\varepsilon)\}
    • δ(q,ε,A)={(q,Ab), (q,Bb), (q,ab)}\delta(q,\varepsilon,A)=\{(q,Ab),\ (q,Bb),\ (q,ab)\}
  • For each terminal, a matching move pops it:
    • δ(q,a,a)={(q,ε)}\delta(q,a,a)=\{(q,\varepsilon)\}
    • δ(q,b,b)={(q,ε)}\delta(q,b,b)=\{(q,\varepsilon)\}

Check. Input abbabb (S⇒AS⇒AbS⇒abbS⇒abbS\Rightarrow AS\Rightarrow AbS\Rightarrow abbS\Rightarrow abb):

(q,abb,S)⊢(q,abb,AS)⊢(q,abb,AbS)⊢(q,abb,abbS)⊢(q,bb,bbS)⊢(q,b,bS)⊢(q,ε,S)⊢(q,ε,ε)(q,abb,S)\vdash(q,abb,AS)\vdash(q,abb,AbS)\vdash(q,abb,abbS)\vdash(q,bb,bbS)\vdash(q,b,bS)\vdash(q,\varepsilon,S)\vdash(q,\varepsilon,\varepsilon)

The stack is empty after all input is read, so abbabb is accepted.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 10 marks

Define Turing machine as enumerators of strings of a language. Encode the Turing machine TM = ({q_0, q_1, q_2} , {a, b}, {a, b, B}, δ, q_2, B, F) with input w = ba and δ is defined as follows: δ(q_0, b) → (q_1, b, R), δ(q_1, a) → (q_2, a, R), δ(q_2, a) → (q_1, a, R), δ(q_2, b) → (q_2, b, L)

Answer

Turing machine as an enumerator. An enumerator is a Turing machine with a work tape and an extra output tape (printer). Started on a blank tape, it runs (possibly forever) and, from time to time, writes a string of the language on the output tape followed by a separator. The language L(E)L(E) of the enumerator is the set of all strings it ever prints. Strings may be printed in any order and may be repeated. A language is recursively enumerable exactly when some enumerator prints it:

  • Enumerator ⇒\Rightarrow TM: on input ww, run EE and accept as soon as ww is printed.
  • TM ⇒\Rightarrow Enumerator: for i=1,2,…i=1,2,\dots generate every string ss up to length ii and run MM on it for ii steps; print ss if MM accepts within the limit.

Encoding the TM. Use the standard binary code: each state qiq_i, each tape symbol XjX_j and each direction is written as a block of 00s, and the blocks are separated by single 11s.

ItemCodeItemCode
q0q_000aa00
q1q_10000bb0000
q2q_2000000BB000000
LL00RR0000

A move δ(qi,Xj)=(qk,Xl,Dm)\delta(q_i,X_j)=(q_k,X_l,D_m) is coded as 0i1 0j1 0k1 0l1 0m0^i1\,0^j1\,0^k1\,0^l1\,0^m (using the block lengths above). Hence:

MoveCode
δ(q0,b)=(q1,b,R)\delta(q_0,b)=(q_1,b,R)0 1 00 1 00 1 00 1 00=01001001001000\,1\,00\,1\,00\,1\,00\,1\,00=0100100100100
δ(q1,a)=(q2,a,R)\delta(q_1,a)=(q_2,a,R)00 1 0 1 000 1 0 1 00=001010001010000\,1\,0\,1\,000\,1\,0\,1\,00=0010100010100
δ(q2,a)=(q1,a,R)\delta(q_2,a)=(q_1,a,R)000 1 0 1 00 1 0 1 00=0001010010100000\,1\,0\,1\,00\,1\,0\,1\,00=0001010010100
δ(q2,b)=(q2,b,L)\delta(q_2,b)=(q_2,b,L)000 1 00 1 000 1 00 1 0=000100100010010000\,1\,00\,1\,000\,1\,00\,1\,0=000100100010010

The moves are written one after another separated by 1111, and the whole description is enclosed by 111111 at both ends:

⟨M⟩=111 0100100100100 11 0010100010100 11 0001010010100 11 000100100010010 111\langle M\rangle=111\,0100100100100\,11\,0010100010100\,11\,0001010010100\,11\,000100100010010\,111

The input w=baw=ba is coded symbol by symbol with 11 as separator: b 1 a=00 1 0=0010b\,1\,a=00\,1\,0=0010. The encoded pair for a universal machine is

⟨M,w⟩=⟨M⟩ 111 0010\langle M,w\rangle=\langle M\rangle\,111\,0010

(The start state is given as q2q_2 and FF is not listed, so these are supplied to the simulator as extra header fields.)

Behaviour on w=baw=ba. Starting in q2q_2 on bb: δ(q2,b)=(q2,b,L)\delta(q_2,b)=(q_2,b,L) moves the head left of the input onto a blank, and no move is defined for (q2,B)(q_2,B), so the machine halts without reaching a final state (the input is rejected).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

Does machine always refer to hardware? Justify. Define positive closure and Kleene closure.

Answer

Does machine always refer to hardware? No. In the theory of computation a "machine" is an abstract mathematical model of computation, not a physical device. Finite automata, pushdown automata and Turing machines are defined by states, symbols and transition rules and are studied on paper; they can be realised in hardware, in software, or not at all. Software artefacts such as a virtual machine (JVM), an interpreter or a compiler's lexical analyser (a finite automaton) are also "machines" even though they contain no new hardware. Their purpose is to reason about what can be computed, and how efficiently, independent of any particular computer.

Positive closure. For an alphabet Σ\Sigma, the positive closure is the set of all strings of length at least one:

Σ+=⋃i≥1Σi=Σ1∪Σ2∪⋯\Sigma^+=\bigcup_{i\ge1}\Sigma^i=\Sigma^1\cup\Sigma^2\cup\cdots

For a language LL: L+=L1∪L2∪L3∪⋯L^+=L^1\cup L^2\cup L^3\cup\cdots (one or more concatenations of strings of LL).

Kleene closure. It also includes the empty string:

Σ∗=⋃i≥0Σi={ε}∪Σ+,L∗=L0∪L1∪L2∪⋯ , L0={ε}\Sigma^*=\bigcup_{i\ge0}\Sigma^i=\{\varepsilon\}\cup\Sigma^+,\qquad L^*=L^0\cup L^1\cup L^2\cup\cdots,\ L^0=\{\varepsilon\}

Example: for Σ={0,1}\Sigma=\{0,1\}, Σ∗={ε,0,1,00,01,10,11,000,… }\Sigma^*=\{\varepsilon,0,1,00,01,10,11,000,\dots\} and Σ+=Σ∗−{ε}\Sigma^+=\Sigma^*-\{\varepsilon\}. For L={ab}L=\{ab\}: L∗={ε,ab,abab,… }L^*=\{\varepsilon,ab,abab,\dots\} and L+={ab,abab,… }L^+=\{ab,abab,\dots\}. Thus L∗=L+L^*=L^+ iff ε∈L\varepsilon\in L.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

What is undecidable problem? Discuss about Post Correspondence Problem.

Answer

Undecidable problem. A decision problem is decidable if some Turing machine (algorithm) halts on every input and answers YES/NO correctly. It is undecidable if no such algorithm exists: the language of its YES-instances is not recursive. A problem may be semi-decidable (a TM halts on YES but may loop on NO), e.g. the halting problem ATMA_{TM} and the question "is L(M)=∅L(M)=\emptyset?" is undecidable too. Other undecidable problems: whether a CFG is ambiguous, whether two CFGs generate the same language.

Post Correspondence Problem (PCP). An instance consists of two lists of strings over Σ\Sigma of equal length:

A=w1,w2,…,wk,B=x1,x2,…,xkA=w_1,w_2,\dots,w_k,\qquad B=x_1,x_2,\dots,x_k

The question: is there a sequence of indices i1,i2,…,imi_1,i_2,\dots,i_m (m≥1m\ge1, repetitions allowed) such that

wi1wi2⋯wim=xi1xi2⋯ximw_{i_1}w_{i_2}\cdots w_{i_m}=x_{i_1}x_{i_2}\cdots x_{i_m}

Example with a solution. A=(1, 10111, 10)A=(1,\ 10111,\ 10), B=(111, 10, 0)B=(111,\ 10,\ 0). Take the sequence 2,1,1,32,1,1,3:

w2w1w1w3=10111⋅1⋅1⋅10=101111110w_2w_1w_1w_3=10111\cdot1\cdot1\cdot10=101111110 x2x1x1x3=10⋅111⋅111⋅0=101111110x_2x_1x_1x_3=10\cdot111\cdot111\cdot0=101111110

The two strings are equal, so this instance has a solution.

Example without a solution. A=(01)A=(01), B=(0)B=(0): the AA-string is always longer than the BB-string.

Undecidability. There is no algorithm that decides, for every instance, whether a solution exists. Proof idea: first show that the Modified PCP (the match must start with pair 1) is undecidable by reducing the acceptance problem ATMA_{TM} to it. Given MM and ww, build pairs so that a partial match simulates the successive configurations of MM on ww, with the BB-string running one configuration ahead; a complete match exists iff MM reaches an accepting configuration. Then MPCP reduces to PCP. Since ATMA_{TM} is undecidable, so is PCP. PCP is the standard tool for proving that problems about CFGs (ambiguity, equivalence, emptiness of intersection) are undecidable.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

Define the language of a grammar. For the grammar S → 0S0 | 1 | ε, show the leftmost derivation for the string 00100 with its parse tree.

Answer

Language of a grammar. For a grammar G=(V,T,P,S)G=(V,T,P,S) the language is the set of all terminal strings derivable from the start symbol:

L(G)={w∈T∗:S⇒∗w}L(G)=\{w\in T^*:S\Rightarrow^*w\}

Grammar: S→0S0∣1∣εS\to0S0\mid1\mid\varepsilon. It generates {0n10n}∪{02n}\{0^n10^n\}\cup\{0^{2n}\} (n≥0n\ge0), i.e. even-length strings of 00s and palindromes with a single 11 in the centre.

Leftmost derivation of 0010000100 (there is only one variable at each step, so every step is leftmost):

S⇒0S0⇒00S00⇒00100S\Rightarrow0S0\Rightarrow00S00\Rightarrow00100

The first two steps use S→0S0S\to0S0, the last uses S→1S\to1.

Parse tree.

            S
         /  |  \
        0   S   0
          / | \
         0  S  0
            |
            1

Reading the leaves left to right gives the yield 0 0 1 0 0=001000\,0\,1\,0\,0=00100, so 00100∈L(G)00100\in L(G).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

Define ε-closure of a state. Differentiate between Moore and Mealy machine.

Answer

ε\varepsilon-closure. In an NFA with ε\varepsilon-transitions, ε-closure(q)\varepsilon\text{-closure}(q) is the set of all states reachable from qq by following zero or more ε\varepsilon-transitions. It always contains qq itself, and if pp is in it and r∈δ(p,ε)r\in\delta(p,\varepsilon) then rr is in it. For a set of states SS, ε-closure(S)=⋃q∈Sε-closure(q)\varepsilon\text{-closure}(S)=\bigcup_{q\in S}\varepsilon\text{-closure}(q).

Example: if δ(q0,ε)={q1}\delta(q_0,\varepsilon)=\{q_1\} and δ(q1,ε)={q2}\delta(q_1,\varepsilon)=\{q_2\} then ε-closure(q0)={q0,q1,q2}\varepsilon\text{-closure}(q_0)=\{q_0,q_1,q_2\}.

Moore vs Mealy machine. Both are finite state machines with output (no final states).

BasisMoore machineMealy machine
Output depends onPresent state onlyPresent state and present input
Definition(Q,Σ,Δ,δ,λ,q0)(Q,\Sigma,\Delta,\delta,\lambda,q_0) with λ:Q→Δ\lambda:Q\to\DeltaSame, but λ:Q×Σ→Δ\lambda:Q\times\Sigma\to\Delta
Output attached toStatesTransitions
Output length for input of length nnn+1n+1 (includes output of initial state)nn
Number of statesUsually moreUsually fewer
Output timingChanges only on a state change (after clock)Can change as soon as the input changes
Response to inputDelayed by one stepImmediate

Every Moore machine has an equivalent Mealy machine and vice versa (ignoring the extra first output of the Moore machine).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

Represent the following regular grammar to finite automata. S → aA | aB | ε A → aA | aS B → bB | ε

Answer

Method. The grammar is right-linear (each production is X→aYX\to aY or X→εX\to\varepsilon). Build an NFA whose states are the variables:

  • X→aYX\to aY gives the transition δ(X,a)∋Y\delta(X,a)\ni Y;
  • X→εX\to\varepsilon makes XX a final state;
  • the start state is SS.

Grammar: S→aA∣aB∣ε, A→aA∣aS, B→bB∣εS\to aA\mid aB\mid\varepsilon,\ A\to aA\mid aS,\ B\to bB\mid\varepsilon.

NFA M=({S,A,B},{a,b},δ,S,{S,B})M=(\{S,A,B\},\{a,b\},\delta,S,\{S,B\}):

Stateaabb
→∗S\to *S{A,B}\{A,B\}∅\emptyset
AA{A,S}\{A,S\}∅\emptyset
∗B*B∅\emptyset{B}\{B\}

(∗* marks final states: SS and BB.)

Transitions as edges: S→aAS\xrightarrow{a}A, S→aBS\xrightarrow{a}B, A→aAA\xrightarrow{a}A, A→aSA\xrightarrow{a}S, B→bBB\xrightarrow{b}B.

Equivalent DFA (subset construction): start {S}\{S\}.

Stateaabb
→∗{S}\to*\{S\}{A,B}\{A,B\}∅\emptyset
∗{A,B}*\{A,B\}{A,S}\{A,S\}{B}\{B\}
∗{A,S}*\{A,S\}{A,S,B}\{A,S,B\}∅\emptyset
∗{B}*\{B\}∅\emptyset{B}\{B\}
∗{A,S,B}*\{A,S,B\}{A,S,B}\{A,S,B\}{B}\{B\}

All subsets containing SS or BB are final; ∅\emptyset is the dead state. The language is (aaa∗)∗(ε+ab∗)(aaa^*)^*(\varepsilon+ab^*): for example aa, abab, aaaa, aabaab are accepted, while bb is rejected.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

Design the DFA that accepts binary string ending with “00” and show its extended transition function for the string 111000.

Answer

Design. Track how many trailing 00s the string read so far ends with (up to two).

  • q0q_0: start; the last symbol is not a 00 (or nothing read yet).
  • q1q_1: the string ends with exactly one 00.
  • q2q_2: the string ends with 0000 (final state).

M=({q0,q1,q2},{0,1},δ,q0,{q2})M=(\{q_0,q_1,q_2\},\{0,1\},\delta,q_0,\{q_2\})

State0011
→q0\to q_0q1q_1q0q_0
q1q_1q2q_2q0q_0
∗q2*q_2q2q_2q0q_0

A 11 always returns to q0q_0; a 00 moves q0→q1→q2q_0\to q_1\to q_2 and stays at q2q_2.

Extended transition function on 111000111000, using δ^(q,wa)=δ(δ^(q,w),a)\hat\delta(q,wa)=\delta(\hat\delta(q,w),a):

δ^(q0,ε)=q0\hat\delta(q_0,\varepsilon)=q_0 δ^(q0,1)=δ(q0,1)=q0\hat\delta(q_0,1)=\delta(q_0,1)=q_0 δ^(q0,11)=δ(q0,1)=q0\hat\delta(q_0,11)=\delta(q_0,1)=q_0 δ^(q0,111)=δ(q0,1)=q0\hat\delta(q_0,111)=\delta(q_0,1)=q_0 δ^(q0,1110)=δ(q0,0)=q1\hat\delta(q_0,1110)=\delta(q_0,0)=q_1 δ^(q0,11100)=δ(q1,0)=q2\hat\delta(q_0,11100)=\delta(q_1,0)=q_2 δ^(q0,111000)=δ(q2,0)=q2\hat\delta(q_0,111000)=\delta(q_2,0)=q_2

Since q2∈Fq_2\in F, the string 111000111000 is accepted.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

Convert the following grammar to CNF. S → AAB, A → aA | ε, B → ab | a

Answer

Grammar: S→AAB, A→aA∣ε, B→ab∣aS\to AAB,\ A\to aA\mid\varepsilon,\ B\to ab\mid a.

Step 1: remove ε\varepsilon-productions. Only AA is nullable. Replace each production containing AA by all versions with and without AA:

  • S→AAB∣AB∣BS\to AAB\mid AB\mid B (dropping the first AA, the second AA, or both)
  • A→aA∣aA\to aA\mid a
  • B→ab∣aB\to ab\mid a

Step 2: remove unit productions. S→BS\to B is replaced by the productions of BB: S→ab∣aS\to ab\mid a.

S→AAB∣AB∣ab∣a,A→aA∣a,B→ab∣aS\to AAB\mid AB\mid ab\mid a,\qquad A\to aA\mid a,\qquad B\to ab\mid a

(No useless symbols; ε∉L\varepsilon\notin L since BB is never empty.)

Step 3: terminals in long bodies. Introduce Ca→aC_a\to a, Cb→bC_b\to b and replace a,ba,b in bodies of length ≥2\ge2:

S→AAB∣AB∣CaCb∣aS\to AAB\mid AB\mid C_aC_b\mid a A→CaA∣a,B→CaCb∣aA\to C_aA\mid a,\qquad B\to C_aC_b\mid a

Step 4: break bodies longer than 2. S→AABS\to AAB becomes S→ADS\to AD, D→ABD\to AB.

Chomsky Normal Form:

S→AD∣AB∣CaCb∣aS\to AD\mid AB\mid C_aC_b\mid a D→ABD\to AB A→CaA∣aA\to C_aA\mid a B→CaCb∣aB\to C_aC_b\mid a Ca→a,Cb→bC_a\to a,\qquad C_b\to b

Every production is of the form X→YZX\to YZ or X→aX\to a.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

  1. For the following Turing Machine, test whether the string “( ) ( )” is accepted or rejected and represent it in transition diagram. [5] | State | ( | ) | X | Y | B | |---|---|---|---|---|---| | q0 | (q1, X, R) | – | – | (q0, Y, R) | (q3, B, R) | | q1 | (q1, (, R) | (q2, Y, L) | – | (q1, Y, R) | – | | q2 | (q2, (, L) | – | (q0, X, R) | (q2, Y, L) | – | | q3 | – | – | – | (q3, Y, R) | (q4, B, R) | | q4 | – | – | – | – | – |

Answer

Reading the machine. From the table (start state q0q_0, q4q_4 has no moves and is the accepting state):

  • q0q_0: on ( write XX, go to q1q_1, move R; on YY skip right; on BB go to q3q_3 (all brackets matched).
  • q1q_1: skip right over ( and YY; on ) write YY, go to q2q_2, move L.
  • q2q_2: move left over ( and YY; on XX go to q0q_0, move R.
  • q3q_3: skip YYs; on BB go to q4q_4 (accept).

This marks each ( with XX and the first following unmatched ) with YY: it accepts balanced parentheses.

Test on ( ) ( ). Tape: ( ) ( ) B. Instantaneous descriptions (the state is written just before the scanned symbol):

q0( )( )⊢Xq1)( )⊢q2XY( )⊢Xq0Y( )⊢XYq0( )q_0(\,)(\,)\vdash Xq_1)(\,)\vdash q_2XY(\,)\vdash Xq_0Y(\,)\vdash XYq_0(\,) ⊢XYXq1)⊢XYq2XY⊢XYXq0Y⊢XYXYq0B⊢XYXYBq3B⊢XYXYBBq4\vdash XYXq_1)\vdash XYq_2XY\vdash XYXq_0Y\vdash XYXYq_0B\vdash XYXYBq_3B\vdash XYXYBBq_4

The machine reaches q4q_4 with no further moves, so the string ( ) ( ) is accepted.

Transition diagram (edges labelled read / write, move):

q0 --( / X,R--> q1        q0 --Y / Y,R--> q0        q0 --B / B,R--> q3
q1 --( / (,R--> q1        q1 --Y / Y,R--> q1        q1 --) / Y,L--> q2
q2 --( / (,L--> q2        q2 --Y / Y,L--> q2        q2 --X / X,R--> q0
q3 --Y / Y,R--> q3        q3 --B / B,R--> q4        q4 : accepting (no moves)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2081 · 5 marks

Differentiate between Class P and Class NP problem. Mention the transition function of DFA, NFA, and ε-NFA.

Answer

Class P vs Class NP.

BasisClass PClass NP
MeaningDecision problems solvable by a deterministic TM in polynomial timeDecision problems solvable by a non-deterministic TM in polynomial time, equivalently, a proposed solution (certificate) can be verified in polynomial time
TimeO(nk)O(n^k) for some constant kkNon-deterministic O(nk)O(n^k); verification in O(nk)O(n^k)
ExamplesSorting, shortest path, searching, 2-SATSatisfiability (SAT), Hamiltonian cycle, subset sum, clique
RelationP⊆NPP\subseteq NPWhether P=NPP=NP is an open problem
PracticalityConsidered tractableBest known algorithms for hard members are exponential

Transition functions.

  • DFA: δ:Q×Σ→Q\delta:Q\times\Sigma\to Q (exactly one next state).
  • NFA: δ:Q×Σ→2Q\delta:Q\times\Sigma\to2^{Q} (a set of next states, possibly empty).
  • ε\varepsilon-NFA: δ:Q×(Σ∪{ε})→2Q\delta:Q\times(\Sigma\cup\{\varepsilon\})\to2^{Q} (as the NFA, but moves on ε\varepsilon without reading input are allowed).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 10 marks

Define yield in parse tree with example. Convert the following grammar to CNF: S→ASB∣ϵA→0AS∣0B→SIS∣A\begin{aligned} S &\rightarrow ASB \mid \epsilon\\ A &\rightarrow 0AS \mid 0\\ B &\rightarrow SIS \mid A \end{aligned}

Answer

Yield of a parse tree. The yield is the string obtained by concatenating the labels of the leaves of the tree from left to right. When the root is the start symbol and all leaves are terminals (or ε\varepsilon), the yield is a string of the language.

Example: grammar S→aSb∣εS\to aSb\mid\varepsilon.

        S
      / | \
     a  S  b
      / | \
     a  S  b
        |
        ε

The leaves read left to right are a,a,ε,b,ba,a,\varepsilon,b,b, so the yield is aabbaabb.

Conversion to CNF. Grammar (taking II as a terminal symbol):

S→ASB∣ε,A→0AS∣0,B→SIS∣AS\to ASB\mid\varepsilon,\qquad A\to0AS\mid0,\qquad B\to SIS\mid A

Step 1: remove ε\varepsilon-productions. The only nullable variable is SS. Add the versions without SS and delete S→εS\to\varepsilon:

S→ASB∣ABS\to ASB\mid AB A→0AS∣0A∣0A\to0AS\mid0A\mid0 B→SIS∣IS∣SI∣I∣AB\to SIS\mid IS\mid SI\mid I\mid A

(The language of the CNF grammar is L(G)−{ε}L(G)-\{\varepsilon\}.)

Step 2: remove unit productions. The only one is B→AB\to A; replace it by the productions of AA:

B→SIS∣IS∣SI∣I∣0AS∣0A∣0B\to SIS\mid IS\mid SI\mid I\mid0AS\mid0A\mid0

Step 3: replace terminals in bodies of length ≥2\ge2. Add C0→0C_0\to0 and CI→IC_I\to I:

S→ASB∣ABS\to ASB\mid AB A→C0AS∣C0A∣0A\to C_0AS\mid C_0A\mid0 B→SCIS∣CIS∣SCI∣I∣C0AS∣C0A∣0B\to SC_IS\mid C_IS\mid SC_I\mid I\mid C_0AS\mid C_0A\mid0

Step 4: shorten bodies longer than 2. Add D1→SBD_1\to SB, D2→ASD_2\to AS, D3→CISD_3\to C_IS:

CNF grammar:

S→AD1∣ABS\to AD_1\mid AB A→C0D2∣C0A∣0A\to C_0D_2\mid C_0A\mid0 B→SD3∣CIS∣SCI∣I∣C0D2∣C0A∣0B\to SD_3\mid C_IS\mid SC_I\mid I\mid C_0D_2\mid C_0A\mid0 D1→SB,D2→AS,D3→CISD_1\to SB,\quad D_2\to AS,\quad D_3\to C_IS C0→0,CI→IC_0\to0,\quad C_I\to I

Every production now has the form X→YZX\to YZ or X→aX\to a.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 10 marks

Distinguish between multi-tape and multi-track Turing Machine. Design a TM that accepts a string over the alphabet Σ={a,b}\Sigma=\{a,b\}, such that L={anbn∣n>0}L=\{a^n b^n \mid n>0\}.

Answer

Multi-tape vs multi-track Turing machine.

BasisMulti-tape TMMulti-track TM
StructureSeveral separate tapes, each with its own independent headOne tape divided into several tracks; a single head reads all tracks at one position
Tape symbolOne symbol per tape; the transition reads kk symbols (one from each tape)One composite symbol [x1,x2,…,xk][x_1,x_2,\dots,x_k]
Head movementEach head may move independentlyAll tracks move together
Transitionδ:Q×Γk→Q×Γk×{L,R,S}k\delta:Q\times\Gamma^k\to Q\times\Gamma^k\times\{L,R,S\}^kδ:Q×Γk→Q×Γk×{L,R}\delta:Q\times\Gamma^k\to Q\times\Gamma^k\times\{L,R\} (same as a one-tape TM over tuple alphabet)
PowerSame as the standard TM (simulated with O(t2)O(t^2) slowdown)Same as the standard TM
UseFaster algorithms (copying, comparing)Marking, e.g. checking primes, storing flags

TM for L={anbn∣n>0}L=\{a^nb^n\mid n>0\}. Idea: repeatedly mark the leftmost unmarked aa by XX, then the leftmost unmarked bb by YY. Accept when no aa remains and no bb remains.

M=({q0,q1,q2,q3,q4},{a,b},{a,b,X,Y,B},δ,q0,B,{q4})M=(\{q_0,q_1,q_2,q_3,q_4\},\{a,b\},\{a,b,X,Y,B\},\delta,q_0,B,\{q_4\})

StateaabbXXYYBB
q0q_0(q1,X,R)(q_1,X,R)––(q3,Y,R)(q_3,Y,R)–
q1q_1(q1,a,R)(q_1,a,R)(q2,Y,L)(q_2,Y,L)–(q1,Y,R)(q_1,Y,R)–
q2q_2(q2,a,L)(q_2,a,L)–(q0,X,R)(q_0,X,R)(q2,Y,L)(q_2,Y,L)–
q3q_3–––(q3,Y,R)(q_3,Y,R)(q4,B,R)(q_4,B,R)
q4q_4–––––
  • q0q_0: replace the leftmost aa by XX and go right (q1q_1). If instead a YY is met, all aas are matched; go to q3q_3 to check that only YYs remain.
  • q1q_1: move right over aas and YYs to the first bb; change it to YY and go left (q2q_2).
  • q2q_2: move left to the last XX, step right and repeat from q0q_0.
  • q3q_3: skip YYs; on blank, accept (q4q_4).

Trace for aabbaabb:

q0aabb⊢Xq1abb⊢Xaq1bb⊢Xq2aYb⊢q2XaYb⊢Xq0aYb⊢XXq1Yb⊢XXYq1b⊢XXq2YY⊢Xq2XYY⊢XXq0YY⊢XXYq3Y⊢XXYYq3B⊢XXYYBq4q_0aabb\vdash Xq_1abb\vdash Xaq_1bb\vdash Xq_2aYb\vdash q_2XaYb\vdash Xq_0aYb\vdash XXq_1Yb\vdash XXYq_1b\vdash XXq_2YY\vdash Xq_2XYY\vdash XXq_0YY\vdash XXYq_3Y\vdash XXYYq_3B\vdash XXYYBq_4

Accepted. Strings such as aabaab (no move from q1q_1 on BB), abbabb (no move from q3q_3 on bb) and ε\varepsilon (no move from q0q_0 on BB) are rejected, so the machine accepts exactly {anbn:n>0}\{a^nb^n:n>0\}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Why do we need asymptotic notation? Discuss about alphabet and power of alphabet.

Answer

Why asymptotic notation? The exact running time of an algorithm depends on the machine, the language, the compiler and the input. We want a measure that

  • is independent of the machine and implementation details (constants and lower-order terms are ignored),
  • describes how the cost grows with the input size nn, which matters for large inputs,
  • lets algorithms be compared: an O(nlog⁡n)O(n\log n) algorithm eventually beats an O(n2)O(n^2) one,
  • gives upper, lower and tight bounds on best, average and worst cases.

The main notations are: f(n)=O(g(n))f(n)=O(g(n)) if f(n)≤c g(n)f(n)\le c\,g(n) for all n≥n0n\ge n_0 (upper bound); f(n)=Ω(g(n))f(n)=\Omega(g(n)) if f(n)≥c g(n)f(n)\ge c\,g(n) (lower bound); f(n)=Θ(g(n))f(n)=\Theta(g(n)) if both hold (tight bound). Example: 3n2+5n+2=Θ(n2)3n^2+5n+2=\Theta(n^2).

Alphabet. An alphabet Σ\Sigma is a finite, non-empty set of symbols. Examples: Σ={0,1}\Sigma=\{0,1\} (binary), Σ={a,…,z}\Sigma=\{a,\dots,z\}.

Power of an alphabet. Σk\Sigma^k is the set of all strings of length exactly kk over Σ\Sigma:

Σ0={ε},Σk={xa:x∈Σk−1,a∈Σ},∣Σk∣=∣Σ∣k\Sigma^0=\{\varepsilon\},\qquad \Sigma^k=\{xa:x\in\Sigma^{k-1},a\in\Sigma\},\qquad |\Sigma^k|=|\Sigma|^k

For Σ={0,1}\Sigma=\{0,1\}: Σ1={0,1}\Sigma^1=\{0,1\}, Σ2={00,01,10,11}\Sigma^2=\{00,01,10,11\}, Σ3\Sigma^3 has 88 strings. The union of all powers gives Σ∗=⋃k≥0Σk\Sigma^*=\bigcup_{k\ge0}\Sigma^k (Kleene closure) and Σ+=⋃k≥1Σk\Sigma^+=\bigcup_{k\ge1}\Sigma^k.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Define ambiguous grammar. How do you use parse tree to show the ambiguity of a grammar?

Answer

Ambiguous grammar. A context free grammar GG is ambiguous if some string w∈L(G)w\in L(G) has more than one parse tree. Equivalently, ww has more than one leftmost derivation (or more than one rightmost derivation). Otherwise the grammar is unambiguous.

Using parse trees to show ambiguity. To prove a grammar ambiguous, find one string and draw two different parse trees for it, both with root SS and the same yield.

Example: E→E+E∣E∗E∣idE\to E+E\mid E*E\mid id and the string id+id∗idid+id*id.

Tree 1 (the ++ is applied last, so the tree means id+(id∗id)id+(id*id)):

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

Tree 2 (the ∗* is applied last, so the tree means (id+id)∗id(id+id)*id):

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

Both trees have the yield id+id∗idid+id*id but different structure (and different meaning), so the grammar is ambiguous. Equivalent leftmost derivations: E⇒E+E⇒id+E⇒id+E∗E⇒⋯E\Rightarrow E+E\Rightarrow id+E\Rightarrow id+E*E\Rightarrow\cdots and E⇒E∗E⇒E+E∗E⇒⋯E\Rightarrow E*E\Rightarrow E+E*E\Rightarrow\cdots.

Ambiguity can often be removed by rewriting with precedence levels: E→E+T∣T, T→T∗F∣F, F→idE\to E+T\mid T,\ T\to T*F\mid F,\ F\to id, which gives only the first tree.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Given the non-deterministic Turing machine, TM = ({q0, q1, q2, qf}, {0, 1}, {0, 1, B}, δ, q0, B, {qf}) with the following transition rules, describe the language accepted by the given Turing machine: δ(q0,0)={(q0,1,R),(q1,1,R)};δ(q1,1)={(q2,0,L)};δ(q2,1)={(q0,1,R)};δ(q1,B)={(qf,B,R)}.\begin{array}{l}\delta(q_0,0)=\{(q_0,1,R),(q_1,1,R)\};\\\delta(q_1,1)=\{(q_2,0,L)\};\\\delta(q_2,1)=\{(q_0,1,R)\};\\\delta(q_1,B)=\{(q_f,B,R)\}.\end{array}

Answer

Analysis of the machine. Moves:

  • δ(q0,0)={(q0,1,R),(q1,1,R)}\delta(q_0,0)=\{(q_0,1,R),(q_1,1,R)\}: on a 00, write 11, move right, and guess whether to stay in q0q_0 or go to q1q_1.
  • δ(q1,1)=(q2,0,L)\delta(q_1,1)=(q_2,0,L): in q1q_1 a 11 is rewritten as 00 and the head steps left.
  • δ(q2,1)=(q0,1,R)\delta(q_2,1)=(q_0,1,R): the cell to the left holds the 11 just written by q0q_0; the head steps right again, onto the cell now holding 00, in state q0q_0.
  • δ(q1,B)=(qf,B,R)\delta(q_1,B)=(q_f,B,R): in q1q_1 on a blank, accept.

There is no move for q0q_0 on 11 or on BB, so a run dies there.

Behaviour.

  • A computation can only start with a 00: if the input begins with 11 (or is empty), q0q_0 has no move and the string is rejected.
  • Suppose q0q_0 is scanning a 00 (an original 00 or a rewritten one). If the next cell is 00, choose q0q_0 and continue. If the next cell is 11, choose q1q_1: the 11 is rewritten to 00, the head goes back and forward through q2q_2 and returns to q0q_0 scanning that new 00. So every 11 that follows a processed 00 is turned into a 00 and processing continues.
  • At the last symbol (which is, after the rewriting, scanned by q0q_0 as a 00), choose q1q_1; it now sees BB and the machine moves to qfq_f and accepts.

Hence every non-empty string that begins with 00 is accepted by some computation, e.g. 00, 0101, 011011, 01010101. Example 0101: q00 1⊢1q11⊢q21 0⊢1q00⊢11q1B⊢11Bqfq_00\,1\vdash1q_11\vdash q_21\,0\vdash1q_00\vdash11q_1B\vdash11Bq_f.

L(M)={ 0w∣w∈{0,1}∗ }=0(0+1)∗L(M)=\{\,0w\mid w\in\{0,1\}^*\,\}=0(0+1)^*

i.e. all binary strings that start with 00.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Given two DFAs M1 and M2 over the same alphabet Σ, design a third DFA M3 such that M3 will accept the string w ∈ Σ* if it is accepted by both M1 and M2.

Answer

Product construction (intersection of two DFAs). Let

M1=(Q1,Σ,δ1,q1,F1),M2=(Q2,Σ,δ2,q2,F2)M_1=(Q_1,\Sigma,\delta_1,q_1,F_1),\qquad M_2=(Q_2,\Sigma,\delta_2,q_2,F_2)

Define M3=(Q,Σ,δ,q0,F)M_3=(Q,\Sigma,\delta,q_0,F) where

  • Q=Q1×Q2Q=Q_1\times Q_2 (pairs of states),
  • q0=(q1,q2)q_0=(q_1,q_2),
  • δ((p,r),a)=(δ1(p,a), δ2(r,a))\delta((p,r),a)=(\delta_1(p,a),\ \delta_2(r,a)) for all (p,r)∈Q(p,r)\in Q, a∈Σa\in\Sigma,
  • F=F1×F2F=F_1\times F_2 (both components final).

M3M_3 runs M1M_1 and M2M_2 in parallel on the same input.

Proof of correctness. By induction on ∣w∣|w|, δ^((p,r),w)=(δ^1(p,w),δ^2(r,w))\hat\delta((p,r),w)=(\hat\delta_1(p,w),\hat\delta_2(r,w)). For w=εw=\varepsilon both sides are (p,r)(p,r). For w=xaw=xa:

δ^((p,r),xa)=δ(δ^((p,r),x),a)=δ((δ^1(p,x),δ^2(r,x)),a)=(δ^1(p,xa),δ^2(r,xa))\hat\delta((p,r),xa)=\delta(\hat\delta((p,r),x),a)=\delta((\hat\delta_1(p,x),\hat\delta_2(r,x)),a)=(\hat\delta_1(p,xa),\hat\delta_2(r,xa))

Therefore w∈L(M3)w\in L(M_3) iff δ^1(q1,w)∈F1\hat\delta_1(q_1,w)\in F_1 and δ^2(q2,w)∈F2\hat\delta_2(q_2,w)\in F_2 iff w∈L(M1)∩L(M2)w\in L(M_1)\cap L(M_2).

Example. M1M_1: even number of 00s ({e0,o0}\{e_0,o_0\}), M2M_2: even number of 11s ({e1,o1}\{e_1,o_1\}). M3M_3 has four states (e0,e1),(e0,o1),(o0,e1),(o0,o1)(e_0,e_1),(e_0,o_1),(o_0,e_1),(o_0,o_1); reading 00 toggles the first component, reading 11 the second; the start and only final state is (e0,e1)(e_0,e_1), so M3M_3 accepts strings with an even number of both 00s and 11s. The number of states is at most ∣Q1∣⋅∣Q2∣|Q_1|\cdot|Q_2|.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Differentiate between Moore and Mealy machines.

Answer

Moore vs Mealy machine. Both are finite automata that produce an output string, and neither has final states.

BasisMoore machineMealy machine
Formal definition(Q,Σ,Δ,δ,λ,q0)(Q,\Sigma,\Delta,\delta,\lambda,q_0), λ:Q→Δ\lambda:Q\to\Delta(Q,Σ,Δ,δ,λ,q0)(Q,\Sigma,\Delta,\delta,\lambda,q_0), λ:Q×Σ→Δ\lambda:Q\times\Sigma\to\Delta
Output depends onPresent state onlyPresent state and present input
Output is associated withStatesTransitions
Length of output for input of length nnn+1n+1nn
Number of statesGenerally moreGenerally fewer
Reaction to inputOutput appears one step later (after entering the state)Output appears immediately with the input
Initial outputProduces an output even before any inputNone
CircuitOutput is a function of state only: more stable, glitch-freeOutput may change with input in the middle of a cycle

Every Moore machine can be converted to an equivalent Mealy machine (give each transition the output of its target state) and every Mealy machine to an equivalent Moore machine (split states by output).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Design a DFA that accepts odd length binary strings.

Answer

Design. Only the parity of the length matters, so two states suffice.

  • qevenq_{even}: an even number of symbols read so far (start state, includes the empty string).
  • qoddq_{odd}: an odd number of symbols read (final state).

M=({qeven,qodd},{0,1},δ,qeven,{qodd})M=(\{q_{even},q_{odd}\},\{0,1\},\delta,q_{even},\{q_{odd}\})

State0011
→qeven\to q_{even}qoddq_{odd}qoddq_{odd}
∗qodd*q_{odd}qevenq_{even}qevenq_{even}

Every symbol, 00 or 11, toggles the state.

Check. 11: qeven→qoddq_{even}\to q_{odd} (accept). 0101: qeven→qodd→qevenq_{even}\to q_{odd}\to q_{even} (reject). 101101: →qodd→qeven→qodd\to q_{odd}\to q_{even}\to q_{odd} (accept). The empty string stays in qevenq_{even} (reject).

The language is L={w∈{0,1}∗:∣w∣ is odd}L=\{w\in\{0,1\}^*:|w|\text{ is odd}\}, with regular expression (0+1)((0+1)(0+1))∗(0+1)((0+1)(0+1))^*.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

How do you convert CFG to PDA? Explain.

Answer

CFG to PDA conversion. Every context free language is accepted by a PDA. Given a CFG G=(V,T,P,S)G=(V,T,P,S), build a PDA with a single state that accepts by empty stack and simulates a leftmost derivation on its stack:

P=({q},T,V∪T,δ,q,S)P=(\{q\},T,V\cup T,\delta,q,S)

where the transitions are:

  1. For each variable AA: δ(q,ε,A)={(q,β):A→β is a production of G}\delta(q,\varepsilon,A)=\{(q,\beta):A\to\beta\text{ is a production of }G\}, i.e. pop the variable and push the right-hand side of a chosen production (with its leftmost symbol on top).
  2. For each terminal aa: δ(q,a,a)={(q,ε)}\delta(q,a,a)=\{(q,\varepsilon)\}, i.e. if the top of the stack equals the next input symbol, pop it and consume the input.

The initial stack symbol is the start symbol SS.

Why it works. The stack always holds the unmatched part of a sentential form (the leftmost variable on top). Rule 1 performs one derivation step on the leftmost variable, rule 2 removes terminals that have been generated and match the input. The PDA empties its stack with the whole input read exactly when a derivation S⇒∗wS\Rightarrow^*w exists, so N(P)=L(G)N(P)=L(G).

Example. GG: S→aSb∣εS\to aSb\mid\varepsilon. The PDA:

δ(q,ε,S)={(q,aSb),(q,ε)},δ(q,a,a)={(q,ε)},δ(q,b,b)={(q,ε)}\delta(q,\varepsilon,S)=\{(q,aSb),(q,\varepsilon)\},\quad\delta(q,a,a)=\{(q,\varepsilon)\},\quad\delta(q,b,b)=\{(q,\varepsilon)\}

Input aabbaabb:

(q,aabb,S)⊢(q,aabb,aSb)⊢(q,abb,Sb)⊢(q,abb,aSbb)⊢(q,bb,Sbb)⊢(q,bb,bb)⊢(q,b,b)⊢(q,ε,ε)(q,aabb,S)\vdash(q,aabb,aSb)\vdash(q,abb,Sb)\vdash(q,abb,aSbb)\vdash(q,bb,Sbb)\vdash(q,bb,bb)\vdash(q,b,b)\vdash(q,\varepsilon,\varepsilon)

(the step (q,bb,Sbb)⊢(q,bb,bb)(q,bb,Sbb)\vdash(q,bb,bb) uses S→εS\to\varepsilon). The stack is empty, so aabbaabb is accepted.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Why do we need ε transition? Give the formal definition of PDA.

Answer

Need for ε\varepsilon-transitions. An ε\varepsilon-transition lets an automaton change state (and, in a PDA, change the stack) without reading any input symbol. It is needed because

  • it allows non-determinism by guessing: e.g. a PDA for palindromes wwRww^R must guess the middle of the string, which it does by an ε\varepsilon-move from the "push" phase to the "pop" phase;
  • the stack can be manipulated at the end of input (popping the bottom marker Z0Z_0 to empty the stack or to reach a final state), and variables can be expanded without consuming input in the CFG-to-PDA construction;
  • in ε\varepsilon-NFAs it makes constructions simple: union, concatenation and Kleene star of automata (Thompson's construction) and regular expression to automaton conversion;
  • it makes some machines smaller and easier to design.

For example, δ(q0,ε,Z0)={(q1,Z0)}\delta(q_0,\varepsilon,Z_0)=\{(q_1,Z_0)\} lets the PDA change phase without input.

Formal definition of a PDA. A pushdown automaton is a 7-tuple

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

where

  • QQ is a finite set of states,
  • Σ\Sigma is the finite input alphabet,
  • Γ\Gamma is the finite stack alphabet,
  • δ:Q×(Σ∪{ε})×Γ→2 Q×Γ∗\delta:Q\times(\Sigma\cup\{\varepsilon\})\times\Gamma\to2^{\,Q\times\Gamma^*} is the transition function (a finite set of pairs: new state and string pushed in place of the top symbol),
  • q0∈Qq_0\in Q is the start state,
  • Z0∈ΓZ_0\in\Gamma is the initial stack symbol,
  • F⊆QF\subseteq Q is the set of final states.

A move is written (q,aw,Xα)⊢(p,w,βα)(q,aw,X\alpha)\vdash(p,w,\beta\alpha) when (p,β)∈δ(q,a,X)(p,\beta)\in\delta(q,a,X). The language accepted by final state is L(P)={w:(q0,w,Z0)⊢∗(q,ε,γ),q∈F}L(P)=\{w:(q_0,w,Z_0)\vdash^*(q,\varepsilon,\gamma),q\in F\}, and by empty stack N(P)={w:(q0,w,Z0)⊢∗(q,ε,ε)}N(P)=\{w:(q_0,w,Z_0)\vdash^*(q,\varepsilon,\varepsilon)\}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2082 · 5 marks

Describe abstract, decision and optimization problems.

Answer

Abstract problem. An abstract problem QQ is a binary relation on a set II of problem instances and a set SS of problem solutions. It only states which solutions are acceptable for which instances, without saying how the instances are represented or how a solution is computed.

For example, the shortest-path problem maps an instance (a graph GG with two vertices uu and vv) to a solution (a path of fewest edges from uu to vv). One instance may have several valid solutions, since there may be several shortest paths. To solve an abstract problem on a computer, its instances are encoded as binary strings, which gives a concrete problem.

Decision problem. A decision problem is an abstract problem whose solution set is {0,1}\{0,1\} (yes or no). Each instance has exactly one answer, so the problem can be viewed as a function Q:I→{0,1}Q: I \to \{0,1\}.

Examples:

  • Is a given integer prime?
  • Does a graph GG contain a Hamiltonian cycle?
  • Given GG, uu, vv and an integer kk, is there a path from uu to vv with at most kk edges?

Decision problems are the basis of complexity classes such as P, NP and NP-complete, because these classes are defined over languages (the set of strings for which the answer is yes).

Optimization problem. An optimization problem asks for the best solution among many feasible solutions, where "best" means the minimum or maximum value of some objective function. Each feasible solution has an associated cost or value.

Examples:

  • Shortest path: find the path with the minimum number of edges.
  • Travelling salesman problem: find the tour of minimum total length.
  • 0/1 knapsack: select items to maximise total profit within the capacity.
  • Graph colouring: colour the vertices using the minimum number of colours.

Relation between them. Every optimization problem can be turned into a decision problem by adding a bound kk. For example, "find the shortest path" becomes "is there a path of length at most kk?". If the decision version is hard, the optimization version is at least as hard, because solving the optimization problem immediately answers the decision problem (compare the optimal value with kk). This is why NP-completeness theory is stated for decision problems.

FeatureDecision problemOptimization problem
OutputYes or noBest feasible solution or its value
ExampleIs there a tour of length ≤k\le k?Find the shortest tour

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

Questions from TU BSc CSIT 2076 Fourth Semester Artificial Intelligence (TU IOST board exam, 2076), TU BSc CSIT 2078 Fourth Semester Artificial Intelligence (TU IOST board exam, 2078), TU BSc CSIT 2079 Fourth Semester Artificial Intelligence (TU IOST board exam, 2079), TU BSc CSIT 2080 Fourth Semester Artificial Intelligence (TU IOST board exam, 2080), TU BSc CSIT 2081 Fourth Semester Artificial Intelligence (TU IOST board exam, 2081), TU BSc CSIT 2082 Fourth Semester Artificial Intelligence (TU IOST board exam, 2082), TU BSc CSIT 2076 Fourth Semester Computer Networks (TU IOST board exam, 2076), TU BSc CSIT 2078 Fourth Semester Computer Networks (TU IOST board exam, 2078), TU BSc CSIT 2079 Fourth Semester Computer Networks (TU IOST board exam, 2079), TU BSc CSIT 2080 Fourth Semester Computer Networks (TU IOST board exam, 2080), TU BSc CSIT 2081 Fourth Semester Computer Networks (TU IOST board exam, 2081), TU BSc CSIT 2082 Fourth Semester Computer Networks (TU IOST board exam, 2082), TU BSc CSIT 2076 Fourth Semester Database Management System (TU IOST board exam, 2076), TU BSc CSIT 2078 Fourth Semester Database Management System (TU IOST board exam, 2078), TU BSc CSIT 2079 Fourth Semester Database Management System (TU IOST board exam, 2079), TU BSc CSIT 2080 Fourth Semester Database Management System (TU IOST board exam, 2080), TU BSc CSIT 2081 Fourth Semester Database Management System (TU IOST board exam, 2081), TU BSc CSIT 2082 Fourth Semester Database Management System (TU IOST board exam, 2082), TU BSc CSIT 2076 Fourth Semester Operating System (TU IOST board exam, 2076), TU BSc CSIT 2078 Fourth Semester Operating System (TU IOST board exam, 2078), TU BSc CSIT 2079 Fourth Semester Operating System (TU IOST board exam, 2079), TU BSc CSIT 2080 Fourth Semester Operating System (TU IOST board exam, 2080), TU BSc CSIT 2081 Fourth Semester Operating System (TU IOST board exam, 2081), TU BSc CSIT 2082 Fourth Semester Operating System (TU IOST board exam, 2082), TU BSc CSIT 2076 Fourth Semester Theory of Computation (TU IOST board exam, 2076), TU BSc CSIT 2078 Fourth Semester Theory of Computation (TU IOST board exam, 2078), TU BSc CSIT 2079 Fourth Semester Theory of Computation (TU IOST board exam, 2079), TU BSc CSIT 2080 Fourth Semester Theory of Computation (TU IOST board exam, 2080), TU BSc CSIT 2081 Fourth Semester Theory of Computation (TU IOST board exam, 2081) and TU BSc CSIT 2082 Fourth Semester Theory of Computation (TU IOST board exam, 2082). Answers are written for this site; check them against your class notes.