Chapter 4 Properties of Regular Languages 1 4

  • Slides: 45
Download presentation
 Chapter 4 Properties of Regular Languages 1

Chapter 4 Properties of Regular Languages 1

 4. 1: Closure Properties of RLs (1) m A language L over an

4. 1: Closure Properties of RLs (1) m A language L over an alphabet is regular if it q has a regular set (expression) over q is accepted y DFA, NFA, or NFA- q is generated by regular grammar 2

 4. 1: Closure Properties of RLs (2) 3

4. 1: Closure Properties of RLs (2) 3

 4. 1: Closure Properties of RLs (3) m If L 1 and L

4. 1: Closure Properties of RLs (3) m If L 1 and L 2 are regular languages, then q L 1 L 2 Union Ø = {w * : w L 1 or w L 2} m q L 1 L 2 Intersection q L 1 L 2 Concatenation q L* Kleene Star q L 1 Complement Ø = {w * : w L 1 and w L 2} Ø n languages: L 1 L 2 …Ln = {w 1 w 2…wn : wi Li} Ø one languages: Ln = {w 1 w 2…wn : wi L} Ø L*= L 0 L 1 L 2 … Ø L = * - L = {w : w * and w L} are also regular languages 4

 4. 1: Closure Properties of RLs (4) m m Prove that RLs are

4. 1: Closure Properties of RLs (4) m m Prove that RLs are closed under complementation. If L is a regular language over , then L = * - L is regular language. Proof: q If L is regular, there exists a DFA M recognizing L. q 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 proof 5

 4. 1: Closure Properties of RLs (5) m m m m Let M

4. 1: Closure Properties of RLs (5) m m m m Let M 1=(Q, , , q 0, F) a DFA that accepts L(M 1) Let M 2=(Q, , , q 0, Q-F) a DFA that accepts L(M 2) Obviously both languages are regular languages Per definition 2. 2: L(M) = {w * : (q 0, w) F} The following are both true q w * : (q 0, w) F (q 0, w) Q – F q w * : (q 0, w) Q - F (q 0, w) F Thus L(M 2) = L(M 1) is an arbitrary regular language and its complement is also a regular language, therefore the regular languages are closed under complementation 6

 4. 1: Closure Properties of RLs (6) m m m Are regular languages

4. 1: Closure Properties of RLs (6) m m m Are regular languages closed under intersection? If L 1 and L 2 are RLs, then L 1 L 2 is RL. Proof: q Since RLs are closed under union and complementation, they are also closed under intersection q L 1, L 2 are RLs, so L 1, L 2 are RLs, and L 1 L 2 is RL, so L 1 L 2 is RL. q Thus L 1 L 2 = L 1 L 2 is RL. 7

 4. 1: Closure Properties of RLs (7) m m Are regular languages closed

4. 1: Closure Properties of RLs (7) m m Are regular languages closed under difference? If L 1 and L 2 are RLs, is L 1 - L 2 RL? Why? m q q L 1 - L 2 = L 1 L 2 Since RLs are closed under intersection and complementation, they are also closed under difference. 8

 4. 1: Closure Properties of RLs (8) m Let L, M and N

4. 1: Closure Properties of RLs (8) m Let L, M and N are regular expressions, which of the followings are correct? m Commutative Rules: q L M=M L q LM = ML m Associative Rules: q (L M) N = L (M N) q (LM)N = L(MN) 9

 4. 1: Closure Properties of RLs (9) m Which of the followings are

4. 1: Closure Properties of RLs (9) m Which of the followings are correct? m Left Distributive Rules: q L(M N) = LM LN q L (MN) = (L M)(L N) m Right Distributive Rules: q (M N)L = ML NL q (MN) L = (M L)(N L) 10

 4. 1: Closure Properties of RLs (10) m Identities: q is the identity

4. 1: Closure Properties of RLs (10) m Identities: q is the identity for union: Ø L=L =L q is the identity for concatenation: Ø L = L 11

 4. 1: Closure Properties of RLs (11) m Other Rules for Kleene Closure

4. 1: Closure Properties of RLs (11) m Other Rules for Kleene Closure q q q (L*)* = L* L+ = LL* = L*L L* = L+ | *= * = 12

 4. 2: Elementary Questions about RLs m Membership question: q If L is

4. 2: Elementary Questions about RLs m Membership question: q If L is a RL over , then for any string w * there exists an algorithm for determining whether or not w is in L. Ø DFA m Empty, finite, or infinite question: q There exists an algorithm for determining whether a RL is empty, finite, or infinite. Ø DFA (path from start to final, cycle) m Equality question: q If L 1 and L 2 are RLs, then there exists an algorithm to determine whether or not L 1 = L 2. Ø L 3 = (L 1 L 2) = iff L 1 = L 2 13

 4. 3: Non-regular Languages (1) m The language q L = {an bn

4. 3: Non-regular Languages (1) m The language q L = {an bn | n 0} is not regular q There is NO DFA/NFA that accepts L m Theorem: If L 1 is RL and L 2 is not-regular, then q The language L = L 1 L 2 is not necessarily regular Proof: q L 1 = a*b* is regular q L 2 = {an bn | n 0} is not regular q L 1 L 2 = L 2 which is not regular m m 14

 4. 3: Non-regular Languages (2) m m The language q L = {an

4. 3: Non-regular Languages (2) m m The language q L = {an bm | n, m 0 and n m} is not regular. Proof: q L is regular iff L a*b* is regular. q But L a*b* = {an bn | n 0}, which we know is not regular. q Then L is not regular. 15

 4. 3: Non-regular Languages (3) m Many languages are non-regular: q {anbn |

4. 3: Non-regular Languages (3) m Many languages are non-regular: q {anbn | n 0} q {ww. R} | w * } 2 q {an | n 0} q {ap | p is prime} q set of well-formed parentheses q set of palindromes: {w = w. R} q …. . . 16

 4. 3: Non-regular Languages (4) m We want to prove that L =

4. 3: Non-regular Languages (4) m We want to prove that L = {akbk | k 0} is nonregular. Prove by contradiction: q q 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: q 0 a q 1 a q 2 a a qr b qr+1 b …. . . ar …. . . b q 2 r br path for accepting arbr 17

 4. 3: Non-regular Languages (5) q Since r n and M has only

4. 3: Non-regular Languages (5) q 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. qi = qj a path in M q 0 q q qf F By skipping the loop, ar-(j-i)br should also be accepted by M, but this is contradictory since ar-(j-i)br L 18

 4. 3: Non-regular Languages (6) m 2 k {a We want to prove

4. 3: Non-regular Languages (6) m 2 k {a We want to prove that L = | k 0} is nonregular. Prove by contradiction: q q 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 an 2 q 0 a q 1 a q 2 a …. . . a q 2 n path for accepting an 2 19

 4. 3: Non-regular Languages (7) q Since n 2 n and M has

4. 3: Non-regular Languages (7) q Since n 2 n and M has only n states, there must be at least two equal states from q 0 to qn 2. Let them be qi and qj where j - i = m n. A loop of length m qi = qj a path in M q 0 q qn 2 F By repeating the loop one more time, a(n 2+m) is also accepted by M, which is a contradiction, since (n 2+m) cannot be a square (the next square after n 2 is (n+1)2 but n 2+m (n+1)2) 20

 4. 3: Non-regular Languages (8) m We want to prove that L =

4. 3: Non-regular Languages (8) m We want to prove that L = {ap | p is a prime} is non -regular. Prove by contradiction: q q 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 q 0 a q 1 a q 2 a …. . . a qp path for accepting ap 21

 4. 3: Non-regular Languages (9) q Since p n and M has only

4. 3: Non-regular Languages (9) q Since p n and M has only n states, there must be at least two equal states from q 0 to qp. Let them be qi and qj where j - i = m 0 qi = qj A loop of length m a path in M q 0 q qp F 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 22

 4. 3: Pumping Lemma (1) m If L is a regular language, there

4. 3: Pumping Lemma (1) m If L is a regular language, there is a DFA which recognizes L. q All finite languages are regular Ø , {a}, {abaabb, b, baabbbbb, bb} q So, only infinite languages are interesting for this discussion. m What are non-regular languages? m Pumping Lemma q A technique for proving a language L is NOT regular 23

 4. 3: Pumping Lemma (2) m m m Any given DFA has a

4. 3: Pumping Lemma (2) m m m Any given DFA has a finite number of states Let k = |Q| Consider any accepted input w = a 1…an, such that |w| = n k q DFA follows a path of states q 0…qn: q 0 q q a 1 q 1 a 2 … an-1 qn-1 an qn This state numbering has nothing to do with state names in Q May repeat states in this path 24

 4. 3: Pumping Lemma (3) m Since |w| k, path must contain at

4. 3: Pumping Lemma (3) m Since |w| k, path must contain at least one loop q Consider any one such loop (cycle) q Break string & path into three parts: before, during, & after loop aj q 0 m a 1 … ai … qi=qj ai+1 aj+1 … an qn Given other inputs, could loop an arbitrary number of times q (a 1…ai)(ai+1…aj)*(aj+1…an) also accepted 25

 4. 3: Pumping Lemma (4) m m m If language L is regular,

4. 3: Pumping Lemma (4) m m m If language L is regular, then for any w L that is longer than |Q|, there is a substring of w that can be repeated (pumped) any number of times with resulting string remaining in the language Lemma: q Let G be the state diagram of a DFA with k states. Any path of length k in G contains a cycle. Proof: q 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. 26

 4. 3: Pumping Lemma (5) m Let G be the state diagram of

4. 3: Pumping Lemma (5) m Let G be the state diagram of a DFA with k states and let p be a path of length k. q q 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 27

 4. 3: Pumping Lemma (6) m m Pumping Lemma Let L be a

4. 3: Pumping Lemma (6) m m Pumping Lemma Let L be a regular language is accepted by a DFA M with k states. Let for ALL w L and |w| k. Then w can be divided into three parts, w = uvz, satisfying the following conditions: q |uv| k, q |v| 1, and q for all i 0, uviz L 28

 4. 3: Pumping Lemma (7) m m m Proof: Let w L with

4. 3: Pumping Lemma (7) m m m Proof: Let w L with |w| = n k where w(i) is the ith symbol of the string w. q 0 w(1) q 1 w(2) q 2 w(3) …. . . w(n) qn m m Since n k and M has only k states, there must be at least one repeated state from q 0 to qn Let qi be the first such repeated state qi q 0 a path in M 29 qn F

 4. 3: Pumping Lemma (8) qi a path in M q 0 q

4. 3: Pumping Lemma (8) qi a path in M q 0 q qn F Let u be the string obtained by traversing from q 0 to qi, v be the string obtained by traversing the loop once (so |v| 1) In the traversal from q 0 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. 30

 4. 3: Pumping Lemma (9) m How to use pumping lemma to prove

4. 3: Pumping Lemma (9) m How to use pumping lemma to prove some L isn’t regular? q Lemma describes a property of regular languages. q Use proof by contradiction: If L doesn't have this property, it is NOT regular m We cannot use the pumping lemma to prove a language is regular q Since we have to check every string in L 31

 4. 3: Pumping Lemma (10) m m m m We assume the given

4. 3: Pumping Lemma (10) m m m m 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 q |uv| n q |v| 1 Claim that uviz are also in L for all i Find one i such that uviz L Contradiction Only one string w is needed q do not need to consider all general cases q choose w that can led to contradiction easily 32

 4. 3: Pumping Lemma (11) m Example 1: q L = {am bm

4. 3: Pumping Lemma (11) m Example 1: q L = {am bm | m 0} q Assume L is regular, n is the # of DFA states q Let w = anbn where |w| = 2 n n where p + j n and j 1 Ø w = ap aj an-p-j bn Øw=u v z q q u & v contain only a’s, since |uv| n Pick i = 2: Ø uv 2 z = ap aj aj an-p-j bn = an aj bn Ø uv 2 z contains more a’s than b’s, since |v| 1 Ø uv 2 z L q Contradiction and hence L is not regular 33

 4. 3: Pumping Lemma (12) m Example 2: q L = {w |

4. 3: Pumping Lemma (12) m Example 2: q L = {w | w {a, b}*, w has equal number of a’s and b’s} q w = an bn q Same argument as previous example m Choice of w is important q Different w’s may lead to different proofs, or none being possible Choice of i is important. q Different i’s may lead to different proofs, or none being possible m 34

 4. 3: Pumping Lemma (13) m Example 3: q L = {(ab)i |

4. 3: Pumping Lemma (13) m Example 3: q L = {(ab)i | i 0} q L is regular: L = (ab)* Ø i. e. , for any w, there is some split u, v, z that allows pumping 35

 4. 3: Pumping Lemma (14) m m m Example 4: Prove that the

4. 3: Pumping Lemma (14) m m m 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| = 2 n n q we do not need to consider other possible cases like ()() q 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 36

 4. 3: Pumping Lemma (15) m By pumping lemma, (m(ji(n-m-j )n = (n+j(i-1)

4. 3: Pumping Lemma (15) m By pumping lemma, (m(ji(n-m-j )n = (n+j(i-1) )n L for all i m When i = 0 (n-j )n L q since j 1 n-j n m Contradiction m We can choose i = 2, i = 3, . . . q but cannot choose i = 1 L is not regular 37

 4. 3: Pumping Lemma (16) To prove L is regular m 1. Constructing

4. 3: Pumping Lemma (16) To prove L is regular m 1. Constructing a FSA (NFA or DFA), RE, or RG for L m 2. Using closure property of regular sets q Let ⊕ be a binary operation (such as union, concatenation and intersection) ØL 3 = L 1 ⊕ L 2 is regular, if both L 1 and L 2 are regular q Let ⊡ be an unary operation (such as complementation and Kleene star) Ø L 1 = ⊡ L 2 is regular, if L 2 is regular 38

 4. 3: Pumping Lemma (17) Example m Prove that L is regular, where

4. 3: Pumping Lemma (17) Example m Prove that L is regular, where L is the set of all strings with three consecutive 0's and = {0, 1} q Let L 1 = (0 | 1)*, which is regular and L 2 = 000, which is also regular q then L = L 1.L 2.L 1 q Hence L is regular due to the closure property of regular sets under concatenation. 39

 4. 3: Pumping Lemma (18) Prove L is not regular m 1. Using

4. 3: Pumping Lemma (18) Prove L is not regular m 1. Using Pumping Lemma m 2. Using closure property of regular sets q Let ⊕ be a binary operation (such as union, concatenation and intersection) Øif L 3 = L 1 ⊕ L 2 and L 1 is regular and L 3 is not regular, then L 2 must be non-regular q Let ⊡ be an unary operation (such as complementation and Kleene star) Øif L 1 = ⊡ L 2 and L 2 is not regular, then L 1 is non-regular 40

 4. 3: Pumping Lemma (19) m L = {w {a, b}*, w has

4. 3: Pumping Lemma (19) m L = {w {a, b}*, w has equal number of a’s and b’s} q Consider q By L’ = L a*b* = {an bn | n 0} closure properties of RLs: Øif L is regular, then L’ is also regular ØBut since L’ not regular, so L can’t be regular 41

 4. 3: Pumping Lemma (20) Example m Prove that L is not regular,

4. 3: Pumping Lemma (20) Example m 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. Let q L 1 = (anb 2 n) , which is non-regular, and L 2 = a*b*, which is regular q then L 1 = L L 2 q By closure properties of RLs: Øif L is regular, then L 1 is also regular ØBut since L 1 not regular, so L can’t be regular 42

 4. 3: Pumping Lemma (21) Common Mistakes m Let L 1 regular language,

4. 3: Pumping Lemma (21) Common Mistakes m Let L 1 regular language, L 3 and L 4 are not regular. m Is L regular? q L = L 1 ⊕ L 3 Ø 0 n 1 n = 0*1* 0 n 1 n Ø 0*1* = 0*1* 0 n 1 n q. L = L 3 ⊕ L 4 Ø 0*1* = 0 p 1 q 0 q 1 p , where p q q L 3 = L ⊕ L 4 Ø 0 n 1 n = 0*1* 0 n 1 n 43

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

Class Discussion m Prove that L = {ww : w (0|1)*} is non-regular m Is L = {(01)6 n : n 0} regular? m Is L = {(ab)n : n 0} regular? 44

 Summary m We use the Pumping Lemma to prove a language is not

Summary m We use the Pumping Lemma to prove a language is not regular q Note, does not work for all non-regular languages, though m Choosing a good string w is first key step m Choosing a good integer i is second key step m Must apply argument to all legal u, v, z 45