简体   繁体   English

设计DFA接受可被数字'n'整除的二进制字符串

[英]Design DFA accepting binary strings divisible by a number 'n'

I need to learn how to design a DFA such that given any number 'n', it accepts binary strings {0, 1} whose decimal equivalent number is divisible by 'n'. 我需要学习如何设计DFA,以便给定任意数字'n',它接受二进制字符串{0,1},其十进制等效数可以被'n'整除。

There will be different DFAs for different 'n', but can somebody give a basic approach that I should follow to proceed with any number 0 < n < 10 . 不同的'n'会有不同的DFA,但有人可以提供一个基本的方法,我应该遵循任何数字0 <n <10。

Below, I have written an answer for n equals to 5, but you can apply same approach to draw DFAs for any value of n and 'any positional number system' eg binary, ternary... 下面,我写了一个n等于5的答案,但是您可以应用相同的方法为任何n值和“任何位置数系统”绘制DFA,例如二进制,三元......

First lean the term 'Complete DFA', A DFA defined on complete domain in δ:Q × Σ→Q is called 'Complete DFA'. 首先使用术语“完全DFA”, 在完整域中定义的DFA:δ:Q×Σ→Q称为“完全DFA”。 In other words we can say; 换句话说,我们可以说; in transition diagram of complete DFA there is no missing edge (eg from each state in Q there is one outgoing edge present for every language symbol in Σ). 在完整DFA的转换图中,没有丢失边缘(例如,从Q中的每个状态,对于Σ中的每个语言符号存在一个输出边缘)。 Note: Sometime we define partial DFA as δ ⊆ Q × Σ→Q (Read: How does “δ:Q × Σ→Q” read in the definition of a DFA ). 注意:有时我们将部分 DFA定义为δ⊆Q×Σ→Q(读: 如何在DFA的定义中读取“δ:Q×Σ→Q” )。

Design DFA accepting Binary numbers divisible by number 'n': 设计DFA接受可被数字'n'整除的二进制数:

Step-1 : When you divide a number ω by n then reminder can be either 0, 1, ..., (n - 2) or (n - 1). 步骤1 :当您将数字ω除以n提醒可以是0,1,...,(n - 2)或(n - 1)。 If remainder is 0 that means ω is divisible by n otherwise not. 如果余数为0 ,则意味着ω可被n整除,否则不能。 So, in my DFA there will be a state q r that would be corresponding to a remainder value r , where 0 <= r <= (n - 1) , and total number of states in DFA is n . 因此,在我的DFA中,将存在对应于余数值r的状态q r ,其中0 <= r <= (n - 1) ,并且DFA中的状态总数为n
After processing a number string ω over Σ, the end state is q r implies that ω % n => r (% reminder operator). 在Σ上处理数字串ω之后,结束状态为q r意味着ω%n => r(%提醒操作符)。

In any automata, the purpose of a state is like memory element. 在任何自动机中,状态的目的就像存储元素。 A state in an atomata stores some information like fan's switch that can tell whether the fan is in 'off' or in 'on' state. 在atomata中的状态存储一些信息,如风扇的开关,可以判断风扇是处于“关闭”还是处于“开启”状态。 For n = 5, five states in DFA corresponding to five reminder information as follows: 对于n = 5,DFA中的五个状态对应于五个提醒信息,如下所示:

  1. State q 0 reached if reminder is 0. State q 0 is the final state(accepting state). 如果提醒为0,则达到状态q 0。状态q 0是最终状态(接受状态)。 It is also an initial state. 它也是一个初始状态。
  2. State q 1 reaches if reminder is 1, a non-final state. 如果提醒为1,则为非最终状态,状态q 1到达。
  3. State q 2 if reminder is 2, a non-final state. 状态q 2如果提醒是2,则是非最终状态。
  4. State q 3 if reminder is 3, a non-final state. 状态q 3如果提醒是3,则是非最终状态。
  5. State q 4 if reminder is 4, a non-final state. 状态q 4如果提醒是4,则是非最终状态。

Using above information, we can start drawing transition diagram TD of five states as follows: 使用以上信息,我们可以开始绘制五种状态的转换图TD,如下所示:

图。1
Figure-1 图1

So, 5 states for 5 remainder values. 因此,5个状态表示5个剩余值。 After processing a string ω if end-state becomes q 0 that means decimal equivalent of input string is divisible by 5. In above figure q 0 is marked final state as two concentric circle. 在处理字符串ω之后,如果结束状态变为q 0 ,则意味着输入字符串的十进制等效值可被5整除。在上图中,q 0被标记为最终状态为两个同心圆。
Additionally, I have defined a transition rule δ:(q 0 , 0)→q 0 as a self loop for symbol '0' at state q 0 , this is because decimal equivalent of any string consist of only '0' is 0 and 0 is a divisible by n . 另外,我已经在状态q 0处定义了转换规则δ:(q 0,0 )→q 0作为符号'0'的自循环,这是因为任何字符串的十进制等效仅由'0'为0并且0可被n整除。

Step-2 : TD above is incomplete; 步骤2 :上述TD不完整; and can only process strings of '0' s. 并且只能处理'0'的字符串。 Now add some more edges so that it can process subsequent number's strings. 现在添加一些边缘,以便它可以处理后续数字的字符串。 Check table below, shows new transition rules those can be added next step: 查看下表,显示可以在下一步添加的新转换规则:

┌──────┬──────┬─────────────┬─────────┐
│NumberBinaryRemainder(%5)End-state│
├──────┼──────┼─────────────┼─────────┤
│One   │1     │1            │q1       │
├──────┼──────┼─────────────┼─────────┤
│Two   │10    │2            │q2       │
├──────┼──────┼─────────────┼─────────┤
│Three │11    │3            │q3       │
├──────┼──────┼─────────────┼─────────┤
│Four  │100   │4            │q4       │
└──────┴──────┴─────────────┴─────────┘
  1. To process binary string '1' there should be a transition rule δ:(q 0 , 1)→q 1 要处理二进制字符串'1' ,应该有一个转换规则δ:(q 0,1 )→q 1
  2. Two:- binary representation is '10' , end-state should be q 2 , and to process '10' , we just need to add one more transition rule δ:(q 1 , 0)→q 2 两个: -二进制表示为'10' ,结束状态应该是Q 2,并且处理'10' ,我们只需要增加一个转移规则δ:(Q 1,0)→Q 2
    Path : →(q 0 )─1→(q 1 )─0→(q 2 ) 路径 :→(q 0 )─1→(q 1 )─0→(q 2
  3. Three:- in binary it is '11' , end-state is q 3 , and we need to add a transition rule δ:(q 1 , 1)→q 3 三: -二进制它是'11'最终状态是Q 3,我们需要添加一个转换规则δ:(Q 1,1)→Q 3
    Path : →(q 0 )─1→(q 1 )─1→(q 3 ) 路径 :→(q 0 )─1→(q 1 )─1→(q 3
  4. Four:- in binary '100' , end-state is q 4 . 四: - 在二进制'100' ,结束状态为q 4 TD already processes prefix string '10' and we just need to add a new transition rule δ:(q 2 , 0)→q 4 TD已经处理了前缀字符串'10' ,我们只需要添加一个新的过渡规则δ:(Q 2,0)→Q 4
    Path : →(q 0 )─1→(q 1 )─0→(q 2 )─0→(q 4 ) 路径 :→(q 0 )─1→(q 1 )─0→(q 2 )─0→(q 4

图-2- Figure-2 图-2

Step-3 : Five = 101 步骤3 :五= 101
Above transition diagram in figure-2 is still incomplete and there are many missing edges, for an example no transition is defined for δ:(q 2 , 1)- ? 图2中的上述转换图仍然是不完整的并且存在许多缺失边,例如,没有为δ定义转换:(q 2,1 ) - . And the rule should be present to process strings like '101' . 并且应该存在规则来处理像'101'这样'101'字符串。
Because '101' = 5 is divisible by 5, and to accept '101' I will add δ:(q 2 , 1)→q 0 in above figure-2. 因为'101' = 5可以被5整除,并且为了接受'101'我将在上面的图-2中添加δ:(q 2,1 )→q 0
Path: →(q 0 )─1→(q 1 )─0→(q 2 )─1→(q 0 ) 路径: →(q 0 )─1→(q 1 )─0→(q 2 )─1→(q 0
with this new rule, transition diagram becomes as follows: 使用这个新规则,转换图如下:

无花果-3- Figure-3 图-3-

Below in each step I pick next subsequent binary number to add a missing edge until I get TD as a 'complete DFA'. 在每个步骤的下面,我选择下一个后续的二进制数来添加缺失的边缘,直到我将TD作为“完整的DFA”。

Step-4 : Six = 110. 步骤4 :六= 110。

We can process '11' in present TD in figure-3 as: →(q 0 )─11→(q 3 ) ─0→( ? ). 我们可以在图3中将当前TD中的'11'处理为:→(q 0 )─11→(q 3 )─0→( )。 Because 6 % 5 = 1 this means to add one rule δ:(q 3 , 0)→q 1 . 因为6%5 = 1,这意味着添加一个规则δ:(Q 3,0)→,Q 1。

无花果-4- Figure-4 图-4-

Step-5 : Seven = 111 步骤5 :七= 111

┌──────┬──────┬─────────────┬─────────┬────────────┬───────────┐
│NumberBinaryRemainder(%5)End-state PathAdd       │
├──────┼──────┼─────────────┼─────────┼────────────┼───────────┤
│Seven │111   │7 % 5 = 2    │q2       │ q0─11→q3    q3─1→q2    │
└──────┴──────┴─────────────┴─────────┴────────────┴───────────┘

无花果-5- Figure-5 图-5

Step-6 : Eight = 1000 步骤-6 :八= 1000

┌──────┬──────┬─────────────┬─────────┬──────────┬─────────┐
│NumberBinaryRemainder(%5)End-state PathAdd     │
├──────┼──────┼─────────────┼─────────┼──────────┼─────────┤
│Eight │1000  │8 % 5 = 3    │q3       │q0─100→q4 │ q4─0→q3  │
└──────┴──────┴─────────────┴─────────┴──────────┴─────────┘

无花果-6- Figure-6 图-6

Step-7 : Nine = 1001 步骤7 :九= 1001

┌──────┬──────┬─────────────┬─────────┬──────────┬─────────┐
│NumberBinaryRemainder(%5)End-state PathAdd     │
├──────┼──────┼─────────────┼─────────┼──────────┼─────────┤
│Nine  │1001  │9 % 5 = 4    │q4       │q0─100→q4 │ q4─1→q4  │
└──────┴──────┴─────────────┴─────────┴──────────┴─────────┘

图-7- Figure-7 图-7

In TD-7, total number of edges are 10 == Q × Σ = 5 × 2. And it is a complete DFA that can accept all possible binary strings those decimal equivalent is divisible by 5. 在TD-7中,边的总数是10 == Q×Σ= 5×2。它是一个完整的DFA,可以接受所有可能的二进制字符串,这些十进制等效值可以被5整除。

Design DFA accepting Ternary numbers divisible by number n: 设计DFA接受可被数字n整除的三元数:

Step-1 Exactly same as for binary, use figure-1. 步骤1与二进制完全相同,使用图-1。

Step-2 Add Zero, One, Two 步骤2添加零,一,二

┌───────┬───────┬─────────────┬─────────┬──────────────┐
│DecimalTernaryRemainder(%5)End-state   Add        │
├───────┼───────┼─────────────┼─────────┼──────────────┤
│Zero   │0      │0            │q0       │ δ:(q0,0)→q0  │
├───────┼───────┼─────────────┼─────────┼──────────────┤
│One    │1      │1            │q1       │ δ:(q0,1)→q1  │
├───────┼───────┼─────────────┼─────────┼──────────────┤
│Two    │2      │2            │q2       │ δ:(q0,2)→q3  │
└───────┴───────┴─────────────┴─────────┴──────────────┘

无花果-8-
Figure-8 图8

Step-3 Add Three, Four, Five 第3步添加三,四,五

┌───────┬───────┬─────────────┬─────────┬─────────────┐
│DecimalTernaryRemainder(%5)End-stateAdd        │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Three  │10     │3            │q3       │ δ:(q1,0)→q3 │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Four   │11     │4            │q4       │ δ:(q1,1)→q4 │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Five   │12     │0            │q0       │ δ:(q1,2)→q0 │
└───────┴───────┴─────────────┴─────────┴─────────────┘

图9
Figure-9 图9

Step-4 Add Six, Seven, Eight 第四步加六,七,八

┌───────┬───────┬─────────────┬─────────┬─────────────┐
│DecimalTernaryRemainder(%5)End-stateAdd        │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Six    │20     │1            │q1       │ δ:(q2,0)→q1 │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Seven  │21     │2            │q2       │ δ:(q2,1)→q2 │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Eight  │22     │3            │q3       │ δ:(q2,2)→q3 │
└───────┴───────┴─────────────┴─────────┴─────────────┘

无花果-10
Figure-10 图-10

Step-5 Add Nine, Ten, Eleven 步骤5加九,十,十一

┌───────┬───────┬─────────────┬─────────┬─────────────┐
│DecimalTernaryRemainder(%5)End-stateAdd        │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Nine   │100    │4            │q4       │ δ:(q3,0)→q4 │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Ten    │101    │0            │q0       │ δ:(q3,1)→q0 │
├───────┼───────┼─────────────┼─────────┼─────────────┤
│Eleven │102    │1            │q1       │ δ:(q3,2)→q1 │
└───────┴───────┴─────────────┴─────────┴─────────────┘

无花果-11
Figure-11 图11

Step-6 Add Twelve, Thirteen, Fourteen 步骤6添加十二,十三,十四

┌────────┬───────┬─────────────┬─────────┬─────────────┐
│DecimalTernaryRemainder(%5)End-stateAdd        │
├────────┼───────┼─────────────┼─────────┼─────────────┤
│Twelve  │110    │2            │q2       │ δ:(q4,0)→q2 │
├────────┼───────┼─────────────┼─────────┼─────────────┤
│Thirteen│111    │3            │q3       │ δ:(q4,1)→q3 │
├────────┼───────┼─────────────┼─────────┼─────────────┤
│Fourteen│112    │4            │q4       │ δ:(q4,2)→q4 │
└────────┴───────┴─────────────┴─────────┴─────────────┘

无花果-12
Figure-12 图-12

Total number of edges in transition diagram figure-12 are 15 = Q × Σ = 5 * 3 (a complete DFA). 转换图-12中的边缘总数为15 = Q×Σ= 5 * 3(完整的DFA)。 And this DFA can accept all strings consist over {0, 1, 2} those decimal equivalent is divisible by 5. 并且这个DFA可以接受所有字符串组成{0,1,2}这些十进制等值可以被5整除。
If you notice at each step, in table there are three entries because at each step I add all possible outgoing edge from a state to make a complete DFA (and I add an edge so that q r state gets for remainder is r )! 如果你注意到每一步,在表中有三个条目,因为在每一步我都添加一个状态的所有可能的传出边缘来制作一个完整的DFA(我添加一个边缘,以便q r状态得到的余数是r )!

To add further, remember union of two regular languages are also a regular. 要进一步添加,请记住两种常规语言的联合也是常规语言。 If you need to design a DFA that accepts binary strings those decimal equivalent is either divisible by 3 or 5, then draw two separate DFAs for divisible by 3 and 5 then union both DFAs to construct target DFA (for 1 <= n <= 10 your have to union 10 DFAs). 如果你需要设计一个接受二进制字符串的DFA,那么十进制等值可以被3或5整除,然后绘制两个单独的DFA,可以被3和5整除,然后将两个DFA结合起来构造目标DFA(对于1 <= n <= 10你必须联合10个DFA)。

If you are asked to draw DFA that accepts binary strings such that decimal equivalent is divisible by 5 and 3 both then you are looking for DFA of divisible by 15 ( but what about 6 and 8?). 如果要求您绘制接受二进制字符串的DFA,使得十进制等效值可以被5和3整除,那么您正在寻找可被15整除的DFA(但是6和8呢?)。

Note: DFAs drawn with this technique will be minimized DFA only when there is no common factor between number n and base eg there is no between 5 and 2 in first example, or between 5 and 3 in second example, hence both DFAs constructed above are minimized DFAs. 注意:使用此技术绘制的DFA只有在数字n和基数之间没有公因子时才会最小化DFA,例如,在第一个示例中没有 5到2之间,或者在第二个示例中没有5到3之间,因此上面构造的两个DFA都是最小化DFA。 If you are interested to read further about possible mini states for number n and base b read paper: Divisibility and State Complexity . 如果您有兴趣进一步阅读有关nb基础可能的迷你状态的文章: 可分性和状态复杂性

below I have added a Python script, I written it for fun while learning Python library pygraphviz. 下面我添加了一个Python脚本,我在学习Python库pygraphviz的同时编写了它。 I am adding it I hope it can be helpful for someone in someway. 我正在添加它我希望它可以对某些人有所帮助。

Design DFA for base 'b' number strings divisible by number 'n': 设计DFA为基数'b'数字字符串可被数字'n'整除:

So we can apply above trick to draw DFA to recognize number strings in any base 'b' those are divisible a given number 'n' . 因此,我们可以应用上述技巧来绘制DFA以识别任何基数'b'数字字符串,这些字符串可以对给定数字'n'进行整除。 In that DFA total number of states will be n (for n remainders) and number of edges should be equal to 'b' * 'n' — that is complete DFA: 'b' = number of symbols in language of DFA and 'n' = number of states. 在那个DFA中,状态总数将是n (对于n余数),边数应该等于'b'*'n' - 这是完整的DFA:'b'= DFA语言中符号的数量和'n '=州的数量。

Using above trick, below I have written a Python Script to Draw DFA for input base and number . 使用上面的技巧,下面我写了一个Python脚本来绘制输入basenumber DFA。 In script, function divided_by_N populates DFA's transition rules in base * number steps. 在脚本中,函数divided_by_Nbase * number步骤填充DFA的转换规则。 In each step-num, I convert num into number string num_s using function baseN() . 在每个step-num中,我使用函数baseN()num转换为数字字符串num_s To avoid processing each number string, I have used a temporary data-structure lookup_table . 为了避免处理每个数字字符串,我使用了临时数据结构lookup_table In each step, end-state for number string num_s is evaluated and stored in lookup_table to use in next step. 在每个步骤中,数字字符串num_s结束状态被评估并存储在lookup_table以用于下一步骤。

For transition graph of DFA, I have written a function draw_transition_graph using Pygraphviz library (very easy to use). 对于DFA的转换图,我使用Pygraphviz库编写了一个函数draw_transition_graph (非常容易使用)。 To use this script you need to install graphviz . 要使用此脚本,您需要安装graphviz To add colorful edges in transition diagram, I randomly generates color codes for each symbol get_color_dict function. 要在转换图中添加彩色边缘,我会为每个符号get_color_dict函数随机生成颜色代码。

#!/usr/bin/env python
import pygraphviz as pgv
from pprint import pprint
from random import choice as rchoice

def baseN(n, b, syms="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
    """ converts a number `n` into base `b` string """
    return ((n == 0) and syms[0]) or (
        baseN(n//b, b, syms).lstrip(syms[0]) + syms[n % b])

def divided_by_N(number, base):
    """
    constructs DFA that accepts given `base` number strings
    those are divisible by a given `number`
    """
    ACCEPTING_STATE = START_STATE = '0'
    SYMBOL_0 = '0'
    dfa = {
        str(from_state): {
            str(symbol): 'to_state' for symbol in range(base)
        }
        for from_state in range(number)
    }
    dfa[START_STATE][SYMBOL_0] = ACCEPTING_STATE
    # `lookup_table` keeps track: 'number string' -->[dfa]--> 'end_state'
    lookup_table = { SYMBOL_0: ACCEPTING_STATE }.setdefault
    for num in range(number * base):
        end_state = str(num % number)
        num_s = baseN(num, base)
        before_end_state = lookup_table(num_s[:-1], START_STATE)
        dfa[before_end_state][num_s[-1]] = end_state
        lookup_table(num_s, end_state)
    return dfa

def symcolrhexcodes(symbols):
    """
    returns dict of color codes mapped with alphabets symbol in symbols
    """
    return {
        symbol: '#'+''.join([
            rchoice("8A6C2B590D1F4E37") for _ in "FFFFFF"
        ])
        for symbol in symbols
    }

def draw_transition_graph(dfa, filename="filename"):
    ACCEPTING_STATE = START_STATE = '0'
    colors = symcolrhexcodes(dfa[START_STATE].keys())
    # draw transition graph
    tg = pgv.AGraph(strict=False, directed=True, decorate=True)
    for from_state in dfa:
        for symbol, to_state in dfa[from_state].iteritems():
            tg.add_edge("Q%s"%from_state, "Q%s"%to_state,
                        label=symbol, color=colors[symbol],
                        fontcolor=colors[symbol])

    # add intial edge from an invisible node!
    tg.add_node('null', shape='plaintext', label='start')
    tg.add_edge('null', "Q%s"%START_STATE,)

    # make end acception state as 'doublecircle'
    tg.get_node("Q%s"%ACCEPTING_STATE).attr['shape'] = 'doublecircle'
    tg.draw(filename, prog='circo')
    tg.close()

def print_transition_table(dfa):
    print("DFA accepting number string in base '%(base)s' "
            "those are divisible by '%(number)s':" % {
                'base': len(dfa['0']),
                'number': len(dfa),})
    pprint(dfa)

if __name__ == "__main__":
    number = input ("Enter NUMBER: ")
    base = input ("Enter BASE of number system: ")
    dfa = divided_by_N(number, base)

    print_transition_table(dfa)
    draw_transition_graph(dfa)

Execute it: 执行它:

~/study/divide-5/script$ python script.py 
Enter NUMBER: 5
Enter BASE of number system: 4
DFA accepting number string in base '4' those are divisible by '5':
{'0': {'0': '0', '1': '1', '2': '2', '3': '3'},
 '1': {'0': '4', '1': '0', '2': '1', '3': '2'},
 '2': {'0': '3', '1': '4', '2': '0', '3': '1'},
 '3': {'0': '2', '1': '3', '2': '4', '3': '0'},
 '4': {'0': '1', '1': '2', '2': '3', '3': '4'}}
~/study/divide-5/script$ ls
script.py filename.png
~/study/divide-5/script$ display filename

Output: 输出:

base_4_divided_5_best
DFA accepting number strings in base 4 those are divisible by 5 DFA接受基数4中的数字字符串,可以被5整除

Similarly, enter base = 4 and number = 7 to generate - dfa accepting number string in base '4' those are divisible by '7' 同样,输入base = 4和number = 7生成 - dfa接受基数'4'中的数字字符串,它们可被'7'整除
Btw, try changing filename to .png or .jpeg . 顺便说一句,尝试将filename更改为.png.jpeg

References those I use to write this script: 引用我用来编写这个脚本的那些:
➊ Function baseN from "convert integer to a string in a given numeric base in python" ➊函数baseN来自“将整数转换为python中给定数字基础中的字符串”
➋ To install "pygraphviz": "Python does not see pygraphviz" ➋安装“pygraphviz”: “Python没有看到pygraphviz”
➌ To learn use of Pygraphviz: "Python-FSM" ➌学习使用Pygraphviz: “Python-FSM”
➍ To generate random hex color codes for each language symbol: "How would I make a random hexdigit code generator using .join and for loops?" ➍为每个语言符号生成随机十六进制颜色代码: “如何使用.join和for循环创建随机十六进制代码生成器?”

I know I am quite late, but I just wanted to add a few things to the already correct answer provided by @Grijesh. 我知道我已经很晚了,但我只是想在@Grijesh提供的正确答案中添加一些内容。 I'd like to just point out that the answer provided by @Grijesh does not produce the minimal DFA. 我想指出@Grijesh提供的答案不会产生最小的DFA。 While the answer surely is the right way to get a DFA, if you need the minimal DFA you will have to look into your divisor. 虽然答案肯定是获得DFA的正确方法,但如果你需要最小的DFA,你将需要调查你的除数。

Like for example in binary numbers, if the divisor is a power of 2 (ie 2^n) then the minimum number of states required will be n+1. 例如,在二进制数中,如果除数是2的幂(即2 ^ n),则所需的最小状态数将是n + 1。 How would you design such an automaton? 你会如何设计这样的自动机? Just see the properties of binary numbers. 只需查看二进制数的属性即可。 For a number, say 8 (which is 2^3), all its multiples will have the last 3 bits as 0. For example, 40 in binary is 101000. Therefore for a language to accept any number divisible by 8 we just need an automaton which sees if the last 3 bits are 0, which we can do in just 4 states instead of 8 states. 对于一个数字,比如说8(即2 ^ 3),它的所有倍数都将最后3位作为0.例如,二进制中的40是101000.因此,对于一种语言来接受任何可被8整除的数字,我们只需要一个看到最后3位为0的自动机,我们可以只用4个状态而不是8个状态。 That's half the complexity of the machine. 这是机器复杂性的一半。

In fact, this can be extended to any base. 实际上,这可以扩展到任何基础。 For a ternary base number system, if for example we need to design an automaton for divisibility with 9, we just need to see if the last 2 numbers of the input are 0. Which can again be done in just 3 states. 对于三元基数系统,如果我们需要设计一个自动机用于9的可除性,我们只需要看看输入的最后2个数是否为0.这可以在3个状态中再次完成。

Although if the divisor isn't so special, then we need to go through with @Grijesh's answer only. 虽然如果除数不是那么特殊,那么我们只需要通过@Grijesh的答案。 Like for example, in a binary system if we take the divisors of 3 or 7 or maybe 21, we will need to have that many number of states only. 例如,在二元系统中,如果我们采用3或7或21的除数,我们将只需要那么多个状态。 So for any odd number n in a binary system, we need n states to define the language which accepts all multiples of n. 因此,对于二进制系统中的任何奇数n,我们需要n个状态来定义接受n的所有倍数的语言。 On the other hand, if the number is even but not a power of 2 (only in case of binary numbers) then we need to divide the number by 2 till we get an odd number and then we can find the minimum number of states by adding the odd number produced and the number of times we divided by 2. 另一方面,如果数字是偶数但不是2的幂(仅在二进制数的情况下),那么我们需要将数字除以2直到我们得到一个奇数,然后我们可以找到最小数量的状态添加生成的奇数和我们除以2的次数。

For example, if we need to find the minimum number of states of a DFA which accepts all binary numbers divisible by 20, we do : 例如,如果我们需要找到DFA的最小状态数,它接受所有可被20整除的二进制数,我们可以:

20/2 = 10 
10/2 = 5

Hence our answer is 5 + 1 + 1 = 7 . 因此我们的答案是5 + 1 + 1 = 7 (The 1 + 1 because we divided the number 20 twice). (1 + 1,因为我们将数字20除以两次)。

You can build DFA using simple modular arithmetics. 您可以使用简单的模块化算法构建DFA。 We can interpret w which is a string of k-ary numbers using a following rule 我们可以使用以下规则来解释w是一串k元数

V[0] = 0
V[i] = (S[i-1] * k) + to_number(str[i])

V[|w|] is a number that w is representing. V[|w|]w表示的数字。 If modify this rule to find w mod N , the rule becomes this. 如果修改此规则以找到w mod N ,则规则变为此。

V[0] = 0
V[i] = ((S[i-1] * k) + to_number(str[i])) mod N

and each V[i] is one of a number from 0 to N-1, which corresponds to each state in DFA. 每个V[i]是从0到N-1的数字之一,它对应于DFA中的每个状态。 We can use this as the state transition. 我们可以将其用作状态转换。

See an example. 看一个例子。

k = 2, N = 5 k = 2,N = 5

| V | (V*2 + 0) mod 5     | (V*2 + 1) mod 5     |
+---+---------------------+---------------------+
| 0 | (0*2 + 0) mod 5 = 0 | (0*2 + 1) mod 5 = 1 |
| 1 | (1*2 + 0) mod 5 = 2 | (1*2 + 1) mod 5 = 3 |
| 2 | (2*2 + 0) mod 5 = 4 | (2*2 + 1) mod 5 = 0 |
| 3 | (3*2 + 0) mod 5 = 1 | (3*2 + 1) mod 5 = 2 |
| 4 | (4*2 + 0) mod 5 = 3 | (4*2 + 1) mod 5 = 4 |

k = 3, N = 5 k = 3,N = 5

| V | 0 | 1 | 2 |
+---+---+---+---+
| 0 | 0 | 1 | 2 |
| 1 | 3 | 4 | 0 |
| 2 | 1 | 2 | 3 |
| 3 | 4 | 0 | 1 |
| 4 | 2 | 3 | 4 |

Now you can see a very simple pattern. 现在你可以看到一个非常简单的模式。 You can actually build a DFA transition just write repeating numbers from left to right, from top to bottom, from 0 to N-1. 您实际上可以构建DFA转换,只需从左到右,从上到下,从0到N-1写入重复数字。

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

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