#
# covers(R) returns the set of covering pairs in an acyclic relation R.
# If a linear extension X of R is known, then covers(R,X) does the same,
#  but more efficiently.
# If the last argument is 'closed', then R is assumed to be transitively
#  closed, which also allows more efficiency.
#
`posets/covers`:=proc(R) local n,X,down,x,y; 
  n:=nargs; if args[n]='closed' then n:=n-1 fi;
  if n>1 and type(args[2],'list') then X:=args[2]
    else X:=map(op,posets['filter'](R)) fi;
  if n=nargs then down:=`posets/downsets`(R,X)
    else down:=`posets/coversets`(R,X) fi;
  for x in X do down[x]:=down[x] minus {seq(op(down[y]),y=down[x])} od;
  {seq(seq([y,x],y=down[x]),x=X)}
end: 
#
save `posets/covers`, cat(HomeLib,`/posets/covers.m`);
read cat(HomeLib,`/help/posets/covers`);
quit;

