Algorithm Search⌘K

Proofs: Hopcroft-Karp equivalence algorithm

These are the human proofs of Hopcroft-Karp equivalence algorithm, at full length.

The Hopcroft-Karp algorithm decides DFA equivalence full

This proof shows: correctness, termination, complexity.

Let Mi=(Qi,Σ,δi,qi,Fi)M_i = (Q_i, \Sigma, \delta_i, q_i, F_i) for i=1,2i = 1, 2 be complete DFAs with disjoint state sets, n=∣Q1∣+∣Q2∣n = |Q_1| + |Q_2| and σ=∣Σ∣\sigma = |\Sigma|. Let Q=Q1∪Q2Q = Q_1 \cup Q_2, F=F1∪F2F = F_1 \cup F_2, and let δ:Q×Σ→Q\delta : Q \times \Sigma \to Q be δ1\delta_1 on Q1Q_1 and δ2\delta_2 on Q2Q_2. Then δ∗\delta^* on Q1Q_1 is δ1∗\delta_1^* and on Q2Q_2 is δ2∗\delta_2^*. For x∈Qx \in Q, "x accepts" means x∈Fx \in F.

Call the pairs pushed by lines L2 and L9 the pushed pairs. Each has the form (p,r)(p, r) with p∈Q1p \in Q_1, r∈Q2r \in Q_2, and a string ww.

Part 1: termination and cost

Each call of UNION in lines L2 and L9 joins two different sets: in line L2 the sets of q1q_1 and q2q_2 are different singletons, and in line L9 line L8 has just checked that the sets differ. So each call reduces the number of sets by one. There are nn sets at the start, so there are at most n−1n - 1 calls. Each pushed pair follows one call, so at most n−1n - 1 pairs are pushed, and the loop of line L3, which pops one pair per pass, runs at most n−1n - 1 times. Each pass makes at most 2σ2\sigma FIND calls in line L8. With union by size and path halving, a sequence of mm operations on nn elements takes O(m α(n))O(m\,\alpha(n)) time, so the total time is O(σn α(n))O(\sigma n\,\alpha(n)), and the space is O(n)O(n).

Part 2: a reported string is a counterexample

Invariant (I1). Every pushed pair (p,r)(p, r) with string ww has p=δ1∗(q1,w)p = \delta_1^*(q_1, w) and r=δ2∗(q2,w)r = \delta_2^*(q_2, w).

Proof. Line L2 pushes (q1,q2)(q_1, q_2) with ε\varepsilon. Line L9 pushes (δ1(p,a),δ2(r,a))(\delta_1(p, a), \delta_2(r, a)) with wawa, where (p,r)(p, r) with ww was pushed before; by (I1) for (p,r)(p, r) and the definition of δ∗\delta^*, δ1(p,a)=δ1∗(q1,wa)\delta_1(p, a) = \delta_1^*(q_1, wa) and δ2(r,a)=δ2∗(q2,wa)\delta_2(r, a) = \delta_2^*(q_2, wa). □\square

If line L5 returns ww for the pair (p,r)(p, r), then exactly one of p∈F1p \in F_1 and r∈F2r \in F_2 holds, so by (I1) exactly one of w∈L(M1)w \in L(M_1) and w∈L(M2)w \in L(M_2) holds. The string has length at most n−2n - 2, because each pushed pair's string is one symbol longer than the string of the pair it came from, and the at most n−1n - 1 pushed pairs form a tree rooted at the first pair.

Part 3: if no string is reported, the DFAs are equivalent

Suppose line L10 is reached, and let ≡\equiv be the final partition of QQ, viewed as an equivalence relation. Let EE be the set of the pushed pairs, as unordered pairs of states.

Claim 1. ≡\equiv is the smallest equivalence relation that contains EE.

Proof. Each UNION joins the sets of the two states of one pushed pair, and the partition changes only by UNION. By induction on the number of UNION calls, the partition is always the set of connected components of the graph on QQ with the edges EE pushed so far. The connected components of a graph are the classes of the smallest equivalence relation that contains its edges. □\square

Claim 2. If x≡yx \equiv y, then xx accepts if and only if yy accepts.

Proof. Every pushed pair was popped, since the stack is empty at line L10, and it passed line L5, so its two states agree on acceptance. By Claim 1, x≡yx \equiv y means that a path x=z0,z1,…,zk=yx = z_0, z_1, \ldots, z_k = y exists with each {zj−1,zj}∈E\{z_{j-1}, z_j\} \in E. Acceptance is the same along each edge, so it is the same at xx and yy. □\square

Claim 3. If x≡yx \equiv y, then δ(x,a)≡δ(y,a)\delta(x, a) \equiv \delta(y, a) for every a∈Σa \in \Sigma.

Proof. First let {x,y}={p,r}\{x, y\} = \{p, r\} be a pushed pair. When it was popped, line L8 compared the sets of δ(p,a)\delta(p, a) and δ(r,a)\delta(r, a). If they were different, line L9 merged them; if not, they were already in one set. Sets only grow by merging, so at the end δ(p,a)≡δ(r,a)\delta(p, a) \equiv \delta(r, a). In general, take the path z0,…,zkz_0, \ldots, z_k of Claim 1 from xx to yy. Each edge gives δ(zj−1,a)≡δ(zj,a)\delta(z_{j-1}, a) \equiv \delta(z_j, a), and transitivity gives δ(x,a)≡δ(y,a)\delta(x, a) \equiv \delta(y, a). □\square

Claim 4. For every w∈Σ∗w \in \Sigma^*, δ1∗(q1,w)≡δ2∗(q2,w)\delta_1^*(q_1, w) \equiv \delta_2^*(q_2, w).

Proof. By induction on ∣w∣|w|. For w=εw = \varepsilon, q1≡q2q_1 \equiv q_2 by line L2. If δ1∗(q1,w)≡δ2∗(q2,w)\delta_1^*(q_1, w) \equiv \delta_2^*(q_2, w), then Claim 3 with the symbol aa gives δ1∗(q1,wa)≡δ2∗(q2,wa)\delta_1^*(q_1, wa) \equiv \delta_2^*(q_2, wa). □\square

By Claims 4 and 2, for every ww, δ1∗(q1,w)∈F1\delta_1^*(q_1, w) \in F_1 if and only if δ2∗(q2,w)∈F2\delta_2^*(q_2, w) \in F_2, that is, L(M1)=L(M2)L(M_1) = L(M_2).

Conclusion

By Part 1 the algorithm stops after at most n−1n - 1 passes in O(σn α(n))O(\sigma n\,\alpha(n)) time. If it returns "not equivalent" with ww, Part 2 shows that ww is in exactly one of the two languages, so the DFAs are not equivalent. If it returns "equivalent", Part 3 shows that L(M1)=L(M2)L(M_1) = L(M_2). Since one of the two answers is always returned, the algorithm decides equivalence. ■\blacksquare

Back to Hopcroft-Karp equivalence algorithm