On this page
A tree is a connected graph with no cycles. Label n points 1 to n and ask how many different trees join all of them. Below you check the answer by hand for n = 3 and n = 4, then learn the Prüfer code that proves it on one page.
What is a spanning tree, and why count them?
A spanning tree of a graph is a subgraph that is a tree and includes every vertex of the graph (Wikipedia: Spanning tree). When every pair of your n points may be joined (the complete graph Kₙ), its spanning trees are exactly the trees on those n labelled vertices.
The number of trees on n labelled vertices is nⁿ⁻² (Wikipedia: Cayley's formula). The result is named after Arthur Cayley, who wrote about it in 1889, but the same source notes that Carl Wilhelm Borchardt found it first, in 1860.
"Labelled" means two trees of the same shape count as different if they join different numbers.
How many trees are there on 3 and 4 vertices?
Try these on paper first. A tree on n vertices always has n − 1 edges.
n = 3. Two edges on three vertices make a path a–b–c. The path is fixed once you choose the middle vertex: 1, 2 or 3. That is 3 trees, and 3³⁻² = 3.
n = 4. Three edges on four vertices give only two shapes.
The star: one centre joined to the other three. Choose the centre: 4 trees.
The path a–b–c–d: there are 4! = 24 orders of the labels, but a path read backwards is the same tree, so 24 ÷ 2 = 12 trees.
That makes 4 + 12 = 16, and 4⁴⁻² = 16. The list continues 125, 1296 and 16807 for n = 5, 6 and 7 (OEIS A000272).
How do Prüfer codes prove Cayley's formula?
Heinz Prüfer gave a proof in 1918 by turning every labelled tree into a list of n − 2 labels, called its Prüfer sequence or code (Wikipedia: Prüfer sequence). Each entry can be any of the n labels, so there are nⁿ⁻² possible lists. Prüfer codes give a bijection: every tree has exactly one code, and every list is the code of exactly one tree. So the two counts are equal.
To encode a tree, remove the leaf (a vertex with one edge) that has the smallest label, write down its neighbour, and repeat until two vertices remain. Take the tree on 6 vertices with edges 1–4, 2–4, 3–4, 4–5 and 5–6.
The leaves are 1, 2, 3 and 6. Remove 1 and write 4.
Remove 2 and write 4.
Remove 3 and write 4.
Now 4 is a leaf, joined only to 5. The leaves are 4 and 6; remove 4 and write 5.
Only 5 and 6 remain, so stop.
The code is (4, 4, 4, 5): four entries, which is 6 − 2.
To decode, walk along the code. At each step take the smallest unused label that does not appear in the rest of the code, join it to the current entry, and mark it used. At the end, join the two labels left over.
For (4, 4, 4, 5) you join 1–4, 2–4, 3–4, then 4–5 (4 no longer appears in what is left), and finally 5–6: the tree you started with.
For n = 4 you can see the bijection directly. A star with centre c has the code (c, c), which gives 4 codes. A path has its two middle vertices as its code, in the order the rule decides: the path 1–2–3–4 encodes to (2, 3), while 1–3–2–4 encodes to (3, 2). There are 4 × 3 = 12 codes with two different entries, one for each path. The split 4 + 12 is the same one you found by hand.
How can you practise counting proofs like this?
Count the small cases by hand first, then check your numbers against the OEIS entry.
Ask what simpler object your count could match; here it was lists of n − 2 labels.
Encode one example and decode it back; a different tree means a step went wrong.
Write the two counts side by side, like 4 + 12 for n = 4, to see that the bijection keeps the structure.
If an encoding goes wrong and you cannot see where, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer. To revise graphs and trees from the start, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing.
Your next step: draw the 16 trees on four vertices and write each one's code.



