
# If U and V are two sets of inversion edges in the covering
# relation of some poset P, decide if U and V are shift-equivalent.

shift_equiv:=proc(P,U,V) local P0,F,Q,e,i,a;
  P0:=`shift_equiv/bound`(P);
  F:=map(op,posets['filter'](P0)); Q:=table();
  for e in P0 do Q[e[2]]:=e[1] od;
  for i in F do
    if i=F[1] then Q[i]:=0; next fi;
    e:=[Q[i],i]; a:=Q[e[1]];
    if member(e,U) then a:=a-1 fi;
    if member(e,V) then a:=a+1 fi;
    Q[i]:=a;
  od;
  for e in P0 do
    a:=Q[e[2]]-Q[e[1]];
    if abs(a)>1 then RETURN(false) fi;
    if member(e,U) then a:=a+1 fi;
    if member(e,V) then a:=a-1 fi;
    if a<>0 then RETURN(false) fi;
  od; true
end;

# add a 0 and a 1 to P

`shift_equiv/bound`:=proc(P) local X,Y,u,J,P0,i;
  X:=map(op,P); Y:={$1..nops(X)+2} minus X;
  u:=max(op(Y)); J:=X minus map(x->op(1,x),P);
  P0:={op(P),seq([i,u],i=J)};
  u:=min(op(Y)); J:=X minus map(x->op(2,x),P);
  {op(P0),seq([u,i],i=J)};
end;

