Docsity

Assignment 3 Problems Solutions - Languages and Computation | CS 3240, Assignments of Computer Science

Toggle sidebar
bg1
CS 3240: Languages and Computation
Problem Set 3 Solutions
Problem 1
Show that the following languages are not regular.
•L1={www :w∈ {a, b}∗}.
Solution: We prove that L1is nonregular by the pumping lemma. Let pbe any given positive
integer, and let s= 0p10p10p1. Clearly s∈L1(with w= 0p1), and |s|= 3p+ 3 > p. We show
that no matter how sis decomposed into three parts, at least one of the three conditions in
the pumping lemma fails to hold, thus the language is nonregular.
Write s=xyz, where y6=εand |xy| ≤ p. Since sstarts with p0’s and |xy| ≤ p,xand ycan
consists of only 0’s; and since y6=ε,yconsists of at least one 0. Thus xy2z= 0q10p10p1 where
q > p, and hence xy2z6∈ L1, contradicting the pumping lemma. Therefore L1is nonregular.
2
•The languages in Parts (a), (c) and (d) of Problem 1.46 on Page 90 of Sipser.
–L2={0n1m0n:m, n ≥0}.
Solution: We prove that L2is nonregular by the pumping lemma. Let pbe any given
positive integer, and let s= 0p10p. Clearly s∈L2(with n=pand m= 1), and
|s|= 2p+ 1 > p. Write s=xyz, where y6=εand |xy| ≤ p. Since sstarts with p0’s
and |xy| ≤ p, just as above yconsists of only 0’s and at least one 0. Thus xy2z= 0q10p
where q > p, and hence xy2z6∈ L2, contradicting the pumping lemma. Therefore L2is
nonregular.
2
–L3={w∈ {0,1}∗:wis not a palindrome}.
Solution: If L3were regular, then by the closure under complementation its complement
L3would also be regular. We show that L3is nonregular, from which it follows that L3
is nonregular.
We use the pumping lemma to show that L3, the language consisting of all palindromes
over {0,1}∗, is nonregular. Let pbe any given positive integer, and let s= 0p110p.
Clearly s∈L3(as sis a palindrome), and |s|= 2p+ 2 > p. Write s=xyz, where y6=ε
and |xy| ≤ p. Since sstarts with p0’s and |xy| ≤ p, just as above yconsists of only 0’s
and at least one 0. Thus xy2z= 0q110pwhere q > p, and hence xy2z6∈ L3(as it is not
a palindrome), contradicting the pumping lemma. Therefore L3is nonregular.
2
–L4=nwtw :w, t ∈ {0,1}+o.
Solution: We prove that L4is nonregular by the pumping lemma. Let pbe any given
positive integer, and let s= 0p110p1. Clearly s∈L4(with w= 0p1 and t= 1),
and |s|= 2p+ 3 > p. Write s=xyz, where y6=εand |xy| ≤ p. Since sstarts
with p0’s and |xy| ≤ p, just as above yconsists of only 0’s and at least one 0. Thus
1
pf3
pf4

Partial preview of the text

Download Assignment 3 Problems Solutions - Languages and Computation | CS 3240 and more Assignments Computer Science in PDF only on Docsity!

CS 3240: Languages and Computation

Problem Set 3 Solutions

Problem 1

Show that the following languages are not regular.

  • L 1 = {www : w ∈ {a, b}∗}. Solution: We prove that L 1 is nonregular by the pumping lemma. Let p be any given positive integer, and let s = 0p 10 p 10 p1. Clearly s ∈ L 1 (with w = 0p1), and |s| = 3p + 3 > p. We show that no matter how s is decomposed into three parts, at least one of the three conditions in the pumping lemma fails to hold, thus the language is nonregular. Write s = xyz, where y 6 = ε and |xy| ≤ p. Since s starts with p 0’s and |xy| ≤ p, x and y can consists of only 0’s; and since y 6 = ε, y consists of at least one 0. Thus xy^2 z = 0q 10 p 10 p1 where q > p, and hence xy^2 z 6 ∈ L 1 , contradicting the pumping lemma. Therefore L 1 is nonregular. 2
  • The languages in Parts (a), (c) and (d) of Problem 1.46 on Page 90 of Sipser.
    • L 2 = { 0 n 1 m 0 n^ : m, n ≥ 0 }. Solution: We prove that L 2 is nonregular by the pumping lemma. Let p be any given positive integer, and let s = 0p 10 p. Clearly s ∈ L 2 (with n = p and m = 1), and |s| = 2p + 1 > p. Write s = xyz, where y 6 = ε and |xy| ≤ p. Since s starts with p 0’s and |xy| ≤ p, just as above y consists of only 0’s and at least one 0. Thus xy^2 z = 0q 10 p where q > p, and hence xy^2 z 6 ∈ L 2 , contradicting the pumping lemma. Therefore L 2 is nonregular. 2
    • L 3 = {w ∈ { 0 , 1 }∗^ : w is not a palindrome}. Solution: If L 3 were regular, then by the closure under complementation its complement L 3 would also be regular. We show that L 3 is nonregular, from which it follows that L 3 is nonregular. We use the pumping lemma to show that L 3 , the language consisting of all palindromes over { 0 , 1 }∗, is nonregular. Let p be any given positive integer, and let s = 0p 110 p. Clearly s ∈ L 3 (as s is a palindrome), and |s| = 2p + 2 > p. Write s = xyz, where y 6 = ε and |xy| ≤ p. Since s starts with p 0’s and |xy| ≤ p, just as above y consists of only 0’s and at least one 0. Thus xy^2 z = 0q 110 p^ where q > p, and hence xy^2 z 6 ∈ L 3 (as it is not a palindrome), contradicting the pumping lemma. Therefore L 3 is nonregular. 2
    • L 4 =

{ wtw : w, t ∈ { 0 , 1 }+

} . Solution: We prove that L 4 is nonregular by the pumping lemma. Let p be any given positive integer, and let s = 0p 110 p1. Clearly s ∈ L 4 (with w = 0p1 and t = 1), and |s| = 2p + 3 > p. Write s = xyz, where y 6 = ε and |xy| ≤ p. Since s starts with p 0’s and |xy| ≤ p, just as above y consists of only 0’s and at least one 0. Thus

xy^2 z = 0q 110 p1 where q > p. We claim that xy^2 z can not be written in the form wtw, where w, t ∈ { 0 , 1 }+. Write x^2 z = utv, where u, t, v are all nonempty. If the middle substring t contains the first 1 in xy^2 z, then u would end with 0 and v would end with 1, thus u 6 = v. If t does not contain the first 1 in xy^2 z, then |u| ≥ q + 1 and |v| ≤ p + 1, and since q > p, |u| > |v| and hence u 6 = u. Therefore xy^2 z 6 ∈ L 4 , and thus L 4 is nonregular. 2

Problem 2

Problem 1.31 on Page 88 of Sipser. Show that regular languages are closed under reversal, that is, if A is regular then so is AR.

Solution: Let A be any regular language, and let M be a DFA recognizing A. To show that AR is regular, we construct an NFA N that recognizes AR, which we describe as follows. In N we keep all the states of M and reverse the direction of all the transition arrows in N. We set the accept state of N to be the start state of M. We also introduce a new state q 0 as the start state of N which goes to every accept state of M by an ε-transition.

Claim 1 AR^ ⊆ L(N ).

Proof: For every w ∈ A = L(M ), the path that w follows in M starts at the start state of M and ends at one of M ’s accept states. If we input wR^ to N , it starts at the new start state q 0 and forks into different paths starting at each of the accept state of M. The computation can reach the accept state of N (that is, the start state of M ) by following the same path as w in M but in the opposite direction. Thus wR^ ∈ L(N ). 2

Claim 2 L(N ) ⊆ AR.

Proof: For every x ∈ L(N ), there is a path P starting at q 0 , going through some accept state of M , say qa, and stopping at the unique accept state of N (that is, the start state of M ). If we input xR^ to M , we can go from M ’s start state to qa in M by following the path P in the opposite direction. Thus x ∈ AR. 2

Therefore, L(N ) = AR^ and AR^ is regular. 2

Problem 3

Problem 1.48 on Page 90 of Sipser.

Solution: We observe that a string w over { 0 , 1 }∗^ contains an even number of occurrences of 01 and 10 if and only if w starts and ends with the same symbol. (Prove this.) Therefore the language D is described by the regular expression 0Σ∗ 0 ∪ 1Σ∗1, and hence regular. 2

Problem 4

Give context-free grammars that generate the following languages.

Problem 5

Problem 2.19 on Page 130 of Sipser. For extra credit, give a rigorous proof of your claims.

Solution: The given grammar G describes exactly the language consisting of all strings not of the form anbn. (Prove this by induction.) Thus L(G) = {anbn^ : n ≥ 0 }, and is generated by the grammar

S → aSb | ε.

2

Problem 6 (Extra Credit)

Parts (b) and (d) of Problem 2.6 on Page 129 of Sipser.

  • The complement of {anbn^ : n ≥ 0 }. Solution: The grammar in Problem 5. (I meant to assign a different problem here but mistakenly assigned one.) 2

{ x 1 # · · · #xk : k ≥ 1, xi ∈ {a, b}k, and xi = xRj for some i and j

} . Solution:

S → U P V P → aP a | bP b | T | ε T → #M T | # U → M #U | ε V → #M V | ε M → aM | bM | ε

2