Homework 2 Solutions Raghunath Tewari September 13, 2013 1. (Marks: 5 + 7 + 8) Prove that the following languages are not regular. (a) L1 = {an! | n ≥ 1}. Solution: By definition, Lengths(L1 ) = {n! | n ≥ 1}. Since L1 is a unary language, to show that L1 is not regular, it is sufficient to show that Lengths(L1 ) is not ultimately periodic. Given n ≥ 0 and p > 0, let m = (n + p)!. Then, m+p = (n + p)! + p < (n + p)(n + p)! + p ≤ (n + p + 1)! This implies that m + p ∈ / Lengths(L1 ), and hence Lengths(L1 ) is not ultimately periodic. n m (b) L2 = {0 1 | n 6= m}. Solution: L2 ∩ L(0∗ 1∗ ) = {0n 1n | n ≥ 0} = L0 (say). Since we know that L0 is not regular, and since regular languages are closed under complement and intersection, L2 is also not regular. (c) L3 = {w ∈ {0, 1}∗ | |w| is not a perfect square}. Solution: Note that it is enough to show that L3 = {w ∈ {0, 1}∗ | |w| is a perfect square} is not regular, since regular languages are closed under complement. We prove this using the contrapositive form of Pumping Lemma for regular languages. 2 Given p, choose w = 0p . By our choice, w ∈ L3 . Given a partition of w = xyz such that |y| = l, choose i = 2. Now, |xy 2 z| = < p2 + l (p + 1)2 (since 0 < l ≤ p). Therefore, xy 2 z ∈ / L3 and thus L3 is not regular. 2. (Marks: 10) Let L ⊆ Σ∗ be a language and a ∈ Σ. Then L/a = {w ∈ Σ∗ | wa ∈ L}. Prove that if L is regular, then so is L/a. Solution: Let D = (Q, Σ, δ, q0 , F ) be a DFA such that L = L(D). We will construct an NFA for L/a. Let Q0 = Q ∪ {f 0 }. Define δ 0 as follows, (a) if δ(p, a) = q then δ 0 (p, a) = {q}, and (b) for every state p ∈ Q such that δ(p, a) ∈ F , define δ 0 (p, ) = {f 0 }. 1 The NFA N = (Q0 , Σ, δ 0 , q0 , {f }) recognises L/a. To see this firstly observe that any string w ∈ L(D), such that w = xa, has the property that if δ(q0 , x) = p, then δ(p, a) ∈ F . Therefore by definition, f 0 ∈ δ 0 (q0 , x). On the other hand if w ∈ L(N ), then δ 0 (q0 , w) = {f 0 }, since f 0 is the only accepting state in N . Now since the only transitions to f 0 are -transitions and since if there is an -transition from p to f 0 is N then there is a transition from p to an accept state in D, therefore δ(q0 , w) ∈ F . 3. (Marks: 15) Let w = w1 w2 . . . wn and x = x1 x2 . . . xn be two strings of length n. Then define alt(w, x) = w1 x1 w2 x2 . . . wn xn . Now we can extend this definition to languages as follows. Let L, M be two languages over Σ. Then, alt(L, M ) = {alt(w, x) | |w| = |x|, w ∈ L, x ∈ M }. Prove that if L and M are regular languages, then so is alt(L, M ). Solution: Let DL = (QL , Σ, δL , q0L , FL ) and DM = (QM , Σ, δM , q0M , FM ) be DFAs such that L = L(DL ) and M = L(DM ). We want to construct an automaton that would recognise the language alt(L, M ). The idea is to have states which are tuples of the form (p, q, b) where p ∈ QL , q ∈ Qm and b ∈ {0, 1}. The machine makes transitions as follows: (a) if b is 0, then move according to DL and flip b to 1, and (b) if b is 1, then move according to DM and flip b to 0. Let N = (Q0 , Σ, δ 0 , q00 , F 0 ) be an NFA defined as follows: (a) Q0 = QL × QM × {0, 1}. (b) δ 0 ((p, q, 0), a) = (δL (p, a), q, 1) and δ 0 ((p, q, 1), a) = (p, δM (q, a), 0). (c) q00 = (q0L , q0M , 0). (d) F 0 = {(p, q, 0) | p ∈ FL , q ∈ FM }. Then alt(L, M ) = L(N ). Note that for the accepting states in N , the third parameter is 0. This is because by definition, the last symbol of alt(w, x) is from x. 4. (Marks: 4 + 8 + 8) Design CFGs for the following languages. (a) L1 = {ai bj ck | i = j + k}. Solution: S −→ aSc | S1 S1 −→ aS1 b | ∗ (b) L2 = {w ∈ {0, 1} | w has more 0’s than 1’s}. (Hint: Try modifying the CFG that we discussed in class for strings having an equal number of 0’s and 1’s) Solution: S −→ S1 0S | S1 0S1 S1 −→ 0S1 1S1 | 1S1 0S1 | ∗ (c) L3 = {w ∈ {0, 1} | number of 0’s in w is not equal to the number of 1’s in w}. (Hint: Use part (b)) Solution: S A −→ −→ A|B C0A | C0C B −→ C1B | C1C C −→ 0C1C | 1C0C | 5. (Marks: 5 + 10) Consider the CFG G defined as S −→ SbS | a. (a) Prove that G is ambiguous. Solution: G. Consider the string w = abababa ∈ L(G). The following are two parse trees for w w.r.t S S b S a S S b b S a S a a S b S S b a S S S a a b S a Therefore G is ambiguous. 0 0 (b) Construct an unambiguous grammar G such that L(G ) = L(G). Solution: It is easy to see that the language recognised by G can be described as L(G) = {w ∈ {a, b}∗ | w has alternating a’s and b’s and w begins and ends with an a}. Thus if we define G0 as follows: S → abS | a 0 then L(G) = L(G ). Observe that only one rule in G0 has a variable in its right hand side, and there is a single occurrence of that variable in that rule. More formally, let w be a string that has two parse trees, T1 and T2 w.r.t. G0 . Since G0 has only two substitution rules, it implies that there is some node labeled S, such that both T1 and T2 are same from the root to this node, and at this node the rule S → abS is applied in one tree (say T1 ) and the rule S → a is applied in the other tree (T2 ). Since a is a terminal, T2 cannot go any further down. This implies that the number of leaves in T1 is greater than the number of leaves in T2 . Now since there are no leaves labeled , it means that the string generated by T1 is longer than the string generated by T2 . This is a contradiction. Therefore G0 is unambiguous.
© Copyright 2025 Paperzz