Sub topic 5
Converting an NFA to a DFA with subset construction
Prerequisites and goal
This chapter assumes you know how a DFA and an NFA run on a string, and what epsilon-closure means. If you need a refresher, review the chapters on nondeterminism and epsilon transitions first.
An NFA and a DFA recognize exactly the same class of languages. The proof is constructive. Given any NFA, the subset construction builds a DFA whose states are sets of NFA states. By the end you should be able to run the construction by hand, count the resulting states, and recognize the worst case that GATE likes to test.
The core idea
An NFA reading a string can be in several states at once. Its configuration after each symbol is a set of states. That set is always one of finitely many subsets of Q, so you can treat each subset as a single DFA state. The DFA then moves deterministically from one set to the next.
Given an NFA N = (Q, Σ, δ, q0, F), the DFA D = (Q', Σ, δ', q0', F') is defined as follows.
| Component | Definition |
|---|---|
| States Q' | Subsets of Q (only reachable ones are needed) |
| Start state q0' | {q0}, or ε-closure({q0}) if N has epsilon moves |
| Transition δ'(S, c) | The union of δ(s, c) over all s in S, then ε-closed |
| Accepting states F' | Every subset S with S ∩ F ≠ ∅ |
The accepting rule uses "at least one", because the NFA accepts if any of its possible paths ends in a final state.
Worked example: (a|b)*abb
Take the NFA with states 0, 1, 2, 3. State 0 loops on a and b. On a, state 0 also goes to 1. State 1 goes to 2 on b. State 2 goes to 3 on b. State 3 is accepting.
Start with {0}. For each new set, compute its move on a and on b.
- From {0}: on a, the targets are {0, 1}. On b, the target is {0}.
- From {0,1}: on a, state 0 gives {0,1} and state 1 has no a-move, so the result is {0,1}. On b, state 0 gives {0} and state 1 gives {2}, so the result is {0,2}.
- From {0,2}: on a, only state 0 contributes, giving {0,1}. On b, state 0 gives {0} and state 2 gives {3}, so the result is {0,3}.
- From {0,3}: on a, the result is {0,1}. On b, the result is {0}.
No new sets appear, so the construction stops. The transition table is:
| DFA state | On a | On b | Accepting? |
|---|---|---|---|
| {0} | {0,1} | {0} | No |
| {0,1} | {0,1} | {0,2} | No |
| {0,2} | {0,1} | {0,3} | No |
| {0,3} | {0,1} | {0} | Yes |
The NFA has 4 states, and the DFA also has 4. Only 4 of the 16 possible subsets were reachable.
The algorithm in code
The standard way to run the construction is a worklist. You keep a queue of sets whose transitions are not yet computed, and you add a set to the queue only when you first see it. This is why only reachable subsets are built.
from collections import deque
nfa = {(0, 'a'): {0, 1}, (0, 'b'): {0}, (1, 'b'): {2}, (2, 'b'): {3}}
def subset_construction(start, accepting, alphabet):
s0 = frozenset([start])
seen = {s0}
queue = deque([s0])
delta = {}
while queue:
S = queue.popleft()
for c in alphabet:
T = frozenset(q for s in S for q in nfa.get((s, c), set()))
delta[(S, c)] = T
if T not in seen:
seen.add(T)
queue.append(T)
dfa_accepting = {S for S in seen if S & accepting}
return seen, delta, dfa_accepting
states, delta, acc = subset_construction(0, {3}, "ab")
print(len(states)) # 4 reachable DFA states
print(len(acc)) # 1 accepting DFA stateThe code uses frozenset because ordinary sets cannot be dictionary keys or members of other sets. This code does not handle epsilon moves. For an epsilon-NFA, replace the start set with its epsilon-closure and close every computed set T before storing it.
The empty set is a state
Sometimes the move on a symbol produces the empty set. The DFA must still have a transition, so ∅ becomes a real state. It is a dead (trap) state: it has self-loops on every symbol and is never accepting.
In the example above, the empty set never appeared because every state could still move on both symbols. In the code, such a transition would be stored as frozenset(), which is the dead state. Do not drop it when you count states. A complete DFA includes the trap state if it is reachable.
Handling epsilon moves
If the NFA has epsilon transitions, modify two steps.
- The start state of the DFA is ε-closure({q0}), not {q0}.
- Each transition is computed as ε-closure(move(S, c)), where move(S, c) is the union of δ(s, c) over all s in S.
A common error is to take the closure only at the start. You must close after every symbol, otherwise states reachable by silent moves are missed.
Size and the worst case
An NFA with n states can produce a DFA with up to 2^n states, since there are 2^n subsets. This bound is tight: some NFAs really need that many.
The classic example is the language of binary strings whose n-th symbol from the end is 1. An NFA needs only n + 1 states. It loops on the start state, guesses that the current 1 is the n-th from the end, then counts n − 1 more symbols. Any DFA for this language must remember the last n symbols, so it needs at least 2^n states.
Practical facts to remember:
- The 2^n figure is an upper bound, not a typical count. Most NFAs give far fewer reachable subsets.
- Counting reachable subsets, not all subsets, is what the algorithm does.
- The conversion can blow up, but a DFA never needs more than 2^n states for an n-state NFA.
- The result of subset construction may not be minimal. Two different subsets can behave identically, so run minimization afterward if the question asks for the smallest DFA.
Why the construction is correct
The proof is by induction on the length of the input string w. The claim is that after reading w, the DFA is in the set of exactly those NFA states that some path on w can reach.
- Base case: for the empty string, the DFA is in the start set, which is {q0} (closed under epsilon moves). That is exactly what the NFA can reach.
- Inductive step: suppose that after w the DFA is in S, the set of NFA states reachable on w. Reading c, the NFA can reach exactly the states in the union of δ(s, c) over s in S. That is what δ' computes, so the claim holds for wc.
Acceptance follows because the NFA accepts w exactly when some reachable state is final, which is exactly when the DFA's set meets F.
Common errors
- Writing {0,1} as a state and then forgetting to compute its transitions.
- Marking a subset accepting only when it equals F, rather than when it intersects F.
- Listing all 2^n subsets as states when only reachable ones are needed.
- Dropping the dead state ∅ when counting states of a complete DFA.
- Skipping the epsilon-closure of the start set or of later sets.
GATE patterns
Typical questions ask you to do one of the following.
- Give the number of states of the DFA obtained by subset construction from a small NFA. Run the worklist by hand and count only reachable subsets, including ∅ if it occurs.
- State the maximum number of DFA states for an n-state NFA. The answer is 2^n.
- Decide which statement about NFAs and DFAs is true. NFAs and DFAs accept the same languages, but NFAs can be exponentially smaller.
- Find the minimum DFA states for "the n-th symbol from the end is 1". The answer is 2^n.
Exercise
An NFA has states {p, q} over the alphabet {0, 1}. State p goes to {p, q} on 0 and to {p} on 1. State q goes to no state on 0 and to {q} on 1... wait, define it exactly: q has no transitions on 0 and goes to {p} on 1. The start state is p and the accepting state is q. Using subset construction, list the reachable DFA states, and say which are accepting.
Show answer
Start with {p}. On 0 it moves to {p,q}. On 1 it moves to {p}. From {p,q}: on 0, p gives {p,q} and q gives nothing, so the result is {p,q}. On 1, p gives {p} and q gives {p}, so the result is {p}. No other sets appear. The reachable states are {p} (not accepting) and {p,q} (accepting, since it contains q). The DFA accepts exactly the strings that end in 0. The empty set never occurs, so there is no dead state.
Exercise
An NFA with 5 states is converted to a DFA by subset construction. What is the largest possible number of DFA states, and why does a typical conversion produce fewer?
Show answer
The largest possible number is 2^5 = 32, one for each subset of the 5 NFA states, including the empty set. A typical conversion produces fewer because the algorithm builds only the subsets reachable from the start set, and many subsets can never be reached.