Question

If I have to draw a deterministic finite automata using a state diagram so that accepts a language, for example {λ ε {a,b}*: the word λ contains an even number of a and an odd number of b}, how do I know how many states I have?

Was it helpful?

Solution

4 states are enough. You have two conditions to satisfy simultaneously: an even number of a's and an odd number of b's. Irrespective of one another, each condition can be either true or false.

If we denote true with 1 and false with 0, we get 4 distinct different possibilities: both are false, one of them (but not the other) is true, or both of them are true. Hence, we get a truth table:

even a | odd b
---------------
   0       0
   0       1
   1       0
   1       1

Let us represent the first row with q[0, 0], the second q[0, 1] and so on. Now we have to specify the transitions for each state, as well as identify our initial state.

Regardless of the state we are in, there are two possible inputs: a or b. Hence, for each state we must specify two transitions.

Now, our initial state is the state we are in before consuming any input. Since 0 is an even number we get that our initial state is q[1, 0]. Our accepting state is when both conditions are satisfied, i.e. q[1, 1].

Finally, we have our state transitions,

q[0, 1] is our initial state

q[1, 0] reads b ->  q[1, 1]
q[1, 0] reads a ->  q[0, 0]

q[1, 1] is our accepting state

q[1, 1] reads b ->  q[1, 0] 
q[1, 1] reads a ->  q[0, 1]

q[0, 1] this state is only ever reached if we have read at least one a

q[0, 1] reads b ->  q[0, 0]
q[0, 1] reads a ->  q[1, 1]  

q[0, 0] this state is only ever reached if we have read at least one a

q[0, 0] reads b ->  q[0, 1]
q[0, 0] reads a ->  q[1, 0]

OTHER TIPS

{λ ε {a,b}*: the word λ contains an even number of a and an odd number of b}

To satisfy this condition we need 4 states. If a number is divisible by 2 then the remainder is 0 representing it is even, else remainder will be 1 saying that it is an odd number

The states in this DFA represents: Q0 - initial state

  • q0 holds even num of a's and even num of b's

Q1 - it holds odd a's and even b's

Q2 - it holds even a's and even b's

Q3 - it holds even a's and odd b's, which is here the final state

this is the dfa for the given problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top