Subset Construction Algorithm
Goal
Convert NFA to equivalent DFA .
Key Idea
Each DFA state represents a set of NFA states.
Epsilon Closure
The epsilon closure of a state :
For a set :
The Algorithm
Input: NFA N = (Q, Σ, δ, q₀, F)
Output: Equivalent DFA D
1. Start state of D: q₀' = ε-closure({q₀})
2. Initialize: Q_D = {q₀'}, unmarked
3. While there is an unmarked state S in Q_D:
Mark S
For each symbol a ∈ Σ:
T = ε-closure(δ(S, a)) // δ(S,a) = ∪_{q∈S} δ(q,a)
If T ∉ Q_D:
Add T to Q_D (unmarked)
Add transition δ_D(S, a) = T
4. Accept states: F_D = {S ∈ Q_D | S ∩ F ≠ ∅}
Example
NFA
Accepts strings ending in "01":
| State | 0 | 1 | |
|---|---|---|---|
| - | |||
| - | - | ||
| (accept) | - | - | - |
Step-by-Step Conversion
Step 1: Start state = -closure() =
Step 2: Process :
- On 0:
- On 1:
Step 3: Process :
- On 0:
- On 1:
Step 4: Process :
- On 0:
- On 1:
Resulting DFA
| DFA State | 0 | 1 | Accept? |
|---|---|---|---|
| No | |||
| No | |||
| Yes |
Handling Epsilon Transitions
Extended Move Function
For set and symbol :
Then:
Example with Epsilon
NFA with -transition:
| State | a | b | |
|---|---|---|---|
| - | - | ||
| - | - | ||
| (accept) | - | - |
Start state: -closure() =
Transitions from :
- On a: move = , -closure =
- On b: move = , -closure =
DFA Minimization
After conversion, minimize the DFA.
Algorithm (Table-Filling / Myhill-Nerode)
- Partition states: initially by accept/non-accept
- Refine: split groups where transitions lead to different groups
- Repeat until stable
- Each final group becomes one state
Example
DFA with states :
- Initial: (non-accept), (accept)
- Check transitions; split if needed
- Continue until stable
Complexity
Worst Case
NFA with states → DFA with up to states.
Example: Exponential Blowup
NFA for "" (n-th from last is 'a')
- NFA: states
- Minimal DFA: states
Lazy Evaluation
On-Demand Construction
Build DFA states only when needed:
- Start with initial state
- When transitioning to unseen state, compute it
- Cache computed states
Useful when only testing specific inputs.
Applications
Lexical Analysis
Compilers convert regex → NFA → DFA for tokenizing.
Pattern Matching
DFA enables matching for string of length .
Network Intrusion Detection
Pattern matching on network traffic.
Comparison
| Aspect | NFA | DFA |
|---|---|---|
| States | Fewer | Up to exponentially more |
| Transitions | Multiple per symbol | Exactly one per symbol |
| Simulation | Backtracking/parallel | Linear scan |
| Construction | Easy from regex | May be expensive |