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

Regular Expression

Uploaded by

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

Regular Expression

Uploaded by

ayushawesome12
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Regular Expression

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 + String of a’s and b’s of even length can be obtained by


bb)* 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*
Minimization of DFA

The process of reducing a given DFA to its minimal form is called as minimization of DFA.

 It contains the minimum number of states.


 The DFA in its minimal form is called as a Minimal DFA.

How To Minimize DFA?

The two popular methods for minimizing a DFA are-


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

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 Pk-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 }
P 2 = { q 0 , q2 } { q 1 } { q 3 } { q 4 }
P 3 = { q 0 , q2 } { q 1 } { q 3 } { q 4 }

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-

Problem-03:
Minimize the given DFA-

Solution-
Step-01:

The given DFA contains no dead states and inaccessible states.

Step-02:

Draw a state transition table-


0 1
→q0 q1 q3
q1 q2 *q4
q2 q1 *q4
q3 q2 *q4
*q4 *q4 *q4

Step-03:

Now using Equivalence Theorem, we have-


P0 = { q0 , q1 , q2 , q3 } { q4 }
P1 = { q0 } { q1 , q2 , q3 } { q4 }
P2 = { q0 } { q1 , q2 , q3 } { q4 }

Since P2 = P1, so we stop.


From P2, we infer that states q1 , q2 and q3 are equivalent and can be merged together.
So, Our minimal DFA is-

Problem-04:
Minimize the given DFA-
Solution-
Step-01:

 State q5 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-

0 1
→q0 q1 q2
q1 q2 *q3
q2 q2 *q4
*q3 *q3 *q3
*q4 *q4 *q4

Step-03:

Now using Equivalence Theorem, we have-


P0 = { q0 , q1 , q2 } { q3 , q4 }
P1 = { q0 } { q1 , q2 } { q3 , q4 }
P2 = { q0 } { q1 , q2 } { q3 , q4 }

Since P2 = P1, so we stop.


From P2, we infer-
 States q1 and q2 are equivalent and can be merged together.
 States q3 and q4 are equivalent and can be merged together.
So, Our minimal DFA is-
DFA Minimization using Myphill-Nerode Theorem
Algorithm

Input − DFA

Output − Minimized DFA

Step 1 − Draw a table for all pairs of states (Qi, Qj) not necessarily connected directly [All are
unmarked initially]

Step 2 − Consider every state pair (Qi, Qj) in the DFA where Qi ∈ F and Qj ∉ F or 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 is an unmarked pair (Qi, Qj), mark it if the pair {δ (Qi, A), δ (Qi, A)} is marked for
some input alphabet.

Step 4 − Combine all the unmarked pair (Qi, Qj) and make them a single state in the reduced
DFA.

Example

Let us use Algorithm 2 to minimize the DFA shown below.

Step 1 − We draw a table for all pair of states.


A b c d e f

Step 2 − We mark the state pairs.

A b c d e f

c ✔ ✔

d ✔ ✔

e ✔ ✔

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

A b c d e f

c ✔ ✔

d ✔ ✔

e ✔ ✔

f ✔ ✔ ✔ ✔ ✔

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}

DFA Minimization using Equivalence Theorem


If X and Y are two states in a DFA, we can combine these two states into {X, Y} if they are
not distinguishable. Two states are distinguishable, if there is at least one string S, such that
one of δ (X, S) and δ (Y, S) is accepting and another is not accepting. Hence, a DFA is
minimal if and only if all the states are distinguishable.

Algorithm 3

Step 1 − All the states Q are divided in two partitions − final states and non-final states and
are denoted by P0. All the states in a partition are 0th equivalent. Take a counter k and
initialize it with 0.

Step 2 − Increment k by 1. For each partition in Pk, divide the states in Pk into two partitions
if they are k-distinguishable. Two states within this partition X and Y are k-distinguishable if
there is an input S such that δ(X, S) and δ(Y, S) are (k-1)-distinguishable.

Step 3 − If Pk ≠ Pk-1, repeat Step 2, otherwise go to Step 4.

Step 4 − Combine kth equivalent sets and make them the new states of the reduced DFA.

Example

Let us consider the following DFA –


q δ(q,0) δ(q,1)
a b c
b a d
c e f
d e f
e e f
f f f

Let us apply the above algorithm to the above DFA −

 P0 = {(c,d,e), (a,b,f)}
 P1 = {(c,d,e), (a,b),(f)}
 P2 = {(c,d,e), (a,b),(f)}

Hence, P1 = P2.

There are three states in the reduced DFA. The reduced DFA is as follows −

Q δ(q,0) δ(q,1)
(a, b) (a, b) (c,d,e)
(c,d,e) (c,d,e) (f)
(f) (f) (f)

You might also like