Closure Properties of Regular Languages Union, Intersection, Difference, Concatenation, Kleene Closure, Reversal, Homomorphism, Inverse Homomorphism 1 Review Closure Properties Recall a closure property is a statement that a certain operation on languages, when applied to languages in a class (e.g., the regular languages), produces a result that is also in that class. For regular languages, we can use any of its representations to prove a closure property. 2 Closure Under Union If L and M are regular languages, so is L M. Proof: Let L and M be the languages of regular expressions R and S, respectively. Then R+S is a regular expression whose language is L M. Illustrates use of RE representation 3 Closure Under Concatenation and Kleene Closure Same idea: RS is a regular expression whose language is LM; therefor LM is regular. R* is a regular expression whose language is L*; therefor L* is regular 4 Closure Under Intersection If L and M are regular languages, then LM is regular. Proof: Let A and B be DFA’s whose languages are L and M, respectively. Construct C = p-DFA of A and B States of C are distinct pairs [q(A),r(B)] Start state is pair [start(A), start(B)] δ([q(A),r(B)],a) = [δA(q,a), δB(r,a)] Make the accepting states of C be the pairs consisting of accepting states of both A and B. 5 Product DFA for Intersection 0 A DFA(L) 1 P-DFA(L M ) B [A,C] 0, 1 1 1 0 [A,D] 1 0 DFA(M) 1 C 0 1 0 D 1 [B,C] 0 0 [B,D] String w accepted by p-DFA iff it is accepted by both DFA(L) and DFA(M). P-DFA defines (LM ), which is regular 6 Closure Under Difference If L and M are regular languages, then so is L – M = strings in L but not M. Proof: Let A and B be DFA’s whose languages are L and M, respectively. Construct C = p-DFA of A and B. Make the accepting states of C be the pairs where A-state is accepting but B-state is not. 7 Product DFA for Difference 0 A DFA(L) 1 p-DFA(L-M) B [A,C] 0, 1 1 C 0 1 [A,D] 1 0 DFA(M) 1 1 0 0 1 [B,C] 0 0 [B,D] D DFA(L-M) is the empty language in this case. How do we know this? 8 Closure Under Complementation The complement of a language L (with respect to an alphabet Σ such that Σ* contains L) is Σ* – L. Since Σ* is regular, the complement of a regular language is regular because it is the difference of regular languaes. 9 Closure Under Reversal Given language L, LR is the set of strings whose reversal is in L. Example: L = {0, 01, 100}; LR = {0, 10, 001}. Proof: Let E be a regular expression for L. We show how to reverse E, to provide a regular expression ER for LR. 10 Reversal of a Regular Expression Basis: If E is a symbol a, ε, or ER = E. Induction: ∅, then If E=F+G, then ER = FR + GR. If E=FG, then ER = GRFR If E=F*, then ER = (FR)*. 11 Example: Reversal of a RE Let E = 01* + 10*. ER = (01* + 10*)R = (01*)R + (10*)R = (1*)R0R + (0*)R1R = (1R)*0 + (0R)*1 = 1*0 + 0*1. 12 Homomorphisms A homomorphism on an alphabet is a function that assigns a string to every symbol in that alphabet Example on S={0,1} Define h(0) = ab h(1) = ε Extend to strings by h(a1…an) = h(a1)…h(an) therefore: h(01010) = ababab h(L)={h(w)|w is in L}=homomorphism of L Language formed by applying h to every string in L 13 Closure Under Homomorphism If L is a regular language, and h is a homomorphism on its alphabet, then h(L)= {h(w)|w is in L} is also a regular language. Proof: Let E be a regular expression for L. Apply h to each symbol in E. Language of resulting RE is h(L). 14 Exercise: find the RE of h(L) Let h(0) = ab; h(1) = ε. Let L be the language of regular expression 01* + 10*. Then h(L) is the language of regular expression abε* + ε(ab)*. Note: use parentheses to enforce the proper grouping. 15 Simply RE for h(L) abε* + ε(ab)* ε* = ε, so abε* = abε. ε is the identity under concatenation. abε*+ε(ab)*=abε+ε(ab)*=ab+(ab)*. ab is contained in (ab)* RE for h(L) is (ab)* 16 Inverse Homomorphism of a string h-1(w) is read “inverse homomorphism of w w’=h-1(w), iff h(w’)=w Testing an inverse homomorphism candidate is easy. Just apply homomorphism to the candidate 17 Inverse homomorphisms of a language Let h be a homomorphism defined on S Let h(L) be a homomorphism of L defined on S Let h-1(L) denote the inverse homomorphism of L defined by h(L) h-1(L) ={w’| such that h(w’) is in L} 18 Example: Inverse Homomorphism Let h(0) = ab; h(1) = ε. Let L = {abab, baba} h-1(L) = {w defined on {0,1} such that h(w) is either abab or baba} No w such that h(w)=baba For any w with two 0’s and any number of 1’s h(w)=abab 19 Closure of RLs under Inverse Homomorphism Proof by construction Start with a DFA = A for L Construct the ih-DFA = B for h-1(L) with: The same set of states. The same start state. The same final states. Input alphabet = the symbols to which homomorphism h applies Transition function δB(q, a) = δA(q, h(a)) 20 Example of ih-DFA Construction DFA defined on {a,b} ih-DFA defined on {0,1} 1 a B a A b 0 A 0 b a B 1 b Since h(1) = ε C δB(q, a) = δA(q, h(a)) h(0) = ab h(1) = ε Since h(0) = ab C 1, 0 21 CptS 317 Fall 2014 Assignment 9, Due 11-7-14 Exercises 4.2.1 (a), (c) and (e) text p 147 22 Quotient of a language L/a L/a = set of all strings w such that wa is in L a is a symbol in the alphabet of L a is the last symbol in the test string wa Example: if L={a,aab,baa} then L/a ={e,ba} If L is regular, so is L/a Proof by construction: q-DFA(L/a) same as DFA(L) except that q is an accepting state of q-DFA iff d(q,a) is an accepting state of DFA illustrate 23 Derivative of a language: a\L a\L = set of all strings w such that aw is in L a is the first symbol in the test string aw Example: if L={a,aab,baa} then a\L={e,ab} Called “derivative” because RE of L compared to RE a\L is similar to derivative of algebraic expression Example: a\(R+S)=a\R+a\S remove a after union is the same as union of strings with a removed Concatenation example on board 24 Exercise 4.2.5e What is the implication of 0\L = nil? Exercise 4.2.5f Find a language such that 0\L = L 25 Using Closure to prove non-regular L1 is language in question L2 is language known to be non-regular O is an operation under which regular languages are closed. L2 = O L1 If L1 were regular, then L2 would be regular, but it isn’t. Therefore L1 is not regular 26 Using Closure to prove non-regular Example: 4.2.13 p149 Prove L1={0i1j|i>0,j>0,i not = j} not reg. Assume L1 is regular Then L2=0*1*-L1 is regular by closure under difference L2={0n1n|n>0} why? L2={0n1n|n>0} is not regular Therefor the assumption about L1 is false 27 28