Finite Automata — DFA, NFA, ε-NFA
1044 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Finite Automata — DFA, NFA, ε-NFA ## 🎯 Learning Objectives - Design DFAs and NFAs for given languages - Convert NFA to DFA using subset construction - Prove language properties using closure operations - Trace computation of automata on input strings * * * ## 1. Deterministic Finite Automata (DFA) ### 1.1 Intuiti...

Finite Automata — DFA, NFA, ε-NFA
🎯 Learning Objectives
- Design DFAs and NFAs for given languages
- Convert NFA to DFA using subset construction
- Prove language properties using closure operations
- Trace computation of automata on input strings
1. Deterministic Finite Automata (DFA)
1.1 Intuition
A DFA is the simplest computational model — like a vending machine that changes state based on coin inputs and eventually decides "yes" or "no" (accept/reject). The key property: from every state, for every input symbol, there is exactly one transition.
1.2 Formal Definition
A DFA is a 5-tuple M=(Q,Σ,δ,q0,F):
- Q: Finite set of states
- Σ: Finite alphabet of input symbols
- δ:Q×Σ→Q: Transition function (deterministic!)
- q0∈Q: Start state
- F⊆Q: Accepting (final) states
1.3 DFA Example: Even Number of 1s
Language: L={w∈{0,1}∗∣w has an even number of 1s}
(Diagram)
Tracing string "1011":
| Step | State | Input | Next State |
|---|---|---|---|
| 0 | q0 (start) | — | — |
| 1 | q0 | 1 | q1 |
| 2 | q1 | 0 | q1 |
| 3 | q1 | 1 | q0 |
| 4 | q0 | 1 | q1 |
| 5 | q1 | — | Reject (q1 not accepting) |
Result: "1011" has three 1s (odd) → rejected ✓
1.4 DFA Example: Strings Ending with "01"
(Diagram)
Tracing "0101":
| Step | State | Input | Next State |
|---|---|---|---|
| 0 | q0 | — | — |
| 1 | q0 | 0 | q1 |
| 2 | q1 | 1 | q0 |
| 3 | q0 | 0 | q1 |
| 4 | q1 | 1 | q0 |
| 5 | q0 | — | Reject (doesn't end with 01) |
For "00101": q0→q1(0)→q2(0? Actually q1→q2 on 0)→q0(1)→q1(0)→q2(1)→Accept
2. Non-Deterministic Finite Automata (NFA)
2.1 Intuition
An NFA is like a DFA that can guess the right path. From a state on a given input, there can be zero, one, or multiple possible next states. A string is accepted if any path leads to an accepting state.
2.2 Formal Definition
M=(Q,Σ,δ,q0,F) where δ:Q×Σε→P(Q)
The transition function returns a set of possible next states.
2.3 NFA Example: Strings Ending with "01"
(Diagram)
Tracing "101":
| Step | Current States | Input | Next States |
|---|---|---|---|
| 0 | {q0} | — | — |
| 1 | {q0} | 1 | δ(q0,1) = {q0} |
| 2 | {q0} | 0 | δ(q0,0) = {q0, q1} |
| 3 | {q0, q1} | 1 | δ(q0,1)∪δ(q1,1) = {q0} ∪ {q2} = {q0, q2} |
| 4 | — | — | q2 ∈ accepting set → Accept |
2.4 NFA to DFA Conversion (Subset Construction)
Algorithm:
- Each DFA state = a set of NFA states
- Start state = ε-closure of NFA start
- Transition: DFA δ(S, a) = ε-closure(∪{q∈S} δ{NFA}(q, a))
- Accept if any NFA accepting state is in the set NFA → DFA Example: For the NFA above (strings ending with "01"):
| DFA State | NFA Set | On 0 | On 1 |
|---|---|---|---|
| A | {q0} | δ(q0,0)={q0,q1}→B | δ(q0,1)={q0}→A |
| B | {q0,q1} | δ(q0,0)∪δ(q1,0)={q0,q1}→B | δ(q0,1)∪δ(q1,1)={q0,q2}→C |
| C (accept) | {q0,q2} | δ(q0,0)∪δ(q2,0)={q0,q1}→B | δ(q0,1)∪δ(q2,1)={q0}→A |
The resulting DFA is exactly the one we designed earlier (with states A=q0, B=q1, C=q2).
3. ε-NFA (NFA with ε-Transitions)
3.1 Intuition
An ε-NFA can move between states without consuming any input (ε transitions). This is useful for combining automata (union, concatenation, Kleene star).
3.2 ε-Closure
The ε-closure of a state q is the set of all states reachable from q via ε-transitions (including q itself).
ε-NFA Example: Accept strings with at least one 'a'
(Diagram)
ε-closure(q0) = {q0, q1} (q0 → ε → q1)
4. Closure Properties
Regular languages are closed under:
| Operation | Description | Construction |
|---|---|---|
| Union | L1∪L2 | ε-NFA: start → ε → NFA₁, start → ε → NFA₂ |
| Concatenation | L1⋅L2 | ε from accepting state of NFA₁ to start of NFA₂ |
| Kleene Star | L∗ | ε from accepting back to start |
| Complement | L | Swap accepting/non-accepting states in DFA |
| Intersection | L1∩L2 | Product construction (pair of states) |
| Reversal | LR | Reverse arrows, swap start/accept |
5. Common Pitfalls
Pitfall 1: DFA must have a transition for every symbol
Mistake: Creating a DFA where some states don't have transitions for some inputs.
Fix: Add a "dead state" (non-accepting, self-loops) for missing transitions.
Pitfall 2: Confusing NFA acceptance
Mistake: Thinking NFA rejects if any path rejects.
Correction: NFA accepts if at least one path accepts. All paths must be explored (or simulated via subset construction).
Pitfall 3: Forgetting ε-closure in NFA→DFA
Mistake: Using raw δ_NFA results without taking ε-closure.
Fix: Always compute ε-closure after each transition during subset construction.
6. 📐 Key Formulas / Concepts
| Concept | Formula |
|---|---|
| DFA transition | δ(q,a)=q′ (single state) |
| NFA transition | δ(q,a)={q1,q2,...} (set) |
| Extended transition | δ^(q,w) = state after reading entire string w |
| DFA states from NFA | SDFA=ε-closure(SNFA) |
| Union of DFAs | L1∪L2 via product construction |
| Complement of DFA | Accepting ↔ Non-accepting states swapped |
7. 📝 Practice Questions
Q1: Design a DFA for the language L = {w ∈ {0,1} | w contains "00" as substring}.*Answer: States: q0 (no 0 yet), q1 (saw first 0), q2 (saw "00" → accept), q3 (dead).Transitions: q0--0→q1, q0--1→q0; q1--0→q2, q1--1→q0; q2--0→q2, q2--1→q2; q3--0,1→q3. Q2: Convert this NFA to DFA: q0--a,b→q0, q0--a→q1, q1 --ε→ q2 (accept).Answer:
- Start: ε-closure(q0) = {q0}
- On a: ε-closure(δ(q0,a)) = ε-closure({q0,q1}) = {q0,q1,q2} = State B (accept)
- On b: ε-closure(δ(q0,b)) = ε-closure({q0}) = {q0} = State A
DFA: A--a→B, A--b→A, B--a→B, B--b→B Q3: Prove that the language L = {a^n b^n | n ≥ 0} is NOT regular.Answer: Use the pumping lemma. Assume L is regular with pumping length p. Choose w = a^p b^p. By pumping lemma, w = xyz, |xy| ≤ p, |y| ≥ 1, and xy^iz ∈ L for all i. Since |xy| ≤ p, y must consist only of a's. Pumping y (i=2) gives a^{p+|y|} b^p, which has more a's than b's → not in L. Contradiction. Hence L is not regular. Q4: Design an NFA for the set of all binary strings that contain "00" AND end with "11".Answer: Use product construction or NFA with branching: create an NFA that tracks both "saw 00" and "ends with 11" in parallel. Accept if both conditions met at end. Q5: What is the difference between Moore and Mealy machines?Answer: Moore machines produce output based on the current state (output associated with state). Mealy machines produce output based on transitions (output associated with edge). Any Mealy machine can be converted to a Moore machine and vice versa.
8. 🔗 Cross-References
- Week 2 - Regular Expressions: Equivalence with DFA/NFA
- Week 3 - DFA Minimization: Merging indistinguishable states
- BSCS4032 (Compiler Design): Lexical analysis uses DFA Join Discord NextNFA to DFA