#
# Canonically order the vertices of a colored directed graph.
# The input should be (G,nu) or (G,nu,flag), where G is a set of edges
# and nu is a vertex partition (a list of sets).
#
# In the second form, the return value is a triple [Gc, w, pg], where
# Gc is the canonical relabeling of G,
#  w is the canonical ordering of its vertices, and
# pg is a list of generators for the autgroup of (G,nu).
#
# Without a flag, we simply return the canonical ordering w.
# (This is for legacy reasons: I have code outside the package that
# depends on this syntax. It may make sense to dump this option in
# the future, especially if we revisit the tie-breaking rules.)
#
# The strategy is to use mu=fastdisc(G,nu) as a first approximation to the
# canonical ordering. Working on the first non-singleton block B of mu,
# we break B into Aut(G,nu)-orbits O1, O2,... and decide which one should
# be considered "first". The decision is made by selecting a point from
# each orbit, color-isolating it, and then recursively generating the
# canonical ordering of this color-refinement. We get one candidate
# ordering w_i for each orbit O_i, and from these, we use a lexorder
# tie-breaker to decide which one is best for (G,nu).
#
# We could be sloppy about the orbit partition--if we select more than
# one point from an O_i, then the corresponding canonical relabelings are
# harmlessly identical, and even yield automorphisms of (G,nu) as a bonus.
# However, in terms of costliness, cisom << autgroup << canorder, so it is
# better to unsloppily minimize the recursive calls to canorder.
#
`posets/canorder`:=proc(G) local mu,k,i,Gc,Hc,alive,pg,nu;
  mu:=`posets/fastdisc`(G,args[2]);
  for k to nops(mu) while nops(mu[k])=1 do od;
  if k>nops(mu) then mu:=map(op,mu);
    Gc:=[subs({seq(mu[i]=i,i=1..nops(mu))},G),mu,[]]
  else
    i:=mu[k][1]; alive:=subsop(1=NULL,mu[k]);
    mu:=subsop(k=({i},alive),mu);
    Gc:=procname(G,mu,0);
    pg:=`posets/buildgrp`(Gc[3],G,mu,k);
    alive:=alive minus `posets/orbit`(pg,i);
    while nops(alive)>0 do
      nu:=subs({i=alive[1],alive[1]=i},mu);
      Hc:=procname(G,nu,0);
      if `posets/canorder/prefer`(Hc[1],Gc[1]) then Gc:=Hc fi;
      alive:=alive minus `posets/orbit`(pg,alive[1])
    od;
    Gc:=subsop(3=pg,Gc)
  fi;
  if nargs>2 then Gc else Gc[2] fi
end:
#
# Do we prefer to replace the canonical labeling G with H?
# Might be worth looking for alternatives that produce nicer results.
#
`posets/canorder/prefer`:=proc(H,G) local fG,fH;
  fG:=`posets/canorder/first`(G minus H);
  fH:=`posets/canorder/first`(H minus G);
  evalb(fH[1]<fG[1] or (fH[1]=fG[1] and fH[2]<fG[2]))
end:
#
# From edges [i,j] of A, first minimize max(i,j), and
# among those, minimize j-i. (We know that i,j are posint).
#
`posets/canorder/first`:=proc(A) local f,u,m,d;
 f:=[infinity,0];
 for u in A do
   m:=max(u[1],u[2]);
   if m<f[1] then f:=[m,u[2]-u[1]]
     elif m=f[1] then d:=u[2]-u[1];
       if d<f[2] then f:=[m,d] fi
   fi
 od; f
end: 
#
save `posets/canorder`, `posets/canorder/prefer`, `posets/canorder/first`,
  cat(HomeLib,`/posets/canorder.m`);
quit;

