Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Toc U2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

UNIT-2

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.

Regular Expressions
Regular Expressions are used to denote regular languages. An expression is
regular if:
o ɸ is a regular expression for regular language ɸ.
o ɛ is a regular expression for regular language {ɛ}.
o If a ∈ Σ (Σ represents the input alphabet), a is regular expression with
language {a}.
o If a and b are regular expression, a + b is also a regular expression with
language {a,b}.
o If a and b are regular expression, ab (concatenation of a and b) is also
regular.
o If a is regular expression, a* (0 or more times a) is also regular.
o

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:

1. R = a*  

That is Kleen closure of a.

Write the regular expression for the language accepting all combinations of a's except the null string, over
the set ∑ = {a}

the regular expression has to be built for the language

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

This set indicates that there is no null string. So we can denote regular expression as:
R = a+
Write the regular expression for the language accepting all the string containing any number of a's
and b's.

The regular expression will be:

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

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:

1. 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:

1. 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:

1. L = {a, aba, aab, aba, aaa, abab, .....}  
The regular expression for the above language is:

1. 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:

1. 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:

1. L = {ε, 00, 0000, 000000, ......}  
The regular expression for the above language is:

1. 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:

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

1. 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:

1. L = {ε, 0, 1, 00, 11, 10, 100, .....}  
The regular expression for the above language is as follows:

1. R = (1* 0*)  

Example 9:
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)*  
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*.

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
 

In this article, we will discuss Arden’s Theorem.

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.

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*

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)

Also Read- Construction of DFA


 

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.


● There is a path going from state qi to state B via state A.

● So, after eliminating state A, we put a direct path from state qi to state B having cost ∈.0 = 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 qi to state qf having cost 0.(10)*.∈ = 0(10)*
 

Eliminating state B, we get-

From here,

Regular Expression = 0(10)*


1. Complementation
If a language L is regular its complement L' is regular.
Let DFA(L) denote the DFA for the language L. Modify the DFA as follows to obtain DFA(L').

1. Change the final states to non-final states.


2. Change the non-final states to final states.

Since there exists a DFA(L') now, L' is regular.


This can be shown by an example using a DFA. Let L denote the language containing strings that
begins and ends with a. Σ = {a, b}. The DFA for L is given below.
Note: q3 denotes the
dead state.
Once you enter q3,
you remain in it
forever.

L' denotes the language that does not contain strings that begin and end with a. This implies L'
contains strings that

● begins with a and ends with b


● begins with b and ends with a
● begins with b and ends with b

The DFA for L' is obtained by flipping the final states of DFA(L) to non-final states and vice-versa.
The DFA for L' is given below.
● q0 ensures ε is
accepted

● q1 ensures all strings


that begin with a and
end with b are
accepted.

● q3 ensures all strings


that begin with b
(ending with either a or
b) are accepted.

Important Note: While specifying the DFA for L, we have also included the dead state q 3. It is
important to include the dead state(s) if we are going to derive the complement DFA since, the
dead state(s) too would become final in the complementation. If we didn't add the dead state(s)
originally, the complement will not accept all strings supposed to be accepted.
In the above example, if we didn't include q 3 originally, the complement will not accept strings
starting with b. It will only accept strings that begin with a and end with b which is only a subset of
the complement.
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER COMPLEMENTATION.

Union
If L1 and L2 are regular, then L1 ∪ L2 is regular.
This is easier proved using regular expressions. If L 1 is regular, there exists a regular expression R1 to
describe it. Similarly, if L2 is regular, there exists a regular expression R2 to describe it. R1 + R2
denotes the regular expression that describe L1 ∪ L2. Therefore, L1 ∪ L2 is regular.

 Intersection
If L1 and L2 are regular, then L1 ∩ L2 is regular.

 Concatenation
If L1 and L2 are regular, then L1 . L2 is regular

 Kleene star
If L is regular, then L* is regular.

Difference
If L1 and L2 are regular, then L1 - L2 is regular.

Reverse
If L is regular, then LR is regular.
How To Minimize DFA?
 
The two popular methods for minimizing a DFA are-
 

 
In this article, we will discuss Minimization of DFA Using Equivalence Theorem.
 

Minimization of DFA Using Equivalence Theorem-


 

Step-01:
 

● Eliminate all the dead states and inaccessible states from the given DFA (if any).

Dead State
 
All those non-final states which transit to itself for all input symbols in ∑ are called as dead states.

Inaccessible State
 

All those states which can never be reached from the initial state are called as inaccessible states.

Step-02:
 
● Draw a state transition table for the given DFA.
● Transition table shows the transition of all states on all input symbols in Σ.
 
Step-03:
 
Now, start applying equivalence theorem.
● Take a counter variable k and initialize it with value 0.
● Divide Q (set of states) into two sets such that one set contains all the non-final states and
other set contains all the final states.
● This partition is called P0.
 
Step-04:
 
● Increment k by 1.
● Find Pk by partitioning the different sets of P k-1 .
● In each set of Pk-1 , consider all the possible pair of states within each set and if the two
states are distinguishable, partition the set into different sets in P k.
 

Two states q1 and q2 are distinguishable in partition Pk for any input symbol ‘a’,
if δ (q1, a) and δ (q2, a) are in different sets in partition P k-1.

 
Step-05:
 
● Repeat step-04 until no change in partition occurs.
● In other words, when you find Pk = Pk-1, stop.
 
Step-06:
 
● All those states which belong to the same set are equivalent.
● The equivalent states are merged to form a single state in the minimal DFA.
 

Number of states in Minimal DFA


= Number of sets in Pk

PRACTICE PROBLEMS BASED ON MINIMIZATION OF DFA-


 

Problem-01:
 
Minimize the given DFA-
 

Solution-
 

Step-01:
 
The given DFA contains no dead states and inaccessible states.
 

Step-02:
 
Draw a state transition table-
 
a b

→q0 q1 q2

q1 q1 q3

q2 q1 q2

q3 q1 *q4

*q4 q1 q2

Step-03:
 
Now using Equivalence Theorem, we have-
P0 = { q0 , q1 , q2 , q3 } { q4 }
P1 = { q0 , q1 , q2 } { q3 } { q4 }
P2 = { q0 , q2 } { q1 } { q3 } { q4 }
P3 = { q0 , q2 } { q1 } { q3 } { q4 }
 
Since P3 = P2, so we stop.
From P3, we infer that states q0 and q2 are equivalent and can be merged together.
So, Our minimal DFA is-
 
 

Problem-02:
 
Minimize the given DFA-
 
 

Solution-
 

Step-01:
 

● State q3 is inaccessible from the initial state.


● So, we eliminate it and its associated edges from the DFA.
 
The resulting DFA is-
 

Step-02:
 
Draw a state transition table-
 

a b

→q0 *q1 q0

*q1 *q2 *q1

*q2 *q1 *q2

 
Step-03:
 
Now using Equivalence Theorem, we have-
P0 = { q0 } { q1 , q2 }
P1 = { q0 } { q1 , q2 }
 
Since P1 = P0, so we stop.
From P1, we infer that states q1 and q2 are equivalent and can be merged together.
So, Our minimal DFA is-
 

You might also like