✖

Converting an NFA to a DFA

We will show how to convert a non-deterministic automaton to a deterministic automaton, which proves that non-deterministic and deterministic automata have equal computational power.

The basic idea

Take a non-deterministic automaton ⟨QN, ΣN, δN, q0, FN⟩ without epsilon transitions. We construct a deterministic automaton ⟨QD, ΣD, δD, p0, FD⟩ as follows:

  • QD ⊆ 2QN
  • ΣD = ΣN
  • p0 = {q0}
  • FD = {Q ∈ QD | Q ∩ FN ≠ ∅}

We define the transition function δD as follows:

∀Q∈QD,a∈ΣD:δD(Q,a)=⋃q∈QδN(q,a)

What does this mean? A non-deterministic automaton can be in several states at once, so, for example, we can reach a situation where our non-deterministic automaton is in the states {q0, q1}. By reading the next symbol, say 1, we can get to the set of states {q0, q2, q3}. In the deterministic version of the same automaton this appears as follows: we create two states, name them {q0, q1} and {q0, q2, q3} and introduce a transition between them for the symbol 1. Formally we move from one state to the other, but thanks to the naming we know that in the non-deterministic automaton we would find ourselves in two states, or three states respectively.

Example

Take this non-deterministic automaton:

To construct the deterministic automaton we first need to build the new transition function. A table in which we record all the transitions will help us. At the start the table looks like this:

state/symbol01{q0}

At the end of the algorithm the left-hand column will contain all the states of the deterministic automaton. The completed table will then define the transition function. Now we write into the table which states we get to if we are in the state {q0} and the input symbols are 0 and 1.

state/symbol01{q0}{q1}{q1,q2}

From state q0 there is a transition for the symbol 0 only to state q1, whereas for the symbol 1 it leads to states q1 and q2. Now we add two new states to the table: {q1} and {q1, q2}. Why? Whenever we write into the table a set of states that is not yet in the left-hand column, we add that set of states there.

state/symbol01{q0}{q1}{q1,q2}{q1}{q1,q2}

and we fill it in again. For the state {q1, q2} we must not forget to check both states, that is, the union of where we get to from state q1 and where we get to from state q2.

state/symbol01{q0}{q1}{q1,q2}{q1}{q0,q1,q3}{q0,q3}{q1,q2}{q0,q1,q3}{q0,q3}

We have got several new states, which we must add to the table:

state/symbol01{q0}{q1}{q1,q2}{q1}{q0,q1,q3}{q0,q3}{q1,q2}{q0,q1,q3}{q0,q3}{q0,q1,q3}{q0,q3}

…and again we fill in the transitions:

state/symbol01{q0}{q1}{q1,q2}{q1}{q0,q1,q3}{q0,q3}{q1,q2}{q0,q1,q3}{q0,q3}{q0,q1,q3}{q0,q1,q3}{q0,q1,q2,q3}{q0,q3}{q1}{q1,q2}

We already have the states {q0, q1, q3}, {q1} and {q1,q2} in the table, so we add only the state {q0,q1,q2,q3}.

state/symbol01{q0}{q1}{q1,q2}{q1}{q0,q1,q3}{q0,q3}{q1,q2}{q0,q1,q3}{q0,q3}{q0,q1,q3}{q0,q1,q3}{q0,q1,q2,q3}{q0,q3}{q1}{q1,q2}{q0,q1,q2,q3}{q0,q1,q3}{q0,q1,q2,q3}

We have not added any new state, so the table is complete. Now we just draw the diagram. The states of the deterministic automaton are in the first column, and the transitions for the symbols 0 and 1 are in the other columns. Every state that contains an accept state of the original automaton will also be an accept state in this automaton. That is, every state containing q3 will be an accept state. The automaton will look like this:

We can show that the automaton really works correctly. Let’s try to accept the string 11001. In the deterministic automaton we follow the path

q0→1q1,q2→1q0,q3→0q1→0q0,q1,q3→1q0,q1,q2,q3

and the automaton would accept the string, because the last state {q0, q1, q2, q3} is an accept state. In the non-deterministic automaton, during a simulation, we would get to the same states: we would start in state q0 and for the symbol 1 we would get to the states {q1, q2}. The next symbol 1 would take us to the states {q0,q3}, and so on for the other states. In the end we would find ourselves in all the states {q0, q1, q2, q3} and the automaton would accept the string.

Sources

Chapters: Finite-state machine, Complete automaton, Non-deterministic finite automaton, Simulating a non-deterministic automaton, Converting an NFA to a DFA