CS154 Midterm Solutions Winter 1998/99 1. is a finite nonempty alphabet. Prove whether following sets are closed under union, intersection, and complement. (i). Set of countable subsets of *. a. UNION For any two countable subsets of *, A and B, A B is closed because the resulting set is also countable. We can prove that the resulting set is countable by observing that the largest possible set that can be generated from A B happens when A and B are disjoint. Since both A and B are countable, the union of A and B would also be enumerable and, thus, countable. We can also show countability by constructing an enumeration table or scheme. b. INTERSECTION For any two countable subsets of *, A and B, A B is closed. We observe that under intersection, A B has to be a subset of both A and B and, thus, contains less elements than A and B, or can be A or B if A contains B or B contains A. Since A and B are countable, the intersection of A and B is also countable, since we can remove extraneous elements from the enumeration without loss of generality. Thus, we can remove extraneous elements from the enumeration of either A or B and arrive at an enumeration scheme for A B. c. COMPLEMENT For any countable subset of *, A, the complement of A is closed. From lecture, we proved that * is countable. The complement of A is equivalent to * - A. Since * is countable, any subset of * is countable by making use of the same argument as above where extraneous elements can be removed from an enumeration without loss of generality. (ii). Set of countable subsets of P(*). a. UNION For any two countable subsets of P(*), A and B, A B is closed. We can prove that the resulting set is countable by observing that the largest possible set that can be generated from A B happens when A and B are disjoint. Since both A and B are countable, the union of A and B would also be enumerable and, thus, countable. We can also show countability by constructing an enumeration table or scheme. b. INTERSECTION For any two countable subsets of P(*), A and B, A B is closed. We observe that under intersection, A B has to be a subset of both A and B and, thus, contains less elements than A and B, or can be A or B if A contains B or B contains A. Since A and B are countable, the intersection of A and B is also countable, since we can remove extraneous elements from the enumeration without loss of generality. Thus, we can remove extraneous elements from the enumeration of either A or B and arrive at an enumeration scheme for A B. c. COMPLEMENT For any countable subset of P(*), A, the complement of A is not closed. From lecture, we proved that P( ) is uncountable. The complement of A is equivalent to P(*) - A. Clearly, we can pick A to be a single element set. Thus, the complement of A would contain 2* - 1 elements which is uncountable. * (ii). Set of countable and cocountable subsets of P(*). a. UNION For any two cocountable subsets of P(*), A and B, A B is closed. We can prove that the resulting set is cocountable by observing that the union of A and B produces a set that contains at least A or B. Since both A and B are cocountable, the union of A and B would also be cocountable. We can show cocountability by noting that the complements of A and B are countable. Thus, the union of A and B would produce a set whose complement would be a subset of the complements of A and B. Therefore, the union of A and B is closed, since the resulting set is cocountable. For any one countable subset of P(*), A, and any one cocountable subset of P(*), B, A B is closed. If A contains B, then A B is A and, thus, is countable. If B contains A, then A B is B and, thus, is cocountable. For any other case, we can prove that the resulting set is cocountable by making use of the same argument as above for the union for two cocountable subsets of P(*). Since B is cocountable, the complement of B is countable. The union of A and B would produce a set whose complement would be a subset of the complement of B. Therefore, the union of A and B is closed, since the resulting set is cocountable. b. INTERSECTION For any two cocountable subsets of P(*), A and B, A B is closed. We can prove that the resulting set is cocountable by making use of DeMorgan’s Law. A B is equivalent to NOT(NOT A NOT B). NOT A and NOT B are countable by the definition of cocountability. As proved above, the union of two countable subsets of P(*) is closed, so NOT A NOT B is countable. Therefore NOT(NOT A NOT B) is cocountable. For any one countable subset of P(*), A, and any one cocountable subset of P(*), B, A B is closed. If A contains B, then A B is B and, thus, is cocountable. If B contains A, then A B is A and, thus, is countable. For any other case, we can prove that the resulting set is countable by observing the fact that the intersection of A and B has to result in a set that is a subset of A. The intersection of A and B is countable, since we can remove extraneous elements from the enumeration of A without loss of generality. Therefore, the intersection of A and B is closed, since the resulting set is countable. c. COMPLEMENT For any countable subset of P(*), A, the complement of A is cocountable, since the complement of the complement of A is A and, thus, countable. Therefore, A is closed. For any cocountable subset of P(*), A, the complement of A is countable, since the complement of the complement of A is A and, thus, cocountable. Therefore, A is closed. The set of countable and cocountable subsets of P(*) is closed under complement. 2. For any language L over a one-letter alphabet {a}, show that L is regular iff L = A1 … An B where Ai has the form {aj+kd | k 0} and B is finite. a. Show given a regular language L, L = A1 … An B. If L is regular, L can be represented by a DFA. Let us call that DFA, M. M has the characteristic that there exists only one possible loop in the DFA, since only a one-letter alphabet is used. Each AI and B represents the strings accepted by a final state of M. B corresponds to all the strings that are accepted by M before the loop. For each final state in M before the loop, there is only one specific string that will be accepted. Thus, the number of strings in B corresponds to the number of final states in M before the loop. Each AI is associated with one final state in the loop in M. Ai has the form {aj+kd | k 0} where j corresponds to the number of states from the start state of M it takes to get to the final state and d corresponds to the number of states through the loop it takes to get back to the final state. Since there is only a finite number of final states, the language L = A1 … An B. b. Given L = A1 … An B, show L is regular. Given L = A1 … An B, we can show that L is regular by constructing a DFA that recognizes L. Since a one-letter alphabet is used, each AI and B represents the strings accepted by a final state of a DFA. To construct a DFA that recognizes L, first sort the strings in B by increasing length, then take each string in increasing order of length and start constructing the DFA. Add a state for each a and make that state a final state if the string is a member of B. If B = {aa, aaaaa}, then the DFA would contain five states where the second and fifth states are final states. Sort each AI by increasing length of strings. Take the AI ({aj+kd | k 0}) with the smallest initial string, add a state for each a and make that state a final state if the string is a member of that AI. Add d – 1 additional states and a transition from the last state to the final state. Do the same thing for the other AI’s except don’t add any additional states, just mark the states that should be final states. Once that is done, a DFA that recognizes L is constructed. 3. Give an example of a NFA having two states that has no equivalent DFA with two states. Prove the property. 0*0(0 1)* 4. Problem from previous midterms. Will insert solutions. 5. Use the pumping lemma to show the following languages over {0, 1, #} are not context free. a. A = {0n1n0n1n | n 0} We assume A is a CFL and obtain a contradiction. Let p be the pumping length for A that is guaranteed to exist by the pumping lemma. Select the string s = 0p1p0p1p. Clearly s is a member of A and of length at least p. The pumping lemma states that s can be pumped, but we show that it cannot. In other words, we show that no matter how we divide s into uvxyz, one of the three conditions of the lemma is violated. 1. When both v and y contain only one type of alphabet symbol, v does not contain both 0’s and 1’s and the same holds for y. In this case, the string uv2xy2z cannot contain equal numbers of 0’s and 1’s across groups, where the first group of 0’s and 1’s would not have the same number as the second group of 0’s and 1’s. Therefore it cannot be a member of A. That violates condition 1 of the lemma and is thus a contradiction. 2. When either v or y contain more than one type of symbol, uv2xy2z will not be of the form 0n1n0n1n, where the 0’s and 1’s are in the correct order and in four distinct groups. Hence it cannot be a member of A and a contradiction occurs. One of these cases must occur. Because both cases result in a contradiction, a contradiction is unavoidable. So that the assumption that A is a CFL must be false. Thus we have proved that A is not a CFL. b. B = {0n#02n#03n | n 0} We assume B is a CFL and obtain a contradiction. Let p be the pumping length for B that is guaranteed to exist by the pumping lemma. Select the string s = 0p#02p#03p. Clearly s is a member of B and of length at least p. The pumping lemma states that s can be pumped, but we show that it cannot. In other words, we show that no matter how we divide s into uvxyz, one of the three conditions of the lemma is violated. When both v and y contain only one type of alphabet symbol, v does not contain both 0’s and #’s and the same holds for y. In this case, the string uv2xy2z cannot contain equal numbers of 0’s across groups, where the constraints for length such as n, 2n, and 3n separated by # no longer hold. Therefore it cannot be a member of B. That violates condition 1 of the lemma and is thus a contradiction. 2. When either v or y contain more than one type of symbol, uv2xy2z will not be of the form 0n#02n#03n, where the 0’s and #’s are in the correct order and there are a total of two #’s. Hence it cannot be a member of B and a contradiction occurs. One of these cases must occur. Because both cases result in a contradiction, a contradiction is unavoidable. So that the assumption that B is a CFL must be false. Thus we have proved that B is not a CFL. 1. c. C = {w#x | w is a substring of x, where w, x {0, 1}*} We assume C is a CFL and obtain a contradiction. Let p be the pumping length for C that is guaranteed to exist by the pumping lemma. Select the string s = 01p#01p. Clearly s is a member of C and of length at least p. The pumping lemma states that s can be pumped, but we show that it cannot. In other words, we show that no matter how we divide s into uvxyz, one of the three conditions of the lemma is violated. 1) When both v and y contain only one type of alphabet symbol, v does not contain 0’s, 1’s, and #’s and the same holds for y. a) If v and y are part of string before #, the string uv2xy2z creates a string before # that is longer than the string after #. Since the string before # is suppose to be a substring of the string after #, it cannot be a member of C. That violates condition 1 of the lemma and is thus a contradiction. b) If v and y are part of string after #, the string uvoxyoz creates a string after # that is shorter than the string before #. Since the string before # is suppose to be a substring of the string after #, it cannot be a member of C. That violates condition 1 of the lemma and is thus a contradiction. c) If v is part of the string before # and y is part of string after #, the string uvoxyoz creates a situation where the string before # is not a substring of the string after #. Therefore it cannot be a member of C. That violates condition 1 of the lemma and is thus a contradiction 2) When either v or y contain more than one type of symbol, uv2xy2z will not be of the form 01p#01p, where the 0’s, 1’s, and #’s are in the correct order and there are a total of one #’s. Hence it cannot be a member of C and a contradiction occurs. One of these cases must occur. Because both cases result in a contradiction, a contradiction is unavoidable. So that the assumption that C is a CFL must be false. Thus we have proved that C is not a CFL. d. D = {x1#x2#…#xk | k 2, xi {0, 1}*, xi = xj for some i j} We assume D is a CFL and obtain a contradiction. Let p be the pumping length for D that is guaranteed to exist by the pumping lemma. Select the string s = 01p#01p. Clearly s is a member of D and of length at least p. The pumping lemma states that s can be pumped, but we show that it cannot. In other words, we show that no matter how we divide s into uvxyz, one of the three conditions of the lemma is violated. 1) When both v and y contain only one type of alphabet symbol, v does not contain 0’s, 1’s, and #’s and the same holds for y. a) If v and y are part of string before #, the string uv2xy2z creates a string before # that is different from the string after #. Therefore it cannot be a member of D. That violates condition 1 of the lemma and is thus a contradiction. b) If v and y are part of string after #, the string uv2xy2z creates a string after # that is different from the string before #. Therefore it cannot be a member of D. That violates condition 1 of the lemma and is thus a contradiction. c) If v is part of the string before # and y is part of string after #, the string uvoxyoz has a string before # that is different from the string after #. Therefore it cannot be a member of D. That violates condition 1 of the lemma and is thus a contradiction 2) When either v or y contain more than one type of symbol, uv0xy0z will not be of the form 01p#01p, where the 0’s, 1’s, and #’s are in the correct order and the string before # is equivalent to the string after #. Hence it cannot be a member of D and a contradiction occurs. One of these cases must occur. Because both cases result in a contradiction, a contradiction is unavoidable. So that the assumption that D is a CFL must be false. Thus we have proved that D is not a CFL. 6. G is the following CFG: S aSb | bY | Ya Y bY | aY | Give a description of L(G) in English. Use that description to give a CFG for the complement of L(G). a. L(G) in English L(G) contains all strings such that each string begins with n a’s and ends with n b’s, where n can be zero, and has either a b followed by any number of a’s and b’s or any number of a’s and b’s followed by an a in the middle. b. Complement of L(G) L(G) = anb(a b)*bn an(a b)*abn 7. Problem from previous midterms. Will insert solutions. 8. Problem from previous midterms. Will insert solutions. 9. DPDA vs. NPDA (i). Prove that the complement of {anbncn | n 0} is context free. anbncn = anbncm ambncn, where n m. NOT anbncn = NOT anbncm NOT ambncn NOT anbncn = aibjck, where i j and i k. (ii). Prove NPDA is more powerful than DPDA. NPDA is more powerful than DPDA. Since DPDA is closed under complement, DPDA would not be able to prove part (i) since anbncn is non-context-free. However, as seen above, NPDA can prove part (i). Thus, NPDA is more powerful. 10. The powers of 2 written in binary form a regular language. Show that the powers of 2 written in decimal are not regular. We assume that only the nonnegative powers of two are under consideration for this problem. When the powers of two are written in binary, it is easy to see that the language is regular since the language is described by the regular expression, 10*. To show that the powers of two written in decimal are not regular, we will need to make use of the pumping lemma. However, the way in which we will apply the pumping lemma would be very different from the normal applications. In short, we will try to exploit some numerical properties of the powers of two to give us the framework from which we can use the pumping lemma to show that the powers of two in decimal is not regular. The property that we are taking advantage of is the repeatable pattern of remainders generated when we take the powers of two modulo some power of ten. Thus, the powers of two will be converted to a repeated series of numbers. If we set the modulus to be 10p, the powers of two will start to repeat sometime after the numbers reach above lengths of p. If we take any power of two with length greater than p, we can pump up or down some portion of the number beyond the pth decimal place and get a resulting number of length m. Since the powers of two is sparse for each string length, we can show that the resulting string of length m does not correspond to the powers of two modulo 10p for strings of length m. Thus, the powers of two in decimal is not regular.