Moore's algorithm outputs the minimal DFA full
This proof shows: correctness, termination.
Let M=(Q,Σ,δ,q0,F) be a complete DFA, and let R be the set of states reachable from q0. Line L1 computes R. For q∈R and a∈Σ, δ(q,a)∈R, so all the classes in lines L5 and L11 are defined.
Definitions. For i≥0, states p,q are i-equivalent, p∼iq, if δ∗(p,z)∈F⇔δ∗(q,z)∈F for every z∈Σ∗ with ∣z∣≤i. They are equivalent, p∼q, if this holds for every z. Each ∼i and ∼ is an equivalence relation, ∼i+1 refines ∼i, and p∼q if and only if p∼iq for every i.
Lemma 1 (recursion). For all p,q and i≥0: p∼i+1q if and only if p∼iq and δ(p,a)∼iδ(q,a) for each a∈Σ.
Proof. A word z with ∣z∣≤i+1 is either of length at most i, or of the form az′ with a∈Σ and ∣z′∣≤i; and every word az′ with ∣z′∣≤i has length at most i+1. For the second kind, δ∗(p,az′)=δ∗(δ(p,a),z′). So "p and q agree on all z with ∣z∣≤i+1" is the conjunction of "they agree on all z with ∣z∣≤i" and "for each a, δ(p,a) and δ(q,a) agree on all z′ with ∣z′∣≤i". □
Lemma 2 (invariant). Number the passes through lines L5 to L9 as rounds 1,2,…, and call the state after lines L2 and L3 round 0. After round i, for all p,q∈R: cls[p]=cls[q] if and only if p∼iq, and k is the number of classes of ∼i on R.
Proof. By induction on i. For i=0, p∼0q means that p and q agree on z=ε, that is, p∈F⇔q∈F, which is what line L2 records; line L3 counts the classes. For round i+1, line L5 sets sig[q]=(cls[q],cls[δ(q,a1)],…,cls[δ(q,aσ)]) with the classes of round i. Line L6 gives two states the same new number if and only if their signatures are equal. By the induction hypothesis, the signatures of p and q are equal if and only if p∼iq and δ(p,a)∼iδ(q,a) for each a, which by Lemma 1 holds if and only if p∼i+1q. Line L7 counts the distinct signatures, which is the number of classes of ∼i+1, and line L9 stores both. □
Lemma 3 (termination). The loop stops after at most ∣R∣ rounds. When it stops after round j, the partition cls is the partition of R by ∼.
Proof. Let ki be the number of classes of ∼i on R. Since ∼i+1 refines ∼i, ki+1≥ki, and ki+1=ki 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+1 with ki (Lemma 2). As long as the loop runs, k grows by at least 1 per round; it starts at k0≥1, since q0∈R, and never exceeds ∣R∣. So at most ∣R∣−1 rounds increase k, and the round after the last of them stops the loop.
Suppose the loop stops after round j, so ∼j=∼j−1 on R. We show ∼i=∼j−1 on R for each i≥j−1, by induction on i. Lemma 1 defines ∼i+1 from ∼i by a rule that does not depend on i, and it only looks at states of R when p,q∈R. So if ∼i=∼i−1 on R, then ∼i+1=∼i on R. Hence all ∼i with i≥j−1 agree on R, and since p∼q if and only if p∼iq for all i, the partition after round j is the partition of R by ∼. □
Theorem. The DFA M′ returned by line L11 is a complete DFA with L(M′)=L(M), and it has the least number of states among all complete DFAs for L(M).
Proof. By Lemma 3, cls[p]=cls[q] if and only if p∼q, for p,q∈R. The relation ∼ is compatible with the transitions: if p∼q, then δ(p,a)∼δ(q,a), because a word z that separated δ(p,a) from δ(q,a) would make az separate p from q. So δ′(cls[q],a)=cls[δ(q,a)] does not depend on the choice of q in the class, and δ′ is a complete function on {0,…,k−1}. Equivalent states agree on z=ε, so a class lies inside F or inside Q∖F, and F′ contains exactly the classes inside F.
By induction on ∣w∣, δ′∗(cls[q0],w)=cls[δ∗(q0,w)]: the case w=ε is trivial, and for w=va, δ′(cls[δ∗(q0,v)],a)=cls[δ(δ∗(q0,v),a)]=cls[δ∗(q0,va)]. So M′ accepts w if and only if the class of δ∗(q0,w) is in F′, if and only if δ∗(q0,w)∈F, if and only if w∈L(M).
Every class contains a reachable state q=δ∗(q0,x), and cls[q] is δ′∗(cls[q0],x), so every state of M′ is reachable. Two different classes cls[p]=cls[q] have p∼q, so some z has δ∗(p,z)∈F and δ∗(q,z)∈/F, or the other way round. By the same induction from p and q, z separates the two classes in M′. So 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). ■