Blog
Theory of Automata BSCS 5th Term Past Paper 2016 UOS
University of Sargodha Theory of Automata & Formal Language (CS-3131) past paper for BS Computer Science 5th term 2016, retyped from the original paper, with solved answers below.
Solved answers
Notation: → start state, * final state, Λ the null string, + means “or” in regular expressions.
Q1 – Short questions
(1) Moore and Mealy machines: finite automata with output. In a Moore machine the output depends only on the current state; in a Mealy machine it depends on the current state and the input (output is on transitions). They model sequential circuits — counters, registers, vending machines, traffic lights — and are used to design digital logic and controllers in computer hardware.
(2) “A language is closed”: a class of languages is closed under an operation if applying that operation to languages in the class always gives a language in the same class, e.g. regular languages are closed under union, concatenation, Kleene star, complement and intersection.
(3) Productions: the rules of a grammar of the form A → α that show how a non-terminal can be replaced by a string of terminals and non-terminals, e.g. S → aSb | Λ.
(4) CFG for infix and postfix expressions: yes. E → E + E | E * E | (E) | id generates infix expressions (with derivation trees); E → E E + | E E * | id generates postfix expressions. Derivation trees show the structure and evaluation order.
(5) Use of pushdown automata: PDAs (finite automata with a stack) recognise context-free languages; they are the model behind parsers in compilers, checking balanced parentheses, nested structures and expression evaluation.
(6) Null string (Λ): the string with no symbols, length 0. It is the identity for concatenation: Λw = wΛ = w.
(7) Valid and invalid alphabets: an alphabet is valid if every string over it can be broken into its letters in exactly one way (unambiguous tokenising), e.g. {a, b} or {B, aB, bab}. It is invalid if some string can be factorised in more than one way, e.g. {a, ab, ba} — “aba” could be (a)(ba) or (ab)(a).
(8) Determinism vs non-determinism: in a deterministic machine each state has exactly one transition per input symbol, so the next state is always unique. A non-deterministic machine may have zero, one or several transitions for a symbol (and Λ-moves); a string is accepted if any path reaches a final state.
(9) Equivalent FAs: two finite automata are equivalent if they accept exactly the same language, even if they have different numbers of states.
(10) Kleene star: for an alphabet or language S, S* is the set of all strings formed by concatenating zero or more elements of S, including Λ.
(11) Kleene star vs plus: S* includes Λ (zero or more repetitions); S⁺ = SS* means one or more repetitions and excludes Λ unless Λ ∈ S.
(12) Regular expression: an expression built from symbols of Σ, Λ and ∅ using union (+), concatenation and Kleene star, which describes a regular language, e.g. (a + b)*abb.
(13) Union of FAs: an FA that accepts L1 ∪ L2 — built from FA1 and FA2 by the product construction (states are pairs, final if either is final) or by an NFA with a new start state and Λ-moves to both machines.
(14) Creating an RE for a language: identify the basic strings, then build the language with union, concatenation and star — e.g. “strings ending in 0” = (0 + 1)*0 — or convert an FA to an RE by state elimination (Kleene’s theorem).
(15) FA vs NFA — opposites? An FA (DFA) has exactly one move per symbol per state; an NFA may have several or none and Λ-moves. They are not opposites — they have equal power: every NFA can be converted to an equivalent DFA by subset construction.
(16) Why use an NFA? NFAs are usually smaller and much easier to design and understand (e.g. for unions, concatenations, “contains a substring”), and they are the natural step when converting regular expressions to automata; they can later be converted to DFAs.
Q2 – Regular expressions over Σ = {0, 1}
- (i) Length six or less: (0 + 1 + Λ)⁶ — equivalently Λ + (0+1) + (0+1)² + … + (0+1)⁶.
- (ii) Next-to-last symbol is 0: (0 + 1)* 0 (0 + 1)
- (iii) Ending with 0: (0 + 1)* 0
Q3 – L1 = {a, ab}, L2 = {ab}
- Union: L1 ∪ L2 = {a, ab}
- Intersection: L1 ∩ L2 = {ab}
- Difference (complement of L2 relative to L1): L1 − L2 = L1 ∩ L2′ = {a}
- Complements over Σ = {a, b}: L1′ = Σ* − {a, ab} (every string except a and ab); L2′ = Σ* − {ab}.
Automata: for L1: → q0 —a→ q1* —b→ q2*; every other move goes to a dead state. For L2: → p0 —a→ p1 —b→ p2*; others to dead state. The union automaton is the same as L1’s (since L2 ⊂ L1). For the intersection, build the product machine (qi, pj): only (q2, p2) is final, giving the machine for {ab}. For a complement, take the complete DFA (with dead state) and swap final and non-final states.
Q4 – Recognisers
(a) At least four 1s: states count 1s seen so far.
| State | 0 | 1 |
|---|---|---|
| →q0 | q0 | q1 |
| q1 | q1 | q2 |
| q2 | q2 | q3 |
| q3 | q3 | q4 |
| *q4 | q4 | q4 |
(b) Contains substring 1010: yes — this language is regular, so a DFA exists. States remember the longest matched prefix of 1010.
| State (matched) | 0 | 1 |
|---|---|---|
| →q0 (Λ) | q0 | q1 |
| q1 (1) | q2 | q1 |
| q2 (10) | q0 | q3 |
| q3 (101) | q4 | q1 |
| *q4 (1010) | q4 | q4 |
Equivalent RE: (0 + 1)*1010(0 + 1)*.
Q5 – Pumping Lemma I and II
The pumping lemma is used to prove that a language is not regular.
Pumping Lemma (version I): if L is an infinite regular language, then there exist strings x, y, z with y ≠ Λ such that xyⁿz ∈ L for all n ≥ 1. Reason: an FA with N states accepting a long word must repeat a state, forming a loop (y) that can be repeated.
Pumping Lemma (version II): if L is regular and accepted by an FA with N states, then every word w ∈ L with length(w) > N can be written as w = xyz with y ≠ Λ and length(x) + length(y) ≤ N, such that xyⁿz ∈ L for all n ≥ 1.
Difference: version II adds the length condition, so the loop must occur within the first N letters. This makes it stronger: it can prove non-regularity of languages where version I fails, e.g. PALINDROME, because we can choose w = aᴺbaᴺ and force y to lie inside the first block of a’s. Example with version I: {aⁿbⁿ} is non-regular because pumping y (all a’s, all b’s or mixed) breaks the equal count or the order.
Q6 – DFAs for the given sets
(1) {0}: → q0 —0→ q1*; q0 —1→ D; q1 —0,1→ D; D (dead) loops on 0, 1.
(2) {1, 00}: → q0 —1→ q1*; q0 —0→ q2; q2 —0→ q3*; q2 —1→ D; q1 —0,1→ D; q3 —0,1→ D; D loops.
(3) {1ⁿ | n = 2, 3, 4, …}: → q0 —1→ q1 —1→ q2*; q2 —1→ q2; any 0 from q0, q1 or q2 → D; D loops. (RE: 111*)
Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
