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 for be complete DFAs with disjoint state sets, and . Let , , and let be on and on . Then on is and on is . For , "x accepts" means .
Call the pairs pushed by lines L2 and L9 the pushed pairs. Each has the form with , , and a string .
Part 1: termination and cost
Each call of UNION in lines L2 and L9 joins two different sets: in line L2 the sets of and 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 sets at the start, so there are at most calls. Each pushed pair follows one call, so at most pairs are pushed, and the loop of line L3, which pops one pair per pass, runs at most times. Each pass makes at most FIND calls in line L8. With union by size and path halving, a sequence of operations on elements takes time, so the total time is , and the space is .
Part 2: a reported string is a counterexample
Invariant (I1). Every pushed pair with string has and .
Proof. Line L2 pushes with . Line L9 pushes with , where with was pushed before; by (I1) for and the definition of , and .
If line L5 returns for the pair , then exactly one of and holds, so by (I1) exactly one of and holds. The string has length at most , because each pushed pair's string is one symbol longer than the string of the pair it came from, and the at most 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 be the final partition of , viewed as an equivalence relation. Let be the set of the pushed pairs, as unordered pairs of states.
Claim 1. is the smallest equivalence relation that contains .
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 with the edges pushed so far. The connected components of a graph are the classes of the smallest equivalence relation that contains its edges.
Claim 2. If , then accepts if and only if 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, means that a path exists with each . Acceptance is the same along each edge, so it is the same at and .
Claim 3. If , then for every .
Proof. First let be a pushed pair. When it was popped, line L8 compared the sets of and . 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 . In general, take the path of Claim 1 from to . Each edge gives , and transitivity gives .
Claim 4. For every , .
Proof. By induction on . For , by line L2. If , then Claim 3 with the symbol gives .
By Claims 4 and 2, for every , if and only if , that is, .
Conclusion
By Part 1 the algorithm stops after at most passes in time. If it returns "not equivalent" with , Part 2 shows that is in exactly one of the two languages, so the DFAs are not equivalent. If it returns "equivalent", Part 3 shows that . Since one of the two answers is always returned, the algorithm decides equivalence.