DFA Minimization and Pumping Lemma
2241 words
11 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
# DFA Minimization and Pumping Lemma ## 🎯 Learning Objectives - Minimize a DFA using table-filling and equivalence algorithms - Apply the Myhill-Nerode theorem to characterize regular languages - Prove a language is non-regular using the pumping lemma - Convert between DFAs, NFAs, and regular expressions - Trace al...

DFA Minimization and Pumping Lemma
🎯 Learning Objectives
- Minimize a DFA using table-filling and equivalence algorithms
- Apply the Myhill-Nerode theorem to characterize regular languages
- Prove a language is non-regular using the pumping lemma
- Convert between DFAs, NFAs, and regular expressions
- Trace algorithm execution with step-by-step tables
1. DFA Minimization
1.1 Intuition
When you first design a DFA, you often create extra states that are redundant — they behave identically from the perspective of acceptance. Minimization is like removing duplicate workers: if two employees do exactly the same job (they have the same future behavior for all inputs), you can fire one and redirect their work to the other.
A minimal DFA is unique (up to renaming of states), which makes it the canonical representation of a regular language.
1.2 Table-Filling Algorithm
The algorithm works by marking pairs of states as distinguishable (non-equivalent) if their behavior differs.
(Diagram)
1.3 Worked Example
DFA to minimize:
(Diagram)
States: q0, q1, q2, q3 Final states: q3
Step 1 — Initialize table:
| q1 | q2 | q3 | |
|---|---|---|---|
| q0 | |||
| q1 | |||
| q2 |
Step 2 — Mark (final, non-final) pairs: (q0, q3), (q1, q3), (q2, q3) are marked ✓ because q3 is final and others are not.
| q1 | q2 | q3 | |
|---|---|---|---|
| q0 | ✓ | ||
| q1 | ✓ | ||
| q2 | ✓ |
Step 3 — Check each unmarked pair:
Check (q0, q1):
- On 0: δ(q0,0)=q1, δ(q1,0)=q2 → (q1, q2) unmarked
- On 1: δ(q0,1)=q2, δ(q1,1)=q3 → (q2, q3) marked ✓
- Since (q2, q3) is distinguishable, mark (q0, q1) ✓ Check (q0, q2):
- On 0: δ(q0,0)=q1, δ(q2,0)=q2 → (q1, q2) unmarked
- On 1: δ(q0,1)=q2, δ(q2,1)=q3 → (q2, q3) marked ✓
- Mark (q0, q2) ✓ Check (q1, q2):
- On 0: δ(q1,0)=q2, δ(q2,0)=q2 → (q2, q2) same state, OK
- On 1: δ(q1,1)=q3, δ(q2,1)=q3 → (q3, q3) same state, OK
- No marked pair found → (q1, q2) are equivalent Final table:
| q1 | q2 | q3 | |
|---|---|---|---|
| q0 | ✓ | ✓ | ✓ |
| q1 | OK | ✓ | |
| q2 | ✓ |
Result: q1 ≡ q2 (merge them). Merge q1 and q2 into q1'.
Minimized DFA:
- States: {q0}, {q1, q2} → q1', {q3}
- Transitions: δ(q0,0)=q1', δ(q0,1)=q1', δ(q1',0)=q1', δ(q1',1)=q3, δ(q3,0)=q3, δ(q3,1)=q3
2. Myhill-Nerode Theorem
2.1 Intuition
The Myhill-Nerode theorem gives an alternative characterization of regular languages based on equivalence relations on strings. Two strings are equivalent if, for any possible continuation, the resulting strings are either both in the language or both not. The number of equivalence classes equals the number of states in the minimal DFA.
2.2 Definition
Define the equivalence relation x≡Ly if for all strings z, xz∈L⟺yz∈L.
Theorem: L is regular if and only if ≡L has finitely many equivalence classes.
2.3 Worked Example
Language: L={w∈{0,1}∗∣w has an even number of 0s}
Equivalence classes:
| Class | Representative | Meaning | After adding '0' | After adding '1' |
|---|---|---|---|---|
| C0 | ε | Even 0s so far | Goes to C1 | Stays in C0 |
| C1 | 0 | Odd 0s so far | Goes to C0 | Stays in C1 |
Only 2 classes → L is regular. The minimal DFA has exactly 2 states.
3. The Pumping Lemma for Regular Languages
3.1 Intuition
The pumping lemma says: if a language is regular, then all sufficiently long strings have a loop (a cycle in the DFA) that can be pumped — repeated any number of times — while staying in the language. It's like a repeating section in a song: the chorus comes around again and again, and the song is still recognizable.
(Diagram)
3.2 Formal Statement
If L is regular, then there exists a pumping length p>0 such that for any string s∈L with ∣s∣≥p, we can split s=xyz where:
- ∣y∣≥1 (the loop is non-empty)
- ∣xy∣≤p (the loop is within the first p characters)
- xyiz∈L for all i≥0 (pumping preserves membership)
3.3 Proving Non-Regularity
The pumping lemma is used by contradiction to prove languages are NOT regular.
Template:
- Assume L is regular (pumping length p exists)
- Choose s∈L with ∣s∣≥p (strategically pick s)
- Show that for ALL possible splits s=xyz satisfying conditions 1-2, xy2z∈/L (pumping breaks membership)
- Contradiction → L is not regular
3.4 Worked Example: L={0n1n∣n≥0}
Proof that L is not regular:
- Assume L is regular with pumping length p.
- Choose s=0p1p∈L. Clearly ∣s∣=2p≥p.
- The pumping lemma says we can split s=xyz with ∣xy∣≤p and ∣y∣≥1. Since ∣xy∣≤p, both x and y consist only of 0s.
- Let y=0k for some k≥1.
- Pump once: xy2z=x(0k)(0k)1p=0p+k1p.
- This string has more 0s than 1s → not in L.
- Contradiction. Therefore L is not regular. Tracing Table — Possible splits for p=3, s=000111: | Split | x | y | z | |y| | |xy| | xy²z | In L? | |-------|---|---|---|-----|------|------|-------| | 1 | ε | 0 | 00111 | 1 | 1 | 000111 | No (4 zeros, 3 ones) | | 2 | 0 | 0 | 0111 | 1 | 2 | 000111 | No (4 zeros, 3 ones) | | 3 | 00 | 0 | 111 | 1 | 3 | 000111 | No (4 zeros, 3 ones) | All possible splits produce xy2z∈/L.
4. More Pumping Lemma Examples
4.1 Example 2: L={wwR∣w∈{0,1}∗}
Proof:
- Assume L is regular with pumping length p.
- Choose s=0p110p∈L (this is wwR where w=0p1).
- With ∣xy∣≤p, y consists only of 0s: y=0k, k≥1.
- xy2z=0p+k110p — first part has more 0s than second part. This is not a palindrome → not in L.
- Contradiction. L is not regular.
4.2 Example 3: L={an∣n is prime}
Proof:
- Assume L is regular with pumping length p.
- Choose s=aq where q≥p is prime.
- Split s=xyz where y=ak, k≥1.
- xyq+1z=aq+q⋅k=aq(1+k).
- Since q(1+k) is composite (q⋅(1+k) where both factors >1), xyq+1z∈/L.
- Contradiction. L is not regular.
5. Common Pitfalls
Pitfall 1: Pumping the Wrong Direction
The mistake: Trying to prove non-regularity by pumping in the wrong direction (pumping down when you should pump up, or vice versa).
Why students make it: The lemma says xyiz∈L for all i≥0, so pumping down (i=0) is often easier to check.
How to catch it: Pumping down doesn't always work — shortening the string might still produce a valid string in the language. Pumping up (i=2) is usually more effective.
Correct approach: Choose the pumping direction (i=0 or i>1) based on which creates a contradiction for your specific language.
Pitfall 2: Choosing the Wrong String s
The mistake: Picking an s that can be split such that pumping works.
Why students make it: Any s∈L with ∣s∣≥p should work in theory, but some strings are easier to disprove.
How to catch it: If you try s=0p1p and it works, try s=0p1p — oh wait, that's the same. The key is picking s such that ALL possible splits satisfying ∣xy∣≤p force y to be in a restricted region.
Correct approach: Use s=0p1p for {0n1n} because xy limited to first p chars forces y to be all 0s.
Pitfall 3: Forgetting ∣xy∣≤p
The mistake: Considering splits where ∣xy∣>p, which violates the pumping lemma condition.
Why students make it: The condition isn't intuitive — why does the loop have to be within the first p characters?
How to catch it: The ∣xy∣≤p condition comes from the DFA having only p states — within p characters, you must have revisited a state (formed a loop).
Correct approach: Only consider splits where ∣xy∣≤p. If a split has ∣xy∣>p, the pumping lemma doesn't guarantee pumping works.
6. Key Concepts Reference
| Concept | Definition | Application |
|---|---|---|
| DFA minimization | Merging equivalent states | Optimizing DFA size |
| Distinguishable states | States with different future behavior | Table-filling algorithm |
| Myhill-Nerode | Equivalence classes of strings | Characterizes regular languages |
| Pumping lemma | Loops in DFAs can be repeated | Proving non-regularity |
| Pumping length p | Number of states in DFA | Guarantees a loop exists |
| Contradiction proof | Assume regular → pump → contradiction | Non-regularity proofs |
7. 📝 Practice Questions
Q1: Minimize a DFA with states {A,B,C} where A is initial, C is final. Transitions: δ(A,0)=B, δ(A,1)=C, δ(B,0)=B, δ(B,1)=C, δ(C,0)=C, δ(C,1)=C.Answer:Step 1: Mark (A,C) and (B,C) — C is final, A/B are not.
| B | C | |
|---|---|---|
| A | ✓ | |
| B | ✓ |
Step 2: Check (A,B):
- On 0: δ(A,0)=B, δ(B,0)=B → same state, OK
- On 1: δ(A,1)=C, δ(B,1)=C → same state, OK
- No marked pair found → A ≡ B
Result: Merge A and B. Minimized DFA has 2 states: {A,B} and {C}. Q2: Prove that L = {ww | w ∈ {0,1}*} is not regular using the pumping lemma.Answer:
- Assume L is regular with pumping length p.
- Choose s = 0^p10^p1. This is ww where w = 0^p1 ∈ L.
- |s| = 2p+2 ≥ p. With |xy| ≤ p, y consists only of 0s from the first block: y = 0^k, k ≥ 1.
- Pump once: xy²z = 0^{p+k}10^p1.
- The first half (before the middle) is 0^{p+k}1, the second half is 0^p1. These are different → not ww.
- Contradiction. L is not regular. Q3: For L = {0^n1^m | n < m}, prove it's not regular.
Answer:
- Assume L is regular with pumping length p.
- Choose s = 0^p1^{p+1} ∈ L (n=p, m=p+1, so n < m).
- With |xy| ≤ p, y consists only of 0s: y = 0^k, k ≥ 1.
- Pump up: xy²z = 0^{p+k}1^{p+1}. Now n = p+k, m = p+1.
- For k ≥ 1, p+k ≥ p+1, so n ≥ m. This violates n < m → xy²z ∉ L.
- Contradiction. L is not regular. Q4: What are the Myhill-Nerode equivalence classes for L = {0^n1^0^n} (language of strings with equal 0s at both ends)?
Answer:The equivalence classes are:
- C_0: Strings where we haven't seen a 1 yet (prefix of 0s)
- C_1: Strings that have seen a 1 (mid-section)
- C_reject: Strings that break the pattern
Since there are finitely many classes (3), L is regular. This is expected since this language is just 0_10_. Q5: Explain why the pumping lemma condition |xy| ≤ p is necessary.Answer: The |xy| ≤ p condition comes from the pigeonhole principle: a DFA with p states processing p characters must visit at least p+1 states (starting state + p transitions). With only p states, by the pigeonhole principle, at least one state is visited twice. The first occurrence marks the start of the loop (x), the repeat marks the end of y. So xy ≤ p ensures the loop is completed within the first p characters. Without this condition, y might not correspond to a DFA loop. Q6: Can a non-regular language satisfy the pumping lemma? Why or why not?Answer: Yes! The pumping lemma is a necessary but not sufficient condition for regularity. Some non-regular languages satisfy the pumping lemma. Example: L = {a^nb^m | n ≥ 1, m ≥ n} ∪ {a^nb^m | n ≥ 1, m = n²}. This language satisfies the pumping lemma but is not regular. This is why the pumping lemma can only prove non-regularity (by contradiction) — it cannot prove regularity. Q7: For a DFA with 5 states, what's the maximum possible number of distinguishable state pairs?Answer: Total pairs = C(5,2) = 10. If all states are mutually distinguishable, all 10 pairs are marked. If some are equivalent (merged), fewer pairs are marked. The maximum is 10 (no equivalent states), giving a minimal DFA with 5 states. Q8: Use the pumping lemma to prove L = {0^i1^j0^k | i = j + k} is not regular.Answer:
- Assume L is regular with pumping length p.
- Choose s = 0^{2p}1^p0^p ∈ L (i=2p, j=p, k=p, so i = j+k).
- With |xy| ≤ p, y consists only of 0s: y = 0^m, m ≥ 1.
- Pump up: xy²z = 0^{2p+m}1^p0^p. Now i = 2p+m, j = p, k = p.
- j + k = p + p = 2p, but i = 2p+m > 2p. So i ≠ j+k → xy²z ∉ L.
- Contradiction. L is not regular.
8. 🔗 Cross-References
- Week 1 - Finite Automata: DFA/NFA foundations
- Week 2 - Regular Expressions: Equivalence to DFAs
- Week 4 - Context-Free Grammars: Beyond regular languages
- BSCS4032 (Compiler Design): Lexical analysis, DFA minimization Join Discord PreviousRegex to DFANextContext-Free Grammars