Theory of Computation: Lecture 2
Nondeterminism, NFAs and their acceptance, the subset construction with its equivalence proof, epsilon closure, and closure under concatenation.
From Concatenation to Nondeterminism
Complement, union, and intersection all came with DFA constructions. Concatenation did not: to recognize , a machine reading must know where the first part ends and the second part begins, and the input does not mark that split. A deterministic run cannot try all splits, so the machine is given a new ability: it may guess.
Nondeterminism. A machine is nondeterministic when it may have several possible next moves. A string is accepted when at least one possible computation accepts; all other computations are simply ignored.
Nondeterministic Finite Automata
Nondeterministic finite automaton (NFA). A finite automaton whose transition rule may offer any number of next states, including none, and may move without reading input. The differences from a DFA:
- Branching. On a given state and symbol, there may be more than one next state.
- Missing transition. There may be no next state at all; that computation branch dies.
- Epsilon transition. A transition labeled moves to another state without consuming an input symbol.
Example 1. Strings containing 11 or 101.
This NFA waits in until it guesses that a matching substring has started. If the guess succeeds, it reaches and stays there.
The transition has two labels. Reading 0 helps match 101; taking helps match 11.
Accepted: , , . Rejected: , , .
Formal NFA. An NFA is a 5-tuple with the same parts as a DFA except for the transition function:
| Symbol | Role |
|---|---|
| states | |
| alphabet | |
| transition function | |
| start state | |
| accepting states |
Power set. The power set is the set of all subsets of . The transition function returns an element of because there may be zero, one, or many next states.
If , then .
For Example 1, the diagram is equivalent to this transition table, where each cell is a set and an impossible move is , not an error:
| State | 0 | 1 | |
|---|---|---|---|
NFA acceptance. An NFA accepts if there exists a path that starts at , reads exactly , and ends in a state of when the input ends. Epsilon transitions may be taken anywhere along the path because they read nothing. Visiting an accepting state in the middle of the input proves nothing; only the state where the input runs out counts.
Computation of an NFA. Two equivalent ways to read the same definition.
- Parallel view. The machine forks into one copy per possible move and runs all copies at once. A copy with no valid move dies. The input is accepted if any surviving copy ends in an accepting state.
- Guess view. The machine magically guesses a single lucky path and accepts if that path ends in an accepting state.
Guess and verify: design an NFA so that a guess marks the interesting position, and the rest of the machine merely checks it.
Example 2. Every possibility at once on 010110.
Running Example 1's NFA in the parallel view, the set of live states after each symbol (including states reached by free -moves) evolves as follows.
| Input read | Possible states |
|---|---|
| start | |
0 | |
1 | |
0 | |
1 | |
1 | |
0 |
Reading 1 in opens three branches: stay in , move to , or move to and slide on to by the -transition. Branches die quietly, as does on 0. The final set contains the accepting state , so 010110 is accepted: in the guess view, one lucky path guessed the 101 in the middle.
Designing NFAs
Nondeterminism has no physical analog, yet it earns its place: NFAs are often easier to build and much smaller than DFAs for the same language, and the equivalence theorem below converts any NFA into a DFA mechanically. Two patterns cover most designs.
Guess-and-check pattern. The NFA stays in a broad waiting state, guesses the important position, then checks the remaining symbols.
Example 3. A 1 in the third position from the end.
The machine may leave whenever it reads a 1. After that guess, exactly two more symbols must be read before the input ends.
The accepting state has no outgoing transition, so a branch that guesses too early dies before the input ends. The string visits after three symbols but is rejected: the path is not at when the input ends.
Accepted: , , . Rejected: , , .
Epsilon-branching pattern. An NFA can choose between several submachines before reading any input, which is exactly a union of their languages.
Example 4. Unary strings whose length is divisible by 2 or 3.
The start state chooses, by -transition, whether to test divisibility by 2 or divisibility by 3.
The top branch cycles with period 2. The bottom branch cycles with period 3. The empty string is accepted because is divisible by both 2 and 3.
Accepted: , , , , . Rejected: , .
The Subset Construction
The parallel view already contains the determinization idea. In Example 2, the set of possible states after each prefix evolved deterministically: each row of the table was computed from the previous row and one input symbol, with no guessing anywhere. A DFA can therefore simulate an NFA by storing that whole set as its own single state.
Possible-states set. After reading a prefix, the set of all NFA states reachable by some path reading that prefix. The empty set means every branch has died, and a set is accepting exactly when it contains at least one accepting NFA state.
One-symbol set transition. For an NFA without -transitions, the next possible-states set after reading from any state of :
Example 5. Computing set transitions.
Reading the table as a DFA, with one state per subset and start state , gives the determinized machine. Only four of the subsets are reachable from , and they already recognize the language:
Subset construction. Given an NFA without -transitions , build the DFA :
| DFA part | Definition |
|---|---|
| start state | |
The construction does not shrink the branching; it stores it. One DFA state is one complete set of live NFA branches.
The construction lists all subsets, but only the subsets reachable from matter, as in Example 5. The exponential blow-up is sometimes unavoidable: for Example 3's language, a DFA must remember the last three symbols read, which is possibilities, while the NFA gets away with four states. NFAs can be exponentially smaller than any equivalent DFA, which is one reason they are worth designing with.
Extended transition functions. To compare the two machines on whole strings, extend both transition functions by the same last-symbol recursion used for DFAs. For the DFA , whose states are subsets :
For the NFA , acting on sets of states:
So is exactly the possible-states set after reading . Both functions also split over concatenation, , by the same kind of induction.
Theorem: NFA-DFA equivalence. Every nondeterministic finite automaton has an equivalent deterministic finite automaton. Consequently, a language is regular if and only if some NFA recognizes it.
The proof is stated for NFAs without -transitions; the closure paragraph below removes that assumption.
1. Lemma. For every subset and every string : . The DFA's state after is exactly the NFA's possible-states set after .
2. Base case. For , both sides are by definition.
3. Inductive step. Write with and assume the lemma for :
4. Conclusion. The machines agree on every string:
The first two steps unfold the construction's accepting set, the third is the lemma, and the last is the definition of NFA acceptance. Hence .
Epsilon closure. For a set of states , the set of all states reachable from by zero or more -transitions. To determinize an NFA with -transitions, charge the free moves to the bookkeeping: start the DFA in and set
The same induction then goes through unchanged, which is how Example 2's table already behaved: every row included the states reached by sliding along -arrows.
Nondeterminism adds convenience, not power.
Concatenation with NFAs
Nondeterminism resolves the split problem that opened the lecture. The NFA for runs the first machine, guesses when the first part is finished, and jumps into the second machine by an -transition.
Concatenation construction. Let recognize and recognize , with disjoint state sets. Build for :
| Part of | Definition |
|---|---|
| States | |
| Start state | |
| Accepting states | |
| Old transitions | Keep all transitions of and |
| New transitions | Add -transitions from each state in to |
Written as one transition function:
Only accepting states of may jump, and the jump reads nothing. Old accepting states of are no longer accepting in , so finishing a string of proves nothing by itself; the rest of the input must still drive to .
Recipe: proving an NFA construction correct. needs two directions, and both are exchanges of witnesses.
- Path to split. Take any accepting path of . It must use exactly one -jump, since the jump is the only way from to the accepting states in . The jump marks a split where drove to , so , and drove to , so .
- Split to path. Take any with and . Glue the accepting run of on , the -jump, and the accepting run of on into one accepting path of .
Closure under concatenation. If and are regular, then is regular: the construction gives an NFA for it, and every NFA has an equivalent DFA.
This is the general workflow the equivalence theorem unlocks: build with nondeterminism, conclude regularity for free. The remaining question is whether the whole approach has a ceiling: is every language regular, or does finite memory have a real limit?