G27
Self-checkedTier 2 11 min readtoc.myhill-nerode

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 L⊆Σ∗L \subseteq \Sigma^*. For strings x,y∈Σ∗x, y \in \Sigma^* write

x≡Ly  ⟺  for every z∈Σ∗:  (xz∈L⇔yz∈L).x \equiv_L y \iff \text{for every } z \in \Sigma^* : \; (xz \in L \Leftrightarrow yz \in L).

In words: xx and yy have the same future (the set of suffixes zz with xz∈Lxz \in L). A string zz with exactly one of xzxz, yzyz in LL distinguishes xx and yy.

  • ≡L\equiv_L is an equivalence relation. Its classes partition Σ∗\Sigma^*. The number of classes is the index of LL.
  • It is right invariant: if x≡Lyx \equiv_L y then xa≡Lyaxa \equiv_L ya for every symbol aa (a continuation of xaxa is a continuation of xx).
  • LL is a union of classes (all members of a class are in LL or all are not, since z=εz = \varepsilon is a test).

Theorem (Myhill-Nerode). For a language LL the following are equivalent: (1) LL is regular; (2) LL is the union of some classes of a right invariant equivalence relation of finite index; (3) ≡L\equiv_L has finitely many classes. Moreover, the number of classes of ≡L\equiv_L is the number of states of the minimal complete DFA for LL, and that DFA is unique up to renaming the states.

Proof idea. (1) to (3): if a DFA with kk states accepts LL, strings that end in the same state have the same future, so ≡L\equiv_L has at most kk classes. (3) to (1): build the DFA whose states are the classes, whose start state is the class of ε\varepsilon, whose transition on aa sends the class of xx to the class of xaxa (well defined because of right invariance) and whose accepting states are the classes inside LL. It accepts LL. No smaller DFA exists, by the first direction.

How it works

Reading the minimal DFA off the classes

Take LL = the strings over {a,b}\{a, b\} that end with abab. Group the strings by their future:

The Myhill-Nerode classes of the strings that end with ab: start

Computed by a script, not typed
classmembers (first few)accepting?after reading aafter reading bcompleting 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.

0 / 3
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

Three classes: strings that do not end in aa or abab (their future is "must still read abab"), strings that end in aa (they need one more bb) and strings that end in abab (accepting). The last three columns of the table are the transition table of the minimal DFA, and no DFA with two states accepts LL.

The class of a string that cannot be extended to a member of LL (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 LL is not regular. Method: choose an infinite family x1,x2,…x_1, x_2, \dots and, for every pair i≠ji \ne j, a suffix zijz_{ij} that is completed to a member of LL from one of them but not from the other.

For L={anbn:n≥0}L = \{a^n b^n : n \ge 0\} take xi=aix_i = a^i. For i<ji < j the suffix bib^i gives aibi∈La^i b^i \in L but ajbi∉La^j b^i \notin L:

a^i and a^j are never in the same class: start

Computed by a script, not typed
prefixa^0a^1a^2a^3a^4a^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.

0 / 6
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

Every pair is separated, so LL has infinitely many classes (at least one for each ii) 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 anbna^n b^n 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 lengthends with abeven number of a'scontains abafirst symbol = last symbol (nonempty)a^n b^npalindromes
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.

0 / 8
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

Two cautions. (1) The suffix must work for the pair: one of xzxz, yzyz in LL, 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 aa's is divisible by 3 and the number of bb's is even": the information is (number of aa's mod 3, number of bb's mod 2): 3×2=63 \times 2 = 6 classes.
  • "the kk-th symbol from the end is aa": the information is the last kk symbols (a shorter string counts as if padded with bb's): 2k2^k classes. This is why the DFA for the third symbol from the end has 88 states while the NFA has 44.
  • "the length is ≡1(mod4)\equiv 1 \pmod 4": the information is the length mod 4: 44 classes.
  • "contains abaaba": the information is the longest suffix that is a prefix of abaaba, or "already found": 44 classes.

A language whose classes are the elements of a group

Let the alphabet be the set of all permutations of kk objects, and LL 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 k!k! permutations.

Strings of permutations whose composition is the identity: start

Computed by a script, not typed
alphabetalphabet sizeclasses found among prefixes of length ≤ 1states 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.

0 / 3
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

The minimal DFA has k!k! states; for k=5k = 5 that is 120120.

Compared with the pumping lemma

pumping lemmaMyhill-Nerode
can prove "not regular"yesyes
can prove "regular"noyes (finite index)
gives the minimal number of statesnoyes
work neededfind a string and show every cut failsfind 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

LL = strings over {a,b}\{a, b\} with an even number of aa's. Two classes: even and odd number of aa's. ε≢a\varepsilon \not\equiv a (the suffix ε\varepsilon distinguishes them). So the minimal DFA has 22 states. Check against the growth table: the column "even number of a's" stops at 22.

EX 2Why the third symbol from the end needs 8 states

Easy

Two 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 23=82^3 = 8 possible last-three-symbol patterns are pairwise distinguishable: the minimal DFA has 88 states, although a 4-state NFA accepts LL.

EX 3Strings with the same first and last symbol

Medium

LL = nonempty strings whose first and last symbols are equal. Classes: the empty string; strings that start with aa and end with aa; start with aa, end with bb; start with bb, end with bb; start with bb, end with aa: 55 classes (as the growth table shows), so LL is regular with a 5-state minimal DFA.

EX 4A language that is not regular

Medium

LL = palindromes over {a,b}\{a, b\}. Take xi=aibx_i = a^i b. The suffix aia^i completes xix_i to the palindrome aibaia^i b a^i, while xjai=ajbaix_j a^i = a^j b a^i with j≠ij \ne i is not a palindrome. All xix_i are pairwise distinguishable: infinitely many classes, so LL is not regular.

EX 5Typical questions

GATE level

(a) "The minimum number of states in a DFA for LL 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 anbna^n b^n, {ww}\{ww\}, palindromes), the language is not regular. (c) "Number of classes of ≡L\equiv_L / states of the minimal DFA for strings of permutations whose product is the identity": the number of permutations. (d) "The index of ≡L\equiv_L is finite iff LL 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

  • x≡Lyx \equiv_L y iff for all zz: xz∈L⇔yz∈Lxz \in L \Leftrightarrow yz \in L. Right invariant.
  • LL regular   ⟺  \iff 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: 3×2=63 \times 2 = 6.
  • kk-th symbol from the end: 2k2^k. Length mod mm: mm. Permutations of kk objects under composition: k!k!.
  • Stronger than the pumping lemma: "if and only if".

Traps and GATE patterns

Connections

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

GATE 2025 · Set 1 · MCQ · 2 marksQ6 · page 197 of the book
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.
GATE 2019 · NAT · 2 marksQ27 · page 200 of the book
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 COMPUTATION
GATE 2014 · Set 2 · MCQQ44 · page 202 of the book
Let 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 x≡Lyx \equiv_L y when xx is abab and yy is abababab in LL = strings ending with abab?

Show answer

They are equivalent. Both end with abab, so for every zz the last two symbols of xzxz and of yzyz are the same (if zz has at least two symbols they are the last two of zz; if zz is shorter they are made of the end of abab and zz, for both strings). Membership depends only on those two symbols.

Question 2

How many states has the minimal DFA for the strings over {0,1}\{0, 1\} whose length is divisible by 5?

Show answer

5 (the length mod 5 is all that matters; no dead state since every string can be completed).

Question 3

Which suffix separates a2a^2 and a5a^5 for {anbn}\{a^n b^n\}?

Show answer

b2b^2: a2b2∈La^2 b^2 \in L, a5b2∉La^5 b^2 \notin L.

Question 4

Does a finite number of classes of ≡L\equiv_L prove that LL is regular?

Show answer

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 answer

No.

Revision card

One-screen revision

  • x≡Lyx \equiv_L y 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.
  • kk-th from the end: 2k2^k. Mod mm: mm. Permutations of kk objects: k!k!.
  • 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