Document 6528705
Transcription
Document 6528705
CS2MJ3 – Fall 2012. Sample solutions to the assignment 1. Most questions have more than one solution. Total of this assignment is 153pts. 100% of this assignment is 125pts. There are 28 bonus pts. Each assignment is worth 7%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. 1.[5] Let M = (Q, Σ, δ, s0, F) be a deterministic finite automaton. Give a deterministic finite state automaton M’ such that L(M’) = L(M) - {ε}. Solution: Let M=(Q,Σ,δ,s0,F) be a DFA such that L=L(M). If ε∉L(M), then M’=M. If ε∈L(M) then s0∈F. Define M’= (Q∪{q0},Σ,δ1,q0,F), where q0∉Q, and δ1 is defined as follows: δ1(q0,a) = δ(s0,a) for each a∈Σ, δ1(s,a) = δ(s,a) for each s∈Q. Clearly L(M’)=L(M)-{ε}. 2.[5] Let L = {a,b} and L1* = L*. Prove that L ⊆ L1. Solution: First we prove that a ∈ L1. Suppose that a ∉ L1. This means that a ∉ L1*, a contradiction since a ∈ L ⊆ L* = L1*. Identically we prove b ∈ L1. Thus L ⊆ L1. An immediate extension of this result (identical proof!) is: for every alphabet Σ and any language L, if L* = Σ*, then Σ ⊆ L. 3.[10] Consider the automaton below. Construct an equivalent regular expression using the idea of generalised nondeterministic automata. Provide all steps. b a C b a b a b a 1 2 3 4 4.[15] Consider the following regular expression: a*b*∪(a*bb∪ab*)a*b. a. Using a standard construction construct an equivalent non-deterministic finite state automaton. b. Transform the automaton from 4a into an equivalent deterministic finite state Solution: a.[5] Step 1 [5]. We construct a nondeterministic automaton with - moves: Q={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22}, initial_state=1, F={22}, ={a,b}, and is defined below 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 a {2,8} {4} {2} {22} {4} {9} {6} {6,11} b {3} {5} {7} {10} {13} {12} {14} {15} {16} {17} {17} {14} {18} {20} {18} {19} {21} {22} 5 Step 2[5]. Now we get rid of - moves: Q={1,2,3,4,5,6,7,8,9}, initial_state=1, F={1,2,4,6,9}, and is defined below 1 2 3 4 5 6 7 8 9 a {2,7} {2} {5} b {3} {3,4} {5} {4} {6} {8} {8} {7,9} {9} Step 3[5]. We now transform non deterministic automaton into deterministic one. Q={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}, initial_state=1, F={1,2,4,5,6,9,10,12,13,14,15,17,18}, ={a,b}, garbage=19 and is defined below 1 2 3 4 5 6 7 8 9 10 11 12 11 13 14 15 16 17 18 19 a 2 5 19 7 5 11 7 8 19 7 11 19 11 7 19 8 19 19 19 19 b 3 4 8 6 12 10 9 16 19 13 14 15 14 13 19 17 19 18 18 19 6 5.[19] a. b. c. Is a language { anbkcn+k | n≥0, k≥0 } regular? Prove your answer. Is the language {a 2m+3n+2 | m,n ≥ 0} regular? Prove your answer. Is the language {a n! | n ≥ 0} regular? Prove your answer. Solution: 6[5]. a.[5] L is not regular. Suppose it is regular, so Pumping Lemma hold. Let n the number from Pumping Lemma, and let x = anbkcn+k. Clearly |x|n. Let x = uvw, |uv|n, |v|1. Since |uv|n, v=ai, for some i, where 1in. Hence, y=uviw=an+ibkcn+k L, a contradiction, n+i+k > n+k. b.[5] L is regular as L=(aa)*(aaa)*aa. c.[5] L is not regular. Suppose it is regular, so Pumping Lemma hold. Let n the number from Pumping Lemma, and let x = an!. Clearly |x|=n! n. Let x = uvw, |uv|n, |v|1. Let y=uv2w. Since |uv|n then |v|n. Hence |y|=|uvw|+|v| n!+n. However the shortest string in L, which is longer than x is a(n+1)!, and |a(n+1)!| = (n+1)! = (n+1)n! > n!+n, so y L, a contradiction. Eliminate all useless productions from the below grammar. Give all the steps. S → a | aA ∣ B | C A → aB | ε B → Aa C → cCD D → ddd What language does this grammar generate? Solution: Step 1. There is only one type 1 useless symbol, namely: C After erasing it we get: S 6 a | aA * B A 6 aB | ε B 6 Aa D 6 ddd Step 2. There is only one type 2 useless symbol, namely: D After erasing it we get: S 6 a | aA * B A 6 aB | ε B 6 Aa 7 The grammar generates the language with odd number of a=s, i.e. { an | n is odd } 7[5]. Eliminate all ε-productions from S → ABaC A → BC B→b|ε C→D|ε D→d Give all the steps. Solution: In the first step we conclude that B, C can eventually generate ε, so they are nullable. Then, since A 6 BC we conclude that A is also nullable. It is easy to observe that only A,B,C are nullable. Hence: S 6 ABaC | BaC | AaC | ABa | aC | Ba | Aa | a A 6 BC | B | C B6b C6D D6d 8.[5] Eliminate all unit-productions from S → Aa | N A → a | bc |B B → A | bb Give all the steps. Solution: S 6 N is useless. We have A 6 B and B 6 A, so actually A is the same as B. Therefore we can change: A 6 a | bc | B B 6 A | bb into just A 6 a | bc | bb Hence the final grammar is: S 6 Aa A 6 a | bc | bb 8 9. [10] Convert the grammar S → abAB A → bAB | ε B → BAa | A | ε into Chomsky normal form. Give all the steps. Solution: First we have to get rid of all ε-productions. The obtained grammar is: S 6 abAB | abA | abB |ab A 6 bAB | bA | bB |b B 6 BAa | Aa | Ba | a | A Next we have to get rid of all unit-productions. The result is: S 6 abAB | abA | abB |ab A 6 bAB | bA | bB | b B 6 BAa | Aa | Ba | a | bAB | bA | bB |b Now, we may produce a Chomsky normal form: S6 XaXbAB | XaXbA | XaXbB |XaXb Xa 6 a Xb 6 b XbAB 6XaXAB XbA 6XbA XbB 6XbB XAB 6AB A 6 XBXAB | XbA | XbB | b B 6 BAXa | AXa | BXa | a | XBXAB | XaA | XbB | b 10.[10] Convert the grammar S → ABb | a A → aaA | b B → bAb | bBB | ABB | a into Greibach normal form. Solution: First substitute A by aaA | b in B 6 ABB. The result is i.e. B 6 bAb | bBB | a | aaABB | bBB, B 6 bAb | bBB | a | aaABB Now we substitute A by aaA | b in S → ABb. The result is 9 S → aaABb | bAAb | a Hence now we have: S → aaABb | bAAb | a A → aaA | b B 6 bAb | bBB | a | aaABB which is almost Graibach. We just need to add Ca 6 a, Cb 6 b, and replace a and b that are not first by Ca and Cb, respectively. Final result: S → aCaABCb | bAACb | a A → aCaA | b B 6 bACb | bBB | a | aCaABB Ca 6 a Cb 6 b 11.[10] Construct a pushdown automaton accepting: { anbm ∣ 2n = 3m, n≥0, m≥0 }. Solution: For a string aibj to be in { anbm ∣ 2n = 3m, n≥0, m≥0 }, it must be true that i=3k and j=2k for some k$0. Thus, for each symbol ‘a’ of the input, we push two copies of symbol ‘a’ into the stack (this can be done in two steps, using the states s1, s2). Then, for each symbol ‘b’ we read from the input, we match it with three a=s in the stack (this has to be done in three steps, using the states q1, q2, q3). The automaton looks as follows: 10 For an alternative definition of pushdown automaton (more popular, red stuff in class notes), we have: A = (Q, Σ, Γ, s, $, δ, F), where Q = {s, q1, q2, q3, q4, r}, Σ={a,b}, Γ= {a,b,$}, F = {r} and δ is defined as follows: δ(s,ε,$) = { (r,ε)} (just to accept empty string) δ(s,a,$) = {(s,aa$)} (we assume here that the top of the stack is the first symbol on the left, so ‘a’ in this case) δ(s,a,a) = {(s,aa)} δ(s,b,a) = {(q1, ε )} δ(q1,ε,a) = { (q2,ε)} δ(q2,ε,a) = { (q3,ε)} δ(q3,b,a) = { (q4,ε)} δ(q3,ε,$) = { (r,ε)} δ(q4,b,a) = { (q1,ε)} 12.[10] Construct a pushdown automaton accepting: { aibjck ∣ i,j,k≥0, i+k=j }. Solution: The basic idea for this problem is to use different states to enforce the order of the occurrences of the letters a, b and c, and use the stack to meet the requirement of i+k=j. 11 For an alternative definition of pushdown automaton (more popular, red stuff in class notes), we have: A = (Q, Σ, Γ, s, $, δ, F), where Q = {s, q, p, r}, Σ={a,b,c}, Γ= {a,b,c,$}, F ={r} and δ is defined as follows: δ(s,a,$) = {(s,a$)} δ(s,b,$) = {(q,b$)} δ(s,a,a) = {(s,aa)} δ(s,b,a) = {(q,ε)} δ(q,b,a) = {(q,ε)} δ(q,b,$) = {(q,b$)} δ(q,b,b) = {(p,bb)} δ(p,c,b) = {(p,ε)} δ(p,ε,$) = {(r,ε)} 12 13.[10] Construct a pushdown automaton that accept the language generated by the following grammar: S → aS | aSbS ∣ε You may use either the construction from the textbook or the one presented in class. Solution: Let L be the language generated by a given grammar and let L= = L - {ε}. We may use the simpler construction presented in class. After removing ε-productions and transforming the obtained result into the Graibach normal form, we may get the following grammar (construction is not unique): S 6 aS | aSCbS | a | aCbS | aSCb | aCb Cb 6 b Now, the construction from the class results in (colours in automata correspond to colours in grammar, ε treated separately – q0 is final): 13 For an alternative definition of pushdown automaton (more popular, red stuff in class notes), we have: A = ({p,r},{a,b},{S, Cb}, p,S ,δ, F), where F ={r} and δ is the following: δ(p,a,S ) = {(p,SCbS), (p,S), (p,CbS), (p,SCb), (p,Cb),(p,ε)} δ(p,b,Cb) = { (p,ε)} δ(p,ε,S ) = {(r,ε)}. 14.[10] Show that the following language is not context-free { aibj ∣ i ≤ j2 }. Solution: Assume L = { anbj ∣ n≤j2 } is context free, then pumping lemma hold. Let p be the number in pumping lemma. Let z = aqbp and q=p2. Clearly, |z| ≥p and z∈L. Therefore, z=uvwxy and |vx| ≥1, |vwx| ≤p, and (∀i ≥0) uviwxiy ∈L. Let us consider two cases, Case 1) vx = ak, i.e. containing no b’s. Then, z’ = uv2wx2y = aq+kbp, and k ≥1. Therefore, z’ ∉L, contradiction. Case 2) vx = ajbk, and k ≥1, i.e. containing at least one b. Then, consider z’ = uv0wx0y. #a(z’) = q-j =p2-j, and 1≤j ≤p-1 (#a(z’) is the number of ‘a’ in z’) #b(z’) = p-k, and 1≤k≤p. So, (#b(z’))2 = (p-k)2 = p2 -2kp +k2 Clearly kp-j >k2 - kp, since kp-j ≥p-(p-1)=1, but k2 - kp = k(k-p) ≤0. Now, it is easy to verify that, p2-j >p2 -2kp +k2. Therefore, z’ ∉L, contradiction. Bonus questions. 15.[8] Transform the pushdown automaton from Example 2.14 (page 115, Figure 2.15 of the textbook) into an equivalent context free grammar using the construction from Lemma 27 of the textbook. Solution: To simplify notation we will write ‘i’ instead of ‘qi’, so Q=[1,2,3,4}. We start with productions that are in fact automaton independent (steps 2 and 3 from the top of page 122): For all i,j = 1,2,3: Aij 6 Ai1 A1j | Ai2 A2j | Ai3 A3j 14 For all i=1,2,3: Aii 6 ε We have Γ = {0,$}. For u=$, we have: δ(1, ε, ε ) = {(2, $ )} and δ(3, ε, $ ) = {(4, ε)}, so we add A14 6 A23 For u=0, we have : δ(2, 0, ε ) = {(2, 0 )} and δ(2, 1, 0 ) = {(3, ε)}, so we add A13 6 0A221 and δ(2, 0, ε ) = {(2, 0 )} and δ(3, 1, 0 ) = {(3, ε)}, so we add A23 6 0A231 16.[10] Read pages 3-9 (27-29 on the right top) from Lecture Notes 3, or the similar pages from any other appropriate textbook, and using this method (that does not use the idea of ‘generalized automaton’) find a regular expression that generates the same language as the automaton below. a C 1 b b a a 2 b 3 15 k=0 k=1 k=2 k=3 r11k a ∪ε a* a* r12k b a* b a*ba* a*ba* ∪a*ba*b ε ∪(a ∪ba*b) a*b * (a ∪ba*b) a* r13k ∅ ∅ a*ba*b a*ba*b ∪a*ba*b ε ∪(a ∪ba*b) a*b r21k ∅ ∅ ∅ a*b(ε ∪(a ∪ba*b) a*b * ba* r22k a ∪ε a* a* a* ∪a*b ε ∪(a ∪ba*b) a*b * (a ∪ba*b) a* r23k b b a* b a*b ∪ a*b ε ∪(a ∪ba*b) a*b r31k b ba* ba* ba* ∪ ε ∪(a ∪ba*b) a*b ε ∪(a ∪ba*b) a*b *ba* r32k a a ∪ba*b (a ∪ba*b) a* (a ∪ba*b) a* ∪ ε ∪(a ∪ba*b) a*b ε ∪(a ∪ba*b) a*b * (a ∪ba*b) a* r33k ε ε ε ∪(a ∪ba*b) a*b a* ∪ a*ba*b ε ∪(a ∪ba*b) a*b * ba* ε ∪(a ∪ba*b) a*b * * * L(M) = L(r113 ∪ r133 ) = L( a* ∪ a*ba*b ε ∪(a ∪ba*b) a*b * ba* ∪a*ba*b ∪a*ba*b ε ∪(a ∪ba*b) a*b * Or, by popular ‘abuse of notation’: L(M) = a* ∪ a*ba*b ε ∪(a ∪ba*b) a*b * ba* ∪a*ba*b ∪a*ba*b ε ∪(a ∪ba*b) a*b * 17.[10] Construct a pushdown automaton accepting: { anbm ∣ 2n≠3m, n≥0, m≥0 }. Solution: The idea here is to use the pushdown automaton for the language { anbm ∣ 2n = 3m, n≥0, m≥0} from Question 11, to match every 2 b’s with 3 a=s. First observe that if the state q1 has been rached them m>0. Moreover if the top of the stack is $ in any of the states q1, q2, or q4, then 2n>3m and if the top of the stack in q3 is ‘a’ and there is no symbol to read, then 2n<3m. The case of m=0 (which implies n>0) needs to treated separately. This lead us to the following automaton, where the black part is from the automaton accepting the language { anbm ∣ 2n = 3m, n≥0,m≥0}, red part deals with m>0 and blue part with m=0. 16 . For an alternative definition of pushdown automaton (more popular, red stuff in class notes), we have: A = (Q, Σ, Γ, s, $, δ, F), where Q = {s, q1, q2, q3, q4, r, t}, Σ={a,b}, Γ= {a,b,$}, F = {r,t} and δ is defined as follows: (the part from acceptance of { anbm ∣ 2n≠3m, n≥0, m≥0 }, and beginning of the case m=0). δ(s,a,$) = {(s,aa$), (t,$)} (we assume here that the top of the stack is the first symbol on the left, so ‘a’ in this case) δ(s,a,a) = {(s,aa)} δ(s,b,a) = {(q1, ε )} δ(q1,ε,a) = { (q2,ε)} δ(q2,ε,a) = { (q3,ε)} δ(q3,b,a) = { (q4,ε)} δ(q4,b,a) = { (q1,ε)} (the part that deals with 2n<3m, m>0) δ(q1,ε,$) = { (r,ε)} δ(q2,ε,$) = { (r,ε)} δ(q4,ε,$) = { (r,ε)} 17 (the part that deals with 2n>3m, m>0) δ(q3, ε,a) = { (r,ε)} (the remaining part that of m=0) δ(t,a,$) = {(t, $ )} 18