We earlier looked at the equivalence relation Two states A,B of a DFA M are equivalent if the sets of strings accepted from A and B are equal. The number of equivalence classes is equal to the number of states of a smallest DFA equivalent to M. Merging the states which are in the same equivalence class gives this smallest machine. Thus, it would be nice if there exists a polynomial-time algorithm to compute these equivalence classes. This is a fairly standard problem in algorithmics. It can be solved using dynamic programming. Define Two states A,B of a DFA M are k-equivalent if the sets of strings of length k accepted from A and B are equal. The "0-equivalence" classes are simply the set of accepting states and the set of rejecting states. Once you have calculated the "i-equivalence" classes it is easy to calculate the "(i+1)-equivalence" classes. It is not hard to prove that as soon as "i-equivalence" classes = "(i+1)-equivalence" classes, then no further refinement is possible. These are the equivalence classes of a smallest DFA equivalent to M. If M has n states then the "n-equivalence" classes = "(n+1)-equivalence" classes. So the dynamic programming algorithm halts after at most n refinement steps.