What finite-state machine captures binary strings with equal numbers of '01' and '10'?

前端 未结 2 1331
[愿得一人]
[愿得一人] 2021-01-23 03:46

I need help designing a finite state machine that accepts binary strings containing as many occurrences of the pattern 01 as occurrences of the pattern 10

2条回答
  •  一生所求
    2021-01-23 04:13

    What is the language in question?

    [...] binary strings containing as many occurrences of the pattern 01 as occurrences of the pattern 10. I kinda have a hard time understanding exactly which strings should be accepted and which should be rejected.

    The language defined by your specs is in fact none other than the set composed of

    • the empty string,
    • all strings that start and end with the same character.

    The empty string is accepted because it contains zero occurrences of either pattern; easy. To understand why all non-empty accepted strings must start and end with the same character, instead of coming up with a formal proof, let's take a look at a couple of examples. I'll use

    • -- to highlight occurrences of the 01 pattern, and
    • ** to highlight occurrences of the 10 pattern.

    String 10001010

    This string contains

    • 2 occurrences of 01, and
    • 3 occurrences of 10,

    as shown below:

    10001010
    **  ****
       ----
    

    Therefore, it is not accepted. Note that it doesn't start and end with the same character.

    String 11001111

    This string contains

    • 1 occurrence of 01, and
    • 1 occurrence of 10,

    as shown below:

    11001111
     **--
    

    Therefore, it is accepted. Note that it starts and ends with the same character (1).

    You get the idea...

    A finite-state machine that describes the language in question

    I need help designing a finite state machine [...]

    Here is an FSM that describes the language in question:

    enter image description here

    To convince yourself that it does indeed describe the language of interest here, you can think of

    • s0 as the state that accepts only the empty string;
    • s1 as the state that accepts only strings that start and end with a 0;
    • s2 as the state in which the next character needs to be a 0 for the input string so far to get accepted;
    • s3 as the state that accepts only strings that start and end with a 1;
    • s4 as the state in which the next character needs to be a 1 for the input string so far to get accepted.

    Bonus!

    Here the LaTeX code I wrote for drawing the FSM above.

    \documentclass{standalone}
    
    \usepackage{tikz}
    \usetikzlibrary{
      automata,
      positioning,
    }
    
    \begin{document}
      \begin{tikzpicture}[
        node distance=2cm,
        on grid,
        auto,
        scale=.8,
        transform shape,
      ]
        \node[state, initial, accepting] (s0)                     {$s_0$};
        \node[state,          accepting] (s1) [above right=of s0] {$s_1$};
        \node[state                    ] (s2) [right      =of s1] {$s_2$};
        \node[state,          accepting] (s3) [below right=of s0] {$s_3$};
        \node[state                    ] (s4) [right      =of s3] {$s_4$};
    
        \path[->] (s0) edge              node        {0}    (s1)
                  (s1) edge [bend left]  node        {1}    (s2)
                       edge [loop above] node        {0}    ()
                  (s2) edge [loop right] node        {1}    ()
                       edge [bend left]  node        {0}    (s1);
        \path[->] (s0) edge              node [swap] {1}    (s3)
                  (s3) edge [bend right] node [swap] {0}    (s4)
                       edge [loop below] node        {1}    ()
                  (s4) edge [loop right] node        {0}    ()
                       edge [bend right] node [swap] {1}    (s3);
      \end{tikzpicture}
    \end{document}
    

提交回复
热议问题