Ever needed (or wanted) to generate a tree (in this case, a graph on n vertices with n-1 edges) at random? Dismayed that the obvious method (adding a 1-2 edge, then randomly picking a vertex to connect 3 to, then 4) doesn't produce all possible trees (1-3-2 simply can't be done)? Your prayers are answered! This code uses Prüfer sequences (for which Google generates no useful introductory hits) to describe trees... turns out you can rank the nn-2 labelled trees, generate the Prüfer sequence corresponding to that rank, then reconstruct the tree from the sequence.

That was actually the first thing I googled for. Apparently, though, "Prufer" is the more common Anglicism, and neither produced any useful introductory documents.

So what the hell, I'll give it a shot.

The idea is to generate a unique sequence for each labelled tree on N vertices. What you end up doing is removing vertices one at a time, starting with the highest label leaf vertex (you can start with the lowest, it doesn't matter, as long as you're consistent). When you remove a vertex, you add the label of the vertex it was adjacent to to the sequence. Keep going until you only have two vertices left. You've got a Prüfer sequence!