Discrete

Sixteen trees on four points

How many ways are there to connect n labelled points into a single tree? The answer is n to the power n minus two, which is a strange enough formula to demand an explanation — and the explanation is a code that turns every tree into a short list of numbers, and every short list of numbers back into a tree.

Worth reading first: Seven bridges, and the invention of throwing things away · One sequence, counting everything.

Four points, labelled 1 to 4. Join them with three edges so that everything is connected and nothing loops. How many different ways are there?

All 16 trees on 4 labelled pointsEvery tree on 4 labelled points, drawn one by one. There are 16 of them, which is 4 to the power 2.1234123412341234123412341234123412341234123412341234123412341234all 16 trees on 4 labelled points, found by taking every set of 3 edges and keeping the ones with no cycle4 to the power 2 is 16, which is the count — and the same rule gives 1,296 on six points, which is why they are not all drawn
Fig. 1 All sixteen trees on four labelled points, found by taking every set of three edges from the six available and keeping the ones with no cycle. The points are labelled, so two drawings that differ only in which label sits where are two different trees.

Sixteen. On five points there are 125, on six there are 1,296, and the pattern is n to the power n − 2. That is Cayley’s formula, and the exponent is what makes it worth explaining: nothing about connecting points suggests a power two less than the number of them.

Why the labels matter

The count is of labelled trees, and the qualification is doing a great deal of work.

Ignoring labels, there are only two shapes on four points: a path, and a star with one centre joined to three leaves. The sixteen come from asking which labels sit where — there are 12 ways to label a path and 4 ways to label a star, and 12 + 4 = 16.

That split explains why the unlabelled count is a much harder problem. Counting labelled objects is usually the easier question because there are no symmetries to worry about: two labelled trees are the same only if they have exactly the same edges. Counting shapes means quotienting by all the relabellings that produce the same picture, and the quotient is uneven — a path can be labelled in more ways than a star, because the star has more symmetry.

The arithmetic of that split is worth doing once. There are 24 ways to write four labels in a row, and a path read forwards is the same path read backwards, so the 24 orderings give 12 distinct labelled paths. A star is determined by which label is at the centre, so there are 4 of them. Every one of the 16 is one or the other, and the blocks a symmetry cuts out are exactly what makes the two divisions different: the path’s symmetry group has two elements and the star’s has six.

This is the same asymmetry that makes counting colourings up to symmetry a separate subject from counting colourings. Labelled counts come out as clean formulas; unlabelled counts come out as sequences with no closed form.

A tree, written down as a list

The proof of the formula is a code. Given a tree, produce a list of numbers; given the list, reconstruct the tree. If the two operations undo each other, the trees and the lists are in one-to-one correspondence, and counting lists is trivial.

One tree, and the 4 numbers that hold itA labelled tree and its code, read off by stripping the smallest leaf repeatedly and writing down the point it was attached to. The coding is a bijection, which is why the trees can be counted by counting sequences instead.123456the treestep 1leaf 1attached to4step 2leaf 2attached to4step 3leaf 3attached to5step 4leaf 4attached to5the code: 4, 4, 5, 5strip the smallest leaf and write down its neighbour, 4 times: the code is 4, 4, 5, 5every tree gives a different code and every code comes from a tree — checked by enumerating all of them upto 6 points, which is 1,296 trees
Fig. 2 The encoding, step by step. Find the leaf with the smallest label, write down what it is attached to, and remove it. Doing that four times on a six-point tree leaves two points and a list of four numbers.

The rule is: repeatedly find the leaf with the smallest label, record the label of its neighbour, and delete the leaf. Stop when two points are left. For a tree on n points that produces a list of n − 2 entries, each of which is one of the n labels — so there are n to the power n − 2 possible lists.

The encoding is easy to believe. The decoding is where the argument lives.

Reading the list backwards

Given a list, the tree can be recovered without guessing, and the key is a single observation: a label appears in the list exactly as many times as its point has neighbours, minus one.

So the leaves of the original tree are precisely the labels that never appear in the list. The smallest of those was the first leaf removed, and the first entry of the list says what it was attached to, which reconstructs the first edge. Delete that leaf, cross off the first entry, and repeat: the process is forced at every step, with no choices to make.

When the list runs out, two points remain and there is exactly one edge left to draw between them.

Nothing in that procedure could have gone differently, which is what makes it a genuine inverse rather than a plausible reversal. Encoding then decoding returns the tree it started from; decoding then encoding returns the list. The correspondence is exact, so the number of trees is the number of lists, which is n to the power n − 2.

Why a tree can be coded at all

The decoding works because trees are rigid in a way that most graphs are not, and the rigidity is worth stating separately because it is what the whole construction rests on.

A tree on n points has exactly n − 1 edges — one fewer than the number of points — and that is forced. Fewer and it is disconnected; more and it has a cycle. So a tree is simultaneously the sparsest connected graph and the densest acyclic one, which is a coincidence of exactly the kind that makes an object countable.

The bridges of KönigsbergA schematic of the city: a river splitting around an island, four landmasses, and the seven bridges joining them.north banksouth bankthe islandeast bank
Fig. 3 A graph whose degrees decide a question about traversal. Counting degrees is what settles most elementary statements about trees too — including the one that keeps the encoding from getting stuck.

Two consequences follow immediately and both are used above. Every tree with at least two points has at least two leaves, since the degrees add to 2(n − 1) and cannot all be two or more; that is what guarantees the encoding never gets stuck. And removing a leaf from a tree leaves a tree, which is what lets the encoding recurse.

These are the same counting facts that decide when a walk over every bridge exists — there, degrees are counted modulo two rather than added up — and they are the reason so many statements about trees can be proved by peeling one point off and appealing to a smaller case.

Checked by enumeration

The argument above is a proof and it is also the kind of proof that is easy to get subtly wrong — an off-by-one in the length of the list, a tie-breaking rule that is not quite forced, a claim of uniqueness that fails on some small case.

So the figures check it. For each size up to six, every tree is generated by brute force over subsets of edges, every one is encoded, and two things are required: that no two trees produce the same list, and that the number of lists produced is exactly n to the power n − 2. For six points that is 1,296 trees, each encoded and compared against all the others.

Both halves are needed. Distinct codes with too few of them would mean the encoding is injective but not onto; the right number of codes with a repeat among them would mean the count is a coincidence. Together they say the correspondence is a bijection, which is the whole content of the theorem.

Counting by bijection, as a method

Setting up a one-to-one correspondence with something easier to count is the standard move in this part of the subject, and it is worth putting several instances side by side.

Every triangulation of a 6-gonAll 14 ways of cutting a convex 6-gon into triangles with non-crossing diagonals — the 4th Catalan number, counted by drawing them.14 of them
Fig. 4 Every way of cutting a hexagon into triangles with non-crossing diagonals. That count is a Catalan number, and it is proved by a different bijection — with bracket sequences, or with paths that stay above a line.

The Catalan numbers count triangulations of a polygon, bracket sequences, binary trees and lattice paths that stay above a diagonal, and all of those equalities are proved by exhibiting correspondences. Pascal’s triangle counts subsets and lattice paths at once for the same reason.

What distinguishes the Prüfer code from most such correspondences is how far it goes beyond the count. The code is a genuine data structure — a tree on n labelled points can be stored in n − 2 numbers rather than as a list of edges, and reconstructed from it. That is a compression scheme obtained as a side effect of a counting argument, and it is used in practice.

The code also proves more than the headline formula. Since a label appears in the list once fewer than its degree, the number of trees in which point k has a specified degree can be read off directly by counting lists with a specified number of occurrences — a refined count that follows from the same correspondence with no extra work.

What it counts, in the other language

There is a second reading of the same number that connects it to a different corner of the subject.

Two graphs that will not lie flat, and one that willK4, K5 and K3,3 in the best straight-line drawings a search could find. K4 has no crossings; the other two have one each, and Euler's formula shows that none can have none.K4no crossingsK51 crossing, and no drawing has noneK3,31 crossing, and no drawing has noneK4: 4 points, 6 edges, and at most 6 allowed; K5: 5 points, 10 edges, and at most 9 allowed; K3,3: 6 points, 9 edges,and at most 8 allowed4,000 layouts were tried for each; the best found is drawn, and the bound is what rules out anything better
Fig. 5 The complete graph on four points, and two larger relatives. A tree on n labelled points is exactly a spanning tree of the complete graph on those points, so Cayley’s formula counts the spanning trees of the complete graph.

A tree on n labelled points is a spanning tree of the complete graph on those points: a subset of its edges that reaches every point without a cycle. So Cayley’s formula says the complete graph on n points has n to the power n − 2 spanning trees, and asked that way it is a special case of a much more general theorem.

The matrix-tree theorem says the number of spanning trees of any graph is a determinant — take the matrix whose diagonal is the degrees and whose off-diagonal entries are minus one for each edge, delete any row and column, and take the determinant. For the complete graph that determinant evaluates to n to the power n − 2, which is Cayley’s formula obtained by linear algebra rather than by coding.

Two proofs of one formula, and they generalise in different directions: the code refines into degree-specified counts, and the determinant extends to every graph.

The determinant route has a further advantage that is easy to miss. It computes the count for a graph that is not complete — a grid, a cube, a road network — where no coding argument is available and no formula exists. The spanning trees of a graph are what a network’s redundancy is measured by, and the number of them is a determinant away for any graph somebody can write down, which is a far more useful statement than a formula for one family. The rank of a matrix is what the deletion of a row and a column is really about: the full matrix is singular for the same reason every row sums to zero, and the deleted minor is what carries the information.

There is one more count worth putting beside these. Not every subset of n − 1 edges is a tree, and the ones that fail do so by containing a cycle and therefore missing a point. At four points, 20 subsets of three edges give 16 trees, so exactly 4 fail — one for each triangle in the complete graph, since a triangle plus an isolated point is the only way to fail on four points. That kind of small-case accounting is what makes an exhaustive enumeration worth running: the failures are as informative as the successes, and a count that does not add up is the first sign of an error.

The count of shapes, and why nobody has a formula for it

Setting the labelled count beside the unlabelled one shows what the labels bought.

Pascal's triangle, 12 rowsEach entry is the sum of the two above it.1111211331146411510105116152015611721353521711828567056288119368412612684369111045120210252210120451011115516533046246233016555111
Fig. 6 A triangle of counts where every entry is a sum of two above it. Formulas of that kind — a recurrence with a closed form — are what labelled counting produces, and what counting shapes almost never does.

The number of trees on n labelled points is 1, 1, 3, 16, 125, 1296. The number of tree shapes on n points is 1, 1, 1, 2, 3, 6, 11, 23, 47, 106 — a sequence with no closed form, computed by a generating function that satisfies an equation in terms of itself, and whose growth rate is a constant nobody can write in elementary terms.

The reason for the difference is the one given above: dividing by symmetry is not a division. If every tree had exactly n factorial labellings, the shape count would be the labelled count divided by n factorial and the formula would survive. Symmetric trees have fewer labellings than that, the deficit varies from tree to tree, and the arithmetic falls apart.

That contrast is a reliable guide to which counting questions are answerable. Counting arrangements with everything distinguishable tends to give a formula; counting them up to symmetry tends to give a sequence. Burnside’s average is the tool for the second case and it turns a hard count into an average of easier ones rather than into a formula.

What it costs

Generating every tree on n points by testing every set of n − 1 edges costs the number of such subsets, which is a binomial coefficient and grows fast: 20 subsets at four points, 210 at five, 3,003 at six, 54,264 at eight. The figures stop at six for that reason, and the count they verify grows faster still.

Encoding is cheap. A careful implementation of the Prüfer code runs in time proportional to n, by keeping track of which points have become leaves rather than searching for the smallest each time — though the obvious implementation searches, and costs n² for no good reason.

The enumeration is also the reason these figures are drawn at all rather than asserted. A formula printed beside a picture of one tree is a claim; sixteen trees drawn from a search that took every three-edge subset and kept the acyclic ones is a measurement, and the two differ in exactly the way this site cares about. The same choice was made for the sixteen spheres that fill a cube and for the twenty-four syllogisms out of two hundred and fifty-six: where the whole space is small enough to walk, walking it is better evidence than any argument about it.

Generating a random labelled tree uniformly is where the code earns its keep in practice. Choosing a random tree by picking edges at random and rejecting cycles is fiddly and biased; choosing n − 2 labels at random and decoding is uniform by construction, because the correspondence is exact and every list is equally likely. That is the standard method, and it exists only because the bijection does.

What the picture cannot show

Sixteen trees fit on a page and 1,296 do not. The figure showing every tree is drawn at four points because that is the largest case a reader can check by eye, and the claim being made is about every n.

The encoding figure shows one tree and one code. That the correspondence is one-to-one is a statement about all of them at once, and it is checked by enumeration rather than shown.

And nothing in these drawings distinguishes a labelled tree from a shape. The labels are printed on the points, but the visual difference between two trees that are the same shape with different labels is a matter of reading small numbers — which is exactly the distinction the whole count turns on, and the one a picture conveys worst.

The ladder from here

Rungs above: the matrix-tree theorem, and the determinant that counts spanning trees of any graph. The multivariate refinement, where each tree is weighted by its edges and the determinant becomes a polynomial. Counting forests, and the trees on n points with a specified number of components. Unlabelled trees, where the counting needs a generating function and Pólya’s enumeration rather than a bijection. Random trees, and the shape a uniformly chosen large tree tends to have — its diameter grows like the square root of n, which is not what most people guess. Random graphs and the moment a giant connected piece appears. And the parking-function bijection, which counts the same objects a third way and connects them to a problem about cars and parking spaces.

The shape of the idea

A formula with an exponent nobody expects usually means a correspondence nobody has found yet, and the search for the correspondence is more valuable than the formula.

Cayley published the count in 1889 with a proof by generating functions. Prüfer’s code arrived in 1918 and replaced an argument that verified the number with one that explains it: the trees on n points are lists of n − 2 labels, because a tree is a list of n − 2 labels once the right way of writing it down has been found. The formula stops being a coincidence at that moment.

The general lesson is one this site keeps meeting. Two collections have the same size for one of two reasons — either both have been counted and the answers agree, or a correspondence has been exhibited. The first is a fact and the second is an explanation, and the second usually comes with a data structure, an algorithm, or a refinement attached.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

BijectionCatalan numbersCayleys formulaCounting argumentEncodingGraphLabelled treeLeafPrufer codeSpanning tree