Help:=proc(): print(`Flip(pi,i), Ad1(pi), GC(pi),Tree1(pi)`):end: #Flip(pi,i): Inputs a permutation pi and a place i #(i=2..n) outputs the permutation obtained by reversing #pi[1]...pi[i] and keeping the rest the same Flip:=proc(pi,i) local j: [seq(pi[i-j],j=0..i-1),op(i+1..nops(pi),pi) ]: end: #Ad1(pi): the number of adjacencies of a permutation pi Ad1:=proc(pi) local i,c: c:=0: for i from 1 to nops(pi)-1 do if abs(pi[i+1]-pi[i])=1 then c:=c+1: fi: od: if pi[nops(pi)]=nops(pi) then c:=c+1: fi: c: end: #GC(pi): inputs a perm pi and outputs its set of #children (the set of perms obtained from pi via #one prefix-reversal and having one more adjaceny GC:=proc(pi) local i,n,j,S,pi1: n:=nops(pi): i:=pi[1]: S:={}: for j from 2 to n while pi[j]<>i+1 do od: pi1:=Flip(pi,j-1): if Ad1(pi1)>Ad1(pi) then S:=S union {pi1}: fi: for j from 2 to n while pi[j]<>i-1 do od: pi1:=Flip(pi,j-1): if Ad1(pi1)>Ad1(pi) then S:=S union {pi1}: fi: S: end: #The wasteless tree: the family tree of permutations #obtained by wasteless flipping Tree1:=proc(pi) local S,T,S1,i: S:=GC(pi): if S={} then RETURN([pi,{}]): fi: S1:={}: for i from 1 to nops(S) do S1:=S1 union {Tree1(S[i])}: od: [pi,S1]: end: