#
# A recursive isomorphism tester for vertex-colored directed graphs.
# Calling sequence: `posets/cisom`(H,nu,G,mu);
# The value NULL is returned if there is no isomorphism; otherwise,
# the return value *is* the isomorphism.
#
# We try five things in order:
#  1. Do the graphs have incompatible vertex partitions?
#  2. Is there a lucky isomorphism available immediately?
# Next use `posets/separate` to  repartition the vertices of G and H.
#  3. Do G and H have different separation invariants? 
#  4. Was the repartitioning non-trivial? (If so, recurse.)
# At this point, all singletons must have matching edges, and there must
# be one or more blocks of size >=2. So we delete singleton blocks from
# both graphs, and pick a smallest block B from the remainder of G.
#  5. Is there an isomorphism from H to G in which B[1] is the target of
#     any of the possible elements from the corresponding block of H?
# Set infolevel[isom]>=2 to see information about step 4 reported.
#
`posets/cisom`:=proc() local G,H,muG,muH,X,Y,i,j,r,pi,q;
  muG:=args[4]; muH:=args[2];
  if map(nops,muH)<>map(nops,muG) then RETURN() fi;
  G:=args[3]; H:=args[1];
  X:=map(op,muG); Y:=map(op,muH);
  pi:={seq(Y[i]=X[i],i=1..nops(X))};
  if subs(pi,H)=G then RETURN(pi) else r:=nops(muG) fi;
  muG:=`posets/separate`(G,muG,q);
  muH:=`posets/separate`(H,muH,q);
  if map(x->x[1],muG)<>map(x->x[1],muH) then RETURN() fi;
  muG:=map(x->x[2],muG); muH:=map(x->x[2],muH);
  if nops(muG)>r then RETURN(procname(H,muH,G,muG)) fi;
  X:=zip((A,B)->if nops(A)=1 then A[1]=B[1] fi,muH,muG);
  G:=`posets/cisom/restrict`(G,muG);
  H:=`posets/cisom/restrict`(H,muH);
  j:=`posets/cisom/smallest`(G[2],'r');
  userinfo(2,'isom',cat(`descending to a `,r,`-block`));
  G:=`posets/cisom/pick`(1,j,G);
  for i to r do
    pi:=procname(`posets/cisom/pick`(i,j,H),G);
    if pi<>NULL then RETURN({op(X),op(pi)}) fi;
  od; NULL
end:
#
# Restrict to the subgraph of G induced by vertices in blocks of size >=2,
# and return the new G and new mu (as an expression sequence).
#
`posets/cisom/restrict`:=proc(G,mu) local X,nu;
  nu:=map(x->if nops(x)>1 then x fi,mu); X:=map(op,nu);
  map(proc(x,y) if member(x[1],y) and member(x[2],y)
    then x fi end,G,X),nu;
end:
#
# Find the index of the smallest block of mu; assign its size to args[2].
# Note that mu will always be nonempty at this point, with blocks >= 2.
# We could bail out early upon finding a 2-block, but we don't bother.
#
`posets/cisom/smallest`:=proc(mu) local m,k,i,j;
  m:=nops(mu[1]); j:=1;
  for i from 2 to nops(mu) do
    k:=nops(mu[i]);
    if k<m then m:=k; j:=i fi
  od;
  assign(args[2],m); j
end:
#
# Replace the j-th block of mu with two blocks: a singleton consisting
# of the i-th member of the block, and the remainder of the block.
#
`posets/cisom/pick`:=proc(i,j,G,mu) local B;
  B:=mu[j]; G,subsop(j=({B[i]},subsop(i=NULL,B)),mu)
end:
#
save `posets/cisom`, `posets/cisom/restrict`, `posets/cisom/smallest`,
  `posets/cisom/pick`, cat(HomeLib,`/posets/cisom.m`);
quit;

