ATCD UT2 Material
ATCD UT2 Material
Finite Automata with Epsilon-transitions: NFA with Epsilon transitions, Equivalence between
NFA without and with Epsilon transitions, Equivalence between DFA and NFA without Epsilon
Transitions, Minimization of Finite Automaton.
Regular Expressions and Languages: Regular Expressions and Operators, Conversion from
DFA to regular expression, Conversion from Regular expression to DFA, Algebraic Laws of
Regular Expressions & Closure properties of Regular Languages.
Example:
State 0 1 (ε)epsilon
A B,C A B
B - B C
C C C -
ε - closure(B) :{B,C} means B is a self-state and C is a state obtained from B with ε input.
ε - closure(C) : {C}
The language L accepted by Non-deterministic finite automata (NFA) with ε, denoted by M= (Q,
Σ, 𝛿, q0, F) and can be defined as follows −
1
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Let M= (Q, Σ, 𝛿, q0, F) be a NFA with ε
Where,
Q is a set of states
Σ is input set
q0 is start state
F is a final state
Example:
Construct NFA with epsilon which accepts a language consisting the strings of any number of
a’s followed by any number of b’s followed by any number of c’s
Solution:
Here, any number of a’s or b’s or c’s means that there can be zero or more a’s followed by zero,
more number of b’s followed by zero or more c’s.
M = ({q0,q1,q2},{a,b}, 𝛿, q0,q2})
Explanation:
Step 1 − q0 is the initial state q0 on ‘a’ goes to q0 itself, and on epsilon transition q0 goes to q1.
2
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Step 3 − q2 on ‘c’ goes to q2 itself which is the final state.
State\ input
a b c ε
symbol
q0 q0 - - q1
q1 - q1 - q2
q2 - - q2 -
|- δ(q0,bbcc)
|- δ(q0, εbbcc)
|- δ(q1,bbcc)
|- δ(q1,bcc)
|- δ(q1,cc)
|- δ(q1, εcc)
|- δ(q2,cc)
|- δ(q2,c)
|- δ(q2, ε)
Thus, we reach the accept state, after scanning the compute input string.
The ε – closure (p) is a set of all states which are reachable from state p on ε – transitions such
that:
3
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
2.1 Conversion of NFA with ε to NFA without ε:
In this method, we try to remove all the ε-transitions from the given Non-deterministic finite
automata (NFA) −
• Step 1 − Find out all the ε-transitions from each state from Q. That will be called as ε -
closure (qi) where, qi ∈Q.
• Step 2 − Then, 𝛿' transitions can be obtained. The 𝛿' transitions mean a ε-closure on 𝛿
moves.
• Step 3 − Step 2 is repeated for each input symbol and for each state of given NFA.
• Step 4 − by using the resultant status, the transition table for equivalent NFA without ε
can be built.
Example 1:
Solution:
We will first obtain ε-closure of each state i.e., we will find ε-reachable states from the current
state.
Hence,
ε-closure(q0) = {q0,q1,q2}
ε-closure(q1) = {q1,q2}
ε-closure(q2) = {q2}
ε-closure(q0) means with null input (no input symbol) we can reach q0, q1, q2. In a similar
manner for q1 and q2 ε-closure are obtained. Now we will obtain 𝛿1 transitions for each state on
each input symbol as shown below –
4
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
δ'(q0, 0) = ε-closure(δ(δ^(q0, ε),0))
= ε-closure(δ(ε-closure(q0),0))
= ε-closure(δ(q0,q1,q2), 0))
= ε-closure(q0 U Φ ∪ Φ)
= ε-closure(q0)
= ε-closure(δ(q0,q1,q2), 1))
= ε-closure(Φ ∪q1 U Φ)
= ε-closure(q1)
= {q1, q2}
= ε-closure(δ(q0,q1,q2), 2))
= ε-closure(Φ U ΦU q2)
= ε-closure(q2)
= {q2}
= ε-closure(δ(q1,q2), 0))
= ε-closure(δ(q1, 0) U δ(q2, 0) )
= ε-closure(Φ ∪ Φ)
= ε-closure(Φ)
=Φ
5
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
δ'(q1,1) = ε-closure(δ(δ^(q1, ε),1))
= ε-closure(δ(q1,q2), 1))
= ε-closure(δ(q1, 1) U δ(q2, 1) )
= ε-closure(q1 ∪ Φ)
= ε-closure(q1)
= {q1,q2}
= ε-closure(δ(q1,q2), 2))
= ε-closure(δ(q1, 2) U δ(q2, 2) )
= ε-closure(Φ ∪ q2)
= ε-closure(q2)
= {q2}
= ε-closure(δ(q2), 0))
= ε-closure(δ(q2, 0))
= ε-closure(Φ)
=Φ
= ε-closure(δ(q2), 1)
= ε-closure(δ(q2, 1))
= ε-closure(Φ)
=Φ
= ε-closure(δ(q2), 2))
= ε-closure(δ(q2, 2))
6
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
= ε-closure(q2)
= {q2}
Now, we will summarize all the computed δ' transitions as given below –
States\inputs 0 1 2
q0 {q0,q1,q2} {q1,q2} {q2}
q1 Φ {q1,q2} {q2}
q2 Φ Φ {q2}
Here, q0, q1, q2 are final states because ε-closure(q0), ε-closure(q1) and ε-closure(q2) contain a
final state q2.
Exercise Problem 2:
7
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
2.2 Conversion NFA to DFA:
The following steps are followed to convert a given NFA to a DFA-
Step-01: The start state of NFA M will be the start for DFA M'. Hence add q0 of NFA (start
state) to Q'. Then find the transitions from this start state.
Step-02: For each state [q1, q2, q3,……..qi] in Q' the transitions for each input symbol Σ can be
obtained as –
a) δ'([q1, q2,…….qi], a) = δ(q1, a) ∪ δ(q2, a) ∪………….. δ(qi, a) = [q1, q2, ……….qk] may be
some state.
b) Add the state [q1, q2.….qk]to DFA if it not already added in Q'.
c) Then find the transitions for every input symbol from Σ for state [q1, q2.….qk]. If we got
some state [q1, q2, ……….qn] which is not in Q' of DFA then add this state to Q'.
d) If there is no new state generating then stop the process after finding all the transitions.
Step-03: For the state [q1, q2, ……….qn] ∈ Q' of DFA if any one state qi is a final state of NFA
then [q1, q2, ……….qn] becomes a final state. Thus the set of all the final states ∈ F' of DFA.
Solution:
For the given transition diagram we will first construct the transition table.
State 0 1
→q0 q0 q1
q1 {q1, q2} q1
*q2 q2 {q1, q2}
δ'([q0], 0) = [q0]
δ'([q0], 1) = [q1]
δ'([q1], 1) = [q1]
δ'([q2], 0) = [q2]
= [q1, q2]
= {q1, q2}
= [q1, q2]
The state [q1, q2] is the final state as well because it contains a final state q2.
State 0 1
→[q0] [q0] [q1]
[q1] [q1, q2] [q1]
*[q2] [q2] [q1, q2]
*[q1, q2] [q1, q2] [q1, q2]
Solution:
For the given transition diagram we will first construct the transition table.
State 0 1
→q0 {q0, q1} {q1}
*q1 ϕ {q0, q1}
δ'([q1], 0) = ϕ
= {q0, q1} ∪ ϕ
= {q0, q1}
= [q0, q1]
Similarly,
= {q0, q1}
10
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
= [q0, q1]
As in the given NFA, q1 is a final state, then in DFA wherever, q1 exists that state becomes a
final state. Hence in the DFA, final states are [q1] and [q0, q1]. Therefore set of final states F =
{[q1], [q0, q1]}.
State 0 1
→[q0] [q0, q1] [q1]
*[q1] ϕ [q0, q1]
*[q0, q1] [q0, q1] [q0, q1]
Suppose
A = [q0]
B = [q1]
C = [q0, q1]
11
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Exercise Problem 03: Convert the given NFA to its equivalent DFA.
Problem 04: Convert the following Non-Deterministic Finite Automata (NFA) to Deterministic
Finite Automata (DFA)-
Solution: Transition table for the given Non-Deterministic Finite Automata (NFA) is-
State / Alphabet a b
→q0 q0 q0, q1
q1 – *q2
*q2 – –
Step-01:
Let Q' be a new set of states of the Deterministic Finite Automata (DFA).
Step-02:
State / Alphabet a b
→q0 q0 {q0, q1}
Step-03:
Add transitions for set of states {q0, q1} to the transition table T'.
12
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
State / Alphabet a b
→q0 q0 {q0, q1}
{q0, q1} q0 {q0, q1, q2}
Step-04:
Add transitions for set of states {q0, q1, q2} to the transition table T'.
State / Alphabet a b
→q0 q0 {q0, q1}
{q0, q1} q0 {q0, q1, q2}
{q0, q1, q2} q0 {q0, q1, q2}
Step-05:
Since no new states are left to be added in the transition table T', so we stop.
States containing q2 as its component are treated as final states of the DFA.
State / Alphabet a b
→q0 q0 {q0, q1}
{q0, q1} q0 *{q0, q1, q2}
*{q0, q1, q2} q0 *{q0, q1, q2}
13
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Problem 05:
Solution: Transition table for the given Non-Deterministic Finite Automata (NFA) is-
State / Alphabet 0 1
→q0 q0 q1, *q2
q1 q1, *q2 *q2
*q2 q0, q1 q1
Step-01:
Let Q' be a new set of states of the Deterministic Finite Automata (DFA).
Step-02:
State / Alphabet 0 1
→q0 q0 {q1, q2}
Step-03:
Add transitions for set of states {q1, q2} to the transition table T'.
State / Alphabet 0 1
→q0 q0 {q1, q2}
{q1, q2} {q0, q1, q2} {q1, q2}
14
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Step-04:
Add transitions for set of states {q0, q1, q2} to the transition table T'.
State / Alphabet 0 1
→q0 q0 {q1, q2}
{q1, q2} {q0, q1, q2} {q1, q2}
{q0, q1, q2} {q0, q1, q2} {q1, q2}
Step-05:
Since no new states are left to be added in the transition table T', so we stop.
States containing q2 as its component are treated as final states of the DFA.
State / Alphabet 0 1
→q0 q0 *{q1, q2}
*{q1, q2} *{q0, q1, q2} *{q1, q2}
*{q0, q1, q2} *{q0, q1, q2} *{q1, q2}
15
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
2.3 Minimization of Finite Automaton:
DFA Minimization using Myhill -Nerode Theorem(Table Filling Method):
Algorithm –
Input − DFA
Step 1: Draw a table for all pairs of states (P, Q) [All are unmarked initially].
Step 2: Mark all pairs where P ∈ F and Q ∉ for vice versa and mark them. [Here F is the set of
final states]
If there are any unmarked pairs (P, Q), such that {δ (P, x), δ(Q, x)} is marked for some input
alphabet, then mark [P, Q].
Step 4: Combine all the unmarked pairs and make them a single state in the reduced DFA.
16
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Step 2: Mark all the pairs (Qa,Qb) such a that Qa is Final state and Qb is Non-Final State.
Step 3: If there is any unmarked pair (Qa,Qb) such a that δ(Qa,x) and δ(Qb,x) is marked, then
mark (Qa,Qb). Here x is a input symbol. Repeat this step until no more marking can be made.
• Check when x=0 : δ(Q2,0) = Q4 and δ(Q1,0) = Q3, check if the pair Q4,Q3 is marked
and no it is not marked.
• Check when x=1 : δ(Q2,1) = Q3 and δ(Q1,1) = Q4, check if the pair Q4,Q3 is marked
and no it is not marked.
• Hence we cannot mark the pair Q2,Q1.
• Check when x=0 : δ(Q3,0) = Q5 and δ(Q0,0) = Q1, check if the pair Q5,Q1 is marked
and no it is not marked.
• Check when x=1 : δ(Q3,1) = Q5 and δ(Q0,1) = Q2, check if the pair Q5,Q2 is marked
and no it is not marked.
• Hence we cannot mark the pair Q3,Q0.
• Check when x=0 : δ(Q4,0) = Q5 and δ(Q0,0) = Q1, check if the pair Q5,Q1 is marked
and no it is not marked.
• Check when x=1 : δ(Q4,1) = Q5 and δ(Q0,1) = Q2, check if the pair Q5,Q2 is marked
and no it is not marked.
• Hence we cannot mark the pair Q4,Q0.
• Check when x=0 : δ(Q4,0) = Q5 and δ(Q3,0) = Q5, Such pair of state Q5,Q5 don’t exists.
17
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
• Check when x=1 : δ(Q4,1) = Q5 and δ(Q3,1) = Q5, Such pair of state Q5,Q5 don’t exists.
• Hence we cannot mark the pair Q4,Q3.
• Check when x=0 : δ(Q5,0) = Q5 and δ(Q1,0) = Q3, check if the pair Q5,Q3 is marked
and yes it is marked.
• Hence we can mark the pair Q5,Q1.
• Check when x=0 : δ(Q5,0) = Q5 and δ(Q2,0) = Q4, check if the pair Q5,Q4 is marked
and yes it is marked.
• Hence we can mark the pair Q5,Q2.
g. We have checked for all the unmarked pairs but don’t need to stop here we need to continue
this process until no more markings can be made.
• Check when x=0 : δ(Q2,0) = Q4 and δ(Q1,0) = Q3, check if the pair Q4,Q3 is marked
and no it is not marked.
• Check when x=1 : δ(Q2,1) = Q3 and δ(Q1,1) = Q4, check if the pair Q4,Q3 is marked
and no it is not marked.
• Hence we cannot mark the pair Q2,Q1.
• Check when x=0 : δ(Q3,0) = Q5 and δ(Q0,0) = Q1, check if the pair Q5,Q1 is marked
and yes it is marked.
• Hence we can mark the pair Q3,Q0.
18
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
iii. Check for the unmarked pair Q4,Q3
• Check when x=0 : δ(Q4,0) = Q5 and δ(Q3,0) = Q5, Such pair of state Q5,Q5 don’t exists.
• Check when x=1 : δ(Q4,1) = Q5 and δ(Q3,1) = Q5, Such pair of state Q5,Q5 don’t exists.
• Hence we cannot mark the pair Q4,Q3.
h. Now even though we repeat the procedure we cannot mark the pairs Q2,Q1(since Q4,Q3 is
not marked) and Q4,Q3(since Q5,Q5 such pair of states does not exists.). Hence we stop here.
Step 4: Combine all the unmarked pairs and make them as a single state in the minimized DFA.
• The unmarked Pairs are Q2,Q1 and Q4,Q3 hence we combine them.
Following is the Minimized DFA with Q1Q2 and Q3Q4 as the combined states.
19
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Step 1 − Draw a table for all pairs of states.
Step 3 − we will try to mark the state pairs, with green colored check mark, transitively. If we
input 1 to state ‘a’ and ‘f’, it will go to state ‘c’ and ‘f’ respectively. (c, f) is already marked,
hence we will mark pair (a, f). Now, we input 1 to state ‘b’ and ‘f’; it will go to state‘d’ and ‘f’
respectively. (d, f) is already marked, hence we will mark pair (b, f).
20
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
After step 3, we have got state combinations {a, b} {c, d} {c, e} {d, e} that are unmarked.
So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}
Let Σ be an alphabet which is used to denote the input set. The regular expression over Σ can be
defined as follows.
1. ε is a Regular Expression indicates the language containing an empty string. (L (ε) = {ε})
The R* is known as kleen closure or closure which indicates occurrence of r for ∞ number of
times.
21
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example 1:
Write the regular expression for the language accepting all combinations of a's, over the set ∑ =
{a}.
Solution:
All combinations of a's means a may be zero, single, double and so on. If a is appearing zero
times, that means a null string. That is we expect the set of {ε, a, aa, aaa, ....}. So we give a
regular expression for this as:
R = a*
Example 2:
Write the regular expression for the language accepting all combinations of a's except the null
string, over the set ∑ = {a}.
Solution:
This set indicates that there is no null string. So we can denote regular expression as:
R = a+
Example 3:
Write the regular expression for the language accepting all the string containing any number of
a's and b's.
Solution:
This will give the set as L = {ε, a, aa, b, bb, ab, ba, aba, bab, .....}, any combination of a and b.
The (a + b)* shows any combination with a and b even a null string.
Example 4:
Write the regular expression for the language accepting all the string which are starting with 1
and ending with 0, over ∑ = {0, 1}.
22
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Solution:
In a regular expression, the first symbol should be 1, and the last symbol should be 0. The r.e. is
as follows:
R = 1 (0+1)* 0
Example 5:
Write the regular expression for the language starting and ending with a and having any having
any combination of b's in between.
Solution:
R = a b* b a
Example 6:
Write the regular expression for the language starting with a but not having consecutive b's.
R = {a + ab}*
Example 7:
Write the regular expression for the language accepting all the string in which any number of a's
is followed by any number of b's is followed by any number of c's.
Solution: As we know, any number of a's means a* any number of b's means b*, any number of
c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So
the regular expression could be:
R = a* b* c*
Example 8:
Write the regular expression for the language over ∑ = {0} having even length of the string.
Solution:
23
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
L = {ε, 00, 0000, 000000, …………….}
R = (00)*
Example 9:
Write the regular expression for the language having a string which should have atleast one 0 and
alteast one 1.
Solution:
Example 10:
Solution:
The language can be predicted from the regular expression by finding the meaning of it. We will
first split the regular expression as:
L = {The language consists of the string in which a's appear triples, there is no restriction on the
number of b's}
Example 11:
Write the regular expression for the language L over ∑ = {0, 1} such that all the string do not
contain the substring 01.
Solution:
R = (1* 0*)
24
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example 12:
Write the regular expression for the language containing the string over {0, 1} in which there are
at least two occurrences of 1's between any two occurrences of 1's between any two occurrences
of 0's.
Solution: At least two 1's between two occurrences of 0's can be denoted by (0111*0)*.
Similarly, if there is no occurrence of 0's, then any number of 1's are also allowed. Hence the r.e.
for required language is:
R = (1 + (0111*0))*
Example 13:
Write the regular expression for the language containing the string in which every 0 is
immediately followed by 11.
Solution:
R = (011 + 1)*
Arden's Theorem –
If P does not contain null string, then the following equation in R, viz R = Q + RP,
Here,
25
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
• It must have only initial state q1.
• Its states are q1, q2, q3,....qn. The final state may be some qi where i<=n.
• qi is the regular expression representing a set of strings accepted by the finite automata
even though qi is a final state.
To find the regular expression for the given automata, we can first create the equations in the
given form for all the states.
q1=q1α11+q2α21+----------+qnαn1+ε
q2=q1α12+q2α22+-----------+qnαn2
qn=q1α1n+q2α2n+------------+qnαnn.
By repeatedly applying substitutions and Arden's Theorem we can express qi in terms of α ij's.
To get the set of strings recognized by the Finite State Automata (FSA), we have to take the
union of all qi's corresponding to final states
Solution:
26
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
q3 = q2a
q1 = q1a + q3a + €
Method:
For ε, NFA is
27
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
For a NFA is
For a + b, or a | b NFA is
28
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example 2: Draw NFA for a + b + ab
29
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
2.7 Conversion of Regular Expression to FA:
To convert the RE to FA, we are going to use a method called the subset method. This method is
used to obtain FA from the given regular expression.
Step 1: Design a transition diagram for given regular expression, using NFA with ε moves.
Example 1:
Solution: First we will construct the transition diagram for a given regular expression.
Step 1:
Step 2:
Step 3:
Step 4:
30
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Step 5:
Now we have got NFA without ε. Now we will convert it into required DFA for that, we will
first write a transition table for this NFA.
State 0 1
→q0 q3 {q1, q2}
q1 qf ϕ
q2 ϕ q3
q3 q3 qf
*qf ϕ ϕ
State 0 1
→[q0] [q3] [q1, q2]
[q1] [qf] ϕ
[q2] ϕ [q3]
[q3] [q3] [qf]
[q1, q2] [qf] [qf]
*[qf] ϕ ϕ
31
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example 2:
Solution:
Step 1:
Step 2:
Step 3:
Step 4:
32
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example 3:
Step 1:
Step 2:
Step 3:
For showing the equivalence of two regular expressions we need to show some identities of
regular expression’s.
Let P, Q and R be the regular expressions then the identity rules are as follows –
• ∅+r=r
• ∅.r= r.∅ = ∅
• ∈.r = r.∈ =r
• ∈* = ∈ and ∅* = ∈
• r+r=r
• r*.r* = r*
• r.r* = r*.r = r+.
33
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
• (r*)* = r*
• ∈ +r.r* = r* = ∈ + r.r*
• (p.q)*.p = p.(q.p)*
• (p + q)* = (p*.q*)* = (p* + q*)*
• (p+ q).r= p.r+ q.r and r.(p+q) = r.p + r.q
1. Union -
If L1 and If L2 are two regular languages, their union L1 U L2 will also be regular.
Example:
2. Intersection -
If L1 and If L2 are two regular languages, their intersection L1 ∩ L2 will also be regular.
Example:
3. Concatenation -
If L1 and If L2 are two regular languages, their concatenation L1.L2 will also be regular.
Example:
34
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
L1 = {an | n > 0} and L2 = {bn | n > O}
4. Kleene Closure -
Example:
L1 = (a U b)
L1* = (a U b)*
5. Complement -
If L(G) is a regular language, its complement L'(G) will also be regular. Complement of a
language can be found by subtracting strings which are in L(G) from all possible strings.
Example:
6. Reverse Operator -
Example:
Proof: Let E be a regular expression for L. We show how to reverse E, to provide a regular
expression E^R for L^R.
Proof: Let A and B be DFA’s whose languages are L1 and L2, respectively. Construct C, the
product automaton of A and B make the final states of C be the pairs, where A-state is final but
B-state is not.
8. Homomorphism -
A homomorphism on an alphabet is a function that gives a string for each symbol in that
alphabet. Example: h(0) = ab; h(1) = E. Extend to strings by h(a1…an) =h(a1)…h(an).
35
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example: h(01010) = ababab.
Proof: Let E be a regular expression for L. Apply h to each symbol in E. Language of resulting
R, E is h(L).
9. Inverse Homomorphism:
Let h be a homomorphism and L a language whose alphabet is the output language of h. h^-1 (L)
= {w | h(w) is in L}.
36
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.