COMP 330 Autumn 2014 Assignment 2 Solutions Prakash Panangaden
Transcription
COMP 330 Autumn 2014 Assignment 2 Solutions Prakash Panangaden
COMP 330 Autumn 2014 Assignment 2 Solutions Prakash Panangaden October 8, 2014 I am not giving solutions to the alternate questions. You can come and see me if you would like to see the solutions. Question 1[20 points] Give regular expressions for the following languages over {a, b}: 1. {w|w begins and ends with an a}. Solution: a + a(a + b)∗ a. 2. {w| every odd position is a b}. Solution: (b(a+b))∗ (b+ε). Note that “every odd position is a b” does not imply that every even position is an a. 3. {w|w contains at least two as and at most one b}. Solution: aaa∗ + aaa∗ b + aa∗ baa∗ + baaa∗ . 4. All strings except the empty string. Solution: a(a + b)∗ + b(a + b)∗ . Question 2[20 points] Suppose that you have a DFA M = (S, Σ, s0 , δ, {sf }) with s0 6= sf . Suppose further that for all a ∈ Σ δ(s0 , a) = δ(sf , a). Show that for any nonempty word w over Σ we have δ ∗ (s0 , w) = δ ∗ (sf , w). Solution : We do this by induction on the length of the word w. This time the base case is when |w| = 1. Let a be any letter in Σ, i.e. a one-letter word. Then we have δ ∗ (s0 , a) = δ(s0 , a) = δ(sf , a) = δ ∗ (sf , a). 1 This verifies the base case. Now suppose that we have a word w = xa and the claim is assumed to be true for x. Then we have δ ∗ (s0 , xa) = δ(δ ∗ (s0 , x), a) = δ(δ ∗ (sf , x), a) = δ ∗ (sf , xa). This completes the inductive step and the proof is complete. Question 3[20 points] Show that the following languages are not regular by using the pumping lemma. 1. {an bm an+m |n, m ≥ 0}, 2. {x|x = xR , x ∈ Σ∗ }, where xR means x reversed; these strings are called palindromes. An example is abba a non-example is baba. Solution 1. The demon picks a number p. I pick the string ap ba(p+1) . Whatever the demon tries to do he is forced to pick y to consist of a nonempty string that consists exclusively of as, because |xy| ≤ p and |y| > 0, say that y = al . Now I choose i = 3. The new string is a(p+2l) ba(p+1) which is not in the language since 2l 6= 1. My strategy is sure to work because I have taken into account anything that the demon might do. 2. Suppose the demon picks the number p. I choose the string ap bbap which is a palindrome. Now the demon tries to divide it up into x, y, z with |xy| ≤ p, |y| > 0, in such a way that xy i z a palindrome for all i ≥ 0. It must be the case that x = ak for some k ∈ {0, ..., p − 1}, y = aj for some j ∈ {1, ..., p} and z = al bbap for some l ∈ {0, ..., p − 1}. Now I choose i = 2 and we see that xy 2 z = ap+j bbap , which is not a palindrome, since j > 0. Therefore, this language is not regular. Question 4[20 points] Show that the language is not regular. Show, however, that it satisfies the statement of the pumping lemma as I proved it in class, i.e. there is a p such that all three conditions for the pumping lemma are met. Explain why this does not contradict the pumping lemma. Solution: First, I will show that F is not regular. If F were regular, than the intersection of F and any regular language would also be regular. We know that L = {abi cj |i, j ≥ 0} is a regular language, and clearly L ∩ F = {abn cn }, since any word in L must begin with exactly one a, and so if it is also in F then i = j. But we know that {abn cn } is not regular by an easy variation of the example done in class, so F must not be regular. 2 Next I will show that F satisfies the pumping lemma. Let p = 2. Then the demon gives you a word w from F , of the form ai bj ck . First, consider the case where i = 0, so w = bx cy . Then choose x = and y = b if the first letter is a b, or y = c otherwise. Let z be the rest of the word. If y = b, then z = bx−1 cy , and pumping results in a string of the form bn bx−1 cy , which is clearly in F for any n. If y = c, then z = cy−1 . So pumping results in a string of the form cn cy−1 which is also clearly in F . Next consider the case where i = 1, so w = abj cj . Again let x = , y = a, and z = bj cj . Then pumping results in a string of the form an bj cj , which is clearly in F . Next consider the case where i = 2, so w = aabj ck . Now we can’t choose x = and y = a, because if we do, then xy 0 z = abj ck is not necessarily in our language. So choose x = aa. Then pumping cannot result in a string of the form abj ck , because we either pump down and have no a’s, or we pump up and have more than one a. So pumping results in strings which are still in our language. Finally, consider the case where i > 2. Again choose x = and y = a. If we pump down, we remove only one a, so our string still has at least two a’s, and j and k don’t have to match. If we pump up, the string is clearly still in our language. This proves that the language satisfies the pumping lemma. This does not contradict the pumping lemma because the pumping lemma claims that all regular languages are “pumpable,” which is not the same as claiming that all languages that are “pumpable” must be regular. Question 5[20 points] Let D be the language of words w such that w has an even number of a’s and an odd number of b’s and does not contain the substring ab. 1. Give a DFA with only five states that recognizes D. Solution Once you see an a you can never have a b later on. If your first letter is an a it has to be rejected because there is now no way to get a b and the word has to have at least one b. The picture is on the next page. 3 start q0 b a q1 q2 b a q3 b a a q4 b a, b 2. Give a regular expression for this language. Solution b(bb)∗ (aa)∗ . 4