Title: CSC 3130: Automata theory and formal languages
1Fall 2009
The Chinese University of Hong Kong
CSC 3130 Automata theory and formal languages
Closure propertiesLimitations of regular
languages
Andrej Bogdanov http//www.cse.cuhk.edu.hk/andrej
b/csc3130
2Operations that preserve regularity
- We saw three operations that preserve regularity
- Union If L, L are regular languages, so is L ?
L - Concatenation If L, L are regular languages, so
is LL - Star If L is a regular language, so is L
- Exercise If L is regular, is L4 also regular?
- Answer Yes, because
L4 ((LL)L)L
3Example
- The language L of strings that end in 101 is
regular - How about the language L of strings that do not
end in 101?
(01)101
4Example
- Hint A string does not end in 101 if and only if
it ends in one of the following patterns(or
it has length 0, 1, or 2) - So L can be described by the regular expression
000, 001, 010, 011, 100, 110, 111
(01)(000001010010100110111) e (0
1) (0 1)(0 1)
5Complement
- The complement L of a language L is the set of
all strings that are not in L - Examples (S 0, 1)
- L1 all strings that end in 101
- L1 all strings that do not end in 101 all
strings end in 000, , 111 or have length 0, 1,
or 2 - L2 1 e, 1, 11, 111,
- L2 all strings that contain at least one 0
(0 1)0(0 1)
6Closure under complement
- If L is a regular language, is L also regular?
- Previous examples indicate answer should be yes
- Theorem
If L is a regular language, so is L.
7Proof of closure under complement
- To argue this, we can use any of the equivalent
definitions for regular languages - The DFA definition will be most convenient
- We will assume L is accepted by a DFA, and show
the same for L
regularexpression
DFA
NFA
8Proof of closure under complement
- Suppose L is regular, then it is accepted by a
DFA M - Now consider the DFA M with the accepting and
rejecting states of M reversed
9Proof of closure under complement
- Now for every input x ? S
M accepts x
After processing x, M ends in an accepting state
Language of M is L
After processing x, M ends in an rejecting state
L is regular
M rejects x
10Intersection
- The intersection L ? L is the set of strings
that are in both L and L - Examples
- If L, L are regular, is L ? L also regular?
L ? L
L (0 1)111
L 1
1111
L ? L
Ø
L (0 1)101
L 1
11Closure under intersection
If L and L are regular languages, so is L ? L.
L regular
L regular
L regular
L regular
L ? L regular
L ? L regular
L ? L regular.
But
L ? L L ? L
12Reversal
- The reversal wR of a string w is w written
backwards - The reversal LR of a language L is the language
obtained by reversing all its strings
w cave
wR evac
L cat, dog
LR tac, god
13Reversal of regular languages
- L all strings that end in 101 is regular
- How about LR?
- This is the language of all strings beginning in
101 - Yes, because it is represented by
(01)101
101(01)
14Closure under reversal
- Theorem
- Proof
- We will use the representation of regular
languages by regular expressions
If L is a regular language, so is LR.
regularexpression
DFA
NFA
15Proof of closure under reversal
- If L is regular, then there is a regular
expression E that describes it - We will give a systematic way of reversing E
- Recall that a regular expression can be of the
following types - Special expressions ? and e
- Alphabet symbols a, b,
- The union, concatenation, or star of simpler
expressions - In each of these cases we show how to do a
reversal
16Proof of closure under reversal
regular expression E
reversal ER
Æ
Æ
e
e
a (alphabet symbol)
a
E1 E2
E1R E2R
E1E2
E2RE1R
E1
(E1R)
17A question
LDUP ww w ? L
L cat, dog
Ex.
LDUP catcat, dogdog
If L is regular, is LDUP also regular?
?
regularexpression
DFA
NFA
18A question
- Lets try with regular expression
- Lets try with NFA
LDUP LL
19An example
L 01 is regular
LDUP 1, 01, 001, 0001, ...
LDUP 11, 0101, 001001, 00010001, ...
0n10n1 n 0
- Lets try to design an NFA for LDUP
20An example
LDUP 11, 0101, 001001, 00010001, ...
0n10n1 n 0
21Non-regular languages
22A non-regular language
- Another example
- We reason by contradiction
- Suppose we have managed to construct a DFA M for
L - We argue something must be wrong with this DFA
- In particular, M must accept some strings outside
L
L 0n1n n 0 is not regular.
23A non-regular language
imaginary DFA for L with n states
M
x
- What happens when we run M on input x 0n11n1?
- M better accept, because x ? L
24A non-regular language
0
M
x
0
0
0
0
0r1n1
- What happens when we run M on input x 0n11n1?
- M better accept, because x ? L
- But since M has n states, it must revisit at
least one of its states while reading 0n1
25Pigeonhole principle
Suppose you are tossing n 1 balls into n bins.
Then two balls end up in the same bin.
- Here, balls are 0s, bins are states
If you have a DFA with n states and it reads n
1 consecutive 0s, then it must end up in the same
state twice.
26A non-regular language
0
M
x
0
0
0
0
0r1n1
- What happens when we run M on input x 0n11n1?
- M better accept, because x ? L2
- But since M has n states, it must revisit at
least one of its states while reading 0n1 - But then the DFA must contain a loop with 0s
27A non-regular language
0
M
0
0
0
0
0r1n1
- The DFA will then also accept strings that go
around the loop multiple times - But such strings have more 0s than 1s, so they
are not in L2!
28General method for showing non-regularity
- Every regular language L has a property
- For every sufficiently long input z in L, there
is a middle part in z that, even if repeated
several times, keeps the input inside L
an
z
a1
ak1
an-1
ak
an1am
29Pumping lemma for regular languages
- Pumping lemma For every regular language L
There exists a number n such that for every
string z in L, we can write z u v w where ?
uv n ? v 1 ? For every i 0, the
string u vi w is in L.
v
z
w
u
30Proving non-regularity
- If L is regular, then
- So to prove L is not regular, it is enough to
show
There exists n such that for every z in L, we can
write z u v w where ?uv n, ?v 1 and ?
For every i 0, the string u vi w is in L.
For every n there exists z in L, such that for
every way of writing z u v w where?uv n
and ?v 1, the string u vi w is not in L for
some i 0.
31Proving non regularity
For every n there exists z in L, such that for
every way of writing z u v w where?uv n
and ?v 1, the string u vi w is not in L for
some i 0.
- This is a game between you and an imagined
adversary
adversary choose nwrite z uvw (uv n,v
1)
you choose z ? Lchoose iyou win if uviw ? L
1
2
32Arguing non-regularity
- You need to give a strategy that, regardless of
what the adversary does, always wins you the game
adversary choose nwrite z uvw (uv n,v
1)
you choose z ? Lchoose iyou win if uviw ? L
1
2
33Example
adversary choose nwrite z uvw (uv n,v
1)
you choose z ? Lchoose iyou win if uviw ? L
1
2
L 0n1n n 0
adversary
you
choose n
z 0n11n1
1
write z uvw
i 2
2
uv2w 0j2kl1n1 0n1k1n1 ? L
34Example
LDUP 0n10n1 n 0
adversary
you
choose n
z 0n110n11
1
write z uvw
i 2
2
uv2w 0j2kl10n11 0n1k10n11 ? L
35Which of these are regular?
L1 1n n is divisible by 3 L2 1n n is
prime
S 1
S 0, 1
L3 x x has same number of 0s and 1s L4 x
x has same number of patterns 01 and 10 L5 x
x has more 0s than 1s L6 x x has different
number of 0s and 1s
36Example
L3 x x has same number of 0s and 1s
adversary
you
choose n
z 0n11n1
1
write z uvw
i 2
2
uv2w 0j2kl1n1 0n1k1n1 ? L3
37Example
L4 x x has same number of 01s and 10s
is regular!
adversary
you
choose n
z (01)n1(10)n1
1
write z uvw
i 2
2
38Example
more 10s
1
0
0
r1
r0
1
1
q0
more 01s
0
1
0
1
s0
s1
0
L4 x x has same number of 01s and 10s
39Example
L5 x x has more 0s than 1s
adversary
you
choose n
z 0n11n
1
write z uvw
i 0
2
uv0w 0jl1n1 ? L5
40Example
L6 x x has different number of 0s than 1s
adversary
you
choose n
z ?
1
there is an easier way!
41Example
L2 1p p is prime
adversary
you
choose n
z 1n n gt p is prime
1
write z uvw 1a1b1c
i a c
2
uviw 1a1ib1c 1aibc 1a(ac)bc
1(ac)(b1) 1composite ? L2