Blog
Theory of Automata BSCS 5th Term Past Paper 2019 UOS
University of Sargodha BS 5th term Theory of Automata and Formal Languages (CS-3131) past paper 2019, retyped from the original paper, with solved answers below.
Solved answers
Q1 – Short questions
(1) Union of two CFLs is a CFL: let G1 (start S1) generate L1 and G2 (start S2) generate L2, with variables renamed so they are disjoint. A new grammar with start S and the productions of both plus S → S1 | S2 generates exactly L1 ∪ L2. So the union is context-free.
(2) Equivalent FAs: two finite automata are equivalent if they accept exactly the same language, even if they have different numbers of states.
(3) True. Take a complete DFA for L and make every final state non-final and every non-final state final. The new DFA accepts exactly the words not in L, so L′ is regular.
(4) The Turing machine is the most powerful: FA < PDA < TM (every language accepted by an FA is accepted by a PDA, and every PDA language by a TM, but not the reverse).
(5) Determinism vs non-determinism: in a deterministic machine each state has exactly one move for each input symbol. In a non-deterministic machine a state may have zero, one or several moves on a symbol (and Λ-moves); the word is accepted if any path leads to a final state. For finite automata both have the same power (every NFA can be converted to a DFA).
(6) False. (a+b)* is regular, but its subset {aⁿbⁿ : n ≥ 0} is not regular. Some subsets are regular (e.g. any finite subset, or a*), so being a subset of a regular language does not guarantee regularity.
(7) CFG is more powerful than FA: every regular language has a CFG (for each FA move X –a→ Y write X → aY, and X → Λ for final X). But {aⁿbⁿ : n ≥ 1} has the CFG S → aSb | ab and no FA accepts it (an FA cannot count unboundedly). So regular languages are a proper subset of context-free languages.
(8) 0²1³ = 00111, so the regular expression is simply 00111 (a language with one word).
(9) Kleene star vs PLUS: S* is all concatenations of zero or more words of S, so it always contains Λ. S⁺ is concatenations of one or more words, so it contains Λ only if Λ ∈ S. Example: {a}* = {Λ, a, aa, …}, {a}⁺ = {a, aa, …}.
(10) True. A finite language {w1, w2, …, wn} is described by the regular expression w1 + w2 + … + wn (or a DFA with a separate path for each word), so it is regular.
(11) CFLs are in fact always closed under union — proof as in (1): S → S1 | S2. (They are not always closed under intersection and complement.)
(12) Pumping property: for every infinite regular language L there is a length p (number of DFA states) such that any w ∈ L with |w| ≥ p can be split w = xyz with |xy| ≤ p, |y| > 0 and xyⁱz ∈ L for all i ≥ 0. Example: L = ab*, w = abb, x = a, y = b, z = b; then abⁱb ∈ L for every i.
(13) A Null (Λ) production is a production of the form A → Λ. A nullable variable is one that can derive Λ in one or more steps, e.g. with A → BC, B → Λ, C → Λ, A is nullable although A → Λ is not a production.
(14) FA for binary numbers divisible by 3: states q0, q1, q2 store the remainder; reading bit b changes remainder r to (2r + b) mod 3. q0 is start and final.
| State | 0 | 1 |
|---|---|---|
| →q0 (final) | q0 | q1 |
| q1 | q2 | q0 |
| q2 | q1 | q2 |
(15) A language is Turing recognizable if some TM accepts every word in it (it may loop forever on words not in it). It is Turing decidable if some TM halts on every input, accepting words in the language and rejecting the rest. Every decidable language is recognizable, but not conversely (e.g. the halting language).
(16) Any two words starting with a, e.g. a and ab (or aa, aba).
Q2
(a) CFG is more powerful than FA — see Q1(7): every regular language is context-free (FA → regular grammar), while L = {aⁿbⁿ} is context-free (S → aSb | ab) but not regular. Suppose an FA with k states accepted it; reading aᵏ it must repeat a state, so it would also accept aⁱbᵏ with i ≠ k — contradiction. Hence CFL ⊋ regular languages.
(b) Reverse of a regular language is regular. Let r be a regular expression for L. Define rev(r) by induction: rev(Λ) = Λ, rev(∅) = ∅, rev(a) = a, rev(r1 + r2) = rev(r1) + rev(r2), rev(r1r2) = rev(r2) rev(r1), rev(r*) = (rev(r))*. By induction on the structure of r, rev(r) describes exactly the reversed words of L, so Lᴿ has a regular expression and is regular.
Automaton proof: take an FA for L, reverse every arrow, make the old start state the only final state, and add a new start state with Λ-moves to all old final states. The result is an NFA accepting Lᴿ, so Lᴿ is regular.
Q3 – CFGs over Σ = {a, b}
a) L = {abⁿaaⁿ : n > 0} = a bⁿ aⁿ⁺¹: S → aXa, X → bXa | ba. (n = 1: abaa; n = 2: abbaaa.)
b) No substring “bbb”: S → aS | bA | Λ, A → aS | bB | Λ, B → aS | Λ (A = one b just read, B = two b’s just read).
c) L = {aᵐbⁿ : m ≠ n}: S → A | B, A → aA | aC, B → Bb | Cb, C → aCb | Λ. A gives more a’s than b’s, B gives more b’s than a’s.
d) All a’s before the first b (no a after any b, i.e. a*b*): S → aS | B, B → bB | Λ.
Q4
(a) L1 = words that start and end with a:
| State | a | b |
|---|---|---|
| →q0 | q1 | q3 |
| q1 (final) | q1 | q2 |
| q2 | q1 | q2 |
| q3 (trap) | q3 | q3 |
Eliminating q2: from q1, b goes to q2, which loops on b and returns to q1 on a, giving the loop label a + bb*a. So the regular expression is a(a + bb*a)*, which equals a + a(a + b)*a.
(b) CFLs are not closed under complement. Suppose they were. Since they are closed under union, L1 ∩ L2 = (L1′ ∪ L2′)′ would always be context-free. But L1 = {aⁿbⁿcᵐ} and L2 = {aᵐbⁿcⁿ} are both CFLs and L1 ∩ L2 = {aⁿbⁿcⁿ}, which is not context-free. Contradiction. So the complement of a CFL may be a CFL (e.g. for regular languages) but is not always one.
Q5
a) PDA for L = {aⁿb²ⁿ : n > 0}: for each a push two X’s; for each b pop one X; accept when only the bottom marker Z remains.
- δ(q0, a, Z) = (q0, XXZ); δ(q0, a, X) = (q0, XXX)
- δ(q0, b, X) = (q1, Λ); δ(q1, b, X) = (q1, Λ)
- δ(q1, Λ, Z) = (qf, Z) — qf is the accept state
Trace aabbbb: after aa the stack is XXXXZ; four b’s pop four X’s; Z is on top → accept.
b) {aⁿb²ⁿ} is not regular. Assume it is regular with pumping length p. Take w = aᵖb²ᵖ. Any split w = xyz with |xy| ≤ p and |y| ≥ 1 has y = aᵏ, k ≥ 1. Then xy²z = aᵖ⁺ᵏb²ᵖ, and 2p ≠ 2(p + k), so xy²z ∉ L. This contradicts the pumping lemma, so the language is not regular.
Q6 – Halting problem is undecidable
Suppose a TM H decides halting: H(M, w) accepts if M halts on w and rejects if it does not. Build a TM D that, on input ⟨M⟩, runs H(M, ⟨M⟩) and then does the opposite: if H says “halts”, D loops forever; if H says “does not halt”, D halts.
Now run D on its own description ⟨D⟩. If D halts on ⟨D⟩, then H(D, ⟨D⟩) said “halts”, so D loops — contradiction. If D loops on ⟨D⟩, then H said “does not halt”, so D halts — contradiction. Both cases are impossible, so H cannot exist and the halting problem is undecidable.
Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
