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 , and its validity rule is the language
that is, the words that contain or contain : words with two equal adjacent letters.
Each of the two parts already has a validator, built by following progress in a pattern.
The validator for “contains ” remembers how much of the end of the word already matches:
(start, or the last letter is ), (the last letter is ) and ( found; an accepting trap state).
The validator for “contains ” is built the same way, with , as in the section Combine simple machines.
Think: build the transition table of the product automaton of and , 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:
advances () and returns to (except , which is a trap state), and acts symmetrically.
A pair accepts for “or” when at least one coordinate accepts.
Answer
There are pairs. The arrow marks the start state and the star the accepting states, the five in which or is one of the coordinates:
| State | on | on |
|---|---|---|
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 :
(start, no letter read), and (the last letter, and no pair found yet), and (a pair found; an accepting trap state).
| State | on | on |
|---|---|---|
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 . Two automata over the same alphabet are equivalent automata if they recognize the same language, 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 for “contains ” equivalent to the product automaton?
If not, give a word on which their answers differ.
Answer
No. The word contains , so the product accepts it, but it has no , so rejects it.
Every word that contains without shows that .
States that cannot be reached
Question 1. Which state of the product does no run starting at pass through?
Explain without searching the table, by what each coordinate remembers.
Hint
What does the coordinate say about the last letter? What does the coordinate say?
Answer
The state . The coordinate says that the last letter read is , and the coordinate says that it is ; the two cannot hold together.
In the table: no row leads to , and it is not the start state either.
A state of a DFA (here is the start state) is a reachable state if there is a word such that , that is, the run on 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
, , and . 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 or 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, , and , right below.
Let us check whether we can also merge and . From the letter leads to , which accepts, and from it leads to , which does not.
If we merge the two states, we will not know where to continue on :
the word must be accepted and the word 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 and of a DFA “the same”, so that we can merge them without changing the language?
Write a condition using and .
Hint
Candidate definitions: (1) and both accept or both do not; (2) for every letter , ; (3) the pair has no distinguishing word.
Which of them is right in the example of and , and which fits the five accepting states here?
The formal definition
Let be a DFA. A word is a distinguishing word for the pair of states if exactly one of the states and belongs to .
The states and are equivalent states, and we write , if they have no distinguishing word:
The empty word distinguishes every accepting state from every state that does not accept, because .
Candidate (1) is only the case , and candidate (2) is too strict:
from and from the letter leads to different states, but they have no distinguishing word.
Question 3. Find a distinguishing word for the pair and for the pair .
Answer
For the first pair : from it leads to , which accepts, and from to , which does not.
For the second pair : from to , which accepts, and from to , which does not.
Equivalence behaves like equality: every state is equivalent to itself; if then ; and if and then , because for every word we have .
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 is the class of , and we denote it .
Question 4. Is ? 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 that for every accepting state of the product, accepts.
Base: accepts. Step: if accepts, then , and every letter leads from an accepting state to an accepting state (the coordinate or 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 is , and distinguishes the two states that leads to.
For this we need to read a word in two parts, the first letter and then the rest, while is defined by the last letter.
Claim and proof: reading a word in two parts
Claim. For every state and every two words : .
In particular, for a letter : .
Proof by induction on the length of , with and fixed.
- Base, : , by the definition of on the empty word.
- Induction hypothesis: the claim holds for every word of length .
- Step, with and :
The first and third equalities are the definition of on the last letter, and the second is the induction hypothesis.
The case gives the conclusion, because .
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 ).
- Round : mark a pair not yet marked if there is a letter that leads it to a pair marked in an earlier round. The distinguishing word is 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 has length : the pair that the letter leads to was marked exactly in round , 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 with six states , start state and a single accepting state .
The names are numbered, because we do not yet know what each state remembers.
| State | on | on |
|---|---|---|
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 if the pair was not marked by the time the algorithm stops.
What is the language of the automaton?
? | |||||
? | ? | ||||
? | ? | ? | |||
? | ? | ? | ? | ||
? | ? | ? | ? | ? |
Answer: the rounds and the language
Round 0: the five pairs of with another state. Round 1: the letter leads and to , and to ; so the six pairs of a state from and a state from are marked, by the word .
The letter leads no state to , so it marks nothing in this round.
Round 2: leads to and to , and these two pairs were marked in round 1; so and are marked by the word .
The pair goes on to and on to , and the pair goes on to and on to ; neither reaches a marked pair.
Round 3: no new pair, and we stop.
The classes: , , , . Now we can say what each class remembers:
is “the word started with ” (a rejecting trap state), is “started with and the last letter is ”, and is “started with and the last letter is ”.
The language: the words that start with and end with .
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 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 distinguishes them.
If was marked in round , there is a letter that leads it to a pair marked in an earlier round, and by the induction hypothesis that pair has a distinguishing word .
By the claim on reading a word in two parts, and likewise for , so distinguishes from .
(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 and a distinguishing word of it of smallest length.
, because a pair of an accepting state and a state that does not accept is marked in round 0.
So , and by the claim on reading a word in two parts distinguishes from .
That pair has a distinguishing word shorter than , so by the choice of it was marked, say in round .
Round marked a pair, so the algorithm also ran round , and in it would have been marked if it had not been marked before.
This contradicts the assumption that 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 , eight states remained, that is, 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, 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 , and , from Question 3 and the example before it).
The pairs of the accepting states will not be marked (Question 4).
In round 2 there is no new pair, and we stop. Four classes: , , and , 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 from a class is the class of :
This is the quotient automaton. In the product:
| State | on | on |
|---|---|---|
If we call by the name , by the name , by the name and by the name , 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 we chose one state 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 be a DFA, and let be its quotient automaton:
its states are the classes, its start state is , its transition is , and the accepting classes are those whose states are in .
(a) The transition is well defined. Suppose and let be a letter.
For every word , , by the claim on reading a word in two parts, and likewise for .
Since , the word does not distinguish from , so does not distinguish from .
This holds for every , so : two states of the same class move to the same class.
(b) The accepting states are well defined. If then does not distinguish them, that is, .
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 the claim:
for every state , .
- Base, : both sides equal by the definition of .
- Induction hypothesis: the claim holds for every word of length .
- Step, with and :
The first and last equalities are the definition of , the second is the induction hypothesis, and the third is the definition of (well defined by (a)).
Conclusion. if and only if accepts, if and only if (by (c)) accepts, if and only if (by (b)) , if and only if .
Minimization: the full order
- Find the reachable states, and remove the rest.
- Fill in the table of pairs in rounds, until a round marks no pair.
- 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 we are given a DFA that counts the ‘s modulo 4:
states (the subscript is the remainder), start , the letter leads from to and the letter keeps the state.
The accepting states are and . Minimize the automaton and describe its language.
Hint
Which pairs are marked in round 0? Where does lead the pair , and was that pair marked?
Answer
All the states are reachable. In round 0 the pairs , , , are marked.
In round 1 there is no new pair: leads to and to , and neither pair was marked; keeps every pair in place.
The classes are and , and the quotient automaton has two states.
The language: the words with an even number of ‘s.
2. A validator for the language “ends in ” over was designed to remember the last two letters:
(start, no letter read), (one letter read), and (the last two letters).
From every state, the letter leads to the state of the last two letters after adding it (for example and ), and the only accepting state is .
Minimize the validator, and compare the result with the validator of exercise 2 in Designing a Deterministic Finite Automaton.
Hint
Which states lead on to ? What do they have in common?
Answer
All seven states are reachable. In round 0 every pair with in it is marked.
In round 1 every pair of a state from (ends in ) and a state from is marked, by the word .
In round 2 there is no new pair: within each of the two groups, leads both states to the group and leads both to the same group (to from the first group, and to from the second).
The classes are , and : three states, and after renaming this is the validator 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 ‘s modulo 3 (, raises the remainder by 1, keeps the state) with the accepting states (the number of ‘s is not divisible by 3):
accepts and does not, so is a distinguishing word for two accepting states.
4. For the language “at least letters ” over (for ) we are given a counter with the states :
in the state , letters have been read (and in at least ), the letter raises the subscript by 1 up to , the letter keeps the state, the start state is and the accepting states are .
In which round of table filling will the pair be marked, and what is its distinguishing word?
How many states will remain?
Hint
Fill in the table for : which pair is marked in round 1, and which in round 2?
Look for the pattern.
Answer
In round (for ) the pairs of with every state of smaller subscript are marked:
brings to and the other state to a state that does not accept, and no shorter word distinguishes them, because does not change the state and a word with fewer than letters leaves both states below .
So is marked in round , by the word : from it reaches , and from it reaches .
The pairs among are not marked, because from each of them every letter leads to an accepting state; the three merge, and states remain.
Back to Combining Automata, or see the glossary.