450 likes | 480 Views
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).
E N D
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 (3) • If L1 and L2 are regular languages, then • L1L2 Union • = {w* : w L1 or w L2} • L1L2 Intersection • = {w* : w L1 and w L2} • L1L2 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 languages
4.1: Closure Properties of RLs (4) • Prove that RLs are closed under complementation. • If L is a regular language over , then L = * - L is regular language. • 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 proof
4.1: Closure Properties of RLs (5) • Let M1=(Q, , , q0, F) a DFA that accepts L(M1) • Let M2=(Q, , , q0, Q-F) a DFA that accepts L(M2) • Obviously both languages are regular languages • Per definition 2.2: L(M) = {w* : (q0, w) F} • The following are both true • w* : (q0, w) F (q0, w) Q – F • w* : (q0, w) Q - F (q0, w) F • Thus L(M2) = L(M1) • L(M1) is an arbitrary regular language and its complement is also a regular language, therefore the regular languages are closed under complementation
4.1: Closure Properties of RLs (6) • Are regular languages closed under intersection? • If L1 and L2 are RLs, then L1 L2 is RL. • Proof: • Since RLs are closed under union and complementation, they are also closed under intersection • L1, L2 are RLs, so L1, L2are RLs, and L1L2 is RL, so L1L2is RL. • Thus L1L2= L1 L2is RL.
4.1: Closure Properties of RLs (7) • Are regular languages closed under difference? • If L1 and L2 are RLs, is L1 - L2 RL? Why? • L1 - L2 = L1 L2 • Since RLs are closed under intersection and complementation, they are also closed under difference.
4.1: Closure Properties of RLs (8) • Let L, M and N are regular expressions, which of the followings are correct? • Commutative Rules: • LM = ML • LM = ML • Associative Rules: • (LM) N = L (MN) • (LM)N = L(MN)
4.1: Closure Properties of RLs (9) • Which of the followings are correct? • Left Distributive Rules: • L(MN) = LMLN • L (MN) = (LM)(LN) • Right Distributive Rules: • (MN)L = MLNL • (MN) L = (ML)(NL)
4.1: Closure Properties of RLs (10) • Identities: • is the identity for union: • L = L = L • is the identity for concatenation: • L = L = L
4.1: Closure Properties of RLs (11) • Other Rules for Kleene Closure • (L*)* = L* • L+ = LL* = L*L • L* = L+| • * = • * =
4.2: Elementary Questions about RLs • Membership question: • 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 • Empty, finite, or infinite question: • There exists an algorithm for determining whether a RL is empty, finite, or infinite. • DFA (path from start to final, cycle) • Equality question: • If L1 and L2 are RLs, then there exists an algorithm to determine whether or not L1 = L2. • L3 = (L1L2) (L1 L2) = iff L1 = L2
4.3: Non-regular Languages (1) • The language • L= {anbn | n 0} is not regular • There is NO DFA/NFA that accepts L • Theorem: • If L1 is RL and L2 is not-regular, then • The language L = L1L2 is not necessarily regular • Proof: • L1= a*b*is regular • L2= {anbn | n 0} is not regular • L1L2 = L2 which is not regular
4.3: Non-regular Languages (2) • The language • L= {anbm | n, m 0 and n m} is not regular. • Proof: • L is regular iff La*b* is regular. • But La*b* = {anbn | n 0}, which we know is not regular. • Then L is not regular.
4.3: Non-regular Languages (3) • 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} • …...
4.3: Non-regular Languages (4) • We want to prove that L = {akbk | k 0} is non-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: a a a a b b b q0 q1 q2 qr qr+1 …... q2r …... ar br path for accepting arbr
4.3: 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 qi= qj qf F a path in M q0
4.3: Non-regular Languages (6) • We want to prove that L = {ak2 | k 0} is non-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 a a a a q0 q1 q2 qn2 …... path for accepting an2
4.3: Non-regular Languages (7) A loop of length m qi= qj qn2 F a path in M q0 • 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)
4.3: Non-regular Languages (8) • We want to prove that L = {ap | p is a prime} is 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 a a a a q0 q1 q2 qp …... path for accepting ap
4.3: Non-regular Languages (9) qi= qj A loop of length m qp F a path in M q0 • 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
4.3: Pumping Lemma (1) • If L is a regular language, there is a DFA which recognizes L. • All finite languages are regular • , {a}, {abaabb, b, baabbbbb, bb} • So, only infinite languages are interesting for this discussion. • What are non-regular languages? • Pumping Lemma • A technique for proving a language L is NOT regular
4.3: Pumping Lemma (2) a1 a2 an-1 an q0 q1 qn-1 qn … • Any given DFA has a finite number of states • Let k = |Q| • Consider any accepted input w = a1…an, such that |w| = nk • 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
4.3: Pumping Lemma (3) … aj ai+1 a1 ai aj+1 an q0 qi=qj qn … … • Since |w| k, path must contain at least one loop • Consider any one such loop (cycle) • Break string & path into three parts: before, during, & afterloop • Given other inputs, could loop an arbitrary number of times • (a1…ai)(ai+1…aj)*(aj+1…an) also accepted
4.3: Pumping Lemma (4) • 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: • Let G be the state diagram of a DFA with k states. Any path of length k in G contains a cycle. • 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.
4.3: Pumping Lemma (5) • Let G be the state diagram of a DFA with k states and let p be a path of length k. • 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
4.3: Pumping Lemma (6) • 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: • |uv| k, • |v| 1, and • for all i 0, uviz L
4.3: Pumping Lemma (7) qi qn F w(1) w(2) w(3) w(n) q0 q1 q2 qn a path in M …... q0 • Proof: • Let w L with |w| = nk • 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
4.3: Pumping Lemma (8) qi qn F a path in M q0 • 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.
4.3: 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
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 uvizL • Contradiction • Only one string w is needed • do not need to consider all general cases • choose w that can led to contradiction easily
4.3: Pumping Lemma (11) • Example 1: • L = {ambm | m 0} • Assume L is regular, n is the # of DFA states • Let w = anbn where |w| = 2nn • w =apajan-p-jbn where p + j n and j 1 • w =u vz • u & v contain only a’s, since |uv| n • Pick i = 2: • uv2z =apajajan-p-jbn = anajbn • uv2zcontains more a’s than b’s, since |v| 1 • uv2z L • Contradiction and hence L is not regular
4.3: Pumping Lemma (12) • Example 2: • L = {w | w {a, b}*, w has equal number of a’s and b’s} • w = anbn • Same argument as previous example • Choice of w is important • Different w’s may lead to different proofs, or none being possible • Choice of i is important. • Different i’s may lead to different proofs, or none being possible
4.3: Pumping Lemma (13) • 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
4.3: 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)nL, |w| = 2nn • we do not need to consider other possible cases like ()() • we only need to ensure |w| n • Then w = (m(j(n-m-j)nwhere j 1 and m + jn uvz
4.3: Pumping Lemma (15) • By pumping lemma, (m(ji(n-m-j)n = (n+j(i-1))nL for all i • When i = 0 (n-j)n L • since j 1n-j n • Contradiction L is not regular • We can choose i = 2, i = 3, ... • but cannot choose i = 1
4.3: 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
4.3: Pumping Lemma (17) Example • Prove that L is regular, where L is the set of all strings with three consecutive 0's and = {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.
4.3: Pumping Lemma (18) Prove L 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
4.3: Pumping Lemma (19) • L = {w {a, b}*, w has equal number of a’s and b’s} • Consider L’ = La*b* = {anbn | 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
4.3: Pumping Lemma (20) Example • Prove that Lis 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 • L1 = (anb2n) , which is non-regular, and L2 = a*b*, which is regular • then L1 = LL2 • By closure properties of RLs: • if L is regular, then L1 is also regular • But since L1 not regular, so L can’t be regular
4.3: 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* = 0p1q0q1p, where p q • L3 = L⊕L4 • 0n1n = 0*1* 0n1n
Class 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?
Summary • We use the Pumping Lemma to prove a language is not regular • Note, does not work for all non-regular languages, though • Choosing a good string w is first key step • Choosing a good integer i is second key step • Must apply argument to all legal u, v, z