Algorithm Search⌘K

Proofs: Moore's DFA minimization algorithm

These are the human proofs of Moore's DFA minimization algorithm, at full length.

Moore's algorithm outputs the minimal DFA full

This proof shows: correctness, termination.

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. Line L1 computes RR. For q∈Rq \in R and a∈Σa \in \Sigma, δ(q,a)∈R\delta(q, a) \in R, so all the classes in lines L5 and L11 are defined.

Definitions. For i≥0i \ge 0, states p,qp, q are ii-equivalent, p∼iqp \sim_i q, if δ∗(p,z)∈F⇔δ∗(q,z)∈F\delta^*(p, z) \in F \Leftrightarrow \delta^*(q, z) \in F for every z∈Σ∗z \in \Sigma^* with ∣z∣≤i|z| \le i. They are equivalent, p∼qp \sim q, if this holds for every zz. Each ∼i\sim_i and ∼\sim is an equivalence relation, ∼i+1\sim_{i+1} refines ∼i\sim_i, and p∼qp \sim q if and only if p∼iqp \sim_i q for every ii.

Lemma 1 (recursion). For all p,qp, q and i≥0i \ge 0: p∼i+1qp \sim_{i+1} q if and only if p∼iqp \sim_i q and δ(p,a)∼iδ(q,a)\delta(p, a) \sim_i \delta(q, a) for each a∈Σa \in \Sigma.

Proof. A word zz with ∣z∣≤i+1|z| \le i + 1 is either of length at most ii, or of the form az′a z' with a∈Σa \in \Sigma and ∣z′∣≤i|z'| \le i; and every word az′a z' with ∣z′∣≤i|z'| \le i has length at most i+1i + 1. For the second kind, δ∗(p,az′)=δ∗(δ(p,a),z′)\delta^*(p, a z') = \delta^*(\delta(p, a), z'). So "pp and qq agree on all zz with ∣z∣≤i+1|z| \le i + 1" is the conjunction of "they agree on all zz with ∣z∣≤i|z| \le i" and "for each aa, δ(p,a)\delta(p, a) and δ(q,a)\delta(q, a) agree on all z′z' with ∣z′∣≤i|z'| \le i". □\square

Lemma 2 (invariant). Number the passes through lines L5 to L9 as rounds 1,2,…1, 2, \ldots, and call the state after lines L2 and L3 round 0. After round ii, for all p,q∈Rp, q \in R: cls[p]=cls[q]\mathrm{cls}[p] = \mathrm{cls}[q] if and only if p∼iqp \sim_i q, and kk is the number of classes of ∼i\sim_i on RR.

Proof. By induction on ii. For i=0i = 0, p∼0qp \sim_0 q means that pp and qq agree on z=εz = \varepsilon, that is, p∈F⇔q∈Fp \in F \Leftrightarrow q \in F, which is what line L2 records; line L3 counts the classes. For round i+1i + 1, line L5 sets sig[q]=(cls[q],cls[δ(q,a1)],…,cls[δ(q,aσ)])\mathrm{sig}[q] = (\mathrm{cls}[q], \mathrm{cls}[\delta(q, a_1)], \ldots, \mathrm{cls}[\delta(q, a_\sigma)]) with the classes of round ii. Line L6 gives two states the same new number if and only if their signatures are equal. By the induction hypothesis, the signatures of pp and qq are equal if and only if p∼iqp \sim_i q and δ(p,a)∼iδ(q,a)\delta(p, a) \sim_i \delta(q, a) for each aa, which by Lemma 1 holds if and only if p∼i+1qp \sim_{i+1} q. Line L7 counts the distinct signatures, which is the number of classes of ∼i+1\sim_{i+1}, and line L9 stores both. □\square

Lemma 3 (termination). The loop stops after at most ∣R∣|R| rounds. When it stops after round jj, the partition cls\mathrm{cls} is the partition of RR by ∼\sim.

Proof. Let kik_i be the number of classes of ∼i\sim_i on RR. Since ∼i+1\sim_{i+1} refines ∼i\sim_i, ki+1≥kik_{i+1} \ge k_i, and ki+1=kik_{i+1} = k_i holds if and only if the two partitions are equal, because a refinement with the same number of blocks has the same blocks. Line L8 compares ki+1k_{i+1} with kik_i (Lemma 2). As long as the loop runs, kk grows by at least 1 per round; it starts at k0≥1k_0 \ge 1, since q0∈Rq_0 \in R, and never exceeds ∣R∣|R|. So at most ∣R∣−1|R| - 1 rounds increase kk, and the round after the last of them stops the loop.

Suppose the loop stops after round jj, so ∼j=∼j−1\sim_j = \sim_{j-1} on RR. We show ∼i=∼j−1\sim_{i} = \sim_{j-1} on RR for each i≥j−1i \ge j - 1, by induction on ii. Lemma 1 defines ∼i+1\sim_{i+1} from ∼i\sim_i by a rule that does not depend on ii, and it only looks at states of RR when p,q∈Rp, q \in R. So if ∼i=∼i−1\sim_i = \sim_{i-1} on RR, then ∼i+1=∼i\sim_{i+1} = \sim_i on RR. Hence all ∼i\sim_i with i≥j−1i \ge j - 1 agree on RR, and since p∼qp \sim q if and only if p∼iqp \sim_i q for all ii, the partition after round jj is the partition of RR by ∼\sim. □\square

Theorem. The DFA M′M' returned by line L11 is a complete DFA with L(M′)=L(M)L(M') = L(M), and it has the least number of states among all complete DFAs for L(M)L(M).

Proof. By Lemma 3, cls[p]=cls[q]\mathrm{cls}[p] = \mathrm{cls}[q] if and only if p∼qp \sim q, for p,q∈Rp, q \in R. The relation ∼\sim is compatible with the transitions: 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 δ′(cls[q],a)=cls[δ(q,a)]\delta'(\mathrm{cls}[q], a) = \mathrm{cls}[\delta(q, a)] does not depend on the choice of qq in the class, and δ′\delta' is a complete function on {0,…,k−1}\{0, \ldots, k - 1\}. Equivalent states agree on z=εz = \varepsilon, so a class lies inside FF or inside Q∖FQ \setminus F, and F′F' contains exactly the classes inside FF.

By induction on ∣w∣|w|, δ′∗(cls[q0],w)=cls[δ∗(q0,w)]\delta'^*(\mathrm{cls}[q_0], w) = \mathrm{cls}[\delta^*(q_0, w)]: the case w=εw = \varepsilon is trivial, and for w=vaw = va, δ′(cls[δ∗(q0,v)],a)=cls[δ(δ∗(q0,v),a)]=cls[δ∗(q0,va)]\delta'(\mathrm{cls}[\delta^*(q_0, v)], a) = \mathrm{cls}[\delta(\delta^*(q_0, v), a)] = \mathrm{cls}[\delta^*(q_0, va)]. So M′M' accepts ww if and only if the class of δ∗(q0,w)\delta^*(q_0, w) is in F′F', if and only if δ∗(q0,w)∈F\delta^*(q_0, w) \in F, if and only if w∈L(M)w \in L(M).

Every class contains a reachable state q=δ∗(q0,x)q = \delta^*(q_0, x), and cls[q]\mathrm{cls}[q] is δ′∗(cls[q0],x)\delta'^*(\mathrm{cls}[q_0], x), so every state of M′M' is reachable. Two different classes cls[p]≠cls[q]\mathrm{cls}[p] \neq \mathrm{cls}[q] have p≁qp \not\sim q, so some zz has δ∗(p,z)∈F\delta^*(p, z) \in F and δ∗(q,z)∉F\delta^*(q, z) \notin F, or the other way round. By the same induction from pp and qq, zz separates the two classes in M′M'. So M′M' has all its states reachable and no two equivalent states, and by Part 3 of the uniqueness of the minimal DFA, it is the minimal DFA for L(M)L(M). ■\blacksquare

Back to Moore's DFA minimization algorithm