Hrvrd University Computer Science 121 Midterm Octoer 23, 2012 This is closed-ook exmintion. You my use ny result from lecture, Sipser, prolem sets, or section, s long s you quote it clerly. The lphet is Σ = {, } except where otherwise stted. You hve 80 minutes. The prolems totl 75 points. Use pen nd write your nme on ll lueooks you use. Good luck! PROBLEM 1 (1+1+1+1+1 points) For ech of the following, sy whether it is string, lnguge, or neither. No justifiction necessry. (A) Σ (B) ε (C) (D) { } (E) {ε} (A) Lnguge (B) String (C) Lnguge (D) Neither (E) Lnguge Consider the following NFA N: PROBLEM 2 (3+4+3+5 points), q 1 q 2 q 0 q 3 q 4, (A) Which of the following strings re ccepted y N?,,. (B) Write out the forml 5-tuple for N. (C) Descrie in English the lnguge L(N). (D) Convert the NFA to DFA using the Suset Construction. (You my simply drw the stte digrm of the DFA, omitting unrechle sttes.) (A) Yes, Yes, No. (B) ({q 0, q 1, q 2, q 3, q 4 }, {, }, δ, q 0, {q 2, q 4 }), with δ given y ε q 0 {q 1 } {q 3 } q 1 {q 1, q 2 } {q 1 } q 2 q 3 {q 3 } {q 3, q 4 } q 4 (C) All strings over Σ = {, } of length t lest two where the first nd lst chrcters re the sme.
(D) {q 1 } {q 1, q 2 } {q 0 } {q 3 } {q 3, q 4 } PROBLEM 3 (5 points) For string w {, }, define w to e the string where ll s re replced with s nd vice-vers. For exmple, =. Give context-free grmmr for the lnguge {w {, } : w R = w}. Solution: Denote w s w = σ 1 σ 2 σ n, where σ i Σ = {, }. Then w R = w if nd only if σ i = σ n i+1 for every i. We cn design CFG s follows. G = ({S}, {, }, {S S, S S, S ɛ}, S). The min ide is tht, in ech step, if G genertes σ in the first position, then it must generte σ in the lst position. PROBLEM 4 (4+4+4+4 points) TRUE or FALSE? Justify your nswers in one or two sentences. (A) If L 1 is regulr nd L 2 L 1, then L 2 is regulr (B) L((( ) ) ) (C) There exists lnguge L such tht L is uncountle (D) The grmmr S SS is miguous (A) flse: Let L 1 = Σ nd L 2 = { n n : n N}. We proved in clss tht L 2 is non-regulr, nd L 2 L 1, so the nswer must e flse. (B) flse: We see tht ecuse the regulr expression is of the form ((X) ), where X =, we know tht ny string in L is either ε or it ends in. (C) flse: Every lnguge is countle (shown in clss) nd L is still lnguge.
(D) true: The string hs two different prse trees: S S / \ / \ S S S S / \ / \ S S S S PROBLEM 5 (6+6+6+6 points) For ech of the following lnguges, sy whether it is regulr, context-free, oth, or neither. Briefly justify your nswers. (A) L = { i j : i j = 2012} (B) L = { i j : i + j = 2012} (C) L = { n n n : n 0} (D) L = {w : w contins oth om nd romney s sustrings}, over lphet Σ = {,,..., z} (A) context-free, not regulr. This is very similr to { n n : n 0}. In fct, it is { n+2012 n : n 0}. We cn use this to show tht it is nonregulr: Consider L 1 = { 2012 } nd L 2 = { n n : n < 2012}. Both L 1 nd L 2 re finite nd hence regulr. But L L 1 L 2 = { n n : n 0}, which is known non-regulr lnguge. Becuse the regulr lnguges re closed under conctention nd union, then it must e the cse tht L is not regulr. L is, however, context free. We cn devise grmmr S 2012 T, T T, T ε to produce L. Becuse L cn e generted y context-free grmmr, L is context-free. (B) oth. All strings in this lnguge re 2012 chrcters long. Becuse the lnguge hs lrgest string, tht mens tht the lnguge is finite nd hence regulr. And if it is regulr, then it is lso context-free. (C) neither. It suffices to show tht L is not context-free, s this implies tht it is lso nonregulr. We cn prove tht L is not context-free y n ppliction of the CF Pumping Lemm, similr to the proof tht { n n c n } is not context-free from lecture. Let p e the CF pumping length for L, nd consider the string s = p p p. By the CF pumping lemm, s cn e prtitioned into s = uvxyz where v or y is nonempty nd uv i xy i z L for ll i N. If either v or y contin two different symols, then uv 2 xy 2 z is not of the form, nd hence is not in L. If v nd y ech contin only single symol, then uv 2 xy 2 z will not hve n equl numer of symols in ll three segments (the initil s, the s, nd the finl s), s we will e pumping only one or two of the three segments. This is contrdiction, so L cnnot e CF. (D) oth. L is regulr ecuse it is the intersection of the two regulr lnguges L(Σ omσ ) nd L(Σ romneyσ ), nd the clss of regulr lnguges is closed under intersection. L is context free ecuse every regulr lnguge is context free. PROBLEM 6 (10 points) For string w {, }, define odd(w) to e the string consisting of the symols in odd-numered positions in w. Tht is, if w = w 1 w 2 w 3 w n, then odd(w) = w 1 w 3 w 5 w m, where m is either n or n 1 depending on whether n is odd or even.
Show tht if L is regulr, then so is {w {, } : odd(w) L}. Solution: Suppose L is regulr; then there is DFA M recognizing L. We construct new DFA M s follows: For every old trnsition δ(q 1, σ) = q 2, we crete new stte q, delete the old trnsition, nd crete the trnsitions δ(q 1, σ) = q nd δ(q, σ ) = q 2 for every σ Σ. For exmple: q1 q2 q1 q', q2 Also, if q 2 is n ccept stte, we mke q n ccept stte. This mkes sure tht we ccept oth odd nd even length strings. Now we justify tht this DFA M recognizes L = {w : odd(w) L}. First, if odd(w) L, then there ws n ccepting pth in M on every odd letter in w, so M must hve n ccepting pth on w (since the odd letters go through sttes corresponding to M, nd the even letters cn e nything). Second, if w is ccepted y M, then the odd letters of w must hve followed trnsitions on the sttes corresponding to M, so odd(w) would e ccepted y M, so odd(w) L nd thus w L. Since M is DFA recognizing L, L must e regulr. (Note: mny students ttempted to show tht {odd(w) : w L} is regulr. Unfortuntely, while this is still true, it is trickier to do.) PROBLEM 7 (CHALLENGE! 1 points) Prove tht if R is regulr expression, then L(R) stisfies the pumping lemm with pumping length equl to R +1, where R is the size (or length) of R. (You cn omit the xy p condition.) Solution: Intuitively, if regulr expression R mtches string w with w > R, then Kleene str must hve een used to do the mtching. We cn simply let y e the (nonempty) prt of w tht mtches the Kleene str. Then y n still mtches the Kleene str. Formlly, we prove this y structurl induction on R. Bse cse: If R =, R = ε, or R = σ for some σ Σ, then, since L(R) contins no strings of length t lest R + 1, the result is vcuously true. Induction step: Let R e regulr expression with R > 1. Suppose the result holds for regulr expressions smller thn R. By the definition of regulr expressions, we cn write R in one of the following wys. In ech cse, R 1 nd R 2 re regulr expressions tht re smller thn R nd, using the induction hypothesis, we cn show tht the result holds for R. R = R 1 R 2 : Let w L(R) with w > R. Then either w L(R 1 ) nd w > R 1 or w L(R 2 ) nd w > R 2. In the first cse, we cn write w = xyz with y ε nd xy n z L(R 1 ) L(R) for ll n 0. The other cse is similr. Either wy, the result holds for R.
R = R 1 R 2 : Let w L(R) with w > R. Then we cn write w = w 1 w 2 with w 1 L(R 1 ) nd w 2 L(R 2 ). Either w 1 > R 1 or w 2 > R 2. In the first cse, let w 1 = xyz with y ε nd xy n z L(R 1 ) for ll n 0; then w = xy(zw 2 ) nd xy n zw 2 L(R 1 ) L(R 2 ) = L(R) for ll n 0. The second cse is similr nd, either wy, the result holds for R. R = R1 : If w L(R) nd w ε, then we cn write w = w 1w 2 w k where w i L(R 1 ) {ε} for ll i. Set x = ε, y = w 1, nd z = w 2 w k. Since y L(R 1 ) {ε}, y n L(R 1 ) for ll n 0. Thus xy n z = w1 nw 2 w k L(R1 ) = L(R) nd, hence, the result holds for R. THE END REMEMBER TO PUT YOUR NAME ON YOUR WORK.