简体   繁体   English

DFA 中的最小状态数

[英]Minimum number of states in DFA

Minimum number states in the DFA accepting strings (base 3 ie,, ternary form) congruent to 5 modulo 6? DFA 中接受字符串(基数为 3,即三元形式)中与 5 模 6 一致的最小数状态?

I have tried but couldn't do it.我试过了,但做不到。

At first sight, It seems to have 6 states but then it can be minimised further.乍一看,它似乎有 6 个状态,但可以进一步最小化。
Let's first see the state transition table:我们先来看看状态转移表: 状态转换表

Here, the states q 0 , q 1 , q 2 ,...., q 5 corresponds to the states with modulo 0,1,2,..., 5 respectively when divided by 6. q 0 is our initial state and since we need modulo 5 therefore our final state will be q 5这里,状态 q 0 , q 1 , q 2 ,...., q 5 分别对应于模 0,1,2,..., 5 除以 6 时的状态。 q 0是我们的初始状态,由于我们需要模 5,因此我们的最终状态将是 q 5

Few observations drawn from above state transition table:从上面的状态转换表中得出的一些观察结果:

  • states q 0 , q 2 and q 4 are exactly same状态 q 0 , q 2和 q 4完全相同
  • states q 1 , q 3 and q 5 are exactly same状态 q 1 , q 3和 q 5完全相同

The states which make transitions to the same states on the same inputs can be merged into a single state.在相同输入上转换到相同状态的状态可以合并为单个状态。

Note: Final and Non-final states can never be merged.注意:最终状态和非最终状态永远不能合并。

Therefore, we can merge q 0 , q 2 , q 4 together and q 1 , q 3 together leaving the state q 5 aloof from collation.因此,我们可以将 q 0 , q 2 , q 4合并在一起,将 q 1 , q 3合并在一起,使状态 q 5远离整理。
The final Minimal DFA has 3 states as shown below :最终的最小 DFA 有 3 个状态,如下所示 最小 DFA

Let's look at a few strings in the language:让我们看一下语言中的几个字符串:

 12 =              1*3 + 2 =  5 ~ 5 (mod 6)
102 =        1*9 + 0*3 + 2 = 11 ~ 5 (mod 6)
122 =        1*9 + 2*3 + 2 = 17 ~ 5 (mod 6)
212 =        2*9 + 1*3 + 2 = 23 ~ 5 (mod 6)

1002 = 1*18 + 0*9 + 0*9 + 2 = 29 ~ 5 (mod 6) 1002 = 1*18 + 0*9 + 0*9 + 2 = 29 ~ 5 (mod 6)

We notice that all the strings end in 2. This makes sense since 6 is a multiple of 3 and the only way to get 5 from a multiple of 3 is to add 2. Based on this, we can try to solve the problem of strings congruent to 3 modulo 6:我们注意到所有的字符串都以 2 结尾。这是有道理的,因为 6 是 3 的倍数,从 3 的倍数得到 5 的唯一方法是加上 2。基于此,我们可以尝试解决字符串的问题与 3 模 6 一致:

  10 =  3
 100 =  9
 120 = 15
 210 = 21
1000 = 27

There's not a real pattern emerging, but consider this: every base-3 number ending in 0 is definitely divisible by 3. The ones that are even are also divisible by 6;没有出现真正的模式,但请考虑一下:每个以 0 结尾的基数为 3 的数字肯定可以被 3 整除。偶数也可以被 6 整除; so the odd numbers whose base-3 representation ends in 0 must be congruent to 3 mod 6. Because all the powers of 3 are odd, we know we have an odd number if the number of 1s in the string is odd.所以基数 3 表示以 0 结尾的奇数必须与 3 mod 6 一致。因为 3 的所有幂都是奇数,我们知道如果字符串中 1 的个数是奇数,我们就有一个奇数。

So, our conditions are:所以,我们的条件是:

  1. the string begins with a 1;字符串以 1 开头;
  2. the string has an odd number of 1s;字符串有奇数个 1;
  3. the string ends with 2;字符串以 2 结尾;
  4. the string can contain any number of 2s and 0s.字符串可以包含任意数量的 2 和 0。

To get the minimum number of states in such a DFA, we can use the Myhill-Nerode theorem beginning with the empty string:要获得此类 DFA 中的最小状态数,我们可以使用以空字符串开头的 Myhill-Nerode 定理:

  1. the empty string can be followed by any string in the language.空字符串后面可以跟该语言中的任何字符串。 Call its equivalence class [e]称其等价类 [e]
  2. the string 0 cannot be followed by anything since valid base-3 representations don't have leading 0s.字符串 0 后面不能跟任何东西,因为有效的 base-3 表示没有前导 0。 Call its equivalence class [0].称其为等价类 [0]。
  3. the string 1 must be followed with stuff that has an even number of 1s in it ending with a 2. Call its equivalence class [1].字符串 1 后面必须跟有以 2 结尾的偶数个 1 的内容。调用它的等价类 [1]。
  4. the string 2 can be followed by anything in the language.字符串 2 后面可以跟该语言中的任何内容。 Indeed, you can verify that putting a 2 at the front of any string in the language gives another string in the language.实际上,您可以验证将 2 放在该语言中任何字符串的前面会给出该语言中的另一个字符串。 However, it can also be followed by strings beginning with 0. Therefore, its class is new: [2].但是,它也可以跟以 0 开头的字符串。因此,它的类是新的:[2]。
  5. the string 00 can't be followed by anything to fix it;字符串 00 后面不能跟任何修复它的东西; its class is the same as its prefix 0, [0].它的类与其前缀 0 [0] 相同。 same for the string 01.字符串 01 相同。
  6. the string 10 can be followed by any string with an even number of 1s that ends in a 2;字符串 10 后面可以跟任何以 2 结尾的偶数个 1 的字符串; it is therefore equivalent to the class [1].因此它等价于类 [1]。
  7. the string 11 can be followed by any string in the language whatever;字符串 11 后面可以跟任何语言中的任何字符串; indeed, you can verify prepending 11 in front of any string in the language gives another solution.实际上,您可以验证在该语言中的任何字符串前添加 11 会提供另一种解决方案。 However, it can also be followed by strings beginning with 0. Therefore, its class is the same as [2].但是,它也可以后跟以0开头的字符串。因此,它的类与[2]相同。
  8. 12 can be followed by a string with an even number of 1s ending in 2, as well as by the empty string (since 12 is in fact in the language). 12 后面可以跟一个以 2 结尾的偶数个 1 的字符串,以及空字符串(因为 12 实际上在语言中)。 This is a new class, [12].这是一个新类,[12]。
  9. 21 is equivalent to 1; 21 等于 1; class [1]类 [1]
  10. 22 is equivalent to 2; 22 等于 2; class [2]类 [2]
  11. 20 is equivalent to 2; 20 相当于 2; class [2]类 [2]
  12. 120 is indistinguishable from 1; 120 与 1 没有区别; its class is [1].它的类是[1]。
  13. 121 is indistinguishable from [2]. 121 与 [2] 没有区别。
  14. 122 is indistinguishable from [12]. 122 与 [12] 没有区别。

We have seen no new equivalence classes on new strings of length 3;我们在长度为 3 的新字符串上没有看到新的等价类; so, we know we have seen all the equivalence classes.所以,我们知道我们已经看到了所有的等价类。 They are the following:它们如下:

  • [e]: any string in the language can follow this [e]: 语言中的任何字符串都可以跟在这个后面
  • [0]: no string can follow this [0]:没有字符串可以跟在这个后面
  • [1]: a string with an even number of 1s ending in 2 can follow this [1]:以2结尾的偶数个1的字符串可以跟在这个后面
  • [2]: same as [e] but also strings beginning with 0 [2]:与 [e] 相同,但也是以 0 开头的字符串
  • [12]: same as [1] but also the empty string [12]: 同 [1] 但也是空字符串

This means that a minimal DFA for our language has five states.这意味着我们语言的最小 DFA 有五个状态。 Here is the DFA:这是 DFA:

      [0]
       ^
       |
       0
       |
----->[e]--2-->[2]<-\
       |        ^   |
       |        |   |
       1   __1__/   /
       |  /        /
       | |         1
       V V         |
       [1]--2-->[12]
         ^       |
         |       |
         \___0___/

(transitions not pictured are self-loops on the respective states). (未图示的转换是各自状态的自循环)。

Note: I expected this DFA to have 6 states, as Welbog pointed out in the other answer, so I might have missed an equivalence class.注意:我希望这个 DFA 有 6 个状态,正如 Welbog 在另一个答案中指出的那样,所以我可能错过了一个等价类。 However, the DFA seems right after checking a few examples and thinking about what it's doing: you can only get to accepting state [12] by seeing a 2 as the last symbol (definitely necessary) and you can only get to state [12] from state [1] and you must have seen an odd number of 1s to get to [1]…然而,在检查了几个例子并思考它在做什么之后,DFA 似乎是正确的:你只能通过将 2 作为最后一个符号(绝对必要)来接受状态 [12],并且你只能进入状态 [12]从状态 [1] 开始,您必须看到奇数个 1 才能到达 [1] ...

The minimum number of states for almost all modulus problems is the base of the modulus.几乎所有模数问题的最小状态数是模数的基础。 The general strategy is one state for every modulus, as transitions between moduli are independent of what the previous numbers were.一般策略是每个模数都有一个状态,因为模数之间的转换与之前的数字无关。 For example, if you're in state r4 (representing x = 4 (mod 6) ), and you encounter a 1 as your next input, your new modulus is 4x6+1 = 25 = 1 (mod 6) , so the transition from r4 on input 1 is to r1 .例如,如果您处于状态r4 (代表x = 4 (mod 6) ),并且您遇到1作为下一个输入,则您的新模数为4x6+1 = 25 = 1 (mod 6) ,因此转换从输入1上的r4r1 You'll find that the start state and r0 can be merged, for a total of 6 states.你会发现 start state 和r0可以合并,总共有 6 个 state。

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

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