Theory of Computation · Unit 3 · 6 hrs
Regular Expressions
Exam-focused notes for Regular Expressions (Theory of Computation, CSC262): what the TU syllabus asks and how it has actually been tested, with 13 solved past questions from this unit.
Regular Expressions in short
This unit gives regular expressions as an algebraic, textual alternative to drawing an automaton, proves the two notations are equally expressive, and develops the tools, the pumping lemma and the table filling algorithm, used to show a language is not regular and to shrink a DFA to its smallest form.
What this unit covers
- Regular Expressions
- Regular Operators
- Regular Languages and their applications
- Algebraic Rules for Regular Expressions
- Equivalence of Regular Expression and Finite Automata
- Reduction of Regular Expression to ε – NFA
- Conversion of DFA to Regular Expression
- Properties of Regular Languages
- Pumping Lemma
- Application of Pumping Lemma
- Closure Properties of Regular Languages over (Union, Intersection, Complement)
- Minimization of Finite State Machines: Table Filling Algorithm
Minimization of Finite State Machines
2081 · 10 marks
List any two regular operators. Minimize the following finite state machine using Table Filling algorithm.[10]
Show solved answerHide answer
Regular Operators and DFA Minimization Using Table Filling Algorithm
Part 1: Two Regular Operators
Regular operators are the operations used to construct regular expressions from smaller ones. Any two of the following are acceptable:
-
Union (
|or+): If $R$ and $S$ are regular expressions, then $R + S$ denotes the set of strings belonging to either $R$ or $S$.- Example: $(a \mid b)$ denotes ${a, b}$
-
Concatenation (
.): If $R$ and $S$ are regular expressions, then $RS$ denotes strings formed by a string of $R$ followed by a string of $S$.- Example: $ab$ denotes ${ab}$
-
Kleene Star (
*): If $R$ is a regular expression, then $R^{*}$ denotes zero or more concatenations of $R$.- Example: $a^{*}$ denotes ${\varepsilon, a, aa, aaa, \ldots}$
Part 2: DFA Minimization Using Table Filling
Given Data (extracted transition table)
| State | 0 | 1 |
|---|---|---|
| $\to A$ | B | C |
| $B$ | A | D |
| $C$ | E | F |
| $D$ | E | F |
| $E$ | E | F |
| $*F$ | F | F |
- Start state: $A$
- Final state(s): ${F}$
Note: The problem statement references a machine whose diagram/table is not fully reproduced in the prompt. The solution uses the transition table extracted above. If the intended machine differs, the extracted table above is what this solution assumes.
Step 1: Initialize the triangular table
Mark every pair where exactly one state is final. Final = ${F}$.
Marked pairs (one is $F$): $(A,F), (B,F), (C,F), (D,F), (E,F)$.
B | _ |
C | _ | _ |
D | _ | _ | _ |
E | _ | _ | _ | _ |
F | X | X | X | X | X |
| A | B | C | D | E |
Step 2: Iterative marking
For each unmarked pair, check transitions on $0$ and $1$; mark if any successor pair is marked.
Pass 1:
- $(A,B)$: $0\to(B,A)$ unmarked, $1\to(C,D)$ unmarked → unmarked
- $(A,C)$: $1\to(C,F)$ marked → MARK
- $(A,D)$: $1\to(C,F)$ marked → MARK
- $(A,E)$: $1\to(C,F)$ marked → MARK
- $(B,C)$: $0\to(A,E)$ marked → MARK
- $(B,D)$: $0\to(A,E)$ marked → MARK
- $(B,E)$: $0\to(A,E)$ marked → MARK
- $(C,D)$: $0\to(E,E)$ same, $1\to(F,F)$ same → unmarked
- $(C,E)$: $0\to(E,E)$ same, $1\to(F,F)$ same → unmarked
- $(D,E)$: $0\to(E,E)$ same, $1\to(F,F)$ same → unmarked
Pass 2 (re-check remaining unmarked pairs):
- $(A,B)$: $0\to(A,B)$ still unmarked, $1\to(C,D)$ still unmarked → remains unmarked
- $(C,D)$: both transitions to same states → remains unmarked
- $(C,E)$: both transitions to same states → remains unmarked
- $(D,E)$: both transitions to same states → remains unmarked
No further changes → algorithm terminates.
Final table:
B | _ |
C | X | X |
D | X | X | _ |
E | X | X | _ | _ |
F | X | X | X | X | X |
| A | B | C | D | E |
Step 3: Group equivalent (unmarked) states
Unmarked pairs: $(A,B)$, $(C,D)$, $(C,E)$, $(D,E)$.
- $A \equiv B$
- $C \equiv D \equiv E$
- $F$ (alone)
Equivalence classes: $${A, B}, \quad {C, D, E}, \quad {F}$$
Step 4: Minimized DFA
Let:
- $P = {A, B}$ (start state)
- $Q = {C, D, E}$
- $R = {F}$ (final state)
Compute transitions (using any representative):
- $P$: on $0$, $\delta(A,0)=B\in P$; on $1$, $\delta(A,1)=C\in Q$
- $Q$: on $0$, $\delta(C,0)=E\in Q$; on $1$, $\delta(C,1)=F\in R$
- $R$: on $0$, $\delta(F,0)=F\in R$; on $1$, $\delta(F,1)=F\in R$
Minimized transition table:
| State | 0 | 1 |
|---|---|---|
| $\to P$ | P | Q |
| $Q$ | Q | R |
| $*R$ | R | R |
The minimized DFA has 3 states: ${A,B}$, ${C,D,E}$, ${F}$.
2078 · 10 marks
Find the minimum state DFA for the given DFA below:
| States | 0 | 1 |
|---|---|---|
| A | B | F |
| B | E | C |
| C | B | D |
| *D | E | F |
| E | B | C |
| F | B | A |
[10]
Regular Expressions
2080 · 5 marks
Write regular expression over {a,b} that represents a. Strings having exactly two a's and at least two b's. b. Strings having an even number of a's and each a followed by at least one b. [5]
Show solved answerHide answer
Regular Expressions over {a, b}
Given data
- Alphabet: $\Sigma = {a, b}$
- Part (a): strings with exactly two a's AND at least two b's.
- Part (b): strings with an even number of a's, and each a immediately followed by at least one b.
Part (a): Exactly two a's and at least two b's
Structure. With exactly two a's, every string has the shape:
$$b^,a,b^,a,b^*$$
The three $b^*$ blocks (before, between, after) hold all the b's. The constraint is that the total number of b's across all three blocks is at least 2.
Deriving a compact expression. The plain shape $b^*ab^ab^$ allows 0 or 1 b's, which we must forbid. So we subtract those cases by forcing the b-count $\ge 2$. This is cleanly expressed by casing on where the "guaranteed" b's sit. A correct enumeration (each covers b-total $\ge 2$, union covers all):
$$R = b^{2+}ab^ab^ ;+; b^+ab^+ab^* ;+; b^+ab^*ab^+ ;+; b^ab^{2+}ab^ ;+; b^*ab^+ab^+ ;+; b^*ab^*ab^{2+}$$
where $b^{2+} = bbb^$ (at least two b's) and $b^+ = bb^$.
A shorter equivalent that is fully acceptable in an exam:
$$\boxed{R = b^*a,b^a,b^\ \text{restricted to total } b\text{'s} \ge 2}$$
expressed formally as the union above. In terms of "at least one block has $bb$, OR two blocks each have a $b$":
$$R = bbb^* a, b^* a, b^* + b^* a, bbb^* a, b^* + b^* a, b^* a, bbb^* + bb^* a, bb^* a, b^* + bb^* a, b^* a, bb^* + b^* a, bb^* a, bb^*$$
Check. Every accepted string must carry exactly two a's, so test $baab$: it has one b before the a's, the two a's adjacent, and one b after, giving a b-total of 2, and it matches the term $b^+ab^*ab^+$. The string $aab$ carries only one b and is correctly rejected. The string $bbaa$ has two b's ahead of the first a and matches the term $b^{2+}ab^ab^$.
Part (b): Even number of a's, each a followed by at least one b
Structure.
- Each a must be immediately followed by at least one b, so the basic unit is $ab^+$ (i.e. $abb^*$).
- The number of a's must be even, so units come in pairs: $(ab^+ab^+)$, repeated zero or more times.
- Extra b's may appear at the start (before any a).
Regular expression:
$$\boxed{R = b^,(ab^+,ab^+)^}$$
Verification:
| String | Match? | Reason |
|---|---|---|
| $\varepsilon$ | Yes | zero a's (even) |
| $bb$ | Yes | $b^*$ only |
| $abab$ | Yes | one pair $ab,ab$ |
| $abbabb$ | Yes | $abb,abb$ |
| $babbab$ | Yes | $b^*=b$, then $abb,ab$ |
| $ab$ | No | one a (odd) |
| $aab$ | No | first a not followed by b |
| $abba$ | No | last a not followed by b |
Final Answers
(a) Exactly two a's and at least two b's: $$R = b^{2+}ab^ab^ + b^+ab^+ab^* + b^+ab^*ab^+ + b^ab^{2+}ab^ + b^*ab^+ab^+ + b^*ab^*ab^{2+}$$
(b) Even number of a's, each a followed by at least one b: $$R = b^(ab^+ab^+)^$$
2078 · 5 marks
Give the regular expressions for the following language over alphabet {a, b}. a. Set of all strings with substring bab or abb. b. Set of all strings whose 3rd symbol is 'a' and 5th symbol is 'b'. [5]
2076 · 5 marks
Give the regular expressions for following language over alphabet {0, 1}. a. Set of all strings with 2nd symbol from right is 1. b. Set of all strings starting with 00 or 11 and ending with 10 or 01. [5]
2080.1 · 5 marks
Construct regular expression over {1,2,...,9} that represents a. strings of even numbers with length 4 starting with 2 and ending with 8. b. strings starting with odd numbers and ending with even numbers. [5]
Pumping Lemma
2080 · 5 marks
Using pumping lemma, prove that the language is not regular. $L = {a^i b^j c^k \mid j=i+k}$ [5]
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.[10]
2076 · 5 marks
Show that language is not a regular language. $L = { 0^m 1^m \mid m \geq 1 }$ [5]
Reduction of Regular Expression to ε – NFA
2079 · 5 marks
Convert the following regular expression into equivalent Finite Automata. a.(0+1)∗10(1+0)a. (0+1)10(1+0)a.(0+1)∗10(1+0)b.1∗0(0+1)∗1b. 10(0+1)*1b.1∗0(0+1)∗1[5]
2076 · 10 marks
Define the NFA with ϵ−transition\epsilon-transitionϵ−transition and ϵ−closure\epsilon-closureϵ−closure of a state. Show that for every regular expression r, representing a language L, there is ϵ−NFA\epsilon-NFAϵ−NFA accepting the same language. Also convert regular expression (a+b)ab into equivalent Finite Automata.[10]
Show solved answerHide answer
NFA with ε-Transition, ε-Closure, and Regular Expression to FA Conversion
1. NFA with ε-Transition (ε-NFA)
An NFA with ε-transition (also called ε-NFA) is a Nondeterministic Finite Automaton that allows transitions on the empty string ε (without consuming any input symbol).
Formally, an ε-NFA is defined as a 5-tuple:
M = (Q, Σ, δ, q₀, F)
where:
- Q = finite set of states
- Σ = finite input alphabet
- δ : Q × (Σ ∪ {ε}) → 2^Q = transition function (can take ε as input)
- q₀ ∈ Q = start state
- F ⊆ Q = set of final/accepting states
The key difference from a standard NFA is that the transition function accepts ε as a valid input, allowing the automaton to move from one state to another without reading any symbol.
2. ε-Closure of a State
The ε-closure of a state q is the set of all states reachable from q by following zero or more ε-transitions (including q itself).
ε-closure(q) = {p ∈ Q | p is reachable from q using only ε-transitions}
Properties:
- q is always in ε-closure(q) (reflexive)
- If p ∈ ε-closure(q) and r ∈ ε-closure(p), then r ∈ ε-closure(q) (transitive)
Example: If q --ε--> p --ε--> r, then ε-closure(q) = {q, p, r}
3. Theorem: For Every Regular Expression r, There Exists an ε-NFA Accepting L(r)
Proof by Structural Induction (Thompson's Construction Rules):
We show construction rules for each case of a regular expression.
Base Cases
Rule 1: r = ∅ (empty language)
→[q₀] (no transitions, no final state)
The ε-NFA has start state q₀ with no accepting states.
Rule 2: r = ε (empty string)
→[q₀] --ε--> ((q_f))
One ε-transition from start to final state.
Rule 3: r = a (single symbol a ∈ Σ)
→[q₀] --a--> ((q_f))
One transition on symbol a from start to final state.
Inductive Cases
Assume ε-NFAs N(r) and N(s) exist for regular expressions r and s.
Rule 4: r = r₁ | r₂ (Union / Alternation)
Create a new start state and new final state, connect with ε-transitions:
ε --> [N(r₁)] --> ε
→[q₀] --< >--> ((q_f))
ε --> [N(r₂)] --> ε
- New start state q₀ goes to start of N(r₁) and N(r₂) via ε
- Final states of N(r₁) and N(r₂) go to new final state q_f via ε
Rule 5: r = r₁ . r₂ (Concatenation)
Connect the final state of N(r₁) to the start state of N(r₂) via ε:
→[N(r₁)] --ε--> [N(r₂)]-->((q_f))
- Final state of N(r₁) becomes non-final
- ε-transition connects it to start of N(r₂)
Rule 6: r = r₁ (Kleene Star / Closure)*
ε (loop back)
↑___________↓
→[q₀] --ε--> [N(r₁)] --ε--> ((q_f))
|_________________________↑
ε (skip, for empty string)
- New start state q₀ connects to start of N(r₁) via ε
- Final state of N(r₁) loops back to start of N(r₁) via ε
- New start state also connects directly to new final state via ε (to accept ε)
Since every regular expression is built from these base cases and operations, and we have shown an ε-NFA construction for each, by induction, every regular expression has an equivalent ε-NFA. ∎
4. Convert Regular Expression (a+b)ab to Equivalent Finite Automaton
The regular expression is: (a+b)*ab*
This means: strings over {a,b} that contain at least one 'a', ending with zero or more b's -- more precisely, any string of a's and b's, followed by 'a', followed by zero or more b's.
Step 1: Break the expression into parts
| Part | Meaning |
|---|---|
| (a+b)* | Zero or more a's or b's (any string) |
| a | Single symbol a |
| b* | Zero or more b's |
Step 2: Build ε-NFA for each part
Part 1: (a+b)*
a
ε ↗[q₁]↘ ε
[q₀] [q₃] --ε--> ((q₄))
ε ↘[q₂]↗ ε
b
With loop: q₄ --ε--> q₀ (for the * closure), and q₀ --ε--> q₄ (for accepting zero repetitions of (a+b), i.e. the empty string). This gives the complete ε-NFA fragment for (a+b)*, with q₀ as its start state and q₄ as its accepting state.
Part 2: Single symbol a
→[q5] --a--> ((q6))
Part 3: b*
ε (loop back)
↑___________↓
→[q7] --ε--> [q8] --b--> ((q9))
|_____________________↑
ε (skip, for zero b's)
Step 3: Concatenate the Three Parts
By the concatenation rule (Rule 5 above), the accepting state of one block is joined to the start state of the next block with an ε-transition, and it stops being an accepting state in its own right (only the final block keeps an accepting state):
- q₄ --ε--> q₅ (end of the (a+b)* block feeds into the mandatory a)
- q₆ --ε--> q₇ (end of the mandatory a feeds into the b* block)
Step 4: Complete ε-NFA for (a+b)*ab*
| State | On ε | On a | On b |
|---|---|---|---|
| q0 (start) | q1, q2, q4 | -- | -- |
| q1 | -- | q3 | -- |
| q2 | -- | -- | q3 |
| q3 | q0 | -- | -- |
| q4 | q5 | -- | -- |
| q5 | -- | q6 | -- |
| q6 | q7 | -- | -- |
| q7 | q8, q9 | -- | -- |
| q8 | q9 | -- | q8 |
| q9 (accept) | -- | -- | -- |
Verification:
- On "ab": q0 --ε--> q4 (zero repetitions of (a+b)) --ε--> q5 --a--> q6 --ε--> q7 --ε--> q9 (zero b's). Accepted.
- On "aabb": q0 --ε--> q1 --a--> q3 --ε--> q0 --ε--> q4 --ε--> q5 --a--> q6 --ε--> q7 --ε--> q8 --b--> q8 --b--> q8 --ε--> q9. The symbols consumed, in order, are a (one turn of the star), a (the mandatory a), b, b (two turns of b*), i.e. exactly "aabb". Accepted.
This ε-NFA accepts precisely the language of (a+b)*ab*: any string over {a, b} (possibly empty), followed by a mandatory a, followed by zero or more b's. This completes the required conversion of the regular expression to an equivalent finite automaton via Thompson's construction.
Application of Pumping Lemma
2078 · 5 marks
Show that L = {ana^nan | n is a prime number} is not a regular language. [5]
Conversion of DFA to Regular Expression
2080.1 · 5 marks
State Arden's theorem. Convert following DFA into its regular expression using Arden theorem.
| 0 | 1 | |
|---|---|---|
| $\rightarrow *Q1$ | $Q1$ | $Q2$ |
| $Q2$ | $Q3$ | $Q2$ |
| $Q3$ | $Q1$ | $Q2$ |
[5]
Make Unit 3 stick
Practice CSC262 with flashcards & quizzes