简体   繁体   中英

DFA for binary number equivalent decimal divisible by 3

DFA for generating binary numbers that are divisible by 3 ,2&5 have been known in that we read string for eg 1 next 0 next 0 100 is string for this first we read string and assing base 2 ( binary powers) from right to left ......

Suppose if we read same string in same order but first placing numbers sequentially in power of 2 if we read 1 it be first bit and 0 2nd bit so we will read as 001 for above DFA we read the string oppositely ....so what is DFA for this by placing bits from left to right

The usual way of making a DFA that computes something like this is to first make an NFA (since NFAs are must easier to compose vis union/intersection/etc), and then convert the NFA to a DFA.

So how do you compute if a binary number is divisible by 3? Well, given a number in base-b, you can easily compute mod b-1 of it by adding the digits of mod b-1. Given a binary number you can generate base 2 k digits by simply taking them in groups. So for mod 3, you need base 4 digits (pairs of bits). You can get mod 7 by using groups of 3 bits, and mod 15 with groups of 4. mod 15 can then be trivially converted to mod 5 and mod 3.

So how do you make an NFA that adds mod n? You need a loop of n states that correspond to the value 0..n-1 and have transitions between them for adding bits. For the base 3 case, that is 3 states

state      00 01 10 11
  0         0  1  2  0
  1         1  2  0  1
  2         2  0  1  2

This is an NFA, so a 2-bit transition goes through an intermediate state that is otherwise unconnected. Your start and accepting state is 0. One final subtlety is dealing with an odd number of bits. How you handle that depends on whether your number is big-endian or little-endian. For little-endian you want to treat the final odd bit as a digit, so make the edge-intermediate states that transition to 0 on a 0 bit accepting states. For big endian, you instead add an additional start state that transitions to 0 and 1 on a single 0 or 1 bit.

We can use the Myhill Nerode theorem to guide us toward a minimal DFA for this language directly.

We begin by examining strings of increasing length and asking whether they are distinguishable from strings we have already seen.

Strings are distinguishable if they are followed by different sets of strings to get strings in the target language.

The empty string, e, can be followed by any string in L to get a string in L. Call this .

The string 0, too, can be followed by any string in L to get a string in L. We might as well allow leading 0s and ignore them. If you'd rather make such strings not part of your language, then 0 is distinct from e. We will let it be indistinguishable.

The string 1 is distinguishable since not all strings in L can follow it and produce a string in L. Indeed, a moment's reflection will show that no string in L can follow 1 and lead to another string in L. Call this <1>.

We need not consider 00 and 01 since 0 was indistinguishable from e and so 00 and 01 are indistinguishable from 0 and 1 which we have already considered.

The string 10 is distinguishable from both 0 and 1 in that neither 1 nor 11 can follow it to produce a string in L. Call this <10>

The string 11, on the other hand, is totally indistinguishable from e and 0: we can add any string in L to it to get another.

We need not consider 000, 001, 010, 011, 110 or 111 since the prefixes were already found to be indistinguishable earlier.

The string 100 is, perhaps surprisingly, indistinguishable from the string 1: anything we can add to 1 to get a string in L leads to a string in L if added to 100 as well.

The string 101 is, perhaps surprisingly, indistinguishable from the string 10: anything we can to 10 to get a string in L leads to a string in L if added to 101 as well.

We named three classes of distinguishable strings:

  • : e, 0, 11, etc.
  • <1>: 1, 100, etc.
  • <10>: 10, 101, etc.

And these account for all distinguishable prefixes of length no more than three. Myhill-Nerode guarantees there is a minimal DFA with three states corresponding to these equivalene classes. And the transitions couldn't be easier to figure out: if x = ys, then the state corresponding to y's class leads to the state corresponding to x's class on symbol s.

Q    s    Q'
<e>  0    <e>
<e>  1    <1>
<1>  0    <10>
<1>  1    <e>
<10> 0    <1>
<10> 1    <10>

Naturally, the accepting states are the ones which contain strings in L; only fits the bill here. The initial state is the one containing e, also in our case.

We can look at this now and rationalize the rules mathematically. To make the discussion simpler, we define a "three token" as follows:

A three-token is a substring of the input which represents a number evenly divisible by three in binary notation.

The states can now be understood as follows:

  1. <e> : we have seen a sequence of three-tokens separated by an arbitrary number of zeroes.
  2. <1> : we are looking at an incomplete three-token and the part we see is the binary representation of a number congruent to one modulo three.
  3. <10> : we are looking at an incomplete three-token and the part we see is the binary representation of a number conruent to two modulo three.

The transitions then make sense:

  1. <e> 0 <e> : discard 0s separating three-tokens.
  2. <e> 1 <1> : new three-token initially 1 (mod 3)
  3. <1> 0 <10> : partial three-token becomes 2 (mod 3).
  4. <1> 1 <e> : completed three-token.
  5. <10> 0 <1> : partial three-token becomes 1 (mod 3).
  6. <10> 1 <10> : partial three-token remains 2 (mod 3).

Seeing a sequence of three-tokens is like adding multiples of three each multiplied by a different power of two; such a sum is guaranteed to be divisble by three.

If we are constructing a new three-token, reading the next symbol s multiplies our current value v by 2 and then adds s; that is, v' = 2v + s . The only way v' can be divisible by 3 is if v is 1 (mod 3) and s is 1. We can ignore the case where v is 0 (mod 3) and s is 0, since in that case we are in <e> and reading 0 s between three-tokens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM