DFA minimization
In automata theory, DFA minimization is the task of transforming a given deterministic finite automaton into an equivalent DFA that has a minimum number of states. Here, two DFAs are called equivalent if they recognize the same regular language. Several different algorithms accomplishing this task are known and described in standard textbooks on automata theory.
Minimal DFA
For each regular language, there also exists a minimal automaton that accepts it, that is, a DFA with a minimum number of states and this DFA is unique. The minimal DFA ensures minimal computational cost for tasks such as pattern matching.There are three classes of states that can be removed or merged from the original DFA without affecting the language it accepts.
- Unreachable states are the states that are not reachable from the initial state of the DFA, for any input string. These states can be removed.
- Dead states are the states from which no final state is reachable. These states can be removed unless the automaton is required to be complete.
- Nondistinguishable states are those that cannot be distinguished from one another for any input string. These states can be merged.
- remove dead and unreachable states,
- merge nondistinguishable states,
- optionally, re-create a single dead state if the resulting DFA is required to be complete.
Unreachable states
let reachable_states :=
let new_states :=
do while
unreachable_states := Q \ reachable_states
Assuming an efficient implementation of the state sets and operations on them, this algorithm can be implemented with time complexity, where is the number of states and is the number of transitions of the input automaton.
Unreachable states can be removed from the DFA without affecting the language that it accepts.
Nondistinguishable states
The following algorithms present various approaches to merging nondistinguishable states.Hopcroft's algorithm
One algorithm for merging the nondistinguishable states of a DFA, due to, is based on partition refinement, partitioning the DFA states into groups by their behavior. These groups represent equivalence classes of the Nerode congruence, whereby every two states are equivalent if they have the same behavior for every input sequence. That is, for every two states and that belong to the same block of the partition, and every input word, the transitions determined by should always take states and to either states that both accept or states that both reject. It should not be possible for to take to an accepting state and to a rejecting state or vice versa.The following pseudocode describes the form of the algorithm as given by Xu. Alternative forms have also been presented.
P :=
W :=
while do
choose and remove a set A from W
for each c in Σ do
let X be the set of states for which a transition on c leads to a state in A
for each set Y in P for which X ∩ Y is nonempty and Y \ X is nonempty do
replace Y in P by the two sets X ∩ Y and Y \ X
if Y is in W
replace Y in W by the same two sets
else
if |X ∩ Y| <= |Y \ X|
add X ∩ Y to W
else
add Y \ X to W
The algorithm starts with a partition that is too coarse: every pair of states that are equivalent according to the Nerode congruence belong to the same set in the partition, but pairs that are inequivalent might also belong to the same set. It gradually refines the partition into a larger number of smaller sets, at each step splitting sets of states into pairs of subsets that are necessarily inequivalent.
The initial partition is a separation of the states into two subsets of states that clearly do not have the same behavior as each other: the accepting states and the rejecting states. The algorithm then repeatedly chooses a set from the current partition and an input symbol, and splits each of the sets of the partition into two subsets: the subset of states that lead to on input symbol, and the subset of states that do not lead to. Since is already known to have different behavior than the other sets of the partition, the subsets that lead to also have different behavior than the subsets that do not lead to. When no more splits of this type can be found, the algorithm terminates.
Lemma. Given a fixed character c and an equivalence class Y that splits into equivalence classes B and C, only one of B or C is necessary to refine the whole partition.
Example: Suppose we have an equivalence class Y that splits into equivalence classes B and C. Suppose we also have classes D, E, and F; D and E have states with transitions into B on character c, while F has transitions into C on character c. By the Lemma, we can choose either B or C as the distinguisher, let's say B. Then the states of D and E are split by their transitions into B. But F, which doesn't point into B, simply doesn't split during the current iteration of the algorithm; it will be refined by other distinguisher.
Observation. All of B or C is necessary to split referring classes like D, E, and F correctly—subsets won't do.
The purpose of the outermost
if statement is to patch up W, the set of distinguishers. We see in the previous statement in the algorithm that Y has just been split. If Y is in W, it has just become obsolete as a means to split classes in future iterations. So Y must be replaced by both splits because of the Observation above. If Y is not in W, however, only one of the two splits, not both, needs to be added to W because of the Lemma above. Choosing the smaller of the two splits guarantees that the new addition to W is no more than half the size of Y; this is the core of the Hopcroft algorithm: how it gets its speed, as explained in the next paragraph.The worst case running time of this algorithm is, where is the number of states and is the size of the alphabet. This bound follows from the fact that, for each of the transitions of the automaton, the sets drawn from that contain the target state of the transition have sizes that decrease relative to each other by a factor of two or more, so each transition participates in of the splitting steps in the algorithm. The partition refinement data structure allows each splitting step to be performed in time proportional to the number of transitions that participate in it. This remains the most efficient algorithm known for solving the problem, and for certain distributions of inputs its average-case complexity is even better,.
Once Hopcroft's algorithm has been used to group the states of the input DFA into equivalence classes, the minimum DFA can be constructed by forming one state for each equivalence class. If is a set of states in, is a state in, and is an input character, then the transition in the minimum DFA from the state for, on input , goes to the set containing the state that the input automaton would go to from state on input. The initial state of the minimum DFA is the one containing the initial state of the input DFA, and the accepting states of the minimum DFA are the ones whose members are accepting states of the input DFA.
Moore's algorithm
Moore's algorithm for DFA minimization is due to. Like Hopcroft's algorithm, it maintains a partition that starts off separating the accepting from the rejecting states, and repeatedly refines the partition until no more refinements can be made. At each step, it replaces the current partition with the coarsest common refinement of partitions, one of which is the current one and the rest of which are the preimages of the current partition under the transition functions for each of the input symbols. The algorithm terminates when this replacement does not change the current partition. Its worst-case time complexity is : each step of the algorithm may be performed in time using a variant of radix sort to reorder the states so that states in the same set of the new partition are consecutive in the ordering, and there are at most steps since each one but the last increases the number of sets in the partition. The instances of the DFA minimization problem that cause the worst-case behavior are the same as for Hopcroft's algorithm. The number of steps that the algorithm performs can be much smaller than, so on average its performance is or even depending on the random distribution on automata chosen to model the algorithm's average-case behavior.Brzozowski's algorithm
Reversing the transitions of a non-deterministic finite automaton and switching initial and final states produces an NFA for the reversal of the original language. Converting this NFA to a DFA using the standard powerset construction leads to a DFA for the same reversed language. As observed, repeating this reversal and determinization a second time, again keeping only reachable states, produces the minimal DFA for the original language.The intuition behind the algorithm is this: determinizing the reverse automaton merges states that are nondistinguishable in the original automaton, but may produce several accepting states. In such case, when we reverse the automaton for the second time, these accepting states become initial, and thus the automaton will not be deterministic due to having multiple initial states. That is why we need to determinize it again, obtaining the minimal DFA.