Professional Documents
Culture Documents
Homework 3
1. (20 points) Use the pumping lemma to prove that each language listed below is not regular. For each language,
I state the input alphabet.
(a) A1 = {w | the number of zeros in w is less than the number of ones}. = {0, 1}.
For example, 1, 011, and 10100111 are in this language but 0 and 100 are not.
Solution:
Let p be a proposed pumping lemma constant, and let w = 0p 1p+1 A1 . For any xyz = w with
1 |y| |xy| p, y 0+ . Therefore, xy 2 z = 0p+|y| 1p+1 has at least as many 0s as 1s, and therefore is
not in A1 . It follows by the pumping lemma that A1 is not regular.
n
(b) A2 = 12 . = {1}.
For example, 1, 11 and 11111111 are in this language but 111 is not.
Solution:
p
Let p be a proposed pumping lemma constant, and let w = 12 A2 . For any xyz = w with |xy| p <
p
2
2p +|y|
p
2 , xy z = 1
has length that is greater than 2 and less than 2p+1 , i.e. 2p < 2p + |y| < 2p+1 , which
holds since 1 |y| p < 2p . It follows by the pumping lemma that A2 is not regular.
2. (20 points) Let
3
0
0
1
0
0 , 0 , 1 , 1 .
=
0
1
0
1
3 contains all size 3 columns of 0s and 1s. A string of symbols in 3 gives three rows of 0s and 1s. Consider
each row to be a binary number with the most significant bit first. For example, let
0
1
1
1
w = 0 1 0 1 .
1
1
0
0
The first row of w is the binary representation of 7, the second row corresponds to 5, and the third row corresponds to 12.
Let
B+
D
B
q1
q0
C
R
Solution 1: Ill first present a NFA that recognize the reverse of B+ , B+
. Figure 1 shows the NFA with
1
0
0
0 , 0 , 1 ,
A =
0
1
1
1
1 ,
=
0
0 ,
=
0
1
1
1 , 1 , 0 .
=
1
0
0
This NFA basically checks the pencil-and-paper method for addition starting from the least-significant bit
and working to the most signficant bit. The machine is in state q0 when the previous bit did2 not 3generate
0
7
6
7
a carry, and state q0 when the previous bit does generate a carry. For example, the symbol 6
4 0 5 is in A
0
2
3
1
6
7
7
because 0 + 0 = 0 with no carry in or carry out. Likewise, 6
4 1 5 is in A because 1 + 1 produces a sum
0
of 0 and a carry to the next bit of 1 when the carry in is 0. The symbols for which a state does not have an
outgoing edge correspond to errors, which the NFA rejects.
R
The language B+
is regular because the NFA presented above recognizes it. As shown on homework 2,
the regular languages are closed under reversal. Thus, B+ is regular as well.
Solution 2: This time, we construct an NFA the processes the string from left-to-right. The approach is very
similar to the right-to-left DFA; the only difference is that the states of the left-to-right DFA keep track
of whether or not a carry is expected from the next less significant bit. In fact, the two machines are so
similar, that we can use the same transition diagram with just a slight change to the labels.
The NFA shown in Figure 1 recognizes B+ with labels A and D as before, and exchanging the definitions
of sets B and C:
1
1 ,
B =
0
0 ,
C =
1
3. (20 points) Let 3 be defined as in question 2. Let
B
p1
p
0
0
1
0
0 0
1 0 .
1
0
0
0
This is a member of B , because the first two rows correspond to integers 2p , and 2p 2p = 22p which
is an interger corresponding to the last row. For any xyz = w with |xy| p, xy 2 z leaves the value of the
first two rows unchanged, but changes the value of the last row, i.e. the last row represents some integer
that is not 22p . Therefore xy 2 z
/ B and B is not regular by the pumping lemma.
4. (20 points) Consider the two languages described below:
C1 = {w {a, b} | x, y . (w = xy) #a(x) = #b(y)}
C2 = {w {a, b, c} | x, y . (w = x c y) #a(x) = #b(y)}
One of these languages is regular and the other is not. Determine which is which and give short proofs for your
conclusions.
Solution:
C1 is regular, and has the regular expression , where is the alphabet. We prove that all strings are in C1 by
induction:
Base case: s = Let x = y = . Then xy = = = s and #a(x) = #b(y) = 0.
Induction step: s = w c By the induction hypothesis, w C1 ; thus we can find strings x and y with xy = w
and #a(x) = #b(y). If c 6= b, then let y = yc and let x = x. Then w c = x y and
#a(x ) = #a(x) = #b(y) = #b(y )
Thus, s = w c C1 as required.
If c = b and y = , then #a(x) = #b(y) = 0. Choosing x = w c and y = satisfies wc = x y and
#a(x ) = #a(x) = 0 = #b(y) = #b(y )
Thus, s = w c C1 as required
If c = b and y 6= , let y = d v for d and v . If d = a, choose x = x a and y = v b. We
have w c = x y and
#a(x ) = #a(x) + 1 = #b(y) + 1 = #b(y )
Thus, s = w c C1 as required.
If d = b, choose x = x b and y = v b. We have w c = x y and
#a(x ) = #a(x) = #b(y) = #b(y )
Again, s = w c C1 as required.
C2 is not regular.
Let p be a proposed pumping lemma constant, and let w = ap cbp C2 . For any xyz = w with |xy| p
and |y| 1, xy 0 z = ap|y| cbp has fewer as than bs (and contains only one c), and therefore is not in C2 . It
follows by the pumping lemma that C2 is not regular.
5. (30 points, from Sipser, problem 2.6)
Give context free grammars generating the following languages:
(a) (10 points) {w | n 0. (w = an b2n ) (w = a3n bn )}
Solution:
S
T2b
T3a
T2b | T3a
aT1 bb
aaaT2 b
Solution:
S
Ta
Tb
T0
Tba
Tx
Ta | Tb | Tba
aT0 | aTa
T0 b | Tb b
aT0 b |
Tx baTx
aTx | bTx |
Heres how it works. In the first step, the derivation chooses whether it will generate a string with
more as than bs (thats what Ta generates), a string with more bs than as (thats what Tb generates),
or a string that has a b before an a (thats what Tba does).
Variable T0 generates strings of the form an bn . Variable Ta generates strings that have one or more
as preceeding a string generated by T0 ; in other words, these are strings of the form an bm with
n > m. Likewise, Tb generates strings of the form an bm with n < m. Variable Tx generates any
string in (assuming = {a, b}), and Tba uses Tx to generate all strings in ba .
(c) (10 points) {x1 cx2 c xk | each xi {a, b} , and for some i and j, xi = xR
j }.
Solution:
S
M
L
R
Tx
LM R
aM a | bM b | cL
Tx cL |
RcTx |
aTx | bTx |
A derivation creates a left part (L), a middle part (M ) and a right part (R). The middle part
generates strings of the form xi cxi+1 c cxR
i , and the left and right parts generate the rest of the
string.
In particular, L generates strings of the form ((a b) c) , and R generates strings of the form
(c(a b) ) . Note that an equivalent regular expression for L is ( c) Thus, cL generates c( c) )
which matches the string between xi and xj : cxi+1 cxi+1 c c.
For parts (a) and (b), the alphabet is {a, b}. For part (c), the alphabet is {a, b, c}.
6. (20 points, Extra Credit) Consider the language below from the September 22 lecture notes:
=
A =
{a, b, c}
(aa c)n (bb c)n cc
(a) (10 points) Prove that A satisfies the conditions of the pumping lemma as stated in Sipser or the September
22 notes. In other words, show that you can find a constant p > 0 such that for any string w A with
|w| > p, you can find strings x, y and z such that w = xyz and xy i z A for any i 0.
Solution: Let p = 3, and let w A with |w| p. We consider five cases according to the first few
symbols of w:
w = aau for some u :
Let x = , y = a and z = au. Thus, xy i z = ai+1 u. If w (aa c)n (bb c)n then,
a
i+1
u
u