Вы находитесь на странице: 1из 7

CSCI3255: Math Foundations of CS

Homework 2, (2.1), page 1

September 2006

Chapter 2, Section 1. Problems 1-15


1. Which of the following strings 0001, 01001, 0000110 are accepted by the dfa in
Figure 2.1?
0
1
0
0
q0

q1

q2

Just follow the bouncing ball. Starting at the initial state, just follow the arrows and see
where you wind up. 0001 leaves us in state q1, 01001 leaves us in state q1, and 0000110
leaves us in state q2. So the first two are accepted and the last one is rejected.
2.

For = {a, b} construct dfas that accepts the sets consisting of


a. all strings with exactly one a.
As done in class:
a,b
b
b
a

q0

q1

q2

b. all strings with at least one a.


b
a, b
q0

q1

c. all strings with no more than three as. (notice how the states indicate # of
as).
b
b
b
b
a,b
a
a
a
a
q2
q0
q3
q1
q4+
d. all strings with at least one a and exactly two bs.
When building a DFA, remember that the states have meaning. This language
has two possibilities for as: no as or at least one a. It has four possibilities for
bs: no bs, 1 b, 2 bs or more than two bs. So there are two times four, or eight
states. Ill label the states to point out their meaning. q00: no as or bs, q10: at
least one a but no bs, q01: no as and 1 b, etc.
b

q00
a

q01

b
a

q11
a

q02+

a
q10

q02

q12
a

q12+

a,b

CSCI3255: Math Foundations of CS

Homework 2, (2.1), page 2

September 2006

e. all the strings with exactly two as and more than two bs.
q00

b
a

q11

b
a

q02
b
q02+

q12

q21

q12+

q2+1
b

b
a

q22

q2+0
b

q20
b

q01

q10

q22+

q2+2
b
q2+2++

a,b

3. Show that if we change Figure 2.6, making q3 a nonfinal state and making q0, q1, q2
final states,L the resulting dfa accepts
. This will be true for any dfa. If we switch the final and
non-final (non-accepting) states, then any string that leaves us in an accepting state in the original now
leaves us in a non-final state (and vice-versa) because we have switched around which of the states is a final
state. By definition then, the language accepted by the new machine is the complement of the language
accepted by the original one.
4. Generalize the statement in exercise 3.
We just did!
5. Give dfas for the languages
a. L = {ab5wb2 : w {a, b}*}
a

b
a

b
a

b
b

a,b
b. L = { abnam : n 2, m 3}
a
b
a,b

b
a

b
a

a
a,b

a
b

a
b

note: I used two trap


states just to make the
picture clearer.

CSCI3255: Math Foundations of CS


Homework 2, (2.1), page 3
c. L = {w1abw2 : w1 {a, b}*, w2 {a, b}*}
This is any string containing ab somewhere in it.
b
a
a,b
b
a

September 2006

7. Find dfas for the following languages on = {a, b}


a. L = {w : |w| mod 3 = 0}
a,b
The state number represents the
0
1
number of symbols so far, modulo
3.
a,b
a,b
2
b. L = {w : |w| mod 5 0}
1

0 a,b

a,b
2

a,b
4

a,b

a,b

c. L = {w : na(w) mod 3 > 1}


b
b
a
1
0
a

The state number represents the


number of symbols so far, modulo
5.

The state number represents the


number of as so far, modulo 3.
The bs are ignored.

a
b

d. L = {w : na(w) mod 3 > nb(w) mod 3}


a
a
20
10
00
b a
b
b
The state number ab represents
a
a
the number of as and the number
11
b
01
21
b of bs so far, modulo 3. The
b
b a
b
b
accepting states are the ones
a
where the first subscript is greater
a
12
02
22
than the second one!
a
e. L = {w : (na(w) nb(w)) mod 3 > 0} Change accepting states of part d. to
10, 20, 01, 21,
02 and 12.
f. L = {w : (na(w) + 2nb(w)) mod 3 < 2}
As b's go in the sequence: 0, 1, 2, 0, ...
2b goes in the sequence: 0, 2, 1, 0,...

CSCI3255: Math Foundations of CS


Homework 2, (2.1), page 4
September 2006
So change the label 01 with 02, 11 with 12, 21 with 22, 02 with 01, 12 with 11
and 22 with 21.
Change accepting states of part d. to 00, 10 and 01

8. A run in a string is a substring of length at least two, as long as possible and


consisting entirely of the same symbol. Find dfas for the following languages over
{a, b}:
a. L = {w: w contains no runs of length less than four}

aa

aaa

b
b

b
b

trap

a,b
bb

bbb

aaaa

bbbb

b. L = {w: every run of as has a length of either two or three}


only when we get to
four consecutive as do
we reject, and once we
a
a,b
do, theres no going
b
a
a
a
back (trap state)
b

c. L = {w : there are at most two runs of as of length three}


a,b

b
a

0
b
b

a
a
a

b
b

2+

a
b

a
State 0: no runs, State 1: one run, State 2: two
runs, State 3: more than two
runs (reject)

CSCI3255: Math Foundations of CS

Homework 2, (2.1), page 5

September 2006

d. L = {w : there are exactly two runs of as of length three}


Same as in part c, but only state 2 and the previously accepting states after it
would now be accepting.
9. Consider the sets of strings on {0, 1} defined by the requirements below. Construct
dfas.
a. Every 00 is followed immediately by a 1
1

0,1

1
1

0,1

b. all strings containing 00 but not 000


1

c. The leftmost symbol differs from the rightmost


0
1
1
0
0
0
1
0
1
1
d. Every substring of four symbols has at most two 0s

0
0

000

0000

0,10,
1
0,10,
1

01

001

00

010

10

011

100

1
1

0
101

11

110

111

1
0001

0010

0011

0100

0101

0110

0111

1000

1001

INCOMPLETE. You can fill


in the rest!

1010

1011

1100

1101

1110

1
1111

CSCI3255: Math Foundations of CS


Homework 2, (2.1), page 6
September 2006
I stopped drawing all the transitions because it would have looked like a real mess.
The idea is that for the first four symbols of a string we remember those symbols by
going to an appropriate state. For up to the first three symbols, we accept since there
are no runs of length four to consider. Some of the fourth level states are accepting;
some are not. Once we go into a non-accepting state, we stay there (trap state) because
the string can no longer be accepted. For the fourth level accepting state, we perform a
transition to a state whose label consists of the last three symbols of the accepting state
followed by the symbol just read.
e. All strings of length five or more in which the fourth symbol from the right end is
different than
the leftmost symbol.
f. All strings in which the leftmost two symbols and the righmost two symbols are
identical.
g. All strings of length four or greater in which the leftmost 3 symbols are the
same, but different from the rightmost symbol.
I have abandoned doing e. f. and g. because there are not enough hours in the day! If
youve managed any of these, congratulations!
10. Construct a dfa that accepts strings on {0, 1} if and only if the value of the string,
viewed as the binary representation of a number, is equal to zero modulo 5.
1
0

0
1

1
2

11. Show that the language L = {vwv: v, w {a,b}*. |v| = 2} is regular.


a.b a
a
a
Sometimes its just a whole lot
a.b
a
b
a
easier with an nfa!
b
a.b
a
a
b
b
a.b
b
b
b
12. Show that L = {an: n 4} is regular.
a

13. Show that the language L = {an: n 0, n 4} is regular.


a

CSCI3255: Math Foundations of CS

Homework 2, (2.1), page 7

September 2006

14. Show that the language L = {an: n is either a multiple of three or a multiple of 5} is
regular.
a
a
a
a
a
a
4
1
5
6
2
7
a
3
a
0
8
a
a
a
a
a
a
a
10
13
11
14
9
12
Since 15 is the least common multiple of 3 and 5, we can do modulo 15 arithmetic with
the accepting states as shown above.
15. Show that the language L = {an: n is a multiple of three but not a multiple of 5} is
regular.
Same picture as 14, but states 3, 6, 9 and 12 are the only accepting states.

Вам также может понравиться