regular language

28
Regular Language

Upload: ariel-doyle

Post on 01-Jan-2016

36 views

Category:

Documents


1 download

DESCRIPTION

Regular Language. Class Discussion. Can you draw a DFA that accepts the language {a k b k | k = 0,1,2,… } over the alphabet ={a,b}?. Limitations of FA. Many languages are non-regular: {a n b n | n = 0,1,2,… } {0 i 2 | i  0,1,2,…} {0 p | p is a prime number } - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Regular Language

Regular Language

Page 2: Regular Language

Class Discussion Can you draw a DFA that accepts the

language {akbk | k = 0,1,2,…} over the alphabet ={a,b}?

Page 3: Regular Language

Limitations of FA Many languages are non-regular:

• {anbn | n = 0,1,2,…}• {0i2 | i 0,1,2,…}• {0p | p is a prime number }• the set of all well-formed parentheses

over the alphabet ={(, )}• the set of all palindromes over the

alphabet ={a, b}• …...

Page 4: Regular Language

Why Impossible? We want to prove that L = {aibi | i =

0,1,2,…} is non-regular. Prove by contradiction:

• Assume that there is a DFA M that recognizes L. Let n be the total number of states in M.

• Consider the path followed by the input string anbn in M:

qπ(0) qπ(1) qπ(n) qπ(n+1) qπ(2n)a b b b

…...…...a a

Page 5: Regular Language

Why Impossible?• Since M has only n states, there must be at least

one state visited twice in the first n transitions. Let this state be visited at the ith and the jth steps, where j > i.

• By skipping the loop, an-(j-i)bn should also be accepted, but this is contradictory since an-(j-i)bnL

qπ(0)

qπ(i) = qπ(j)

qπ(2n) F

The path in M when reading anbn

Page 6: Regular Language

Another Example We want to prove that L = {1k2 | k > 0}

is non-regular. Prove by contradiction:

• Assume that there is a DFA M that recognizes L. Let n be the total number of states in M.

• M should also accept the string 1n2

qπ(0) qπ(1) qπ(2) qπ(n2)

1 1…...

1 1

Page 7: Regular Language

Another Example• Since n2 n and M has only n states, there must be at least

two equal states from qπ(0) to q π(n2). Let qπ(i) = qπ(j) be the first

repeated state where j-i = m n.

• By repeating the loop one more time, 1(n2+m) is also accepted by

M, which is a contradiction, since (n2+m) cannot be a square

(the next square after n2 is (n+1)2 but n2+m < (n+1)2).

qπ(0)

qπ(i) = qπ(j)

qπ(n2 ) FThe path in M

when reading 1n2

a loop oflength m

Page 8: Regular Language

Pumping LemmaL: regular languageThere exists a constant n s.t. every string zL

with |z| nWe can find z = uvw

v i.e. |v| 1|uv| nFor all i 0, string uviw also in L

Page 9: Regular Language

Proof of Pumping Lemma If L is a regular language, there are DFAs that recognize L.

Let n be the number of states in one such DFA (called M)

recognizing L.

If z is a word in L with |z| = k n:

where z(i) is the ith symbol of the string z.

Since k n and M has only n states, there must be at least

one repeated states from qπ(0) to qπ(k). Let qπ(i) be the first

such repeated state.

qπ(0) qπ(1) qπ(2) qπ(k)z(2) z(k)

…...z(1) z(3)

Page 10: Regular Language

Proof of Pumping Lemma

Let u be the string obtained by traversing from qπ(0) to qπ(i), and v be

the string obtained by traversing the loop once (|v|1). In the traversal

from qπ(0) to qπ(i) and then through the loop once back to qπ(i), nothing

except qπ(i) repeats, so |uv| n. By traversing the loop 0 or more times,

we obtain uviw where i 0. These strings should all be accepted by M

since they can all reach qπ(k) which is a final state in M.

a path in Mqπ(0)

qπ(k) F

qπ(i)

Page 11: Regular Language

Using Pumping LemmaUse the proof of pumping lemmaSuppose L is regular, then we can construct a

DFA M that accepts LSuppose M has n statesCarefully choose a string z where |z| = k n

Page 12: Regular Language

Using Pumping LemmaClaim that

when accepting z, there must be at least one state

visited twice in the first n transitions (by pigeonhole

principle)

Let the state be qi ,0 i n

qπ(0) = q0

qi

qπ(k) F

The path in M when reading z

Page 13: Regular Language

Using Pumping LemmaClaim that By skipping the loop or repeating the

loop for certain times to generate new string z’

M does not accept z’So contradiction occurs

Page 14: Regular Language

Example of using pumping lemma

Consider a language L:The set of all strings over {0,1} with number of ‘1’s is three times number of ‘0’s

Show that L is non-regular

Page 15: Regular Language

ExampleAssume L is regular Let n be the constant in the lemma Let z = 0n13n Luvw = z = 0p0q0n-p-q13n, where |uv| = p+q n, |

v|=q 1

Page 16: Regular Language

ExampleBy Pumping Lemma, uviw is also in L for all i0p0qi0n-p-q13n L for all i 0For i=0, uviw = 0n-q13n LThere is a contradiction

Page 17: Regular Language

ExampleSuppose L is regular, then we can construct a

DFA M that accepts LSuppose M has n statesLet z = 0n13n LWhen accepting z, there must be at least one

state visited twice in the first n transitionsLet the state be qi ,where 0 i n

Page 18: Regular Language

ExampleThe path when reading z can be illustrated by

the following diagram:

By skipping the loop, we have z’ = 0n-q13n LBy the property of M, z’ should be accepted

by MSo by contradiction, there is no such M

qπ(0) = q0

qi

qπ(k) F0p

0q 0n-p-q13n

Page 19: Regular Language

Example We want to prove that L = {1y | y is a

prime} is non-regular. We can make use of the Pumping Lemma:

• If L is a regular language, it follows the Pumping Lemma. Let n be the constant in the lemma.

• Consider z=1p where p is a prime and p n. (The number of primes are infinite, so such a p exists.)

• According to the lemma, we can write z into uvw where |uv| n and |v| 1 and uviwL for all i 0.

Page 20: Regular Language

ExampleLet |v| = m.Consider the case when i = p+1. Then |

uviw| = p + pm = p (m+1), which is not a prime.

So it is not true that uviw is in L for all i 0.

Therefore L is not a regular language.

Page 21: Regular Language

Example We can also prove that L = {1y | y is a

prime} is non-regular by following the argument directly:

• Assume that there is a DFA M that recognizes L. Let n be the total number of states in M.

• We know that the number of primes are infinite, so there exists a prime p n.

qπ(0) qπ(1) qπ(p-1) qπ(p)1 1

…...1 1

Page 22: Regular Language

Example• Since p n and M has only n states, there must be at least two

equal states from qπ(0) to qπ(p). Let them be qπ(i) and qπ(j) where j-i =

m > 0.

• By repeating the loop p+1 times, 1(p-m) + (p+1)m = 1p(m+1) is also

accepted by M, which is a contradiction since p(m+1) is not a

prime.

qπ(0)

qπ(p) Fa path in M

A loop oflength m

qπ(i) = qπ(j)

Page 23: Regular Language

Closure Properties Regular sets are said to be closed under an

operation op if the application of op to a

regular set will result in a regular set.

For example, if the union of two regular sets

will result in a regular set, regular sets are

said to be closed under union.

Page 24: Regular Language

Closure Properties Are regular sets closed under:

Union ?Concatenation ?Kleene Closure ?

Why?

Page 25: Regular Language

Closure Properties Are regular sets closed under complementation?

If A is a regular set over , is A’ = *-A regular?

Why?

If A is regular, there exists a DFA M recognizing A.

Given M, we can construct a DFA M’ for A’ by

copying M to M’ except that all final states in M

are changed to non-final, and all non-final states to

final.

Page 26: Regular Language

Closure Properties Are regular sets closed under intersection?

If A and B are regular sets, is C = AB

regular? Why?

C = AB = AB

Since regular sets are closed under union and

complementation, they are also closed under

intersection.

Page 27: Regular Language

Closure Properties (Summary) Regular sets are closed under:

UnionConcatenationKleene StarComplementationIntersectionReversal

Page 28: Regular Language

Thank you