Interactive Enhanced Edition
Arrow keys to navigate | Based on CS305 lecture materials (Ullman)
Three formalisms, one class of languages
DFA, NFA, and ε-NFA all recognize exactly the same languages — the regular languages.
NFAs are easier to design (fewer states, more flexibility). ε-NFAs are easiest to compose (union = just add ε-arrows). But only DFAs can run on a computer (one state at a time).
This lecture teaches the conversions between them.
The key difference from DFAs: choice
DFA: one path | NFA: multiple paths explored simultaneously
An NFA changes two rules from a DFA:
An NFA accepts if ANY sequence of choices leads to a final state. It only needs one successful path out of all possibilities.
Imagine a maze where every fork lets you clone yourself and explore all paths simultaneously. If any clone reaches the exit, you win. That's an NFA.
"NFAs are more powerful than DFAs." FALSE! They recognize exactly the same languages. Nondeterminism gives convenience, not extra power.
Same power, different style
An NFA for "n-th symbol from end is 1" needs ~n states. The equivalent DFA needs ~2n states! NFAs give you design convenience, even though they don't add computational power.
Five components — the only difference is δ
An NFA is a 5-tuple (Q, Σ, δ, q0, F):
| Symbol | Meaning | Same as DFA? |
|---|---|---|
| Q | Finite set of states | Yes |
| Σ | Input alphabet | Yes |
| δ | Transition function | DIFFERENT |
| q0 | Start state | Yes |
| F | Set of accept states | Yes |
DFA: δ(q, a) = one state p
NFA: δ(q, a) = a SET of states {p, r, s} or even {}
That's it! The transition function returns a set instead of a single state.
If δ(q, a) = {}, that branch of computation simply dies. This is fine — other branches may still succeed.
Type a string of r's and b's to move the king across a 3×3 board
From square 5 (center), reading 'r', you could move to four different red squares: {1, 3, 7, 9}. Multiple choices = nondeterminism!
| r | b | |
|---|---|---|
| →1 | {5} | {2,4} |
| 2 | {1,3,5} | {4,6} |
| 3 | {5} | {2,6} |
| 4 | {1,5,7} | {2,8} |
| 5 | {1,3,7,9} | {2,4,6,8} |
| 6 | {3,5,9} | {2,8} |
| 7 | {5} | {4,8} |
| 8 | {5,7,9} | {4,6} |
| *9 | {5} | {6,8} |
Watch parallel branches live — type any binary string
| 0 | 1 | |
|---|---|---|
| → q0 | {q0, q1} | {q0} |
| q1 | {} | {q2} |
| * q2 | {} | {} |
On seeing 0, q0 has a choice: stay in q0 (this 0 isn't the end) OR go to q1 (bet that this 0 starts "01"). Both branches run in parallel.
Extending δ from single symbols to entire strings
We need δ(q, a) for one symbol to become δ̂(q, w) for a whole string w.
Base: δ̂(q, ε) = {q}
"Reading nothing from q, you're still at q."
Induction: δ̂(q, wa) = ⋃p ∈ δ̂(q,w) δ(p, a)
"Process w to get a set S. Then from each state in S, follow transition on symbol a. Union all results."
Pour water into the start state. At each input symbol, water flows through all matching transitions. It spreads to every reachable state simultaneously. After the last symbol, if water reached any accept state — accept!
A string w is accepted if:
δ̂(q0, w) ∩ F ≠ ∅
"After reading w, the set of active states contains at least one accept state."
The language L(N) = { w | δ̂(q0, w) ∩ F ≠ ∅ }
The NFA accepts if any path succeeds. It rejects only if ALL paths fail. One accepting branch out of millions is enough!
The most important theorem of this lecture
For every NFA, there exists a DFA that accepts exactly the same language. And vice versa.
Every DFA is already an NFA! Just wrap each single next-state in a set:
This requires the Subset Construction — the central algorithm of this lecture.
If the NFA has n states, the equivalent DFA can have up to 2n states (one for each subset of NFA states).
In practice, lazy construction usually finds far fewer reachable states. But the worst case is real!
NFAs are a design tool — easier to create. DFAs are an execution tool — can actually run on a computer. The subset construction is the bridge between design and implementation.
Each DFA state represents a set of NFA states
An NFA can be in a set of states. A DFA must be in one state. Solution: make each DFA state represent a set of NFA states!
NFA state set {q1, q3, q5} becomes ONE DFA state named "{q1,q3,q5}"
Given NFA (Q, Σ, δN, q0, F), build DFA:
| DFA Component | How to Build |
|---|---|
| States | 2Q = all subsets of Q |
| Alphabet | Same Σ |
| Start state | {q0} |
| Accept states | Any subset containing a member of F |
| δD(S, a) | ⋃q∈S δN(q, a) |
{p, q} is a single DFA state whose name happens to be a set. Don't confuse it with "being in two places" — the DFA is always in exactly one state.
Watch the DFA being built step by step from the 9-state chessboard NFA
| r | b | |
|---|---|---|
| →1 | 5 | 2,4 |
| 2 | 1,3,5 | 4,6 |
| 3 | 5 | 2,6 |
| 4 | 1,5,7 | 2,8 |
| 5 | 1,3,7,9 | 2,4,6,8 |
| 6 | 3,5,9 | 2,8 |
| 7 | 5 | 4,8 |
| 8 | 5,7,9 | 4,6 |
| *9 | 5 | 6,8 |
| DFA State | r | b |
|---|
Trace the computation and predict the result
Step through converting the 3-state NFA to a DFA
| 0 | 1 | |
|---|---|---|
| → q0 | {q0, q1} | {q0} |
| q1 | {} | {q2} |
| * q2 | {} | {} |
| DFA State | 0 | 1 |
|---|
Watch the DFA graph form as states are discovered
Fill in the DFA transition table from this NFA
| a | b | |
|---|---|---|
| → q0 | {q0, q1} | {q0} |
| q1 | {} | {q2} |
| * q2 | {} | {} |
Click each ? cell and select the correct state set.
Proof by induction on string length
For any string w:
δ̂N(q0, w) = δ̂D({q0}, w)
"The set of NFA states after reading w equals the DFA state (named by that set) after reading w."
Assume it works for x. Let S = δ̂N(q0, x) = δ̂D({q0}, x).
The DFA state after reading w literally IS the set of all NFA states after reading w. The DFA's transition function was defined to make this true. So of course it works — it's almost a tautology!
Ullman calls this proof "almost a pun."
Transitions that consume no input — "teleportation"
Solid = real transitions | Dashed purple = ε (free moves)
An ε-transition lets the automaton move between states without reading any input. It's a free, spontaneous move.
Think of ε-transitions as secret passages in a maze. You can walk through them anytime without using a key (input symbol). They're hidden shortcuts between rooms.
| 0 | 1 | ε | |
|---|---|---|---|
| → A | {E} | {B} | ∅ |
| B | ∅ | {C} | {D} |
| C | ∅ | {D} | ∅ |
| * D | ∅ | ∅ | ∅ |
| E | {F} | ∅ | {B, C} |
| F | {D} | ∅ | ∅ |
ε-NFAs are a convenience, not extra power. They recognize exactly the same languages as DFAs and NFAs.
Click a state to watch its ε-closure expand step by step
CL(q) = all states reachable from q by following zero or more ε-transitions.
q itself is always in CL(q) (zero ε-moves = stay put).
Real transitions dimmed. ε-transitions highlighted.
Click a state to compute CL(q):
Click any state above to visualize its ε-closure...
CL(E) isn't just {E, B, C}. Since B has ε → D, you must follow that too!
CL(E) = {E} ∪ {B,C} ∪ CL(B) ∪ CL(C) = {E, B, C, D}
"Bake in" the ε-transitions — step through the conversion
Same states, alphabet, start state.
New transitions: δN(q, a) =
New accept states F': any q where CL(q) ∩ F ≠ ∅
ε-NFA
| 0 | 1 | ε | |
|---|---|---|---|
| →A | {E} | {B} | ∅ |
| B | ∅ | {C} | {D} |
| C | ∅ | {D} | ∅ |
| *D | ∅ | ∅ | ∅ |
| E | {F} | ∅ | {B,C} |
| F | {D} | ∅ | ∅ |
Ordinary NFA (result)
| 0 | 1 | |
|---|---|---|
| →A | {E} | {B} |
| *B | ∅ | {C} |
| C | ∅ | {D} |
| *D | ∅ | ∅ |
| *E | {F} | {C,D} |
| F | {D} | ∅ |
δN incorporates ε-transitions before each real move. The closure "bakes in" the free moves so they're no longer needed as separate transitions.
A student did subset construction but made a mistake. Find it!
| 0 | 1 | |
|---|---|---|
| → p0 | {p0} | {p0, p1} |
| p1 | {p2} | {} |
| * p2 | {} | {} |
One cell is WRONG. Click it!
| DFA State | 0 | 1 |
|---|
Look at {p0, p1} on input 0. What should δ(p0, 0) ∪ δ(p1, 0) be?
Everything connects
Three questions covering NFA fundamentals
Q1: Complexity
If an NFA has 5 states, the equivalent DFA can have at most how many states?
Q2: Acceptance
An NFA accepts a string w if:
Q3: ε-Closure
If CL(q) contains an accept state, what happens in ε-NFA → NFA conversion?
Given the NFA, predict the final state set
| 0 | 1 | |
|---|---|---|
| → q0 | {q0, q1} | {q0} |
| q1 | {} | {q2} |
| * q2 | {} | {} |
What is δ̂(q0, "0110")?
Enter the final set of active states after reading "0110".
Process one symbol at a time. At each step, take the union of transitions from all active states. The string "0110" has 4 symbols — you'll compute 4 transitions.
Test your understanding of ε-NFA conversion
| a | b | ε | |
|---|---|---|---|
| → X | {Y} | ∅ | ∅ |
| Y | ∅ | {Z} | {Z} |
| * Z | ∅ | ∅ | ∅ |
Questions:
1. What is CL(Y)?
2. In the converted NFA, is Y an accept state?
3. In the converted NFA, what is δN(Y, b)?
Y has an ε-transition to Z. Z is an accept state. What does that mean for CL(Y)? And if CL(Y) contains an accept state, what happens to Y in the converted NFA?