Prof_ Marvin Nakayama - HomeWork 2

5
CS 530, Fall 2010 Homework 2 – Solutions Problem 1 Prove that the following languages over {0, 1, 2} are not regular: Solution : 1 L 1 = {0 n 1 n 2 n }. Assume that L is regular. Let p be the constant in the statement of the pumping lemma. Now consider w =0 p 1 p 2 p . Since w L and |w|≥ p, by the pumping lemma, there exists a decomposition w = xyz such that |y | > 0, |xy |≤ q and xy i z L for each i 0. In particular, xy 0 z = xz should be in L. But notice that y 0 , and so xz has less 0 s than 1 s and 2 s. 2 L 2 = {w : w = w R }. Assume that L is regular, and let p be the constant in the statement of the pumping lemma. Let w =0 p 10 p . Since w L 2 and |w|≥ p, by the pumping lemma, there exists a decomposition w = xyz such that |y | > 0, |xy |≤ p and xy i z L 2 for each i 0. For i = 0 this implies that xz =0 r 10 p is in L where r<q . But this is not possible, so it must be the case that L 2 is not regular. 3 L 3 = {w : w = w R }. Notice that L is the complement of L 2 . By closure under complement, L 3 is regular iff L 2 is regular. But we saw in part 3.2 that this is not the case. 3 L 3 = {w : w = w R }. Notice that L is the complement of L 2 . By closure under complement, L 3 is regular iff L 2 is regular. But we saw in part 3.2 that this is not the case. 4 L 4 = {0 i 1 j : i = j }. Let A = {0 i 1 j : i 0,j 0} and B = {0 i 1 j : i = j }. Note that B = A L 4 Now A is regular since it corresponds to the regular expression 0 1 . So, if L 4 were regular, then B would be regular because of the closure properties of regular languages. But we know from example 1.38 in the textbook that B is not regular. Problem 2 Give context-free grammars that generate the following languages over the alpha- bet {0, 1}: Solution :

description

automata theory

Transcript of Prof_ Marvin Nakayama - HomeWork 2

Page 1: Prof_ Marvin Nakayama - HomeWork 2

CS 530, Fall 2010

Homework 2 – Solutions

Problem 1 Prove that the following languages over {0, 1, 2} are not regular:

Solution : 1 L1 = {0n1n2n}.

Assume that L is regular. Let p be the constant in the statement of the pumping lemma.Now consider w = 0p1p2p. Since w ∈ L and |w| ≥ p, by the pumping lemma, there exists adecomposition w = xyz such that |y| > 0, |xy| ≤ q and xyiz ∈ L for each i ≥ 0. In particular,xy0z = xz should be in L. But notice that y ∈ 0∗, and so xz has less 0′s than 1′s and 2′s.

2 L2 = {w : w = wR}.

Assume that L is regular, and let p be the constant in the statement of the pumping lemma.Let w = 0p10p. Since w ∈ L2 and |w| ≥ p, by the pumping lemma, there exists a decompositionw = xyz such that |y| > 0, |xy| ≤ p and xyiz ∈ L2 for each i ≥ 0. For i = 0 this implies thatxz = 0r10p is in L where r < q. But this is not possible, so it must be the case that L2 is notregular.

3 L3 = {w : w 6= wR}.

Notice that L is the complement of L2. By closure under complement, L3 is regular iff L2 isregular. But we saw in part 3.2 that this is not the case.

3 L3 = {w : w 6= wR}.

Notice that L is the complement of L2. By closure under complement, L3 is regular iff L2 isregular. But we saw in part 3.2 that this is not the case.

4 L4 = {0i1j : i 6= j}.

Let A = {0i1j : i ≥ 0, j ≥ 0} and B = {0i1j : i = j}. Note that

B = A ∩ L4

Now A is regular since it corresponds to the regular expression 0∗1∗. So, if L4 were regular,then B would be regular because of the closure properties of regular languages. But we knowfrom example 1.38 in the textbook that B is not regular.

2

Problem 2 Give context-free grammars that generate the following languages over the alpha-bet {0, 1}:

Solution :

Page 2: Prof_ Marvin Nakayama - HomeWork 2

(a) {w ∈ {0, 1}∗ | w contains more 1’s than 0’s}

S → A1S | A1A

A → 0A1A | 1A0A | ε

By applying productions S → A1S and S → A1A we can produce all strings of the formA1A1A . . . A1A. Since A can produce all strings with same number of 1’s and 0’s, thegrammar will generate exactly the required language.

(b) {0n1m | n + m is odd}.

S → AB | CD

A → A00 | ε

B → B11 | 1

C → C00 | 0

D → D11 | ε

Here, A is used to generate any even number of 0’s, B is used to generate any odd numberof 1’s, and so on.

(c){

w ∈ {0, 1}∗ | w 6= wR}

S → 0S0 | 1S1 | 0A1 | 1A0

A → A0 | A1 | ε

The only way to get rid of S and then derive a string containing only terminals is toapply one of the productions S → 0A1 or S → 1A0, which gives a pair of non-matchingterminals.

2

Problem 3 Prove that following: if L is a context-free language, then LR = {wR : w ∈ L} isalso context-free.

Proof : Let L be a CFL generated by G = (V, Σ, R, S), where G is in Chomsky normalform. Let G′ = (V, Σ, R′, S) where a rule of the form A → a or S → ε is in R′ iff it is in R, anda rule of the form A → BC is in R′ iff A → CB is in R.

Page 3: Prof_ Marvin Nakayama - HomeWork 2

It remains to show that L(G′) = LR. In other words, we have to prove that we can derive astring w in G′ if and only if wR can be generated in G. The proof is by induction on the lengthof w.

The claim is clearly true if |w| = 0. Assume that the claim is true for all strings of length atmost n, and let w be a string of length n+1. It follows that there is a derivation of w in G′ thatstarts with a rule of the form S → AB; by construction, this happens iff the rule S → BA is inG. Hence w = wAwB where |wA|, |wB| ≤ n, A

⇒ wA and B∗

⇒ wB in G′. From our induction

hypothesis, these two derivations happen iff A∗

⇒ wRA and B

⇒ wRB in G. We conclude that

S∗

⇒ w in G′ iffS ⇒ BA

⇒ wRBwR

A = (wAwB)R,

which completes the proof of the inductive step. 2

Problem 4 Convert the following CFG into an equivalent CFG in Chomsky normal form,using either the procedure given in the textbook or the one from the lecture.

A → BAB|B|ǫ

B → 00|ǫ

Solution :Only the final result is given (one should have also showed the steps).

S → AB|BA|BB|BC|DD|ε

A → AB|BA|BB|BC|DD

B → DD

C → AB

D → 0

2

Problem 5 Show that, if G is a CFG in Chomsky normal form, then for any string w ∈ L(G)of lenght n ≥ 1, exactly 2n − 1 steps are required for any derivation of w.

Proof : Note that since |w| > 0, any derivaion of w must use rules of one of these two forms

(i) A → BC

(ii) A → a

Page 4: Prof_ Marvin Nakayama - HomeWork 2

Also notice that applying rules of type (i) increase the length of the string by 1, whileapplying rules of type (ii) keep the string length constant. Hence in any derivation of w theremust be exactly n−1 applications of rules of type (i). Similarly any terminal symbol in a stringmust have been produced by the application of a rule of type (ii), and such an applicationcan produce only one terminal symbol. Hence in any derivation of w there must be exactly n

applications of rules of type (ii). It follows that every derivation of w must take exactly 2n− 1steps. 2

Problem 6

Solution : 6. (a) Give context-free grammars that generate the languages L1 = {aibjck :

i, j, k ≥ 0, i = j} and L2 = {aibjck : i, j, k ≥ 0, j = k}.The following grammar generates L1:

S → DC

C → cC | ε

D → aDb | ε

The following grammar generates L2:

S → AD

A → aA|ε

D → bDc|ε

(b) Show that L1 ∩ L2 = {anbncn|n ≥ 0}.

Proof: Assume that w ∈ L1 ∩ L2. Then w = aibjck where i = j and j = k. This implies thati = j = k, and so w ∈ {anbncn : n ≥ 0}.

Conversely, if w ∈ {anbncn : n ≥ 0}, then w ∈ L1 and w ∈ L2. Hence, L1 ∩ L2 = {anbncn :n ≥ 0}.

(c) Use the result established in Example 2.36, page 126 of the text, to show that the class ofcontext-free languates is not closed under intersection.

Proof: By (b), {anbncn : n ≥ 0} is the intersection of two context-free languages, namely L1

and L2. But Example 2.20 shows that {anbncn : n ≥ 0} is not context-free. Hence, context-freelanguages are not closed under intersection.

Page 5: Prof_ Marvin Nakayama - HomeWork 2

(d) Use (c) and DeMorgan’s law to prove that the class of context-free languages is not closedunder complementation.

Proof: From (c) we have that {anbncn : n ≥ 0} = L1 ∩L2. Also from DeMorgan’s law we have

that L1 ∩ L2 = L1 ∪ L2, so

{anbncn : n ≥ 0} = L1 ∪ L2.

Suppose for a contradiction that the class of context-free languages is closed under comple-mentation. Since L1 is context-free, this would imply that L1 is context-free. Similarly, sinceL2 is context-free, we would also get that L2 is context-free. But the union of two context-freelanguages is context-free, so L1 ∪ L2 would also be contex-free. Now, by applying again the

supposed closure under complementation, we would get that L1 ∪ L2 is context-free. However,this contradicts the fact that {anbncn : n ≥ 0} is not context-free, as proved in Example 2.20.2