#
# closure(R) returns the transitive closure of an acyclic relation R.
# If a linear extension X of R is known, then closure(R,X) does the same,
#  but more efficiently.
# If the last argument is 'table', then a down-table of relations is
#  returned: down[x] = { y : y < x }.
#
`posets/closure`:=proc(R) local n,X,down,x,y;
  n:=nargs; if args[n]='table' then n:=n-1 fi;
  if n>2 then ERROR(`invalid arguments`)
    elif n=2 and type(args[2],'list') then X:=args[2]
    else X:=map(op,posets['filter'](args[1..n])) fi;
  down:=`posets/downsets`(R,X);
  if args[nargs]='table' then op(down)
    else {seq(seq([y,x],y=down[x]),x=X)} fi;
end:
#
save `posets/closure`, cat(HomeLib,`/posets/closure.m`);
read cat(HomeLib,`/help/posets/closure`);
quit;

