0% found this document useful (0 votes)
34 views

ATCD UT2 Material

ATCD MATERIAL OF OUR COLLEGE

Uploaded by

RAHUL M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

ATCD UT2 Material

ATCD MATERIAL OF OUR COLLEGE

Uploaded by

RAHUL M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit – II: Finite Automata with Epsilon-transitions and Regular Expressions

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.

2.0 NFA with Epsilon Transitions:


The ε transitions in NFA are given in order to move from one state to another state without
having any symbol or with from only (null) or ε moves including the self-state from input set Σ.

Example:

Consider the following figure of NFA with ε move −

The transition state table for the above NFA is as follows –

State 0 1 (ε)epsilon
A B,C A B
B - B C
C C C -

For the above example, ε closure is as follows −

ε - closure(A) : {A, B,C} means self-state + ε – reachable states.

ε - closure(B) :{B,C} means B is a self-state and C is a state obtained from B with ε input.

ε - closure(C) : {C}

2.0.1 Language of NFA with ε –

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

δ is a transition function from Q x { Σ U ε } to 2Q

q0 is start state

F is a final state

The string w in L accepted by NFA can be represented as follows −

L(M)={w| w ∈ Σ* and 𝛿 transition for w from q0 reaches to F}

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.

Hence NFA with epsilon can be as follows –

Normally, epsilon is not shown in the input string.

The NFA with epsilon is as follows −

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.

Step 2 − q1 on ‘b’ goes to q1 itself and on epsilon transition it goes to q2.

2
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Step 3 − q2 on ‘c’ goes to q2 itself which is the final state.

The transition table is as follows −

State\ input
a b c ε
symbol
q0 q0 - - q1

q1 - q1 - q2

q2 - - q2 -

Consider the string aabbcc as given below –

δ(q0, aabbcc) |- δ(q0,abbcc)

|- δ(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.

2.0.2 Definition of ε – closure:

The ε – closure (p) is a set of all states which are reachable from state p on ε – transitions such
that:

1. ε – closure (p) = p where p ϵ Q.


2. If there exists ε – closure (p) = {q} and (q, ε) = r then ε – closure (p) = {q, r}.

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) −

The method is mentioned below stepwise −

• 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.

NFA with ε to without ε is as follows −

δ'(q, a) = ε - closure (δ (δ^(q,∈ ε),a)) where, δ^(q,∈ ε) = ε - closure(q)

Example 1:

Convert the given NFA with epsilon to NFA without epsilon.

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, 0) ∪ δ(q1, 0) U δ(q2, 0) )

= ε-closure(q0 U Φ ∪ Φ)

= ε-closure(q0)

= {q0, q1, q2}

δ'(q0, 1) = ε-closure(δ(δ^(q0, ε),1))

= ε-closure(δ(q0,q1,q2), 1))

= ε-closure(δ(q0, 1) ∪ δ(q1, 1) U δ(q2, 1) )

= ε-closure(Φ ∪q1 U Φ)

= ε-closure(q1)

= {q1, q2}

δ'(q0, 2) = ε-closure(δ(δ^(q0, ε),2))

= ε-closure(δ(q0,q1,q2), 2))

= ε-closure(δ(q0, 2) ∪ δ(q1, 2) U δ(q2, 2) )

= ε-closure(Φ U ΦU q2)

= ε-closure(q2)

= {q2}

δ'(q1, 0) = ε-closure(δ(δ^(q1, ε),0))

= ε-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}

δ'(q1, 2) = ε-closure(δ(δ^(q1, ε),2))

= ε-closure(δ(q1,q2), 2))

= ε-closure(δ(q1, 2) U δ(q2, 2) )

= ε-closure(Φ ∪ q2)

= ε-closure(q2)

= {q2}

δ'(q2, 0) = ε-closure(δ(δ^(q2, ε),0))

= ε-closure(δ(q2), 0))

= ε-closure(δ(q2, 0))

= ε-closure(Φ)

δ'(q2, 1) = ε-closure(δ(δ^(q2, ε),1))

= ε-closure(δ(q2), 1)

= ε-closure(δ(q2, 1))

= ε-closure(Φ)

δ'(q2, 2) = ε-closure(δ(δ^(q2, ε),))

= ε-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 –

δ'(q0,0)={q0,q1,q2} δ'(q1,0)= { Φ } δ'(q2,0)={ Φ }


δ'(q0,1)={q1,q2} δ'(q1,1)={q1,q2} δ'(q2,1)={ Φ }
δ'(q0,2)={q2} δ'(q1,2)={q2} δ'(q2,2)={q2}

The transition table is given below –

States\inputs 0 1 2
q0 {q0,q1,q2} {q1,q2} {q2}
q1 Φ {q1,q2} {q2}
q2 Φ Φ {q2}

The NFA will be –

Here, q0, q1, q2 are final states because ε-closure(q0), ε-closure(q1) and ε-closure(q2) contain a
final state q2.

Exercise Problem 2:

Convert the following NFA with ε to NFA without ε.

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.

2.2.1 Practice problems based on converting NFA to DFA:


Problem 01: Convert the given NFA to 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}

Now we will obtain δ' transition for state q0.

δ'([q0], 0) = [q0]

δ'([q0], 1) = [q1]

The δ' transition for state q1 is obtained as:


8
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
δ'([q1], 0) = [q1, q2] (new state generated)

δ'([q1], 1) = [q1]

The δ' transition for state q2 is obtained as:

δ'([q2], 0) = [q2]

δ'([q2], 1) = [q1, q2]

Now we will obtain δ' transition on [q1, q2].

δ'([q1, q2], 0) = δ(q1, 0) ∪ δ(q2, 0)

= {q1, q2} ∪ {q2}

= [q1, q2]

δ'([q1, q2], 1) = δ(q1, 1) ∪ δ(q2, 1)

= {q1} ∪ {q1, q2}

= {q1, q2}

= [q1, q2]

The state [q1, q2] is the final state as well because it contains a final state q2.

The transition table for the constructed DFA will be:

State 0 1
→[q0] [q0] [q1]
[q1] [q1, q2] [q1]
*[q2] [q2] [q1, q2]
*[q1, q2] [q1, q2] [q1, q2]

The transition diagram will be as follows -

The state q2 can be eliminated because q2 is an unreachable state.


9
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Problem 02: Convert the given NFA to DFA.

Solution:

For the given transition diagram we will first construct the transition table.

State 0 1
→q0 {q0, q1} {q1}
*q1 ϕ {q0, q1}

Now we will obtain δ' transition for state q0.

δ'([q0], 0) = {q0, q1}

= [q0, q1] (new state generated)

δ'([q0], 1) = {q1} = [q1]

The δ' transition for state q1 is obtained as:

δ'([q1], 0) = ϕ

δ'([q1], 1) = [q0, q1]

Now we will obtain δ' transition on [q0, q1].

δ'([q0, q1], 0) = δ(q0, 0) ∪ δ(q1, 0)

= {q0, q1} ∪ ϕ

= {q0, q1}

= [q0, q1]

Similarly,

δ'([q0, q1], 1) = δ(q0, 1) ∪ δ(q1, 1)

= {q1} ∪ {q0, q1}

= {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]}.

The transition table for the constructed DFA will be:

State 0 1
→[q0] [q0, q1] [q1]
*[q1] ϕ [q0, q1]
*[q0, q1] [q0, q1] [q0, q1]

The Transition diagram will be:

Even we can change the name of the states of DFA.

Suppose

A = [q0]

B = [q1]

C = [q0, q1]

With these new names the DFA will be as follows:

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).

Let T' be a new transition table of the DFA.

Step-02:

Add transitions of start state q0 to the transition table T'.

State / Alphabet a b
→q0 q0 {q0, q1}
Step-03:

New state present in state Q' is {q0, q1}.

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:

New state present in state Q' is {q0, q1, q2}.

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.

Finally, Transition table for Deterministic Finite Automata (DFA) is-

State / Alphabet a b
→q0 q0 {q0, q1}
{q0, q1} q0 *{q0, q1, q2}
*{q0, q1, q2} q0 *{q0, q1, q2}

Now, Deterministic Finite Automata (DFA) may be drawn as-

13
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Problem 05:

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 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).

Let T' be a new transition table of the DFA.

Step-02:

Add transitions of start state q0 to the transition table T'.

State / Alphabet 0 1
→q0 q0 {q1, q2}

Step-03:

New state present in state Q' is {q1, q2}.

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:

New state present in state Q' is {q0, q1, q2}.

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.

Finally, Transition table for Deterministic Finite Automata (DFA) is-

State / Alphabet 0 1
→q0 q0 *{q1, q2}
*{q1, q2} *{q0, q1, q2} *{q1, q2}
*{q0, q1, q2} *{q0, q1, q2} *{q1, q2}

Now, Deterministic Finite Automata (DFA) may be drawn as-

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

Output − Minimized 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]

Step 3: Repeat this step until we cannot mark anymore 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.

Example 1: Minimize the DFA shown below.

Step 1: Create the pairs of all the states involved in 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.

a. Check for the unmarked pair Q2,Q1

• 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.

b. Check for the unmarked pair Q3,Q0

• 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.

c. Check for the unmarked pair Q4,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.

d. 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.
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.

e. Check for the unmarked pair Q5,Q1

• 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.

f. Check for the unmarked pair Q5,Q2

• 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.

i. Check for the unmarked pair Q2,Q1

• 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.

ii. Check for the unmarked pair Q3,Q0

• 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.

Example 2: Minimize the DFA shown below.

19
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Step 1 − Draw a table for all pairs of states.

Step 2 − Mark all pairs where P ∈ F and Q ∉ F.

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.

We can recombine {c, d} {c, e} {d, e} into {c, d, e}

Hence we got two combined states as − {a, b} and {c, d, e}

So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}

2.4 Regular Expressions and Languages:


A regular expression can also be described as a sequence of pattern that defines a string.

A Regular Expression can be recursively defined as follows –

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 (ε) = {ε})

2. φ is a Regular Expression denoting an empty language. (L (φ) = { }) i.e., empty set.

3. x is a Regular Expression where L = {x}

4. If X is a Regular Expression denoting the language L(X) and Y is a Regular Expression


denoting the language L(Y), then

• X + Y is a Regular Expression corresponding to the language L(X) ∪ L(Y) where


L(X+Y) = L(X) ∪ L(Y) i.e. union.
• X . Y is a Regular Expression corresponding to the language L(X) . L(Y) where L(X.Y)
= L(X) . L(Y) i.e. concatenation.
• R* is a Regular Expression corresponding to the language L(R*) where L(R*) = (L(R))*
i.e. closure.

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*

That is Kleen closure of 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:

The regular expression has to be built for the language

L = {a, aa, aaa, ....}

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:

The regular expression will be: r.e. = (a + b)*

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:

The regular expression will be:

R = a b* b a

Example 6:

Write the regular expression for the language starting with a but not having consecutive b's.

Solution: The regular expression has to be built for the language:

L = {a, aba, aab, aba, aaa, abab, .....}

The regular expression for the above language is:

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:

The regular expression has to be built for the language:

23
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
L = {ε, 00, 0000, 000000, …………….}

The regular expression for the above language is:

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:

The regular expression will be:

R = [(0 + 1)* 0 (0 + 1)* 1 (0 + 1)*] + [(0 + 1)* 1 (0 + 1)* 0 (0 + 1)*]

Example 10:

Describe the language denoted by following regular expression

r.e. = (b* (aaa)* b*)*

Solution:

The language can be predicted from the regular expression by finding the meaning of it. We will
first split the regular expression as:

r.e. = (any combination of b's) (aaa)* (any combination of b's)

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:

The Language is as follows:

L = {ε, 0, 1, 00, 11, 10, 100, .....}

The regular expression for the above language is as follows:

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:

The regular expectation will be:

R = (011 + 1)*

2.5 Conversion from DFA to Regular Expression:


The two popular methods for converting a DFA to its regular expression are-

Arden's Theorem –

Let P and Q be the two regular expressions.

If P does not contain null string, then the following equation in R, viz R = Q + RP,

Which has a unique solution by R = QP*

Here,

• The finite automaton (FA) does not have epsilon moves.

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.

Use of Arden's Theorem to find RE of DFA –

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

Example: Find the regular expression for the following DFA.

Solution:

q 1= q1a + q3a + € ( € move is because q1is the initial state)

q2 = q1b + q2b + q3b

26
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
q3 = q2a

Now, we will solve these equations, as shown below−

q2 = q1b + q2b + q3b

= q1b + q2b + (q2a)b (Substituting value of q3)

= q1b + q2(b + ab)

= q1b (b + ab) * (Applying Arden's Theorem)

q1 = q1a + q3a + €

= q1a + q2aa + € (Substituting value of q3)

= q1a + q1b(b + ab*)aa + € (Substituting value of q2)

= q1(a + b(b + ab) *aa) + €

= € (a+ b(b + ab) *aa) *

= (a + b(b + ab) *aa) *

Hence, the regular expression is (a + b(b + ab) *aa) *.

2.6 Conversion of Regular Expression to NFA:


A Regular Expression is a representation of Tokens. But, to recognize a token, it can need a
token Recognizer, which is nothing but a Finite Automata (NFA). So, it can convert Regular
Expression into NFA.

Algorithm for the conversion of Regular Expression to NFA

Input − A Regular Expression R

Output − NFA accepting language denoted by R

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

For ab, NFA is

For a*, NFA is

Example 1: Draw NFA for the Regular Expression a(a+b)*ab

28
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example 2: Draw NFA for a + b + ab

Example 3: Draw NFA for letter (letter + digit)*

Example 4: Draw NFA corresponding to (0+1)*1(0+1)

Exercise Problem 5: Construct NFA for the regular expression b+ba*.

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.

This method is given below:

Step 1: Design a transition diagram for given regular expression, using NFA with ε moves.

Step 2: Convert this NFA with ε to NFA without ε.

Step 3: Convert the obtained NFA to equivalent DFA.

Example 1:

Design a FA from given regular expression 10 + (0 + 11)0* 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 ϕ ϕ

The equivalent DFA will be:

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:

Construct the FA for regular expression 0*1 + 10.

Solution:

We will first construct FA for R = 0*1 + 10 as follows:

Step 1:

Step 2:

Step 3:

Step 4:

32
Mr. K. Leela Prasad, Asst. Prof. CSE Dept.
Example 3:

Design a DFA from given regular expression 1 (1* 01* 01*)*.

Solution: The DFA for the given regular expression is as follows:

Step 1:

Step 2:

Step 3:

2.8 Algebraic Laws of RE:


The two regular expression’s P and Q are equivalent (denoted as P=Q) if and only if P represents
the same set of strings as Q does.

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

2.8.1 Problems for Equivalence of two:

Example: Prove (1 + 00*1) + (1 +00*1) (0+10*1)* (0+10*1) = 0*1(0+10*1)

2.9 Closure Properties of Regular Expressions:


Closure properties on regular languages are defined as certain operations on regular language
which are guaranteed to produce regular language. Closure refers to some operation on a
language, resulting in a new language that is of same “type” as originally operated on i.e.,
regular.

Regular languages are closed under following operations.

1. Union -

If L1 and If L2 are two regular languages, their union L1 U L2 will also be regular.

Example:

L1 = {an | n > O} and L2 = {bn | n > O}

L3 = L1 U L2 = {an U bn | n > O} is also regular.

2. Intersection -

If L1 and If L2 are two regular languages, their intersection L1 ∩ L2 will also be regular.

Example:

L1= {am bn | n > 0 and m > O} and

L2= {am bn U bn am | n > 0 and m > O}

L3 = L1 ∩ L2 = {am bn | n > 0 and m > O} are also regular.

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}

L3 = L1.L2 = {am . bn | m > 0 and n > O} is also regular.

4. Kleene Closure -

If L1 is a regular language, its Kleene closure L1* will also be regular.

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:

L(G) = {an | n > 3} L'(G) = {an | n <= 3}

6. Reverse Operator -

Given language L, L^R is the set of strings whose reversal is in L.

Example:

L = {0, 01, 100};

L^R ={0, 10, 001}.

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.

7. Set Difference operator –

If L1 and L2 are regular languages, then so is L1 – L2 = strings in L1 but not L2.

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.

If L is a regular language, and h is a homomorphism on its alphabet, then h(L) = {h(w) | w is in


L} is also a regular language.

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.

You might also like