Lesson

Minimizing Automata by Table Filling

Two correct automata for the same language, one larger than the other: unreachable states, 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:
to find the states that cannot be reached and remove them; 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.

Two validators for the same rule

A form field accepts codes over {a,b}\{a,b\}⁠, and its validity rule is 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\}^*\}

that is, the words that contain aaaa or contain bbbb⁠: words with two equal adjacent letters.
Each of the two parts already has a validator, built by following progress in a pattern.
The validator AA for “contains aaaa” remembers how much of aaaa the end of the word already matches:
q0q_0 (start, or the last letter is bb⁠), qaq_a (the last letter is aa⁠) and qaaq_{aa} (aaaa found; an accepting trap state).
The validator BB for “contains bbbb” is built the same way, with p0,pb,pbbp_0,p_b,p_{bb}⁠, as in the section Combine simple machines.

Think: build the transition table of the product automaton of AA and BB⁠, with the set of accepting states for “or”.
How many states does it have, and how many of them accept?

Hint

Each letter moves each coordinate by its own automaton only:
aa advances qq (q0→qa→qaaq_0\to q_a\to q_{aa}⁠) and returns pp to p0p_0 (except pbbp_{bb}⁠, which is a trap state), and bb acts symmetrically.
A pair accepts for “or” when at least one coordinate accepts.

Answer

There are 3⋅3=93\cdot3=9 pairs. The arrow marks the start state and the star the accepting states, the five in which qaaq_{aa} or pbbp_{bb} is one of the coordinates:

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)(q0,pbb)(q_0,p_{bb})
(qa,pb)(q_a,p_b)(qaa,p0)(q_{aa},p_0)(q0,pbb)(q_0,p_{bb})
∗ (qaa,p0){*}\,(q_{aa},p_0)(qaa,p0)(q_{aa},p_0)(qaa,pb)(q_{aa},p_b)
∗ (qaa,pb){*}\,(q_{aa},p_b)(qaa,p0)(q_{aa},p_0)(qaa,pbb)(q_{aa},p_{bb})
∗ (q0,pbb){*}\,(q_0,p_{bb})(qa,pbb)(q_a,p_{bb})(q0,pbb)(q_0,p_{bb})
∗ (qa,pbb){*}\,(q_a,p_{bb})(qaa,pbb)(q_{aa},p_{bb})(q0,pbb)(q_0,p_{bb})
∗ (qaa,pbb){*}\,(q_{aa},p_{bb})(qaa,pbb)(q_{aa},p_{bb})(qaa,pbb)(q_{aa},p_{bb})
(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)

We can also design a validator for the same language directly, by what it must remember:
the last letter, and whether two equal adjacent letters have already been found.
This is a third machine on the same page, so its states use the letter rr⁠:
r0r_0 (start, no letter read), rar_a and rbr_b (the last letter, and no pair 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 validator designed directly: 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

Both validators are correct: both recognize exactly LL⁠. Two automata M1,M2M_1,M_2 over the same alphabet are equivalent automata if they recognize the same language, that is L(M1)=L(M2)L(M_1)=L(M_2)⁠:
on every word they give the same answer. The product and the validator are equivalent, but one has 9 states and the other 4.
The product is a general method that gives a correct validator without designing again, and it grows fast:
each additional validator multiplies the number of states, and every state is one more row in the transition table that the computer stores.
The question of this lesson: how do we get from the large automaton to a small equivalent one by a method, without designing again?

Check: is the validator AA for “contains aaaa” equivalent to the product automaton?
If not, give a word on which their answers differ.

Answer

No. The word bbbb contains bbbb⁠, so the product accepts it, but it has no aaaa⁠, so AA rejects it.
Every word that contains bbbb without aaaa shows that L(A)≠LL(A)\neq L⁠.

States that cannot be reached

Question 1. Which state of the product does no run starting at (q0,p0)(q_0,p_0) pass through?
Explain without searching the table, by what each coordinate remembers.

Hint

What does the coordinate qaq_a say about the last letter? What does the coordinate pbp_b say?

Answer

The state (qa,pb)(q_a,p_b)⁠. The coordinate qaq_a says that the last letter read is aa⁠, and the coordinate pbp_b says that it is bb⁠; the two cannot hold together.
In the table: no row leads to (qa,pb)(q_a,p_b)⁠, and it is not the start state either.

A state qq of a DFA M=(Q,Σ,δ,s,F)M=(Q,\Sigma,\delta,s,F) (here ss is the start state) is a reachable state if there is a word ww such that δ∗(s,w)=q\delta^*(s,w)=q⁠, that is, the run on ww ends in it.
Removing a state that is not reachable, together with the arrows that leave it, does not change the language:
the run on every word passes only through reachable states (each prefix of the word brings the run to one of them), so it is the same before and after the removal, and ends in the same state.
The automaton also stays complete, because every arrow from a reachable state leads to a reachable state.

We find the reachable states by a search in the graph: start from the start state, and add every state that an arrow leads to from a state already found, until no state is added.
In the product 8 states remain.

Question 2. In the validator designed directly, which word reaches each of the four states?
Does it have a state that is not reachable?

Answer

δ∗(r0,ε)=r0\delta^*(r_0,\varepsilon)=r_0⁠, δ∗(r0,a)=ra\delta^*(r_0,a)=r_a⁠, δ∗(r0,b)=rb\delta^*(r_0,b)=r_b and δ∗(r0,aa)=rpair\delta^*(r_0,aa)=r_{\mathrm{pair}}⁠. All four states are reachable.

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 xx⁠, δ(p,x)=δ(q,x)\delta(p,x)=\delta(q,x)⁠; (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.

Question 3. 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)⁠.

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 4. 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?

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,ux)=δ(δ∗(p,u),x)\delta^*(p,ux)=\delta(\delta^*(p,u),x)⁠, 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, and an automaton without a clear structure has no such short argument.
We need a method that finds all the pairs that have a distinguishing word.
The idea: a distinguishing word that starts with the letter xx is xwxw⁠, and ww distinguishes the two states that xx 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 xx⁠: δ∗(q,xw)=δ∗(δ(q,x),w)\delta^*(q,xw)=\delta^*\bigl(\delta(q,x),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=xu=x gives the conclusion, because δ∗(q,x)=δ(q,x)\delta^*(q,x)=\delta(q,x)⁠.

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 xx that leads it to a pair δ(p,x),δ(q,x)\delta(p,x),\delta(q,x) marked in an earlier round. The distinguishing word is xx 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.

The pairs are written in a triangular table, one cell per pair, and 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 5. 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.
What is the language of the automaton?

q0q_0q1q_1q2q_2q3q_3q4q_4
q1q_1
?abab
q2q_2
?bb
?bb
q3q_3
?ε\varepsilon
?ε\varepsilon
?ε\varepsilon
q4q_4
?abab
?≡\equiv
?bb
?ε\varepsilon
q5q_5
?bb
?bb
?≡\equiv
?ε\varepsilon
?bb
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) A marked pair has a distinguishing word. By strong induction on the number kk of the round in which the pair was marked.
In round 0 the pair is an accepting state and a state that does not accept, and ε\varepsilon distinguishes them.
If p,qp,q was marked in round k≥1k\geq1⁠, there is a letter xx that leads it to a pair δ(p,x),δ(q,x)\delta(p,x),\delta(q,x) marked in an earlier round, and by the induction hypothesis that pair has a distinguishing word ww⁠.
By the claim on reading a word in two parts, δ∗(p,xw)=δ∗(δ(p,x),w)\delta^*(p,xw)=\delta^*(\delta(p,x),w) and likewise for qq⁠, so xwxw distinguishes pp from qq⁠.

(2) A pair not marked when the algorithm stops has no distinguishing word. Suppose, for contradiction, that some such pairs have a distinguishing word, and choose among them a pair p,qp,q and a distinguishing word ww of it of smallest length.
w≠εw\neq\varepsilon⁠, because a pair of an accepting state and a state that does not accept is marked in round 0.
So w=xuw=xu⁠, and by the claim on reading a word in two parts uu distinguishes δ(p,x)\delta(p,x) from δ(q,x)\delta(q,x)⁠.
That pair has a distinguishing word shorter than ww⁠, so by the choice of ww it was marked, say in round jj⁠.
Round jj marked a pair, so the algorithm also ran round j+1j+1⁠, and in it p,qp,q would have been marked if it had not been marked before.
This contradicts the assumption that p,qp,q was not marked.

(3) The algorithm stops. Every round that is not the last marks at least one pair, and there are finitely many pairs.

Quotient automaton

Question 6. 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.
How many pairs will be marked in round 0, how many in round 1, and how many will not be marked at all?
What are the classes?

Answer

In round 0, 3⋅5=153\cdot5=15 pairs (a state that does not accept and an accepting state).
In round 1, the three pairs of the states that do not accept (the words aa⁠, bb and aa⁠, from Question 3 and the example before it).
The 5⋅42=10\tfrac{5\cdot4}{2}=10 pairs of the accepting states will not be marked (Question 4).
In round 2 there is no new pair, and we stop. Four classes: [(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.

We build an automaton whose states are the classes: the start state is the class of the start state, a class accepts if its states accept, and the transition on a letter xx from a class [q][q] is the class of δ(q,x)\delta(q,x)⁠:

δ′([q],x)=[δ(q,x)]\delta'\bigl([q],x\bigr)=\bigl[\delta(q,x)\bigr]

This is the quotient automaton. In the product:

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′M' be its quotient automaton:
its states are the classes, its start state is [s][s]⁠, its transition is δ′\delta'⁠, and the accepting classes are those whose states are in FF⁠.

(a) The transition is well defined. Suppose p≡qp\equiv q and let xx be a letter.
For every word ww⁠, δ∗(δ(p,x),w)=δ∗(p,xw)\delta^*(\delta(p,x),w)=\delta^*(p,xw)⁠, by the claim on reading a word in two parts, and likewise for qq⁠.
Since p≡qp\equiv q⁠, the word xwxw does not distinguish pp from qq⁠, so ww does not distinguish δ(p,x)\delta(p,x) from δ(q,x)\delta(q,x)⁠.
This holds for every ww⁠, so δ(p,x)≡δ(q,x)\delta(p,x)\equiv\delta(q,x)⁠: 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=uxw=ux with ∣u∣=n|u|=n and x∈Σx\in\Sigma⁠:
δ′∗([q],ux)=δ′(δ′∗([q],u),x)=δ′([δ∗(q,u)],x)=[δ(δ∗(q,u),x)]=[δ∗(q,ux)]\delta'^*([q],ux)=\delta'\bigl(\delta'^*([q],u),x\bigr)=\delta'\bigl([\delta^*(q,u)],x\bigr)=\bigl[\delta(\delta^*(q,u),x)\bigr]=\bigl[\delta^*(q,ux)\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)\delta'^*([s],w) accepts, if and only if (by (c)) [δ∗(s,w)][\delta^*(s,w)] accepts, 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, 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.

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.

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.

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 xx 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 Combining Automata, or see the glossary.