automatadfa

DFA that will accepts the string having odd number of 1's and odd number of 0's


I want the DFA generation, that will accepts the string having odd number of 1's and odd number of 0's.


Solution

  • First, let's build a DFA that accepts an odd number of 0. We need at least one state or else we can't accept anything. That state can't be accepting since the empty string leads there and the empty string doesn't have an odd number of 0. So, we need at least two states - an initial state that is not accepting, and an accepting state. Do we need more?

    To answer that question let's start filling in the transitions and see where we get. There must be a transition originating in the accepting state. Where does it go? If it goes to itself, then we don't accept the string 0, which has an odd number (one) of 0. So we need to go to some accepting state on 0 in the initial state. We just so happen to have an accepting state already; let's go there.

    Next, we must have a transition from the accepting state. If we return to the accepting state, we would accept the string 00, so we can't do that. We must transition to some non-accepting state. We have a non-accepting state already - our initial state - so that choice might work. Alternatively, if not, we must introduce a new state. Let us first consider whether returning to the initial state works, since in that case we are done.

    We have already reasoned that the strings 0 and 00 are handled correctly. From then on, 000 will be handled correctly since we return to the accepting state from the initial state on the subsequent 0; indeed, we return to the initial state on 0^2k and to the accepting state on 0^(2k+1), for k >= 0. Therefore, this DFA is correct for the language of strings of odd numbers of 0. The diagram looks like:

            /---0----\
            |        |
            V        |
    ----->(q0)--0-->(q1)
    

    By changing the labels we can get an automaton for the language of strings of odd numbers of 1:

            /---1----\
            |        |
            V        |
    ----->(q2)--1-->(q3)
    

    To get an automaton accepting strings containing odd numbers of both 0 and 1, imagine running both automata simultaneously: whenever we see a 0, we pass it to the first one, and whenever we see a 1, we pass it to the second one. Then, we accept if both automata ended up in accepting states. We can represent the combined state of the two automata by considering all four pairs of states from the first and second automata as states of a new, combined automaton, the transitions graph of which looks like this:

              /----0----\
              |         |
              V         |
    ----->(q0,q2)--0-->(q1,q2)
           ^  |         ^  |
           |  1         |  1
           1  |         1  |
           |  V         |  V
          (q0,q3)--0-->(q1,q3)
              ^         |
              |         |
              \----0----/
    

    These are the intuitions behind the Myhill-Nerode theorem on regularity of languages and the Cartesian product machine construction for the intersection of regular languages.