#
# Given a directed graph G with vertex set X, canon(G,X) finds a 
# canonical relabeling of G. The vertex set of the relabeled graph
# will be 1,...,n for some n, and another graph (H,Y) will be isomorphic
# to (G,X) if and only if canon(G,X)=canon(H,Y).
#
# If G is acyclic, and the last argument is 'natural', produce a canonical
# labeling that is "natural" (i.e., if [i,j] is an edge, then i<j).
# Note we do not test whether G is acyclic in this case.
# If you try it on a G with cycles, you're on your own.
#
# An undocumented feature: we also allow G to be a vertex-colored digraph,
# with args[2] the partition of X into color classes (a list of sets).
#
# Might be nicer to return a poset "structure".
#
`posets/canon`:=proc(P) local Q,mu,pi,i,n;
  n:=nargs; if args[n]='natural' then n:=n-1 fi;
  if n=1 then mu:=[map(op,P)]
    elif type(args[2],'list(set)') then mu:=args[2]
    elif type(args[2],'integer') then mu:=[{$1..args[2]}]
    else mu:=[args[2]]
  fi;
  Q:=`posets/canorder`(P,mu,'all');
  if nargs=n then Q[1] else
    pi:=posets['filter'](Q[1],nops(Q[2]));
    pi:=map(x->op(sort([op(x)])),pi);
    subs({seq(pi[i]=i,i=1..nops(pi))},Q[1])
  fi;
end:
#
save `posets/canon`, cat(HomeLib,`/posets/canon.m`);
read cat(HomeLib,`/help/posets/canon`);
quit;

