简体   繁体   English

DFA的二进制数等效值小数可被3整除

[英]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 ...... DFA用于生成可被3、2&5整除的二进制数,这是众所周知的,因为我们首先读取字符串,例如1下一个0接下来0 100是字符串,这是我们首先读取字符串并从右到左分配基数2(二进制幂)。 ....

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 假设如果我们以相同的顺序读取相同的字符串,但是如果首先读取数字,则首先以2的幂顺序放置数字,如果我们读取1,则它是第一位,而第二位是0,因此对于以上DFA,我们将其读为001,那么我们相反地读取了字符串....那么通过从左到右放置位来实现DFA

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. 制作可计算出类似内容的DFA的通常方法是先制作NFA(因为必须使NFA相对于联合/交叉点等易于编写),然后将NFA转换为DFA。

So how do you compute if a binary number is divisible by 3? 那么,如何计算二进制数是否可以被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. 好吧,给定以b为底的数字,您可以通过将mod b-1的数字相加来轻松计算其mod b-1。 Given a binary number you can generate base 2 k digits by simply taking them in groups. 给定一个二进制数,您只需简单地将它们成组就可以生成2 k个基数。 So for mod 3, you need base 4 digits (pairs of bits). 因此,对于mod 3,您需要基数4位(成对的位)。 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. 您可以通过使用3位的组来获得mod 7,而将mod 15与4组一起使用,则可以将mod 15轻松转换为mod 5和mod 3。

So how do you make an NFA that adds mod n? 那么,如何制作添加mod n的NFA? You need a loop of n states that correspond to the value 0..n-1 and have transitions between them for adding bits. 您需要一个n个状态的循环,这些状态对应于值0..n-1,并在它们之间进行转换以添加位。 For the base 3 case, that is 3 states 对于基数为3的情况,即为3个状态

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. 这是一个NFA,因此2位转换会经历中间状态,否则该状态将不连接。 Your start and accepting state is 0. One final subtlety is dealing with an odd number of bits. 您的开始和接受状态为0。最后一个细微之处是处理奇数位数。 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. 对于little-endian,您要将最后的奇数位视为一个数字,因此使边缘中间状态在接受状态的0位上转换为0。 For big endian, you instead add an additional start state that transitions to 0 and 1 on a single 0 or 1 bit. 对于大字节序,您可以添加一个附加的开始状态,该状态在单个0或1位上转换为0和1。

We can use the Myhill Nerode theorem to guide us toward a minimal DFA for this language directly. 我们可以使用Myhill Nerode定理直接指导我们朝该语言的最低DFA迈进。

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 . 空字符串e后面可以跟有L中的任何字符串,以获得L中的字符串。将此称为。

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. 字符串0也可以跟在L中的任何字符串之后,以得到L中的字符串。我们也可以允许前导0并忽略它们。 If you'd rather make such strings not part of your language, then 0 is distinct from e. 如果您希望使这样的字符串不属于您的语言,则0不同于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>. 字符串1是可区分的,因为不是L中的所有字符串都可以跟随它并在L中产生一个字符串。的确,片刻的反思将表明L中没有字符串可以跟随1并导致L中的另一个字符串。将此称为<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. 我们不需要考虑00和01,因为0与e不可区分,因此00和01与我们已经考虑过的0和1不可区分。

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> 字符串10与0和1有所区别,因为1和11都不能跟随它来产生L中的字符串。将此称为<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. 另一方面,字符串11与e和0完全没有区别:我们可以在L中添加任何字符串以得到另一个。

We need not consider 000, 001, 010, 011, 110 or 111 since the prefixes were already found to be indistinguishable earlier. 我们不必考虑000、001、010、011、110或111,因为早已发现前缀是无法区分的。

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. 也许令人惊讶的是,字符串100与字符串1不可区分:我们可以加到1以得到L中的字符串的任何东西,如果也加到100,也会导致L中的字符串。

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. 可能令人惊讶的是,字符串101与字符串10是无法区分的:如果将10加到101,我们可以得到10以得到L中的字符串的任何结果也会导致L中的字符串。

We named three classes of distinguishable strings: 我们命名了三类可区分的字符串:

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

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. Myhill-Nerode保证存在最小的DFA,其中三个状态对应于这些等价物类别。 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. 而且,这种过渡也很容易找出:如果x = ys,则与y的类相对应的状态会导致在符号s上与x的类相对应的状态。

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; 自然,接受状态是在L中包含字符串的状态; only fits the bill here. 只适合这里的账单。 The initial state is the one containing e, also in our case. 在我们的例子中,初始状态是包含e的状态。

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. <e> :我们已经看到了由三个任意数量的零分隔的三个令牌的序列。
  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. <1> :我们正在查看一个不完整的三令牌,我们看到的部分是与一个模三相同的数字的二进制表示。
  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. <10> :我们正在看一个不完整的三令牌,我们看到的部分是与两个以三为模的数字的二进制表示。

The transitions then make sense: 这样的过渡才有意义:

  1. <e> 0 <e> : discard 0s separating three-tokens. <e> 0 <e> :丢弃分隔三个令牌的0s。
  2. <e> 1 <1> : new three-token initially 1 (mod 3) <e> 1 <1> :最初的新三令牌1(mod 3)
  3. <1> 0 <10> : partial three-token becomes 2 (mod 3). <1> 0 <10> :部分三个令牌变为2(mod 3)。
  4. <1> 1 <e> : completed three-token. <1> 1 <e> :完成了三个令牌。
  5. <10> 0 <1> : partial three-token becomes 1 (mod 3). <10> 0 <1> :部分三个令牌变为1(mod 3)。
  6. <10> 1 <10> : partial three-token remains 2 (mod 3). <10> 1 <10> :保留三个令牌的部分2(mod 3)。

Seeing a sequence of three-tokens is like adding multiples of three each multiplied by a different power of two; 看到三个标记的序列就像将三个的倍数乘以不同的2的乘积相乘; 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; 如果我们要构建一个新的三令牌,则读取下一个符号s将当前值v乘以2,然后将s相加; that is, v' = 2v + s . 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. v'可以被3整除的唯一方法是,如果v1 (mod 3)s为1。我们可以忽略v0 (mod 3)s为0的情况,因为在这种情况下,我们处于<e>并在三个标记之间读取0 s。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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