CmSc 365 Theory of Computation Finite Representations of Languages Regular expressions and regular languages Problem Solutions: 1. Exercise 1.8.1, p.51. What language is represented by the regular expression ((( a* a ) b ) U b) L( ((( a* a ) b ) U b)) = L ((( a* a ) b ) ) L (b ) = = L (( a* a ) ) L (b ) L (b) = = L ( a* ) L ( a ) L (b ) L (b ) = = L ( a )* L ( a ) L (b ) L (b ) = {a}*{a}{b} {b} = ( {a}*{a} e) {b} = = {a}* {b} = {a*b} = = { w є {a,b}*: all strings contain only one b, and end with ab} Note that {a}*{a} e = {a}* {a}*{a} contains strings with one or more a's, without the empty string. {a}* contains the empty string and strings with one or more a's, i.e. {a}*{a} e = {a}* 2. Exercise 1.8.2 p. 51: Rewrite each of the regular expressions as a simpler expression, representing the same language: a. Ø* U a* U b* U (a U b)* Consider Ø* U a* Ø* gives the empty word, a* would also give the empty word ( a* represents all strings containing zero, one or more as), so we can write: Ø* U a* = a*, resulting at a simplified expression: a* U b* U (a U b)* (a U b)* represents the set ({a} {b})*, and since {a} {b} = {a,b}, (a U b) * would represent {a,b}* - the set of all possible strings containing a and b a* U b* gives all strings containing zero, one or more as, and all strings containing zero one or more bs. These strings are contained in {a,b}*. So we can write: 1 a* U b* U (a U b)* = (a U b)* hence Ø* U a* U b* U (a U b)* = (a U b)* b. ((a*b*)*(b*a*)*)* Let us consider first (a*b*) This expression would generate the following strings: e a aa aaa aaaa …. b ab aab aaab aaaab ….. bb abb aabb aaabb aaaabb ….. bbb abbb aabbb aaabbb aaaabbb …… …… (a*b*)* would give all possible concatenations of the above strings, which is the set {a,b}*, represented by the regular expression (a U b) * Let us consider now (b*a*) This expression would generate the following strings e b bb bbb bbbb …. a ba bba bbba bbbba ….. aa baa bbaa bbbaa bbbbaa ….. aaa baaa bbaaa bbbaaa bbbbaaa …… …… (b*a*)* would give all possible concatenations of the above strings, which is the set {a,b}*, represented by the regular expression (a U b) * Hence ((a*b*)* (b*a*)*)* = ((aUb) * (aUb) *)* Now, (aUb) * represents all possible strings over the alphabet {a,b}, the concatenation of all possible strings with themselves would result in the same set, hence (aUb) * (aUb) * = (aUb) * Thus we obtain: 2 ((a*b*)* (b*a*)*)* = ((aUb) * (aUb) *)* = ((aUb) *)* Again, since all possible strings are in (aUb) *, ((aUb) *)* is the same as (aUb) *. So finally we get: ((a*b*)* (b*a*)*)* = (a U b) * c. (a*b)* U (b*a)* a*b gives all strings containing one b at the end. (a*b)* would give all possible strings over {a,b} , i.e. {a,b}* Same holds for (b*a)* - it would also give all possible strings over {a,b} So we have: {a,b}* {a,b}*, which is {a,b}*, represented by (a U b)* Thus (a*b)* U (b*a)* = (a U b)* d. (a U b)* a (a U b)* This expression cannot be simplified. It represents the set {a,b}*a{a,b}*, the strings are: All possible strings over {a,b} to the left, followed by an a, followed by all possible strings over {a,b}to the right. 3. Exercise 1.8.3. Let ∑ = {a,b}. Write the regular expressions for the following sets: a. All strings in ∑* with no more than three a's We need: a. strings without a's. These strings are b* b. strings containing one a, possible preceded and followed by some b's These strings are b*ab* c. strings of the above type, concatenated once - they will have exactly two a's These strings are b*ab* b*ab* = b*ab*ab* (b*b* = b*) d. Strings of type (b) concatenated with strings of type (c) - they will have exactly three a's 3 These strings are b*ab* b*ab*ab* = b*ab* ab*ab* Thus the regular expression would be: b* U b*ab* U b*ab*ab* U b*ab*ab*ab* b. All strings in ∑* with number of a's divisible by three. The strings would be concatenations of strings that contain exactly zero or three a's, preceded, separated and followed by any number of b's Such a string is represented by: b*ab*ab*ab* U b* To get the concatenations, we write: b*(ab*ab*ab*)* c. All strings in ∑* with exactly one occurrence of the substring aaa The answer is: ((a U aa U b*)b) *aaa(b(a U aa U b*))* Let's see how we arrive at this expression. 1. The strings need to have the form: α aaa β, where α and β are possible empty, or containing zero, one or two consecutive a's. with b's preceding, ending or in between. If not empty, α should end with b, and β should start with b. α = (α1b)*, β = (b β1)* So our next representation is (α1b)*aaa(b β1)* 2. α1 and β1 should be one a only, or two a's, or any number of b's, i.e. α1 = a U aa U b* β1 = a U aa U b* Thus we obtain: ((a U aa U b*)b)*aaa(b(a U aa U b*))* Each string in (a U aa U b*)b ends in b, and each string in 4 (b(a U aa U b*))* starts with b, so in the concatenations we will not get a's from different strings put together - there will always be a separating b. 4. Exercise 1.8.5, p. 52 Which of the following are true? Explain. a. baa є a*b*a*b* True. baa can be obtained from a0ba2b0 b. b*a* a*b* = a* b* True b*a* contains : the empty string, strings consisting of one or more b's, strings consisting of one or more a's, and strings with any number of b's to the left and any number of a's to the right. a*b* contains: the empty string, strings consisting of one or more a's, strings consisting of one or more b's, and strings with any number of a's to the left and any number of b's to the right. As we can see b*a* a*b* will contain: the empty string, strings consisting of one or more a's, strings consisting of one or more b's, On the other hand, a* b* contains the empty string, strings consisting of one or more a's, strings consisting of one or more b's, Hence b*a* a*b* = a* b* 5 c. a*b* b*c* = Ø False. a*b* contains: the empty string, strings consisting of one or more a's, strings consisting of one or more b's, and strings with any number of a's to the left and any number of b's to the right. b*c* contains: the empty string, strings consisting of one or more b's, strings consisting of one or more c's, and strings with any number of b's to the left and any number of c's to the right. As we can see, a*b* b*c* will contain the empty string, strings consisting of one or more b's, d. abcd є (a(cd)*b)* False. We shall consider first the string a(cd) *b, and then its Kleene star 1. a(cd)*b gives the strings: ab, acdb acdcdb …. acd…cdb. Only in one string we have b after a, but there we don't have c and d. In all other strings a is followed by c. Thus abcd cannot be obtained from a(cd) *b . 2. (a(cd) *b) * gives strings starting with a and ending with b, so b could never be followed by c in the concatenations 6