Unit 2-Theory of Computation
Unit 2-Theory of Computation
Regular Expression
1
Syllabus and Planner
Lecture Topic Book
1 Formal definition and Construction of Regular Expression of the T1,T2
given Language
Tutorial No Topic
1 Construction of Regular Expression of the given Language, Construction of Language
from the RE, DFA to RE conversion Using Arden’s Theorem,
2
Text Books & Reference Books
• Text Books
1. Michael Sipser “Introduction to the Theory of Computation” CENGAGE Learning, 3rd
Edition ISBN-13:978-81-315-2529-6
2. Vivek Kulkarni, “Theory of Computation”, Oxford University Press, ISBN-13: 978-0-
19-808458-7
• Reference Books
1. Hopcroft Ulman, “Introduction To Automata Theory, Languages And Computations”,
Pearson Education Asia, 2nd Edition
2. Daniel. A. Cohen, “Introduction to Computer Theory” Wiley-India, ISBN:978-
81-265-1334-5
3. K.L.P Mishra ,N. Chandrasekaran ,“Theory Of Computer Science (Automata, Languages
and Computation)”, Prentice Hall India,2nd Edition
4. John C. Martin, “Introduction to Language and Theory of Computation”, TMH, 3rd
Edition ISBN: 978-0-07-066048-9
• Regular expressions are short notations that can denote complex and infinite regular
languages.
4
Definition of a Regular Expression
1. Regular expressions over Σ, include letters, ∅(empty set) and
ε(empty string of length zero).
4. Regular expressions are only those that are obtained using rules1-3.
5
Operators of RE
6
Continued…
1. “a+b” stands for either a or b (parallel)
b
(2) (3)
(1)
7
Continued…
Concatenation of 2 sets:
U.V ={x|x=uv, u ⊆ U and v ⊆ V}
UV ≠ VU
U(VW)=(UV)W
E.g. U={000,111},V={101,010}
Closure of a set:
S*=S0 U S1 U S2…..
Where S0 ={€} and Si =Si-1.S for i>0
e.g. S={01,11}
S1= S0 .S ={€}.{01,11}
S2= S1 .S ={01,11} {01,11}
…..
S*={€ , 01,11,0101,0111,1101,1111,……}
8
Precedence of Regular Expression operators
The Star operator :Closure
The Dot operator :Concatenation
The Plus operator :Union
E.g.
01*+1
Set of all strings over {0,1} consisting of 1 or a 0 followed by zero or
more number of 1s. e.g. 01, 011, 011111, 1,…..
(01)*+1
Set of all strings over {0,1} consisting of 1 or zero or more number of
01. e.g. 01, 0101, 01011, 1,…..
0(1*+1)
Set of all strings over {0,1} starting with 0 followed by single one or
zero or more number of 1’s. e.g. 0, 01, 011, 01111,….
9
Identities of regular expressions
1. R U Φ=R
Adding the empty language to any other language will not change it.
2. Φ.R = R.Φ = Φ
if R = 0, then L(R) = {0} but L(R ◦ ∅) = ∅.
3. €.R =R.€ =R
Joining the empty string to any string will not change it
True or False?
1. ((R*)*)* = R* ?
2. (R+S)* = R* + S* ?
11
Construction of RE from regular Language
Write RE to represent L over ∑* where ∑={0,1}:
10. The set of all strings of 0’s and 1’s such that the tenth
symbol from the right is 1.
R=(0+1)*.1.(0+1)(0+1)(0+1)(0+1)(0+1)(0+1)(0+1)(0+1)(0+1)
13
Construction of Regular Language from RE
Describe the language represented by following R.E.
1) r=(0|1)*011
Ans: ∑={0,1},
L{r}=Set of all strings over {0,1} such that all strings end with 011
2) r= 0*1*2*
Ans: ∑={0,1,2},
L{r}=Set of strings over {0,1,2} with zero or more number of 0’s,
followed by zero or more number of 1’s,
followed by zero or more number of 2’s
3) r= 00*11*22*
Ans:∑={0,1,2},
L{r}=Set of all strings over {0,1,2}such that every string will have at least one 0
followed by at least one 1 followed by at least one 2.
4) r= (0*1*)*000(0+1)*
L=Set of all strings over {0,1} with 3 consecutive 0’s.
5)r= (0 ∪ ε)(1 ∪ ε)
L= {ε, 0, 1, 01}
14
Construction of Regular Language from RE
6) R= (∑∑)*
L={w| wє∑ and is a string of even length}
7) R= (∑∑∑)*
L={w| wє∑ and length of w is a multiple of 3}
8) Φ*= ?
L= €
9) R . € =?
L= R
15
Equivalence of FA and RE
•Kleen’s Theorem:
States that:
1) ANY regular language can be recognized (accepted) by a
finite automata.
2) Languages accepted by the finite automata are regular.
16
RE to NFA (with €- moves Conversion)
1) a.b
2) a+b
0 = Start State, 5 = final state
17
RE to NFA (with €- moves Conversion)
3) a*
0 = Start State, 3 = final state
18
Example
Construct an NFA for the regular expression, (a +
b)* ab. Convert the NFA to its equivalent DFA .
Solution:
It is expected to construct a DFA that recognizes the
regular set: R = (a+b)*· a · b
Let us first build the NFA with ε moves and the convert
the same to DFA.
19
Let us convert this NFA with ε -moves to its equivalent DFA
using a direct method.
20
RE to NFA
22
23
DFA to RE: Observation
Derive the equivalent RE for DFA
Strategy:
1. R=(a+b).(a+b)*
1. Explore Parallel paths between “two
states” or “start to final state” individually:
“+” operator
2. Find self-loops: “*”
2. R=a.(a+b)*
3. Find out the trap (or dead) state.
Which is correct:
1. a.(a+b)*
2. (a+b).(a+b)*
24
DFA to RE: Arden's Theorem
Statement −
Let P and Q be two regular expressions.
If P does not contain null string(ε),
then R = Q + RP has a unique solution that is R = QP*
Proof −
R = Q + (Q + RP)P [After putting the value R = Q + RP]
= Q + QP + RPP
When we put the value of R recursively again and again, we get the
following equation −
R = Q + QP + QP2 + QP3…..
R = Q (ε + P + P2 + P3 + …. )
R = QP* [As P* represents (ε + P + P2 + P3 + ….) ]
Hence, proved.
25
Arden's Theorem Example 1
Solution:
The state equations for the given DFA are:
q0 = q0 b + q2 a + ε
q1 = q0 a
q2 = q1 a + q1 b + q2 b
26
Arden's Theorem
q2 = q1 a + q1 b + q2 b
Substituting for q1 in q2,
q2 = q0aa + q0ab + q2 b
q2= q0 a (a + b) + q2 b
q2 = q0 a (a + b)b* ... using Arden's Theorem(R = Q + RP has a
unique solution that is R = QP*)
Substituting for q2 in q0,
q0 = q0 b + q2 a + ε
q0 = q0 b + q0 a (a + b)b* a + ε
q0 = q0 (b + a (a + b)b* a ) + ε
q0 = ε (b + a (a + b)b* a )* ... using Arden's Theorem
Hence, q0 = (b + a (a + b)b* a )*
q0 being the only final state for the DFA,
R= (b + a (a + b)b* a )* 27
Example 2.
q1=q1.a+q2.b+є
q2=q1.a+q2.b+q3.a
q3=q2.a
28
Using q3in q2 (q3=q2.a)
q2 =q1.a+q2.b+q3.a
q2=q1.a+q2.b+q2.a.a
q2=q1.a+q2(b+aa) {R = Q + RP has a unique solution that is R = QP*)
Applying Arden’s Theorem,
q2=q1.a(b+aa)*
Using q2 in q1 (q2=q1.a(b+aa)*)
q1=q1.a+q2.b+є
q1=q1.a+q1.a(b+aa)*.b+ є
q1= є+q1[a+a(b+aa)*.b] {R = Q + RP has a unique solution that is R = QP*)
Applying Arden’s Theorem,
q1= є. [a+a(b+aa)*.b]*
q1= [a+a(b+aa)*.b]*
30
Closure properties of Regular Languages
31
Closure properties of Regular Languages
Union : If L1 and If L2 are two regular languages, their union L1 ∪ L2 will also
be regular.
For example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1 ∪ L2 = {an ∪ bn | n ≥ 0} is also regular.
For example,
L1= {am bn | n ≥ 0 and m ≥ 0} and L2= {am bn ∪ bn am | n ≥ 0 and m ≥ 0}
L3 = L1 ∩ L2 = {am bn | n ≥ 0 and m ≥ 0} is also regular.
32
Closure properties of Regular Languages
Concatenation : If L1 and If L2 are two regular languages, their
concatenation L1.L2 will also be regular.
For example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1.L2 = {am . bn | m ≥ 0 and n ≥ 0} is also regular.
For example,
L1 = (a ∪ b)
L1* = (a ∪ b)*
33
Closure properties of Regular Languages
Complement
For example,
L(G) = {an | n > 3}
L’(G) = {an | n <= 3}
34
Application of Regular Expression
• Application in Linux
Example : Text File Search , Unix tool: egrep
Editing Commands , cw :Change word
• Web application
(ii) |uv| ≤ n
(iii) uviw L for all i ≥0 , where vi denotes that v is repeated or pumped i times .
Pumping lemma Application