CS 454 Theory of Computation Fall 2011 Section 1 (closed-book) Total points 30 1. Which of the following are true? (a) a PDA can always be converted to an equivalent PDA that at each step pops or pushes a single symbol (but not both). (b) a PDA can be converted to an equivalent PDA that always empties its stack before entering an accepting state. (c) a PDA can always be converted to an equivalent PDA that has exactly one accepting state. Choose the correct answer: (1) (a) and (c) (2) (a) and (b) (3) all of them (4) (a) only (5) none of them. 2. Under which operations are context free languages closed? (a) union (b) complement (c) intersection Choose the correct answer: (1) (a) and (c) (2) (a) and (b) (3) all of them (4) (a) only (5) none of them. 3. Let L be a regular language. Which of the following are true? (a) L is context free. (b) there is a regular expression for L. (c) the complement of L is context free. Circle the correct answer: A. (a) and (b) only B. (a) and (c) only C. (b) and (c) only D. All of them are true. E. None of them are true. 4. Complete the following sentences using phrases (1) (4) given below: (a) A sentential form is a string consisting of any sequence of 2 (b) A leftmost derivation involves replacing 4 in a sentential form. (c) A derivation tree has 1 at the root node. (d) there are at least two 3 for some string generated by an ambiguous grammar. (1) start symbol (2) terminals and nonterminals (3) leftmost derivations (4) leftmost nonterminal 5. Let A and B be two languages over {0,1} such that A is a subset of B. Consider the following statements:
(1) If B is finite, then A is finite. (2) If B is regular, then A is regular. (3) If the complement of B is context free, then the complement of A is context free. Which of the above statements is true? Circle the correct option. (A) 1 only (B) 1 and 2 only (C) 1 and 3 only (D) 2 and 3 only (E) all of them are true. 6. Under which of the operations are the regular languages closed? (1) union (2) intersection (3) concatenation (4) complement Circle the correct choice. (A) 1 only (B) 2 and 3 only (C) 1 and 3 only (D) 1 and 2 only (E) all of them are true. 7. Let P be the PDA <Q, Σ, Γ, δ, q0, Z0, F>. Consider the ID (configuration) <q, x, γ> where q is the current state, x is the part of the input not yet read, and γ is the string stored in the stack. Write the condition for the ID to be an accepting ID. <q, x, γ> where q is in F, x = ε 8. Define Chomsky Normal Form. (State the allowed formats of the rules in Chomsky Normal Form). It is a CFG in which all the rules are of the form A BC (where B, C are non-terminals) or A a (a is a terminal). In addition, S e is an allowed rule. (However S e is not allowed for any A!= S.) 9. The significance of a universal Turing machine is that: (a) it gave a precise mathematical definition of an algorithm. (b) it showed a Turing machine to be more powerful than a PDA. (c) it provided a basis for creating a general purpose computer. (d) it was used to show that 1 tape Turing machines are equivalent to 2 tape Turing machines. 10. State the pumping lemma for context free languages. For every CFL L, there is a constant p (that depends only on L) such that for any string s in L of length at least p, there exist strings u, v, w, x and y such that s = uvwxy, vx > 0 (i.e., at least one of v or x is non-null), vwx <= p and for every j, uv j wx j y is L.
CS 454 Theory of Computation Fall 2011 Section 2 (open-book) Total points 70 Answer any FIVE questions. Each question will be weighted 14 points. Provide an informal explanation of answers before a formal construction. 1. Write a context free grammar G for the complement of the language L over {a,b}: L = { a n b n n > 0 } (First describe the complement of L.) Exhibit a leftmost derivation of the string aaabb for G. Solution: L = complement of L = L1 U L2 U L3 U L4 where L1 = { a n b m n > m >= 0 }, L2 = { a n b m 0 <= n < m }, L3 = { w w is not of the form a*b*} and L4 = {ε} CFG for L1 is A aa B, B abb ε CFG for L2 is C Cb B, B abb ε CFG for L3 is D EbaE, E ae be ε CFG for L4 is F ε Finally, CFG for L is the set of all productions above combined with S A C D F Leftmost derivation for aaabb: S => A =>aa =>ab=>aabb=>aaabbb=>aaabb 2. Describe a PDA for the language L = { w w is in {0,1}*, length of w is odd and the middle symbol in w is 1}. Exhibit the sequence of configurations leading to acceptance of the string 10100 in M. Informal solution: as with other PDA s, first add $ to the stack. Then, in state q1, push an A for each 0 and 1 seen until the middle symbol is reached. Then, if a 1 is encountered, then
move to state q2. Now we have to check that the transition made was really on the middle symbol. This is done by counting the number of remaining input symbols and checking that it is equal to the number of symbols on the stack. If successful verification is made, then when all the input symbols have been read, the stack must have only $ symbol left. So at this point, move to the accepting state q3. To accept string 10100: (e stands for null string.) (q0, 10100, e) (q1, 10100, $) (q1, 0100, A$) (q1, 100, AA$) (q2, 00, AA$) (q2, 1, A$) (q2, e, $) (q3, e, e). 3. Prove using pumping lemma that the language L = { a n b m n, m > 0 and m = n 2 } is not context free. Suppose L is context-free. Let p be the pumping constant for L. I choose a string s of length at least p in L, namely s = a p b p^2. Consider an arbitrary partition of s into u, v, w, x and y such that s = uvwxy with the condition that vx > 0 and vwx < p. If either v or x is non-uniform, then pumping twice we give a string that is not of the form a*b*. So, we assume that both v and x are uniform. If both v and x consisted only a s, then pumping twice we increase the number of a s without changing the number of b s so clearly the resulting string is not in L. Similar argument also holds if both v and x consisted of b s. (namely pumping twice, we will increase the number of b s, but the number of a s remains the same.) Also if v or x is null, then pumping twice, the number of a s or b s increases while the other remains unchanged so the resulting string is not in L. So the only case that we have considered is the one in which v consists of a s, and x consists of b s. namely, v = a^t, x = b^s for some t > 0 and s > 0. Note that t + s < p. Pumping twice, we get the string s = a^(p+t) b^(p^2+s). Since t + s < p, it follows that s < p as well. For the resulting string s to be in L, p^2 + s must be equal to (p+t)^2. However, (p+t)^2 >= (p+1)^2, while p^2 + s < p^2 + p < (p+1)^2. This finally shows that L is not contextfree. 4. Shown below is a PDA M. Use the algorithm described in the text (Lemma 2.27) to convert M into an equivalent Context free grammar.
1 Solution: (in this solution e stands for non symbol ε.) For each state pair of states, we have a nonterminal such as [AA], [AB] etc. Rules: (1) [AA] e, [BB] e, [CC] e, [DD] e. (2) [PR] [PQ][QR] for all P, Q, R in {A, B, C, D}. (There are 64 such rules). (3) choose a push move, and a corresponding pop move. There are 8 such pairs. Each one generates a rule: PAIR 1: (e, e $) from A to B and (e, $ e) from B to D This adds the rule [AD] [BB] PAIR 2: (e, e $) from A to B and (e, $ e) from C to D This adds the rule [AD] [BC] PAIR 3: (e, e $) from A to C and (e, $ e) from B to D This adds the rule [AD] [CB] PAIR 4: (e, e $) from A to C and (e, $ e) from C to D This adds the rule [AD] [CC] PAIR 5: (a, e 1) from B to B and (b, 1 e) from B to B This adds the rule [BB] a[bb]b PAIR 6: (a, e 1) from B to B and (a, 1 e) from C to C This adds the rule [BC] a[bc]a PAIR 7: (b, e 1) from C to C and (b, 1 e) from B to B This adds the rule [CB] b[cb]b PAIR 8: (b, e 1) from C to C and (a, 1 e) from C to C This adds the rule [CC] b[cc]a 5. Design a single tape Turing machine for the language L over {a, b, c} defined as follows: L = { a n b m c n+m n, m >= 0 }. First describe your solution informally identifying the role of each state. Also state clearly the tape alphabet used and the role of additional tape symbols used, if any. Then draw the transition diagram. Informal solution 1: The obvious solution will be to first make a sweep making sure that the input is of the form a*b*c* (if not reject), then start replace an a or b with a
1, then move right until it finds a c replace it with 2, sweep back until it finds a 1, move right and repeat the process. While sweeping right looking for a 2, if no c is found, the input is rejected. During the last pass (this happens when it moves right on seeing a 1, looking for an a or b, it finds a 2), if any 2 is encountered the input is rejected. Else the input is accepted. The above solution has complexity O(n 2 ). Solution 2: This one has time complexity O(n log n). Do the same as in the first pass, checking that the input is in the form a*b*c*. Then, mark off every other a and b, and remember the parity of the number of a s + number of b s. Do the same for c s. If the parities don t match, then reject. Else repeat. Since half of the input string gets replaced by 1 s and 2 s each time, the number of passes needed is O(log n) and since each pass involves O(n) moves, the total number of moves is O(n log n). 6. (a) Convert the following CFG G1 into Chomsky Normal Form: S A1B A 0A ε B 0B 1B ε Remove A ε by adding S 1B, A 0. Similarly Remove B ε and add S A1, B 0, B 1. Finally, replace S A1B by S AP, P QB, Q 1. Thus the final grammar is: S AP, P QB, Q 1, S AQ, B 0, B 1 S QB, A 0 A RA, R 0 B RB QB (b) Exhibit three strings that can t be generated using the grammar G1 and three strings that can be generated using G1. Strings that can be generated: 1, 011, 100 Strings not generated: 0, 00, 0000 etc. 7. (a) Shown below is a CFG G over the alphabet {+, *, a, b} : S +SS *SS a b Use the algorithm presented in class for converting CFG to PDA to construct a PDA equivalent to G.
(b) Which of the following strings can be generated using G? (1) ++++aba (2) +*aab+*a (3) **+ab*baa Only (3) can be generated. (c) Exhibit a derivation sequence for the string *+aaa in the PDA you constructed in (a). <q0, *+aaa, e) => <q1, *+aaa, S$> => < q1, *+aaa, *SS$> => <q1, +aaa, SS$> <q1, +aaa, +SSS$> => <q1, aaa, SSS$> => <q1, aaa, ass$> <q1, aa, SS$> => <q1, aa, as$> => <q1, a, S$> => <q1, a, a$> => <q1, e, $> <q2, e, e>