Unit-Ii Regular Expressions and Languages Definition
Unit-Ii Regular Expressions and Languages Definition
Definition:
= L+ - {ε}
Definition:
Let ∑ be an alphabet. The regular expressions over ∑ and the sets that they denote
are defined recursively as follows:
i. ϕ is a regular expression and denotes the empty set.
ii. ε is a regular expression and denotes the set {ε}.
iii. For each a ϵ ∑, ‘a’ is a regular expression and denotes the set {a}.
iv. If r and s are regular expressions denoting the language R and S
respectively then (r+s), (rs), (r)* are regular expressions that denotes the
sets R∪S, RS and R respectively.
Example 1:
Write a regular expression for the language accepting all combination of a’s over
the set ∑ = {a}.
Solution:
All combinations of a’s means a may be single, double, triple and so on.
There may be the case that ‘a’ is appearing for zero times, which means a
null string. So L={ε,a,aa,aaa,aaaa,...}.
R=a*
Example 2:
Design the regular expression for the language accepting all combinations of a’s
except the null string over ∑={a}.
Solution:
R=a+
Example 3:
Design the regular expression for the language containing all the strings
containing any number of a’s and b’s.
Solution:
R = (a+b)*
Example 4:
Construct the regular expression for the language accepting all the strings which
are ending with 00 over the set ∑={0,1}.
Solution:
R = (0+1)*00
Example 5:
Write the regular expression for the language accepting the string which are
starting with 1 and ending with 0 over the set ∑={0,1}.
Solution:
R = 1(0+1)*0
Example 6:
Solution:
L(r) = {a,ab,aab,abab,.....}
I1 : ϕ+R=R
I2 : ϕR =Rϕ = R
I3 : λR = Rλ =R
I4 : λ* = λ
I5 : R+R=R
I6 : R*R* = R*
I7 : RR* = R*R
I8 : (R*)* = R*
I9 : λ + RR* = R* = λ + R*R
Regular Expressions
Can be Can be
converted to converted to
Can be
Can be converted
NFA without ε-
converted to
moves
to
Theorem:
Let r be a regular expression, then there exists a NFA with ε transition that accepts
L(r).
Proof:
q0
If r= ϕ, then
q0 q1
If r = a, then
a
q0 q1
2. Induction:
This theorem can be true for n number of operators.
The regular expression contains equal to or more than one operator.
In any type of regular expression there are only three cases possible.
o Union
o Concatenation
o Closure
Star
Positive
The δ is denoted by
i. δ(q0,ε) = {Q1,Q2}
ii. δ(q0,a) = δ1(q,a) for q in Q1 – {f1} and a in ∑1 ∪ {ε}.
iii. δ(q,a) = δ2(q,a) for q in Q2 – {f2} and a in ∑2 ∪ {ε}.
iv. δ(f,ε) = δ1(f2,ε) = {f0}.
q1 M1 f1
ε
ε
Start
f0
q0
ε
ε
q2 M2 f2
Start ε
q1 M1 f1 q2 M2 f2
The initial state q1 by some input a, the next state will be f1.
And on receiving ε, the transitions will be from f1 to q2 and the final state
will be f2.
The transition from q2 to f2 will be on receiving some input b.
Thus L(M) = ab
i.e) a is in L(M1), b is in L(M2).
Start ε ε
q0 q1 M1 f1 f0
ε
The machine M1 shows that from q0 to q1, there is a transition on receiving
ε.
Similarly from q0 to f0 on ε, there is a path.
The path exists from f1 to q1 a back path.
Similarly a transition from f1 to f0 final state on receiving ε.
Thus one can derive ε,a,aa,aaa,... for the input a.
Thus L(M) = L(M1)* is proved.
Start 0 1
q0 q1 q2
Example 2:
ε
Start
q0 q1 q2 q3
ε a ε
ε
Example 3:
Start
q0 q1 q2 q3
ε a ε
Example 4:
Start ε b
q0 q1 q2 q3 q4 q5
ε a ε
Example 5:
Start b ε ε
q0 q1 q2 q3 q4
a
Example 6:
b ε
q1 q2
ε
ε
Start ε ε
q6 q7 q8 q9
q0 q5
a
ε ε ε
q3 q4
b
Example 7:
0
q2 q3 ε
ε
Start ε ε
q0 q1 q6 q7
ε ε
1
q4 q5
Exercise Problems:
i. R = (01+10)*
ii. R = 0(00)*
iii. R = (01+2*)1
iv. R = (a(aa)*b+ab*a)*
Theorem:
Formula:
The formula for getting the language rijk is given by the regular expression:
Example 1:
Start 0
q0 q1
Solution:
k=0
r11(0) = ε
r12(0) = 0
r21(0) = ϕ
r22(0) = ε
k=1 i=j=k=1
= ε (ε)* ε + ε
= ε+ε
r11(1) = ε
= ε (ε)* 0 + 0
= 0+0
r12(1) = 0
= ϕ. ε*. ε + ε
= ϕ+ϕ
r21(1) = ϕ
= ϕ. ε*.0 + ε
= ϕ.0 + ε
= ϕ+ε
r22(1) = ε
Now let us compute for final state which denotes the regular expression.
R12(2) will be computed, because there are total 2 states and final state is q2,
whose start state is q1.
r21 (2) = (r12 (1)) (r22 (1))*(r22 (1)) + (r12 (1))
= 0. (ε)*. ε + 0
= 0+0
(2)
r21 = 0 which is a final regular expression.
Example 2:
Start 0
q1 q2
Solution:
k=0
r11 (0) = ε
r12 (0) = 0
r21 (0) = ϕ
= ε (ε*) ε + ε
r11 (1) = ε
= ε (ε*) 0 + 0
= ε. 0 + 0
= 0+0
r12 (1) = 0
r21 (1) = r21 (0) (r11 (0))* (r11 (0)) + (r21 (0))
= ϕ (ε)* ε + ϕ
= ϕ+ϕ
r21 (1) = ϕ
r22 (1) = r21 (0) (r11 (0))* (r12 (0)) + (r22 (0))
= ϕ (ε)*. 0 + (1+ε)
= ϕ + (1+ε)
Now let us compute for final state, which denotes the regular expression.
Here
o Start state i = 1
o Final state j = 2 (q2)
o Total number of states, k = 2. (q1,q2)
r12 (2) = (r21 (1)) (r22 (1))* (r22 (1)) + (r21 (1))
= 0 (1+ε)* (1+ε) + 0
= 0 (1*) (1) + 0
= 0.1* + 0
This is a final regular expression. This is a language beginning with 0 and followed by
any number of 1’s.
Exercise Problem:
Start 0 1
q1 q2 q3
0,1
Steps:
Step 1:
Find the ε-closure of the state q0 from the constructed ε-NFA that is, from state
q0, ε-transition to other states is identified as well as ε-transitions from other states are
identified and combined as one set (new state).
Step 2:
Perform the following steps until there are no more new state has been
constructed.
i. Find the transition of the given regular expression symbols over ∑ from the
new state, that is move(new state, symbol)
ii. Find the ε-closure of move(new state, symbol)
Example:
Solution:
ε regular expression is as follows:
Equivalent ε-NFA for the given
a
3 4 ε
ε
Start ε ε a b
1 2 7 8 9 10
9
ε
ε
5 6
b
Move(C,b) = {6}
Transition Table:
State Input
a b
->A B C
B B D
C B C
*D B C
Equivalent DFA is
a
Start a b
A B C
a
a
b
b
D
Examples:
1. (aa* | bb*)
2. (a|b) (a|b) (a|b)
3. (a|b)*abb(a|b)*
4. 011(0|1)*
5. 4(0|123)*
6. (12|543)11
7. (a|b)* (a|b)*
8. (0|1)011(0|1)*
Basic formula on regular expression:
1. (ε+1)* = 1*
2. 1*(ε+1) = 1*
3. (ε+1)+1* = 1*
4. 0+1*0 = 1*0
5. 0+01* = 01* = 0(ε+1*)
6. ε+00* = 0*
ε+0+ = 0*
7. (1+0)* = (1*0*)*
8. ϕ 0 = ϕ
9. ϕ + 0 = 0
10. ϕ* = ε
11. 11* ≠ 1*
12. 1*1 ≠ 1*
13. (01)* ≠ 0*1*
14. ε0 = 0
15. (01)*0 = 0(10)*
16. (0*1*)0* = (0+1)*
17. RR* = R+
18. ε+R+ = R*
19. 00* = 0* (0+) 00*=0+
20. RR* = R*
Start 0 1
q1 q2 q3
0,1
Solution:
i = initial state = q1 = 1
k = number of states = 3
R12(3) + R13(3)
k = 0:
R11 (0) = ε
R12 (0) = 0
R13 (0) = 1
R21 (0) = 0
R22 (0) = ε
R23 (0) = 1
R31 (0) = ϕ
R33 (0) = ε
k = 1:
R11 (1) = R11 (0) + R11 (0) (R11 (0))* R11 (0)
= ε + ε ε* ε
R11 (1) = ε
R12 (1) = R12 (0) + R11 (0)( R11 (0))* R12 (0)
= 0+εε*0
= 0+0
R12 (1) = 0
R13 (1) = R13 (0)+ R11 (0) (R11 (0)) *R13 (0)
= 1 + ε ε*1 = 1 + 1
R13 (1) = 1
R21 (1) = R21 (0)+ R21 (0) (R11 (0))* R11 (0)
= 0 + 0 ε* ε = 0+ 0
R21 (1) = 0
R22 (1) = R22 (0)+ R21 (0)( R11 (0))* R12 (0)
= ε + 0 ε* 0 = ε + 00
R22 (1) = 00
R23 (1) = R23 (0)+ R21 (0)( R11 (0))* R13 (0)
= 1 + 0 ε* 1 = 1 + 01
R23 (1) = 1 + 01
R31 (1) = R31 (0)+ R31 (0)( R11 (0))* R11 (0)
= ϕ + ϕ ε* ε = ϕ + ϕ
R31 (1) = ϕ
R32 (1) = R32 (0)+ R31 (0)( R11 (0))* R12 (0)
= (0+1) + ϕ ε*0 = 0 + 1
R32 (1) = 0 +1
R33 (1) = R33 (0)+ R31 (0)( R11 (0))* R13 (0)
= ε + ϕ ε* 1 = ε + ϕ
R33 (1) = ε
k=2:
R11 (2) = R11 (1) + R12 (1) (R22 (1))* R22 (1)
R12 (2) = R12 (1) + R12 (1) (R22 (1))* R22 (1)
= 0 + 0 (00)* 00
R13 (2) = R13 (1) + R12 (1) (R22 (1))* R23 (1)
= 1+ 0(00) * (1+01)
= 1 + 0(00)* (1 + (ε + 0))
= 1+ 0 (00)* (ε + 0) 1
= 1 + 0(00)*01
= 1 + 00(00)* 1
= 1+ (ε + 00*)
R21 (2) = R21 (1)+ R22 (1) (R22 (1) )* R21 (1)
= 0 + 00 (00)* 0
= 0 + (00)* 0
= 0 (ε + (00)*)
R22 (2) = R22 (1)+ R22 (1)( R22 (1))* R22 (1)
= 00 + 00 (00)* 00
= 00 (ε + 00 (00)*)
= 00 (00)*
R23 (2) = R23 (1)+ R22 (1)( R22 (1))* R23 (1)
= (1+01) (00)*
= 1 (ε + 0) (00)*
= 10(00)*
= 1(00)*
R31 (2) = R31 (1)+ R32 (1)( R22 (1))* R21 (1)
= ϕ + (0+1)(00)* 0
R32 (2) = R32 (1)+ R32 (1)( R22 (1))* R22 (1)
= (0 +1) + (ε + (00)*)
R33 (2) = R33 (1)+ R32 (1)( R22 (1))* R23 (1)
= ε +(0+1)(00)*(1+01)
= ε +(0+1)(00)*1(ε+ 0)
R13 (3) = R13 (2)+ R13 (2)( R33 (2))* R33 (2)
R12 (3) = R12 (2) + R13 (2) ( R33 (2))* R32 (2)
= 0(00)* + 10 ((0+1)0*1)((0+1)(00)*)
= 0(00)* + 10 ((0+1)0*1)*((0+1)(00)*)
R = 0(00)* + 10*[((0+1)0*1)*((0+1)(00)*)]
Theorem:
Proof:
Induction:
The formula for getting the language rijk is given by regular expression
To get the final regular expression, we have to simply get the language rijn , where
'i' indicates start state, 'j' indicates the final state and 'n' will be number of items.
If there are 'p' number of paths, leading to final state by
rijk = rij1n + rij2n +...+ rijpn
where F is a set of final state.
F = {qj1,qj2,...qjp}
Arden's method for converting DFA to Regular expression:
R =Q + RP i.e) R = QP*
Q+ RP = Q + (QP*)P
= Q (ɛ + P*P)
= QP*
=R
Algorithm:
1. Let q1 be the initial state.
2. There are q2,q3,q4,...qn number of states. The final state may be some qj where j≤n.
3. Let αji represents transition from qj to qi.
4. Calculate qi such that qi = αjiqi
If qi is a start state qi = αjiqj + ɛ.
5. Similarly compute the final state which ultimately gives the regular expression.
Example 1:
Construct regular expression from the given DFA.
0 1
Start 1
q1 q2
0
0
q3
Solution:
q1 = q10 + q30 + ɛ ------------------- ①
q3 = q20 ---------------------------③
substitute ③ to ②
R P Q
substitute ④ to ①
q1 = q10 + q200 + ɛ
q1 = q1 [ 0 + 1 (1 + 01)*00] + ɛ ∴ Q + RP = QP*
R P Q
q1 = [0 + 1 (1 + 01 )* 00]*
q1 = [ 0 + 1 ( 1 + 01 )* 00 ]*
Example 2:
b
a
Start b a
q1 q2 q3
Solution:
q1 = q1 a + ɛ ----------------------------------> ①
q2 = q1 b + q 2 b ----------------------------------> ②
we can rewrite ① as
q1 = ɛ + q1 a ∴ Q + RP = QP*
Q=ɛ
R = q1
P=a
q1 = ɛ a*
q1 = a* ∴ ɛ.R = R
q2 = q1 b + q 2 b
q2 = a*b + q2b
∴ = Q + RP
= a*bb* ∴ b.b* = b+
q2 = a*b+
Exercise Problems:
0 1 0,1
Start 1 0
q1 q2 q3
Start
q1 q2
0
1 0
q3 q4
1
3. Construct regular expression for the given DFA.
a,b
q2
b
Start
q1
a
q3
a,b
0
1
Start 0 1 0
A B C D
0 1
1 0 1
0
1 1 0
E F G H
0
Solution:
Transition Table
Input
State
0 1
→A B F
B G C
*C A C
D C G
E H F
F C G
G G E
H G C
Construct a table as follows:
B X
C X X
D X XX
E X
X X
FX XX X
GX X
X X X X
HX X X X X X
A B C D E F G
A = E, B = H, D = F are equivalent.
Transition Table: Minimized transition table:
Input
State
0 1
→A B F
B G C
*C A C
D C G
E H F
F C G
G G E Input
H G C State
0 1
→A B F
B G C
*C A C
D C G
G G A
Minimized DFA:
0
Start 0 1
A B C 1
1 1 0
D G
1 0
0
Example 2:
Input
State
0 1
→A B A
B A C
C D B
*D D A
E D F
F G E
G F G
H G D
Solution:
B X
C X X
D X X X
E X X X
F X X X X
G X X X X X
H X X X X X X
A B C D E F G
Step 1:
Step 2:
Step 3:
BG → AF, so mark BG as X
FG → EG then mark FG as X.
Step 4:
(AG), (BF),(CE), so A = G, B = F, C = E.
Input
State
0 1
→A B A
B A C
C D B
*D D A
H A D
Minimized DFA:
Start 0 1
A,G B,F C,E
0 1
1
0 0
D H
1
0
Steps in Pumping Lemma:
Problems:
1. Verify whether the finite automaton machine M1 and M2 shown below are
equivalent over the alphabet {0, 1}.
Finite Automata M1:
0
1
1
Start
A B
0
Finite Automata M2:
0
Start 0
C D
0 1
1
E
Solution:
Step 1: Construct the table using table filling algorithm by combining the state of both the
finite automata M1 and M2. (For same final state don’t mark as X).
B X
C X
D X
EX X X
A B C D
0
BE → AC don’t mark. (Mark all the accepting state).
Step 2:
Do not compare the accepting with non accepting state. So (A,B) (A,E) (B,C)
(B,D) (C,E) (D,E) are not equivalent state.
Step 3:
Step 4:
Here the start state of M1 is A and the start state of M2 is C. So (AC) is A≡C, then
M1 and M2 are equivalent.
Example
Verify whether the finite automata machine M1 and m2 shown below are equivalent over
the alphabet {a, b}.
Input
State
a b
→*q1 q1 q2
q2 q3 q1
q3 q2 q3
Input
State
a b
→*q4 q4 q5
q5 q6 q4
q6 q7 q6
q7 q6 q4