chapter 4 properties of regular languages

44
1 Chapter 4 Chapter 4 Properties of Properties of Regular Languages Regular Languages

Upload: lysandra-bullock

Post on 31-Dec-2015

165 views

Category:

Documents


9 download

DESCRIPTION

Chapter 4 Properties of Regular Languages. 4.1:Closure Properties of RLs (1). A language L over an alphabet S is regular if it has a regular set (expression) over S is accepted y DFA, NFA, or NFA- e is generated by regular grammar. 4.1:Closure Properties of RLs (2). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 4 Properties of  Regular Languages

1

Chapter 4Chapter 4

Properties of Properties of

Regular LanguagesRegular Languages

Page 2: Chapter 4 Properties of  Regular Languages

2

4.1:4.1: Closure Properties of RLs (1)Closure Properties of RLs (1)

A language A language LL over an alphabet over an alphabet is is regularregular if itif it has a regular set (expression) over

is accepted y DFA, NFA, or NFA-

is generated by regular grammar

Page 3: Chapter 4 Properties of  Regular Languages

3

4.1:4.1: Closure Properties of RLs (2)Closure Properties of RLs (2)

Page 4: Chapter 4 Properties of  Regular Languages

4

4.1:4.1: Closure Properties of RLs (3)Closure Properties of RLs (3)

If If LL11 and and LL22 are regular languages, then are regular languages, then L1 L2 Union

= {w * : w L1 or w L2} L1 L2 Intersection

= {w * : w L1 and w L2} L1 L2 Concatenation

n languages: L1L2 …Ln = {w1w2…wn : wi Li} one languages: Ln = {w1w2…wn : wi L}

L* Kleene Star L*= L0 L1 L2 …

L1 Complement L = * - L = {w : w * and w L}

are also regular languagesare also regular languages

Page 5: Chapter 4 Properties of  Regular Languages

5

4.1:4.1: Closure Properties of RLs (4)Closure Properties of RLs (4)

Prove that RLs are closed under Prove that RLs are closed under complementationcomplementation.. If If LL is a regular language over is a regular language over , then , then LL = = ** - - LL is is

regular language.regular language. Proof:Proof:

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

We can construct a DFA M’ for L by copying M to M’ except that all final states in M are changed to non-final, and all non-final states to final.

See next slide for a formal proofSee next slide for a formal proof

Page 6: Chapter 4 Properties of  Regular Languages

6

4.1:4.1: Closure Properties of RLs (5)Closure Properties of RLs (5)

Let Let MM11=(=(QQ, , , , , , qq00, , FF) a DFA that accepts ) a DFA that accepts LL((MM11)) Let Let MM22=(=(QQ, , , , , , qq00, , QQ--FF) a DFA that accepts ) a DFA that accepts LL((MM22)) Obviously both languages are regular languagesObviously both languages are regular languages Per definition 2.2: Per definition 2.2: LL((MM) = {) = {ww ** : : ((qq00, , w)w) FF} } The following are both trueThe following are both true

w * : (q0, w) F (q0, w) Q – F w * : (q0, w) Q - F (q0, w) F

Thus Thus LL((MM22) = ) = LL((MM11)) LL((MM11) is an arbitrary regular language and its ) is an arbitrary regular language and its

complement is also a regular language, therefore the complement is also a regular language, therefore the regular languages are closed under complementationregular languages are closed under complementation

Page 7: Chapter 4 Properties of  Regular Languages

7

4.1:4.1: Closure Properties of RLs (6)Closure Properties of RLs (6)

Are regular languages closed under Are regular languages closed under intersectionintersection?? If If LL11 and and LL22 are RLs, then are RLs, then LL11 LL22 is RL. is RL.

ProofProof:: Since RLs are closed under union and

complementation, they are also closed under intersection

L1, L2 are RLs, so L1, L2 are RLs, and L1 L2

is RL, so L1 L2 is RL.

Thus L1 L2 = L1 L2 is RL.

Page 8: Chapter 4 Properties of  Regular Languages

8

4.1:4.1: Closure Properties of RLs (7)Closure Properties of RLs (7)

Are regular languages closed under Are regular languages closed under differencedifference?? If If LL11 and and LL22 are RLs, is are RLs, is LL11 - - LL22 RL? Why? RL? Why?

L1 - L2 = L1 L2

Since RLs are closed under intersection and complementation, they are also closed under difference.

Page 9: Chapter 4 Properties of  Regular Languages

9

4.1:4.1: Closure Properties of RLs (8)Closure Properties of RLs (8)

Let Let LL, , MM and and NN are regular expressions, which of are regular expressions, which of the followings are correct?the followings are correct?

Commutative Rules:Commutative Rules: L M = M L LM = ML

Associative Rules:Associative Rules: (L M) N = L (M N) (LM)N = L(MN)

Page 10: Chapter 4 Properties of  Regular Languages

10

4.1:4.1: Closure Properties of RLs (9)Closure Properties of RLs (9)

Which of the followings are correct?Which of the followings are correct?

Left Distributive Rules: L(M N) = LM LN L (MN) = (L M)(L N)

Right Distributive Rules: (M N)L = ML NL (MN) L = (M L)(N L)

Page 11: Chapter 4 Properties of  Regular Languages

11

4.1:4.1: Closure Properties of RLs (10)Closure Properties of RLs (10)

Identities:Identities:

is the identity for union: L = L = L

is the identity for concatenation: L = L = L

Page 12: Chapter 4 Properties of  Regular Languages

12

4.1:4.1: Closure Properties of RLs (11)Closure Properties of RLs (11)

Other Rules for Kleene ClosureOther Rules for Kleene Closure

(L*)* = L*

L+ = LL* = L*L L* = L+ U {} * = {} * =

Page 13: Chapter 4 Properties of  Regular Languages

13

4.3:4.3: Non-regular Languages (1)Non-regular Languages (1)

The language The language L = {an bn | n 0} is not regular There is NO DFA/NFA that accepts L

Theorem:Theorem: If If LL11 is RL and is RL and LL22 is not-regular, then is not-regular, then

The language L = L1 L2 is not necessarily regular

Proof:Proof: L1 = a*b* is regular L2 = {an bn | n 0} is not regular L1 L2 = L2 which is not regular

Page 14: Chapter 4 Properties of  Regular Languages

14

4.3:4.3: Non-regular Languages (2)Non-regular Languages (2)

The languageThe language L = {an bm | n, m 0 and n m} is not regular.

Proof: Proof: L is regular iff L a*b* is regular. But L a*b* = {an bn | n 0}, which we know

is not regular.

Then L is not regular.

Page 15: Chapter 4 Properties of  Regular Languages

15

4.3:4.3: Non-regular Languages (3)Non-regular Languages (3)

Many languages are non-regular:Many languages are non-regular: {anbn | n 0} {wwR} | w * } {an2 | n 0} {ap | p is prime} set of well-formed parentheses set of palindromes: {w = wR} …...

Page 16: Chapter 4 Properties of  Regular Languages

16

4.3:4.3: Non-regular Languages (4)Non-regular Languages (4)

We want to prove that We want to prove that LL = { = {aakkbbkk | | kk 0 0} is non-} is non-regular. Prove by contradiction:regular. Prove by contradiction:

Assume that there is a DFA M which recognizes L. Let n be the no. of states in M

Consider the acceptance of the input arbr where r = n:

q0 q1 q2 qr qr+1 q2ra a b b b…...…...

a a

arbr

path for accepting arbr

Page 17: Chapter 4 Properties of  Regular Languages

17

4.3:4.3: Non-regular Languages (5)Non-regular Languages (5)

Since r = n and M has only n states, there must be at least one state visited twice in the first r transitions. Let this state be visited at the ith and the jth steps, where j i.

By skipping the loop, ar-(j-i)br should also be accepted by M,

but this is contradictory since ar-(j-i)br L

q0

qi = qj

qf Fa path in M

Page 18: Chapter 4 Properties of  Regular Languages

18

4.3:4.3: Non-regular Languages (6)Non-regular Languages (6)

We want to prove that We want to prove that LL = { = {aakk22 | | kk 0} is non- 0} is non-

regular. Prove by contradiction:regular. Prove by contradiction:

Assume that there is a DFA M which recognizes L. Let n be the no. of states in M

M should also accept the string an2

q0 q1 q2 qn2a a

…...a a

path for accepting an2

Page 19: Chapter 4 Properties of  Regular Languages

19

4.3:4.3: Non-regular Languages (7)Non-regular Languages (7)

Since n2 n and M has only n states, there must be at least two equal states from q0 to qn2. Let them be qi and qj where j - i = m n.

By repeating the loop one more time, a(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)

q0

qi = qj

qn2 Fa path in M

A loop oflength m

Page 20: Chapter 4 Properties of  Regular Languages

20

4.3:4.3: Non-regular Languages (8)Non-regular Languages (8)

We want to prove that We want to prove that LL = { = {aapp | | pp is a prime} is is a prime} is non-regular. Prove by contradiction:non-regular. Prove by contradiction:

Assume that there is a DFA M which recognizes L. Let n be the no. of states in M

From Number Theory, we know that the no. of primes are infinite, so there exists a prime p n

q0 q1 q2 qpa a

…...a a

path for accepting ap

Page 21: Chapter 4 Properties of  Regular Languages

21

4.3:4.3: Non-regular Languages (9)Non-regular Languages (9)

Since p n and M has only n states, there must be at least two equal states from q0 to qp. Let them be qi and qj where j - i = m 0

By repeating the loop (p-m) times, a(p-m)m + (p-m) = a(p-m)(m+1) is also accepted by M, which is a contradiction since (p-m)(m+1) is not a prime, since it can be divided by (p-m) and (m+1) 1

q0

qi = qj

qp Fa path in M

A loop oflength m

Page 22: Chapter 4 Properties of  Regular Languages

22

4.3:4.3: Pumping Lemma (1) Pumping Lemma (1)

If If LL is a regular language, there is a DFA which is a regular language, there is a DFA which recognizes recognizes LL. . All finite languages are regular

, {a}, {abaabb, b, baabbbbb, bb} So, only infinite languages are interesting for

this discussion.

What are non-regular languages?What are non-regular languages?

Pumping LemmaPumping Lemma A technique for proving a language L is NOT

regular

Page 23: Chapter 4 Properties of  Regular Languages

23

4.3:4.3: Pumping Lemma (2)Pumping Lemma (2)

Any given DFA has a finite number of statesAny given DFA has a finite number of states Let Let kk = | = |QQ|| Consider any accepted input Consider any accepted input w = a1…an, such that |

w| = n kk DFA follows a path of states q0…qn:

This state numbering has nothing to do with state names in Q

May repeat states in this path

q0

a1 q1… qn-1 qn

a2 anan-1

Page 24: Chapter 4 Properties of  Regular Languages

24

4.3:4.3: Pumping Lemma (3)Pumping Lemma (3)

Since |Since |ww| | kk, path must contain at , path must contain at least one loopleast one loop Consider any one such loop (cycle) Break string & path into three parts: before,

during, & after loop

Given other inputs, could loop an arbitrary number Given other inputs, could loop an arbitrary number of timesof times (a1…ai)(ai+1…aj)*(aj+1…an) also accepted

q0

a1 qi=qj… qn

ai an

ai+1

…aj+1

…aj

Page 25: Chapter 4 Properties of  Regular Languages

25

4.3:4.3: Pumping Lemma (4)Pumping Lemma (4)

If language If language LL is regular, then for any is regular, then for any ww LL that is that is longer than |longer than |QQ|, there is a |, there is a substringsubstring of of ww that can be that can be repeated (repeated (pumpedpumped) any number of times with ) any number of times with resulting string remaining in the languageresulting string remaining in the language

Lemma:Lemma: Let G be the state diagram of a DFA with k states.

Any path of length k in G contains a cycle. Proof:Proof:

A path of length k contains k+1 nodes. Since there are only k nodes (states) in G, there must be a node, call it qi, that occurs in at least two positions in the path. The subpath from the first occurrence of qi to the second produces the desired cycle.

Page 26: Chapter 4 Properties of  Regular Languages

26

4.3:4.3: Pumping Lemma (5)Pumping Lemma (5)

Let Let GG be the state diagram of a DFA with be the state diagram of a DFA with kk states and let states and let pp be a path of length be a path of length kk..

The path p can be subdivided into subpaths u, v, and z where p = uvz

The length of uv is |uv| k, and v is a cycle

Page 27: Chapter 4 Properties of  Regular Languages

27

4.3:4.3: Pumping Lemma (6)Pumping Lemma (6)

Pumping Lemma Pumping Lemma Let Let LL be a regular language is accepted by a DFA be a regular language is accepted by a DFA

MM with with kk states. Let for ALL states. Let for ALL ww LL and | and |ww| | kk. . Then Then ww can be divided into three parts, can be divided into three parts, ww = = uuvvzz, , satisfying the following conditions:satisfying the following conditions:

|uv| k,

|v| 1, and

for all i 0, uviz L

Page 28: Chapter 4 Properties of  Regular Languages

28

4.3:4.3: Pumping Lemma (7)Pumping Lemma (7)

Proof: Let w L with |w| = n k where w(i) is the ith symbol of the string w.

Since n k and M has only k states, there must be at least one repeated state from q0 to qn

Let qi be the first such repeated state

q0 q1 q2 qnw(2) w(3) …...w(1) w(n)

q0

qi

qn Fa path in M

Page 29: Chapter 4 Properties of  Regular Languages

29

4.3:4.3: Pumping Lemma (8)Pumping Lemma (8)

Let u be the string obtained by traversing from q0 to qi, v be the string obtained by traversing the loop once (so |v| 1)

In the traversal from q0 to qi and then through the loop once back to qi, nothing except qi repeats, thus |uv| k

By traversing the loop 0 or more times, we obtain uviz for all i 0 and they should all be accepted by the DFA, i.e., in L.

q0

qi

qn Fa path in M

Page 30: Chapter 4 Properties of  Regular Languages

30

4.3:4.3: Pumping Lemma (9)Pumping Lemma (9)

How to use pumping lemma to prove some L isn’t regular? Lemma describes a property of regular

languages. Use proof by contradiction: If L doesn't have

this property, it is NOT regular

We cannot use the pumping lemma to prove a language is regular Since we have to check every string in L

Page 31: Chapter 4 Properties of  Regular Languages

31

4.3: Pumping Lemma (10)4.3: Pumping Lemma (10)

We assume the given language L is regular Let n be the constant of pumping lemma Find a string w L where |w| n Divide w into 3 parts, w = uvz, where

|uv| n |v| 1

Claim that uviz are also in L for all i Find one i such that uviz LL Contradiction

Only one string Only one string ww is needed is needed do not need to consider all general cases choose w that can led to contradiction easily

Page 32: Chapter 4 Properties of  Regular Languages

32

4.3:4.3: Pumping Lemma (11)Pumping Lemma (11)

Example 1: L = {am bm | m 0} Assume L is regular, n is the # of DFA states Let w = anbn where |w| = 2n

n w = ap aj an-p-j bn where p + j n and j 1 w = u v z

u & v contain only a’s, since |uv| n Pick i = 2:

uv2z = ap aj aj an-p-j bn = an aj bn uv2z contains more a’s than b’s, since |v| 1 uv2z L

Contradiction and hence L is not regular

Page 33: Chapter 4 Properties of  Regular Languages

33

4.3:4.3: Pumping Lemma (12)Pumping Lemma (12)

Example 2:Example 2: L = {w | w {a, b}*, w has equal number of a’s

and b’s} w = an bn

Same argument as previous example

Choice of Choice of ww is important is important Different w’s may lead to different proofs, or

none being possible Choice of Choice of ii is important. is important.

Different i’s may lead to different proofs, or none being possible

Page 34: Chapter 4 Properties of  Regular Languages

34

4.3:4.3: Pumping Lemma (13)Pumping Lemma (13)

Example 3:Example 3: L = {(ab)i | i 0} L is regular: L = (ab)*

i.e., for any w, there is some split u, v, z that allows pumping

Page 35: Chapter 4 Properties of  Regular Languages

35

4.3:4.3: Pumping Lemma (14)Pumping Lemma (14)

Example 4: Prove that the language L of well-formed

parentheses is non-regular Assume L is regular and let n be the constant of

pumping lemma Let w = (n)n L, |w| = 2n n

we do not need to consider other possible cases like ()()

we only need to ensure |w| n Then w = (m (j (n-m-j )n where j 1 and m + j n

u v z

Page 36: Chapter 4 Properties of  Regular Languages

36

4.3:4.3: Pumping Lemma (15)Pumping Lemma (15)

By pumping lemma,By pumping lemma,

((mm((jjii((nn--mm--jj ))nn = = ((nn++jj((ii-1)-1) ))nn LL for all for all ii

When When ii = 0 = 0 ((nn--jj ))nn LL since since jj 11 nn--jj nn

ContradictionContradiction LL is not regular is not regular

We can choose We can choose i i = 2, = 2, ii = 3, ... = 3, ... but cannot choose i = 1

Page 37: Chapter 4 Properties of  Regular Languages

37

4.3:4.3: Pumping Lemma (16)Pumping Lemma (16)

To prove L is regular

1. Constructing a FSA (NFA or DFA), RE, or RG for L

2. Using closure property of regular sets Let ⊕ be a binary operation (such as union,

concatenation and intersection)L3 = L1 ⊕ L2 is regular, if both L1 and L2 are regular

Let ⊡ be an unary operation (such as complementation and Kleene star) L1 = ⊡ L2 is regular, if L2 is regular

Page 38: Chapter 4 Properties of  Regular Languages

38

4.3:4.3: Pumping Lemma (17)Pumping Lemma (17)

Example Example Prove that Prove that LL is regular, where is regular, where LL is the set of all is the set of all

strings with three consecutive 0's and strings with three consecutive 0's and = {0,1} = {0,1} Let L1 = (0 | 1)*, which is regular and

L2 = 000, which is also regular then L = L1 . L2 . L1 Hence L is regular due to the closure property

of regular sets under concatenation.

Page 39: Chapter 4 Properties of  Regular Languages

39

4.3:4.3: Pumping Lemma (18)Pumping Lemma (18)

Prove Prove LL is not regular is not regular 1. Using Pumping Lemma 2. Using closure property of regular sets

Let ⊕ be a binary operation (such as union, concatenation and intersection)if L3 = L1 ⊕ L2 and L1 is regular and L3 is not regular,

then L2 must be non-regular

Let ⊡ be an unary operation (such as complementation and Kleene star)if L1 = ⊡ L2 and L2 is not regular, then L1 is non-regular

Page 40: Chapter 4 Properties of  Regular Languages

40

4.3:4.3: Pumping Lemma (19)Pumping Lemma (19)

L = {w {a, b}*, w has equal number of a’s and b’s}

Consider L’ = L a*b* = {an bn | n 0}

By closure properties of RLs:if L is regular, then L’ is also regular

But since L’ not regular, so L can’t be regular

Page 41: Chapter 4 Properties of  Regular Languages

41

4.3:4.3: Pumping Lemma (20)Pumping Lemma (20)

Example Prove that L is not regular, where L is the set of all

strings over {a, b} such that the number of b is the double of the number of a. HW: Use pumping lemma to prove it.

Page 42: Chapter 4 Properties of  Regular Languages

42

4.3:4.3: Pumping Lemma (21)Pumping Lemma (21)

Common Mistakes Let L1 regular language, L3 and L4 are not regular. Is L regular?

L = L1 ⊕ L3

0n1n = 0*1* 0n1n

0*1* = 0*1* 0n1n

L = L3 ⊕ L4

0*1* = 0p1q 0q1p , where p q

L3 = L ⊕ L4

0n1n = 0*1* 0n1n

Page 43: Chapter 4 Properties of  Regular Languages

43

Class DiscussionClass Discussion

Prove that L = {ww : w (0|1)*} is non-regular

Is L = {(01)6n : n 0} regular?

Is L = {(ab)n : n 0} regular?

Page 44: Chapter 4 Properties of  Regular Languages

44

SummarySummary

We use the Pumping Lemma to prove a language is We use the Pumping Lemma to prove a language is not regularnot regular Note, does not work for all non-regular

languages, though

Choosing a good string Choosing a good string w w is first key stepis first key step

Choosing a good integer Choosing a good integer ii is second key step is second key step

Must apply argument to all legal Must apply argument to all legal uu, , vv, , zz