ppt

Fall 2010
The Chinese University of Hong Kong
CSCI 3130: Automata theory and formal languages
Non-regular languages
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
A non-regular language
• An example
L = {0n1n: n ≥ 0} is not regular.
• 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
A non-regular language
imaginary DFA for L with n states
x
M
• What happens when we run M on input x = 0n+11n+1?
– M better accept, because x  L
A non-regular language
0
x
0
0
0
M
0
0r1n+1
• What happens when we run M on input x = 0n+11n+1?
– M better accept, because x  L
– But since M has n states, it must revisit at least one of its
states while reading 0n+1
Pigeonhole 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.
A non-regular language
0
x
0
0
0
M
0
0r1n+1
• What happens when we run M on input x = 0n+11n+1?
– M better accept, because x  L2
– But since M has n states, it must revisit at least one of its
states while reading 0n+1
– But then the DFA must contain a loop with 0s
A non-regular language
0
0
0
0
M
0
0r1n+1
• 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!
General method for showing non-regularity
• Every regular language L has a property:
an
z
a1
…
ak
ak+1 … an-1
an+1…am
• For every sufficiently long input z in L, there is a
“middle part” in z that, even if repeated any number
of times, keeps the input inside L
Pumping 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.
z
…
u
v
…
w
Arguing non-regularity
• If L is regular, then:
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.
• So to prove L is not regular, it is enough to show:
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.
Proving 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
1 choose n
2 write z = uvw (|uv| ≤ n,|v| ≥ 1)
you
choose z  L
choose i
you win if uviw  L
Arguing non-regularity
• You need to give a strategy that, regardless of what
the adversary does, always wins you the game
adversary
1 choose n
2 write z = uvw (|uv| ≤ n,|v| ≥ 1)
you
choose z  L
choose i
you win if uviw  L
Example
adversary
you
1 choose n
2 write z = uvw (|uv| ≤ n,|v| ≥ 1)
choose z  L
choose i
you win if uviw  L
L = {0n1n: n ≥ 0}
adversary
you
1 choose n
z = 0 n 1n
i=2
2 write z = uvw
000000000000000111111111111111
u
v
w
0000000000000000000111111111111111
u
v
v
w
uv2w = 0n+k1n  L
Example
LDUP = {0n10n1: n ≥ 0}
adversary
you
1 choose n
z = 0n10n1
i=2
2 write z = uvw
uv2w = 0n+k10n1  L
000000000000001000000000000001
u
v
w
0000000000000000001000000000000001
u
v
v
w
Which of these are regular?
L1 = {x: x has same number of 0s and 1s}
L2 = {x: x = 0n1m, n > m ≥ 0}
L3 = {x: x has same number of patterns 01 and 10}
L4 = {x: x has same number of patterns 01 and 10}
L5 = {x: x has different number of 0s and 1s}
S = {0, 1}
Example
L1 = {x: x has same number of 0s and 1s}
adversary
you
1 choose n
z = 0 n 1n
i=2
2 write z = uvw
uv2w = 0n+k1n  L3
00000000000000011111111111111
v
w
1 u
0000000000000000000111111111111111
u
v
v
w
Example
L2 = {x: x = 0m1n, m > n ≥ 0}
adversary
you
1 choose n
z = 0n+11n
i=0
2 write z = uvw
uv0w
000000000000000111111111111111
u
v
w
00000000000111111111111111
u
w
= 0j+l1n+1  L2
Example
L3 = {x: x has same number of 01s and 11s}
adversary
you
1 choose n
z = (01)n(11)n
n=1
z = 0111  L4
has too many 11s
What we have in mind:
n=1
n=2
n=3
z = 011
z = 010111
z = 010101111
z = (01)n1n
has n 01s and n 11s
Example
L3 = {x: x has same number of 01s and 11s}
adversary
you win!
1 choose n
z = (01)n1n
i=0
2 write z = uvw
or
010101010101010111111111
Taking out v will kill
at least one 01,
but it does not kill any 11s
or
010101010101010111111111
so uv0w  L3
010101010101010111111111
u
u
u v
v
v
w
w
w
Example
L4 = {x: x has same number of 01s and 01s}
adversary
you
1 choose n
z = (01)n(10)n
n=1
n=2
z = 0110
z = 01011010
n=3
z = 010101101010
Example
L4 = {x: x has same number of 01s and 10s}
is regular!
adversary
you
1 choose n
z = (01)n(10)n
i = 20
2 write z = uvw
010101101010
u v
w
01010101101010
u v v
5 01 patterns
5 10 patterns
w
6 01 patterns
6 10 patterns
0101101010
u
w
4 01 patterns
4 10 patterns
Example
1
1
0
r0
q0
1
s0
1
0
one more 10
1
one more 01
r1
0
0
0
s1
L4 = {x: x has same number of 01s and 10s}
Example
L4 = {x: x has different number of 0s than 1s}
adversary
1 choose n
you
z=?
there is an easier way!
L1 = {x: x has same number of 0s and 1s} = L4
If L4 is regular, then L1 = L4 is also regular
But L1 is not regular, so L4 cannot be regular
Extra example
L5 = {1p: p is prime}
adversary
1 choose n
2 write z = uvw = 1a1b1c
you
z = 1p: p > n is prime
i = a? + c
uviw
11111111111111111111111111111
1u = 1a v = 1b
w = 1c
= 1a1ib1c
= 1(a+c)+ib
= 1(a+c)+(a+c)b
= 1(a+c)(b+1)
= 1composite  L5