Lesson

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

L={u aa v∣u,v∈{a,b}∗}∪{u bb v∣u,v∈{a,b}∗}L=\{u\,aa\,v \mid u,v\in\{a,b\}^*\}\cup\{u\,bb\,v \mid u,v\in\{a,b\}^*\}

that is, the words that contain aaaa or contain bbbb⁠. Minimizing its product automaton gave a DFA with four states, which can also be designed directly; we call it RR⁠.
Its states: r0r_0 (start, no letter read), rar_a and rbr_b (the last letter, and no pair of equal adjacent letters found yet), and rpairr_{\mathrm{pair}} (a pair found; an accepting trap state).

Stateon aaon bb
→r0\to r_0rar_arbr_b
rar_arpairr_{\mathrm{pair}}rbr_b
rbr_brar_arpairr_{\mathrm{pair}}
∗ rpair{*}\,r_{\mathrm{pair}}rpairr_{\mathrm{pair}}rpairr_{\mathrm{pair}}
r0rarbrpairabababa,b
The DFA with four states for the language LL⁠. The states rar_a and rbr_b remember the last letter, and rpairr_{\mathrm{pair}} remembers that a pair has already been found.

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 ε\varepsilon reaches r0r_0⁠: every letter takes the run out of r0r_0⁠, and no arrow returns to it.
  • The words that have no pair of equal adjacent letters and end in aa reach rar_a⁠: aa⁠, baba⁠, abaaba⁠.
  • The words that have no pair and end in bb reach rbr_b⁠: bb⁠, abab⁠, babbab⁠.
  • The words of LL reach rpairr_{\mathrm{pair}}⁠: aaaa⁠, bbbb⁠, abbaabba⁠.

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 LL with fewer than four states?

Indistinguishable words

Suppose a DFA M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) (here ss is the start state) recognizes a language LL⁠, and the runs on two words xx and yy end in the same state qq⁠.
For every word zz⁠, by the claim on reading a word in two parts,

δ∗(s,xz)=δ∗(δ∗(s,x),z)=δ∗(q,z)=δ∗(δ∗(s,y),z)=δ∗(s,yz)\delta^*(s,xz)=\delta^*\bigl(\delta^*(s,x),z\bigr)=\delta^*(q,z)=\delta^*\bigl(\delta^*(s,y),z\bigr)=\delta^*(s,yz)

so the runs on xzxz and on yzyz end in the same state, for every continuation zz⁠.

Formulate: what must two words xx and yy satisfy, without mentioning an automaton, so that a DFA for the language LL “does not need to tell them apart”?
Write a condition using LL only.

Hint

Candidate definitions: (1) both words are in LL or both are not in LL⁠; (2) both words end in the same letter; (3) for every word zz⁠, xz∈Lxz\in L if and only if yz∈Lyz\in L⁠.
Check each of them on x=ax=a and y=by=b⁠, and on x=ax=a and y=bbay=bba⁠.

The formal definition

Let L⊆Σ∗L\subseteq\Sigma^* be a language, and let x,y∈Σ∗x,y\in\Sigma^*⁠. A word z∈Σ∗z\in\Sigma^* distinguishes the words xx and yy with respect to LL if exactly one of the words xzxz and yzyz belongs to LL⁠.
If they have no distinguishing word, they are indistinguishable words with respect to LL⁠, and we write x≡Lyx\equiv_L y⁠:

x≡Ly  ⟺  ∀z∈Σ∗: (xz∈L  ⟺  yz∈L)x\equiv_L y \iff \forall z\in\Sigma^*:\ \bigl(xz\in L \iff yz\in L\bigr)

Otherwise xx and yy are distinguishable. Candidate (1) is only the case z=εz=\varepsilon⁠:
the words aa and bb are both not in LL⁠, but z=az=a distinguishes them, because aa∈Laa\in L and ba∉Lba\notin L⁠.
Candidate (2) is wrong for this language too, in both directions:
aa and bbabba end in the same letter, but z=εz=\varepsilon distinguishes them; and aa≡Lbbaa\equiv_L bb although they end in different letters.

By the computation at the beginning of the section, if the runs of a DFA for LL on xx and on yy end in the same state, then x≡Lyx\equiv_L y⁠.
So if xx and yy are distinguishable, no DFA for the language can end the runs on them in the same state.

The symbol ≡\equiv without a subscript, from the lesson on table filling, is a relation between states of one automaton.
The symbol ≡L\equiv_L 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 LL⁠, and if so, write a distinguishing word:
aa and baba⁠; ε\varepsilon and aa⁠; abab and bb⁠; aaaa and bbbb⁠.

Answer
  • a≡Lbaa\equiv_L ba⁠: the runs on both end in rar_a⁠, and by the computation at the beginning of the section they have no distinguishing word.
  • ε\varepsilon and aa are distinguishable: z=az=a gives a∉La\notin L and aa∈Laa\in L⁠.
  • ab≡Lbab\equiv_L b⁠: both reach rbr_b⁠.
  • aa≡Lbbaa\equiv_L bb⁠: both are in LL⁠, and every continuation of them stays in LL⁠, 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 M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) be a DFA that recognizes LL⁠, and let x,y∈Σ∗x,y\in\Sigma^*⁠.
Then

δ∗(s,x)≡δ∗(s,y)  ⟺  x≡Ly\delta^*(s,x)\equiv\delta^*(s,y) \iff x\equiv_L y

In particular, if δ∗(s,x)=δ∗(s,y)\delta^*(s,x)=\delta^*(s,y) then x≡Lyx\equiv_L y⁠, because every state is equivalent to itself.

Proof. For every word zz⁠, by the claim on reading a word in two parts and by the definition of L(M)L(M)⁠,

δ∗(δ∗(s,x),z)∈F  ⟺  δ∗(s,xz)∈F  ⟺  xz∈L\delta^*\bigl(\delta^*(s,x),z\bigr)\in F \iff \delta^*(s,xz)\in F \iff xz\in L

and likewise for yy⁠. So “no word zz distinguishes the states δ∗(s,x)\delta^*(s,x) and δ∗(s,y)\delta^*(s,y)” (the definition of ≡\equiv between states) says exactly “there is no word zz such that exactly one of xz,yzxz,yz is in LL” (the definition of ≡L\equiv_L⁠).
Every step is an “if and only if”, so both directions are proved together.

Myhill–Nerode classes

Question 2. Prove that ≡L\equiv_L is reflexive, symmetric and transitive.

Hint

For transitivity: if x≡Lyx\equiv_L y and y≡Lty\equiv_L t⁠, what can we say, for every zz⁠, about xz∈Lxz\in L and tz∈Ltz\in L⁠?

Answer

For every zz⁠, xz∈L  ⟺  xz∈Lxz\in L\iff xz\in L⁠, so x≡Lxx\equiv_L x⁠. The condition xz∈L  ⟺  yz∈Lxz\in L\iff yz\in L is symmetric in xx and yy⁠, so x≡Lyx\equiv_L y implies y≡Lxy\equiv_L x⁠.
If x≡Lyx\equiv_L y and y≡Lty\equiv_L t⁠, then for every zz we have xz∈L  ⟺  yz∈L  ⟺  tz∈Lxz\in L\iff yz\in L\iff tz\in L⁠, so x≡Ltx\equiv_L t⁠.

As with equivalence of states, the relation splits all the words in Σ∗\Sigma^* into disjoint sets:
in each set every two words are indistinguishable, and words from different sets are distinguishable.
The set of all the words yy such that x≡Lyx\equiv_L y is called the class of xx⁠, and we denote it [x][x]⁠.
These sets are the Myhill–Nerode classes of LL⁠, named after John Myhill and Anil Nerode.
In this lesson x,y,z,t,u,v,wx,y,z,t,u,v,w are words, c,dc,d are letters and p,qp,q are states, so [x][x] is a set of words and [q][q] (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 RR are in the same class.
To know how many classes LL has, it remains to check whether words that reach different states are distinguishable.
From each state we choose one word that reaches it: ε\varepsilon⁠, aa⁠, bb and aaaa⁠.

Question 3. In each cell write a distinguishing word for the word of the row and the word of the column, with respect to LL⁠.

ε\varepsilonaabb
aa
?aa
bb
?bb
?aa
aaaa
?ε\varepsilon
?ε\varepsilon
?ε\varepsilon
Answer: the reason for each cell

ε\varepsilon and aa⁠: aa∈Laa\in L and a∉La\notin L⁠. ε\varepsilon and bb⁠: bb∈Lbb\in L and b∉Lb\notin L⁠.
aa and bb⁠: aa∈Laa\in L and ba∉Lba\notin L⁠. In the three cells of the row aaaa⁠, the word ε\varepsilon distinguishes, because aa∈Laa\in L and the three other words are not in LL⁠.
So LL has exactly four classes, one for each state of RR⁠:

[ε]={ε},[a],[b],[aa]=L[\varepsilon]=\{\varepsilon\},\quad [a],\quad [b],\quad [aa]=L

where [a][a] and [b][b] are the words that have no pair of equal adjacent letters and end in aa or in bb⁠.

Question 4. To which class does each of the words abababab⁠, abbaabba⁠, babababababa belong?

Answer

abab∈[b]abab\in[b] (no pair, ends in bb⁠), abba∈[aa]abba\in[aa] (it contains bbbb⁠), bababa∈[a]bababa\in[a] (no pair, ends in aa⁠).

A lower bound on the number of states

Question 3 found four words, every two of which are distinguishable with respect to LL⁠.
By the computation in the section Indistinguishable words, in every DFA for LL 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 x1,…,xnx_1,\dots,x_n are nn words, every two of which are distinguishable with respect to LL⁠, then every DFA that recognizes LL has at least nn states.

Proof

Let M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) be a DFA that recognizes LL⁠, and suppose, for contradiction, that ∣Q∣<n|Q|<n⁠.
To each of the nn words corresponds one state, δ∗(s,xi)\delta^*(s,x_i)⁠, and there are fewer than nn states; so there are two words xi≠xjx_i\neq x_j to which the same state corresponds (the pigeonhole principle: if we divide nn objects among fewer than nn boxes, some box holds at least two objects).
By the claim on equivalent states and indistinguishable words, δ∗(s,xi)=δ∗(s,xj)\delta^*(s,x_i)=\delta^*(s,x_j) implies xi≡Lxjx_i\equiv_L x_j⁠, contradicting the fact that xix_i and xjx_j are distinguishable.

This is the answer to the question of the lesson. The words ε,a,b,aa\varepsilon,a,b,aa are pairwise distinguishable, so every DFA for the language LL has at least four states, and it has no DFA with three states.
The DFA RR⁠, which came out of minimizing the product automaton, is the smallest possible.

Question 5. Let Lab={u ab∣u∈{a,b}∗}L_{ab}=\{u\,ab \mid u\in\{a,b\}^*\}⁠, the language of the words that end in abab⁠.
Find three words, every two of which are distinguishable with respect to LabL_{ab}⁠.
What follows from this?

Hint

What must be remembered during reading to know whether the word will end in abab⁠?
Try words that show each of the states of this memory.

Answer

ε\varepsilon⁠, aa and abab⁠. ε\varepsilon and aa⁠: z=bz=b gives b∉Labb\notin L_{ab} and ab∈Labab\in L_{ab}⁠.
ε\varepsilon and abab⁠: z=εz=\varepsilon⁠. aa and abab⁠: z=εz=\varepsilon⁠. So every DFA for LabL_{ab} has at least three states.
The validator q0,qa,qabq_0,q_a,q_{ab} 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 LL is recognized by some DFA MM⁠, the number of its classes is at most the number of states of MM⁠:
if there were more than ∣Q∣|Q| classes, we would choose one word from each of ∣Q∣+1|Q|+1 classes; every two of the chosen words are distinguishable, and by the lower bound MM would have at least ∣Q∣+1|Q|+1 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 LL be a language recognized by some DFA. Build a DFA MLM_L whose states are the Myhill–Nerode classes of LL⁠:
what is the start state, where does the state [x][x] go on the letter cc⁠, and which states accept?

Hint

The class [x][x] represents “we have read xx”. Which class should we reach after reading one more letter cc⁠?
In which class are we before reading any letter?

The formal construction

ML=(QL,Σ,δL,sL,FL)M_L=(Q_L,\Sigma,\delta_L,s_L,F_L)⁠, where QLQ_L is the set of classes of ≡L\equiv_L and

sL=[ε],δL([x],c)=[xc],FL={[x]∣x∈L}s_L=[\varepsilon],\qquad \delta_L\bigl([x],c\bigr)=[xc],\qquad F_L=\bigl\{[x] \mid x\in L\bigr\}

QLQ_L is finite, because the number of classes is at most the number of states of the DFA that recognizes LL⁠.

Think: in the definition of δL\delta_L and of FLF_L we chose one word xx from the class.
Why does the result not depend on the choice, and why does MLM_L recognize LL⁠?

Proof: the automaton of the classes is well defined and recognizes L

(a) The transition is well defined. Suppose x≡Lyx\equiv_L y and let cc be a letter.
For every word zz⁠, (xc)z=x(cz)(xc)z=x(cz) and (yc)z=y(cz)(yc)z=y(cz)⁠, and czcz does not distinguish xx from yy⁠; so (xc)z∈L  ⟺  (yc)z∈L(xc)z\in L\iff(yc)z\in L⁠.
This holds for every zz⁠, so xc≡Lycxc\equiv_L yc⁠, that is, [xc]=[yc][xc]=[yc]⁠.

(b) The accepting states are well defined. If x≡Lyx\equiv_L y then z=εz=\varepsilon does not distinguish them, that is, x∈L  ⟺  y∈Lx\in L\iff y\in L⁠.
So every class is either contained in LL or disjoint from it.

(c) The run on a word. We prove by induction on the length of ww⁠:
δL∗([ε],w)=[w]\delta_L^*([\varepsilon],w)=[w]⁠.

  • Base, w=εw=\varepsilon⁠: δL∗([ε],ε)=[ε]\delta_L^*([\varepsilon],\varepsilon)=[\varepsilon] by the definition of δ∗\delta^* on the empty word.
  • Induction hypothesis: the claim holds for every word of length nn⁠.
  • Step, w=ucw=uc with ∣u∣=n|u|=n and c∈Σc\in\Sigma⁠:
δL∗([ε],uc)=δL(δL∗([ε],u),c)=δL([u],c)=[uc]\delta_L^*([\varepsilon],uc)=\delta_L\bigl(\delta_L^*([\varepsilon],u),c\bigr)=\delta_L\bigl([u],c\bigr)=[uc]

The first equality is the definition of δ∗\delta^* on the last letter, the second is the induction hypothesis, and the third is the definition of δL\delta_L (well defined by (a)).

Conclusion. w∈L(ML)w\in L(M_L) if and only if δL∗([ε],w)∈FL\delta_L^*([\varepsilon],w)\in F_L⁠, if and only if (by (c)) [w]∈FL[w]\in F_L⁠, if and only if (by (b)) w∈Lw\in L⁠.

Theorem (the smallest automaton). Let LL be a language recognized by some DFA, and let nn be the number of its Myhill–Nerode classes.
Then every DFA that recognizes LL has at least nn states, and MLM_L has exactly nn 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 MLM_L⁠.

Question 6. Build MLM_L for the language LL of this lesson (words that contain aaaa or bbbb⁠), with the four classes [ε],[a],[b],[aa][\varepsilon],[a],[b],[aa]⁠.
Compare with the DFA RR⁠.

Answer

By δL([x],c)=[xc]\delta_L([x],c)=[xc]⁠: from [ε][\varepsilon] the letter aa leads to [a][a] and the letter bb to [b][b]⁠; from [a][a]⁠, [aa][aa] on aa and [ab]=[b][ab]=[b] on bb⁠; from [b][b]⁠, [ba]=[a][ba]=[a] on aa and [bb]=[aa][bb]=[aa] on bb⁠; from [aa][aa] every letter leads to [aa][aa]⁠.
The only accepting state is [aa]=L[aa]=L⁠.

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

If we call [ε][\varepsilon] by the name r0r_0⁠, [a][a] by the name rar_a⁠, [b][b] by the name rbr_b and [aa][aa] by the name rpairr_{\mathrm{pair}}⁠, we get exactly the table of RR⁠.

Uniqueness up to renaming

A DFA M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) and a DFA M′=(Q′,Σ,δ′,s′,F′)M'=(Q',\Sigma,\delta',s',F') are equal up to renaming if there is a one-to-one and onto correspondence φ:Q→Q′\varphi:Q\to Q' that takes the start state to the start state, a transition to a transition and an accepting state to an accepting state:

φ(s)=s′,φ(δ(q,c))=δ′(φ(q),c),q∈F  ⟺  φ(q)∈F′\varphi(s)=s',\qquad \varphi\bigl(\delta(q,c)\bigr)=\delta'\bigl(\varphi(q),c\bigr),\qquad q\in F\iff\varphi(q)\in F'

for every state qq and every letter cc⁠. This was the case in Question 6, with φ(r0)=[ε]\varphi(r_0)=[\varepsilon]⁠, φ(ra)=[a]\varphi(r_a)=[a] and so on.

Theorem (uniqueness). Let M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) be a DFA that recognizes LL⁠, all of whose states are reachable and which has no two different equivalent states, and let nn be the number of classes of LL⁠.
Then MM and MLM_L are equal up to renaming, and in particular MM has exactly nn states.

Proof

For each state qq we choose a word xx such that δ∗(s,x)=q\delta^*(s,x)=q (one exists, because qq is reachable), and define φ(q)=[x]\varphi(q)=[x]⁠.
At every step we use the claim on equivalent states and indistinguishable words.

φ\varphi is well defined. If also δ∗(s,y)=q\delta^*(s,y)=q⁠, then x≡Lyx\equiv_L y (the particular case of the claim), so [x]=[y][x]=[y]⁠.

φ\varphi is one-to-one. If φ(p)=φ(q)\varphi(p)=\varphi(q)⁠, with δ∗(s,x)=p\delta^*(s,x)=p and δ∗(s,y)=q\delta^*(s,y)=q⁠, then x≡Lyx\equiv_L y⁠, and by the claim p≡qp\equiv q⁠.
MM has no two different equivalent states, so p=qp=q⁠.

φ\varphi is onto. For every class [x][x]⁠, φ(δ∗(s,x))=[x]\varphi\bigl(\delta^*(s,x)\bigr)=[x]⁠.

The start state. δ∗(s,ε)=s\delta^*(s,\varepsilon)=s⁠, so φ(s)=[ε]=sL\varphi(s)=[\varepsilon]=s_L⁠.

Transitions. If δ∗(s,x)=q\delta^*(s,x)=q⁠, then by the definition of δ∗\delta^* on the last letter δ∗(s,xc)=δ(q,c)\delta^*(s,xc)=\delta(q,c)⁠, so φ(δ(q,c))=[xc]=δL([x],c)=δL(φ(q),c)\varphi\bigl(\delta(q,c)\bigr)=[xc]=\delta_L\bigl([x],c\bigr)=\delta_L\bigl(\varphi(q),c\bigr)⁠.

Accepting states. If δ∗(s,x)=q\delta^*(s,x)=q⁠, then q∈F  ⟺  x∈L  ⟺  [x]∈FLq\in F\iff x\in L\iff[x]\in F_L⁠; the first because MM recognizes LL⁠, 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 [q][q] is reached by a word that reaches qq⁠) and which has no two different equivalent states: by (c) in the proof on the quotient automaton, δ′∗([p],w)=[δ∗(p,w)]\delta'^*([p],w)=[\delta^*(p,w)]⁠, and this class accepts exactly when δ∗(p,w)∈F\delta^*(p,w)\in F⁠; so [p][p] and [q][q] are equivalent in the quotient automaton only if p≡qp\equiv q⁠, that is, only if [p]=[q][p]=[q]⁠. By the uniqueness theorem it has nn states, and it is MLM_L up to renaming, whichever DFA for LL we started from.
  • Every DFA for LL with exactly nn states is MLM_L 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 LL with fewer than nn states, contradicting the lower bound. For the same reason it has no two different equivalent states: otherwise its quotient automaton would recognize LL with fewer than nn states. By the uniqueness theorem it is MLM_L up to renaming.

Question 7. A certain DFA for the language LL 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 LL⁠.

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 8. 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 cc deletes the first letter and adds cc at the end (δ(qdv′,c)=qv′c\delta(q_{dv'},c)=q_{v'c} for every letter dd 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 9. 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 10. 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⁠.

Practice

1. Let Leven={w∈{a,b}∗∣#a(w) mod 2=0 ∧ #b(w) mod 2=0}L_{\mathrm{even}}=\{w\in\{a,b\}^* \mid \#_a(w)\bmod 2=0\ \wedge\ \#_b(w)\bmod 2=0\}⁠, the words in which the number of aa‘s is even and the number of bb‘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 ε,a,b,ab\varepsilon,a,b,ab are pairwise distinguishable: ε,a\varepsilon,a and ε,b\varepsilon,b and ε,ab\varepsilon,ab⁠:
z=εz=\varepsilon⁠; a,ba,b⁠: z=az=a (aa∈Levenaa\in L_{\mathrm{even}}⁠, ba∉Levenba\notin L_{\mathrm{even}}⁠); a,aba,ab⁠: z=az=a (aa∈Levenaa\in L_{\mathrm{even}}⁠, aba∉Levenaba\notin L_{\mathrm{even}}⁠); b,abb,ab⁠:
z=bz=b (bb∈Levenbb\in L_{\mathrm{even}}⁠, abb∉Levenabb\notin L_{\mathrm{even}}⁠). So there are at least four states. The product automaton of the parity counter of aa and the parity counter of bb⁠, with “and”, is a DFA with four states for the language.

2. Find the Myhill–Nerode classes of Lab={u ab∣u∈{a,b}∗}L_{ab}=\{u\,ab \mid u\in\{a,b\}^*\} 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: [ab]=Lab[ab]=L_{ab}⁠; [a][a]⁠, the words that end in aa⁠; [ε][\varepsilon]⁠, the words that end neither in aa nor in abab (the empty word, bb and words that end in bbbb⁠).
Words from two different sets are distinguishable, by Question 5.
Two words from the same set are indistinguishable, because whether xzxz belongs to LabL_{ab} depends only on zz and on the set of xx⁠:
if ∣z∣≥2|z|\geq2⁠, only on zz⁠; if z=bz=b⁠, xb∈Labxb\in L_{ab} exactly when xx ends in aa⁠; if z=az=a⁠, none is in the language; and if z=εz=\varepsilon⁠, x∈Labx\in L_{ab} exactly when x∈[ab]x\in[ab]⁠.
These are the three states {q0,qb,qbb}\{q_0,q_b,q_{bb}\}⁠, {qa,qaa,qba}\{q_a,q_{aa},q_{ba}\} and {qab}\{q_{ab}\} that merged in the minimization of that exercise.

3. Prove or disprove, for every language LL and every two words x,yx,y⁠:

(a) If x≡Lyx\equiv_L y⁠, then x∈Lx\in L if and only if y∈Ly\in L⁠.

(b) If x∈Lx\in L and y∈Ly\in L⁠, then x≡Lyx\equiv_L y⁠.

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: z=εz=\varepsilon does not distinguish xx from yy⁠, that is, xε∈L  ⟺  yε∈Lx\varepsilon\in L\iff y\varepsilon\in L⁠.

(b) False. Let L={w∈{a,b}∗∣#a(w) mod 3≠0}L=\{w\in\{a,b\}^* \mid \#_a(w)\bmod 3\neq0\}⁠. a∈La\in L and aa∈Laa\in L⁠, but z=az=a distinguishes them:
aa∈Laa\in L and aaa∉Laaa\notin L⁠.

4. The language L3L_3 has a DFA that remembers everything read, up to three letters:
a state qvq_v for each word vv of length 0 to 3, that is, 15 states.
How many states will remain after minimization? Which states will merge with qbbbq_{bbb}⁠, and which with qbaq_{ba}⁠?

Hint

The class of vv is determined by the last three letters of bbb vbbb\,v⁠, as in the convention.

Answer

Eight, the number of classes. qε,qb,qbbq_\varepsilon,q_b,q_{bb} merge with qbbbq_{bbb}⁠, because for each of the words ε,b,bb\varepsilon,b,bb the last three letters of bbbbbb followed by the word are bbbbbb⁠.
qaq_a and qbbaq_{bba} merge with qbaq_{ba} (all three behave like bbabba⁠).
In the same way qaa≡qbaaq_{aa}\equiv q_{baa} and qab≡qbabq_{ab}\equiv q_{bab}⁠. In total 4+3+2+2=114+3+2+2=11 states merge into four, and with the four states qaaa,qaab,qaba,qabbq_{aaa},q_{aab},q_{aba},q_{abb} we get eight.

5. Let MM be a DFA that recognizes a language LL⁠, with nn states, and suppose there are nn words, every two of which are distinguishable with respect to LL⁠.
Prove that every state of MM is reachable.

Hint

Which states do the runs on the nn words reach? How many different states are these?

Answer

Let x1,…,xnx_1,\dots,x_n be the words. By the claim on equivalent states and indistinguishable words, if δ∗(s,xi)=δ∗(s,xj)\delta^*(s,x_i)=\delta^*(s,x_j) then xi≡Lxjx_i\equiv_L x_j⁠; so for i≠ji\neq j the states δ∗(s,xi)\delta^*(s,x_i) and δ∗(s,xj)\delta^*(s,x_j) are different.
These are nn different reachable states, and MM has nn 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.