Formal languages / finite automata

NFA to DFA Conversion: The Subset Construction Step by Step

The subset construction turns every reachable set of NFA states into one DFA state. The start is the set holding the NFA start state, a move takes the union of the targets of all states in the set, and a set accepts when it contains an NFA accepting state. The result can have up to 2ⁿ states.

Published
ScopeRegular languages
NFA states in the example3
DFA states in the example4
Worst case2ⁿ states

From an NFA to a table of sets

  1. An NFA with 3 states accepts strings whose second-to-last symbol is 1.

    The NFA has states 0, 1 and 2. State 0 loops on both symbols and also moves to state 1 on a 1. State 1 moves to state 2 on either symbol, and state 2 accepts. The machine accepts strings whose second-to-last symbol is 1. A DFA for the same language cannot guess, so it has to remember more.

  2. The first DFA state is the start set {0}.

    The construction starts with one DFA state, the set that holds only the NFA start state: {0}. Each DFA state stands for all the NFA states the machine could be in at that moment. Before any symbol is read that is exactly one state. The set goes on the list of sets that still need their moves.

  3. From {0}: on 0 to {0}, on 1 to {0,1} (new).

    Take {0} and compute one move per symbol. On 0, state 0 only loops, which gives {0}, a set already known. On 1, state 0 can loop or move to state 1, which gives {0,1}. That set has not appeared before, so it becomes a new DFA state and joins the list to be processed.

  4. From {0,1}: on 0 to {0,2} (new), on 1 to {0,1,2} (new).

    Now take {0,1}. A move is the union of the targets of all states in the set. On 0, state 0 gives {0} and state 1 gives {2}, so the result is {0,2}. On 1, state 0 gives {0,1} and state 1 gives {2}, so the result is {0,1,2}. Both sets are new and join the list.

  5. From {0,2}: on 0 to {0}, on 1 to {0,1}. Nothing is new.

    Take {0,2}. State 2 has no moves, so it adds nothing to the union and only state 0 counts. On 0 the result is {0}, and on 1 it is {0,1}. Both sets already exist, so the list of states stops growing here. Only one set is still waiting for its moves.

  6. From {0,1,2}: on 0 to {0,2}, on 1 to {0,1,2}. Nothing is new.

    Take the last set, {0,1,2}. On 0 the union is {0,2}, and on 1 it is {0,1,2}, which loops back to the same set. Nothing new appears and nothing is waiting, so the construction ends. The table now has 4 DFA states and 8 moves, and every move has a known target.

  7. Every set containing state 2 accepts: {0,2} and {0,1,2}.

    A DFA state accepts when its set contains at least one NFA accepting state. Here that means every set with state 2 in it: {0,2} and {0,1,2}. The rule mirrors NFA acceptance, since a set holds an accepting state exactly when one of the runs it stands for ends in one. The other 2 sets do not accept.

  8. 4 sets are reached out of 8 possible; the next pattern reaches 8 of 16.

    The construction reached 4 sets, which is 2^2 for this pattern with n = 2. The upper bound for 3 NFA states is 8, and the other 4 subsets stay out of reach because every reachable set contains state 0. The pattern with n = 3 has 4 NFA states, a bound of 16 and 8 reached sets. Testing strings finds mistakes but proves no general claim.

  9. Remember one state instead of the set, and 110 is wrongly rejected.

    A machine that keeps one state instead of the set has to pick a target at every branch. Keeping the lowest target stays in state 0 forever and accepts nothing. Keeping the highest one reaches state 2 too early on 110 and then has no move. Of the 4,094 strings up to length 12 in the language, these habits accept 0 and 22. The set accepts them all.

NFA
start 0, 1 1 0, 1 0 1 2

  • ∅
  • {0}
  • {1}
  • {2}
  • {0,1}
  • {0,2}
  • {1,2}
  • {0,1,2}
DFA built so far
Seton 0on 1Accepts

The NFA

An NFA with 3 states accepts strings whose second-to-last symbol is 1.

    Sources: Rabin and Scott, Finite Automata and Their Decision Problems, 1959, Wikipedia, Powerset construction, Columbia University, COMS W3261, University of Washington, CSE 311, Lecture 25, Chalmers University of Technology, DIT321, Lecture 7.

    A DFA can remember one thing only: where it is. An NFA can be in several places at once. The subset construction bridges that gap by letting the DFA remember the set of all states in which the NFA could currently be. It is the standard way to show that nondeterminism adds no power to a finite automaton, and it works on any nondeterministic finite automaton.

    How does the subset construction turn an NFA into a DFA?

    Each state of the new DFA is a set of NFA states. The construction builds only the sets that can actually be reached, which keeps the result small, instead of creating all possible subsets.

    1. Create the first DFA state: the set that contains only the NFA start state, written {q0}.
    2. Take a DFA state that has not been processed yet, which is a set S of NFA states.
    3. For each input symbol x, collect the targets of all NFA moves on x from every state in S. The union of these targets is the set the DFA moves to on x.
    4. If that set is new, add it to the list of unprocessed states.
    5. Repeat from step 2 until no unprocessed state remains.
    6. Mark every set that contains at least one NFA accepting state as accepting.

    The process ends because there are only finitely many subsets of a finite state set. An empty target set is a legitimate DFA state: it stands for "the NFA has no run left", it loops to itself on every symbol, and it never accepts. Whenever a state and a symbol have no NFA move at all, the DFA can use this dead state as the target.

    How are the DFA states, start state and accepting states defined?

    The construction goes back to Rabin and Scott's 1959 paper on finite automata and their decision problems, where the states of the new automaton are all subsets of the old state set. Written out, with T for the NFA transition function and F for its accepting states, the definition has three parts.

    PartDefinition
    StatesSubsets of Q; in practice only the reachable ones
    Start state{q0}
    TransitionT′(S, x) is the union of T(q, x) over all q in S
    Accepting setsevery S with S ∩ F not empty

    The accepting condition expresses the rule for the NFA: a string is accepted if some run ends in an accepting state, and a set contains such a state exactly when at least one of the runs it represents does. In the original paper an automaton may have a set of start states, and the DFA then starts in the set of all of them. With a single start state this is the set {q0}.

    Why the result is correct follows from one invariant. After the DFA has read a string w, its current state is exactly the set of NFA states that some run on w can reach. The invariant holds before any symbol is read, because the start set is {q0}, and each transition of the DFA computes the next such set from the previous one. At the end of the string the DFA accepts exactly when that set contains an accepting NFA state, which is the NFA acceptance condition.

    How do epsilon moves change the conversion?

    Epsilon moves are transitions that consume no input. The conversion handles them with the epsilon-closure of a state, which is the set of states reachable from it through epsilon moves alone. A set of states has the closure of all its members.

    There are three common ways to include it:

    • Remove the epsilon moves from the whole automaton first, which gives an ordinary NFA, then run the subset construction.
    • Compute the closure for each NFA state separately, store it, and use it while building sets.
    • Apply the closure to each set as it is formed. The transition becomes the closure of the usual target set, and the start state of the DFA is the closure of {q0}.

    All three give a DFA for the same language. A four-state example on Wikipedia, for instance, starts in the set {1, 2, 3}, and only five of the sixteen possible subsets are reachable. The set method that Chalmers lecture notes use is the third one.

    Worked example: the second-to-last symbol is 1

    The NFA has states 0, 1 and 2 and accepts strings over {0, 1} whose second-to-last symbol is 1. State 0 loops on both symbols and also moves to state 1 on a 1. State 1 moves to state 2 on either symbol, and state 2 is accepting. The NFA is allowed to guess on a 1 in state 0 that this symbol is the second to last.

    The construction proceeds in four rounds.

    1. Start with {0}. On 0 it goes to {0}. On 1 it goes to {0, 1}, which is new.
    2. For {0, 1}: on 0, state 0 gives {0} and state 1 gives {2}, so the result is {0, 2}. On 1, state 0 gives {0, 1} and state 1 gives {2}, so the result is {0, 1, 2}. Both sets are new.
    3. For {0, 2}: on 0 it goes to {0}, because state 2 has no moves, and on 1 it goes to {0, 1}. Nothing is new.
    4. For {0, 1, 2}: on 0 it goes to {0, 2}, and on 1 it goes to {0, 1, 2}. Nothing is new.
    DFA stateon 0on 1Accepting
    {0}{0}{0, 1}no
    {0, 1}{0, 2}{0, 1, 2}no
    {0, 2}{0}{0, 1}yes
    {0, 1, 2}{0, 2}{0, 1, 2}yes

    The accepting sets are those that contain state 2: {0, 2} and {0, 1, 2}. The result is a DFA with four states and eight transitions. To see it work, read 010. From {0}, the 0 keeps the set at {0}, the 1 moves it to {0, 1}, and the final 0 moves it to {0, 2}. That set is accepting, and indeed the second-to-last symbol of 010 is 1.

    Why can the DFA need 2ⁿ states?

    A set of n NFA states has 2n subsets, so the DFA never needs more than 2n states, and for some NFAs it needs exactly that many. The question is how close real examples get.

    NFA statesUpper bound 2nReached in this language
    3 (second-to-last symbol)84
    4 (third-to-last symbol)168

    The family behind these rows is the language of strings whose n-th symbol from the end is 1. It needs an NFA with n + 1 states, while every DFA for it needs at least 2n states, according to lecture notes from Columbia University. The example above has n = 2, so three NFA states lead to 22 = 4 DFA states. The bound for three NFA states would be 8, and only 4 are reached, because every reachable set contains state 0.

    NFA or DFA: which one should you use?

    NFADFA
    Language classregularregular
    Writing the machine downoften shortermay need up to 2n states
    Running ittrack a set of statesone state per step

    Both machines describe the same languages, so the choice is practical. The NFA is the compact way to specify a language, and the DFA is the form that executes directly. A regular grammar is a third way to describe the same class. The conversion costs time and, in the worst case, exponential size, which is why it is applied when a deterministic machine is really needed.

    The same step appears on the way from patterns to programs. A regular expression can be translated into an NFA by induction on its structure, which is the idea behind the construction usually credited to Thompson, and the subset construction then turns that NFA into a DFA that a program can run symbol by symbol.

    How do you check that the DFA is correct?

    Send chosen strings through both machines and compare the answers. Useful probes are the empty string and the shortest strings that should and should not be accepted. In the example, 11 and 010 are accepted, and 001 is rejected because its second-to-last symbol is 0.

    Running every string over {0, 1} up to length 12, 8,191 in total, through the NFA, the DFA and the plain rule gives identical answers. A test over strings up to a given length finds mistakes but does not prove equality for all strings; the proof is the argument of the construction itself. The reach of both machines ends with the regular languages, and the pumping lemma explains how to show that a language lies beyond them. The overview of formal languages shows where regular languages fit.

    FAQ

    Frequently asked questions

    01What is the difference between DFA and NFA?

    A DFA has exactly one move for each state and input symbol, while an NFA may have several moves or none and accepts when at least one run ends in an accepting state. Both recognize exactly the regular languages. The page on nondeterministic finite automata compares them in detail.

    02How do you convert an epsilon NFA to a DFA?

    Use the subset construction with the epsilon-closure. The start state of the DFA is the closure of the NFA start state, and after each symbol the set of reached states is extended by everything reachable through epsilon moves. A set is accepting if it contains an accepting NFA state.