Help:=proc() print(`N(pi), decompose(pi), find132Tree(pi), search_for_subtree(tree, i), find_descendants(tree), findIndecomposableTree(pi), findForest(pi)`): end: #redu(pi) finds the reduction of the given pattern redu:=proc(pi) RETURN(sort(sort(pi,'output=[permutation]'),'output=[permutation]')): end: #N(pi) computes the normalization of pi, i.e. the 132-avoiding permutation with the same left-to-right minima as pi. N:=proc(pi) local ltrMinPositions, minVal, n, i, j, k, nonltr, filler, output, minElt: ltrMinPositions:=[]: minVal:=infinity: n:=nops(pi): for i from 1 to n do if pi[i] FAIL then RETURN(result): fi: od: RETURN(FAIL): end: find_descendants:=proc(tree) local output,subtree,treepee: output:={tree[1]}: subtree := tree[2]: for treepee in subtree do output:=output union find_descendants(treepee): od: RETURN(output): end: #findIndecomposableTree(pi) finds the labelled tree associated to the indecomposable 1342-avoiding permutation pi findIndecomposableTree:=proc(pi) local norm,tree1,i,tree2,n,tree3,j,l,beaters,beatees,label,new_beaters,min_before,max_after,descendants,subtree,d,children_of_root,child: n:=nops(pi): norm:=N(pi): tree1:=find132Tree(norm): tree2:=subs({seq(norm[i]=[pi[i],l],i=1..n)},tree1): tree3:=subs({seq(norm[i]=pi[i],i=1..n)},tree1): for i from 1 to n-1 do subtree:=search_for_subtree(tree3,pi[i]): descendants:=find_descendants(subtree): beaters:={}: beatees:={seq(j,j=i+1..n)}: while beatees <> {} do new_beaters:={}: for d in {seq(j,j=2..n)} minus beaters do min_before:=min({seq(pi[j],j=1..d-1)}): max_after:=max({seq(pi[b], b in beatees)} intersect {seq(j,j=1..pi[d]-1)}): if min_before [] do first_parent:=first_leaf: first_leaf:=first_leaf[2][1]: od: #finds the tree when we are in the first two cases laid out in Combinatorics of Permutations 2nd Ed if nops(first_parent[2]) >= 2 or first_parent[1] = 0 then #print(`Case 1/2`): treepee:=deleteFirstLeaf(tree): permpee:=findPermutation(treepee): for i from 1 to nops(permpee) do if permpee[i] >= first then permpee[i] := permpee[i]+1: fi: od: RETURN([first, op(permpee)]): fi: #tests to see whether we are in the 3rd or 4th case laid out in Combinatorics of Permutations 2nd Ed first_zero:=FAIL: treepoint:=tree: while treepoint[2] <> [] do if treepoint[1] = 0 then first_zero := treepoint: fi: treepoint:=treepoint[2][1]: od: #handles the third case laid out in ... if first_zero <> FAIL then #print(`Case 3`): treepee := insertFirstLeaf(deleteFirstSubtree(tree, first_zero)): permpee := redu(findPermutation(treepee)[2..-1]): treevee := first_zero: # print(treepee): treevee[1] := add(first_zero[2][i][1], i=1..nops(first_zero[2])): # print(treevee): peevee := findPermutation(treevee): m := node_count(treevee): num_set1 := sort([op(1..m, tree132)]): num_set2 := sort([op(m+1..-1, tree132)]): RETURN([seq(num_set1[peevee[i]],i=1..m),seq(num_set2[permpee[i]],i = 1..nops(permpee))]): fi: #handles the fourth case laid out in ... #print(`Case 4`): #while treepoint[2][1][2] <> [] do # treepoint[1] := treepoint[1]-1: # parent_point := treepoint: # treepoint := treepoint[2][1]: #od: #parent_point[2] := [treepoint[2][1]]: treepee := deleteFirstLeaf(tree): treepee := reduceEntries(treepee): permpee := findPermutation(treepee): RETURN([permpee[1],n,op(2..-1,permpee)]): end: #findPermutationFromForest(forest) finds the (possibly decomposible) permutation associated to a forest findPermutationFromForest:=proc(forest) local tree,i,n,running_n,output,m,subperm,num_set: n:=add(node_count(tree),tree in forest): running_n:=n: output:=[]: for tree in forest do m:=node_count(tree): subperm:=findPermutation(tree): num_set:=[seq(i,i=running_n-m+1..running_n)]: running_n:=running_n-m: output:=[op(output),seq(num_set[subperm[i]],i=1..m)]: od: RETURN(output): end: