Theory of Computation · Week 1 · Lesson 0004 of 7
Nondeterministic finite automata
A set of states represents every way the input read so far could have been processed.
Keep all possible branches, compute ε-closure, and turn each reachable state set into one DFA state.
1Warm-up
2Read every available branch
A nondeterministic finite automaton (NFA) is a finite automaton that allows zero, one, or several next states for a symbol. A transition labeled ε consumes no input. A path is one sequence of moves through the diagram. The NFA accepts if at least one path consumes the entire input and ends in an accepting state.
Its five parts are (Q, Σ, δ, q₀, F). The transition function δ maps a state and a label in Σ ∪ {ε} to a set of states. The power set 2Q is the set of all subsets of Q. It is the set of possible values of δ, including ∅ for a missing move.
The ε-closure E(S) is the set of states reachable from S by zero or more ε-moves. Zero moves retain all members of S. To find it, follow ε-arrows repeatedly until the set stops growing. Use it before the first symbol and after every symbol, including the last.
Acceptance requires one complete accepting branch. A dead branch cannot cancel it, and an accepting branch that cannot read the remaining input does not establish acceptance.
For 101, the start set is {q₀}. After 1, it is {q₀,q₁,q₂}. The 0 then gives {q₀,q₂}. The final 1 gives {q₀,q₁,q₂,q₃}, which includes q₃. More generally, the route to q₃ reads either 11 or 101, with arbitrary bits allowed before and after.
Sipser, Introduction to the Theory of Computation, §1.2.
3Build a DFA out of reachable sets
The subset construction makes each reachable NFA state set a single DFA state. Begin at E({q₀}). For a state set S and bit b, take the union of all δ(q,b) for q ∈ S and then take its ε-closure. Accept a set if it intersects F. If ∅ is reached, it loops on every symbol.
| DFA state | On 0 | On 1 | Accepts? |
|---|---|---|---|
| {q₀} | {q₀} | {q₀,q₁} | No |
| {q₀,q₁} | {q₀,q₂} | {q₀,q₁,q₂} | No |
| {q₀,q₂} | {q₀} | {q₀,q₁} | Yes |
| {q₀,q₁,q₂} | {q₀,q₂} | {q₀,q₁,q₂} | Yes |
For 011, begin at {q₀}. The 0 leaves {q₀}. The first 1 gives {q₀,q₁}. The last 1 gives {q₀,q₁,q₂} because q₀ can loop or branch, and q₁ advances to q₂. The second-last symbol was 1, so the DFA accepts.
On ε, the start set alone decides acceptance. For an NFA with s → p → f on ε twice, the start set is {s,p,f}. If only f accepts, the corresponding DFA start state still accepts. With no outgoing symbol transitions, the next set on any bit is ∅.
An n-state NFA gives a DFA with at most 2n states because there are that many subsets. Many may be unreachable. Conversely, a q-state DFA gives a q-state NFA by treating each next state as a singleton set and adding no ε-moves. Thus both kinds recognize the regular languages.
4Combine machines with ε-arrows
Take separate copies of NFAs for A and B. For union, add a new start with ε-arrows to both old starts and preserve both sets of accepting states. For concatenation, start in A, add ε-arrows from all of A’s accepting states to B’s start, and make only B’s accepting states accepting in the combined machine.
For star, add a new accepting start with an ε-arrow to the old start. Keep the old accepting states and add ε-arrows from them to the old start. A run can take another block, and ε can be accepted without a block.
For {0} ∪ {1}, the two branches read exactly one bit each. For {0}{1}, the link from the first machine’s old accepting state to the second start lets a run read 0 then 1. For {01}*, a run over 0101 follows the machine for 01 twice, taking one ε-return between the two blocks.
5Compare state counts
For “the k-th symbol from the end is 1” with k ≥ 1, an NFA can loop on both bits, guess a 1, and count exactly k−1 remaining bits along a chain. This uses k+1 states. A DFA can store the last k bits in a k-bit register initialized with zeros, shifting in each new bit. Leading padding zeros ensure strings shorter than k reject.
The 2k register states are necessary. Take two different k-bit prefixes and a position i where they differ, numbered from the left starting at 1. Append i−1 zeros. The differing bit becomes the k-th from the end so the inputs require different answers. Reaching the same DFA state after those prefixes would force the same answer.
The NFA also needs at least k+1 states. Take an accepting path for 10k−1 and pick a state at each of the k+1 input positions, from before the first symbol to after the last. If two chosen states were equal, deleting the part of the path between them would give acceptance on an input shorter than k. Thus these states must be distinct.
For unary length divisible by 2 or 7, an ε-split gives a start plus two cycles of lengths 2 and 7. Each cycle accepts at its entry, producing an NFA with 10 states. The remainder construction uses a DFA with 14 states.
6Recap
- Take ε-closure before the first symbol and after every symbol.
- A subset accepts if it contains any NFA accepting state.
- Every q-state DFA is already a q-state NFA when each next state is treated as a singleton.
7Check yourself
8Cheat card
- NFA
- Zero or more next states per symbol. An ε-arrow consumes no input.
- Trace
- Start with closure, move on the symbol, take closure again.
- Accept
- The final reachable set intersects F.
- Memory hook
- Close, read, close. Keep every live path.