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 “Minimization: Merging Equivalent States”.

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 σ\sigma is the class of xσx\sigma⁠, 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⁠.

The k-th letter from the end

For k≥1k\geq1 we define over {a,b}\{a,b\} the language

Lk={u a v∣u,v∈{a,b}∗, ∣v∣=k−1}L_k=\bigl\{u\,a\,v \mid u,v\in\{a,b\}^*,\ |v|=k-1\bigr\}

that is, the words whose kk-th letter from the end is aa⁠.
For example, abab and baabaa are in L2L_2⁠, and aa and abbabb are not.

Question 7. How many states does the smallest DFA for L2L_2 have?
Predict a number, and then find as many words as you can, every two of which are distinguishable with respect to L2L_2⁠.

Hint

Try the four words of length 2. What distinguishes abab from bbbb⁠?
What distinguishes baba from bbbb⁠?

Answer

The four words aa,ab,ba,bbaa,ab,ba,bb are pairwise distinguishable. If two words differ in the first letter, z=εz=\varepsilon distinguishes them, because the first letter of a word of length 2 is the second letter from the end; for example ab∈L2ab\in L_2 and bb∉L2bb\notin L_2⁠.
If they agree in the first letter and differ in the second, z=bz=b distinguishes them, because after adding bb the second letter of the original word is the second letter from the end; for example bab∈L2bab\in L_2 and bbb∉L2bbb\notin L_2⁠.
So every DFA for L2L_2 has at least four states.

The same argument works for every kk⁠. Let x≠yx\neq y be two words of length kk⁠, and let ii be the first position (from the left) in which they differ; suppose that in this position xx has aa and yy has bb⁠.
We add z=bi−1z=b^{i-1}⁠: in the word xzxz⁠, whose length is k+i−1k+i-1⁠, the letter in position ii is the kk-th letter from the end, so xz∈Lkxz\in L_k and yz∉Lkyz\notin L_k⁠.
All 2k2^k words of length kk are pairwise distinguishable, and by the lower bound every DFA for LkL_k has at least 2k2^k states.

A DFA for LkL_k can remember the last kk letters read: a state for each word of length kk⁠, and the first letter of the word in the state is the kk-th letter from the end.
But at the beginning of reading, kk letters have not yet been read.
A DFA that remembers everything read, as long as fewer than kk letters have been read, has a state for each word of length 0,1,…,k0,1,\dots,k⁠, that is,

∑j=0k2j=1+2+4+⋯+2k=2k+1−1\sum_{j=0}^{k}2^j=1+2+4+\cdots+2^k=2^{k+1}-1

states. A shorter way is a convention: we start as if kk letters bb had already been read before the word.
The states are qvq_v for each word vv of length kk⁠, the start state is qbkq_{b^k}⁠, the transition on the letter σ\sigma deletes the first letter and adds σ\sigma at the end (δ(qτv′,σ)=qv′σ\delta(q_{\tau v'},\sigma)=q_{v'\sigma} for every letter τ\tau and word v′v' of length k−1k-1⁠), and the accepting states are those whose first letter is aa⁠.
This automaton has 2k2^k states.

The convention does not change the language. After reading ww the automaton is in the state of the last kk letters of bkwb^kw⁠.
If ∣w∣≥k|w|\geq k⁠, these are the last kk letters of ww⁠, and the state accepts exactly when w∈Lkw\in L_k⁠.
If ∣w∣<k|w|<k⁠, the kk-th letter from the end of bkwb^kw is one of the letters bb we added, so the state does not accept; and w∉Lkw\notin L_k too, because it has no kk-th letter from the end.

For k=2k=2⁠:

Stateon aaon bb
→qbb\to q_{bb}qbaq_{ba}qbbq_{bb}
qbaq_{ba}qaaq_{aa}qabq_{ab}
∗ qaa{*}\,q_{aa}qaaq_{aa}qabq_{ab}
∗ qab{*}\,q_{ab}qbaq_{ba}qbbq_{bb}
qbbqbaqaaqabbaababab
A DFA for L2L_2 that remembers the last two letters, with start state qbbq_{bb} by the convention. The accepting states are those whose name starts with aa⁠.

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 LkL_k with fewer than 2k2^k states, so this automaton is the smallest, and by the second conclusion of the uniqueness theorem it is MLkM_{L_k} up to renaming.
In the automaton with 2k+1−12^{k+1}-1 states all the states are reachable, and it recognizes the same language with more than 2k2^k states; so by the uniqueness theorem it has two different equivalent states.

Question 8. For k=3k=3⁠: how many states does the smallest DFA for L3L_3 have?
Find a distinguishing word for aabaab and abbabb⁠, and for babbab and bbabba⁠.

Answer

23=82^3=8 states. aabaab and abbabb first differ in position 2, so z=bz=b⁠:
aabb∈L3aabb\in L_3 (the third letter from the end is aa⁠) and abbb∉L3abbb\notin L_3⁠.
babbab and bbabba first differ in position 2; here babbab has aa⁠, so z=bz=b gives babb∈L3babb\in L_3 and bbab∉L3bbab\notin L_3⁠.

Minimizing the automaton without the convention

The DFA for L2L_2 that remembers everything read, up to two letters, has seven states, with qaaq_{aa} and qabq_{ab} accepting:

Stateon aaon bb
→qε\to q_\varepsilonqaq_aqbq_b
qaq_aqaaq_{aa}qabq_{ab}
qbq_bqbaq_{ba}qbbq_{bb}
∗ qaa{*}\,q_{aa}qaaq_{aa}qabq_{ab}
∗ qab{*}\,q_{ab}qbaq_{ba}qbbq_{bb}
qbaq_{ba}qaaq_{aa}qabq_{ab}
qbbq_{bb}qbaq_{ba}qbbq_{bb}

Question 9. How many states will remain after minimization, and which states will merge?
Give reasons using the classes of L2L_2⁠, 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:
qε≡qb≡qbbq_\varepsilon\equiv q_b\equiv q_{bb} (the class [bb][bb]⁠, which also contains ε\varepsilon and bb⁠), and qa≡qbaq_a\equiv q_{ba} (the class [ba][ba]⁠).
qaaq_{aa} and qabq_{ab} remain alone. This is exactly the convention:
ε\varepsilon and bb behave like bbbb⁠, and aa like baba⁠.

Continue with The Lower Bound in Action, go back to Minimization: Merging Equivalent States, or see the glossary.