#
# Compute the "fast discriminant" of a directed graph G with vertex
# partition mu: this is the coarsest refinement nu of mu such that
# for all vertices x in block i of nu,
#   the number of edges x -> block j depends only on i and j, and
#   the number of edges x <- block j depends only on i and j.
#
# The return value nu is a canonically ordered vertex partition,
# with the ordering determined by the values of the above numbers.
# If f:(G,mu) -> (H,nu) is an isomorphism of colored graphs, then f maps
# the fast discriminant of (G,mu) to the fast discriminant of (H,nu).
#
# Note: even though mu=[{}] is not a valid vertex partition, it is
# useful to allow calling procedures to provide this as input.
#
`posets/fastdisc`:=proc() local G,X,Xold,Y,mu,n;
  G:=args[1]; mu:=args[2]; X:={};
  do
    Xold:=X; n:=nops(mu);
    X:={op(map(x->if nops(x)=1 then op(x) fi,mu))};
    if nops(X)=n then break fi;
    mu:=`posets/separate`(G,mu);
    if nops(mu)=n then break
      elif nops(X)>nops(Xold) then
        Y:=X minus Xold;
        G:=map(proc(x,y) if not (member(x[1],y) or member(x[2],y))
          then x fi end,G,Y)
    fi
  od; mu
end:
#
save `posets/fastdisc`, cat(HomeLib,`/posets/fastdisc.m`);
quit;

