Nondeterministic Finite Automata

Interactive Enhanced Edition

NFA
Nondeterminism
2n
Subset Construction
ε
Epsilon Transitions

Arrow keys to navigate  |  Based on CS305 lecture materials (Ullman)

The Big Picture

Three formalisms, one class of languages

All Three Are Equal!

DFA, NFA, and ε-NFA all recognize exactly the same languages — the regular languages.

Why learn all three?

NFAs are easier to design (fewer states, more flexibility). ε-NFAs are easiest to compose (union = just add ε-arrows). But only DFAs can run on a computer (one state at a time).

This lecture teaches the conversions between them.

What is an NFA?

The key difference from DFAs: choice

DFA: one path  |  NFA: multiple paths explored simultaneously

An NFA changes two rules from a DFA:

  • From one state on one input, there can be zero, one, or many next states
  • The NFA can be in several states at once

Acceptance Rule

An NFA accepts if ANY sequence of choices leads to a final state. It only needs one successful path out of all possibilities.

Maze with Clones

Imagine a maze where every fork lets you clone yourself and explore all paths simultaneously. If any clone reaches the exit, you win. That's an NFA.

Common Misconception

"NFAs are more powerful than DFAs." FALSE! They recognize exactly the same languages. Nondeterminism gives convenience, not extra power.

DFA vs NFA: Side by Side

Same power, different style

DFA (Deterministic)

δ(q, a) = one state
"From q reading a, go to exactly one state"
  • Always in exactly one state
  • Every state has exactly one transition per symbol
  • No dead ends (total function)
  • Easy to simulate on a computer

NFA (Nondeterministic)

δ(q, a) = set of states
"From q reading a, go to any of these states"
  • Can be in multiple states simultaneously
  • A state may have 0 or many transitions per symbol
  • Dead ends allowed (kills that branch)
  • Easier to design (more compact)

Design Advantage: Exponential Compression

An NFA for "n-th symbol from end is 1" needs ~n states. The equivalent DFA needs ~2n states! NFAs give you design convenience, even though they don't add computational power.

Formal Definition of an NFA

Five components — the only difference is δ

An NFA is a 5-tuple (Q, Σ, δ, q0, F):

SymbolMeaningSame as DFA?
QFinite set of statesYes
ΣInput alphabetYes
δTransition functionDIFFERENT
q0Start stateYes
FSet of accept statesYes

The Key Difference: δ

DFA: δ(q, a) = one state p

NFA: δ(q, a) = a SET of states {p, r, s} or even {}

That's it! The transition function returns a set instead of a single state.

DFA: δ(q0, '0') = q1 // one answer
NFA: δ(q0, '0') = {q1,q3} // multiple
NFA: δ(q2, '1') = {} // dead end

The empty set {} is valid

If δ(q, a) = {}, that branch of computation simply dies. This is fine — other branches may still succeed.

Chessboard NFA — Interactive

Type a string of r's and b's to move the king across a 3×3 board

Active squares: {1}

How it works

  • States = 9 squares (1–9)
  • r = move to adjacent red square
  • b = move to adjacent black square
  • Start: square 1  |  Accept: square 9
  • Adjacent = shares edge or corner (king moves)

Why is this an NFA?

From square 5 (center), reading 'r', you could move to four different red squares: {1, 3, 7, 9}. Multiple choices = nondeterminism!

rb
→1{5}{2,4}
2{1,3,5}{4,6}
3{5}{2,6}
4{1,5,7}{2,8}
5{1,3,7,9}{2,4,6,8}
6{3,5,9}{2,8}
7{5}{4,8}
8{5,7,9}{4,6}
*9{5}{6,8}

NFA Simulator: "ends with 01"

Watch parallel branches live — type any binary string

Active states: {q0}

NFA: L = {w ∈ {0,1}* | w ends with "01"}

01
→ q0{q0, q1}{q0}
q1{}{q2}
* q2{}{}

The "guessing" strategy

On seeing 0, q0 has a choice: stay in q0 (this 0 isn't the end) OR go to q1 (bet that this 0 starts "01"). Both branches run in parallel.

Type a binary string and click Step...

Extended Transition Function

Extending δ from single symbols to entire strings

We need δ(q, a) for one symbol to become δ̂(q, w) for a whole string w.

Inductive Definition

Base: δ̂(q, ε) = {q}

"Reading nothing from q, you're still at q."

Induction: δ̂(q, wa) = ⋃p ∈ δ̂(q,w) δ(p, a)

"Process w to get a set S. Then from each state in S, follow transition on symbol a. Union all results."

// Worked example: δ̂(q0, "101")
Start: δ̂(q0, ε) = {q0}
Read 1: δ(q0, 1) = {q0}
Read 0: δ(q0, 0) = {q0, q1}
Read 1: δ(q0,1)∪δ(q1,1) = {q0, q2}
q2 ∈ F ⇒ ACCEPT!

Water in a Pipe Network

Pour water into the start state. At each input symbol, water flows through all matching transitions. It spreads to every reachable state simultaneously. After the last symbol, if water reached any accept state — accept!

Language of an NFA

A string w is accepted if:

δ̂(q0, w) ∩ F ≠ ∅

"After reading w, the set of active states contains at least one accept state."

The language L(N) = { w | δ̂(q0, w) ∩ F ≠ ∅ }

Remember

The NFA accepts if any path succeeds. It rejects only if ALL paths fail. One accepting branch out of millions is enough!

NFAs and DFAs are Equivalent!

The most important theorem of this lecture

Theorem

For every NFA, there exists a DFA that accepts exactly the same language. And vice versa.

Direction 1: DFA → NFA (trivial)

Every DFA is already an NFA! Just wrap each single next-state in a set:

DFA: δD(q, a) = p // single state
NFA: δN(q, a) = {p} // set with one element
// Behaves identically — sets always have size 1

Direction 2: NFA → DFA (the hard part!)

This requires the Subset Construction — the central algorithm of this lecture.

Exponential Blowup

If the NFA has n states, the equivalent DFA can have up to 2n states (one for each subset of NFA states).

In practice, lazy construction usually finds far fewer reachable states. But the worst case is real!

Why this matters

NFAs are a design tool — easier to create. DFAs are an execution tool — can actually run on a computer. The subset construction is the bridge between design and implementation.

Subset Construction: The Idea

Each DFA state represents a set of NFA states

Core Insight

An NFA can be in a set of states. A DFA must be in one state. Solution: make each DFA state represent a set of NFA states!

NFA state set {q1, q3, q5} becomes ONE DFA state named "{q1,q3,q5}"

The Construction Recipe

Given NFA (Q, Σ, δN, q0, F), build DFA:

DFA ComponentHow to Build
States2Q = all subsets of Q
AlphabetSame Σ
Start state{q0}
Accept statesAny subset containing a member of F
δD(S, a)q∈S δN(q, a)

Critical Point

{p, q} is a single DFA state whose name happens to be a set. Don't confuse it with "being in two places" — the DFA is always in exactly one state.

Chessboard: Subset Construction

Watch the DFA being built step by step from the 9-state chessboard NFA

NFA Reference

rb
→152,4
21,3,54,6
352,6
41,5,72,8
51,3,7,92,4,6,8
63,5,92,8
754,8
85,7,94,6
*956,8
Click Step to begin subset construction...

DFA Being Built

DFA Staterb

Challenge: Does This NFA Accept?

Trace the computation and predict the result

Question 1 of 3

How to Trace an NFA

  1. Start with {start state}
  2. For each symbol, compute union of all transitions from all active states
  3. After the last symbol: does the set contain an accept state?

Subset Construction: "ends with 01"

Step through converting the 3-state NFA to a DFA

NFA Reference

01
→ q0{q0, q1}{q0}
q1{}{q2}
* q2{}{}

DFA Being Built

DFA State01
Click Step to begin...

Visualizer: Subset Construction

Watch the DFA graph form as states are discovered

"ends with 01" — NFA → DFA

Click Step to watch the DFA graph being built...

Challenge: Build the Subset Construction

Fill in the DFA transition table from this NFA

NFA (accepts strings ending with "ab")

States: {q0, q1, q2} | Start: q0 | Accept: {q2}
q0 --a--> {q0, q1} q0 --b--> {q0}
q1 --a--> {} q1 --b--> {q2}
q2 --a--> {} q2 --b--> {}
ab
→ q0{q0, q1}{q0}
q1{}{q2}
* q2{}{}

Fill in the DFA table

Click each ? cell and select the correct state set.

Why Does Subset Construction Work?

Proof by induction on string length

What We Need to Show

For any string w:

δ̂N(q0, w) = δ̂D({q0}, w)

"The set of NFA states after reading w equals the DFA state (named by that set) after reading w."

Base Case: w = ε

δ̂N(q0, ε) = {q0}
δ̂D({q0}, ε) = {q0}
Both just stay at the start. ✓

Inductive Step: w = xa

Assume it works for x. Let S = δ̂N(q0, x) = δ̂D({q0}, x).

NFA side:
δ̂N(q0, xa) = ⋃p∈S δN(p, a)
DFA side:
δ̂D({q0}, xa) = δD(S, a)
= ⋃p∈S δN(p, a)
Same computation! ✓

In Plain English

The DFA state after reading w literally IS the set of all NFA states after reading w. The DFA's transition function was defined to make this true. So of course it works — it's almost a tautology!

Ullman calls this proof "almost a pun."

ε-NFA: Adding Free Transitions

Transitions that consume no input — "teleportation"

Solid = real transitions  |  Dashed purple = ε (free moves)

ε-transitions

An ε-transition lets the automaton move between states without reading any input. It's a free, spontaneous move.

Secret Passages

Think of ε-transitions as secret passages in a maze. You can walk through them anytime without using a key (input symbol). They're hidden shortcuts between rooms.

01ε
→ A{E}{B}
B{C}{D}
C{D}
* D
E{F}{B, C}
F{D}

Still only regular languages!

ε-NFAs are a convenience, not extra power. They recognize exactly the same languages as DFAs and NFAs.

ε-Closure: CL(q)

Click a state to watch its ε-closure expand step by step

Definition

CL(q) = all states reachable from q by following zero or more ε-transitions.

q itself is always in CL(q) (zero ε-moves = stay put).

Real transitions dimmed. ε-transitions highlighted.

Explore ε-closure

Click a state to compute CL(q):

Click any state above to visualize its ε-closure...

Follow the chain!

CL(E) isn't just {E, B, C}. Since B has ε → D, you must follow that too!

CL(E) = {E} ∪ {B,C} ∪ CL(B) ∪ CL(C) = {E, B, C, D}

Converting ε-NFA to NFA

"Bake in" the ε-transitions — step through the conversion

The Recipe

Same states, alphabet, start state.

New transitions: δN(q, a) =

  1. Compute CL(q) — everywhere you can be via ε before reading 'a'
  2. From each state in CL(q), follow real transition on 'a'
  3. Union all results

New accept states F': any q where CL(q) ∩ F ≠ ∅

Worked Example: δ̂(A, "01")

Step 0: δ̂(A, ε) = CL(A) = {A}
Step 1: Read '0'
From A: δ(A,0) = {E}
CL({E}) = {B, C, D, E}
Step 2: Read '1'
From B: δ(B,1) = {C}
From C: δ(C,1) = {D}
From D: δ(D,1) = {}
From E: δ(E,1) = {}
CL({C,D}) = {C, D}
D ∈ F ⇒ "01" ACCEPTED!

Side-by-Side Conversion

ε-NFA

01ε
→A{E}{B}
B{C}{D}
C{D}
*D
E{F}{B,C}
F{D}

Ordinary NFA (result)

01
→A{E}{B}
*B{C}
C{D}
*D
*E{F}{C,D}
F{D}

What Changed?

  • ε column removed entirely
  • E on '1': CL(E)={B,C,D,E}, B→{C}, C→{D} ⇒ {C,D}
  • B and E become accept states — CL(B)={B,D} and CL(E)={B,C,D,E} both contain D

Intuition

δN incorporates ε-transitions before each real move. The closure "bakes in" the free moves so they're no longer needed as separate transitions.

Challenge: Spot the Bug!

A student did subset construction but made a mistake. Find it!

NFA (accepts strings ending with "10")

01
→ p0{p0}{p0, p1}
p1{p2}{}
* p2{}{}

Student's Subset Construction

One cell is WRONG. Click it!

DFA State01

Common Mistakes

  • Forgetting to union: When multiple NFA states are active, union ALL their transitions
  • Missing states: δ(p1, 0) = {p2} is easy to forget when computing δ({p0,p1}, 0)
  • Wrong accept states: A DFA state is accepting if it contains ANY NFA accept state

Summary: The Complete Picture

Everything connects

Key Takeaways

  • DFA, NFA, ε-NFA all recognize the regular languages
  • NFAs/ε-NFAs are easier to design (exponentially fewer states)
  • Only DFAs can be directly executed on a computer
  • Subset Construction: NFA → DFA (may blow up to 2n)
  • ε-Closure: eliminates ε-transitions systematically

Cheat Sheet

── Subset Construction (NFA → DFA) ──
1. Start state = {q0}
2. For each DFA state S, each symbol a:
δ_D(S, a) = ∪ δ_N(q, a) for q ∈ S
3. Accept = any S containing member of F
4. Repeat until no new states (lazy!)
── ε-NFA → NFA ──
1. δ_N(q, a) = ∪ δ_E(p, a) for p ∈ CL(q)
2. F' = { q | CL(q) ∩ F ≠ ∅ }
── ε-Closure ──
CL(q) = {q} ∪ all states reachable
via ε-transitions (BFS/DFS)

Common Mistakes

  • {p,q} is ONE DFA state, not two
  • CL() is transitive — follow the full chain
  • NFA accepts if ANY path succeeds
  • Don't forget the dead state {} in the DFA
  • In ε-NFA→NFA, update accept states F' too!

Quiz: Test Your Knowledge

Three questions covering NFA fundamentals

Q1: Complexity

If an NFA has 5 states, the equivalent DFA can have at most how many states?

Q2: Acceptance

An NFA accepts a string w if:

Q3: ε-Closure

If CL(q) contains an accept state, what happens in ε-NFA → NFA conversion?

Quiz: Trace Exercise

Given the NFA, predict the final state set

NFA for "ends with 01"

01
→ q0{q0, q1}{q0}
q1{}{q2}
* q2{}{}

What is δ̂(q0, "0110")?

Enter the final set of active states after reading "0110".

Strategy

Process one symbol at a time. At each step, take the union of transitions from all active states. The string "0110" has 4 symbols — you'll compute 4 transitions.

Quiz: ε-Closure Challenge

Test your understanding of ε-NFA conversion

Given this ε-NFA:

abε
→ X{Y}
Y{Z}{Z}
* Z

Questions:

1. What is CL(Y)?

2. In the converted NFA, is Y an accept state?

3. In the converted NFA, what is δN(Y, b)?

Conversion Steps

  1. Compute CL(q) for each state
  2. δN(q, a) = ⋃p ∈ CL(q) δE(p, a)
  3. F' = {q | CL(q) ∩ F ≠ ∅}

Think About It

Y has an ε-transition to Z. Z is an accept state. What does that mean for CL(Y)? And if CL(Y) contains an accept state, what happens to Y in the converted NFA?