Proofs: Hopcroft's DFA minimization algorithm
These are the human proofs of Hopcroft's DFA minimization algorithm, at full length.
Hopcroft's algorithm outputs the minimal DFA in O(σ n log n) time full
This proof shows: correctness, termination, complexity.
Let be a complete DFA, and let be the set of states reachable from , computed by line L1. All sets below are subsets of , and for . For and let ; line L7 computes from the lists of line L2. States are equivalent, , if for all .
The work list holds pairs (block, symbol). A block is a set that can change when it is split; a pair in always refers to the current contents of its block, and line L11 keeps this true by putting both halves of a split block into .
Part 1: no two equivalent states are separated
Invariant (I1). Every block of is a union of classes of .
Proof. Initially the blocks are and , and equivalent states agree on . Suppose (I1) holds, and line L9 splits a block by , where is a block and so a union of classes. If , then , because a word that separated from would make separate from . So and are both in or both outside it, and are both in or both outside it. Hence and are unions of classes.
Part 2: termination
Each split increases the number of blocks by one, and there are never more than blocks, so there are at most splits. Each split adds at most pairs to (line L11 adds one pair for if , and line L12 adds one pair otherwise). Line L4 adds at most pairs. Each pass of the loop removes one pair. So the loop runs at most times and stops.
Part 3: the final partition is stable
Say that is -stable for a set if every block of satisfies or . Three facts follow from the definition:
- (F1) is -stable for , since .
- (F2) If is -stable for , then every refinement of is -stable for , since each new block lies inside an old one.
- (F3) If is -stable for and for , then it is -stable for , and , since commutes with union, intersection and difference, and a block that lies inside or outside each of two sets lies inside or outside each of these combinations.
For a symbol , let be the family of the sets for which the current is -stable, and the family of the blocks with . Let be the closure of under union, intersection and difference.
Invariant (I2). Before each test of line L5, for every symbol , every block of is in .
Proof. Start. If has one block, it is . If with the smaller block, line L4 puts in for each , so and is a difference of and .
One pass. Let be removed by line L6, and let and be the partition and the work list after the pass. We check the three kinds of generators of the old and show that each of them is in the new family :
- is a generator of both.
- A set in is in by (F2), since refines .
- A block with : if is not split, it is still in . If is split into and , line L11 puts both halves into for , so is in .
- The removed pair : after the pass, every block of lies inside or outside it. A block that line L8 did not split already did; a split block becomes and . Here and are the sets of the moment line L7 ran, so the set is in , even if the block was split later in the pass.
So the old is contained in . Now take a block of . If it is a block of , it is in . Otherwise it is a half of a block of that was split into and . If was in when the split happened, line L11 put both halves into , so unless it was split again, which cannot happen in the same pass because each half lies inside or outside . Otherwise line L12 put the smaller half, say , into , so , and with . In both cases .
When the loop stops, , so and every block is in the closure of . By (F1) and (F3), is -stable for every set in this closure. So is -stable for every block, for every symbol : if are in one block, then and are in one block.
Part 4: the output is the minimal DFA
Lemma. At the end, states in one block are equivalent.
Proof. We show by induction on that for in one block, and are in one block. For this is the assumption. For , and are in one block by Part 3, and the induction hypothesis applies to them and . Each block lies inside or inside , because the first partition did and blocks only split. So .
With (I1), the blocks are exactly the classes of on . Line L13 builds the quotient: its transition is well defined by Part 3, a block is accepting when it lies in , and by induction on the quotient reaches the block of on , so it accepts . Its states are all reachable and pairwise inequivalent, so it is the minimal DFA by Part 3 of the uniqueness of the minimal DFA.
Part 5: the running time
Lines L1 and L2 take time. Line L13 takes time. A pass of the loop that removes costs , where and is the number of splits in the pass. The splits take time in total if each split moves the states of , which line L7 has already listed, into a new block, and the work-list updates take per split. Over the whole run there are at most splits and at most passes, so these terms are . It remains to bound over all passes.
Claim. For each state and symbol , at most removed pairs have .
Proof. Let and later be two consecutive removed pairs with in the set. We show , with the sizes at the moments of removal. When is removed, the block of has no pair with in . From then on, let be the block of at time ; it only shrinks. Consider the first moment after this when a pair with enters . It cannot come from line L11, which adds a pair for the half of only when a pair for the block of and the symbol is already in . So it comes from line L12, as the smaller half of a split block , and . From then until is removed, the set of the pair that contains can only shrink, by later splits with line L11. So . Also, line L4 and line L12 never add a pair that is already in , so the pairs in are distinct. All sets are non-empty and have at most states, so there are at most such pairs.
Hence
since for each . The total time is , and the space is for the lists , the blocks and the work list.