Sub topic 12
The Myhill-Nerode theorem
The pumping lemma can show that a language is not regular, but it can never show that a language is regular. The Myhill-Nerode theorem works in both directions. It gives an exact test for regularity, and it also tells you how many states the smallest DFA needs. This chapter assumes you know DFAs, the subset construction, DFA minimization, and the pumping lemma. It also assumes you know what an equivalence relation is.
Distinguishable strings
Fix a language L over an alphabet Σ. Two strings x and y are indistinguishable with respect to L, written x ≡_L y, if appending any suffix z to both gives the same membership answer:
- For every z in Σ*, xz ∈ L if and only if yz ∈ L.
If some z gives different answers, then z distinguishes x and y. The relation ≡_L is an equivalence relation, so it splits Σ into equivalence classes. The number of classes is the index* of L. The index can be infinite.
Another way to see a class: it is the set of strings with the same future. The future of x is the set {z : xz ∈ L}.
The theorem
The following three statements are equivalent for any language L:
- L is regular.
- L is the union of some equivalence classes of a right-invariant equivalence relation of finite index.
- ≡_L has finite index.
Statement 3 is the one you use most. It gives two consequences:
- L is regular exactly when ≡_L has finitely many classes.
- If L is regular, the minimal complete DFA has exactly as many states as ≡_L has classes, and it is unique up to renaming states.
Why it holds
Regular implies finite index. Take any DFA for L with k states. If x and y lead to the same state, then xz and yz also lead to the same state for every z. So x ≡_L y, and there are at most k classes.
Finite index implies regular. Build a DFA whose states are the classes [x]. Use the transition δ([x], a) = [xa], the start state [ε], and accepting states [x] with x ∈ L. The transition is well defined because x ≡L y implies xa ≡L ya. Acceptance is well defined because ε is one of the suffixes, so x ≡_L y forces x ∈ L if and only if y ∈ L.
Minimality. In any DFA for L, strings that are in different classes must end in different states. So every DFA has at least as many states as there are classes.
The two ways to use it
Proving non-regularity
To show L is not regular, exhibit an infinite set of strings that are pairwise distinguishable. For each pair, you must name a suffix that separates them.
Take L = {aⁿbⁿ : n ≥ 0}. Consider the strings aⁱ for i = 0, 1, 2, and so on. For i ≠ j, use the suffix bⁱ:
- aⁱbⁱ is in L.
- aʲbⁱ is not in L, because the counts differ.
So every pair is distinguishable, the index is infinite, and L is not regular. No pumping-length argument is needed.
Counting minimal DFA states
To find the size of the minimal DFA, group strings by future and count the groups. Take L = (a|b)*abb. Track the longest suffix of the input that is a prefix of abb:
| Class | Representative | Meaning | Distinguishing suffix from ε |
|---|---|---|---|
| 1 | ε | no useful suffix | none (this is the base class) |
| 2 | a | ends in a | bb |
| 3 | ab | ends in ab | b |
| 4 | abb | ends in abb | ε |
The classes are pairwise distinguishable, so the minimal DFA has 4 states. This matches the subset-construction result for this language.
You can check this by computer. The code below groups all prefixes up to a given length by their answers on all suffixes up to a given length. This is a finite approximation of ≡_L, so it can only suggest the index, not prove it. It is still useful for checking a hand count.
import re
from itertools import product
pattern = re.compile(r"(a|b)*abb")
def strings_upto(n):
for k in range(n + 1):
for t in product("ab", repeat=k):
yield "".join(t)
def classes(prefix_len, suffix_len):
suffixes = list(strings_upto(suffix_len))
groups = {}
for x in strings_upto(prefix_len):
signature = tuple(bool(pattern.fullmatch(x + z)) for z in suffixes)
groups.setdefault(signature, []).append(x)
return list(groups.values())
groups = classes(4, 3)
print(len(groups)) # 4
print([g[0] for g in groups]) # ['', 'a', 'ab', 'abb']More worked examples
The k-th symbol from the end. Let L be the set of strings over {a, b} whose third symbol from the end is a. The future of a string depends on its last three symbols, and each of the 2³ = 8 patterns of three symbols has a different future. For example, strings ending in aaa and baa differ on the suffix ε, aab, and others. So the index is 8 and the minimal DFA needs 8 states. An NFA needs only 4. In general the DFA needs 2ᵏ states and the NFA needs k + 1. This is the standard example of exponential blow-up in the subset construction that cannot be avoided.
Length divisible by 4. The future of x depends only on |x| mod 4, so the index is 4.
Perfect squares. Let L = {aⁿ : n is a perfect square}. Take aⁱ and aʲ with i < j. Choose a suffix a^k such that i + k is a perfect square but j + k is not. For i = m² and j = n² with m < n, we get k = 2mn − ... easier: if i is not a square, extend to the next square. This is more fiddly than pumping, so for unary languages the pumping lemma is often quicker.
Myhill-Nerode compared with the pumping lemma
| Aspect | Pumping lemma | Myhill-Nerode |
|---|---|---|
| Direction | Necessary condition only | Necessary and sufficient |
| Proves regularity | No | Yes (finite index) |
| Proves non-regularity | Yes, by contradiction on any decomposition | Yes, by an infinite distinguishable set |
| Gives minimal DFA size | No | Yes |
| Typical effort | Handle every split xyz | Find one separating suffix per pair |
Common errors
- Showing only a few distinguishable strings. Three or four distinguishable strings prove only that the DFA has at least that many states. Non-regularity needs an infinite family.
- Forgetting the dead state. If some prefix can never be extended into L, those prefixes form a class. The minimal complete DFA counts it. GATE questions usually count it unless they say "minimal DFA without dead state".
- Distinguishing by the wrong side. The test appends a suffix to both strings. Prepending or comparing lengths alone proves nothing.
- Confusing the index with the NFA size. The index equals the minimal DFA size and can be exponentially larger than the smallest NFA.
Exercises
Exercise
Let L be the set of binary strings whose length is divisible by 4. What is the number of states in the minimal complete DFA, and which suffixes distinguish the classes?
Show answer
The future of a string x depends only on |x| mod 4, so the index is 4 and the minimal complete DFA has 4 states, with no dead state. Strings of lengths r and s with r ≠ s (mod 4) are distinguished by any suffix of length 4 − r mod 4. For example, ε and a string of length 1 are separated by a suffix of length 4, which is accepted after ε but not after one extra symbol.
Exercise
Use Myhill-Nerode to show that L = {w in {a, b}* : w has equal numbers of a's and b's} is not regular.
Show answer
Consider the strings aⁱ for i = 0, 1, 2, and so on. For i ≠ j, the suffix bⁱ gives aⁱbⁱ, which has equal counts and is in L, while aʲbⁱ has unequal counts and is not in L. So these strings are pairwise distinguishable. There are infinitely many of them, so ≡_L has infinite index and L is not regular.