Sub topic 3
Nondeterminism and NFAs
This chapter assumes you know how a DFA reads a string and how its transition function works. It also assumes basic set notation, including power sets. A nondeterministic finite automaton (NFA) relaxes one rule of the DFA, and that change makes many machines much smaller. It does not make them more powerful.
What changes compared with a DFA
A DFA has exactly one move for every state and input symbol. An NFA may have zero, one, or several moves for the same pair. The machine accepts a string if at least one sequence of choices consumes the whole string and ends in an accepting state.
Formally, an NFA is a 5-tuple (Q, Σ, δ, q0, F):
- Q is a finite set of states.
- Σ is the input alphabet.
- δ: Q × Σ → 2^Q is the transition function. It returns a set of states, possibly empty.
- q0 ∈ Q is the start state.
- F ⊆ Q is the set of accepting states.
The only difference from a DFA is the type of δ. A DFA returns one state. An NFA returns a subset of Q. Every DFA is therefore an NFA whose sets all have exactly one element.
Acceptance: "some path accepts"
Extend δ to strings. For a set of states S and a symbol a, define the next set as the union of δ(s, a) over all s in S. Then:
- δ*(q, ε) = {q}
- δ(q, wa) = the union of δ(p, a) for every p in δ(q, w)
The NFA accepts w exactly when δ*(q0, w) ∩ F ≠ ∅. Two consequences matter in practice:
- One accepting path is enough. Rejecting requires every path to fail.
- A path that has no move for the next symbol dies. It contributes nothing, but other paths may survive.
You can think of the machine in two equivalent ways. It may branch into parallel copies at each choice. Or it may guess the right path and then verify it.
A worked example: strings ending in abb
Consider the language of strings over {a, b} that end in abb. Its regular expression is (a|b)*abb. The NFA below stays in q0 while it reads anything. When it sees an a, it may also guess that this a begins the final abb.
The table lists δ. Empty cells mean the empty set, so that path dies.
| State | on a | on b |
|---|---|---|
| q0 | {q0, q1} | {q0} |
| q1 | ∅ | {q2} |
| q2 | ∅ | {q3} |
| q3 | ∅ | ∅ |
Trace the input aabb. After each symbol, the set of live states is:
- Start: {q0}
- Read a: {q0, q1}
- Read a: {q0, q1}. The q1 path dies, because q1 has no
amove. A new q1 is created from q0. - Read b: {q0, q2}
- Read b: {q0, q3}
The final set contains q3, which is accepting, so the string is accepted. For abab, the final set is {q0, q2}, so the string is rejected.
Simulating an NFA in code
The simulation follows the definition. It keeps the set of current states and replaces it after each symbol.
nfa = {(0, 'a'): {0, 1}, (0, 'b'): {0}, (1, 'b'): {2}, (2, 'b'): {3}}
accepting = {3}
def nfa_accepts(w, trace=False):
current = {0}
for ch in w:
current = {q for s in current for q in nfa.get((s, ch), set())}
if trace:
print(ch, sorted(current))
return bool(current & accepting)
print(nfa_accepts("aabb", trace=True)) # True
print(nfa_accepts("abab")) # False
print(nfa_accepts("")) # False, q0 is not acceptingThe .get(..., set()) call encodes "no move". If the set becomes empty, it stays empty for the rest of the input, and the function returns False.
This simulation costs at most |Q| work per symbol per source state, so it is polynomial. The exponential cost appears only when you build an equivalent DFA in advance. That conversion is the subject of a later chapter.
Why NFAs are worth having
NFAs are often much smaller than any DFA for the same language. Take the language of strings over {a, b} whose third symbol from the end is a. An NFA needs four states:
nfa3 = {
(0, 'a'): {0, 1}, (0, 'b'): {0},
(1, 'a'): {2}, (1, 'b'): {2},
(2, 'a'): {3}, (2, 'b'): {3},
}
def accepts(w, start=0, final={3}):
cur = {start}
for ch in w:
cur = {q for s in cur for q in nfa3.get((s, ch), set())}
return bool(cur & final)
print(accepts("abb")) # True, third from the end is a
print(accepts("bab")) # False, third from the end is b
print(accepts("bbabb")) # TrueThe NFA guesses which a is third from the end. It then counts two more symbols and requires the input to end there. A DFA cannot guess, so it must remember the last three symbols. Its minimal form has 2³ = 8 states. For "k-th symbol from the end is a", the NFA has k + 1 states and the minimal DFA has 2^k.
NFAs are also easier to compose. Union, concatenation, and Kleene star all have short constructions on NFAs, and these constructions are the basis of the regular-expression-to-automaton conversion later in the plan.
DFA versus NFA
| Property | DFA | NFA |
|---|---|---|
| Moves per (state, symbol) | Exactly one | Zero or more |
| Type of δ | Q × Σ → Q | Q × Σ → 2^Q |
| Acceptance | The single run ends in F | Some run ends in F |
| Languages recognized | Regular | Regular (the same class) |
| Worst-case state count | Up to 2^n for an n-state NFA | n |
| Complement | Swap F and Q∖F | Not valid by swapping |
The "same class" row is the key theorem of this section. Every NFA has an equivalent DFA, so nondeterminism adds convenience and no new languages. The proof is the subset construction, covered later in the plan. For GATE, remember the bound: an NFA with n states gives a DFA with at most 2^n states.
Common errors
- Requiring all paths to accept. One accepting path is enough. A string is rejected only if every path fails.
- Stopping at an accepting state. The NFA must consume the whole input. A path that reaches an accepting state in the middle and then dies does not count.
- Treating a missing transition as an error. It only kills that path. You do not need an explicit dead state, although adding one is harmless.
- Complementing by swapping accepting states. This works only for a complete DFA. Take an NFA with start state q0, δ(q0, a) = {q0, q1}, and F = {q1}. It accepts a⁺. Swap the accepting set to {q0}. The new machine accepts every string aⁿ, including
a, which was already in the original language. The true complement of a⁺ over {a} is {ε}. - Forgetting the empty string. The NFA accepts ε exactly when q0 is in F.
- Confusing "number of paths" with acceptance. Many accepting paths do not matter, and neither do many rejecting ones.
Key points
- An NFA returns a set of next states, possibly empty.
- A string is accepted if at least one complete run ends in an accepting state.
- You can simulate an NFA by tracking the set of live states.
- NFAs and DFAs recognize the same languages, but the smallest DFA can be exponentially larger.
- Do not complement an NFA by flipping its accepting states.
Exercise
Give the transition table of an NFA with three states for the language of binary strings that end in 01. Then state the set of live states after reading 1101.
Show answer
Use states q0, q1, q2 with start q0 and F = {q2}. Transitions: δ(q0,0) = {q0,q1}, δ(q0,1) = {q0}, δ(q1,1) = {q2}, and the other moves are empty. Trace for 1101: start {q0}; read 1 gives {q0}; read 1 gives {q0}; read 0 gives {q0,q1}; read 1 gives {q0,q2}. The set contains q2, so 1101 is accepted.
Exercise
An NFA has 5 states. What is the largest number of states the equivalent DFA built from it can need? Can the minimal DFA for a language ever be smaller than the smallest NFA for the same language?
Show answer
At most 2^5 = 32 states, because each DFA state is a subset of the NFA's states. Yes, it can be smaller or equal in size only when the NFA is not minimal in the DFA sense. In general the smallest NFA is never larger than the smallest DFA, since every DFA is already an NFA. So the minimal DFA is never smaller than the minimal NFA, and it is often exponentially larger.
Exercise
Why does swapping accepting and non-accepting states fail to complement an NFA? Give a one-sentence reason.
Show answer
A string can have several runs, some ending in accepting states and some not, so after the swap the same string can still be accepted by a different run, and the machine no longer accepts exactly the strings that were rejected before.