HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The...

7
HW6 Solutions Micha l Derezi´ nski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are as and bs. Its diagram is below. q 0 start q 1 q 2 a, Z 0 /XZ 0 b, Z 0 /XZ 0 a, X/XX b, X/XX a, X/X a, Z 0 /Z 0 a, X/Λ b, X/Λ Λ,Z 0 /Z 0 (b) q 0 start q 1 a, Z 0 /XZ 0 a, X/XX Λ, X/X Λ,Z 0 /Z 0 a, X/Λ b, X/Λ 1

Transcript of HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The...

Page 1: HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are

HW6 Solutions

Micha l Derezinski

March 20, 2015

1 Exercise 5.5

(a) The PDA accepts odd-length strings whose middle symbol is a and whoseother letters are as and bs. Its diagram is below.

q0start q1 q2

a, Z0/XZ0

b, Z0/XZ0

a,X/XXb,X/XX

a,X/Xa, Z0/Z0

a,X/Λb,X/Λ

Λ, Z0/Z0

(b)

q0start q1

a, Z0/XZ0

a,X/XX

Λ, X/XΛ, Z0/Z0

a,X/Λb,X/Λ

1

Page 2: HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are

(c)

q0start q1 q2 q3

a, Z0/XZ0

a,X/XX

Λ, X/XΛ, Z0/Z0

Λ, X/MXΛ, Z0/MZ0

b,X/Λb, Y/Y Yb,M/YM

Λ, Y/YΛ,M/MΛ, Z0/Z0

c, Z0/Z0

c, Y/Λ

Λ, Z0/Z0

Λ,M/Λ

2 Exercise 5.10

Let L = L(M) be a regular language defined through an FA M . Since ourPDA (call it M ′) may only have two states q′0, q

′1, exactly one of them should

be an accepting state, and one of them should be the starting state. Supposeq′0 is the starting state. If Λ ∈ L, then q′0 has to be also the acceptingstate, otherwise, q′1 will be the accepting state. Since M ′ has to simulate M ,which may contain more than 2 states, we have to store that informationsomewhere. So, we will make the stack alphabet out of the set Q of statesin M . For every transition

q1a→ q2

in M , we create a transition

q′ia,q1/q2q1−→ q′j

in machine M ′, where we select i, j as 0 or 1 to match the acceptance ofstates q1 and q2 in M . Machine M ′ simulates automaton M by keeping itscurrent state at the top of the stack.

2

Page 3: HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are

3 CYK Exercise

b a b a a1 B AC B AC AC2 BS AS BS ACS3 B ACS BS4 BS ACS5 BS

Since S appears in the last cell of the table, we know that babaa is gen-erated by the grammar. To be able to retrieve a derivation tree from thetable, we would need to keep more information in each cell. Specifically,for each non-terminal in cell, we write which production is used for it, andwhat partition of the subword is obtained. For example, we show below theaugmented version of the above table:

b a b a a1 B → b A,C → a B → b A,C → a A,C → a

2 B, Sb,a→ BC A, S

a,b→ AB B, Sb,a→ BC A

a,a→ AA

Ca,a→ CC

Sa,a→ AC

3 Bba,b−→ BB A, S

a,ba−→ AB

Cab,a−→ AC

B, Sb,aa−→ BC

4 B, Sb,aba−→ BC A, S

a,baa−→ AB

Cab,aa−→ AC

5 B, Sb,abaa−→ BC

To retrieve the derivation tree from the above table, we have to retracethe productions used, starting from the initial S in the bottom cell. Theproductions used in the correct derivation have been underlined in the table.

3

Page 4: HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are

4 Pumping Lemma Exercise

(a) L = {arbsct : r > s > t}For the sake of contradiction, suppose that L is context-free. Let n be theconstant from the Pumping Lemma. Take u = an+1bncn−1. Since |u| ≥ n, bythe Pumping Lemma we know that u = vwxyz, as in the lemma. Moreover,since |wxy| ≤ n, we have two cases:

1. wy = akbl, where k, l ≥ 0. Then, vxz = an+1−kbn−lcn−1 has to be inL, according to the Pumping Lemma. But notice that either l > 0, inwhich case n− l ≤ n− 1, or k > 0 = l, which means that n + 1− k ≤n = n− l, so the conditions of language L will be violated, obtaining acontradiction.

2. wy = bkcl, where l > k ≥ 0. Then u2 = vw2xy2z has to be in L,according to the Pumping Lemma. But notice that either k > 0, inwhich case #b(u2) = n + k ≥ #a(u2), or k = 0, which means that#c(u2) = n − 1 + l ≥ #b(u2), so the conditions of language L will beviolated, getting contradiction.

(b) L = {abnabnabn : n ≥ 0}For the sake of contradiction, suppose that L is context-free. Let n be theconstant from the Pumping Lemma. Take u = abnabnabn. Since |u| ≥ n, bythe Pumping Lemma we know that u = vwxyz, as in the lemma. Moreover,since |wxy| ≤ n, we have two cases:

1. wy contains at least one a. Word vxz has to be in L, according to thePumping Lemma. But notice that since u had exactly 3 letters a andwe removed at least one of them, then vxz will have less than 3 a’s, sothe conditions of language L will be violated, obtaining a contradiction.

2. wxy = bk, where n ≥ k > 0. Observe, that wxy has to then becontained within one of the three blocks bn. Without loss of generality,we can assume that it is the first block. According to Pumping Lemma,the word vw2xy2z = abn+kabnabn has to be in the language, but the firstblock of b’s is larger than the other two, so the conditions of languageL will be violated, getting contradiction.

4

Page 5: HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are

5 Exercise 6.5

(e) [Extra Credit] We will show that L = {x ∈ {a, b}∗ | na(x) < nb(x) <2na(x)} is a CFL by demonstrating a grammar for this language. First, letus describe three related languages:

1. L1 = {x ∈ {a, b}∗ | na(x) = nb(x)}. We know from the previoushomework, that L1 has for instance this grammar:

S1 → aS1bS1 | bS1aS1 | Λ.

Each production in this grammar adds one a and one b to the word.We will call those the type 1 productions.

2. L2 = {x ∈ {a, b}∗ | nb(x) = 2na(x)}. We can similarly obtain agrammar for this language:

S2 → aS2bS2bS2 | bS2aS2bS2 | bS2bS2aS2 | Λ.

Here, we have productions that add two b’s for each a - type 2 produc-tions.

3. L12 = {x ∈ {a, b}∗ | na(x) ≤ nb(x) ≤ 2na(x)}. For this language, weget a grammar by combining the productions of both types:

S12 → B1 | B2 | Λ

B1 → aS12bS12 | bS12aS12

B2 → aS12bS12bS12 | bS12aS12bS12 | bS12bS12aS12.

Language L12 is already very close to L, except the inequalities are not strict.This becomes a problem when a derivation consists of only productions usingB1, and never B2, or vice versa. We can solve that by forcing the derivationto have first a type 1 production, then a type 2 production, and then we canessentially revert to the last grammar:

S → aS2bS12 | aS12bS2 | bS2aS12 | bS12aS2

S2 → B2

S12 → B1 | B2 | Λ,

5

Page 6: HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are

with non-terminals B1, B2 having the same productions as earlier described.This gives us language L.

(g) Language L is a CFL because we know that it is the complementof the language of balanced parentheses, which is a DCFL, and DCFLs areclosed under complement.

6 Exercise 6.9

(a) Below is a PDA for the language L = {aibjck | i ≥ j or i ≥ k}, showingthat it is a CFL.

q0start q1 q2 q3

a, Z0/XZ0

a,X/XX

Λ, X/XΛ, Z0/Z0

Λ, X/MXΛ, Z0/MZ0

b,X/Λb, Y/Y Yb,M/YM

Λ, X/XΛ, Y/YΛ,M/MΛ, Z0/Z0

c,X/Xc, Z0/Z0

c, Y/Λ

Λ, Z0/Z0

Λ,M/ΛΛ, Y/YΛ, X/X

Let R be the regular language corresponding to a∗b∗c∗. Moreover, letL2 = {aibjck | i < j and i < k}. Then, the complement language satisfiesthe following formula:

L2 = L′ ∩R.

For the sake of contradiction, suppose that L′ is a CFL. Then, L2 has to bea CFL too, as an intersection of a CFL and a regular language. Let n bethe constant from the Pumping Lemma for language L2. Take u = an−1bncn.Since |u| ≥ n, by the Pumping Lemma we know that u = vwxyz, as in thelemma. Moreover, since |wxy| ≤ n, we have two cases:

1. wy = akbl, where k, l ≥ 0. Then, vxz = an−1−kbn−lcn and vw2xy2z =an−1+kbn+lcn both have to be in L, according to the Pumping Lemma.But notice that either k > 0, in which case n−1+k ≥ n (no less a’s thanc’s in vw2xy2z), or l > 0 = k, which means that n−l ≤ n−1 = n−1−l

6

Page 7: HW6 Solutions - Courses · HW6 Solutions Micha l Derezinski March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are

(no less a’s than b’s in vxz), so the conditions of language L will beviolated, obtaining a contradiction.

2. wy = bkcl, where l > k ≥ 0. Then vxz = an−1bn−kcn−l has to be in L,according to the Pumping Lemma. But notice that since l ≥ 1, thereis at least as many a’s as c’s in vxz so the conditions of language L willbe violated, getting contradiction.

This proves that L′ is not a CFL.

7