G27
Self-checkedTier 1 9 min readtoc.nfa-dfa-conversion

Subset construction

Converting an NFA (with or without epsilon moves) into an equivalent DFA: the DFA's states are sets of NFA states, the start set is the epsilon closure of the start state, transitions follow all moves and take closures, a set is accepting if it contains an accepting NFA state; building only the reachable subsets, the dead state, the exponential blow-up (the n-th symbol from the end needs 2^n states), and the fact that the result need not be minimal.

Why it exists

Nondeterministic machines are convenient to design but cannot be run directly by simple hardware or a simple program: a DFA can. The subset construction shows that nothing is lost: every NFA can be converted into a DFA for the same language. It is the algorithm that turns the NFA produced from a regular expression into the DFA of a scanner, and its worst case, an exponential blow-up, is one of the standard facts GATE asks.

Prerequisites

You need the NFA, its epsilon closure and the set simulation (NFA).

Precise definitions

Let N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F) be an NFA (it may have ε\varepsilon edges). The subset construction builds the DFA D=(QD,Σ,δD,d0,FD)D = (Q_D, \Sigma, \delta_D, d_0, F_D):

  • the states of DD are sets of states of NN: QD⊆P(Q)Q_D \subseteq \mathcal{P}(Q);
  • the start state is d0=ECLOSE⁡({q0})d_0 = \operatorname{ECLOSE}(\{q_0\});
  • for a set SS and a symbol aa: δD(S,a)=ECLOSE⁡(⋃p∈Sδ(p,a))\delta_D(S, a) = \operatorname{ECLOSE}\bigl(\bigcup_{p \in S} \delta(p, a)\bigr) (every state of SS moves on aa, then the closure is taken);
  • SS is accepting iff S∩F≠∅S \cap F \ne \emptyset.

The empty set ∅\emptyset is a legitimate state: it is the dead state of DD (once reached, no computation of NN survives).

Correctness idea. After reading ww, the DFA is in the set of all NFA states that some computation on ww can be in. So DD accepts ww iff some computation of NN accepts ww.

How it works

The algorithm

  1. Compute d0=ECLOSE⁡({q0})d_0 = \operatorname{ECLOSE}(\{q_0\}) and put it in the list of discovered sets.
  2. Take the next undiscovered-transition set SS from the list. For each symbol aa compute T=δD(S,a)T = \delta_D(S, a). Record the transition. If TT is new, add it to the list.
  3. Stop when every set in the list has all its transitions. Mark as accepting every set that contains a state of FF.

Only sets reachable from d0d_0 are built, so the DFA usually has far fewer than 2∣Q∣2^{|Q|} states.

Example 1: ends with 01

The NFA has states p,q,rp, q, r (pp loops on 0,10, 1; p→0qp \xrightarrow{0} q; q→1rq \xrightarrow{1} r; rr accepting). Start: {p}\{p\}. On 00: p→{p,q}p \to \{p, q\}; on 11: p→{p}p \to \{p\}. From {p,q}\{p, q\}: on 00: p→{p,q}p \to \{p, q\}, qq has no 00-edge: {p,q}\{p, q\}; on 11: p→pp \to p, q→rq \to r: {p,r}\{p, r\}. From {p,r}\{p, r\}: on 00: {p,q}\{p, q\} (rr has no edges); on 11: {p}\{p\}. Three sets, which is the DFA for "ends with 0101" (the {p,r}\{p, r\} contains rr so it accepts).

Subset construction for the NFA of 'ends with 01': start

Computed by a script, not typed
DFA state (set of NFA states)on 0on 1accepting?
{p}
{p,q}
{p,r}

Step 0

Start from {p}. For each set and each symbol take the set of all states reachable by one move; new sets become new DFA states. Only sets that can be reached from {p} are built (3 of the 8 possible subsets).

0 / 3
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

Example 2: the third symbol from the end is 1

The NFA (44 states) leads to 88 reachable subsets; each records which of the last three symbols were 11 (a 11 can be a "guess candidate" at distance 00, 11 or 22 from the end). The DFA below is the result; run it on 0110101101 and compare with the NFA run of the previous page: the DFA is in one state at a time, which is the set that the NFA had active.

Subset construction when the third symbol from the end is 1: start

Computed by a script, not typed
DFA state (set of NFA states)on 0on 1accepting?
{p}
{p,s1}
{p,s2}
{p,s1,s2}
{p,s3}
{p,s1,s3}
{p,s2,s3}
{p,s1,s2,s3}

Step 0

The NFA has 4 states (p, s1, s2, s3). The subset construction produces 8 reachable subsets, and all 8 are needed (the DFA is minimal): the DFA must remember the last 3 symbols.

0 / 8
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

The DFA (8 states) from the subset construction for the same language: start

Simulated, not typed
0101010101010101{p}{p,s1}{p,s2}{p,s1,s2}{p,s3}{p,s1,s3}{p,s2,s3}{p,s1,s2,s3}

Double circle = accepting state. Arrow from nowhere = start state.

input01101
state{{p}}

Step 0

Start in the start state {p}. The machine reads the input from left to right, one symbol at a time, and every symbol moves it along exactly one edge.

0 / 6
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

The blow-up: 2n2^n is reached

For the language "the nn-th symbol from the end is 11" the NFA has n+1n + 1 states and the subset construction produces 2n2^n states, and no DFA for the language has fewer: the 2n2^n strings of length nn are pairwise distinguishable (two different ones differ in some position ii; appending i−1i - 1 symbols puts that position at the nn-th place from the end, accepted after one string and rejected after the other). So the worst case of the construction is exact.

The exponential price of determinism: start

Computed by a script, not typed
nNFA statesDFA states (subset construction)states of the minimal DFA2^n
1
2
3
4
5
6

Step 0

For each n, the language of the binary strings whose n-th symbol from the end is 1, recognised by an NFA with n + 1 states. Converting to a DFA needs 2^n states, and this cannot be improved.

0 / 6
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

For a general nn-state NFA the DFA has at most 2n2^n states, and this bound is reached by the machines above.

Epsilon moves

Take the closure at the start and after every move; the rest is unchanged. For the ε\varepsilon-NFA for a∗b∗a^* b^* (states s0s_0 with an aa-loop and an ε\varepsilon edge to s1s_1, s1s_1 with a bb-loop, accepting): the start set is ECLOSE⁡({s0})={s0,s1}\operatorname{ECLOSE}(\{s_0\}) = \{s_0, s_1\}; on aa: {s0}\{s_0\}, closure {s0,s1}\{s_0, s_1\}; on bb: {s1}\{s_1\}; from {s1}\{s_1\} on aa: ∅\emptyset (dead), on bb: {s1}\{s_1\}. Three DFA states: {s0,s1}\{s_0, s_1\}, {s1}\{s_1\}, ∅\emptyset, the DFA with a dead state that has the language a∗b∗a^* b^*. (The accepting ones are the first two, since they contain s1s_1.)

The result need not be minimal

The NFA for "contains 0000 or 1111" has 44 states; the subset construction produces 55 reachable sets; two of them, {a,s,t}\{a, s, t\} and {b,s,t}\{b, s, t\}, behave identically and are merged by minimisation, leaving 44 states (minimisation). A good procedure is: build the subset DFA, then minimise it.

Subset construction for 'contains 00 or 11': a DFA that is not yet minimal: start

Computed by a script, not typed
DFA state (set of NFA states)on 0on 1accepting?
{s}
{a,s}
{b,s}
{a,s,t}
{b,s,t}

Step 0

The NFA has 4 states. The subset construction builds 5 reachable subsets; two of them are equivalent, so the minimal DFA has only 4 states.

0 / 5
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

Cost of doing it

The construction takes time proportional to the size of the DFA times ∣Σ∣|\Sigma| times the work to compute a set (about ∣Q∣2|Q|^2 per move with closures): exponential in the worst case. In practice compilers build the DFA lazily, only for the states that the input actually reaches, or minimise the NFA first.

Worked examples

EX 1A two-state NFA

Easy

NFA over {a,b}\{a, b\}: δ(1,a)={1,2}\delta(1, a) = \{1, 2\}, δ(1,b)=∅\delta(1, b) = \emptyset, δ(2,a)=∅\delta(2, a) = \emptyset, δ(2,b)={1}\delta(2, b) = \{1\}; start 11, accepting 22. Subset construction: {1}\{1\}: on a→{1,2}a \to \{1, 2\}, on b→∅b \to \emptyset. {1,2}\{1, 2\}: on aa: {1,2}∪∅={1,2}\{1, 2\} \cup \emptyset = \{1, 2\}; on bb: ∅∪{1}={1}\emptyset \cup \{1\} = \{1\}. ∅\emptyset: loops. DFA states {1}\{1\}, {1,2}\{1, 2\} (accepting), ∅\emptyset: 33 states. The language is a(a∣ba)∗a(a \mid ba)^*: the strings that start with aa, end with aa and contain no bbbb. Check: abaaba ends in {1,2}\{1, 2\}, accepted; abab ends in {1}\{1\}, rejected; bb goes to ∅\emptyset, rejected.

EX 2How many states at most

Easy

An NFA with 55 states can give a DFA with at most 25=322^5 = 32 states (counting the empty set). With ε\varepsilon moves the bound is the same. The DFA for the example "fifth symbol from the end is 00" has exactly 3232 states.

EX 3Only reachable subsets

Medium

For the NFA of "ends with 0101" the powerset has 23=82^3 = 8 subsets but only 33 are reachable from {p}\{p\} ({p}\{p\}, {p,q}\{p, q\}, {p,r}\{p, r\}). For the NFA "contains 0000 or 1111" the powerset has 1616 and 55 are reachable. Always build the table by following transitions from the start set, not by listing all subsets.

EX 4With an epsilon move

Medium

NFA: states x,y,zx, y, z; δ(x,ε)={y}\delta(x, \varepsilon) = \{y\}, δ(y,a)={z}\delta(y, a) = \{z\}, δ(z,b)={y}\delta(z, b) = \{y\}; start xx, accepting zz. Start set ECLOSE⁡({x})={x,y}\operatorname{ECLOSE}(\{x\}) = \{x, y\}. On aa: y→zy \to z: {z}\{z\} (no further ε\varepsilon from zz). On bb: nothing: ∅\emptyset. From {z}\{z\}: on aa: ∅\emptyset; on bb: {y}\{y\}. From {y}\{y\}: on aa: {z}\{z\}, on bb: ∅\emptyset. DFA states: {x,y}\{x, y\}, {z}\{z\} (accepting), ∅\emptyset, {y}\{y\}: 44 states. The language is (ab)∗a(ab)^* a. The states {x,y}\{x, y\} and {y}\{y\} behave alike, so minimisation leaves 33 states.

EX 5Typical questions

GATE level

(a) The maximum number of states of the DFA obtained from an nn-state NFA by the subset construction: 2n2^n. (b) Is the DFA from the subset construction always minimal? No. (c) The minimum number of states of a DFA for the language whose nn-th symbol from the end is 11: 2n2^n (the NFA has n+1n + 1). (d) Which subset is the dead state? ∅\emptyset. (e) Which subsets are accepting? Those that contain at least one accepting NFA state. (f) Is the bound 2n2^n ever reached? Yes: the NFA with states s0,s1s_0, s_1 over {a,b}\{a, b\}, edges s0→as1s_0 \xrightarrow{a} s_1, s1→as0s_1 \xrightarrow{a} s_0, s1→as1s_1 \xrightarrow{a} s_1, s1→bs0s_1 \xrightarrow{b} s_0, start and accepting state s0s_0, has a minimal DFA with 4=224 = 2^2 states; for n=3n = 3 and n=4n = 4 machines with 2n2^n DFA states also exist (found by search); in general the bound 2n2^n is tight (a result of Moore).

Cheat box

Cheat box

  • DFA states = sets of NFA states. Start =ECLOSE⁡({q0})=\operatorname{ECLOSE}(\{q_0\}). δD(S,a)=ECLOSE⁡(⋃p∈Sδ(p,a))\delta_D(S, a) = \operatorname{ECLOSE}(\bigcup_{p \in S} \delta(p, a)). Accepting iff the set meets FF. ∅\emptyset is the dead state.
  • Build only reachable subsets (follow transitions from the start set).
  • At most 2n2^n states. The nn-th symbol from the end is 11: NFA n+1n + 1 states, minimal DFA 2n2^n.
  • The result may be non-minimal: minimise afterwards.
  • Epsilon NFA: closure at the start and after each move.

Traps and GATE patterns

Connections

The construction is the second half of "regular expression to scanner" (lexical analysis); the DFA it produces is then reduced by minimisation; combined with the product it gives the closure of regular languages under complement (closure properties).

PYQs

5 previous year questions from your book. I re-solved each one myself (2 by running or simulating, 3 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · Set 1 · MSQ · 1 markQ1 · page 197 of the book
Let M be a nondeterministic finite automaton (NFA)
with 6 states over a finite alphabet. Which of the
following options CANNOT be the number of states in
the minimal deterministic finite automaton (DFA) that is
equivalent to M?
  • (a). 32
  • (b). 65
  • (c). 1
  • (d).128
GATE 2025 · Set 1 · MSQ · 1 markQ4 · page 197 of the book
A regular language L is accepted by a non-deterministic
finite automaton (NFA) with n states. Which of the
following statement(s) is/are FALSE?
  • (a). L may have an accepting NFA with < n states.
  • (b). L may have an accepting DFA with < n states.
  • (c). There exists a DFA with ≤ 2n states that accepts L.
  • (d). Every DFA that accepts L has > 2n states.
GATE 2018 · MCQ · 1 markQ29 · page 201 of the book
Let N be an NFA with n states. Let k be the number of
states of a minimal DFA which is equivalent to N. Which
one of the following is necessarily true?
  • (a)k ≥ 2n
  • (b)k ≥ n
  • (c)k ≤ n2
  • (d)k ≤ 2n
GATE 2011 · MCQ · 1 markQ55 · page 205 of the book
Which of the following pairs have DIFFERENT
expressive power?
  • (a)Deterministic finite automata (DFA) and Non- deterministic finite automata (NFA)
  • (b). Deterministic push down automata (DPDA) and Non-deterministic push down automata (NPDA)
  • (c). Deterministic single-tape Turning machine and Non-deterministic single tape Turning machine
  • (d). Single-tape Turning machine and multi-tape Turning machine
GATE 2005 · MCQ · 2 marksQ79 · page 211 of the book
Consider the regular grammar:
S → Xa | Ya
X → Za
Z → Sa | ε
Y → Wa
W → Sa
where S is the starting symbol, the set of terminals is
{a} and the set of non-terminals is {S, W, X, Y, Z}. We
wish to construct a deterministic finite automaton
(DFA) to recognize the same language. What is the
minimum number of states required for the DFA?
  • (a)2
  • (b)3
  • (c)4
  • (d)5 THEORY OF COMPUTATION

Self-check

Question 1

An NFA has 33 states, no ε\varepsilon edges. What is the maximum number of states in the DFA from the subset construction?

Show answer

23=82^3 = 8 (including the empty set).

Question 2

In the subset construction for an NFA with accepting state q2q_2, is {q1,q2}\{q_1, q_2\} accepting?

Show answer

Yes: it contains q2q_2.

Question 3

What is the start state of the DFA built from an ε\varepsilon-NFA with start q0q_0?

Show answer

ECLOSE⁡({q0})\operatorname{ECLOSE}(\{q_0\}).

Question 4

Why does the DFA for "the 44th symbol from the end is 11" need 1616 states?

Show answer

The 1616 strings of length 44 are pairwise distinguishable, so any DFA must be in 1616 different states after reading them.

Revision card

One-screen revision

  • Sets of NFA states; start closure; move then closure; accept iff meets FF; ∅\emptyset = dead.
  • Reachable subsets only; at most 2n2^n; nn-th symbol from the end: 2n2^n exactly.
  • Not necessarily minimal; minimise next.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 1.2 (Theorem 1.39: every NFA has an equivalent DFA)
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 2.3.5 and Section 2.5.5 (the subset construction, eliminating epsilon transitions)
  • ·Aho, Lam, Sethi, Ullman, Compilers: Principles, Techniques, and Tools (2nd edition), Section 3.7.1 (conversion of an NFA to a DFA)

Verification

The construction in scripts/toc_lib.py was run on 400 random NFAs (half with epsilon moves): the resulting DFA was complete, had only reachable states and at most 2^n states, was equivalent to the NFA by a search of the product automaton and by comparing acceptance on every string up to length 8. For the n-th symbol from the end the subset construction gave exactly 2^n states and minimisation merged none of them, for n = 1 to 6; the 2^n strings of length n have pairwise different futures, which proves the lower bound for n up to 5. The tables are the output of that construction. For 'contains 00 or 11' the construction gave 5 states and the minimal DFA has 4. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never