Image: Kilom691, CC BY-SA 3.0Cayley's formula, or how many trees there are
Take four points labelled 1, 2, 3, and 4 and join them by three edges so that the result is connected and has no cycle. Such a graph is a tree, and there are exactly 16 of them. Twelve are paths: a path through four labelled points can be written in 4 factorial = 24 orders, and each path is written twice, once in each direction, so there are 12. Four are stars: pick the centre. There are no other shapes with four vertices, so 12 + 4 = 16, which is 4 to the power 2. On five labelled points the count is 125, which is 5 to the power 3. The pattern holds: the number of trees on n labelled vertices is n to the power n minus 2.
Who found it
Arthur Cayley stated the formula in 1889 in a short paper in the Quarterly Journal of Mathematics, A theorem on trees. He had been counting trees since the 1850s because chemists needed the counts: the alkanes with formula C_nH_{2n+2} are trees with n carbon vertices of degree at most four, and the number of isomers is the number of such trees. Cayley's paper of 1889 checks the formula for n up to 6 and sketches why it should hold in general; the argument is not a proof by modern standards. The formula had in fact appeared earlier, in a paper of Carl Wilhelm Borchardt in 1860 on determinants, without the language of trees. It is Cayley's name that stuck. The MacTutor biography has the rest of his career, which includes the first abstract definition of a group.
The code that proves it
The proof every textbook gives is from 1918, by Heinz Prüfer, and it is a bijection: every labelled tree on n vertices is turned into a sequence of n minus 2 numbers from 1 to n, every such sequence comes from exactly one tree, and there are n to the power n minus 2 sequences. The encoding is this. Find the leaf with the smallest label, write down the label of its only neighbour, and delete the leaf. Repeat until two vertices remain. The written labels form the code.
An example. Take the tree on six vertices with edges 1 to 4, 2 to 4, 3 to 5, 4 to 5, 5 to 6. The leaves are 1, 2, 3, and 6. The smallest is 1, its neighbour is 4; write 4, delete 1. The leaves are now 2, 3, 6; the smallest is 2, its neighbour is 4; write 4, delete 2. Now 4 has become a leaf, and the leaves are 3, 4, 6; the smallest is 3, neighbour 5; write 5, delete 3. Leaves 4 and 6; the smallest is 4, neighbour 5; write 5, delete 4. Two vertices remain, 5 and 6, and we stop. The code is 4, 4, 5, 5. It has n minus 2 = 4 entries, and a vertex appears in it exactly one time fewer than its degree: 4 has degree 3 and appears twice, 5 has degree 3 and appears twice, the leaves do not appear at all.
Decoding reverses this. Given the code 4, 4, 5, 5 on the vertex set 1 to 6, the vertices that do not appear in the code are the leaves: 1, 2, 3, 6. The smallest of them, 1, is joined to the first code entry, 4; delete 1 from the vertex set and the first entry from the code. The code is now 4, 5, 5, the unused vertices are 2, 3, 6 and also any vertex that no longer appears, which is still none; join 2 to 4. Code 5, 5; the vertices absent from it among those remaining are 3, 4, 6; join 3 to 5. Code 5; absent are 4 and 6; join 4 to 5. The code is empty and two vertices remain, 5 and 6; join them. The five edges are the five we started with. Unit 4 of the Graph Theory Masterclass carries out the proof that decoding always works and always gives a tree.
A hundred million trees on ten vertices
The formula grows fast. On ten labelled vertices there are 10 to the power 8 trees, one hundred million. On twelve there are 12 to the power 10, which is 61,917,364,224. The sequence is A000272 in the On-Line Encyclopedia of Integer Sequences, and the encyclopedia's entry lists a dozen other things the same numbers count, among them the parking functions of length n minus 1.
Labels are what make the count large. Forget the labels and ask only for the shapes: on four vertices there are 2 shapes, the path and the star, against 16 labelled trees. On six vertices there are 6 shapes against 1,296 labelled trees. On ten there are 106 shapes against 100,000,000. The number of shapes is sequence A000055, and it begins 1, 1, 1, 1, 2, 3, 6, 11, 23, 47, 106, 235, 551 for n from 0 to 12. There is no closed formula for it. Cayley's successors, above all George Pólya in 1937, found a way to compute it from a functional equation, and Richard Otter proved in 1948 that it grows like a constant times 2.9558 to the power n divided by n to the power 5/2, so that the shapes are exponentially fewer than the labelled trees, whose count grows like n to the power n.
Why labelled trees matter
A spanning tree of a graph is a tree that uses all its vertices and some of its edges, and the vertices of a graph come with their identities, which is to say with labels. The number of spanning trees of the complete graph on n vertices is therefore exactly Cayley's n to the power n minus 2. For an arbitrary graph the count comes from Kirchhoff's matrix-tree theorem of 1847, which Unit 4 states and checks on small graphs, and which gives Cayley's formula back when applied to the complete graph. Kirchhoff needed the count for electrical circuits; the same determinant now counts the ways a communications network can be held together by a minimal set of links.
Chemists still use Cayley's trees. The alkane isomer counts, 1, 1, 1, 2, 3, 5, 9, 18, 35, 75 for one to ten carbon atoms, are the numbers of tree shapes with maximum degree four, and they were the original reason anyone counted trees at all. The three isomers of pentane, drawn as trees, are the three shapes on five vertices with no vertex of degree five, which is all of them.


