Honors Theory, Spring 2002, Yap Homework 2 Due: Wed Feb 13 in class

MOSTLY ABOUT CONTEXT FREE LANGUAGES.

1. No credit work, as this exercise is self-rewarding :-). Recall the example of translating “The spirit is

willing but the flesh is weak” into Russian, and back again, producing “The vodka is strong
but the meat is rotten”. The sentence “Out of sight, out of mind” was translated into “Blind

idiot”. Try to outdo these machine translations produced in the early days of MT. [It only goes to prove that this problem is too hard for machines.]

2. (10+15 Points)

For n ≥ 1, let Σ_{n} = {a_{1}, …, a_{n}} be an alphabet with n letters. Define L_{n} ⊆ Σ_{n}^{∗} to comprise those words w for which there is some i = 1, …, n such that a_{i} does not occur in w.

(i) Show that there is an nfa N_{n} with n + 1 states that accept L_{n}. (ii) Show that every dfa tha accepts L_{n} has at least 2^{n} states. HINT: why 2^{n}?

SOLUTION (i): We construct a NFA M with n + 1 states that accepts L_{n}.

M = (Q, Σ, δ, q_{0}, F) Q = {q_{0}, q_{1}...q_{n}} Σ = {a_{1}, a_{2}...a_{n}} F = {q_{1}, q_{2}...q_{n}} δ(q_{0}, ϵ) = q_{i}, i ≠ 0 δ(q_{i}, a_{j}) = q_{i}, i ≠ j, i ≠ 0.

SOLUTION (ii): Assign to each string w a binary characteristic number f(w) = b_{n}...b_{0}, where b_{i} = 0 if a_{i} does not appear in w, and b_{i} = 1 if a_{i} appears in w. There are 2^{n} such numbers. Only the strings that have the characteristic number with all bits set to 1 are rejected. Show that every DFA that accepts L_{n} has at least 2^{n} states. Suppose not, then there is a DFA M with less than 2^{n} states that accepts L_{n}. According to Pigeonhole Principle, there are at least two strings w and w′ with different characteristic numbers x and y that reach the same state q_{1}. We have: δ(q_{0}, w) = q_{1} δ(q_{0}, w′) = q_{1}. As x ≠ y, they have at least one bit b_{k} that is different. WLOG, assume b_{k} = 1 in x, and b_{k} = 0 in y. Choose a string s = a_{1}...a_{k−1}a_{k+1}...a_{n}, then f(ws) has all bits equal to 1, and f(w′s) has all bits except b_{k} equal to 1. So ws should be rejected, while w′s should be accepted. But δ(q_{0}, ws) = δ(q_{1}, s) = δ(q_{0}, w′s). We obtain a contradiction.

3. (15+10 Points)

Let A, B ⊆ Σ^{∗}. The right quotient of A by B is defined to be

A/B:={w ∈ Σ^{∗} : (∃u ∈ B)[wu ∈ A]}.

(i) Show that if A is context free and B is regular, then A/B is context free. (ii) Use part (i) to show that the language {0^{p}1^{n} : p is prime, n > p} is not context free.

SOLUTION (i): As A is context free, there is a pda M_{1} = {Q_{1}, Σ_{1}, Γ_{1}, δ_{1}, S, F_{1}} that recognizes it. As B is regular, there is a dfa M_{2} = {Q_{2}, Σ_{2}, δ_{2}, q_{0}, F_{2}} that recognizes it. We may assume Σ_{1} = Σ_{2}. We construct a pda M that recognizes A/B: M = {Q, Σ, Γ, δ, S, F } Q = Q_{1} ∪ (Q_{1} × Q_{2}) Σ = Σ_{1} Γ = Γ_{1} F = F_{1} × F_{2}. There are three steps. (a) The basic idea is that M will initially simulate the actions of M_{1} until the end of input is reached. To do this, we put all the transitions of M_{1} to M, but make all the accepting states of M_{1} nonaccepting in M.

Once the end of input is reached, M will try to simulate the actions of M_{2} on some unknown (hidden) input sequence. This is achieved by letting M perform ϵ-transitions based on this hidden input sequence. But, as M_{2} acts on this hidden sequence, M_{1} is also acting on exactly the same sequence. If both M_{1} and M_{2} halts at the same time, then we accept. Hence M must keep track of both the states of M_{1} and M_{2}.

(b) First, we ϵ-transitions from Q_{1} to Q_{1} × Q_{2}: for each q_{i} ∈ Q_{1} add a transition q_{i}, ϵ, ϵ → q_{i0}, ϵ.

(c) Now we add ϵ-transitions to simulation the simultaneous actions of M_{1} and M_{2}. Add a new state q_{ij} for each pair (q_{i}, q_{j}), where q_{i} ∈ Q_{1}, q_{j} ∈ Q_{2}. Add transitions among the states in Q_{1} × Q_{2}: for each symbol a such that q_{i}, a, A → q_{I}, B in M_{1}, and q_{j}, a → q_{J} in M_{2} add a transition q_{ij}, ϵ, A → q_{IJ}, B The accepting states are those q_{ij}’s, with q_{i} ∈ F_{1}, and q_{j} ∈ F_{2}.

We leave it as an exercise for the student to prove that this construction is correct.

SOLUTION (ii): Let A = {0^{p}1^{n}, p is prime, n ≥ p}. By way of contradiction, assume A is context free. Let B = {01^{∗}} and C = {0^{p−1}, p is prime }. As B is regular, then C = A/B is context free by part (i). But we will show that C is not context free, which is a contradiction. Let l be the pumping length for C. Select the string s = 0^{m−1} where m is a prime greater than l. By the pumping lemma, s = uvxyz where |vxy| ≤ l and |vy| > 0, and uv^{i}xy^{i}z ∈ C for all i ≥ 0. Pick i = m + 1. Then |uv^{i}xy^{i}z| = |uvxyz| + |vy| × (i − 1) = m − 1 + m × |vy| = m × (|vy| + 1) − 1. As m ≥ 2 and |vy| ≥ 1, |uv^{i}xy^{i}z| + 1 can not be a prime. Contradiction.

4. (10 Points Each) Prove or disprove that the following are context free: (i) L_{1} = {w ∈ {a, b, c}^{∗} : #_{a}(w) = #_{b}(w) or #_{b}(w) = #_{c}(w) or #_{a}(w) = #_{c}(w)}. (ii) L_{2} = {w ∈ {a, b, c}^{∗} : #_{a}(w) ≠ #_{b}(w) or #_{b}(w) ≠ #_{c}(w) or #_{a}(w) ≠ #_{c}(w)}.

(iii) L_{3} = {w ∈ {a, b, c}^{∗} : #_{a}(w) = #_{b}(w) and #_{b}(w) = #_{c}(w) and #_{a}(w) = #_{c}(w)}. NOTE: #_{a}(w) counts the number of occurences of a in w. SOLUTION (i): L_{1} is context free, as there is a PDA that recognizes it.

SOLUTION (ii): L_{2} is context free, as there is a PDA that recognizes it.

SOLUTION (iii): Use the pumping lemma to show that L_{3} is not context free. If L_{3} is context free, let p be its pumping length. Let s = a^{p}b^{p}c^{p}. Clearly s ∈ L_{3}. Let s = uvxyz be the decomposition of s given by the pumping lemma. As |vxy| ≤ p, vy can not contain all three types of alphabet symbols. The string uv^{2}xy^{2}z can not contain equal numbers of a’s, b’s, and c’s. Contradiction.

5. (15+15 Points) (i) Construct an efficient algorithm that, on input ⟨G, w⟩ where G = (V, T, S, R) is a grammar in Chomsky Normal Form and w a string, decide whether w ∈ L(G).

HINT: Use dynamic programming. For 1 ≤ i ≤ j ≤ n, let w_{ij} denote the substring a_{i} ⋯ a_{j} where w = a_{1}, …, a_{n}. Define V_{ij} = {A ∈ V : A ⇒^{∗} w_{ij}}. How do you compute V_{ij} if you know the sets V_{ik}, V_{kj} for all k = i, …, j? (ii) What is the worst-case complexity of your algorithm, as a function of input sizes m = |G|, n = |w|? There is an interesting low-level issue here: |w| and |G| must be suitably interpreted. Note that V, T are arbitrary alphabets, but your algorithm must accept input with a fixed alphabet (say Σ). Hence use the following convention: assume Σ contains the special symbols A, a, 0, 1 (among others), and each symbol of V is encoded as a string of the form A(0 + 1)^{∗}, and each symbol of T and w is encoded as a string of the form a(0 + 1)^{∗}. The definition of |G| can be taken to be the number of symbols in writing down all the rules of G plus |V ∪ T |. Then each symbol in x has length equal to its encoding as a string in L(a(0 + 1)^{∗})! Similarly, you need specify your encoding G over the fixed alphabet Σ and tell us how to determine its length |G|.

SOLUTION (i) (see Sipser’s book p.241) The algorithm M (Recall that the empty string is handeled specially in a Chomsky normal form grammar):

On input w = w_{1} ... w_{n}:

1. If w = ϵ and S → ϵ is a rule, accept.

2. For i = 1 to n,

3. For each variable A,

4. Test whether A → w_{i} is a rule.

5. If so, place A in table(i, i).

6. For l = 2 to n,

7. For i = 1 to n − l + 1,

8. Let j = i + l − 1,

9. For k = i to j − 1,

10. For each rule A → BC,

11. If table(i, k) contains B and table(k + 1, j) contains C, put A in table(i, j).

12. If S is in table(1, n), accept. Otherwise, reject.

SOLUTION (ii): Let v be the number of variables in G, r be the number of rules in G. m is the length of G, n is the length of the input string. Stages 4 and 5 run in O(vn) times. Stage 6 runs at most n times. Each time stage 6 runs, stage 7 runs at most n times. Each time stage 7 runs, stages 8 and 9 run at most n times. Each time stage 9 runs, stage 10 runs r times. Thus stage 11, the inner loop of the algorithm, runs O(r × n^{3}). Summing the total shows that M executes in time O(m × n^{3}).

Use the following convention: Assume the fixed alphabet Σ contains the special symbols A, a, 0, 1 (among others), with each symbol of V encoded as a string of the form A(0 + 1)^{∗}, and each symbol of T and w encoded as a string of the form a(0 + 1)^{∗}. After encoding G with the fixed alphabet Σ, the definition of |G| can be taken to be the number of symbols in writing down all the rules of G plus |V ∪ T |. After encoding, each symbol in V has length O(log|V |), and each symbol in T has length O(log|T |). The length of encoded V ∪ T is O(|V | × log|V | + |T | × log|T |). As each rule of G has length O(log|V |+log|T |), writng down all the rules takes O(|R|×(log|V |+log|T |)). Thus the length of G after encoding over the fixed alphabet is: O(|V | × log|V | + |T | × log|T | + |R| × (log|V | + log|T |))