You are on page 1of 4

ToC (Summer-05), DFA Workbook

2.2

DFA that accepts strings that BEGIN with 110

Intro

This would be a similar one, except that now the accepting state goes to itself on all inputs (since once we determine the string starts with 110, whatever else it has doesnt matter).
1 1 0

To help you get some practice designing DFAs, here are some solved exercises. You will also nd unsolved ones you may want to try on your own.

q0

q1

q2

q3

0,1

2
2.1

Variations on 110 ( = {0, 1})


DFA that accepts ONLY the string 110

0 d

1 0,1

As a rst approximation, youd probably do something like this:


q0 1 q1 1 q2 0 q3

Figure 3: DFA that accepts strings that begin with 110 And its transition table is:

Figure 1: DFA that accepts only 110 (incomplete) However, for a DFA we have to make sure there is one and only one transition for each pair of symbol and state; here, if we stray anywher from the string 110, we cannot accept, so we can introduce a dead state, that is NOT accepting, and goes to itself on every input (so it will never accept). Then, every transition that leads us away from 110 would go to this dead state. The nal solution would look like this:

q0 q1 q2 q3 d

0 q1 d q3 q3 d

1 d q2 d q3 d

2.3

DFA that accepts strings that CONTAIN 110

q0

q1

q2

q3

This can be thought as modifying the automata for BEGINS with 110, eliminating the dead state (since now from any state you can go to accept if you see a 110) and making the transitions that went to dead go back into some previous state. For q0 and q1 , that state is q0 , but for q2 , we go back to q2 (if weve seen 11, and we see another one, we have now 111, which for our purposes is the same as 11).

0 d

1 0,1

0,1
0
1 1

1
0

0,1

q0 0

q1

q2

q3

Figure 2: DFA that accepts only 110 And its transition table is: 0 q1 d q3 d d 1 d q2 d d d 1

Figure 4: DFA that accepts strings that contain 110 And its transition table is: q0 q1 q2 q3 0 q1 q0 q3 q3 1 q0 q2 q2 q3

q0 q1 q2 q3 d

2.4

DFA that accepts strings that END with 110

q0

q1

q2

q3

a,b,c

For this one we can modify the automata for CONTAINS 110, we only need to modify what happens for the accepting state. Before, we just stay there, but now you have to go back, since if you ended with 110, and you see another symbol, you dont end with 110 anymore.

b,c d

a,c

a,b a,b,c

0 1 0
1 1

Figure 6: DFA that accepts strings that begin with abc a q1 d d q3 d b d q2 d q3 d c d d q3 q3 d

1
0

q0 0

q1

q2

q3

q0 q1 q2 q3 d

3.2
Figure 5: DFA that accepts strings that end with 110 And its transition table is: 0 q1 q0 q3 q0 1 q0 q2 q2 q1

Additional Exercises

You can try the other variations (only, contain, end) with this string and alphabet, or just invent new strings and alphabets. If you nd yourself getting attached to an alphabet, try some of the other exercises with dierent alphabets.

q0 q1 q2 q3

Counting (and modulo)

2.5

Additional exercises

You can try repeating the above exercises (only, begin, contain,end) with other strings you fancy. A good one to try could be 1010

Oftentimes we need to count the length, or the number of specic symbols in a string (or other kinds of information). If we were counting, we would need an innite number of states (since there can be an innite number of symbols), however, we usually count modulo a number (that is, when we reach a number, we go back). This allows us to check things like even/odd and divisibility.

4.1

Even length, = {x, y }

Dierent Alphabet

Here we are counting the length, so any symbol will cause a change of state. Since will be accepted, q0 is accepting.

Most of the times our alphabet, , is the set {0,1}, or maybe {a,b}, just to change it slightly :). This is by convention, same reason variables are x and y in Math, or loop variables are i,j in programming. Some people get confused when the alphabet changes (I can do the problem with oranges, but not with apples :), so just to get everybody accustomed to a dierent alphabet, lets try one (also, I will keep changing the alphabet for other problems):

q0

x,y x,y

q1

3.1

begins with abc, = {a, b, c}

Figure 7: DFA that accepts strings that have even length

And its transition table is: 2

4.2

Odd number of as = {a, b, c}

5.1

Here we are only interested in the number of as, so only as cause a change of state. Any other symbol (b,c) would maintain the same state. Since we are looking for an odd number of as, is NOT accepted.

Number of 1s NOT divisible by 3, = {0, 1}

As an example, we will negate the previous exercise. Now we want to accept strings with a number of 1s that is NOT divisible by 3. We just reverse the acceptability of all states.

b,c q0 a a

b,c
0 1 q0

0 0 q1 1 1 q2

q1

Figure 8: DFA that accepts strings that have an odd number of as

Figure 10: DFA that accepts strings with a number of 1s thats NOT divisible by 3

4.3

Number of 1s divisible by 3, = {0, 1}


is ac-

Here we are counting modulo 3 (so we go 0,1,2). cepted.

Combining Automatas

0 0 1 q0 q1 1 1 q2 0

You can combine automatas easily by thinking about the states as being pairs now, so youd have q0 ,q0 , as the rst automata in q0 and the second in q0 (so this is the start state) and going from there. You can of course change the name of the state, to something like q00 , and I usually do. You can make this sistematically, by combining their transition tables. The number of states is the product of the number of states for the individual DFAs; however, oftentimes there will be states that are not valid. If you make the transition table, it is not actually wrong to leave those states in. If you make a transition diagram, you can easily eliminate states if they are not joined with the main group.

6.1
Figure 9: DFA that accepts strings with a number of 1s thats divisible by 3

DFA that accepts strings with an Even number of 1s and an odd number of 0s
0 q0 q1 1 q1 q0

The DFA that accepts an even number of 1s is:

4.4

Additional Exercises

Here you can try with dierent alphabets, and counting different things.

q0 q1

And the one for an odd number of 0s: q0 q 1 0 q1 q0 1 q0 q1

Negating DFAs

To negate a DFA (make it accept the strings it didnt and viceversa), you only need to change all accepting states to non-accepting and viceversa. Notice this only works with DFAs, and not with other kinds of automata. 3

Now we can combine them, we will name our states like this: q1,0 , meaning q1 on the rst automata (even 1s) and q0 on the second one (odd 0s), and so on. We start on

q0,0 and we can get the right transitions by combining the transitions on both automata. For example, from q0,0 on seeing a 0, we go to q0,1 , since on the rst automata we go from q0 to q0 on seeing a 0, and on the second automata we go to q1 from q0 on seeing a 0. In this case, we want strings that are accepted on BOTH the rst and second original automatas, so we make accepting only the states that correspond to accepting states on both. We could want to accept strings accepted by EITHER automata; in that case we would change acceptance accordingly. 0 q0,0 q0,1 q1,0 q1,1 q0,1 q0,0 q1,1 q1,0 1 q1,0 q1,1 q0,0 q0,1

a
a b c

q0

q1

q2

q3

Figure 12: NFA that accepts any strings that END in the string abc. Compare with similar DFA in gure 2.4

0,1
1 0,1 0,1

q0

q1

q2

q3

6.2

Additional Exercises

Figure 13: NFA that accepts any strings whose 3rd symbol from the right is 1. Try it with DFA ! 2. Design a DFA that accepts any string that contains both two consecutive as and three consecutive bs 3. Design a DFA ( = a, b, c) that accepts any string with a length which is divisible by 3. Does non-determinism help ? 4. Design a DFA ( = a, b) that accepts all strings that start with an a and end with a b

Try to design an automata that accepts both the strings 110 and 101 and no other.

Additional unsolved exercises

1. Design a DFA that accepts any string that does NOT contain 110 2. Design a DFA that accepts any string that contains both two consecutive as and three consecutive bs 3. Design a DFA ( = a, b, c) that accepts any string with a length which is divisible by 3 4. Design a DFA ( = a, b) that accepts all strings that start with an a and end with a b

Example NFAs
a,b,c
a b c

a,b,c

q0

q1

q2

q3

Figure 11: NFA that accepts any strings that CONTAIN the string abc. Compare with similar DFA in gure 2.3

8.1

Others

Try to use non-determinism as much as possible. Compare with DFA 1. Design a DFA that accepts any string that does NOT contain 110 4

You might also like