## Assumes labels are {1..n}. PCpablo:=proc(T) local n,Lvs,i,v,P,N,V,L,nick,e: n:=nops(T)+1: V:= {seq(op(e), e in T)}: N:=table(): for v in V do: N[v]:={}: od: L:={}: # Find the neighbors of every vertex for e in T do: N[e[1]]:= N[e[1]] union {e[2]}: N[e[2]]:= N[e[2]] union {e[1]}: od: # Find all the leaves for v in V do: if nops(N[v])=1 then: L:= L union {v}: fi: od: P:=[-1$(n-2)]: # defined ahead of time to avoid the [ ,op] copying issue. for i from 1 to n-2 do: nick:=PC1(op(N),L): # current PC1 output P[i]:=nick[1]: # remove the smallest leaf from consideration: L:=L minus {nick[2]}: N[nick[1]]:= N[nick[1]] minus {nick[2]}: N[nick[2]]:= {}: # Consider that nick[1] (neighbor) may now be a leaf if nops( N[nick[1]] ) = 1 then: L:=L union {nick[1]}: fi: od: P: end: