Generating Trees

Tree generation is a well known (and solved!) problem in computer science. On the other hand, it's pretty important for various problems - in my case, making tree-like fusanes. I'll describe here the slightly tortuous route I took to make trees.

Firstly, there is a famous theorem due to Cayley that the number of (labelled) trees on n vertices is nn - 2 which can be proved by using Prüfer sequences. That's all very well, you might well say - but what does all this mean?

Well, it's not all that important, since there is a fundamental problem with this approach : the difference between a labelled tree and an unlabelled tree. There are many more labeled trees than unlabeled :


There is only one unlabeled tree on 3 vertices, but 3 labeled ones

this is easy to check using the two OEIS sequences for this : A000272 (labeled) and A000055 (unlabeled). For n ranging from 3 to 8 we have [3, 16, 125, 1296, 16807, 262144] labeled trees and [1, 2, 3, 6, 11, 23] unlabeled ones. Only 23 unique trees on 8 vertices out of 262,144 labelled ones is quite a ratio!

Having said all that, there is code here to generate such trees, with test code here. As always, the algorithms are taken from CAGES.

Far better than generating labelled trees and filtering out isomorphic ones would be to only generate non-isomorphic trees in the first place. For this, I found a paper here - a report, really but one written in a way I could understand (thanks Michael Dinneen!). It describes an algorithm due to Wright, Richmond, Odlyzko and McKay (WROM) which is described here.

As a result, here are all the 11 unlabelled trees on 7 vertices:
Please don't be confused by the fact that they actually have labels...

(As an aside, this tree-drawing algorithm doesn't work very well for 'twig' branches like the tree on the lower left).

Comments