Algorithm Search⌘K

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 M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) be a complete DFA, and let RR be the set of states reachable from q0q_0, computed by line L1. All sets below are subsets of RR, and δ(q,a)∈R\delta(q, a) \in R for q∈Rq \in R. For S⊆RS \subseteq R and a∈Σa \in \Sigma let δa−1(S)={q∈R:δ(q,a)∈S}\delta_a^{-1}(S) = \{ q \in R : \delta(q, a) \in S \}; line L7 computes X=δa−1(S)X = \delta_a^{-1}(S) from the lists prea\mathrm{pre}_a of line L2. States p,qp, q are equivalent, p∼qp \sim q, if δ∗(p,z)∈F⇔δ∗(q,z)∈F\delta^*(p, z) \in F \Leftrightarrow \delta^*(q, z) \in F for all z∈Σ∗z \in \Sigma^*.

The work list WW holds pairs (block, symbol). A block is a set that can change when it is split; a pair in WW always refers to the current contents of its block, and line L11 keeps this true by putting both halves of a split block into WW.

Part 1: no two equivalent states are separated

Invariant (I1). Every block of PP is a union of classes of ∼\sim.

Proof. Initially the blocks are F∩RF \cap R and R∖FR \setminus F, and equivalent states agree on z=εz = \varepsilon. Suppose (I1) holds, and line L9 splits a block BB by X=δa−1(S)X = \delta_a^{-1}(S), where SS is a block and so a union of classes. If p∼qp \sim q, then δ(p,a)∼δ(q,a)\delta(p, a) \sim \delta(q, a), because a word zz that separated δ(p,a)\delta(p, a) from δ(q,a)\delta(q, a) would make azaz separate pp from qq. So δ(p,a)\delta(p, a) and δ(q,a)\delta(q, a) are both in SS or both outside it, and p,qp, q are both in XX or both outside it. Hence B∩XB \cap X and B∖XB \setminus X are unions of classes. □\square

Part 2: termination

Each split increases the number of blocks by one, and there are never more than ∣R∣|R| blocks, so there are at most ∣R∣−1|R| - 1 splits. Each split adds at most σ\sigma pairs to WW (line L11 adds one pair for cc if (B,c)∈W(B, c) \in W, and line L12 adds one pair otherwise). Line L4 adds at most σ\sigma pairs. Each pass of the loop removes one pair. So the loop runs at most σ∣R∣\sigma |R| times and stops.

Part 3: the final partition is stable

Say that PP is aa-stable for a set SS if every block BB of PP satisfies B⊆δa−1(S)B \subseteq \delta_a^{-1}(S) or B∩δa−1(S)=∅B \cap \delta_a^{-1}(S) = \emptyset. Three facts follow from the definition:

  • (F1) PP is aa-stable for RR, since δa−1(R)=R\delta_a^{-1}(R) = R.
  • (F2) If PP is aa-stable for SS, then every refinement of PP is aa-stable for SS, since each new block lies inside an old one.
  • (F3) If PP is aa-stable for SS and for TT, then it is aa-stable for S∪TS \cup T, S∩TS \cap T and S∖TS \setminus T, since δa−1\delta_a^{-1} 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 cc, let Sc\mathcal{S}_c be the family of the sets for which the current PP is cc-stable, and Wc\mathcal{W}_c the family of the blocks CC with (C,c)∈W(C, c) \in W. Let Bc\mathcal{B}_c be the closure of {R}∪Sc∪Wc\{R\} \cup \mathcal{S}_c \cup \mathcal{W}_c under union, intersection and difference.

Invariant (I2). Before each test of line L5, for every symbol cc, every block of PP is in Bc\mathcal{B}_c.

Proof. Start. If PP has one block, it is RR. If P={S,R∖S}P = \{S, R \setminus S\} with SS the smaller block, line L4 puts (S,c)(S, c) in WW for each cc, so S∈WcS \in \mathcal{W}_c and R∖SR \setminus S is a difference of RR and SS.

One pass. Let (S,a)(S, a) be removed by line L6, and let P′P' and W′W' be the partition and the work list after the pass. We check the three kinds of generators of the old Bc\mathcal{B}_c and show that each of them is in the new family Bc′\mathcal{B}'_c:

  • RR is a generator of both.
  • A set in Sc\mathcal{S}_c is in Sc′\mathcal{S}'_c by (F2), since P′P' refines PP.
  • A block C∈WcC \in \mathcal{W}_c with (C,c)≠(S,a)(C, c) \neq (S, a): if CC is not split, it is still in Wc′\mathcal{W}'_c. If CC is split into C1C_1 and C2C_2, line L11 puts both halves into WW for cc, so C=C1∪C2C = C_1 \cup C_2 is in Bc′\mathcal{B}'_c.
  • The removed pair (S,a)(S, a): after the pass, every block BB of P′P' lies inside X=δa−1(S)X = \delta_a^{-1}(S) or outside it. A block that line L8 did not split already did; a split block becomes B∩XB \cap X and B∖XB \setminus X. Here XX and SS are the sets of the moment line L7 ran, so the set SS is in Sa′\mathcal{S}'_a, even if the block SS was split later in the pass.

So the old Bc\mathcal{B}_c is contained in Bc′\mathcal{B}'_c. Now take a block B′B' of P′P'. If it is a block of PP, it is in Bc⊆Bc′\mathcal{B}_c \subseteq \mathcal{B}'_c. Otherwise it is a half of a block BB of PP that was split into B1B_1 and B2B_2. If (B,c)(B, c) was in WW when the split happened, line L11 put both halves into WW, so B′∈Wc′B' \in \mathcal{W}'_c unless it was split again, which cannot happen in the same pass because each half lies inside or outside XX. Otherwise line L12 put the smaller half, say B1B_1, into WW, so B1∈Wc′B_1 \in \mathcal{W}'_c, and B2=B∖B1B_2 = B \setminus B_1 with B∈Bc⊆Bc′B \in \mathcal{B}_c \subseteq \mathcal{B}'_c. In both cases B′∈Bc′B' \in \mathcal{B}'_c. □\square

When the loop stops, W=∅W = \emptyset, so Wc=∅\mathcal{W}_c = \emptyset and every block is in the closure of {R}∪Sc\{R\} \cup \mathcal{S}_c. By (F1) and (F3), PP is cc-stable for every set in this closure. So PP is cc-stable for every block, for every symbol cc: if p,qp, q are in one block, then δ(p,c)\delta(p, c) and δ(q,c)\delta(q, c) 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 ∣z∣|z| that for p,qp, q in one block, δ∗(p,z)\delta^*(p, z) and δ∗(q,z)\delta^*(q, z) are in one block. For z=εz = \varepsilon this is the assumption. For z=cz′z = c z', δ(p,c)\delta(p, c) and δ(q,c)\delta(q, c) are in one block by Part 3, and the induction hypothesis applies to them and z′z'. Each block lies inside FF or inside R∖FR \setminus F, because the first partition did and blocks only split. So δ∗(p,z)∈F⇔δ∗(q,z)∈F\delta^*(p, z) \in F \Leftrightarrow \delta^*(q, z) \in F. □\square

With (I1), the blocks are exactly the classes of ∼\sim on RR. Line L13 builds the quotient: its transition [q]→a[δ(q,a)][q] \xrightarrow{a} [\delta(q, a)] is well defined by Part 3, a block is accepting when it lies in FF, and by induction on ∣w∣|w| the quotient reaches the block of δ∗(q0,w)\delta^*(q_0, w) on ww, so it accepts L(M)L(M). 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 O(σn)O(\sigma n) time. Line L13 takes O(σn)O(\sigma n) time. A pass of the loop that removes (S,a)(S, a) costs O(1+∣X∣+σ⋅s)O(1 + |X| + \sigma \cdot s), where ∣X∣=∑r∈S∣prea(r)∣|X| = \sum_{r \in S} |\mathrm{pre}_a(r)| and ss is the number of splits in the pass. The splits take O(∣X∣)O(|X|) time in total if each split moves the states of B∩XB \cap X, which line L7 has already listed, into a new block, and the work-list updates take O(σ)O(\sigma) per split. Over the whole run there are at most n−1n - 1 splits and at most σn\sigma n passes, so these terms are O(σn)O(\sigma n). It remains to bound ∑∣X∣\sum |X| over all passes.

Claim. For each state rr and symbol aa, at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 removed pairs (S,a)(S, a) have r∈Sr \in S.

Proof. Let (S,a)(S, a) and later (S′,a)(S', a) be two consecutive removed pairs with rr in the set. We show ∣S′∣≤∣S∣/2|S'| \le |S|/2, with the sizes at the moments of removal. When (S,a)(S, a) is removed, the block SS of rr has no pair with aa in WW. From then on, let BtB_t be the block of rr at time tt; it only shrinks. Consider the first moment after this when a pair (C,a)(C, a) with r∈Cr \in C enters WW. It cannot come from line L11, which adds a pair for the half of rr only when a pair for the block of rr and the symbol aa is already in WW. So it comes from line L12, as the smaller half of a split block BtB_t, and ∣C∣≤∣Bt∣/2≤∣S∣/2|C| \le |B_t|/2 \le |S|/2. From then until (S′,a)(S', a) is removed, the set of the pair that contains rr can only shrink, by later splits with line L11. So ∣S′∣≤∣S∣/2|S'| \le |S|/2. Also, line L4 and line L12 never add a pair that is already in WW, so the pairs in WW are distinct. All sets are non-empty and have at most nn states, so there are at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 such pairs. □\square

Hence

∑passes∣X∣=∑(S,a) removed ∑r∈S∣prea(r)∣≤(⌊log⁡2n⌋+1)∑a∈Σ∑r∈R∣prea(r)∣≤(⌊log⁡2n⌋+1) σn,\sum_{\text{passes}} |X| = \sum_{(S, a) \text{ removed}} \ \sum_{r \in S} |\mathrm{pre}_a(r)| \le (\lfloor \log_2 n \rfloor + 1) \sum_{a \in \Sigma} \sum_{r \in R} |\mathrm{pre}_a(r)| \le (\lfloor \log_2 n \rfloor + 1)\, \sigma n,

since ∑r∣prea(r)∣=∣R∣≤n\sum_r |\mathrm{pre}_a(r)| = |R| \le n for each aa. The total time is O(σnlog⁡n)O(\sigma n \log n), and the space is O(σn)O(\sigma n) for the lists prea\mathrm{pre}_a, the blocks and the work list. ■\blacksquare

Back to Hopcroft's DFA minimization algorithm