Lesson

Minimization: Removing Unreachable States

Two correct automata for the same language, one larger than the other: equivalent automata, states that cannot be reached, removing them without changing the language, and finding them by a search in the graph.

Last updated:


In this lesson we learn to make a deterministic finite automaton (DFA) smaller without changing its language:
when two automata are equivalent, that is, recognize the same language; how to find the states that cannot be reached, by a search in the graph step by step; and why removing them does not change the 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, and we write M1≡M2M_1\equiv M_2⁠:

M1≡M2  ⟺  L(M1)=L(M2)M_1\equiv M_2 \iff L(M_1)=L(M_2)

that is, 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.
We write RR for the set of reachable states:

R={q∈Q∣∃w∈Σ∗: δ∗(s,w)=q}R=\{q\in Q \mid \exists w\in\Sigma^*:\ \delta^*(s,w)=q\}

Removing the states that are not in RR⁠, together with the arrows that leave them, gives the DFA

MR=(R,Σ,δR,s,F∩R)M_R=(R,\Sigma,\delta_R,s,F\cap R)

where δR(q,σ)=δ(q,σ)\delta_R(q,\sigma)=\delta(q,\sigma) for every q∈Rq\in R and σ∈Σ\sigma\in\Sigma⁠: the same transitions, only from the states that remain.

A function is a set of pairs of an input and an output, and here the input is a pair of a state and a letter, as in the formal definition of a DFA; so δR\delta_R can also be written as a set:

δR={((q,σ),p)∈δ∣q∈R}\delta_R=\{((q,\sigma),p)\in\delta \mid q\in R\}

That is, δR\delta_R keeps exactly the pairs of δ\delta whose input state is in RR⁠, and it is the restriction of δ\delta to R×ΣR\times\Sigma⁠.
Also p∈Rp\in R⁠: this is part (a) of the proof that MRM_R is a complete DFA.

The removal does not change the language, that is L(MR)=L(M)L(M_R)=L(M)⁠: the run on every word passes only through states in RR⁠, so it is the same in both automata.

Claim and proof: removing the states that are not reachable

Claim. MRM_R is a complete DFA, and L(MR)=L(M)L(M_R)=L(M)⁠.

(a) δR\delta_R returns a state in RR⁠. Let q∈Rq\in R and σ∈Σ\sigma\in\Sigma⁠. Since q∈Rq\in R there is a word ww such that δ∗(s,w)=q\delta^*(s,w)=q⁠, and by the definition of δ∗\delta^* on the last letter δ∗(s,wσ)=δ(q,σ)\delta^*(s,w\sigma)=\delta(q,\sigma)⁠.
So δ(q,σ)∈R\delta(q,\sigma)\in R⁠, and δR\delta_R is a function from R×ΣR\times\Sigma to RR⁠: the automaton is complete.

(b) The run on a word. We prove by induction on the length of ww⁠:
δR∗(s,w)=δ∗(s,w)\delta_R^*(s,w)=\delta^*(s,w)⁠.

  • Base, w=εw=\varepsilon⁠: both sides equal ss⁠.
  • 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⁠:
δR∗(s,uσ)=δR(δR∗(s,u),σ)=δR(δ∗(s,u),σ)=δ(δ∗(s,u),σ)=δ∗(s,uσ)\delta_R^*(s,u\sigma)=\delta_R\bigl(\delta_R^*(s,u),\sigma\bigr)=\delta_R\bigl(\delta^*(s,u),\sigma\bigr)=\delta\bigl(\delta^*(s,u),\sigma\bigr)=\delta^*(s,u\sigma)

The first and last equalities are the definition of δ∗\delta^*⁠, the second is the induction hypothesis, and the third is the definition of δR\delta_R⁠, because δ∗(s,u)∈R\delta^*(s,u)\in R⁠.

Conclusion. w∈L(MR)w\in L(M_R) if and only if δR∗(s,w)∈F∩R\delta_R^*(s,w)\in F\cap R⁠, if and only if (by (b)) δ∗(s,w)∈F∩R\delta^*(s,w)\in F\cap R⁠, if and only if δ∗(s,w)∈F\delta^*(s,w)\in F (because δ∗(s,w)∈R\delta^*(s,w)\in R by the definition of RR⁠), if and only if w∈L(M)w\in L(M)⁠.

We find RR 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:

R0={s},Ri+1=Ri∪{δ(q,σ)∣q∈Ri, σ∈Σ}R_0=\{s\},\qquad R_{i+1}=R_i\cup\{\delta(q,\sigma) \mid q\in R_i,\ \sigma\in\Sigma\}

By induction on ii⁠, RiR_i is the set of the states δ∗(s,w)\delta^*(s,w) of the words ww of length at most ii⁠.
When Ri+1=RiR_{i+1}=R_i⁠, all the following sets are equal to it too, so R=RiR=R_i⁠.

There is no need to continue ii forever: the search stops by i=n−1i=n-1 at the latest, where n=∣Q∣n=|Q|⁠.
As long as Ri+1≠RiR_{i+1}\neq R_i⁠, the set Ri+1R_{i+1} is larger than RiR_i by at least one state.
Since ∣R0∣=1|R_0|=1⁠, if the sets grew at every step up to RiR_i⁠, then ∣Ri∣≥i+1|R_i|\geq i+1⁠; and since always ∣Ri∣≤n|R_i|\leq n⁠, they can grow at most n−1n-1 times.
So R=Rn−1R=R_{n-1}⁠: every reachable state is reached by a word of length at most n−1n-1⁠.
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.

Back to the question of this lesson: removing (qa,pb)(q_a,p_b) made the product smaller, from 9 states to 8, and the validator designed directly has 4 states, all of them reachable.
The gap closes only when we merge states of the product that “do the same thing”.
Removing the unreachable states and merging such states are together called the minimization of the automaton, and merging is taught in the lesson on merging states.

Practice

1. Over {a,b}\{a,b\} a DFA has six states q0,…,q5q_0,\dots,q_5⁠, start state q0q_0 and accepting states q2q_2 and q5q_5⁠:

Stateon $a$on $b$
→q0\to q_0q1q_1q0q_0
q1q_1q1q_1q2q_2
∗ q2{*}\,q_2q1q_1q0q_0
q3q_3q4q_4q2q_2
q4q_4q5q_5q3q_3
∗ q5{*}\,q_5q5q_5q5q_5

Write R0,R1,…R_0,R_1,\dots until the search stops, and build MRM_R⁠. Which accepting states remain, and what is the language of the automaton?

Hint

Which states lead to q3q_3⁠, to q4q_4 and to q5q_5⁠? Is any of them already in some RiR_i⁠?

Answer

R0={q0}R_0=\{q_0\}⁠, R1={q0,q1}R_1=\{q_0,q_1\} (since δ(q0,a)=q1\delta(q_0,a)=q_1 and δ(q0,b)=q0\delta(q_0,b)=q_0⁠), R2={q0,q1,q2}R_2=\{q_0,q_1,q_2\} (since δ(q1,b)=q2\delta(q_1,b)=q_2⁠), and R3=R2R_3=R_2⁠, since aa and bb lead from q2q_2 to q1q_1 and to q0q_0⁠.
So R={q0,q1,q2}R=\{q_0,q_1,q_2\}⁠; the search stops at i=2i=2⁠, well before the bound n−1=5n-1=5⁠.
Arrows into q3,q4,q5q_3,q_4,q_5 come only from q3,q4,q5q_3,q_4,q_5 themselves.

MRM_R has three states, with the same transitions from q0,q1,q2q_0,q_1,q_2⁠, and the single accepting state F∩R={q2}F\cap R=\{q_2\}⁠:
the accepting state q5q_5 was removed, because no word reaches it.
In MRM_R the state q1q_1 says that the last letter is aa⁠, and the state q2q_2 that the last two letters are abab⁠; so the language of MRM_R⁠, and by the claim also of the given automaton, is the words that end in abab⁠.

2. Prove or disprove: if all the states of a DFA AA and all the states of a DFA BB are reachable, then all the states of their product automaton are reachable.

Hint

Do the validators AA and BB for “contains aaaa” and “contains bbbb” have a state that is not reachable?

Answer

The claim is false. In AA all the states are reachable (ε\varepsilon reaches q0q_0⁠, aa reaches qaq_a and aaaa reaches qaaq_{aa}⁠), and so are those of BB (ε\varepsilon⁠, bb⁠, bbbb⁠).
But in their product the state (qa,pb)(q_a,p_b) is not reachable (Question 1):
a word on which the run of AA ends in qaq_a ends in aa⁠, and a word on which the run of BB ends in pbp_b ends in bb⁠.

3. The search for RR stops by i=n−1i=n-1 at the latest. Show that the bound cannot be improved:
for every n≥2n\geq2 find a DFA over {a,b}\{a,b\} with nn states in which Rn−2≠Rn−1R_{n-2}\neq R_{n-1}⁠.

Hint

For the search to take n−1n-1 steps, each step must add exactly one state.

Answer

A chain: the states q0,…,qn−1q_0,\dots,q_{n-1}⁠, start state q0q_0⁠, the letter aa leads from qiq_i to qi+1q_{i+1} (and from qn−1q_{n-1} to itself), and the letter bb keeps the state.
Any set of accepting states will do, for example {qn−1}\{q_{n-1}\}⁠. By induction on ii⁠, Ri={q0,…,qi}R_i=\{q_0,\dots,q_i\} for every i≤n−1i\leq n-1⁠, so Rn−2={q0,…,qn−2}R_{n-2}=\{q_0,\dots,q_{n-2}\} while Rn−1R_{n-1} also contains qn−1q_{n-1}⁠.
The shortest word that reaches qn−1q_{n-1} is an−1a^{n-1}⁠, of length exactly n−1n-1⁠.

Continue with the lesson on merging states, go back to Combining Automata, or see the glossary.