0% found this document useful (0 votes)
48 views46 pages

Regular Expression Notes

Uploaded by

ayushawesome12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views46 pages

Regular Expression Notes

Uploaded by

ayushawesome12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Regular Expression

o The language accepted by finite automata can be easily described by simple expressions
called Regular Expressions. It is the most effective way to represent any language.
o The languages accepted by some regular expression are referred to as Regular languages.
o A regular expression can also be described as a sequence of pattern that defines a string.
o Regular expressions are used to match character combinations in strings. String searching
algorithm used this pattern to find the operations on a string.

For instance:

In a regular expression, x* means zero or more occurrence of x. It can generate { ε, x, xx,


xxx, xxxx, .....}

In a regular expression, x+ means one or more occurrence of x. It can generate {x, xx, xxx,
xxxx, .....}

Operations on Regular Language


The various operations on regular language are:54.9M

Union: If L and M are two regular languages then their union L U M is also a union.

L U M = {s | s is in L or s is in M}

Intersection: If L and M are two regular languages then their intersection is also an
intersection.

L ⋂ M = {st | s is in L and t is in M}

Kleen closure: If L is a regular language then its Kleen closure L1* will also be a regular
language.

L* = Zero or more occurrence of language L.


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.

Regular
Regular Languages
( a∪ e ∪i∪ o ∪ u )
Expression
set of vowels {a, e, i, o, u}
a followed by 0 or (a.b*) {a, ab, abb, abbb, abbbb,….}
more b
any no. of vowels v*.c* ( where v – { ε , a ,aou, aiou, b, abcd…..} where
followed by any no. of vowels and c – ε represent empty string (in case 0
consonants consonants) vowels and 0 consonants )

Regular Grammar : A grammar is regular if it has rules of form A -> a or A ->aB


or A ->ɛ where ɛ is a special symbol called NULL.

Regular Languages : A language is regular if it can be expressed in terms of


regular expression.
Examples:

1. Which one of the following languages over the alphabet {0,1} is described by the
regular expression?
(0+1)*0(0+1)*0(0+1)*
(A) The set of all strings containing the substring 00.
(B) The set of all strings containing at most two 0’s.
(C) The set of all strings containing at least two 0’s.
(D) The set of all strings that begin and end with either 0 or 1.

Solution : Option A says that it must have substring 00. But 10101 is also a part of
language but it does not contain 00 as substring. So it is not correct option.
Option B says that it can have maximum two 0’s but 00000 is also a part of
language. So it is not correct option.
Option C says that it must contain atleast two 0. In regular expression, two 0 are
present. So this is correct option.
Option D says that it contains all strings that begin and end with either 0 or 1. But it
can generate strings which start with 0 and end with 1 or vice versa as well. So it is
not correct.

S ->aS | bS | ∊
2. Which of the following languages is generated by given grammar?

(B) {w ∈ {a,b}* | w has equal number of a’s and b’s}


(A) {an bm | n,m≥ 0}

(C) {an | n ≥ 0} ∪ {bn | n ≥ 0} ∪ {an bn | n ≥ 0}


(D) {a,b}*
Solution : Option (A) says that it will have 0 or more a followed by 0 or more b.
But S ->bS =>baS =>ba is also a part of language. So (A) is not correct.
Option (B) says that it will have equal no. of a’s and b’s. But But S ->bS => b is
also a part of language. So (B) is not correct.
Option (C) says either it will have 0 or more a’s or 0 or more b’s or a’s followed by
b’s. But as shown in option (A), ba is also part of language. So (C) is not correct.
Option (D) says it can have any number of a’s and any numbers of b’s in any order.
So (D) is correct.

3. The regular expression 0*(10*)* denotes the same set as


(A) (1*0)*1*
(B) 0 + (0 + 10)*
(C) (0 + 1)* 10(0 + 1)*
(D) none of these

Solution : Two regular expressions are equivalent if languages generated by them


are same.
Option (A) can generate all strings generated by 0*(10*)*. So they are equivalent.
Option (B) string null can not generated by given languages but 0*(10*)* can. So
they are not equivalent.
Option (C) will have 10 as substring but 0*(10*)* may or may not. So they are not
equivalent.
4. The regular expression for the language having input alphabets a and b, in which
two a’s do not come together:
(A) (b + ab)* + (b +ab)*a
(B) a(b + ba)* + (b + ba)*
(C) both options (A) and (B)
(D) none of the above
Solution:
Option (C) stating both both options (A) and (B) is the correct regular expression for the
stated question.
The language in the question can be expressed as
L={&epsilon,a,b,bb,ab,aba,ba,bab,baba,abab,…}.
In option (A) ‘ab’ is considered the building block for finding out the required regular
expression.(b + ab)* covers all cases of strings generated ending with ‘b’.(b + ab)*a covers
all cases of strings generated ending with a.
Applying similar logic for option (B) we can see that the regular expression is derived
considering ‘ba’ as the building block and it covers all cases of strings starting with a and
starting with b.

What are the rules for developing regular expressions?


Rule 1:
R.E = a+
Strings = a, aa, aaa, aaaa, aaaaa,……….
Explanation: It can generate one a and can also generate multiple a’s together.
Note: This example is for a+. Similarly, you can generate the regular expression for
any other alphabet like b+, c+, 0+ or 1+ and for any other alphabet.
 When + is used as a power, then it represents a loop that can run null time or
any time.
 When + is used as an operator between operands, then it represents the or
operator with the help of which we can chose the right side or left side.
Rule 2:
R.E = a*
Strings = Λ, a, aa, aaa, aaaa, aaaaa,……….
Explanation: It can generate Null(Λ), one a and can also generates multiple a’s
together.
Note: This example is a*. Similarly, you can generate a regular expression for any
other alphabet like b*, c*, 0* or 1* and for any other alphabet.
Rule 3:
R.E = (a+b)
Explanation: It can generate only one a or it can generate only one b. Nothing else.
Strings = a, b
Rule 4:
Every string that is part of the language should be accepted by the R.E and every
string that is not part of the language should be rejected by the R.E.
 If 99% strings that are part of the language and are accepted by the R.E and if only
1% string is the string that is part of the language and is rejected by the R.E, then R.E
is still wrong.
 If 99% strings that are not part of the language and are rejected by the R.E and if only
1% string is the string that is not part of the language and is accepted by the R.E, then
R.E is still wrong.
For example;
Language = language of all those strings starting with a, defined over ∑=(a,b)
Accepted strings: a, aa, aba…………
Rejected strings: b, ba, bb, bab, ………
R.E =a(a+b)*
Practice:
Now, let’s check how strings can be accepted by R.E.
R.E =a(a+b)*
Strings: a, aa, ab, aab, aaab, aba, ababab,………
Similarly, another example is
Language= language of all those strings starting with b, defined over ∑=(a,b)
Accepted strings: b, ba, bb, bab,…………
Rejected strings: a, aa, ab, aba,……..
R.E = b(a+b)*
Practice:
Now let us check that how strings can be accepted by R.E
R.E =b(a+b)*
Strings: b, ba, bb, bab, baab, bba, bbabab,………
1. A regular expression for the language of all those strings in which NO bab occurs in the
string. defined over {a,b}
R.E= a*b*a*

2. Regular expression for the set of all strings of a’s and b’s that have at least 2 a’s.
(a+b)* a (a+b)* a (a+b)*
Valid strings={aa, aaa, baa,aab, aba, abba,…}
Invalid strings={a, b, baa,abb, abb, abbb,…}

3. Regular expression for the set of all strings whose first symbol from the right end is a 0.
L = (0+1)*0

4. Regular expression for the set of all strings whose second symbol from the right end is a
0.
L = (0+1)*.0.(0+1)

5. Regular expression for the set of all strings whose 3rd symbol from the right end is a 0.
L = (0+1)*.0.(0+1).(0+1)
6. Describe the strings that are represented by the regular expression (0+1)*.0.(0+1).(0+1).
Valid Strings={0000,1000, 1010, and many other similar strings}
7. Regular expression for the strings that do not contain a as a string defined over {a,b}
(b)*
8. Regular expression for the strings that do not contain single a as a string defined over
{a,b}
(aa+b)*
9. A regular expression for the language of allthose strings having even length strings and
starting with a or odd length strings starting with b
RE = a(aa+bb+ab+ba)*(a+b) + b(aa+bb+ab+ba)*

A Regular Expression can be recursively defined as follows −


 ε is a Regular Expression indicates the language containing an empty string. (L (ε) = {ε})
 φ is a Regular Expression denoting an empty language. (L (φ) = { })
 x is a Regular Expression where L = {x}
 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).
o

o X . Y is a Regular Expression corresponding to the language L(X) . L(Y) where L(X.Y) =


L(X) . L(Y)
o R* is a Regular Expression corresponding to the language L(R*)where L(R*) = (L(R))*
 If we apply any of the rules several times from 1 to 5, they are Regular Expressions.

Some RE Examples
Regular Regular Set
Expressions

(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }

(0*10*) L = {1, 01, 10, 010, 0010, …}

(0 + ε)(1 + ε) L = {ε, 0, 1, 01}

(a+b)* Set of strings of a’s and b’s of any length including the null string. So L = { ε,
a, b, aa , ab , bb , ba, aaa…….}

(a+b)*abb Set of strings of a’s and b’s ending with the string abb. So L = {abb, aabb,
babb, aaabb, ababb, …………..}

(11)* Set consisting of even number of 1’s including empty string, So L= {ε, 11,
1111, 111111, ……….}

(aa)*(bb)*b Set of strings consisting of even number of a’s followed by odd number of
b’s , so L = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb, …………..}
(aa + ab + ba + bb)* String of a’s and b’s of even length can be obtained by concatenating any
combination of the strings aa, ab, ba and bb including null, so L = {aa, ab, ba,
bb, aaab, aaba, …………..}

Any set that represents the value of the Regular Expression is called a Regular Set.
Properties of Regular Sets
Property 1. The union of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(aa)* and RE2 = (aa)*
So, L1 = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)
and L2 ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}
(Strings of all possible lengths including Null)
RE (L1 ∪ L2) = a* (which is a regular expression itself)
Hence, proved.
Property 2. The intersection of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(a*) and RE2 = (aa)*
So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∩ L2 = { aa, aaaa, aaaaaa,.......} (Strings of even length excluding Null)
RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.
Hence, proved.
Property 3. The complement of a regular set is regular.
Proof −
Let us take a regular expression −
RE = (aa)*
So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length including Null)
Complement of L is all the strings that is not in L.
So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)
RE (L’) = a(aa)* which is a regular expression itself.
Hence, proved.
Property 4. The difference of two regular set is regular.
Proof −
Let us take two regular expressions −
RE1 = a (a*) and RE2 = (aa)*
So, L1 = {a, aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 – L2 = {a, aaa, aaaaa, aaaaaaa, ....}
(Strings of all odd lengths excluding Null)
RE (L1 – L2) = a (aa)* which is a regular expression.
Hence, proved.
Property 5. The reversal of a regular set is regular.
Proof −
We have to prove LR is also regular if L is a regular set.
Let, L = {01, 10, 11, 10}
RE (L) = 01 + 10 + 11 + 10
LR = {10, 01, 11, 01}
RE (LR) = 01 + 10 + 11 + 10 which is regular
Hence, proved.
Property 6. The closure of a regular set is regular.
Proof −
If L = {a, aaa, aaaaa, .......} (Strings of odd length excluding Null)
i.e., RE (L) = a (aa)*
L* = {a, aa, aaa, aaaa , aaaaa,……………} (Strings of all lengths
excluding Null)
RE (L*) = a (a)*
Hence, proved.
Property 7. The concatenation of two regular sets is regular.
Proof −
Let RE1 = (0+1)*0 and RE2 = 01(0+1)*
Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)
and L2 = {01, 010,011,.....} (Set of strings beginning with 01)
Then, L1 L2 = {001,0010,0011,0001,00010,00011,1001,10010,.............}
Set of strings containing 001 as a substring which can be represented
by an RE − (0 + 1)*001(0 + 1)*
Hence, proved.
Identities Related to Regular Expressions
Given R, P, L, Q as regular expressions, the following identities hold −

 ∅* = ε
 ε* = ε
 RR* = R*R
 R*R* = R*
 (R*)* = R*
 RR* = R*R
 (PQ)*P =P(QP)*
 (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
 R + ∅ = ∅ + R = R (The identity for union)
 R ε = ε R = R (The identity for concatenation)
 ∅ L = L ∅ = ∅ (The annihilator for concatenation)
 R + R = R (Idempotent law)
 L (M + N) = LM + LN (Left distributive law)
 (M + N) L = ML + NL (Right distributive law)
 ε + RR* = ε + R*R = R*

Examples of Regular Expression


Example 1:
Write the regular expression for the language accepting all the string which are starting with
1 and ending with 0, over ∑ = {0, 1}.

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 2:
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* a
Example 3:
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 4:
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 5:
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:

L = {ε, 00, 0000, 000000, ......}

The regular expression for the above language is:

R = (00)*

Example 6:
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 7:
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 8:
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*)

Example 9:
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:

1. R = (1 + (0111*0))*
Example 10:
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:

1. R = (011 + 1)*

Conversion of RE 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.


Some basic RA expressions are the following −
Case 1 − For a regular expression ‘a’, we can construct the following FA −

Case 2 − For a regular expression ‘ab’, we can construct the following FA −

Case 3 − For a regular expression (a+b), we can construct the following FA −

Case 4 − For a regular expression (a+b)*, we can construct the following FA −

Method
Step 1 Construct an NFA with Null moves from the given regular expression.
Step 2 Remove Null transition from the NFA and convert it into its equivalent DFA.
Problem
Convert the following RA into its equivalent DFA − 1 (0 + 1)* 0
Solution
We will concatenate three expressions "1", "(0 + 1)*" and "0"

Now we will remove the ε transitions. After we remove the ε transitions from the NDFA, we
get the following −
It is an NDFA corresponding to the RE − 1 (0 + 1)* 0. If you want to convert it into a DFA,
simply apply the method of converting NDFA to DFA
38.9M

761

How to find Nth Highest Salary in SQL

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:
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] [q3]
*[qf] ϕ ϕ
Example 2:
Design a NFA from given regular expression 1 (1* 01* 01*)*.

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

Step 1:

Step 2:

Step 3:

Example 3:
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:
Conversion of Regular Expression to Finite Automata
As the regular expressions can be constructed from Finite Automata using the State
Elimination Method, the reverse method, state decomposition method can be used to
construct Finite Automata from the given regular expressions.
Note: This method will construct NFA (with or without ε-transitions, depending on
the expression) for the given regular expression, which can be further converted to
DFA using NFA to DFA conversion.

State Decomposition Method


Theorem: Every language defined by a regular expression is also defined by a Finite
Automata.
Proof: Let’s assume L = L(R) for a regular expression R. We prove that L = L(M) for
some ε-NFA M with:
1) Exactly one accepting state.
2) No incoming edges at the initial state.
3) No outgoing edges at the accepting state.
The proof is done by structural induction on R by following the steps below:
Step 1: Create a starting state, say q1, and a final state, say q 2. Label the transition q1 to
q2 as the given regular expression, R, as in Fig 1. But, if R is (Q) *, Kleene’s closure of
another regular expression Q, then create a single initial state, which will also be the
final state, as in Fig 2.

Fig 1

Fig 2

Step 2: Repeat the following rules (state decomposition method) by considering the
least precedency regular expression operator first until no operator is left in the
expression. Precedence of operators in regular expressions is defined as Union <
Concatenation < Kleene’s Closure.
Union operator (+) can be eliminated by introducing parallel edges between the two
states as follows.
Fig 3: Removal of Union Operator

The concatenation operator (‘.’ or no operator at all) can be eliminated by


introducing a new state between the states as follows.

Fig 4: Removal of Concatenation Operator

Kleene’s Closure (*) can be eliminated by introducing self-loops on states based on


the following conditions:
1. If there is only one outgoing edge at the left-most state, i.e., A in transition A -> B,
then introduce self-loop on state A and label edge A to B as an ε-transition, as shown
in Fig 5.

Fig 5

2. Else if there is only one incoming edge at the right-most state, i.e., B in transition A
-> B, then introduce self-loop on state B and label edge A to B as an ε-transition, as
shown in Fig 6.
Fig 6

3. Else introduce a new state between two states having self-loop labeled as the
expression. The new state will have ε-transitions with the previous states as follows,
as shown in Fig 7.

Fig 7

Example:

Construct Finite Automata for the regular expression, R = (ab + ba)*


Solution:
Step 1: As the given expression, R, is of the form (Q)*, so we will create a single
initial state that will also be the final state, having self-loop labeled (ab + ba), as
shown in Fig 8. (Refer Fig 2 above)

Fig 8

Step 2:
A. As the least precedency operator in the expression is a union(+). So we will
introduce parallel edges (parallel self-loops here) for ‘ab’ and ‘ba’, as shown in Fig 9.

Fig 9

B. Now we have two labels with concatenation operators (no operator mentioned
between two variables is concatenation), so we remove them one by one by
introducing new states, q1 and q2 as shown in Fig 10 and Fig 11. (Refer Fig 4 above)

Fig 10

Fig 11

Step 3: As no operators are left, we can say that Fig 11 is the required finite automata
(NFA).
DFA to Regular Expression-

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

1. Arden’s Method
2. State Elimination Method

Arden’s Theorem-

Arden’s Theorem is popularly used to convert a given DFA to its regular expression.

It states that-
Let P and Q be two regular expressions over ∑.
If P does not contain a null string ∈, then-
R = Q + RP has a unique solution i.e. R = QP*

Conditions-

To use Arden’s Theorem, following conditions must be satisfied-


 The transition diagram must not have any ∈ transitions.
 There must be only a single initial state.

Steps-

To convert a given DFA to its regular expression using Arden’s Theorem, following steps are
followed-
Step-01:

 Form a equation for each state considering the transitions which comes towards that state.
 Add ‘∈’ in the equation of initial state.

Step-02:

Bring final state in the form R = Q + RP to get the required regular expression.

Important Notes-

Note-01:

Arden’s Theorem can be used to find a regular expression for both DFA and NFA.

Note-02:

If there exists multiple final states, then-


 Write a regular expression for each final state separately.
 Add all the regular expressions to get the final regular expression.

PRACTICE PROBLEMS BASED ON CONVERTING


DFA TO REGULAR EXPRESSION-

Problem-01:

Find regular expression for the following DFA using Arden’s Theorem-

Solution-
Step-01:

Form a equation for each state-


 A = ∈ + B.1 ……(1)
 B = A.0 ……(2)

Step-02:

Bring final state in the form R = Q + RP.

Using (1) in (2), we get-


B = (∈ + B.1).0
B = ∈.0 + B.1.0
B = 0 + B.(1.0) ……(3)

Using Arden’s Theorem in (3), we get-


B = 0.(1.0)*

Thus, Regular Expression for the given DFA = 0(10)*

Problem-02:

Find regular expression for the following DFA using Arden’s Theorem-

Solution-

Step-01:
Form a equation for each state-
 q1 = ∈ ……(1)
 q2 = q1.a ……(2)
 q3 = q1.b + q2.a + q3.a …….(3)

Step-02:

Bring final state in the form R = Q + RP.

Using (1) in (2), we get-


q2 = ∈.a
q2 = a …….(4)

Using (1) and (4) in (3), we get-

q3 = q1.b + q2.a + q3.a


q3 = ∈.b + a.a + q3.a
q3 = (b + a.a) + q3.a …….(5)

Using Arden’s Theorem in (5), we get-


q3 = (b + a.a)a*

Thus, Regular Expression for the given DFA = (b + aa)a*

Problem-03:

Find regular expression for the following DFA using Arden’s Theorem-
Solution-

Step-01:

Form a equation for each state-


 q1 = ∈ + q1.b + q2.a ……(1)
 q2 = q1.a + q2.b ……(2)

Step-02:

Bring final state in the form R = Q + RP.

Using Arden’s Theorem in (2), we get-


q2 = q1.a.b* …….(3)

Using (3) in (1), we get-


q1 = ∈ + q1.b + q1.a.b*.a
q1 = ∈ + q1.(b + a.b*.a) …….(4)

Using Arden’s Theorem in (4), we get-


q1 = ∈.(b + a.b*.a)*
q1 = (b + a.b*.a)*

Thus, Regular Expression for the given DFA = (b + a.b*.a)*

Problem-04:

Find regular expression for the following DFA using Arden’s Theorem-
Solution-

Step-01:

Form a equation for each state-


 q1 = ∈ + q1.a + q3.a ……(1)
 q2 = q1.b + q2.b + q3.b ……(2)
 q3 = q2.a …….(3)

Step-02:

Bring final state in the form R = Q + RP.

Using (3) in (2), we get-


q2 = q1.b + q2.b + q2.a.b
q2 = q1.b + q2.(b + a.b) …….(4)

Using Arden’s Theorem in (4), we get-


q2 = q1.b.(b + a.b)* …….(5)

Using (5) in (3), we get-


q3 = q1.b.(b + a.b)*.a …….(6)

Using (6) in (1), we get-


q1 = ∈ + q1.a + q1.b.(b + a.b)*.a.a
q1 = ∈ + q1.(a + b.(b + a.b)*.a.a) …….(7)
Using Arden’s Theorem in (7), we get-
q1 = ∈.(a + b.(b + a.b)*.a.a)*
q1 = (a + b.(b + a.b)*.a.a)*

Thus, Regular Expression for the given DFA = (a + b(b + ab)*aa)*

State Elimination Method-

This method involves the following steps in finding the regular expression for any given DFA-

Step-01:

Thumb Rule
The initial state of the DFA must not have any incoming edge.

 If there exists any incoming edge to the initial state, then create a new initial state having
no incoming edge to it.

Example-

Step-02:

Thumb Rule
There must exist only one final state in the DFA.

 If there exists multiple final states in the DFA, then convert all the final states into non-
final states and create a new single final state.
Example-

Step-03:

Thumb Rule
The final state of the DFA must not have any outgoing edge.

 If there exists any outgoing edge from the final state, then create a new final state having
no outgoing edge from it.

Example-

Step-04:

 Eliminate all the intermediate states one by one.


 These states may be eliminated in any order.

In the end,
 Only an initial state going to the final state will be left.
 The cost of this transition is the required regular expression.
NOTE
The state elimination method can be applied to any finite automata.
(NFA, ∈-NFA, DFA etc)

PRACTICE PROBLEMS BASED ON CONVERTING


DFA TO REGULAR EXPRESSION-

Problem-01:

Find regular expression for the following DFA-

Solution-

Step-01:

 Initial state A has an incoming edge.


 So, we create a new initial state qi.

The resulting DFA is-

Step-02:
 Final state B has an outgoing edge.
 So, we create a new final state qf.

The resulting DFA is-

Step-03:

Now, we start eliminating the intermediate states.

First, let us eliminate state A.

 So, after eliminating state A, we put a direct path from state q i to state B having cost ∈.0
 There is a path going from state qi to state B via state A.

=0
 There is a loop on state B using state A.
 So, after eliminating state A, we put a direct loop on state B having cost 1.0 = 10.

Eliminating state A, we get-

Step-04:

Now, let us eliminate state B.


 There is a path going from state qi to state qf via state B.
 So, after eliminating state B, we put a direct path from state q i to state qf having cost 0.
(10)*.∈ = 0(10)*

Eliminating state B, we get-


From here,

Regular Expression = 0(10)*

NOTE-

In the above question,


 If we first eliminate state B and then state A, then regular expression would be = (01)*0.
 This is also the same and correct.

Problem-02:

Find regular expression for the following DFA-

Solution-

Step-01:

 There exist multiple final states.


 So, we convert them into a single final state.

The resulting DFA is-


Step-02:

Now, we start eliminating the intermediate states.

First, let us eliminate state q4.


 There is a path going from state q2 to state qf via state q4.
 So, after eliminating state q4 , we put a direct path from state q2 to state qf having cost b.∈
= b.

Step-03:

Now, let us eliminate state q3.


 There is a path going from state q2 to state qf via state q3.
 So, after eliminating state q3 , we put a direct path from state q2 to state qf having cost c.∈
= c.
Step-04:

Now, let us eliminate state q5.


 There is a path going from state q2 to state qf via state q5.
 So, after eliminating state q5 , we put a direct path from state q2 to state qf having cost d.∈
= d.

Step-05:

Now, let us eliminate state q2.


 There is a path going from state q1 to state qf via state q2.
 So, after eliminating state q2 , we put a direct path from state q1 to state qf having cost a.
(b+c+d).
From here,

Regular Expression = a(b+c+d)

Problem-03:

Find regular expression for the following DFA-

Solution-

Step-01:

 Initial state q1 has an incoming edge.


 So, we create a new initial state qi.

The resulting DFA is-

Step-02:

 Final state q2 has an outgoing edge.


 So, we create a new final state qf.
The resulting DFA is-

Step-03:

Now, we start eliminating the intermediate states.

First, let us eliminate state q1.


 There is a path going from state qi to state q2 via state q1 .

∈.c*.a = c*a
 So, after eliminating state q1, we put a direct path from state qi to state q2 having cost

 There is a loop on state q2 using state q1 .


 So, after eliminating state q1 , we put a direct loop on state q2 having cost b.c*.a = bc*a

Eliminating state q1, we get-

Step-04:

Now, let us eliminate state q2.


 There is a path going from state qi to state qf via state q2 .
 So, after eliminating state q2, we put a direct path from state qi to state qf having cost
c*a(d+bc*a)*∈ = c*a(d+bc*a)*

Eliminating state q2, we get-


From here,

Regular Expression = c*a(d+bc*a)*

Problem-04:

Find regular expression for the following DFA-

Solution-

Step-01:

 State D is a dead state as it does not reach to any final state.


 So, we eliminate state D and its associated edges.

The resulting DFA is-

Step-02:

 Initial state A has an incoming edge (self loop).


 So, we create a new initial state qi.
The resulting DFA is-

Step-03:

 There exist multiple final states.


 So, we convert them into a single final state.

The resulting DFA is-

Step-04:

Now, we start eliminating the intermediate states.

First, let us eliminate state C.


 There is a path going from state B to state qf via state C.
 So, after eliminating state C, we put a direct path from state B to state q f having cost
b.b*.∈ = bb*

Eliminating state C, we get-


Step-05:

Now, let us eliminate state B.


 There is a path going from state A to state qf via state B.
 So, after eliminating state B, we put a direct path from state A to state q f having cost a.a*.
(bb*+∈) = aa*(bb*+∈)

Eliminating state B, we get-


Step-06:

Now, let us eliminate state A.


 There is a path going from state qi to state qf via state A.

∈.b*.(aa*(bb*+∈)+∈) = b*(aa*(bb*+∈)+∈)
 So, after eliminating state A, we put a direct path from state q i to state qf having cost

Eliminating state A, we get-

From here,

Regular Expression = b*(aa*(bb*+∈)+∈)

We know, bb* + ∈ = b*
So, we can also write-

Regular Expression = b*(aa*b*+∈)


Problem-05:

Find regular expression for the following DFA-

Solution-

Step-01:

 Since initial state A has an incoming edge, so we create a new initial state q i.
 Since final state A has an outgoing edge, so we create a new final state q f.

The resulting DFA is-


Step-02:

Now, we start eliminating the intermediate states.

First, let us eliminate state B.


 There is a path going from state C to state A via state B.
 So, after eliminating state B, we put a direct path from state C to state A having cost b.b =
bb.
 There is a loop on state A using state B.
 So, after eliminating state B, we put a direct loop on state A having cost a.b = ab.

Eliminating state B, we get-

Step-03:

Now, let us eliminate state C.


 There is a loop on state A using state C.
 So, after eliminating state C, we put a direct loop on state A having cost b.(a+bb) =
b(a+bb)

Eliminating state C, we get-


Step-04:

Now, let us eliminate state A.

 So, after eliminating state A, we put a direct path from state q i to state qf having cost ∈.
 There is a path going from state qi to state qf via state A.

(ab + b(a+bb))*∈ = (ab + b(a+bb))*

Eliminating state A, we get-

From here,

Regular Expression = (ab + b(a+bb))*

Problem-06:

Find regular expression for the following DFA-


Solution-

 State B is a dead state as it does not reach to the final state.


 So, we eliminate state B and its associated edges.

The resulting DFA is-

From here,

Regular Expression = a

Problem-07:

Find regular expression for the following DFA-


Solution-

Step-01:

 There exist multiple final states.


 So, we create a new single final state.

The resulting DFA is-


Step-02:

Now, we start eliminating the intermediate states.

First, let us eliminate state B.


 There is a path going from state A to state qf via state B.
 So, after eliminating state B, we put a direct path from state A to state q f having cost
a.a*.∈ = aa*.

Eliminating state B, we get-

Step-03:

Now, let us eliminate state C.


 There is a path going from state A to state qf via state C.
 So, after eliminating state C, we put a direct path from state A to state q f having cost
b.a*.∈ = ba*.

Eliminating state C, we get-


From here,

Regular Expression = aa* + ba*

You might also like