A connected graph with no cycles is the leanest way to join a set of points, and the problem is to recognise such graphs and count them.
The previous lesson set up the language. Graphs here are simple (no loops or repeated edges), and by the handshake lemma the degrees (edge counts at each vertex) sum to twice the number of edges. A path is a sequence of distinct vertices each joined to the next, a cycle is a path of at least three vertices whose ends are also joined, and a graph is connected when every two vertices are joined by a path. That lesson ended on the simplest connected graphs, the ones without cycles.
Trees, forests and leaves
A tree is a connected graph with no cycles. A graph with no cycles, connected or not, is a forest, and its connected pieces are trees. A leaf is a vertex of degree . On the edges , , and form a tree with leaves , and .
Theorem. Every tree with at least two vertices has at least two leaves.
Take a longest path in the tree. One exists because the graph is finite, and because a connected graph on two or more vertices has an edge. Suppose had a neighbour other than . If for some , then is a cycle, which a tree does not have. If is off the path, then is a longer path, contradicting the choice. So is a leaf, and by the same argument so is , a different vertex.
A path on vertices has exactly two leaves, so the bound is sharp; a star, one centre joined to all the others, has .
Every tree has n - 1 edges
If is a leaf of a tree , then ( with and its edge deleted) is still a tree. Deleting creates no cycle, and a path between two other vertices never passes through , since an interior vertex of a path has two neighbours and has one.
Theorem. A tree with vertices has exactly edges.
By induction on , starting from one vertex and no edges. If trees on vertices have edges, a tree on vertices has a leaf, and removing it takes one edge and leaves a tree on vertices. So has edges.
Adding over components, a forest with vertices and components has edges. And since a tree's degrees sum to , averaging just under , every vertex of degree above must be paid for by leaves.
Example. A tree has one vertex of degree , two of degree , and every other vertex is a leaf. How many leaves does it have?
With leaves there are vertices and edges. The degree sum is , and it equals , so . In general the same algebra gives over the vertices of degree , here .
Now you. A tree has one vertex of degree , two of degree , one of degree , and every other vertex is a leaf. How many leaves does it have, and how many vertices?
Answer
leaves and vertices. The degree sum is and there are edges, so and . The shortcut agrees: .
Five ways to say tree
For a graph on vertices, these are equivalent: (1) is connected and acyclic; (2) is connected with edges; (3) is acyclic with edges; (4) every two vertices are joined by exactly one path; (5) is minimally connected, meaning connected but disconnected by deleting any one edge. The proofs link each to the definition, (1), in both directions.
One fact does much of the work: deleting an edge that lies on a cycle never disconnects a graph, because any path that used can detour from to the long way round the cycle.
(1) gives (2) and (3) by the edge count. For (3) to (1): an acyclic graph is a forest, so with components it has edges, and forces . For (2) to (1): if is connected with edges, delete edges on cycles, one at a time, until none remain. The graph stays connected, so it ends as a tree on vertices with edges; it started with , so nothing was deleted and had no cycle.
(1) gives (4). There is a path from to ; if there were two, and , let be the last vertex before they part, and the first vertex after on that lies on again (both end at , so there is one). The stretches of and between and meet only at their ends and form a cycle. Conversely, a graph satisfying (4) is connected, and has no cycle, since a cycle through and would offer two paths between them, one each way round.
(1) gives (5): if deleting from a tree left and joined by a path, that path plus would be a cycle. Conversely (5) gives (1), since by the fact above a graph with a cycle has an edge whose deletion keeps it connected.
A graph on vertices is thus a tree once it passes any two of three tests: connected, acyclic, edges.
Spanning trees
A spanning tree of a graph is a subgraph that is a tree and contains every vertex of .
Theorem. Every connected graph has a spanning tree.
While the graph has a cycle, delete an edge of that cycle. The graph stays connected, by the fact above, and keeps every vertex, and since edges run out the process stops at a spanning tree. So a connected graph with vertices and edges has , and reaching a spanning tree takes exactly deletions, whichever cycles are broken. Finding the cheapest one when edges carry costs is an algorithmic question, answered in Algorithms and Data Structures; the question here is how many there are.
Cayley's formula
A labelled tree on is a tree with those vertices, two counting as different when their edge sets differ: the paths and are different. They are the spanning trees of the complete graph , in which every pair is joined.
For there is one, the edge . For a tree is a path fixed by its middle vertex, so there are three. For there are two shapes. A star is fixed by its centre, giving . A path on four vertices is one of the orderings, each read in both directions, giving . That makes . A second count confirms it: by characterisation (3), three of the edges of form a tree unless they are one of the triangles, leaving .
The counts are , and . Carl Wilhelm Borchardt proved in 1860, via a determinant, that the pattern continues, and Arthur Cayley stated it in 1889 in "A theorem on trees".
Cayley's formula. There are labelled trees on vertices.
Example. Count the labelled trees on by shape, and compare with .
The five degrees are each at least and sum to , so their excess over is , split as , as or as . The degree sequences are (a star: trees, one per centre), (a path: ) and . In the last, the degree vertex must neighbour the degree one, or those would lie in separate components. Choose the degree vertex ( ways), its degree neighbour ( ways) and the leaf hanging from that neighbour ( ways): . The total is .
Now you. Delete the edge from . How many spanning trees does the remaining graph have?
Answer
. The trees have edges each, in all, and the edges of are alike under relabelling, so each lies in trees. The other avoid .
Prüfer's code
The number counts the sequences of length with entries from , and Heinz Prüfer found a bijection with them in 1918. To encode a labelled tree, repeat while more than two vertices remain: remove the leaf with the smallest label, and write down the label of its neighbour.
The code records degrees. A vertex is written once for each neighbour removed as a leaf, and it keeps exactly one neighbour to the end (when it is removed itself, or as one of the final two), so it appears times. The leaves are precisely the labels missing from the code.
So the first vertex removed is the smallest label missing from the code, its neighbour is the first entry, and the rest of the code is the code of the smaller tree. Decoding therefore runs: for each entry in turn, join it to the smallest label not yet crossed off and not among the entries still to come, and cross that label off; finally join the two labels left over. A tree is determined by its code.
Every sequence is a code, by induction on . For the empty sequence codes the single edge. Given , let be the smallest label missing from it. By induction is the code of a tree on the other labels; attach to . The leaves of the new tree are the labels missing from the whole sequence, so encoding removes first, writes , and continues as for . So encoding is a bijection, which proves Cayley's formula.
Example. Encode the tree on with edges , , , , , , then decode the result.
The leaves are . Remove and write ; remove , write ; remove , write . Now is a leaf, and the smallest: remove it, write . Remove , write . Vertices and remain, and the code is : vertex , of degree , appears three times. To decode, is the smallest label missing from the code: join it to . With to come, join to , then to . With to come, is free: join it to , then to , and finally the leftover and .
Now you. Decode the Prüfer code into a tree on , and check by encoding it again.
Answer
The edges are , , , and : join to , then to , then (now free) to , then to , and finally the leftover and . Encoding removes the leaves and (writing ), then (writing ), then (writing ).
Rooted binary trees
Choosing one vertex of a tree as its root gives the tree a direction. Each other vertex has a unique path to the root, by characterisation (4), and the next vertex on it is the parent of , which makes one of its children. A binary tree is a rooted tree in which every vertex has no children or exactly two, a left and a right, and left differs from right. The vertices with children are internal. With internal vertices there are children, and every vertex but the root is a child, so there are vertices and leaves.
Let count binary trees with internal vertices. A lone root gives , then and . For the root's left subtree holds , or of the other internal vertices, giving shapes. Read each internal vertex as a multiplication and the five ways to multiply appear: , , , and . In general the left subtree has some internal vertices and the right , chosen independently, so
This is the recurrence that the lesson on generating functions set up for the Catalan numbers, and both start at . So , giving as listed and .
Trees, then, are characterised, counted and coded, and they are easy to draw on paper without crossings: root at the top, each generation of children in a row below. Five towns each joined directly to every other, the complete graph , resist every attempt to be drawn that way. The last questions of the course are geometric: which graphs can be drawn in the plane without crossings, and how few colours their regions need so that neighbours always differ. The final lesson answers both, starting from a formula of Euler's.