function M0 = minimize_dfsa(M0) % M1 is the smallest equivalent dfsa to M0 % see str2dfsa.m for basic dfsa definitions nQ = length(M0.Q); % the brute-force, debug version % initialize the \equiv_0 relation EQU = zeros(nQ); for p=1:nQ for q=p+1:nQ Mp = M0; Mp.q0 = p; Mq = M0; Mq.q0 = q; EQU(p,q) = equal_dfsa(Mp,Mq); end end % collapse equivalent states for p=1:nQ for q=p+1:nQ if EQU(p,q) M0 = collapse(M0,p,q); end end end M0 = remove_unreachable(M0); return function M = collapse(M,p,q) % everything that pointed to q must point to p [qq0,ss0] = get_ddparents(M,q); M = set_ddelta(M,qq0,ss0,repmat(p,[length(qq0),1])); % now just zero out q: M = set_ddelta(M,repmat(q,[length(M.ALF) 1]),M.ALF,zeros(length(M.ALF),1)); % take q out of F if it's there M.F = setdiff(M.F,q); return