Regular Languages

parmeet834 1,288 views 35 slides Oct 28, 2020
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

Regular Languages,Constructing NFA for regular expressions


Slide Content

Regular Languages

Rama Bansal 2
Outline
•Regular expressions
•Regular languages
•Equivalence between languages
accepted by FA and regular languages
•Closure Properties

Rama Bansal 3
Regular Expressions
Regular expression over alphabet 
•is aregular expression.
•is a regular expression.
•For any a, ais a regular expression.
•Ifr
1and r
2are regular expressions, then
–(r
1+ r
2)is a regular expression.
–(r
1r
2)is a regular expression.
–(r
1
*
)is a regular expression.
•Nothing else is a regular expression.

Rama Bansal 4
RegularLanguages
•is a regular languagecorresponding to theregular
expression .
•{}is a regular languagecorresponding to the
regular expression .
•For any symbol a,{a}is a regular language
corresponding to theregular expression a.
•If L
1and L
2are regular languagescorresponding to
theregular expression r
1and r
2, then
–L
1L
2, L
1L
2, and L
1
*
are regular languages
corresponding to (r
1+ r
2), (r
1r
2), and (r
1
*
).

Rama Bansal 5
Simple examples
Let = {0,1}.
•{
*
|does not contain 1’s}
–(0
*
)
•{
*
|contains 1’s only}
–(1(1
*
)) (which can can be denoted by (1
+
))
•
*
–((0+1)
*
)
•{
*
|contains only 0’s or only 1’s}
–((00
*
)+(11
*
))
0* + 1* (0+1)*

Rama Bansal 6
Some more notations
Let = {0,1}.
•Parentheses in regular expressions can be
omitted when the order of evaluation is clear.
–((0+1)
*
) 0+1
*
–((0
*
)+(1
*
)) = 0
*
+ 1
*
•For concatenation, can be omitted.
•rrr… ris denoted by r
n
.
ntimes

Rama Bansal 7
More complex examples
Let = {0,1}.
•{*| contains odd number of 1’s}
–0*(10*10*)*10*
•{*| any two 0’s in are separated by
three 1’s}
–1*(0111)*01*+1*
•{
*
| is a binary number divisible by 4}
–(0+1)*00
•{*| does not contain 11}
–0*(10
+
)* (1+) or (0+10)* (1+)

Rama Bansal 8
Notation
Let rbe a regular expression.
The regular language corresponding
to the regular expression ris
denoted by L(r).

Rama Bansal 9
Some rules for language
operations
Let r, sand tbe languages over {0,1}
r += +r = r
r +s = s+r
r= r= r
r= r= 
r(s+t) = rs+rt
r
+
= rr
*

Rama Bansal 10
Rewrite rules for regular
expressions
Let r, sand tbe regular expressions over {0,1}.
* = 
* = 
(r + )
+
= r*
r* = r*(r + ) = r* r* = (r*)*
(r*s*)*= (r + s)*

Rama Bansal 11
Closure properties of the class of
regular languages (Part 1)
Theorem:The class of regular languages is closed
under union, concatenation, and Kleene’s star.
Proof:LetL
1andL
2be regular languages over.
Then, there are regular expressionsr
1andr
2
corresponding toL
1andL
2.
By the definition of regular expression and regular
languages,r
1+r
2,r
1r
2, andr
1
*
are regular
expressions corresponding toL
1L
2, L
1L
2, andL
1
*
.
Thus, the class of regular languages is closed under
union, concatenation, and Kleene’s star.

Rama Bansal 12
Equivalence oflanguage accepted by
FA and regular languages
To show that the languagesaccepted by FA and
regular languages are equivalent, we need to prove:
•For any regular language L, there exists an FA M
such that L= L(M).
•For any FA M, L(M) is a regular language.

Rama Bansal 13
For any regular language L, there
exists an FA Msuch that L= L(M)
Proof:Let Lbe a regular language.
Then, a regular expression r corresponding to L.
We construct an NFA M, from the regular expression r,
such that L=L(M).
Basis:
If r= , Mis
If r= , M is
If r= {a} for some a , Mis
s
fs
a
s f

Rama Bansal 14
Proof (cont’d)
Induction hypotheses:Let r
1andr
2
be regular expressions
with less than noperations. And, there are NFA’s M
1
andM
2accepting regular languages corresponding
to L(r
1)and L(r
2).
Induction step:Let rbe a regular expression with n
operations. We construct an NFA accepting L(r).
rcan be in the form of either r
1+r
2
, r
1r
2
, or r
1
*
, for
regular expressions r
1andr
2
with less than n
operations.

Rama Bansal 15
Proof (cont’d)
If r= r
1+r
2
, then Mis
If r= r
1r
2
, then Mis
If r= r
1
*
, then Mis
Therefore, there is an NFA accepting L(r)for any
regular expression r.
f
2
f
1
s
s
1
s
2


f
2s
2s
1 f
1



s
1 f
1s f

Rama Bansal 16
Constructing NFA for regular
expressions
•0
*
(10
+
)
*
(1+)
1 0
0



0



1
0
*
10
+
(10
+
)
*
1

1+
s




Rama Bansal 17
Some Observations
•Can these
two states
be merged?
NO
•Be careful
when you
decide to
merge some
states.
1 0
0



0



1
s




Rama Bansal 18
For any FA M, L(M) is a regular
language
Proof:Let M= (Q, , , q
1, F)be an FA, where Q={q
i| 1
i n} for some positive integern.
Let R(i, j, k)be the set of all strings in that drive M
from state q
ito state q
jwhile passing through any
state q
l, for l k. (iandjcan be any states)
q
i q
j
q
l
q
l'

Rama Bansal 19
Proof (cont’d)
R(i, j, 0) = +
qj (qi, a) aif ij
R(i, j, 0) = +
qj (qi, a) a+ if i=j
R(i, j, k) = R(i, j, k-1) +R(i, k, k-1) R(k, k, k-1)
*
R(k, j, k-1)
q
i q
j
a
q
i
a
q
i
q
j
q
k
q
1
q
2
q
k-1
R(i,j,k-1)
R(i,k,k-1)
R(k,k,k-1)*
R(k,j,k-1)

Rama Bansal 20
Proof (cont’d)
Then, L(M) =+ R(1, f, n)for all q
fin F.
R(i, j, 0) = +
qj (qi, a) aif ij
R(i, j, 1) = {a| (q
i, a) = q
j} + if i=j
R(i, j, k) = R(i, j, k-1) +R(i, k, k-1) R(k, k, k-1)
*
R(k,
j, k-1)

Rama Bansal 21
Proof (cont’d)
We prove that L(M) is a regular language by
showing that there is a regular expression
corresponding to L(M), by induction.
Basis:R(i, j, 0) corresponds to a regular expression a
if ijand a +if i=jfor some a.
Induction hypotheses:Let R(i, j,k-1) correspond to a
regular expression, for any i, j, kn.

Rama Bansal 22
Proof (cont’d)
Induction step:R(i, j, k) = R(i, j, k-1) R(i, k, k-1) R(k,
k, k-1)
*
R(k, j, k-1) also corresponds to a regular
expression becauseR(i,j, k-1),R(i, k, k-1), R(k, k, k-
1)and R(k, j, k-1) correspond to some regular
expressions and union, concatenation, and
Kleene’s star are allowed in regular expressions.
Therefore,L(M) is also a regular language because
L(M) =+ R(1, f, n)for all q
fin F.

Rama Bansal 23
Find a regular expression for
a DFA
q
4
q
2
q
1 q
3
q
5
b
b
b
a
a a


a
*
b
ba
*
b
a
*
ba
*
b
a
*
ba
*
ba
*
ba
*
ba
*
ba
*
b
a*ba* +
a*ba*b (ba*ba*b+a)* ba*ba*
a*ba* (+b(ba*ba*b+a)* ba*ba*)

Rama Bansal 24
Pumping Lemma
Let L be a regular language.
Then, there exists an integer
n0such that for every string
x in L that |x|n, there are
strings u, v, and w such that
–x = u v w,
–v ,
–|u v| n, and
–for all k 0, u v
k
w is also in L.
Any language L is not a
regular languageiffor any
integer n0, there is a
string x in L such that
|x|n, for any strings u, v,
and w,
–x u v w, or
–v = , or
–Not (|u v| n), or
–there is k 0, u v
k
w is not in
L

Rama Bansal 25
Pumping Lemma
Any language L is not a regular languageif
•for any integer n0,
•there is a string x in L such that |x|n,
•for any strings u, v and w, such that x = u v
w, v , and |u v| n,
–there is k 0, u v
k
w is not in L

Rama Bansal 26
Using Pumping Lemma
•Given a language L.
•Let n be any integer 0.
•Choose a string x in L that |x|n.
•Consider all possible ways to chop x into u, v
and w such that v , and |uv| n.
•For all possible u, v, and w, show that there
is k 0 such that u v
k
w is not in L.
•Then, we can conclude that L is not regular.
If you pick a wrong x, your
proof will fail ! It does not
mean that L is regular !!!!!

Rama Bansal 27
Prove {0
i
1
i
| i0}is not regular
Let L= {0
i
1
i
| i0}.
Let nbe any integer 0.
Let x = 0
n
1
n
.
Make sure that xis in Land |x|n.
The only possible way to chop xinto u, v,and wsuch that
v, and |u v| nis:
u= 0
p
, v= 0
q
, w = 0
n-p-q
1
n
, where 0p<nand 0<qn
Show that there is k0, u v
k
wis not in L.
u v
k
w= 0
p
0
qk
0
n-p-q
1
n
= 0
p+qk+(n-p-q)
1
n
= 0
n+q(k-1)
1
n
If k1, then n+q(k-1) nand u v
k
wis not in L.
Then, Lis not regular.

Rama Bansal 28
Prove {0
i
1
i
| i 0}is not regular
Let L= {0
i
1
i
| i0}.
Let nbe any integer 0, and m= n/2.
Let x = 0
m
1
m
.
Make sure that xis in Land |x|n.
Possible ways to chop xinto u, v,and wsuch that v, and
|u v| nare:
–u = 0
p
, v = 0
q
, w = 0
m-p-q
1
m
,where 0p<mand 0<qm
–u = 0
p
, v = 0
m-p
1
q
, w = 1
m-q
, where 0p<mand 0<qm
–u = 0
m
1
p
, v = 1
q
, w = 1
m-p-q
, where 0p<mand 0<qm

Rama Bansal 29
Prove {0
i
1
i
| i0}is not regular
Show that there is k0, u v
k
wis not inL.
–u=0
p
, v=0
q
, w= 0
m-p-q
1
m
, where where 0p<mand
0<qm
u v
k
w = 0
p
0
qk
0
m-p-q
1
m
= 0
m+q(k-1)
1
m
is not in Lif k1.
–u=0
p
, v=0
m-p
1
q
, w=1
m-q
, where where 0p<mand
0<qm
u v
k
w = 0
p
(0
m-p
1
q
)
k
1
m-q
is not in Lif k1.
–u=0
m
1
p
, v=1
q
, w=1
m-p-q
, where where 0p<mand
0<qm
u v
k
w = 0
m
1
p
1
qk
1
m-p-q
= 0
m
1
m+q(k-1)
is not in Lif k1.
Then, L is not regular.

Rama Bansal 30
Prove {1
i
|iis prime} is not
regular
Let L= {1
i
| iis prime}.
Let nbe any integer 0.
Let pbe a prime n, andw = 1
p
.
Only one possible way to chop winto x, y,and z
such that y, and |x y| nis:
x = 1
q
, y = 1
r
, z = 1
p-q-r
, where 0q<nand 0<r<n
Show that there is k0, x y
k
zis not in L.
x y
k
z = 1
q
1
rk
1
p-q-r
= 1
q+rk+(p-q-r)
= 1
p+r(k-1)
If k=p+1, then p+r(k-1) = p(r+1),which is not a prime.
Then, x y
k
zis not in L.
Then, L is not regular.

Rama Bansal 31
Using closure property
Let be a binary operation on languages and
the class of regular languages is closed under
. (can be , , or -)
•If L
1and L
2are regular, then L
1L
2is regular.
•If L
1L
2is not regular, then L
1or L
2are not
regular.
•If L
1L
2is not regular but L
2is regular, then
L
1is not regular.

Rama Bansal 32
Prove that {w{0,1}
*
| the number of 0’s
and 1’s in w are equal} is not regular
Let L={w{0,1}
*
| the number of 0’s and 1’s in
w are equal}.
Let R= {0
i
1
i
| i 0}.
R = 0
*
1
*
L
We already prove that R is not regular.
But 0*1* is regular.
Then, L is not regular.

Rama Bansal 33
Using closure property
Let be a unary operation on a
language and the class of regular
languages is closed under .
(can be complement or
*
)
•If L is regular, then L is regular.
•If L is not regular, then L is not
regular.

Rama Bansal 34
Prove that {w{0,1}
*
| the number
of 0’s and 1’s in w are not equal}
is not regular
Let L = {w{0,1}
*
| the number of 0’s and 1’s
in w are not equal}.
Let R =L = {w{0,1}
*
| the number of 0’s and
1’s in w are equal}.
We already prove that R is not regular.
Then, L is not regular.

Rama Bansal 35
Check list
Find the language described by a
regular exp.
Construct regular exp. describing a
given language
Convert a regular exp. into an FA
Convert an FA into a regular exp.
Prove a language is regular
–By constructing a regular exp.
–By constructing an FA
–By using closure properties
Construct an FA or a
regular exp. for the
intersection, union,
concatenation,
complementation, and
Kleene’s star of regular
languages
Prove other closure
properties of the class of
regular languages
Surprise!