Lesson

Minimization: Merging Equivalent States

Two correct automata for the same language, one larger than the other: distinguishing words and equivalent states, table filling in rounds, and the quotient automaton that recognizes the same language.

Last updated:


In this lesson we learn to make a deterministic finite automaton (DFA) smaller without changing its language by merging states:
to decide when two states can be merged, using a distinguishing word; to fill a table of pairs in rounds that finds all the equivalent states; and to build from them the quotient automaton and prove that it recognizes the same language.

The language

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\}^*\}

of the words that contain aaaa or contain bbbb has two validators.
One is the product automaton of the validator AA for “contains aaaa” (the states q0,qa,qaaq_0,q_a,q_{aa}⁠) and the validator BB for “contains bbbb” (the states p0,pb,pbbp_0,p_b,p_{bb}⁠), with 9 states.
The other was designed directly, with 4 states: r0r_0 (start), rar_a and rbr_b (the last letter, and no two equal adjacent letters found yet) and rpairr_{\mathrm{pair}} (such a pair found).
The two automata are equivalent, that is, they recognize the same language, and we write M1≡M2M_1\equiv M_2 when L(M1)=L(M2)L(M_1)=L(M_2)⁠.

The transition table of the validator designed directly; the arrow marks the start state and the star the accepting 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}}

In a DFA M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F)⁠, where ss is the start state, a state qq is reachable if there is a word ww such that δ∗(s,w)=q\delta^*(s,w)=q⁠.
In the product the state (qa,pb)(q_a,p_b) is not reachable, and after removing it 8 states remain, twice as many as in the validator designed directly.
The construction of the two validators and the removal of the states that are not reachable are in Minimization: Removing Unreachable States.

Figure: the product automaton
(q0,p0)(q0,pb)(q0,pbb)(qa,p0)(qa,pb)(qa,pbb)(qaa,p0)(qaa,pb)(qaa,pbb)ababababababababa,b
The product automaton: the row is the state of AA and the column the state of BB⁠. The letter aa advances the first coordinate (one row down) and returns the second to p0p_0⁠, except from the trap state pbbp_{bb}⁠; the letter bb acts in the same way on the columns. The five states in the bottom row and the right column accept.

Transitions of the diagram:

  • δ((q_0,p_0), a) = (q_a,p_0)
  • δ((q_0,p_0), b) = (q_0,p_b)
  • δ((q_0,p_b), a) = (q_a,p_0)
  • δ((q_0,p_b), b) = (q_0,p_bb)
  • δ((q_0,p_bb), a) = (q_a,p_bb)
  • δ((q_0,p_bb), b) = (q_0,p_bb)
  • δ((q_a,p_0), a) = (q_aa,p_0)
  • δ((q_a,p_0), b) = (q_0,p_b)
  • δ((q_a,p_b), a) = (q_aa,p_0)
  • δ((q_a,p_b), b) = (q_0,p_bb)
  • δ((q_a,p_bb), a) = (q_aa,p_bb)
  • δ((q_a,p_bb), b) = (q_0,p_bb)
  • δ((q_aa,p_0), a) = (q_aa,p_0)
  • δ((q_aa,p_0), b) = (q_aa,p_b)
  • δ((q_aa,p_b), a) = (q_aa,p_0)
  • δ((q_aa,p_b), b) = (q_aa,p_bb)
  • δ((q_aa,p_bb), a) = (q_aa,p_bb)
  • δ((q_aa,p_bb), b) = (q_aa,p_bb)

Which states can be merged?

Think: which of the 8 remaining states “do the same thing” and can be merged into one state without changing the language?
Suggest groups, and give reasons.

Hint

What happens to the run after it reaches one of the five accepting states?
Can it leave them?

Answer

The five accepting states: every letter leads from an accepting state to an accepting state (in each of them qaaq_{aa} or pbbp_{bb} is a coordinate, and both are trap states), so from each of them every continuation of the word is accepted.
We answer for the three states that do not accept, (q0,p0)(q_0,p_0)⁠, (qa,p0)(q_a,p_0) and (q0,pb)(q_0,p_b)⁠, right below.

Let us check whether we can also merge (q0,p0)(q_0,p_0) and (qa,p0)(q_a,p_0)⁠. From (qa,p0)(q_a,p_0) the letter aa leads to (qaa,p0)(q_{aa},p_0)⁠, which accepts, and from (q0,p0)(q_0,p_0) it leads to (qa,p0)(q_a,p_0)⁠, which does not.
If we merge the two states, we will not know where to continue on aa⁠:
the word aaaa must be accepted and the word aa must not.
Such a word, which leads from one of the states to an accepting state and from the other to a state that does not accept, is a distinguishing word for the pair.

Formulate: when are two states pp and qq of a DFA “the same”, so that we can merge them without changing the language?
Write a condition using δ∗\delta^* and FF⁠.

Hint

Candidate definitions: (1) pp and qq both accept or both do not; (2) for every letter σ\sigma⁠, δ(p,σ)=δ(q,σ)\delta(p,\sigma)=\delta(q,\sigma)⁠; (3) the pair has no distinguishing word.
Which of them is right in the example of (q0,p0)(q_0,p_0) and (qa,p0)(q_a,p_0)⁠, and which fits the five accepting states here?

The formal definition

Let M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) be a DFA. A word w∈Σ∗w\in\Sigma^* is a distinguishing word for the pair of states p,qp,q if exactly one of the states δ∗(p,w)\delta^*(p,w) and δ∗(q,w)\delta^*(q,w) belongs to FF⁠.
The states pp and qq are equivalent states, and we write p≡qp\equiv q⁠, if they have no distinguishing word:

p≡q  ⟺  ∀w∈Σ∗: (δ∗(p,w)∈F  ⟺  δ∗(q,w)∈F)p\equiv q \iff \forall w\in\Sigma^*:\ \bigl(\delta^*(p,w)\in F \iff \delta^*(q,w)\in F\bigr)

The empty word ε\varepsilon distinguishes every accepting state from every state that does not accept, because δ∗(p,ε)=p\delta^*(p,\varepsilon)=p⁠.
Candidate (1) is only the case w=εw=\varepsilon⁠, and candidate (2) is too strict:
from (qaa,p0)(q_{aa},p_0) and from (q0,pbb)(q_0,p_{bb}) the letter aa leads to different states, but they have no distinguishing word.

The sign ≡\equiv here is the same sign as for equivalent automata, and not by chance:
p≡qp\equiv q exactly when the automaton (Q,Σ,δ,p,F)(Q,\Sigma,\delta,p,F)⁠, which starts at pp⁠, is equivalent to the automaton (Q,Σ,δ,q,F)(Q,\Sigma,\delta,q,F)⁠, which starts at qq⁠.
The language of the first is {w∣δ∗(p,w)∈F}\{w\mid \delta^*(p,w)\in F\} and of the second {w∣δ∗(q,w)∈F}\{w\mid \delta^*(q,w)\in F\}⁠, and the condition in the definition says that these languages are equal.

Question 1. Find a distinguishing word for the pair (q0,p0),(q0,pb)(q_0,p_0),(q_0,p_b) and for the pair (qa,p0),(q0,pb)(q_a,p_0),(q_0,p_b)⁠.

Figure: the product automaton (from the start of the lesson)
(q0,p0)(q0,pb)(q0,pbb)(qa,p0)(qa,pb)(qa,pbb)(qaa,p0)(qaa,pb)(qaa,pbb)ababababababababa,b
The product automaton: the row is the state of AA and the column the state of BB⁠. The letter aa advances the first coordinate (one row down) and returns the second to p0p_0⁠, except from the trap state pbbp_{bb}⁠; the letter bb acts in the same way on the columns. The five states in the bottom row and the right column accept.

Transitions of the diagram:

  • δ((q_0,p_0), a) = (q_a,p_0)
  • δ((q_0,p_0), b) = (q_0,p_b)
  • δ((q_0,p_b), a) = (q_a,p_0)
  • δ((q_0,p_b), b) = (q_0,p_bb)
  • δ((q_0,p_bb), a) = (q_a,p_bb)
  • δ((q_0,p_bb), b) = (q_0,p_bb)
  • δ((q_a,p_0), a) = (q_aa,p_0)
  • δ((q_a,p_0), b) = (q_0,p_b)
  • δ((q_a,p_b), a) = (q_aa,p_0)
  • δ((q_a,p_b), b) = (q_0,p_bb)
  • δ((q_a,p_bb), a) = (q_aa,p_bb)
  • δ((q_a,p_bb), b) = (q_0,p_bb)
  • δ((q_aa,p_0), a) = (q_aa,p_0)
  • δ((q_aa,p_0), b) = (q_aa,p_b)
  • δ((q_aa,p_b), a) = (q_aa,p_0)
  • δ((q_aa,p_b), b) = (q_aa,p_bb)
  • δ((q_aa,p_bb), a) = (q_aa,p_bb)
  • δ((q_aa,p_bb), b) = (q_aa,p_bb)
Answer

For the first pair bb⁠: from (q0,pb)(q_0,p_b) it leads to (q0,pbb)(q_0,p_{bb})⁠, which accepts, and from (q0,p0)(q_0,p_0) to (q0,pb)(q_0,p_b)⁠, which does not.
For the second pair aa⁠: from (qa,p0)(q_a,p_0) to (qaa,p0)(q_{aa},p_0)⁠, which accepts, and from (q0,pb)(q_0,p_b) to (qa,p0)(q_a,p_0)⁠, which does not.

Equivalence behaves like equality: every state is equivalent to itself; if p≡qp\equiv q then q≡pq\equiv p⁠; and if p≡qp\equiv q and q≡tq\equiv t then p≡tp\equiv t⁠, because for every word ww we have δ∗(p,w)∈F  ⟺  δ∗(q,w)∈F  ⟺  δ∗(t,w)∈F\delta^*(p,w)\in F\iff\delta^*(q,w)\in F\iff\delta^*(t,w)\in F⁠.
So the states split into disjoint groups, in each of which every two states are equivalent, while states from different groups are not equivalent.
The set of all the states equivalent to a state qq is the class of qq⁠, and we denote it [q][q]⁠.

Question 2. Is (qaa,p0)≡(q0,pbb)(q_{aa},p_0)\equiv(q_0,p_{bb})⁠? How many classes are there among the five accepting states of the product?

Figure: the product automaton (from the start of the lesson)
(q0,p0)(q0,pb)(q0,pbb)(qa,p0)(qa,pb)(qa,pbb)(qaa,p0)(qaa,pb)(qaa,pbb)ababababababababa,b
The product automaton: the row is the state of AA and the column the state of BB⁠. The letter aa advances the first coordinate (one row down) and returns the second to p0p_0⁠, except from the trap state pbbp_{bb}⁠; the letter bb acts in the same way on the columns. The five states in the bottom row and the right column accept.

Transitions of the diagram:

  • δ((q_0,p_0), a) = (q_a,p_0)
  • δ((q_0,p_0), b) = (q_0,p_b)
  • δ((q_0,p_b), a) = (q_a,p_0)
  • δ((q_0,p_b), b) = (q_0,p_bb)
  • δ((q_0,p_bb), a) = (q_a,p_bb)
  • δ((q_0,p_bb), b) = (q_0,p_bb)
  • δ((q_a,p_0), a) = (q_aa,p_0)
  • δ((q_a,p_0), b) = (q_0,p_b)
  • δ((q_a,p_b), a) = (q_aa,p_0)
  • δ((q_a,p_b), b) = (q_0,p_bb)
  • δ((q_a,p_bb), a) = (q_aa,p_bb)
  • δ((q_a,p_bb), b) = (q_0,p_bb)
  • δ((q_aa,p_0), a) = (q_aa,p_0)
  • δ((q_aa,p_0), b) = (q_aa,p_b)
  • δ((q_aa,p_b), a) = (q_aa,p_0)
  • δ((q_aa,p_b), b) = (q_aa,p_bb)
  • δ((q_aa,p_bb), a) = (q_aa,p_bb)
  • δ((q_aa,p_bb), b) = (q_aa,p_bb)
Hint

Prove by induction on the length of the word that from every accepting state of the product, every word ends in an accepting state.

Answer

Yes. We prove by induction on the length of ww that for every accepting state pp of the product, δ∗(p,w)\delta^*(p,w) accepts.
Base: δ∗(p,ε)=p\delta^*(p,\varepsilon)=p accepts. Step: if δ∗(p,u)\delta^*(p,u) accepts, then δ∗(p,uσ)=δ(δ∗(p,u),σ)\delta^*(p,u\sigma)=\delta(\delta^*(p,u),\sigma)⁠, and every letter leads from an accepting state to an accepting state (the coordinate qaaq_{aa} or pbbp_{bb} is a trap state).
So no two accepting states have a distinguishing word, and all five form one class.

Table filling in rounds

With the eight states of the product it is already tedious to check every pair separately.
We merged the five accepting states with one argument, because from each of them every letter leads to an accepting state; an automaton without such a structure has no general argument that covers many pairs at once.
We need a method that finds all the pairs that have a distinguishing word.
The idea: a distinguishing word that starts with the letter σ\sigma is σw\sigma w⁠, and ww distinguishes the two states that σ\sigma leads to.
For this we need to read a word in two parts, the first letter and then the rest, while δ∗\delta^* is defined by the last letter.

Claim and proof: reading a word in two parts

Claim. For every state qq and every two words u,vu,v⁠: δ∗(q,uv)=δ∗(δ∗(q,u),v)\delta^*(q,uv)=\delta^*\bigl(\delta^*(q,u),v\bigr)⁠.
In particular, for a letter σ\sigma⁠: δ∗(q,σw)=δ∗(δ(q,σ),w)\delta^*(q,\sigma w)=\delta^*\bigl(\delta(q,\sigma),w\bigr)⁠.

Proof by induction on the length of vv⁠, with qq and uu fixed.

  • Base, v=εv=\varepsilon⁠: δ∗(q,uε)=δ∗(q,u)=δ∗(δ∗(q,u),ε)\delta^*(q,u\varepsilon)=\delta^*(q,u)=\delta^*(\delta^*(q,u),\varepsilon)⁠, by the definition of δ∗\delta^* on the empty word.
  • Induction hypothesis: the claim holds for every word vv of length nn⁠.
  • Step, v=v′yv=v'y with ∣v′∣=n|v'|=n and y∈Σy\in\Sigma⁠:
δ∗(q,uv′y)=δ(δ∗(q,uv′),y)=δ(δ∗(δ∗(q,u),v′),y)=δ∗(δ∗(q,u),v′y)\delta^*(q,uv'y)=\delta\bigl(\delta^*(q,uv'),y\bigr)=\delta\bigl(\delta^*(\delta^*(q,u),v'),y\bigr)=\delta^*\bigl(\delta^*(q,u),v'y\bigr)

The first and third equalities are the definition of δ∗\delta^* on the last letter, and the second is the induction hypothesis.
The case u=σu=\sigma gives the conclusion, because δ∗(q,σ)=δ(q,σ)\delta^*(q,\sigma)=\delta(q,\sigma)⁠.

So long distinguishing words are built from short ones:

  • Round 0: mark every pair of an accepting state and a state that does not accept (the distinguishing word ε\varepsilon⁠).
  • Round kk⁠: mark a pair p,qp,q not yet marked if there is a letter σ\sigma that leads it to a pair δ(p,σ),δ(q,σ)\delta(p,\sigma),\delta(q,\sigma) marked in an earlier round. The distinguishing word is σ\sigma followed by the word of that pair.
  • Stop: when a whole round marks no pair. The pairs that were not marked are equivalent, and we merge them. In a DFA with nn states this happens by round n−1n-1 at the latest (the proof is in part (3) below).

The pairs are written in a triangular table, one cell per pair.
Half a table is enough: a word distinguishes pp from qq exactly when it distinguishes qq from pp⁠, so the cell of q,pq,p would repeat the cell of p,qp,q⁠; and the diagonal cells are not needed, because no word distinguishes a state from itself.
In every cell we mark we write the distinguishing word. The word written in round kk has length kk⁠:
the pair that the letter leads to was marked exactly in round k−1k-1⁠, because if it had been marked earlier, the current pair would already have been marked in the round after that.
The method is called table filling.

Example. We are given a DFA over {a,b}\{a,b\} with six states q0,…,q5q_0,\dots,q_5⁠, start state q0q_0 and a single accepting state q3q_3⁠.
The names are numbered, because we do not yet know what each state remembers.

Stateon aaon bb
→q0\to q_0q5q_5q1q_1
q1q_1q4q_4q1q_1
q2q_2q5q_5q3q_3
∗ q3{*}\,q_3q2q_2q3q_3
q4q_4q4q_4q4q_4
q5q_5q2q_2q3q_3
q0q5q2q3q1q4abaabbabbaa,b
The DFA of the example. The names are numbered, and what each state remembers becomes clear after minimization.

Transitions of the diagram:

  • δ(q_0, a) = q_5
  • δ(q_0, b) = q_1
  • δ(q_5, a) = q_2
  • δ(q_5, b) = q_3
  • δ(q_2, a) = q_5
  • δ(q_2, b) = q_3
  • δ(q_3, a) = q_2
  • δ(q_3, b) = q_3
  • δ(q_1, a) = q_4
  • δ(q_1, b) = q_1
  • δ(q_4, a) = q_4
  • δ(q_4, b) = q_4

Question 3. All the states are reachable. Fill in the table round by round, and before each round predict which pairs it will mark.
In each cell write a distinguishing word, or ≡\equiv if the pair was not marked by the time the algorithm stops.
The word you write in round kk has length kk⁠, so its length tells in which round the pair was marked.
What is the language of the automaton?

q0q_0q1q_1q2q_2q3q_3q4q_4
q1q_1
?abab (round 2)
q2q_2
?bb (round 1)
?bb (round 1)
q3q_3
?ε\varepsilon (round 0)
?ε\varepsilon (round 0)
?ε\varepsilon (round 0)
q4q_4
?abab (round 2)
?≡\equiv
?bb (round 1)
?ε\varepsilon (round 0)
q5q_5
?bb (round 1)
?bb (round 1)
?≡\equiv
?ε\varepsilon (round 0)
?bb (round 1)
Answer: the rounds and the language

Round 0: the five pairs of q3q_3 with another state. Round 1: the letter bb leads q2q_2 and q5q_5 to q3q_3⁠, and q0,q1,q4q_0,q_1,q_4 to q1,q1,q4q_1,q_1,q_4⁠; so the six pairs of a state from {q2,q5}\{q_2,q_5\} and a state from {q0,q1,q4}\{q_0,q_1,q_4\} are marked, by the word bb⁠.
The letter aa leads no state to q3q_3⁠, so it marks nothing in this round.
Round 2: aa leads q0,q1q_0,q_1 to q5,q4q_5,q_4 and q0,q4q_0,q_4 to q5,q4q_5,q_4⁠, and these two pairs were marked in round 1; so q0,q1q_0,q_1 and q0,q4q_0,q_4 are marked by the word abab⁠.
The pair q1,q4q_1,q_4 goes on aa to q4,q4q_4,q_4 and on bb to q1,q4q_1,q_4⁠, and the pair q2,q5q_2,q_5 goes on aa to q5,q2q_5,q_2 and on bb to q3,q3q_3,q_3⁠; neither reaches a marked pair.
Round 3: no new pair, and we stop.

The classes: {q0}\{q_0\}⁠, {q1,q4}\{q_1,q_4\}⁠, {q2,q5}\{q_2,q_5\}⁠, {q3}\{q_3\}⁠. Now we can say what each class remembers:
[q1][q_1] is “the word started with bb” (a rejecting trap state), [q2][q_2] is “started with aa and the last letter is aa”, and [q3][q_3] is “started with aa and the last letter is bb”.
The language: the words that start with aa and end with bb⁠.

Why we can rely on the table. We need two directions: every marked pair is not equivalent, and every pair not marked when the algorithm stops is equivalent.

Proof: a pair is marked if and only if it has a distinguishing word

(1) The main claim: after round kk⁠, a pair p,qp,q is marked if and only if it has a distinguishing word of length at most kk⁠. By induction on kk⁠.
In round 0 exactly the pairs of an accepting state and a state that does not accept are marked, that is, the pairs that ε\varepsilon distinguishes.
Suppose the claim holds after round kk⁠. A nonempty word of length at most k+1k+1 is σu\sigma u with ∣u∣≤k|u|\leq k⁠, and by the claim on reading a word in two parts it distinguishes pp and qq exactly when uu distinguishes δ(p,σ)\delta(p,\sigma) and δ(q,σ)\delta(q,\sigma)⁠.
So p,qp,q has a distinguishing word of length at most k+1k+1 exactly when it has one of length at most kk⁠, or some letter leads it to a pair that has one.
By the induction hypothesis, this is exactly the condition for the pair to be marked after round k+1k+1⁠:
it was marked before, or round k+1k+1 marks it.

(2) The marking at the end is correct. If round kk marks no pair, the rounds after it mark none either, because they check the same pairs against the same marks.
So, by (1), every pair that has a distinguishing word, of any length, is already marked when the algorithm stops, and every marked pair has a distinguishing word.

(3) The algorithm stops, by round n−1n-1 at the latest, where n=∣Q∣n=|Q|⁠. The relation “no distinguishing word of length at most kk” is reflexive, symmetric and transitive, by the same argument as for equivalence, and by (1) it relates exactly the pairs not marked after round kk⁠.
We write ckc_k for the number of its classes. Marks are only added, so every class after round k+1k+1 is contained in a class after round kk⁠; a round that marks a pair also splits a class, so then ck+1≥ck+1c_{k+1}\geq c_k+1⁠.
If all the states accept, or none of them does, round 0 marks nothing and the algorithm stops there.
Otherwise c0=2c_0=2 and always ck≤nc_k\leq n⁠, so after round 0 there are at most n−2n-2 rounds that mark a pair:
the algorithm stops by round n−1n-1 at the latest. In particular, the shortest distinguishing word of a pair has length at most n−2n-2⁠.

Quotient automaton

Question 4. We apply the table to the product of this lesson.
After removing (qa,pb)(q_a,p_b)⁠, eight states remained, that is, 8⋅72=28\tfrac{8\cdot7}{2}=28 pairs.
We go through the table round by round.

Figure: the product automaton (from the start of the lesson)
(q0,p0)(q0,pb)(q0,pbb)(qa,p0)(qa,pb)(qa,pbb)(qaa,p0)(qaa,pb)(qaa,pbb)ababababababababa,b
The product automaton: the row is the state of AA and the column the state of BB⁠. The letter aa advances the first coordinate (one row down) and returns the second to p0p_0⁠, except from the trap state pbbp_{bb}⁠; the letter bb acts in the same way on the columns. The five states in the bottom row and the right column accept.

Transitions of the diagram:

  • δ((q_0,p_0), a) = (q_a,p_0)
  • δ((q_0,p_0), b) = (q_0,p_b)
  • δ((q_0,p_b), a) = (q_a,p_0)
  • δ((q_0,p_b), b) = (q_0,p_bb)
  • δ((q_0,p_bb), a) = (q_a,p_bb)
  • δ((q_0,p_bb), b) = (q_0,p_bb)
  • δ((q_a,p_0), a) = (q_aa,p_0)
  • δ((q_a,p_0), b) = (q_0,p_b)
  • δ((q_a,p_b), a) = (q_aa,p_0)
  • δ((q_a,p_b), b) = (q_0,p_bb)
  • δ((q_a,p_bb), a) = (q_aa,p_bb)
  • δ((q_a,p_bb), b) = (q_0,p_bb)
  • δ((q_aa,p_0), a) = (q_aa,p_0)
  • δ((q_aa,p_0), b) = (q_aa,p_b)
  • δ((q_aa,p_b), a) = (q_aa,p_0)
  • δ((q_aa,p_b), b) = (q_aa,p_bb)
  • δ((q_aa,p_bb), a) = (q_aa,p_bb)
  • δ((q_aa,p_bb), b) = (q_aa,p_bb)

(a) Round 0: how many pairs will be marked?

A: 2828

The reasoning: the first round marks all the pairs.

Only pairs that have a distinguishing word are marked. In round 0 that word is ε\varepsilon⁠, and it distinguishes only an accepting state from one that does not accept.

B: 1515

The reasoning: a state that does not accept and an accepting state:
3⋅53\cdot5 pairs.

This is the answer: ε\varepsilon distinguishes exactly an accepting state from a state that does not accept.

C: 1010

The reasoning: the pairs of the five accepting states, 5⋅42\tfrac{5\cdot4}{2}⁠.

Two accepting states give the same answer on ε\varepsilon⁠, so their pair is not marked in round 0.

D: 33

The reasoning: we mark the pairs of two states that do not accept.

Round 0 marks a pair of an accepting state and a state that does not accept.
Two states that do not accept give the same answer on ε\varepsilon⁠, so ε\varepsilon does not distinguish them.

(b) Round 1: how many new pairs will be marked?

A: 1313

The reasoning: every pair not marked in round 0 is marked in round 1.

A pair is marked only if a letter leads it to a pair already marked.
The ten pairs of accepting states are not such pairs.

B: 1010

The reasoning: now it is the turn of the pairs of accepting states.

From every accepting state every letter leads to an accepting state, so no letter leads a pair of two accepting states to a marked pair.

C: 33

The reasoning: the three pairs of the states that do not accept, with the words aa⁠, bb and aa⁠.

This is the answer: each of them has a letter that leads it to a pair of an accepting state and one that does not accept (Question 1 and the example before it).

D: 00

The reasoning: from every state that does not accept, the letters lead to similar states, so there is no new pair.

From (qa,p0)(q_a,p_0) the letter aa leads to an accepting state, and from (q0,p0)(q_0,p_0) to a state that does not accept, that is, to a pair marked in round 0.
Each of the three pairs of states that do not accept has such a letter.

(c) Round 2: what happens in it?

A: pairs of accepting states are marked, with words of length 22

The reasoning: what a short word did not separate, a longer word will.

From every accepting state every word is accepted, so a pair of two accepting states has no distinguishing word of any length (Question 2).

B: the pair (q0,p0),(qa,p0)(q_0,p_0),(q_a,p_0) is marked with the word aaaa

The reasoning: the letter aa leads it to a pair marked in round 1.

This pair was already marked in round 1, with the word aa⁠.
Each pair is marked once, and no new word is written for it.

C: we go on to round 3, because the product has 88 states

The reasoning: we stop only at round n−1=7n-1=7⁠.

We stop at the first round that marks no pair. Round n−1n-1 is only a bound on when this happens.

D: no new pair is marked, and the algorithm stops

The reasoning: the pairs left are only pairs of two accepting states.

This is the answer: every letter leads such a pair to a pair of two accepting states, which is not marked, or to a single state.
So the round marks nothing.

(d) The classes: how many classes of equivalent states are there?

A: 22

The reasoning: the accepting states, and the states that do not accept.

The three states that do not accept were separated from each other in round 1, so each of them is in a class of its own.

B: 44

The reasoning: each of the three states that do not accept alone, plus one class of all the accepting states.

This is the answer: [(q0,p0)][(q_0,p_0)]⁠, [(qa,p0)][(q_a,p_0)]⁠, [(q0,pb)][(q_0,p_b)] and [(qaa,p0)][(q_{aa},p_0)]⁠, which is the class of the five accepting states.

C: 55

The reasoning: the three states that do not accept, plus two classes of accepting states:
those with qaaq_{aa} and those with pbbp_{bb}⁠.

No pair of two accepting states is marked, so all five accepting states are in one class.
For example (qaa,p0)≡(q0,pbb)(q_{aa},p_0)\equiv(q_0,p_{bb}) (Question 2).

D: 88

The reasoning: every state is in a class of its own.

A pair that was not marked is a pair of equivalent states, and equivalent states are in the same class.
Ten pairs were not marked.

We build an automaton whose states are the classes. Let M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) be a DFA.
The quotient automaton of MM is the DFA

M′=(Q′,Σ,δ′,s′,F′)M'=(Q',\Sigma,\delta',s',F')

with the components:

  • States: Q′={[q]∣q∈Q}Q'=\{[q]\mid q\in Q\}⁠, the classes of equivalent states.
  • Start state: s′=[s]s'=[s]⁠, the class of the start state.
  • Accepting states: F′={[q]∣q∈F}F'=\{[q]\mid q\in F\}⁠, the classes of the accepting states.
  • Transition function: for every q∈Qq\in Q and every σ∈Σ\sigma\in\Sigma⁠,
δ′([q],σ)=[δ(q,σ)]\delta'\bigl([q],\sigma\bigr)=\bigl[\delta(q,\sigma)\bigr]

That is, the transition on σ\sigma from a class is the class of the state that σ\sigma leads to from one of the states in the class.
The definition of δ′\delta' picks a state qq from the class; below we prove that the result does not depend on the choice.
As for every DFA, δ′∗\delta'^* is the extension of δ′\delta' to words.

In the product, after removing the state that cannot be reached, the transition table of the quotient automaton is:

Stateon aaon bb
→[(q0,p0)]\to[(q_0,p_0)][(qa,p0)][(q_a,p_0)][(q0,pb)][(q_0,p_b)]
[(qa,p0)][(q_a,p_0)][(qaa,p0)][(q_{aa},p_0)][(q0,pb)][(q_0,p_b)]
[(q0,pb)][(q_0,p_b)][(qa,p0)][(q_a,p_0)][(qaa,p0)][(q_{aa},p_0)]
∗ [(qaa,p0)]{*}\,[(q_{aa},p_0)][(qaa,p0)][(q_{aa},p_0)][(qaa,p0)][(q_{aa},p_0)]

If we call [(q0,p0)][(q_0,p_0)] by the name r0r_0⁠, [(qa,p0)][(q_a,p_0)] by the name rar_a⁠, [(q0,pb)][(q_0,p_b)] by the name rbr_b and [(qaa,p0)][(q_{aa},p_0)] by the name rpairr_{\mathrm{pair}}⁠, we get exactly the table of the validator designed directly.
Removing the states that are not reachable and merging the equivalent states are together called minimization of the automaton, and here minimization led from the product to the small validator, without designing again, and among its four states there is no pair that can be merged:
every pair has a distinguishing word.

Think: in the definition of δ′\delta' we chose one state qq from the class.
Why does the result not depend on the choice, and why does the quotient automaton recognize the same language as the original automaton?

Proof: the quotient automaton is well defined and recognizes the same language

Let M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) be a DFA, and let M′=(Q′,Σ,δ′,s′,F′)M'=(Q',\Sigma,\delta',s',F') be its quotient automaton, as defined.

(a) The transition is well defined. Suppose p≡qp\equiv q and let σ\sigma be a letter.
For every word ww⁠, δ∗(δ(p,σ),w)=δ∗(p,σw)\delta^*(\delta(p,\sigma),w)=\delta^*(p,\sigma w)⁠, by the claim on reading a word in two parts, and likewise for qq⁠.
Since p≡qp\equiv q⁠, the word σw\sigma w does not distinguish pp from qq⁠, so ww does not distinguish δ(p,σ)\delta(p,\sigma) from δ(q,σ)\delta(q,\sigma)⁠.
This holds for every ww⁠, so δ(p,σ)≡δ(q,σ)\delta(p,\sigma)\equiv\delta(q,\sigma)⁠: two states of the same class move to the same class.

(b) The accepting states are well defined. If p≡qp\equiv q then ε\varepsilon does not distinguish them, that is, p∈F  ⟺  q∈Fp\in F\iff q\in F⁠.
So each class either accepts entirely or does not accept at all.

(c) The run on a word. We prove by induction on the length of ww the claim:
for every state qq⁠, δ′∗([q],w)=[δ∗(q,w)]\delta'^*([q],w)=[\delta^*(q,w)]⁠.

  • Base, w=εw=\varepsilon⁠: both sides equal [q][q] by the definition of δ∗\delta^*⁠.
  • Induction hypothesis: the claim holds for every word of length nn⁠.
  • Step, w=uσw=u\sigma with ∣u∣=n|u|=n and σ∈Σ\sigma\in\Sigma⁠:
δ′∗([q],uσ)=δ′(δ′∗([q],u),σ)=δ′([δ∗(q,u)],σ)=[δ(δ∗(q,u),σ)]=[δ∗(q,uσ)]\delta'^*([q],u\sigma)=\delta'\bigl(\delta'^*([q],u),\sigma\bigr)=\delta'\bigl([\delta^*(q,u)],\sigma\bigr)=\bigl[\delta(\delta^*(q,u),\sigma)\bigr]=\bigl[\delta^*(q,u\sigma)\bigr]

The first and last equalities are the definition of δ∗\delta^*⁠, the second is the induction hypothesis, and the third is the definition of δ′\delta' (well defined by (a)).

Conclusion. w∈L(M′)w\in L(M') if and only if δ′∗([s],w)∈F′\delta'^*([s],w)\in F'⁠, if and only if (by (c)) [δ∗(s,w)]∈F′[\delta^*(s,w)]\in F'⁠, if and only if (by (b)) δ∗(s,w)∈F\delta^*(s,w)\in F⁠, if and only if w∈L(M)w\in L(M)⁠.

Minimization: the full order

  1. Find the reachable states (by a search in the graph), and remove the rest.
  2. Fill in the table of pairs in rounds, until a round marks no pair.
  3. Merge each class of equivalent states into one state, and build the quotient automaton.

The result is equivalent to the original automaton, that is, it recognizes the same language (the proof is in the section Quotient automaton), it has no state that cannot be reached, and it has no two equivalent states.
The section A lower bound on the number of states shows that no DFA for the same language is smaller.

Connection to later courses: a small automaton in a lexical analyzer

A compiler begins by splitting the program text into words, such as variable names, numbers and reserved words; this part is done by a lexical analyzer, which has a DFA at its core.
When the DFA is built automatically from a description of the kinds of words, it sometimes comes out larger than necessary, and a minimization method like the one in this lesson shrinks its transition table.

A shorter method: splitting into classes

Table filling has one cell for every pair of states: the six states of the example in Question 3 have 15 cells, and the eight states of the product have 28.
We can work with one row per state instead: we keep the states split into classes, and split a class when its states behave differently on a letter.
This is partition refinement, the method of Edward Moore. We write [q]k[q]_k for the class of the state qq after round kk⁠.

  • Round 0: two classes, the accepting states and the states that do not accept.
  • Round kk⁠: for every state qq we write its signature: for every letter σ\sigma⁠, the class [δ(q,σ)]k−1[\delta(q,\sigma)]_{k-1} of the previous round. Over {a,b}\{a,b\} the signature is the pair ([δ(q,a)]k−1,[δ(q,b)]k−1)\bigl([\delta(q,a)]_{k-1},[\delta(q,b)]_{k-1}\bigr)⁠. States of the same class whose signatures differ split into separate classes.
  • Stop: when a whole round splits no class. The classes that remain are the classes of equivalent states.

The method runs the same rounds as table filling: after round kk two states are in the same class exactly when they have no distinguishing word of length at most kk⁠, that is, when their pair was not marked in the table by round kk⁠.
The difference is in the work: each round has nn rows, one per state, instead of n(n−1)2\tfrac{n(n-1)}{2} cells, and we check only where each letter leads, without writing words.

Proof: the classes after each round

Claim. After round kk⁠, [p]k=[q]k[p]_k=[q]_k if and only if p,qp,q have no distinguishing word of length at most kk⁠.

Proof by induction on kk⁠. In round 0 the states p,qp,q are in the same class exactly when both accept or neither accepts, that is, when ε\varepsilon does not distinguish them.
Suppose the claim holds after round kk⁠. By the method, [p]k+1=[q]k+1[p]_{k+1}=[q]_{k+1} exactly when [p]k=[q]k[p]_k=[q]_k and for every letter σ\sigma⁠, [δ(p,σ)]k=[δ(q,σ)]k[\delta(p,\sigma)]_k=[\delta(q,\sigma)]_k⁠.
By the induction hypothesis, the condition means: p,qp,q have no distinguishing word of length at most kk⁠, and for every letter σ\sigma the pair δ(p,σ),δ(q,σ)\delta(p,\sigma),\delta(q,\sigma) has no such word.
A nonempty word of length at most k+1k+1 is σu\sigma u with ∣u∣≤k|u|\leq k⁠, and by the claim on reading a word in two parts it distinguishes pp from qq exactly when uu distinguishes δ(p,σ)\delta(p,\sigma) from δ(q,σ)\delta(q,\sigma)⁠.
So the condition holds exactly when p,qp,q have no distinguishing word of length at most k+1k+1⁠.
This is the argument of part (1) of the proof for table filling, so the stop, by round n−1n-1 at the latest, carries over from part (3) there.

The split log. The classes say which states are equivalent, but they give no distinguishing word.
To get words, every time a class splits we write one line in a log:
what split from what, and a word.

  • In round 0 the word is ε\varepsilon⁠.
  • In round k≥1k\geq1 the word is the letter σ\sigma on which the signatures differ, followed by the word already in the log for the two classes that σ\sigma leads to. Those two classes split in an earlier round, so their line is already in the log.
  • A class that splits into more than two parts gets one line for every two parts.

The word of a pair of states is read from the first line of the log in which the two states are on different sides.
It distinguishes them: from all the states on one side σ\sigma leads to one class, from all the states on the other side to the other class, and the rest of the word distinguishes those two classes.
As in table filling, the word written in round kk has length kk⁠, and the pair has no shorter distinguishing word, because in the previous round the two states were still in the same class.

Splitting into classes: the order of steps.

Input: a DFA M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) with no states that cannot be reached.

Output: the classes of equivalent states, and a distinguishing word for every pair of states that are not equivalent.

  1. Initialization: P0P_0 is the list of the classes FF and Q−FQ-F⁠, without an empty class. If neither is empty, write a line in the log: FF split from Q−FQ-F⁠, with the word ε\varepsilon⁠.
  2. Rounds: repeat steps (a) to (d) for k=1,2,3,…k=1,2,3,\dots⁠, until step (d) stops:
    • (a) for every state qq and every letter σ\sigma⁠, write in the signature of qq the class of Pk−1P_{k-1} that contains δ(q,σ)\delta(q,\sigma)⁠.
    • (b) split every class of Pk−1P_{k-1} into groups of states with the same signature; PkP_k is the list of the groups.
    • (c) for every two groups that came from the same class, choose a letter σ\sigma on which their signatures differ; it leads them to classes XX and YY of Pk−1P_{k-1}⁠. Write in the log the two groups and the word σw\sigma w⁠, where ww is the word already in the log for XX and YY⁠.
    • (d) Stop: if Pk=Pk−1P_k=P_{k-1}⁠, that is, no class split, stop.
  3. Output: the classes of PkP_k are the classes of equivalent states. The word of a pair of states is in the first line of the log in which the two states are on different sides.

When P0P_0 has two classes, every round that splits a class increases the number of classes by at least one, and there are never more than n=∣Q∣n=|Q| classes; so at most n−2n-2 rounds split.
Stopping is safe: the next round would compute the signatures from the same classes and get the same partition, and so would every round after it, so no class will ever split again.
After the stop we build the quotient automaton from the classes.

Example: the product of this lesson. After removing (qa,pb)(q_a,p_b)⁠, eight states remain.
We name the classes with Roman numerals.

Figure: the product automaton (from the start of the lesson)
(q0,p0)(q0,pb)(q0,pbb)(qa,p0)(qa,pb)(qa,pbb)(qaa,p0)(qaa,pb)(qaa,pbb)ababababababababa,b
The product automaton: the row is the state of AA and the column the state of BB⁠. The letter aa advances the first coordinate (one row down) and returns the second to p0p_0⁠, except from the trap state pbbp_{bb}⁠; the letter bb acts in the same way on the columns. The five states in the bottom row and the right column accept.

Transitions of the diagram:

  • δ((q_0,p_0), a) = (q_a,p_0)
  • δ((q_0,p_0), b) = (q_0,p_b)
  • δ((q_0,p_b), a) = (q_a,p_0)
  • δ((q_0,p_b), b) = (q_0,p_bb)
  • δ((q_0,p_bb), a) = (q_a,p_bb)
  • δ((q_0,p_bb), b) = (q_0,p_bb)
  • δ((q_a,p_0), a) = (q_aa,p_0)
  • δ((q_a,p_0), b) = (q_0,p_b)
  • δ((q_a,p_b), a) = (q_aa,p_0)
  • δ((q_a,p_b), b) = (q_0,p_bb)
  • δ((q_a,p_bb), a) = (q_aa,p_bb)
  • δ((q_a,p_bb), b) = (q_0,p_bb)
  • δ((q_aa,p_0), a) = (q_aa,p_0)
  • δ((q_aa,p_0), b) = (q_aa,p_b)
  • δ((q_aa,p_b), a) = (q_aa,p_0)
  • δ((q_aa,p_b), b) = (q_aa,p_bb)
  • δ((q_aa,p_bb), a) = (q_aa,p_bb)
  • δ((q_aa,p_bb), b) = (q_aa,p_bb)

Round 0: I\mathrm{I} is the five accepting states, and II\mathrm{II} is the three states (q0,p0),(qa,p0),(q0,pb)(q_0,p_0),(q_a,p_0),(q_0,p_b)⁠.

Round 1: the signatures with respect to the classes of round 0.

StateClasson aaon bb
(qaa,p0)(q_{aa},p_0)I\mathrm{I}I\mathrm{I}I\mathrm{I}
(qaa,pb)(q_{aa},p_b)I\mathrm{I}I\mathrm{I}I\mathrm{I}
(q0,pbb)(q_0,p_{bb})I\mathrm{I}I\mathrm{I}I\mathrm{I}
(qa,pbb)(q_a,p_{bb})I\mathrm{I}I\mathrm{I}I\mathrm{I}
(qaa,pbb)(q_{aa},p_{bb})I\mathrm{I}I\mathrm{I}I\mathrm{I}
(q0,p0)(q_0,p_0)II\mathrm{II}II\mathrm{II}II\mathrm{II}
(qa,p0)(q_a,p_0)II\mathrm{II}I\mathrm{I}II\mathrm{II}
(q0,pb)(q_0,p_b)II\mathrm{II}II\mathrm{II}I\mathrm{I}

Inside I\mathrm{I} all the states have the same signature, so it does not split.
Inside II\mathrm{II} there are three different signatures, so it splits into three parts, and three lines are written in the log.
After the round I\mathrm{I} stays as it was, II={(q0,p0)}\mathrm{II}=\{(q_0,p_0)\}⁠, III={(qa,p0)}\mathrm{III}=\{(q_a,p_0)\} and IV={(q0,pb)}\mathrm{IV}=\{(q_0,p_b)\}⁠.

Round 2: a class of a single state cannot split, so only I\mathrm{I} is left to check.
From every accepting state every letter leads to an accepting state, so the signature of all five is (I,I)(\mathrm{I},\mathrm{I})⁠.
No class split, and we stop: four classes, as in Question 4.

The log:

  1. Round 0: the five accepting states split from the three states that do not accept. The word ε\varepsilon⁠.
  2. Round 1: (qa,p0)(q_a,p_0) split from (q0,p0)(q_0,p_0)⁠. The signatures differ on aa (I\mathrm{I} against II\mathrm{II}⁠), and the word of I\mathrm{I} and II\mathrm{II} in line 1 is ε\varepsilon⁠. The word aa⁠.
  3. Round 1: (q0,pb)(q_0,p_b) split from (q0,p0)(q_0,p_0)⁠. The signatures differ on bb⁠, and the word of I\mathrm{I} and II\mathrm{II} is again ε\varepsilon from line 1. The word bb⁠.
  4. Round 1: (qa,p0)(q_a,p_0) split from (q0,pb)(q_0,p_b)⁠. The signatures differ on both letters; we choose aa⁠, and again the word of I\mathrm{I} and II\mathrm{II} is ε\varepsilon from line 1. The word aa⁠.

The words are the same as those of table filling: ε\varepsilon for the fifteen pairs of an accepting state and a state that does not accept, and aa⁠, bb⁠, aa for the three pairs of states that do not accept, as in Question 1 and in part (b) of Question 4.
Each round had at most eight rows, instead of 28 cells.

Try it yourself. Apply splitting into classes to the DFA of the example in the section Table filling in rounds (Question 3).
In every round write the signatures and the classes, and keep a split log.
At the end compare the words in the log with the words in the table of Question 3.

Figure: the DFA of the table-filling example
q0q5q2q3q1q4abaabbabbaa,b
The DFA of the example. The names are numbered, and what each state remembers becomes clear after minimization.

Transitions of the diagram:

  • δ(q_0, a) = q_5
  • δ(q_0, b) = q_1
  • δ(q_5, a) = q_2
  • δ(q_5, b) = q_3
  • δ(q_2, a) = q_5
  • δ(q_2, b) = q_3
  • δ(q_3, a) = q_2
  • δ(q_3, b) = q_3
  • δ(q_1, a) = q_4
  • δ(q_1, b) = q_1
  • δ(q_4, a) = q_4
  • δ(q_4, b) = q_4
Answer

Round 0: I={q3}\mathrm{I}=\{q_3\} and II={q0,q1,q2,q4,q5}\mathrm{II}=\{q_0,q_1,q_2,q_4,q_5\}⁠. In the log: q3q_3 split from the other states, with the word ε\varepsilon⁠.

Round 1:

StateClasson aaon bb
q3q_3I\mathrm{I}II\mathrm{II}I\mathrm{I}
q0q_0II\mathrm{II}II\mathrm{II}II\mathrm{II}
q1q_1II\mathrm{II}II\mathrm{II}II\mathrm{II}
q2q_2II\mathrm{II}II\mathrm{II}I\mathrm{I}
q4q_4II\mathrm{II}II\mathrm{II}II\mathrm{II}
q5q_5II\mathrm{II}II\mathrm{II}I\mathrm{I}

II\mathrm{II} splits: {q2,q5}\{q_2,q_5\} separates from {q0,q1,q4}\{q_0,q_1,q_4\}⁠. The signatures differ on bb (I\mathrm{I} against II\mathrm{II}⁠, the line of ε\varepsilon⁠), so the word is bb⁠.
The classes: I={q3}\mathrm{I}=\{q_3\}⁠, II={q0,q1,q4}\mathrm{II}=\{q_0,q_1,q_4\}⁠, III={q2,q5}\mathrm{III}=\{q_2,q_5\}⁠.

Round 2:

StateClasson aaon bb
q3q_3I\mathrm{I}III\mathrm{III}I\mathrm{I}
q0q_0II\mathrm{II}III\mathrm{III}II\mathrm{II}
q1q_1II\mathrm{II}II\mathrm{II}II\mathrm{II}
q4q_4II\mathrm{II}II\mathrm{II}II\mathrm{II}
q2q_2III\mathrm{III}III\mathrm{III}I\mathrm{I}
q5q_5III\mathrm{III}III\mathrm{III}I\mathrm{I}

II\mathrm{II} splits: {q0}\{q_0\} separates from {q1,q4}\{q_1,q_4\}⁠. The signatures differ on aa (III\mathrm{III} against II\mathrm{II}⁠), and the word of {q2,q5}\{q_2,q_5\} and {q0,q1,q4}\{q_0,q_1,q_4\} in the log is bb⁠, so the word is abab⁠.
The classes: I={q3}\mathrm{I}=\{q_3\}⁠, II={q0}\mathrm{II}=\{q_0\}⁠, III={q2,q5}\mathrm{III}=\{q_2,q_5\}⁠, IV={q1,q4}\mathrm{IV}=\{q_1,q_4\}⁠.

Round 3: q1q_1 and q4q_4 have the signature (IV,IV)(\mathrm{IV},\mathrm{IV})⁠, and q2q_2 and q5q_5 the signature (III,I)(\mathrm{III},\mathrm{I})⁠.
No class splits, and we stop.

The classes are {q0}\{q_0\}⁠, {q1,q4}\{q_1,q_4\}⁠, {q2,q5}\{q_2,q_5\}⁠, {q3}\{q_3\}⁠. The log gives ε\varepsilon for the pairs of q3q_3⁠, bb for the pairs of a state from {q2,q5}\{q_2,q_5\} and a state from {q0,q1,q4}\{q_0,q_1,q_4\}⁠, and abab for the pairs q0,q1q_0,q_1 and q0,q4q_0,q_4⁠:
the same words as in the table of Question 3.

Indistinguishable words

The quotient automaton has no two equivalent states. It is not yet clear that there is no other DFA for the same language, with a completely different structure, with fewer states:
equivalence of states speaks about one automaton. We ask the same question about words, which does not depend on any automaton.

Let L⊆Σ∗L\subseteq\Sigma^* be a language, and let x,y∈Σ∗x,y\in\Sigma^*⁠. A word zz 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, 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. Like the equivalence of states, the relation ≡L\equiv_L is reflexive, symmetric and transitive, so it splits all the words into classes; the class of xx is [x]={y∣y≡Lx}[x]=\{y\mid y\equiv_L x\}⁠, and the classes are called the Myhill–Nerode classes, after John Myhill and Anil Nerode.

The link to states. Let M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) be a DFA that recognizes LL⁠, and write p=δ∗(s,x)p=\delta^*(s,x) and q=δ∗(s,y)q=\delta^*(s,y)⁠.
By the claim on reading a word in two parts δ∗(s,xz)=δ∗(p,z)\delta^*(s,xz)=\delta^*(p,z)⁠, so xz∈Lxz\in L exactly when δ∗(p,z)∈F\delta^*(p,z)\in F⁠; the same holds for yy and qq⁠.
So zz distinguishes the words x,yx,y exactly when it distinguishes the states p,qp,q⁠:

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

In particular, two words whose runs end in the same state are indistinguishable.
In a DFA whose states are all reachable, every class of equivalent states corresponds to one class of words:
the words whose runs end in its states.

Question 5. In the product of this lesson, the runs on ε\varepsilon⁠, aa⁠, bb and aaaa end in the four classes of the quotient automaton.
In each cell write a word that distinguishes, with respect to LL⁠, the word of the row from the word of the column.

ε\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 row of aaaa the word ε\varepsilon distinguishes, because aa∈Laa\in L and the other three words are not in LL⁠.

A lower bound on the number of states

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 for LL with fewer than nn states.
Each of the nn words is assigned the state δ∗(s,xi)\delta^*(s,x_i)⁠, so there are i≠ji\neq j with δ∗(s,xi)=δ∗(s,xj)\delta^*(s,x_i)=\delta^*(s,x_j) (the pigeonhole principle: when nn objects are put into fewer than nn boxes, some box holds at least two).
By the link to states xi≡Lxjx_i\equiv_L x_j⁠, which contradicts their being distinguishable.

Conclusion: the quotient automaton is the smallest. In the quotient automaton every state is reachable and no two states are equivalent.
Choose for every state a word whose run ends in it; by the link to states every two of these words are distinguishable, and by the bound every DFA for the same language has at least as many states as the quotient automaton.
In the product of this lesson these are the words ε,a,b,aa\varepsilon,a,b,aa of Question 5:
there is no DFA for LL with three states, and the directly designed validator r0,ra,rb,rpairr_0,r_a,r_b,r_{\mathrm{pair}} is the smallest.

Question 6. 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?

Hint

What must be remembered while reading to know whether the word will end in abab⁠?
Try one word for each state of that 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 DFA shows that three states are enough.

The smallest automaton

The smallest number of states of a DFA for LL is the number of classes of ≡L\equiv_L⁠:
by the bound there is at least one state per class, and the automaton whose states are the classes shows that this is enough.

In the product of this lesson, we name every state of the quotient automaton by the class of the words whose runs end in it:
[(q0,p0)][(q_0,p_0)] is [ε][\varepsilon]⁠, [(qa,p0)][(q_a,p_0)] is [a][a]⁠, [(q0,pb)][(q_0,p_b)] is [b][b] and [(qaa,p0)][(q_{aa},p_0)] is [aa][aa]⁠.
This is the automaton of the classes of LL⁠, in which the transition from a class [x][x] on a letter σ\sigma is the class [xσ][x\sigma]⁠:

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]

For example [a][a] on bb is [ab]=[b][ab]=[b]⁠, because abab and bb are indistinguishable:
neither has a pair of equal adjacent letters, and both end in bb⁠.

The automaton of the classes, and uniqueness up to renaming

The automaton of the classes. When LL has finitely many classes, define ML=(QL,Σ,δL,sL,FL)M_L=(Q_L,\Sigma,\delta_L,s_L,F_L)⁠, where QLQ_L is the set of classes and

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

The transition is well defined: if x≡Lyx\equiv_L y⁠, then for every zz the word σz\sigma z does not distinguish xx and yy⁠, so xσ≡Lyσx\sigma\equiv_L y\sigma⁠.
FLF_L is well defined too, because z=εz=\varepsilon does not distinguish two words of the same class.
By induction on the length of ww⁠, δL∗([ε],w)=[w]\delta_L^*([\varepsilon],w)=[w] (in the step, δL∗([ε],uσ)=δL([u],σ)=[uσ]\delta_L^*([\varepsilon],u\sigma)=\delta_L([u],\sigma)=[u\sigma]⁠), so w∈L(ML)w\in L(M_L) exactly when [w]∈FL[w]\in F_L⁠, that is, when w∈Lw\in L⁠.

Building MLM_L⁠: the order of steps.

  1. Start with the representative ε\varepsilon⁠: the class [ε][\varepsilon] is the start state.
  2. For every representative xx and every letter σ\sigma⁠, check whether xσx\sigma is indistinguishable from one of the representatives. If so, δL([x],σ)\delta_L([x],\sigma) is its class; if not, xσx\sigma is the representative of a new class.
  3. Repeat step 2 until every representative has a transition on every letter.
  4. The accepting states are the classes of the representatives that belong to LL⁠.

Uniqueness up to renaming. Let MM be a smallest DFA for LL⁠.
Every state of it is reachable, since otherwise removing the state would give a smaller DFA.
Assign to every state qq the class [x][x] of a word xx whose run ends in qq⁠; by the link to states the class does not depend on the choice of xx⁠.
By δ∗(s,xσ)=δ(q,σ)\delta^*(s,x\sigma)=\delta(q,\sigma) the assignment preserves the transitions, the start state and the accepting states.
Every class [x][x] is obtained from the state in which the run on xx ends, so the assignment is onto; since both automata have the same number of states (the number of classes), it is also one-to-one.
So every smallest DFA for LL is MLM_L with other names for its states, and in particular table-filling minimization always gives the same automaton, from whatever DFA it starts.

Minimization in a tool, step by step

On the page Minimization Step by Step the product automaton of this lesson is loaded in the tool, and pressing “Minimise step by step” shows the steps:
which states were removed, which pairs were marked in each round, and which classes remained.
In the tool you can also draw another automaton, larger than necessary for some language, decide in advance how many states will remain, and check.

The page Minimization Practice: Table Filling has six minimization problems on paper with solutions, and a tool that draws a new DFA every time and checks each step:
the states that cannot be reached, every cell of the pair table, the classes and the quotient automaton.

On the page Three Processes on a DFA three processes run step by step on the same DFA, and the diagram is coloured at every step:
the sets R0,R1,…R_0,R_1,\dots of reachable states (from Minimization: Removing Unreachable States), the equivalent states by table filling or by splitting into classes with a split log, and the classes of words with a separating suffix for every pair of words.

Practice

1. Over {a,b}\{a,b\} we are given a DFA that counts the aa‘s modulo 4:
states q0,q1,q2,q3q_0,q_1,q_2,q_3 (the subscript is the remainder), start q0q_0⁠, the letter aa leads from qiq_i to q(i+1) mod 4q_{(i+1)\bmod 4} and the letter bb keeps the state.
The accepting states are q0q_0 and q2q_2⁠. Minimize the automaton and describe its language.

Hint

Which pairs are marked in round 0? Where does aa lead the pair q0,q2q_0,q_2⁠, and was that pair marked?

Answer

All the states are reachable. In round 0 the pairs q0,q1q_0,q_1⁠, q0,q3q_0,q_3⁠, q1,q2q_1,q_2⁠, q2,q3q_2,q_3 are marked.
In round 1 there is no new pair: aa leads q0,q2q_0,q_2 to q1,q3q_1,q_3 and q1,q3q_1,q_3 to q2,q0q_2,q_0⁠, and neither pair was marked; bb keeps every pair in place.
The classes are {q0,q2}\{q_0,q_2\} and {q1,q3}\{q_1,q_3\}⁠, and the quotient automaton has two states.
The language: the words with an even number of aa‘s.

2. A validator for the language “ends in abab” over {a,b}\{a,b\} was designed to remember the last two letters:
q0q_0 (start, no letter read), qa,qbq_a,q_b (one letter read), and qaa,qab,qba,qbbq_{aa},q_{ab},q_{ba},q_{bb} (the last two letters).
From every state, the letter σ\sigma leads to the state of the last two letters after adding it (for example δ(qab,a)=qba\delta(q_{ab},a)=q_{ba} and δ(qa,b)=qab\delta(q_a,b)=q_{ab}⁠), and the only accepting state is qabq_{ab}⁠.
Minimize the validator, and compare the result with the validator of exercise 2 in Designing a Deterministic Finite Automaton.

Hint

Which states lead on bb to qabq_{ab}⁠? What do they have in common?

Answer

All seven states are reachable. In round 0 every pair with qabq_{ab} in it is marked.
In round 1 every pair of a state from {qa,qaa,qba}\{q_a,q_{aa},q_{ba}\} (ends in aa⁠) and a state from {q0,qb,qbb}\{q_0,q_b,q_{bb}\} is marked, by the word bb⁠.
In round 2 there is no new pair: within each of the two groups, aa leads both states to the group {qa,qaa,qba}\{q_a,q_{aa},q_{ba}\} and bb leads both to the same group (to qabq_{ab} from the first group, and to {qb,qbb}\{q_b,q_{bb}\} from the second).
The classes are {q0,qb,qbb}\{q_0,q_b,q_{bb}\}⁠, {qa,qaa,qba}\{q_a,q_{aa},q_{ba}\} and {qab}\{q_{ab}\}⁠: three states, and after renaming this is the validator q0,qa,qabq_0,q_a,q_{ab} of exercise 2.

3. Prove or disprove: in every DFA, every two accepting states are equivalent.

Hint

Look for an automaton in which, on the same letter, one accepting state can move to a state that does not accept and the other cannot.

Answer

The claim is false. Take the counter of aa‘s modulo 3 (q0,q1,q2q_0,q_1,q_2⁠, aa raises the remainder by 1, bb keeps the state) with the accepting states q1,q2q_1,q_2 (the number of aa‘s is not divisible by 3):
δ(q1,a)=q2\delta(q_1,a)=q_2 accepts and δ(q2,a)=q0\delta(q_2,a)=q_0 does not, so aa is a distinguishing word for two accepting states.

4. For the language “at least kk letters aa” over {a,b}\{a,b\} (for k≥2k\geq2⁠) we are given a counter with the states q0,…,qk+2q_0,\dots,q_{k+2}⁠:
in the state qiq_i⁠, ii letters aa have been read (and in qk+2q_{k+2} at least k+2k+2⁠), the letter aa raises the subscript by 1 up to k+2k+2⁠, the letter bb keeps the state, the start state is q0q_0 and the accepting states are qk,…,qk+2q_k,\dots,q_{k+2}⁠.
In which round of table filling will the pair q0,q1q_0,q_1 be marked, and what is its distinguishing word?
How many states will remain?

Hint

Fill in the table for k=3k=3⁠: which pair is marked in round 1, and which in round 2?
Look for the pattern.

Answer

In round jj (for 1≤j≤k−11\leq j\leq k-1⁠) the pairs of qk−jq_{k-j} with every state of smaller subscript are marked:
aja^{j} brings qk−jq_{k-j} to qkq_k and the other state to a state that does not accept, and no shorter word distinguishes them, because bb does not change the state and a word with fewer than jj letters aa leaves both states below qkq_k⁠.
So q0,q1q_0,q_1 is marked in round k−1k-1⁠, by the word ak−1a^{k-1}⁠: from q1q_1 it reaches qkq_k⁠, and from q0q_0 it reaches qk−1q_{k-1}⁠.
The pairs among qk,qk+1,qk+2q_k,q_{k+1},q_{k+2} are not marked, because from each of them every letter leads to an accepting state; the three merge, and k+1k+1 states remain.

Back to Minimization: Removing Unreachable States, or see the glossary.