Languages and Finite Automata

Standard Representations
of Regular Languages
Regular Languages
DFAs
NFAs
Fall 2004
Regular
Grammars
Regular
Expressions
COMP 335
1
When we say:
We mean:
Fall 2004
We are given
a Regular Language
L
Language L is in a standard
representation
COMP 335
2
Elementary Questions
about
Regular Languages
Fall 2004
COMP 335
3
Membership Question
Question:
Answer:
Fall 2004
Given regular language L
and string w
how can we check if w 
L?
Take the DFA that accepts L
and check if w is accepted
COMP 335
4
DFA
w
w L
DFA
w
w L
Fall 2004
COMP 335
5
Question:
Answer:
Given regular language L
how can we check
if L is empty: ( L  ) ?
Take the DFA that accepts
L
Check if there is any path from
the initial state to a final state
Fall 2004
COMP 335
6
DFA
L
DFA
L
Fall 2004
COMP 335
7
Question:
Given regular language
how can we check
if L is finite?
Answer: Take the DFA that accepts
L
L
Check if there is a walk with cycle
from the initial state to a final state
Fall 2004
COMP 335
8
DFA
L is infinite
DFA
L is finite
Fall 2004
COMP 335
9
Question: Given regular languages L1 and
how can we check if L1  L2 ?
Answer:
Fall 2004
Find if
L2
( L1  L2 )  ( L1  L2 )  
COMP 335
10
( L1  L2 )  ( L1  L2 )  
and
L1  L2  
L1
L1  L2  
L2
L2 L
2
L1  L2
L1 L1
L2  L1
L1  L2
Fall 2004
COMP 335
11
( L1  L2 )  ( L1  L2 )  
L1  L2  
L1
or
L1  L2  
L2
L2
L1  L2
L1
L2  L1
L1  L2
Fall 2004
COMP 335
12
Non-regular languages
Fall 2004
COMP 335
13
{a b : n  0}
n n
Non-regular languages
{vv : v {a, b}*}
R
Regular languages
a *b
b*c  a
b  c ( a  b) *
etc...
Fall 2004
COMP 335
14
How can we prove that a language
is not regular?
L
Prove that there is no DFA that accepts
L
Problem: this is not easy to prove
Solution: the Pumping Lemma !!!
Fall 2004
COMP 335
15
The Pigeonhole Principle
Fall 2004
COMP 335
16
4 pigeons
3 pigeonholes
Fall 2004
COMP 335
17
A pigeonhole must
contain at least two pigeons
Fall 2004
COMP 335
18
n pigeons
...........
m
pigeonholes
nm
...........
Fall 2004
COMP 335
19
The Pigeonhole Principle
n pigeons
m
pigeonholes
nm
There is a pigeonhole
with at least 2 pigeons
...........
Fall 2004
COMP 335
20
The Pigeonhole Principle
and
DFAs
Fall 2004
COMP 335
21
DFA with
b
q1
4
b
b
a
q2
a
a
Fall 2004
states
COMP 335
q3
b
q4
a
22
In walks of strings:
no state
is repeated
a
aa
aab
b
q1
b
b
a
q2
a
a
Fall 2004
COMP 335
q3
b
q4
a
23
In walks of strings:
a state
is repeated
aabb
bbaa
abbabb
abbbabbabb...
b
q1
b
b
a
q2
a
a
Fall 2004
COMP 335
q3
b
q4
a
24
If string
w has length | w |  4 :
Then the transitions of string w
are more than the states of the DFA
Thus, a state must be repeated
b
q1
b
b
a
q2
a
a
Fall 2004
COMP 335
q3
b
q4
a
25
In general, for any DFA:
String
A state
w has length  number of states
q
walk of
must be repeated in the walk of
w
......
Fall 2004
w
q
......
Repeated
state
COMP 335
26
In other words for a string
w:
a
transitions are pigeons
q
states are pigeonholes
walk of
w
......
q
......
Repeated state
Fall 2004
COMP 335
27
The Pumping Lemma
Fall 2004
COMP 335
28
Take an infinite regular language L
There exists a DFA that accepts
L
m
states
Fall 2004
COMP 335
29
Take string
w with w L
There is a walk with label
w:
.........
walk
Fall 2004
w
COMP 335
30
If string
w has length | w |  m
(number
of states
of DFA)
then, from the pigeonhole principle:
a state is repeated in the walk
......
q
walk
Fall 2004
w
......
w
COMP 335
31
Let q be the first state repeated in the
walk of w
......
q
walk
Fall 2004
......
w
COMP 335
32
Write
w x y z
y
......
Fall 2004
x
q
COMP 335
......
z
33
Observations:
length
length
| x y |  m number
of states
of DFA
| y | 1
y
......
Fall 2004
x
q
COMP 335
......
z
34
Observation:
The string
is accepted
xz
y
......
Fall 2004
x
q
COMP 335
......
z
35
Observation:
The string
is accepted
xyyz
y
......
Fall 2004
x
q
COMP 335
......
z
36
Observation:
The string
is accepted
xyyyz
y
......
Fall 2004
x
q
COMP 335
......
z
37
In General:
The string
is accepted
i
xy z
i  0, 1, 2, ...
y
......
Fall 2004
x
q
COMP 335
......
z
38
In General: wi  x y i z ∈ L
i  0, 1, 2, ...
Language accepted by the DFA
y
......
Fall 2004
x
q
COMP 335
......
z
39
In other words, we described:
The Pumping Lemma !!!
Fall 2004
COMP 335
40
The Pumping Lemma:
• Given a infinite regular language
• there exists an integer
• for any string
• we can write
• with
w L
m
with length
| w| m
w x y z
| x y |  m and | y |  1
• such that: wi  x y z  L,
i
Fall 2004
L
COMP 335
i  0, 1, 2, ...
41
Applications
of
the Pumping Lemma
Fall 2004
COMP 335
42
Theorem: The language L  {a nb n : n  0}
is not regular.
Proof:
Fall 2004
Use the Pumping Lemma
COMP 335
43
L  {a b : n  0}
n n
Assume that
L is a regular language
Since L is an infinite language,
we can apply the Pumping Lemma
Fall 2004
COMP 335
44
L  {a b : n  0}
n n
Let
m
be the integer in the Pumping Lemma
Pick a string
(2) length
w such that: (1) w  L and
| w| m
We pick:
Fall 2004
wa b
m m
COMP 335
45
Write:
a b xyz
m m
From the Pumping Lemma
it must be that length | x
y |  m, | y | 1
m
w  xyz  a b
m m
 a...aa...aa...ab...b
x
Thus:
Fall 2004
m
y
z
y  a , k 1
k
COMP 335
46
x y za b
y  a , k 1
m m
k
From the Pumping Lemma:
wi  x y z  L
i
i  0, 1, 2, ...
Thus:
Fall 2004
w2  x y z  L
2
COMP 335
47
x y za b
y  a , k 1
m m
k
From the Pumping Lemma:
xy z  L
2
mk
m
xy z  a...aa...aa...aa...ab...b  L
2
x
Thus:
Fall 2004
a
y
y
z
m k m
b L
COMP 335
48
a
But:
m k m
b L
k ≥1
L  {a b : n  0}
n n
a
m k m
b L
CONTRADICTION!!!
Fall 2004
COMP 335
49
Therefore:
Our assumption that L
is a regular language is not true
Conclusion: L is not a regular language
Fall 2004
COMP 335
50
Non-regular languages
{a b : n  0}
n n
Regular languages
Fall 2004
COMP 335
51
More Applications
of
the Pumping Lemma
Fall 2004
COMP 335
52
The Pumping Lemma:
• Given a infinite regular language
• there exists an integer
• for any string
• we can write
• with
m
with length
| w| m
w x y z
| x y |  m and | y |  1
• such that:
Fall 2004
w L
L
xy z  L
i
COMP 335
i  0, 1, 2, ...
53
Non-regular languages
L  {vv : v  *}
R
Regular languages
Fall 2004
COMP 335
54
Theorem: The language
L  {vv : v  *}
R
  {a, b}
is not regular
Proof:
Fall 2004
Use the Pumping Lemma
COMP 335
55
L  {vv : v  *}
R
Assume for contradiction
that L is a regular language
Since L is infinite
we can apply the Pumping Lemma
Fall 2004
COMP 335
56
L  {vv : v  *}
R
Let
m
be the integer in the Pumping Lemma
Pick a string
w such that: w  L
length
We pick
Fall 2004
and
| w| m
wa b b a
m m m m
COMP 335
57
Write
a b b a xyz
m m m m
From the Pumping Lemma
it must be that length | x
m
y |  m, | y | 1
m m m
xyz  a...aa...a...ab...bb...ba...a
x
Thus:
Fall 2004
y
z
y  a , k 1
k
COMP 335
58
x y za b b a
y  a , k 1
m m m m
k
From the Pumping Lemma:
xy z  L
i
i  0, 1, 2, ...
Thus:
Fall 2004
xy z  L
2
COMP 335
59
x y za b b a
y  a , k 1
m m m m
k
From the Pumping Lemma:
xy z  L
2
m m m
m+k
2
xy z = a...aa...aa...a...ab...bb...ba...a ∈L
x
Thus:
Fall 2004
y
a
y
m k m m m
b b a
COMP 335
z
L
60
a
BUT:
m k m m m
b b a
L
k 1
L  {vv : v  *}
R
a
m k m m m
b b a
L
CONTRADICTION!!!
Fall 2004
COMP 335
61
Therefore:
Our assumption that L
is a regular language is not true
Conclusion: L is not a regular language
Fall 2004
COMP 335
62
Non-regular languages
n l n l
L  {a b c
: n, l  0}
Regular languages
Fall 2004
COMP 335
63
Theorem: The language
n l n l
L  {a b c
: n, l  0}
is not regular
Proof:
Fall 2004
Use the Pumping Lemma
COMP 335
64
n l n l
L  {a b c
: n, l  0}
Assume for contradiction
that L is a regular language
Since L is infinite
we can apply the Pumping Lemma
Fall 2004
COMP 335
65
n l n l
L  {a b c
Let
m
: n, l  0}
be the integer in the Pumping Lemma
Pick a string
w such that: w  L
length
We pick
Fall 2004
and
| w| m
wa b c
m m 2m
COMP 335
66
Write
m m 2m
a b c
xyz
From the Pumping Lemma
it must be that length | x
m
y |  m, | y | 1
m
2m
xyz  a...aa...aa...ab...bc...cc...c
x
Thus:
Fall 2004
y
z
y  a , k 1
k
COMP 335
67
x y za b c
y  a , k 1
m m 2m
k
From the Pumping Lemma:
xy z  L
i
i  0, 1, 2, ...
Thus:
Fall 2004
0
x y z = xz ∈ L
COMP 335
68
x y za b c
y  a , k 1
m m 2m
k
xz  L
From the Pumping Lemma:
mk
m
2m
xz  a...aa...ab...bc...cc...c  L
x
Thus:
Fall 2004
z
a
mk m 2 m
b c
COMP 335
L
69
a
BUT:
mk m 2 m
b c
n l n l
L  {a b c
a
mk m 2 m
b c
L
k 1
: n, l  0}
L
CONTRADICTION!!!
Fall 2004
COMP 335
70
Therefore:
Our assumption that L
is a regular language is not true
Conclusion: L is not a regular language
Fall 2004
COMP 335
71
Non-regular languages
L  {a : n  0}
n!
Regular languages
Fall 2004
COMP 335
72
Theorem: The language
L  {a : n  0}
n!
is not regular
n!  1  2  (n  1)  n
Proof:
Fall 2004
Use the Pumping Lemma
COMP 335
73
L  {a : n  0}
n!
Assume for contradiction
that L is a regular language
Since L is infinite
we can apply the Pumping Lemma
Fall 2004
COMP 335
74
L  {a : n  0}
n!
Let
m
be the integer in the Pumping Lemma
Pick a string
w such that: w  L
length
We pick
Fall 2004
wa
COMP 335
| w| m
m!
75
Write
a
m!
xyz
From the Pumping Lemma
it must be that length | x
m
xyz  a
m!
m!m
 a...aa...aa...aa...aa...a
x
Thus:
Fall 2004
y |  m, | y | 1
y
z
y  a , 1 k  m
k
COMP 335
76
x y za
y  a , 1 k  m
m!
k
From the Pumping Lemma:
xy z  L
i
i  0, 1, 2, ...
Thus:
Fall 2004
xy z  L
2
COMP 335
77
x y za
y  a , 1 k  m
m!
k
From the Pumping Lemma:
mk
xy z  L
2
m!m
xy z  a...aa...aa...aa...aa...aa...a  L
2
x
Thus:
Fall 2004
y
y
a
m! k
COMP 335
z
L
78
a
Since:
m! k
L
1 k  m
L  {a : n  0}
n!
There must exist
p
such that:
m! k  p!
Fall 2004
COMP 335
79
However:
m! k  m! m
 m! m!
for
m 1
 m!m  m!
 m!(m  1)
 (m  1)!
m! k  (m  1)!
m! k  p!
Fall 2004
COMP 335
for any
p
80
a
BUT:
m! k
L
1 k  m
L  {a : n  0}
n!
a
m! k
L
CONTRADICTION!!!
Fall 2004
COMP 335
81
Therefore:
Our assumption that L
is a regular language is not true
Conclusion: L is not a regular language
Fall 2004
COMP 335
82