Myhill-Nerode theorem
The Myhill-Nerode theorem: the equivalence of strings by their possible continuations, why a language is regular exactly when it has finitely many classes, how the classes are the states of the minimal DFA (with its transition table read off the classes), how to prove a language not regular by exhibiting infinitely many pairwise distinguishable strings, how to count the states of a minimal DFA, and how the theorem compares with the pumping lemma.
Why it exists
How many states does the smallest DFA for a language need? How do you prove that no DFA exists? The pumping lemma answers only the second question, and only in one direction (it can prove non-regularity but never regularity). The Myhill-Nerode theorem gives an exact answer to both: it characterises regular languages by a property of the language alone, and the number it counts is the number of states of the minimal DFA. GATE asks: the minimum number of states of a DFA for this language, prove or decide that this language is regular, how many equivalence classes does this relation have.
Prerequisites
You need DFAs and their minimisation (DFA minimisation), in particular the idea that two states are distinguishable if some string leads one to an accepting state and the other not.
Precise definitions
Fix a language . For strings write
In words: and have the same future (the set of suffixes with ). A string with exactly one of , in distinguishes and .
- is an equivalence relation. Its classes partition . The number of classes is the index of .
- It is right invariant: if then for every symbol (a continuation of is a continuation of ).
- is a union of classes (all members of a class are in or all are not, since is a test).
Theorem (Myhill-Nerode). For a language the following are equivalent: (1) is regular; (2) is the union of some classes of a right invariant equivalence relation of finite index; (3) has finitely many classes. Moreover, the number of classes of is the number of states of the minimal complete DFA for , and that DFA is unique up to renaming the states.
Proof idea. (1) to (3): if a DFA with states accepts , strings that end in the same state have the same future, so has at most classes. (3) to (1): build the DFA whose states are the classes, whose start state is the class of , whose transition on sends the class of to the class of (well defined because of right invariance) and whose accepting states are the classes inside . It accepts . No smaller DFA exists, by the first direction.
How it works
Reading the minimal DFA off the classes
Take = the strings over that end with . Group the strings by their future:
The Myhill-Nerode classes of the strings that end with ab: start
Computed by a script, not typed| class | members (first few) | accepting? | after reading a | after reading b | completing suffixes (length ≤ 2) |
|---|---|---|---|---|---|
| class 0: representative ε | |||||
| class 1: representative a | |||||
| class 2: representative ab |
Step 0
There are 3 classes, so the minimal DFA has 3 states, and the last three columns are its transition table. This is the Myhill-Nerode construction: states are classes, the transition on a sends the class of u to the class of ua.
Three classes: strings that do not end in or (their future is "must still read "), strings that end in (they need one more ) and strings that end in (accepting). The last three columns of the table are the transition table of the minimal DFA, and no DFA with two states accepts .
The class of a string that cannot be extended to a member of (every future is empty) is the trap class; a minimal complete DFA has such a state when it exists. A minimal partial DFA drops it, so state the convention when counting.
Proving that a language is not regular
If there are infinitely many pairwise distinguishable strings, the index is infinite and is not regular. Method: choose an infinite family and, for every pair , a suffix that is completed to a member of from one of them but not from the other.
For take . For the suffix gives but :
a^i and a^j are never in the same class: start
Computed by a script, not typed| prefix | a^0 | a^1 | a^2 | a^3 | a^4 | a^5 |
|---|---|---|---|---|---|---|
| a^0 | ||||||
| a^1 | ||||||
| a^2 | ||||||
| a^3 | ||||||
| a^4 | ||||||
| a^5 |
Step 0
Every pair of different prefixes a^i, a^j is separated by a suffix b^min(i,j): a^i b^i is in the language and a^j b^i is not. So the language has infinitely many classes (one for each i), no DFA exists, and the language is not regular.
Every pair is separated, so has infinitely many classes (at least one for each ) and is not regular. A table of classes cannot prove this by itself, but it shows the pattern: as longer prefixes are allowed, the number of classes keeps growing for and for palindromes, while it stops at the number of states for regular languages.
Counting the classes as longer and longer prefixes are allowed: start
Computed by a script, not typed| prefix length | ends with ab | even number of a's | contains aba | first symbol = last symbol (nonempty) | a^n b^n | palindromes |
|---|---|---|---|---|---|---|
| prefixes of length ≤ 0 | ||||||
| prefixes of length ≤ 1 | ||||||
| prefixes of length ≤ 2 | ||||||
| prefixes of length ≤ 3 | ||||||
| prefixes of length ≤ 4 | ||||||
| prefixes of length ≤ 5 | ||||||
| prefixes of length ≤ 6 | ||||||
| prefixes of length ≤ 7 |
Step 0
Regular languages level off at their number of states (3, 2, 4 and 5 here: the minimal DFAs). The counts for a^n b^n and for palindromes keep increasing: infinitely many classes, so not regular. A table can only suggest this; the proof is a distinguishing suffix for each pair, as in the previous table.
Two cautions. (1) The suffix must work for the pair: one of , in , the other not. (2) Finitely many distinguishable strings never prove anything; the family must be infinite.
Counting the states of a minimal DFA
To find the minimal number of states: find the classes, that is, describe a string by the least information that determines its future, and count the possible values of that information.
- "the number of 's is divisible by 3 and the number of 's is even": the information is (number of 's mod 3, number of 's mod 2): classes.
- "the -th symbol from the end is ": the information is the last symbols (a shorter string counts as if padded with 's): classes. This is why the DFA for the third symbol from the end has states while the NFA has .
- "the length is ": the information is the length mod 4: classes.
- "contains ": the information is the longest suffix that is a prefix of , or "already found": classes.
A language whose classes are the elements of a group
Let the alphabet be the set of all permutations of objects, and the set of strings whose product (composition) is the identity. After reading a string the only thing that matters is the product so far, and two different products are distinguished by the inverse of one of them. So the classes are the permutations.
Strings of permutations whose composition is the identity: start
Computed by a script, not typed| alphabet | alphabet size | classes found among prefixes of length ≤ 1 | states of the minimal DFA |
|---|---|---|---|
| permutations of 2 objects | |||
| permutations of 3 objects | |||
| permutations of 4 objects |
Step 0
For the permutations of k objects the minimal DFA has k! states (a state for every possible product). Reason by Myhill-Nerode: the future of a string depends only on its product, and different products have different futures.
The minimal DFA has states; for that is .
Compared with the pumping lemma
| pumping lemma | Myhill-Nerode | |
|---|---|---|
| can prove "not regular" | yes | yes |
| can prove "regular" | no | yes (finite index) |
| gives the minimal number of states | no | yes |
| work needed | find a string and show every cut fails | find infinitely many distinguishable strings |
There are non-regular languages that satisfy the pumping condition (see the pumping lemma); Myhill-Nerode classifies them correctly because it is an "if and only if".
Worked examples
EX 1The classes of a simple language
Easy= strings over with an even number of 's. Two classes: even and odd number of 's. (the suffix distinguishes them). So the minimal DFA has states. Check against the growth table: the column "even number of a's" stops at .
EX 2Why the third symbol from the end needs 8 states
EasyTwo strings that differ in one of the last three symbols are separated by a suffix of length at most 2 that moves the differing symbol to the third position from the end. So the possible last-three-symbol patterns are pairwise distinguishable: the minimal DFA has states, although a 4-state NFA accepts .
EX 3Strings with the same first and last symbol
Medium= nonempty strings whose first and last symbols are equal. Classes: the empty string; strings that start with and end with ; start with , end with ; start with , end with ; start with , end with : classes (as the growth table shows), so is regular with a 5-state minimal DFA.
EX 4A language that is not regular
Medium= palindromes over . Take . The suffix completes to the palindrome , while with is not a palindrome. All are pairwise distinguishable: infinitely many classes, so is not regular.
EX 5Typical questions
GATE level(a) "The minimum number of states in a DFA for is ..." Describe the information a string must remember and count its values; check the answer by trying to merge two of the states you found. (b) "Which of these languages is regular?" Look for a finite description of the future; if the future depends on an unbounded count that must be compared with something later (for example , , palindromes), the language is not regular. (c) "Number of classes of / states of the minimal DFA for strings of permutations whose product is the identity": the number of permutations. (d) "The index of is finite iff is ..." regular. (e) "The minimal DFA of a regular language is unique." True, up to renaming of states (for complete DFAs).
Cheat box
Cheat box
- iff for all : . Right invariant.
- regular finitely many classes. Number of classes = states of the minimal complete DFA (trap state included).
- Not regular: find infinitely many pairwise distinguishable strings (a distinguishing suffix for each pair).
- Count states: the least information that decides the future. Product of independent counters multiplies: .
- -th symbol from the end: . Length mod : . Permutations of objects under composition: .
- Stronger than the pumping lemma: "if and only if".
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
The minimisation algorithm of DFA minimisation computes the classes by table filling; the level at which a pair of states is marked equals the length of the shortest string that distinguishes them. The theorem complements the pumping lemma, which only proves non-regularity. Designing a DFA by deciding what to remember (DFA design patterns) is the same as finding the classes.
PYQs
3 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider the following two languages over the alphabet {a,
b}:
L1 = { αβα | α ∈ {a, b}+ AND β ∈ {a, b}+ }
L2 = { αβα | α ∈ {a}+ AND β ∈ {a, b}+ }
Which ONE of the following statements is CORRECT?- (a). Both L1 and L2 are regular languages.
- (b). L1 is a regular language but L2 is not a regular language.
- (c). L1 is not a regular language but L2 is a regular language.
- (d). Neither L1 nor L2 is a regular language.
Let Σ be
the
set
of
all
bijections
from {1, ... ,5} to {1, ... ,5}, where id denotes the identity
function, i.e. id(j) = j, ∀j. Let ∘ denote composition on
functions.
For
a
string x= x1x2 ... xn∈Σn, n≥0,
let π(x) = x1 ∘x2 ∘⋯∘xn. Consider the language L=
{x∈Σ*|π(x) = id}. The minimum number of states in
any DFA accepting L is _______
THEORY OF COMPUTATIONLet L1 = {w ∈ {0,1}* | w has at least as many occurrences
of (110)'s as (011)'s}. Let L2 = {w ∈ {0,1}*|w has at least
as many occurrences of (000)'s as (111)'s}. Which one of
the following is TRUE?- (a)L1 is regular but not L2.
- (b)L2 is regular but not L1.
- (c)Both L1 and L2 are regular.
- (d)Neither L1 nor L2 are regular THEORY OF COMPUTATION
Self-check
Question 1
What is when is and is in = strings ending with ?
Show answerHide
They are equivalent. Both end with , so for every the last two symbols of and of are the same (if has at least two symbols they are the last two of ; if is shorter they are made of the end of and , for both strings). Membership depends only on those two symbols.
Question 2
How many states has the minimal DFA for the strings over whose length is divisible by 5?
Show answerHide
5 (the length mod 5 is all that matters; no dead state since every string can be completed).
Question 3
Which suffix separates and for ?
Show answerHide
: , .
Question 4
Does a finite number of classes of prove that is regular?
Show answerHide
Yes, that is the theorem: finite index implies regular, because the classes are the states of a DFA.
Question 5
A language has an infinite set of pairwise distinguishable strings. Regular?
Show answerHide
No.
Revision card
One-screen revision
- iff same future. Classes = states of the minimal complete DFA; unique.
- Finite index iff regular. Infinite pairwise distinguishable family iff not regular.
- Count states by the information the future depends on; independent counters multiply; add the dead state.
- -th from the end: . Mod : . Permutations of objects: .
- Stronger than the pumping lemma (an iff).
References
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 4.4 (equivalence and minimisation of automata) and the exercises of Chapter 4 on the Myhill-Nerode theorem
- ·Kozen, Automata and Computability, the lectures on minimisation and the Myhill-Nerode relations
- ·Sipser, Introduction to the Theory of Computation (3rd edition), the problems of Chapter 1 on distinguishable strings (the theorem itself is not stated in the text)
Verification
scripts/toc_t2.py and scripts/enum-checks/toc_t2_check.py: for four regular languages (ends with ab, even number of a's, contains aba, first symbol equals last symbol) a hand-built DFA agrees with a regular-expression predicate on all strings up to length 10, the number of classes found by comparing futures (prefixes up to length 7, suffixes up to length 8) equals the number of states of the minimised DFA, and the quotient automaton built from the classes (state = class, transition = class of the representative followed by a symbol) accepts exactly the language; for a^n b^n every pair a^i, a^j (i, j up to 8) is separated by exactly the suffix b^min(i, j); the class counts of the growth table are recomputed from the definition with independent predicates (and again in tests/toc-t2-traces.test.ts); the language of strings of permutations whose composition is the identity has n! classes and a minimal DFA with n! states for n = 2, 3, 4 and its DFA agrees with explicit composition on 300 random sequences each; the counts used in the worked examples (6 for a's mod 3 and b's mod 2, 4 for length mod 4 = 1, 2^k for the k-th symbol from the end) are computed; the level at which the table-filling algorithm marks a pair of states equals the length of the shortest distinguishing suffix (all pairs of the four DFAs). The theorem and its proof are standard; the statement about fooling sets for NFAs is QUOTED (needs verification). Textbook chapter references are from memory. The independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)