Some languages are much easier to describe with a machine that is allowed to “guess”. For “strings ending in 01” you would like to say: stay put, and when you see the 0 that starts the final 01, jump ahead. A nondeterministic finite automaton (NFA) allows exactly this. Surprisingly, it is no more powerful than a DFA — but it is often much simpler to design.
Definition 2.23 · Nondeterministic finite automaton (NFA)
An NFA is a 5-tuple N = ( Q , Σ , δ , q 0 , F ) N = (Q, \Sigma, \delta, q_0, F) N = ( Q , Σ , δ , q 0 , F ) like a DFA, except that δ : Q × Σ → P ( Q ) \delta: Q \times \Sigma \to P(Q) δ : Q × Σ → P ( Q ) : for a state and a symbol, δ \delta δ gives a set of possible next states, which may contain several states or be empty. N N N accepts w w w if at least one sequence of choices reading w w w ends in a final state.
Intuition
Imagine reading the string while carrying several coins at once — one on each state the machine could possibly be in. At each symbol, every coin moves along every allowed arrow (a coin with no arrow is removed; a coin with two arrows becomes two coins). After the last symbol, the string is accepted if any coin is on a final state.
Example: strings ending in 01
The NFA has states q 0 q_0 q 0 (start), q 1 q_1 q 1 , q 2 q_2 q 2 (final):
State 0 1 → q 0 q_0 q 0 { q 0 , q 1 } \{q_0, q_1\} { q 0 , q 1 } { q 0 } \{q_0\} { q 0 } q 1 q_1 q 1 ∅ \emptyset ∅ { q 2 } \{q_2\} { q 2 } q 2 q_2 q 2 (final)∅ \emptyset ∅ ∅ \emptyset ∅
q 0 q_0 q 0 loops on everything; on a 0 it may also “guess” that the final 01 01 01 starts here.
Example 2.24 Following all paths of an NFA
Goal. Decide whether the NFA above accepts 00101 00101 00101 and 0110 0110 0110 .
Step. Track the set of possible states, starting with { q 0 } \{q_0\} { q 0 } . For 00101 00101 00101 : after 0 0 0 : { q 0 , q 1 } \{q_0, q_1\} { q 0 , q 1 } ; after 0 0 0 : { q 0 , q 1 } \{q_0, q_1\} { q 0 , q 1 } (q 1 q_1 q 1 has no 0-move); after 1 1 1 : { q 0 , q 2 } \{q_0, q_2\} { q 0 , q 2 } ; after 0 0 0 : { q 0 , q 1 } \{q_0, q_1\} { q 0 , q 1 } ; after 1 1 1 : { q 0 , q 2 } \{q_0, q_2\} { q 0 , q 2 } .
Step. The final set contains q 2 q_2 q 2 , so 00101 00101 00101 is accepted (it ends in 01).
Step. For 0110 0110 0110 : { q 0 } → 0 { q 0 , q 1 } → 1 { q 0 , q 2 } → 1 { q 0 } → 0 { q 0 , q 1 } \{q_0\} \xrightarrow{0} \{q_0, q_1\} \xrightarrow{1} \{q_0, q_2\} \xrightarrow{1} \{q_0\} \xrightarrow{0} \{q_0, q_1\} { q 0 } 0 { q 0 , q 1 } 1 { q 0 , q 2 } 1 { q 0 } 0 { q 0 , q 1 } . No final state: rejected.
Result. 00101 00101 00101 is accepted and 0110 0110 0110 is rejected, exactly as “ends in 01” predicts. (Ex.13 uses this NFA: 001 001 001 and 011101 011101 011101 are accepted; 01110 01110 01110 and 010 010 010 are rejected.)
Correction · Transition table in the answer to Ex.13
Study material says: The answer to Ex.13 (printed as “Ex.12” in §2.7) gives the NFA of Fig. 17 the table q 0 q_0 q 0 : 0 → q 0 , q 1 0 \to q_0, q_1 0 → q 0 , q 1 , 1 → q 0 1 \to q_0 1 → q 0 ; q 1 q_1 q 1 : 0 → − 0 \to - 0 → − , 1 → − 1 \to - 1 → − ; q 2 q_2 q 2 : − , − -, - − , − .
Correct version: q 1 q_1 q 1 must have the transition δ ( q 1 , 1 ) = { q 2 } \delta(q_1, 1) = \{q_2\} δ ( q 1 , 1 ) = { q 2 } ; the full table is δ ( q 0 , 0 ) = { q 0 , q 1 } \delta(q_0, 0) = \{q_0, q_1\} δ ( q 0 , 0 ) = { q 0 , q 1 } , δ ( q 0 , 1 ) = { q 0 } \delta(q_0, 1) = \{q_0\} δ ( q 0 , 1 ) = { q 0 } , δ ( q 1 , 0 ) = ∅ \delta(q_1, 0) = \emptyset δ ( q 1 , 0 ) = ∅ , δ ( q 1 , 1 ) = { q 2 } \delta(q_1, 1) = \{q_2\} δ ( q 1 , 1 ) = { q 2 } , δ ( q 2 , 0 ) = δ ( q 2 , 1 ) = ∅ \delta(q_2, 0) = \delta(q_2, 1) = \emptyset δ ( q 2 , 0 ) = δ ( q 2 , 1 ) = ∅ .
Why: Fig. 17 has an arrow q 1 → 1 q 2 q_1 \xrightarrow{1} q_2 q 1 1 q 2 , and the computations in the same answer use δ ( q 1 , 1 ) = q 2 \delta(q_1, 1) = q_2 δ ( q 1 , 1 ) = q 2 . With the printed table no string could reach q 2 q_2 q 2 . The final accept/reject results in the answer are correct.
From NFA to DFA: the subset construction
Theorem 2.25 · NFAs and DFAs are equivalent
For every NFA N N N there is a DFA M M M with L ( M ) = L ( N ) L(M) = L(N) L ( M ) = L ( N ) .
Idea (subset construction): each state of M M M is a set of states of N N N — exactly the “coins” of the intuition. The start state is { q 0 } \{q_0\} { q 0 } ; from a set S S S on symbol a a a , go to the union of δ ( q , a ) \delta(q, a) δ ( q , a ) for q ∈ S q \in S q ∈ S ; a set is final if it contains a final state of N N N . Only the sets actually reached need to be built. An NFA with n n n states can need up to 2 n 2^n 2 n DFA states, but usually far fewer.
Example 2.26 Converting the 'ends in 01' NFA to a DFA
Goal. Build a DFA equivalent to the NFA for strings ending in 01.
Step. Start with A = { q 0 } A = \{q_0\} A = { q 0 } . On 0: { q 0 , q 1 } = : B \{q_0, q_1\} =: B { q 0 , q 1 } =: B . On 1: { q 0 } = A \{q_0\} = A { q 0 } = A .
Step. From B B B : on 0, δ ( q 0 , 0 ) ∪ δ ( q 1 , 0 ) = { q 0 , q 1 } = B \delta(q_0, 0) \cup \delta(q_1, 0) = \{q_0, q_1\} = B δ ( q 0 , 0 ) ∪ δ ( q 1 , 0 ) = { q 0 , q 1 } = B ; on 1, { q 0 } ∪ { q 2 } = { q 0 , q 2 } = : C \{q_0\} \cup \{q_2\} = \{q_0, q_2\} =: C { q 0 } ∪ { q 2 } = { q 0 , q 2 } =: C .
Step. From C C C : on 0, { q 0 , q 1 } = B \{q_0, q_1\} = B { q 0 , q 1 } = B ; on 1, { q 0 } = A \{q_0\} = A { q 0 } = A . No new sets appear.
Step. Check with a program that the new DFA agrees with “ends in 01”:
Python · subset_check.py Copy from itertools import product
# DFA from the subset construction; C = {q0, q2} is final
dfa = {( "A" , "0" ): "B" , ( "A" , "1" ): "A" ,
( "B" , "0" ): "B" , ( "B" , "1" ): "C" ,
( "C" , "0" ): "B" , ( "C" , "1" ): "A" }
def accepts(word):
state = "A"
for symbol in word:
state = dfa[(state, symbol)]
return state == "C"
ok = all (accepts( "" .join(w)) == "" .join(w).endswith( "01" )
for n in range ( 9 ) for w in product( "01" , repeat = n))
print ( "agrees on all strings up to length 8:" , ok)
Output Copy agrees on all strings up to length 8: True
Result. A 3-state DFA: A A A (start), B B B , C C C (final), with A → 0 B A \xrightarrow{0} B A 0 B , A → 1 A A \xrightarrow{1} A A 1 A , B → 0 B B \xrightarrow{0} B B 0 B , B → 1 C B \xrightarrow{1} C B 1 C , C → 0 B C \xrightarrow{0} B C 0 B , C → 1 A C \xrightarrow{1} A C 1 A .
Note
Nondeterminism is a key idea in complexity theory: the class NP (Unit 3) is defined with nondeterministic machines that “guess” a solution and then check it.
Common mistake
Rejecting because one path fails. An NFA accepts if some path ends in a final state. A path that gets stuck or ends in a non-final state does not matter if another path succeeds.
Common mistake
Thinking NFAs recognise more languages than DFAs. NFAs are often smaller and easier to design, but every NFA can be converted to a DFA for the same language.
Key idea
An NFA may have several or no next states and accepts if any path ends in a final state; by the subset construction, every NFA has an equivalent DFA, so NFAs recognise exactly the regular languages.
TEE Exam note
“Define an NFA” (5-tuple with δ : Q × Σ → P ( Q ) \delta: Q \times \Sigma \to P(Q) δ : Q × Σ → P ( Q ) ), “Check whether … is accepted” (track the set of states after each symbol), “Convert the NFA to a DFA” (subset construction table). Example 20 (correct trace of a b a aba aba ): { q 0 } → a { q 1 , q 2 } → b { q 2 } → a { q 1 } \{q_0\} \xrightarrow{a} \{q_1, q_2\} \xrightarrow{b} \{q_2\} \xrightarrow{a} \{q_1\} { q 0 } a { q 1 , q 2 } b { q 2 } a { q 1 } ; q 1 q_1 q 1 is final, so a b a aba aba is accepted. Ex.14: NFA for strings starting with a b ab ab : q 0 → a q 1 → b q 2 q_0 \xrightarrow{a} q_1 \xrightarrow{b} q_2 q 0 a q 1 b q 2 , with q 2 q_2 q 2 final and looping on a , b a, b a , b .
Key takeaways
NFA: δ ( q , a ) \delta(q, a) δ ( q , a ) is a set of states (possibly empty).
Accept if at least one path ends in a final state; track the set of reachable states.
Subset construction: DFA states are sets of NFA states.
NFAs and DFAs recognise the same languages — the regular languages.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice In an NFA, what is the type of the transition function?
Check answer
Show answer Answer: B. Q × Σ → P ( Q ) Q \times \Sigma \to P(Q) Q × Σ → P ( Q )
It returns a set of states (possibly empty). Option 1 is the DFA transition function.
Question 2 · multiple choice For the “ends in 01” NFA, what is the set of states after reading 10 10 10 ?
Check answer
Show answer Answer: C. { q 0 , q 1 } \{q_0, q_1\} { q 0 , q 1 }
{ q 0 } → 1 { q 0 } → 0 { q 0 , q 1 } \{q_0\} \xrightarrow{1} \{q_0\} \xrightarrow{0} \{q_0, q_1\} { q 0 } 1 { q 0 } 0 { q 0 , q 1 } . Option 4 is wrong because q 0 q_0 q 0 always has moves.
Question 3 · multiple choice An NFA has some paths for w w w that get stuck and one path that ends in a final state. Is w w w accepted?
A Yes B No, because some paths get stuck C Only if all paths end in final states D The question is meaningless for NFAs
Check answer
Show answer Answer: A. Yes
One successful path is enough. Options 2 and 3 describe a much stricter rule than the definition.
Question 4 · multiple choice Using the subset construction, at most how many DFA states can come from an NFA with 4 states?
A 4 B 8 C 12 D 16
Check answer
Show answer Answer: D. 16
Each DFA state is a subset of the 4 NFA states, and there are 2 4 = 16 2^4 = 16 2 4 = 16 subsets. Usually fewer are reachable.
Question 5 · multiple choice Which string is accepted by the NFA of Ex.13 (“ends in 01”)?
Check answer
Show answer Answer: C. 011101 011101 011101
011101 011101 011101 ends in 01. The others end in 10, 10 and 11.
Question 6 · short answer
Give an NFA over { a , b } \{a, b\} { a , b } that accepts the strings starting with a b ab ab (Ex.14), and check a b b a abba abba and b a ba ba .
Show worked answer States q 0 q_0 q 0 (start), q 1 q_1 q 1 , q 2 q_2 q 2 (final). δ ( q 0 , a ) = { q 1 } \delta(q_0, a) = \{q_1\} δ ( q 0 , a ) = { q 1 } , δ ( q 1 , b ) = { q 2 } \delta(q_1, b) = \{q_2\} δ ( q 1 , b ) = { q 2 } , δ ( q 2 , a ) = δ ( q 2 , b ) = { q 2 } \delta(q_2, a) = \delta(q_2, b) = \{q_2\} δ ( q 2 , a ) = δ ( q 2 , b ) = { q 2 } ; all other transitions are ∅ \emptyset ∅ . a b b a abba abba : { q 0 } → { q 1 } → { q 2 } → { q 2 } → { q 2 } \{q_0\} \to \{q_1\} \to \{q_2\} \to \{q_2\} \to \{q_2\} { q 0 } → { q 1 } → { q 2 } → { q 2 } → { q 2 } , accepted. b a ba ba : { q 0 } → b ∅ \{q_0\} \xrightarrow{b} \emptyset { q 0 } b ∅ , stuck, rejected.