简体   繁体   English

DFA - 设计一个DFA,它接受{0,1}上的所有字符串,其中包含最多两个00和三个11作为子字符串

[英]DFA - design a DFA that accepts all strings over {0,1} that contains at most two 00's and three 11's as substring

I am practicing my DFA and I came across this question that seems to be very complex. 我正在练习我的DFA而且我遇到了这个似乎非常复杂的问题。 I tried splitting it up into two smaller questions and I have an answer but it doesn't look right to me. 我尝试将它分成两个较小的问题,我有一个答案,但它看起来不对我。

Can someone help me or at least give me some tips. 有人可以帮助我,或者至少给我一些提示。

They are all accepting states btw. 他们都接受国家顺便说一下。 在此输入图像描述

Another possibility that I can think of is that you only have the accepting states every two O's or 1's since they need to be a pair. 我能想到的另一种可能性是,由于它们需要成对,因此每两个O或1只有接受状态。 So for example 11 accepting 11 accepting 11 accepting. 因此例如11接受11接受11接受。

You're describing your state machine as a huge picture, but you can make your problem much easier by naming your states with structured names rather than trying to draw a huge diagram. 您将状态机描述为一幅巨大的图片,但您可以通过使用结构化名称命名状态而不是尝试绘制巨大的图表来使问题更容易。

Let your states be (n, m, s) where n is the number of 00s you've seen, m is the number of 11s you've seen and s is the previous character read (s='', '1', '0') (where '' means you haven't seen a previous character, or you've just found a '00' or '11'). 让你的状态为(n,m,s),其中n是你看过的00的数量,m是你看过的11s的数量,s是前面的字符读数(s ='','1', '0')(其中''表示你没有看到过前一个角色,或者你刚刚找到'00'或'11')。

Then your transitions are: 然后你的过渡是:

(n, m, '')  -0-> (n, m, '0')
(n, m, '')  -1-> (n, m, '1')
(n, m, '0') -0-> (n+1, m, '')
(n, m, '0') -1-> (n, m, '1')
(n, m, '1') -0-> (n, m, '0')
(n, m, '1') -1-> (n, m+1, '')

All states with n<=2 and m<=3 are accepting. n <= 2且m <= 3的所有状态都在接受。 The start state is (0, 0, '') . 开始状态是(0, 0, '')

This isn't finite, but you can make it so by merging all non-accepting states into a single state. 这不是有限的,但您可以通过将所有非接受状态合并为单个状态来实现。 It'll have (3 * 4 * 3 + 1) = 37 states, which is minimal. 它将具有(3 * 4 * 3 + 1)= 37个状态,这是最小的。

Counting overlapping 00 and 11. 计算重叠的00和11。

The answer above assumes that '000' contains one '00' rather than 2 '00's (that is the number of '00' is the maximal number of non-overlapping '00' in the string, and the same for '11'). 上面的答案假设'000'包含一个'00'而不是2'00'(即'00'的数字是字符串中非重叠'00'的最大数,而'11'则相同) 。 If you want to count '000' as 2, you need this machine: 如果你想把'000'算作2,你需要这台机器:

States are S (start) or (n, m, s) where n, m are as before, and s is '0' or '1'. 状态是S(开始)或(n,m,s),其中n,m如前,s是'0'或'1'。 Then: 然后:

S -0-> (0, 0, '0')
S -1-> (0, 0, '1')
(n, m, '0') -0-> (n+1, m, '0')
(n, m, '0') -1-> (n, m, '1')
(n, m, '1') -0-> (n, m, '0')
(n, m, '1') -1-> (n, m+1, '1')

All states are accepting except those with n>2 or m>3. 所有州都接受,但n> 2或m> 3的州除外。 Again, we merge all non-accepting states into a single state. 同样,我们将所有非接受状态合并为一个状态。

This has (3 * 4 * 2 + 2) = 26 states, which again, is minimal. 这具有(3 * 4 * 2 + 2)= 26个状态,这又是最小的。

Generating a FSM diagram. 生成FSM图。

Given the formal description, one can write a program to generate a DOT file that describes the machine. 给定正式描述,可以编写程序来生成描述机器的DOT文件。 Here's the program to generate the diagram for the machine in the first part of the answer. 这是在答案的第一部分为机器生成图表的程序。 (Note, it doesn't show which states are accepting, but they all are except FAIL). (注意,它没有显示哪些州正在接受,但它们都是FAIL除外)。

def state(n, m, s):
    if n > 2 or m > 3: return 'FAIL'
    return "n%s_%s_%s" % (n, m, s)

def T(st, c):
    n, m, s = st
    if s == '':
        return (n, m, c)
    if s == '0':
        return (n+1, m, '') if c=='0' else (n, m, c)
    if s == '1':
        return (n, m+1, '') if c=='1' else (n, m, c)

print 'digraph {'
for n in xrange(3):
    for m in xrange(4):
        for s in ['', '0', '1']:
            for c in '01':
                print '    %s -> %s [label="%s"]' % (state(n, m, s), state(*T((n, m, s), c)), c)

print '}'

Here's the result from piping the output through dot -Tpng : 这是通过dot -Tpng管道输出的结果: FSM

Well, instead of drawing the automaton you may describe it using words. 好吧,您可以使用单词来描述自动机,而不是绘制自动机。

How would I do that for your problem: Let a vertex be a triple where 我如何解决您的问题:让顶点成为三元组

  • first element is 0, 1, 2, 3 and "at least 4" showing the number of 11s. 第一个元素是0,1,2,3和“至少4”表示11的数量。
  • second element is 0, 1, 2 and "at least 3" depending on number of "00"s 第二个元素是0,1,2和“至少3”,取决于“00”的数量
  • third element is the last symbol (0, 1 or "string is empty") 第三个元素是最后一个符号(0,1或“字符串为空”)

It's pretty easy to define the transitions, start states and finish states. 定义转换,启动状态和完成状态非常容易。

So, there's 5 * 4 * 3 = 60 states 所以,有5 * 4 * 3 = 60个州

You may notice that there are some not achievable states and some states that may be united to "failed states" that can reduce the size of automaton significantly 您可能会注意到有一些不可实现的状态和某些状态可能会联合到“失败状态”,这可能会显着减小自动机的大小

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

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