#
# extensions(P,X) lists all linear extensions of a poset P
#  with vertex set X.
# extensions(P,n) does the same, using X={1,...,n}.
# extensions(P) does the same, assuming there are no isolated vertices.
#
`posets/extensions`:=proc(P) local X,bot,Q,x,res;
  if nargs=1 then X:=map(op,P)
    elif type(args[2],'integer') then X:={$1..args[2]}
    else X:=args[2]
  fi;
  if nops(X)<2 then RETURN([[op(X)]]) fi;
  bot:=X minus map(e->op(2,e),P); res:=NULL;
  for x in bot do
    Q:=map(proc(e,y) if e[1]<>y then e fi end,P,x);
    res:=res,map((y,z)->[z,op(y)],procname(Q,X minus {x}),x)
  od;
  map(op,[res])
end:
#
save `posets/extensions`, cat(HomeLib,`/posets/extensions.m`);
read cat(HomeLib,`/help/posets/extensions`);
quit;

