Help23:=proc(): print(` GrabCycle(S,T), LtoC(L), CtoL(C), FindCycle(f,i) , RandF(n), CanC(C) , Joyal(f), RandTree(n) `): end: #RandF(n): A random function {1,..n}->{1,..n} RandF:=proc(n) local ra,i: ra:=rand(1..n): [seq(ra(),i=1..n)]: end: #GrabCycle(S,T): Given a set S of vertices and a table T grabs a cycle that belongs to the first element of S GrabCycle:=proc(S,T) local s,cu,ne,C: s:=S[1]: C:=[]: cu:=s: ne:=T[cu]: if ne=s then RETURN([s]): fi: while ne<>s do C:=[op(C),cu]: ne:=T[cu]: cu:=ne: od: C: end: #LtoC(L): Given a permutation of an arbitary set of positive integers in one-line notation #converts it to a collection of cycles. Try: #LtoC([6,1,4,5]); LtoC:=proc(L) local T,L1,C,S,C1,i: L1:=sort(L): for i from 1 to nops(L) do T[L1[i]]:=L[i]: od: #S is the vertices yet to be visited S:={op(L)}: #C is the set of cycles C:={}: while S<>{} do C1:=GrabCycle(S,op(T)): C:=C union {C1}: S:=S minus {op(C1)}: od: C: end: #CtoL(C): Given a collection of cycles, converts it to one line notation CtoL:=proc(C) local j,C1,S, T,i: S:=[]: for C1 in C do for j from 1 to nops(C1)-1 do T[C1[j]]:=C1[j+1]: od: T[C1[nops(C1)]]:=C1[1]: S:=[op(S),op(C1)]: od: S:=sort(S): [seq(T[S[i]],i=1..nops(S))]: end: #CanC(C): The canonical form of the cycle C CanC:=proc(C) local s,i: s:=min(op(C)): for i from 1 to nops(C) while C[i]<>s do od: [op(i..nops(C),C),op(1..i-1,C)]: end: #FindCycle(f,i): inputa a function f from {1, ...n} to {1,...,n} and a vertex i , 1<=i<=n output #the cycle it leads to, and where it hits it. Try: #FindCycle([1,2,2,2,3],3); FindCycle:=proc(f,i) local n,cu,ne,C,j: n:=nops(f): if not 1<=i and i<=n then RETURN(FAIL): fi: cu:=i: C:=[]: while not member(cu,C) do C:=[op(C),cu]: ne:=f[cu]: cu:=ne: od: C:=[op(C),ne]: for j from 1 to nops(C) while C[j]<>C[nops(C)] do od: CanC([op(j+1..nops(C),C)]),C[nops(C)]: end: #Joyal(f): The joyal mapping. Inputs a function f from {1,..,n} to {1, ..n} outputs # a doubly-rooted tree Joyal:=proc(f) local n,C,i,T,S,j,s,L: n:=nops(f): C:={seq(FindCycle(f,i)[1],i=1..n)}: S:={seq(op(C[i]),i=1..nops(C))}: for s in S do T[s]:={}: od: for i from 1 to n do if not member(i,S) then j:=FindCycle(f,i)[2]: T[j]:=T[j] union {{i,f[i]}}: fi: od: L:=CtoL(C): [{seq(op(T[L[i]]),i=1..nops(L))} union {seq({L[i],L[i+1]},i=1..nops(L)-1)}, [L[1],L[nops(L)] ] ]: end: #RandTree(n): A random tree on n vertices using the Joyal bijection. Try: #RandTree(30); RandTree:=proc(n) local f: f:=RandF(n): Joyal(f)[1]: end: