Remainder Machines

advertisement
Remainder Machines
Michael Glass
September, 1993
What does it do?
Consider the following deterministic finite state machine, over the alphabet  = {0,
1}.
>a
b
c
0
a
c
b
1
b
a
c
This machine has three states, a, b, and c, where state a is initial (marked with
“>”) and states b and c are final (marked with “”).
Let us think about how this machine works. First we observe that when our
input string starts with a leading “0” symbol, this symbol causes a transition from the
initial state (a) back into the initial state. Leading zeros are thus eaten up, and the first
interesting transition happens when the first “1” symbol is encountered. Let us
tabulate how the machine behaves given a few strings which start with “1”.
input
string
1
10
11
100
101
110
111
1000
1001
100010
100011
100100
100101
final
state
b
c
a
b
c
a
b
c
a
b
c
a
b
accepted
Y
Y
N
Y
Y
N
Y
Y
N
Y
Y
N
Y
numeric
value
1
2
3
4
5
6
7
8
9
34
35
36
37
mod 3
remainder
1
2
0
1
2
0
1
2
0
1
2
0
1
Inspecting the columns “numeric value” and “mod 3 remainder” will reveal
what is going on here. The strings accepted by this machine represent binary numbers
which are not multiples of three! You are invited to test this hypothesis on any string
of your choice over the alphabet  = {0, 1}. Be sure to include some strings with
leading zeros.
This machine seems quite remarkable. If I gave you a long binary number and
asked you if it were a multiple of three, your first approach might be to divide by
three and examine the remainder. Dividing long binary numbers by three is a rather
2
cumbersome operation.
accomplish this feat.
You wouldn’t expect a tiny three-state automaton to
How does it do it?
In fact, our machine is not dividing by three--it is merely determining the remainder.
If you inspect the test strings a bit more closely, you will discover that states a, b, and
c are for remainders 0, 1, and 2, respectively. By picking states b and c for our final
states, we have accepted strings which have nonzero remainders.
Constructing the three state machine is quite easy. We will start with the three
states a, b, and c, representing the remainders 0, 1, and 2. Initially, our transition
diagram is a mystery, like this:
a
b
c
0
?
?
?
1
?
?
?
0-remainder
1-remainder
2-remainder
Let us assume, for the moment, that we are in the middle of processing a string
of binary digits. The digits we have already processed (to the left) represent some
number x, and there are some more digits remaining to the right, something like this:
1
x
For example, after we have processed the first four digits of the string
“100110” the situation looks like this:
1001 10
9
These first four digits, “1001”, represent the number 9. If there were no more
digits to the right the machine would stop right here. In that case, since 9 mod 3 = 0,
we would want stop in state a, the 0-remainder state. Therefore after processing the
prefix “1001” the machine should be in state a. This should be true for any prefix x
which is an exact multiple of three.
The next digit in our example is a “1”. After processing this “1” the situation
looks like this:
10011 0
19
Since 19 mod 3 = 1, we would expect now to be in state b. Processing the
symbol “1” led to a transition from state a to state b. Is this always true? If the
machine is in state a, and the next input symbol is “1”, should the machine always
transit to state b?
3
Let us consider the general case, assuming that the next digit to process is a
“1”:
1   1   
x
Which yields the following after the “1” is processed:
1   1   
2x+1
We suppose that after processing some digits, the machine is in state a. State a
is the 0-remainder state, meaning it has scanned (so far) a numeric value x such that x
mod 3 = 0. After it processes the next digit, a “1”, the numeric value scanned so far
becomes 2x + 1. Since x mod 3 = 0, it follows that 2x + 1 mod 3 = 1, and the machine
should now be in state b.
We conclude that if the machine is in state a (remainder so far is 0), and the
next symbol is a “1”, the machine should always change to state b (remainder so far is
1). We can write that into our transition diagram:
a
b
c
0
?
?
?
1
b
?
?
if x mod 3 = 0, then 2x + 1 mod 3 = 1
Suppose that the machine is already in state b, and the next digit is a “0.” We
can continue with the example string “100110” above:
10011 0
19
which becomes:
100110
38
After processing “10011” the machine should be in state b, because 19 mod 3 = 1.
After processing the following “0” symbol, the numeric value becomes 38. Since 38
mod 3 = 2, we expect the machine to be in state c. It appears that when the machine is
in state b scanning “0” should cause a transition to state c. Is this always true?
When the next digit is “0” the general case looks like this:
1   0   
x
4
After the “0” is scanned, the numeric value doubles:
1   0   
2x
Assume the partially scanned string leaves the machine in state b, meaning the value x
scanned so far has remainder 1. When the next symbol is a “0” the value doubles, so
x mod 3 = 1 becomes 2x mod 3 = 2. After scanning the “0” the machine should
change to state c. This will always be true. If the machine is in state b, meaning the
current remainder is 1, then scanning a “0” should cause a transition to state c,
meaning the remainder is 2. We can put this in our transition diagram:
a
b
c
0
?
c
?
1
b
?
?
if x mod 3 = 1, then 2x mod 3 = 2
In general, adding a “0” to an existing binary string representing x will always
double the numeric value to 2x, allowing us to add two more transitions immediately:
a
b
c
0
a
c
b
1
b
?
?
if x mod 3 = 0, then 2x mod 3 = 0
if x mod 3 = 2, then 2x mod 3 = 1
Since since w showed earlier that adding a “1” to an existing binary string
representing some number x will form the string for the number 2x + 1, we can now
complete the transition function:
a
b
c
0
a
c
b
1
b
a
c
if x mod 3 = 1, then 2x + 1 mod 3 = 0
if x mod 3 = 2, then 2x + 1 mod 3 = 2
Our reasoning so far has been by induction. We assume the machine has
already scanned n symbols, representing a total numeric value of x, and then it scans
the n + 1-st symbol, producing a numeric value of either 2x or 2x + 1. We have not
yet picked an initial state representing the condition before the n = 1 first symbol has
been scanned.
The one-character string “0” represents a numeric value of 0, and the string
“1” represents a numeric value of 1. If, before the first symbol has been scanned, we
set our initial value x = 0, we will be quite happy. In this case, scanning a “0” yields
the value 2  0 = 0 and scanning a “1” yields 2  1 + 1 = 1.
Examining our machine, we see we want to start in state a. If the first symbol
is “0” the machine will stay in state a (0-remainder) as desired, and if the first
5
symbols is “1” it changes to state b (1-remainder) as desired. Therefore picking state
a for the initial state permits us to correctly scan the first symbol.
>a
b
c
0
a
c
b
1
b
a
c
if x mod 3 = 1, then 2x + 1 mod 3 = 0
if x mod 3 = 2, then 2x + 1 mod 3 = 2
The rest of the symbols follow the state transitions as outlined above. The
machine correctly scans one symbol, and by induction the transition table shows how
to scan the remaining symbols.
Since we ultimately want to accept only those strings which represent numbers
with non-zero remainders, we mark the non-zero-remainder states as final, yielding
our machine as originally presented:
>a
b
c
0
a
c
b
1
b
a
c
if x mod 3 = 1, then 2x + 1 mod 3 = 0
if x mod 3 = 2, then 2x + 1 mod 3 = 2
Exercises
1. Build a remainder mod 5 machine for binary strings. The alphabet  = {0, 1}. In
this case, you are to accept binary strings representing numbers which are exactly
divisible by 5. Some strings in this language are “000,” “1010,” “11110,” and .
2. Build a remainder mod 4 machine for ternary (“base 3”) numbers. The alphabet
 = {0, 1, 2}. Accept strings representing numbers which are not exact multiples
of 4. Some strings in this language are “1,” “2,” “10,” “12,” “20,” and “21.”
Some strings which are not in the language are , “0,” “11,” and “22.”
Download