Exercise

Myhill–Nerode Practice

Complete the automaton of the classes in a tool that checks every transition, and questions to solve on paper: the classes of a finite language and of new languages, the classes of the complement, a bound on the number of classes of an intersection, and a claim to refute.

Last updated:


On this page we practise building the automaton MLM_L from the classes of a language, and using the Myhill–Nerode classes in proofs, following the lesson “Myhill–Nerode: What a State Remembers”.

Completing the automaton of the classes

Choose a language. The tool shows its classes, each named by its shortest word (among several, the first in alphabetical order), and we complete MLM_L⁠:
where each class goes on each letter, and which classes are accepting.
By the construction of MLM_L, the transition from [x][x] on cc is the class of xcxc⁠, so each cell asks which class one word belongs to.
For a wrong answer the tool shows a distinguishing word.

The tool needs JavaScript. Use Questions 1 to 3 on paper.

Questions on paper

Question 1. Let Lfin={a,ab,ba}L_{\mathrm{fin}}=\{a,ab,ba\}⁠. How many classes does LfinL_{\mathrm{fin}} have? Find a representative word for each class, prove that every two of them are distinguishable, and build MLfinM_{L_{\mathrm{fin}}}⁠.

Hint

After aaaa no continuation brings the word into the language.
Which other words behave like that?

Answer

Five classes: [ε]={ε}[\varepsilon]=\{\varepsilon\}⁠, [a]={a}[a]=\{a\}⁠, [b]={b}[b]=\{b\}⁠, [ab]={ab,ba}[ab]=\{ab,ba\} and [aa][aa]⁠, all the other words:
they are not prefixes of aa⁠, abab or baba⁠, so no continuation of them is in the language, and no word distinguishes them.
Distinguishing words: ε\varepsilon distinguishes every word of {a,ab}\{a,ab\} from every word of {ε,b,aa}\{\varepsilon,b,aa\}⁠, because a,ab∈Lfina,ab\in L_{\mathrm{fin}} and ε,b,aa∉Lfin\varepsilon,b,aa\notin L_{\mathrm{fin}}⁠; ε\varepsilon and bb⁠:
z=abz=ab (ab∈Lfinab\in L_{\mathrm{fin}}⁠, bab∉Lfinbab\notin L_{\mathrm{fin}}⁠); ε\varepsilon and aaaa⁠: z=az=a⁠; bb and aaaa⁠:
z=az=a (ba∈Lfinba\in L_{\mathrm{fin}}⁠, aaa∉Lfinaaa\notin L_{\mathrm{fin}}⁠); aa and abab⁠: z=bz=b (ab∈Lfinab\in L_{\mathrm{fin}}⁠, abb∉Lfinabb\notin L_{\mathrm{fin}}⁠).
The words abab and baba are in the same class, because both are in the language and every nonempty continuation takes both out of it.

Stateon aaon bb
→[ε]\to[\varepsilon][a][a][b][b]
∗ [a]{*}\,[a][aa][aa][ab][ab]
[b][b][ab][ab][aa][aa]
∗ [ab]{*}\,[ab][aa][aa][aa][aa]
[aa][aa][aa][aa][aa][aa]

The class [aa][aa] is a rejecting trap state.

Question 2. Let

La={u a∣u∈{a,b}∗}−{u bb v∣u,v∈{a,b}∗}L_{a}=\{u\,a \mid u\in\{a,b\}^*\}-\{u\,bb\,v \mid u,v\in\{a,b\}^*\}

the words that end with aa and contain no bbbb⁠. How many states does the smallest DFA for LaL_{a} have?
Prove the lower bound, and build MLaM_{L_a}⁠.

Hint

What must be remembered: the last letter, and whether bbbb has already appeared.
Is the empty word in the class of another word?

Answer

Four. The words ε,a,b,bb\varepsilon,a,b,bb are pairwise distinguishable: z=εz=\varepsilon distinguishes aa from each of the others; ε\varepsilon and bb⁠:
z=baz=ba (ba∈Laba\in L_a⁠, bba∉Labba\notin L_a⁠); ε\varepsilon and bbbb⁠, and also bb and bbbb⁠:
z=az=a (a,ba∈Laa,ba\in L_a⁠, bba∉Labba\notin L_a⁠). Four states are enough: [ε]={ε}[\varepsilon]=\{\varepsilon\}⁠; [a][a] is the words that end in aa and contain no bbbb⁠; [b][b] is the words that end in bb and contain no bbbb⁠; [bb][bb] is the words that contain bbbb⁠.
The table is a DFA for LaL_a⁠:

Stateon aaon bb
→[ε]\to[\varepsilon][a][a][b][b]
∗ [a]{*}\,[a][a][a][b][b]
[b][b][a][a][bb][bb]
[bb][bb][bb][bb][bb][bb]

Question 3. Let L3={w∈{a,b}∗∣(#a(w)−#b(w)) mod 3=0}L_{3}=\{w\in\{a,b\}^* \mid (\#_a(w)-\#_b(w))\bmod 3=0\}⁠. Find the classes of L3L_3 and ML3M_{L_3}⁠.

Answer

Three classes, by the remainder of #a(w)−#b(w)\#_a(w)-\#_b(w) on division by 3: [ε][\varepsilon] (remainder 0, accepting), [a][a] (remainder 1) and [b][b] (remainder 2).
Words with the same remainder are indistinguishable, because for every zz the remainder of xzxz depends only on the remainder of xx and on the word zz⁠.
Distinguishing words: ε\varepsilon between [ε][\varepsilon] and the other two, and z=az=a between aa and bb (baba is in the language, aaaa is not).
The transitions: aa adds 1 to the remainder and bb subtracts 1, that is [ε]→a[a]→a[b]→a[ε][\varepsilon]\xrightarrow{a}[a]\xrightarrow{a}[b]\xrightarrow{a}[\varepsilon] and [ε]→b[b]→b[a]→b[ε][\varepsilon]\xrightarrow{b}[b]\xrightarrow{b}[a]\xrightarrow{b}[\varepsilon]⁠.

Question 4. Let L⊆Σ∗L\subseteq\Sigma^* be a language and LCL^C its complement.
Prove that for every two words x,yx,y⁠, x≡Lyx\equiv_L y if and only if x≡LCyx\equiv_{L^C}y⁠.
What follows about the numbers of classes of LL and of LCL^C⁠?

Hint

For every word uu⁠, u∈LCu\in L^C if and only if u∉Lu\notin L⁠. What does this say about the statement "xz∈L  ⟺  yz∈Lxz\in L\iff yz\in L"?

Answer

For every zz⁠, xz∈LC  ⟺  xz∉Lxz\in L^C\iff xz\notin L⁠, and likewise for yy⁠. So (xz∈LC  ⟺  yz∈LC)\bigl(xz\in L^C\iff yz\in L^C\bigr) if and only if (xz∉L  ⟺  yz∉L)\bigl(xz\notin L\iff yz\notin L\bigr)⁠, if and only if (xz∈L  ⟺  yz∈L)\bigl(xz\in L\iff yz\in L\bigr)⁠, because negating both sides of an “if and only if” does not change its truth value.
The condition holds for every zz for LL exactly when it holds for every zz for LCL^C⁠, so the two relations are the same, and the two languages have the same classes.
In particular, for a language that has a DFA, the smallest DFAs for LL and for LCL^C have the same number of states; this agrees with the complement automaton, which changes only the accepting states.

Question 5. Suppose the language L1L_1 has a DFA and mm classes, and the language L2L_2 has a DFA and nn classes.
Prove that L1∩L2L_1\cap L_2 has at most m⋅nm\cdot n classes. Give an example in which exactly m⋅nm\cdot n is reached.

Hint

What are the states of the product automaton of ML1M_{L_1} and ML2M_{L_2}⁠?
What does the lower bound say about any DFA for the language?

Answer

By the smallest-automaton theorem, L1L_1 has a DFA ML1M_{L_1} with mm states and L2L_2 has a DFA ML2M_{L_2} with nn states.
Their product automaton, with the accepting states of “and”, recognizes L1∩L2L_1\cap L_2 and has m⋅nm\cdot n states.
By the lower bound, if L1∩L2L_1\cap L_2 had more than m⋅nm\cdot n classes, one word from each class would give more than m⋅nm\cdot n pairwise distinguishable words, and the product automaton would need more than m⋅nm\cdot n states; a contradiction.
Example: La,2={w∈{a,b}∗∣#a(w) mod 2=0}L_{a,2}=\{w\in\{a,b\}^*\mid \#_a(w)\bmod 2=0\} and Lb,2={w∈{a,b}∗∣#b(w) mod 2=0}L_{b,2}=\{w\in\{a,b\}^*\mid \#_b(w)\bmod 2=0\}⁠, “the number of aa is even” and “the number of bb is even”, two classes each.
The intersection La,2∩Lb,2L_{a,2}\cap L_{b,2} is LevenL_{\mathrm{even}} from the lesson, with four classes.

Question 6. Prove or refute: if L1⊆L2L_1\subseteq L_2 and both have a DFA, then L1L_1 has at most as many classes as L2L_2⁠.

Answer

The claim is false; a counterexample: {a}⊆{a,b}∗\{a\}\subseteq\{a,b\}^*⁠. The language {a,b}∗\{a,b\}^* has one class, because every word and every continuation of it is in the language.
The language {a}\{a\} has three classes: [ε][\varepsilon]⁠, [a][a] and [b][b] (all the other words, which are not prefixes of aa⁠, so no continuation of them is in the language); ε\varepsilon distinguishes aa from the other two, and z=az=a distinguishes ε\varepsilon from bb⁠.

Continue with The Lower Bound in Action, go back to Myhill–Nerode: What a State Remembers, or see the glossary.