Myhill–Nerode: What a State Remembers
What each state of an automaton remembers, indistinguishable words and Myhill–Nerode classes, a lower bound on the number of states, and the smallest automaton for a language: how to build it and why it is unique up to renaming.
Last updated:
In this lesson we learn to describe a state of a deterministic finite automaton (DFA) by the words that reach it; to define when two words are indistinguishable with respect to a language, and to split all the words into Myhill–Nerode classes; to prove a lower bound on the number of states of every DFA for the language; and to build from the classes the smallest DFA for the language, and prove that it is unique up to renaming.
What each state remembers
The language of the lesson on minimization by table filling is
that is, the words that contain or contain . Minimizing its product automaton gave a DFA with four states, which can also be designed directly; we call it .
Its states: (start, no letter read), and (the last letter, and no pair of equal adjacent letters found yet), and (a pair found; an accepting trap state).
| State | on | on |
|---|---|---|
Transitions of the diagram:
- δ(r_0, a) = r_a
- δ(r_0, b) = r_b
- δ(r_a, a) = r_pair
- δ(r_a, b) = r_b
- δ(r_b, a) = r_a
- δ(r_b, b) = r_pair
- δ(r_pair, a) = r_pair
- δ(r_pair, b) = r_pair
Think: which words reach each of the four states? Describe each set in words, and write three words from each set.
Answer
- Only the empty word reaches : every letter takes the run out of , and no arrow returns to it.
- The words that have no pair of equal adjacent letters and end in reach : , , .
- The words that have no pair and end in reach : , , .
- The words of reach : , , .
A state “remembers” what all the words that reach it have in common.
The automaton does not know which of these words was read, so every continuation of a word acts on all of them in the same way:
the run continues from the same state. Among these four states there are no two states that can be merged:
every pair has a distinguishing word (see Quotient automaton).
But perhaps the language has an entirely different DFA, one that does not come from minimizing the product automaton, with only three states.
The question of this lesson: is there a DFA for the language with fewer than four states?
Indistinguishable words
Suppose a DFA (here is the start state) recognizes a language , and the runs on two words and end in the same state .
For every word , by the claim on reading a word in two parts,
so the runs on and on end in the same state, for every continuation .
Formulate: what must two words and satisfy, without mentioning an automaton, so that a DFA for the language “does not need to tell them apart”?
Write a condition using only.
Hint
Candidate definitions: (1) both words are in or both are not in ; (2) both words end in the same letter; (3) for every word , if and only if .
Check each of them on and , and on and .
The formal definition
Let be a language, and let . A word distinguishes the words and with respect to if exactly one of the words and belongs to .
If they have no distinguishing word, they are indistinguishable words with respect to , and we write :
Otherwise and are distinguishable. Candidate (1) is only the case :
the words and are both not in , but distinguishes them, because and .
Candidate (2) is wrong for this language too, in both directions:
and end in the same letter, but distinguishes them; and although they end in different letters.
By the computation at the beginning of the section, if the runs of a DFA for on and on end in the same state, then .
So if and are distinguishable, no DFA for the language can end the runs on them in the same state.
The symbol without a subscript, from the lesson on table filling, is a relation between states of one automaton.
The symbol is a relation between words, and it depends only on the language and not on any automaton.
Question 1. For each pair, decide whether the words are distinguishable with respect to , and if so, write a distinguishing word:
and ; and ; and ; and .
Answer
- : the runs on both end in , and by the computation at the beginning of the section they have no distinguishing word.
- and are distinguishable: gives and .
- : both reach .
- : both are in , and every continuation of them stays in , because the pair has already been found.
The computation at the beginning of the section shows one direction of a connection between the two relations:
words whose runs end in the same state are indistinguishable.
The next claim gives a full connection, also when the states are different.
Claim and proof: equivalent states and indistinguishable words
Claim. Let be a DFA that recognizes , and let .
Then
In particular, if then , because every state is equivalent to itself.
Proof. For every word , by the claim on reading a word in two parts and by the definition of ,
and likewise for . So “no word distinguishes the states and ” (the definition of between states) says exactly “there is no word such that exactly one of is in ” (the definition of ).
Every step is an “if and only if”, so both directions are proved together.
Myhill–Nerode classes
Question 2. Prove that is reflexive, symmetric and transitive.
Hint
For transitivity: if and , what can we say, for every , about and ?
Answer
For every , , so . The condition is symmetric in and , so implies .
If and , then for every we have , so .
As with equivalence of states, the relation splits all the words in into disjoint sets:
in each set every two words are indistinguishable, and words from different sets are distinguishable.
The set of all the words such that is called the class of , and we denote it .
These sets are the Myhill–Nerode classes of , named after John Myhill and Anil Nerode.
In this lesson are words, are letters and are states, so is a set of words and (from the lesson on table filling) is a set of states.
By the claim on equivalent states and indistinguishable words, all the words that reach the same state of the DFA are in the same class.
To know how many classes has, it remains to check whether words that reach different states are distinguishable.
From each state we choose one word that reaches it: , , and .
Question 3. In each cell write a distinguishing word for the word of the row and the word of the column, with respect to .
? | |||
? | ? | ||
? | ? | ? |
Answer: the reason for each cell
and : and . and : and .
and : and . In the three cells of the row , the word distinguishes, because and the three other words are not in .
So has exactly four classes, one for each state of :
where and are the words that have no pair of equal adjacent letters and end in or in .
Question 4. To which class does each of the words , , belong?
Answer
(no pair, ends in ), (it contains ), (no pair, ends in ).
A lower bound on the number of states
Question 3 found four words, every two of which are distinguishable with respect to .
By the computation in the section Indistinguishable words, in every DFA for the runs on two such words end in different states.
This gives a bound on the number of states of every DFA for the language, including a DFA we have not yet seen.
Claim (lower bound). If are words, every two of which are distinguishable with respect to , then every DFA that recognizes has at least states.
Proof
Let be a DFA that recognizes , and suppose, for contradiction, that .
To each of the words corresponds one state, , and there are fewer than states; so there are two words to which the same state corresponds (the pigeonhole principle: if we divide objects among fewer than boxes, some box holds at least two objects).
By the claim on equivalent states and indistinguishable words, implies , contradicting the fact that and are distinguishable.
This is the answer to the question of the lesson. The words are pairwise distinguishable, so every DFA for the language has at least four states, and it has no DFA with three states.
The DFA , which came out of minimizing the product automaton, is the smallest possible.
Question 5. Let , the language of the words that end in .
Find three words, every two of which are distinguishable with respect to .
What follows from this?
Hint
What must be remembered during reading to know whether the word will end in ?
Try words that show each of the states of this memory.
Answer
, and . and : gives and .
and : . and : . So every DFA for has at least three states.
The validator of exercise 2 in Designing a Deterministic Finite Automaton shows that three states suffice.
The smallest automaton
The lower bound uses words every two of which are distinguishable; words from different classes are exactly such words.
So if is recognized by some DFA , the number of its classes is at most the number of states of :
if there were more than classes, we would choose one word from each of classes; every two of the chosen words are distinguishable, and by the lower bound would have at least states, a contradiction.
In particular, the number of classes is finite. In the example of this lesson the number of classes equals exactly the number of states of the smallest DFA.
We will show that this holds for every language that has a DFA, by building a DFA whose states are the classes themselves.
Formulate: let be a language recognized by some DFA. Build a DFA whose states are the Myhill–Nerode classes of :
what is the start state, where does the state go on the letter , and which states accept?
Hint
The class represents “we have read ”. Which class should we reach after reading one more letter ?
In which class are we before reading any letter?
The formal construction
, where is the set of classes of and
is finite, because the number of classes is at most the number of states of the DFA that recognizes .
Think: in the definition of and of we chose one word from the class.
Why does the result not depend on the choice, and why does recognize ?
Proof: the automaton of the classes is well defined and recognizes L
(a) The transition is well defined. Suppose and let be a letter.
For every word , and , and does not distinguish from ; so .
This holds for every , so , that is, .
(b) The accepting states are well defined. If then does not distinguish them, that is, .
So every class is either contained in or disjoint from it.
(c) The run on a word. We prove by induction on the length of :
.
- Base, : by the definition of on the empty word.
- Induction hypothesis: the claim holds for every word of length .
- Step, with and :
The first equality is the definition of on the last letter, the second is the induction hypothesis, and the third is the definition of (well defined by (a)).
Conclusion. if and only if , if and only if (by (c)) , if and only if (by (b)) .
Theorem (the smallest automaton). Let be a language recognized by some DFA, and let be the number of its Myhill–Nerode classes.
Then every DFA that recognizes has at least states, and has exactly states.
That is, the smallest number of states in a DFA for the language is the number of its classes.
Both parts have already been proved: the first is the lower bound, with one word from each class, and the second is the construction of .
Question 6. Build for the language of this lesson (words that contain or ), with the four classes .
Compare with the DFA .
Answer
By : from the letter leads to and the letter to ; from , on and on ; from , on and on ; from every letter leads to .
The only accepting state is .
| State | on | on |
|---|---|---|
If we call by the name , by the name , by the name and by the name , we get exactly the table of .
Uniqueness up to renaming
A DFA and a DFA are equal up to renaming if there is a one-to-one and onto correspondence that takes the start state to the start state, a transition to a transition and an accepting state to an accepting state:
for every state and every letter . This was the case in Question 6, with , and so on.
Theorem (uniqueness). Let be a DFA that recognizes , all of whose states are reachable and which has no two different equivalent states, and let be the number of classes of .
Then and are equal up to renaming, and in particular has exactly states.
Proof
For each state we choose a word such that (one exists, because is reachable), and define .
At every step we use the claim on equivalent states and indistinguishable words.
is well defined. If also , then (the particular case of the claim), so .
is one-to-one. If , with and , then , and by the claim .
has no two different equivalent states, so .
is onto. For every class , .
The start state. , so .
Transitions. If , then by the definition of on the last letter , so .
Accepting states. If , then ; the first because recognizes , and the second by (b) in the proof on the automaton of the classes.
Two conclusions follow from the theorem.
- Minimization always gives the smallest automaton. The full order of minimization returns a DFA all of whose states are reachable (the class is reached by a word that reaches ) and which has no two different equivalent states: by (c) in the proof on the quotient automaton, , and this class accepts exactly when ; so and are equivalent in the quotient automaton only if , that is, only if . By the uniqueness theorem it has states, and it is up to renaming, whichever DFA for we started from.
- Every DFA for with exactly states is up to renaming. In such a DFA all the states are reachable: otherwise, removing the states that are not reachable would give a DFA for with fewer than states, contradicting the lower bound. For the same reason it has no two different equivalent states: otherwise its quotient automaton would recognize with fewer than states. By the uniqueness theorem it is up to renaming.
Question 7. A certain DFA for the language of this lesson has five states, and all of them are reachable.
What can we say about its states?
Answer
It has two different equivalent states. If it did not, then by the uniqueness theorem it would have exactly four states, the number of classes of .
The k-th letter from the end
For we define over the language
that is, the words whose -th letter from the end is .
For example, and are in , and and are not.
Question 8. How many states does the smallest DFA for have?
Predict a number, and then find as many words as you can, every two of which are distinguishable with respect to .
Hint
Try the four words of length 2. What distinguishes from ?
What distinguishes from ?
Answer
The four words are pairwise distinguishable. If two words differ in the first letter, distinguishes them, because the first letter of a word of length 2 is the second letter from the end; for example and .
If they agree in the first letter and differ in the second, distinguishes them, because after adding the second letter of the original word is the second letter from the end; for example and .
So every DFA for has at least four states.
The same argument works for every . Let be two words of length , and let be the first position (from the left) in which they differ; suppose that in this position has and has .
We add : in the word , whose length is , the letter in position is the -th letter from the end, so and .
All words of length are pairwise distinguishable, and by the lower bound every DFA for has at least states.
A DFA for can remember the last letters read: a state for each word of length , and the first letter of the word in the state is the -th letter from the end.
But at the beginning of reading, letters have not yet been read.
A DFA that remembers everything read, as long as fewer than letters have been read, has a state for each word of length , that is,
states. A shorter way is a convention: we start as if letters had already been read before the word.
The states are for each word of length , the start state is , the transition on the letter deletes the first letter and adds at the end ( for every letter and word of length ), and the accepting states are those whose first letter is .
This automaton has states.
The convention does not change the language. After reading the automaton is in the state of the last letters of .
If , these are the last letters of , and the state accepts exactly when .
If , the -th letter from the end of is one of the letters we added, so the state does not accept; and too, because it has no -th letter from the end.
For :
| State | on | on |
|---|---|---|
Transitions of the diagram:
- δ(q_bb, a) = q_ba
- δ(q_bb, b) = q_bb
- δ(q_ba, a) = q_aa
- δ(q_ba, b) = q_ab
- δ(q_aa, a) = q_aa
- δ(q_aa, b) = q_ab
- δ(q_ab, a) = q_ba
- δ(q_ab, b) = q_bb
By the lower bound there is no DFA for with fewer than states, so this automaton is the smallest, and by the second conclusion of the uniqueness theorem it is up to renaming.
In the automaton with states all the states are reachable, and it recognizes the same language with more than states; so by the uniqueness theorem it has two different equivalent states.
Question 9. For : how many states does the smallest DFA for have?
Find a distinguishing word for and , and for and .
Answer
states. and first differ in position 2, so :
(the third letter from the end is ) and .
and first differ in position 2; here has , so gives and .
Minimizing the automaton without the convention
The DFA for that remembers everything read, up to two letters, has seven states, with and accepting:
| State | on | on |
|---|---|---|
Question 10. How many states will remain after minimization, and which states will merge?
Give reasons using the classes of , and check with table filling.
You can also check the answer with the tool “Minimization through words” on the practice page with tools.
Answer
Four states will remain, the number of classes. By the claim on equivalent states and indistinguishable words, two states are equivalent exactly when the words that reach them are in the same class:
(the class , which also contains and ), and (the class ).
and remain alone. This is exactly the convention:
and behave like , and like .
Practice
1. Let , the words in which the number of ‘s is even and the number of ‘s is even.
How many states does the smallest DFA for the language have?
Prove the lower bound, and describe a DFA of this size.
Hint
Which two facts about the word must be remembered? Find a short word for each combination of the two facts.
Answer
Four. The words are pairwise distinguishable: and and :
; : (, ); : (, ); :
(, ). So there are at least four states. The product automaton of the parity counter of and the parity counter of , with “and”, is a DFA with four states for the language.
2. Find the Myhill–Nerode classes of and describe each of them in words.
Compare with exercise 2 in the lesson on minimization by table filling.
Hint
In Question 5 we found three words, every two of which are distinguishable.
Check to which of them each other word is equivalent.
Answer
Three classes: ; , the words that end in ; , the words that end neither in nor in (the empty word, and words that end in ).
Words from two different sets are distinguishable, by Question 5.
Two words from the same set are indistinguishable, because whether belongs to depends only on and on the set of :
if , only on ; if , exactly when ends in ; if , none is in the language; and if , exactly when .
These are the three states , and that merged in the minimization of that exercise.
3. Prove or disprove, for every language and every two words :
(a) If , then if and only if .
(b) If and , then .
Hint
For (b): exercise 3 in the lesson on minimization by table filling has a DFA with two accepting states that are not equivalent.
Answer
(a) True: does not distinguish from , that is, .
(b) False. Let . and , but distinguishes them:
and .
4. The language has a DFA that remembers everything read, up to three letters:
a state for each word of length 0 to 3, that is, 15 states.
How many states will remain after minimization? Which states will merge with , and which with ?
Hint
The class of is determined by the last three letters of , as in the convention.
Answer
Eight, the number of classes. merge with , because for each of the words the last three letters of followed by the word are .
and merge with (all three behave like ).
In the same way and . In total states merge into four, and with the four states we get eight.
5. Let be a DFA that recognizes a language , with states, and suppose there are words, every two of which are distinguishable with respect to .
Prove that every state of is reachable.
Hint
Which states do the runs on the words reach? How many different states are these?
Answer
Let be the words. By the claim on equivalent states and indistinguishable words, if then ; so for the states and are different.
These are different reachable states, and has states in total; so every state is one of them.
Continue with Myhill–Nerode Classes and Minimization: Practice with Tools, go back to Minimizing Automata by Table Filling, or see the glossary.