Nondeterministic Finite Automata and the Subset Construction
Anchor (Master): Sipser 2013 Introduction to the Theory of Computation 3e (Cengage) §1.2; Hopcroft, Motwani & Ullman 2006 Introduction to Automata Theory, Languages, and Computation 3e (Pearson) §2.3-2.5 (subset construction, exponential blow-up, equivalence proof, Thompson construction from regular expressions)
Intuition Beginner
Imagine you are walking through a maze and you reach a fork. Instead of choosing one path and hoping for the best, you clone yourself. One copy goes left, the other goes right. Every time any copy reaches another fork, it clones again. At the end, if even one copy finds the exit, the whole expedition succeeds.
A nondeterministic finite automaton (NFA) works exactly this way. Unlike a DFA, which has exactly one transition from each state for each symbol, an NFA can have zero, one, or many transitions. It can also take special "epsilon-transitions" that let it jump to another state without reading any input symbol at all.
The NFA accepts a string if there exists at least one path through the machine that ends in an accept state. It does not matter that most paths might fail. One success is enough. This "guess and check" model is powerful for designing automata, because you can encode the correct guess into the machine's transitions.
The surprising fact is that NFAs are no more powerful than DFAs. Every NFA can be converted into a DFA that accepts exactly the same language. The conversion, called the subset construction, tracks all possible states the NFA could be in simultaneously. The price is that the DFA may need exponentially more states than the NFA, but it is always finite.
Visual Beginner
An NFA for strings over {0, 1} that contain "01" or "10" as a substring:
0 1
┌───────┐ ┌───────┐
↓ │ ↓ │
→ [q0] ─ε─→ [q1] ─ε─→ [q3]
│ │ 1 │ 0
│ ↓ ↓
│ [[q2]] [[q4]]
│
└─────────────────────More precisely, q0 has epsilon-transitions to q1 and q3. From q1, reading 0 goes to q2 (accept). From q3, reading 1 goes to q4 (accept). Both q2 and q4 have self-loops on {0, 1}.
| Input string | Possible paths | Accepted? |
|---|---|---|
| 01 | q0 →ε q1 →0→ q2 (accept) | Yes |
| 10 | q0 →ε q3 →1→ q4 (accept) | Yes |
| 00 | q0 →ε q1 →0→ q2 (accept) | Yes (contains 01? No, but 0 then ε stays) |
| 011 | q0 →ε q1 →0→ q2 →1→ q2 | Yes |
Wait — let me correct this. The NFA should accept strings containing "01" or "10". The trace for "00" should be: q0 →ε q1 →0→ q2 →0→ q2 (q2 accepts, since it already saw "01"... but "00" does not contain "01" or "10"). Let me redraw with the correct NFA.
Correct NFA: q0 is start. From q0, reading 0 goes to q1; reading 1 goes to q3. From q1, reading 1 goes to q2 (accept). From q3, reading 0 goes to q4 (accept). States q2 and q4 have self-loops on {0, 1}. q0 has self-loops on {0, 1}. q1 has a self-loop on 0. q3 has a self-loop on 1.
0,1 0 1
→ [q0] ──→ [q1] ──→ [[q2]] (self-loop 0,1)
│ 1 │
↓ ↓ self-loop 0
[q3] ──→ [[q4]] (self-loop 0,1)
0| Input | Trace to accept state | Accepted? |
|---|---|---|
| 01 | q0 →0→ q1 →1→ q2 | Yes |
| 10 | q0 →1→ q3 →0→ q4 | Yes |
| 00 | No path reaches q2 or q4 | No |
| 0110 | q0 →0→ q1 →1→ q2 (accept) | Yes |
Worked example Beginner
We design an NFA for the language of all strings over {0, 1} that contain either "01" or "10" as a substring.
Strategy. The NFA should nondeterministically guess where the desired substring begins. Until that point, it stays in a start state that loops on both symbols. Once it guesses, it checks for "01" or "10".
States. State q0 is the start, meaning "waiting to start matching." State q1 means "just saw a 0, checking for a 1 next." State q2 means "matched 01, accept." State q3 means "just saw a 1, checking for a 0 next." State q4 means "matched 10, accept."
Transitions. From q0, reading 0 can stay in q0 (keep waiting) or go to q1 (start matching 01). Reading 1 can stay in q0 or go to q3 (start matching 10). From q1, reading 1 goes to q2. Reading 0 stays in q1 (still waiting for the 1). From q3, reading 0 goes to q4. Reading 1 stays in q3. From q2 and q4, both symbols loop back (already accepted).
Trace "010". Start at q0. Read 0: can be in {q0, q1}. Read 1: from q0 go to {q0, q3}, from q1 go to q2. So states are {q0, q2, q3}. Since q2 is an accept state, the string is accepted. It contains "01."
Trace "00". Start at q0. Read 0: {q0, q1}. Read 0: from q0 go to {q0, q1}, from q1 stay at q1. States are {q0, q1}. Neither is accepting. Correctly rejected.
Check your understanding Beginner
Formal definition Intermediate+
Definition (NFA). A nondeterministic finite automaton is a 5-tuple where:
- is a finite set of states,
- is a finite input alphabet,
- is the transition function,
- is the start state,
- is the set of accept states.
The transition function returns a set of states (possibly empty) for each state-symbol pair. This is the key departure from the DFA, where is a single state.
Definition (epsilon-closure). The epsilon-closure of a set , denoted , is the set of all states reachable from any state in by following zero or more epsilon-transitions. Formally, it is the smallest set such that for every and every , we have .
Definition (extended transition function). The extended transition function is defined by:
Definition (accepted language). The NFA accepts a string if . The language recognised by is
Subset construction
Theorem (Rabin-Scott). For every NFA there exists a DFA such that . Consequently, a language is recognised by an NFA if and only if it is regular.
Proof (construction). Given , construct the DFA where:
- (all subsets of ),
- ,
- ,
- .
By induction on , . The base case holds by definition of . The inductive step follows from the definition of . Then iff iff iff .
The DFA has at most states where . Not all subsets may be reachable, so the actual number can be smaller.
Counterexamples to common slips
"NFAs are more powerful than DFAs because they can guess." Nondeterminism is a design convenience, not a computational upgrade. The subset construction proves that every NFA language is a DFA language.
"The subset construction always produces states." Many subsets may be unreachable from the start state. The actual reachable subset is often much smaller.
"Epsilon-transitions make NFAs more powerful than DFAs." Epsilon-transitions are subsumed by the subset construction through the epsilon-closure step. They add convenience, not power.
Key theorem with proof Intermediate+
Theorem (exponential blow-up is tight). For each , the language is recognised by an NFA with states but requires a DFA with at least states.
Proof (NFA). Construct with states , start state , accept state . From , reading 1 goes to and reading 0 goes to . From for , both 0 and 1 go to . State has no outgoing transitions. The NFA nondeterministically guesses that the current 1 is the -th symbol from the end and then counts exactly more symbols. This uses states.
Proof (DFA lower bound). Suppose a DFA with fewer than states recognises . By the pigeonhole principle, there exist two distinct strings with . Let be a position where , say and . Append symbols to both. Then (the -th symbol from the right is ) but (the -th symbol from the right is ). Yet both strings reach the same state, a contradiction.
Bridge. The subset construction is the foundational reason NFAs and DFAs recognise the same class of languages: every nondeterministic computation can be simulated by tracking all possible states simultaneously. This builds toward the Thompson construction of 47.01.03, where regular expressions are compiled into NFAs that are then converted to DFAs, and it appears again in 47.01.04 where the pumping lemma applies uniformly to both models. The central insight is that nondeterminism is a convenience for design, not an increase in power — the product construction of 47.01.01 and the subset construction are both instances of building a new automaton whose states encode information about the original machine's configuration. Putting these together yields the equivalence of three models: DFAs, NFAs, and regular expressions, which is the structural backbone of the theory of regular languages.
Exercises Intermediate+
Lean formalization Intermediate+
Mathlib has Computability.NFA with NFA.δ, NFA.evalFrom, and NFA.accepts defining the language recognised by an NFA, along with Computability.DFA for deterministic machines. The subset construction converting an NFA to a DFA is not formalised: there is no NFA.toDFA with a proof that DFA.accepts (NFA.toDFA N) = NFA.accepts N. The epsilon-closure operation and its integration into the extended transition function are also absent. The foundational gap for formalisation is the subset construction correctness proof, which requires building the power-set DFA and showing by induction that its extended transition function tracks the NFA's reachable state sets. This unit ships without a lean_module.
Advanced results Master
Three results sharpen the relationship between nondeterminism and determinism.
Theorem 1 (NFA closure under star). If is recognised by an NFA with states, then is recognised by an NFA with states.
Proof. Let recognise . Construct where is a new start state that is also an accept state (for the empty string), , and for each , . Every time the NFA reaches an accept state, the epsilon-transition allows it to restart, accepting any concatenation of strings from . The new state ensures the empty string is accepted.
Theorem 2 (NFA closure under complement via subset construction). If is recognised by an NFA, then is recognised by a DFA.
The proof runs the subset construction to obtain a DFA with , then flips the accept states to obtain with . The cost is exponential in the NFA state count.
Theorem 3 ( succinctness gap). For infinitely many , there exist languages recognised by NFAs with states that require DFAs with states.
This follows from the languages above: an -state NFA requires at least DFA states. The exponential gap is inherent — it is not an artifact of the subset construction but a fundamental separation between the two models in terms of descriptional complexity. This has practical consequences for regular expression engines, which often keep the NFA representation to avoid exponential blow-up [Hopcroft, Motwani & Ullman §2.5].
Synthesis. The subset construction is the foundational reason nondeterminism does not extend the class of regular languages: every NFA can be determinised at exponential cost. The central insight is that nondeterminism is a descriptive tool — it makes automata easier to design and compose — while the DFA is the operational model used for execution. This is exactly the duality that appears in the Thompson construction of 47.01.03, where regular expressions compile to NFAs for convenience and then to DFAs for simulation. Putting these together with the product construction of 47.01.01 and the pumping lemma of 47.01.04 yields the full equivalence of the three characterisations of regularity: DFAs, NFAs, and regular expressions. The bridge is that the subset construction generalises the product construction — both build a new automaton whose states encode sets of configurations of the original machine.
Full proof set Master
Proposition 1 (subset construction correctness). Let be an NFA without epsilon-transitions. Let be the subset-constructed DFA where and . Then for all , .
Proof. By induction on . Base case: . .
Inductive step: assume for some . Let . For symbol :
The last equality is the definition of the extended NFA transition function.
Proposition 2 (NFA union construction correctness). Let and be NFAs over . The NFA constructed with new start state and satisfies .
Proof. If , then there is an accepting path in from . The NFA can take the epsilon-transition from to and follow this path, accepting . The same holds for . Conversely, any accepting path in must start with for some , then follow a path entirely within the state set of . Hence .
Proposition 3 (exponential lower bound for ). Any DFA recognising has at least states.
Proof. For each string , define . For distinct , let be a position where . Then and differ in membership in : the -th symbol from the right of is and of is , and these differ. Hence and are Myhill-Nerode distinguishable. There are pairwise distinguishable strings, so any DFA needs at least states.
Connections Master
47.01.01— The DFA is the deterministic special case of the NFA; the product construction for intersection of DFAs is the structural dual of the subset construction, both building new automata from power-set configurations.47.01.03— Regular expressions compile to NFAs via the Thompson construction; the subset construction then converts those NFAs to DFAs, completing the equivalence loop of Kleene's theorem.47.01.04— The pumping lemma applies to any regular language regardless of whether it is specified by a DFA or an NFA; the subset construction guarantees a DFA always exists to which the pigeonhole argument applies.50.03.01— The subset construction runs in time, illustrating the exponential gap between nondeterministic and deterministic simulation that reappears in the P vs NP question of47.02.01.
Historical & philosophical context Master
The concept of nondeterminism in finite automata was introduced by Michael Rabin and Dana Scott in their 1959 paper "Finite Automata and Their Decision Problems" [Rabin & Scott 1959]. Their key insight was that allowing a machine to "guess" which transition to follow does not change the class of recognisable languages, because the subset construction can simulate all guesses simultaneously.
The subset construction is sometimes called the "powerset construction" because the DFA states are subsets (elements of the power set) of the NFA states. Rabin and Scott showed that the exponential blow-up is inherent: some NFAs with states genuinely require DFA states. This exponential gap was one of the first examples of a computational resource trade-off, anticipating the later study of complexity classes.
Epsilon-transitions were formalised as part of the Thompson construction, developed by Ken Thompson for the QED text editor in 1968. Thompson's algorithm compiles a regular expression into an NFA with epsilon-transitions in linear time and space, which is then simulated (without full determinisation) for string matching. This approach is still used in modern regular expression engines that prefer backtracking or on-the-fly subset simulation over full DFA construction [Sipser §1.2].
Bibliography Master
@book{sipser2013,
author = {Sipser, Michael},
title = {Introduction to the Theory of Computation},
edition = {3rd},
publisher = {Cengage Learning},
year = {2013},
}
@book{hopcroft2006,
author = {Hopcroft, John E. and Motwani, Rajeev and Ullman, Jeffrey D.},
title = {Introduction to Automata Theory, Languages, and Computation},
edition = {3rd},
publisher = {Pearson},
year = {2006},
}
@article{rabin1959finite,
author = {Rabin, Michael O. and Scott, Dana},
title = {Finite Automata and Their Decision Problems},
journal = {IBM Journal of Research and Development},
volume = {3},
number = {2},
pages = {114--125},
year = {1959},
}