MathLabs

Combinatorics and discrete mathematics

Trees

Connected graphs with no cycles, the simplest and most structured kind of network.

IntuitionNetworks with no shortcuts

A family tree, a company's org chart, a computer file system, and a decision tree in a game all share the same shape: every two points are connected by exactly one path, with no loops to get lost in. Add one more link between two branches of a family tree and you create a cycle — a shortcut that breaks this "exactly one path" property. A tree is the mathematical name for a connected network with the fewest possible edges: remove any single edge and it falls apart into two pieces; add any single edge and it creates a cycle.

The complete graph on 4 vertices with each vertex colored differently, used to illustrate spanning trees and Cayley's formula.
A binary tree T3T_3 on V=7V = 7 vertices has E=V−1=6E = V - 1 = 6 edges, no cycles, and a unique simple path between any two vertices.

UndergraduateFormal definitions

Definition: Tree

A tree is a graph TT that is both connected (there is a path between every pair of vertices) and acyclic (it contains no cycle). A vertex of degree 11 in a tree is called a leaf. A graph in which every connected component is a tree (so it need not itself be connected) is called a forest.

∣V(T)∣=n  ⟹  ∣E(T)∣=n−1|V(T)| = n \implies |E(T)| = n - 1

This is the tree's defining balance: with nn vertices, a tree has exactly n−1n-1 edges — just enough to keep everything connected, with nothing left over to create a cycle. Summing degrees over all vertices and using the handshake lemma (∑vdeg⁡(v)=2∣E∣\sum_v \deg(v) = 2|E|) gives a companion identity that will be useful when counting leaves and internal vertices.

∑v∈V(T)deg⁡(v)=2(n−1)\sum_{v \in V(T)} \deg(v) = 2(n-1)
Equivalent ways to characterize a tree on nn vertices
ConditionEdge countExtra property
Connected and acyclicexactly n−1n-1unique path between every pair of vertices
Connected with n−1n-1 edgesn−1n-1removing any edge disconnects it
Acyclic with n−1n-1 edgesn−1n-1adding any edge creates exactly one cycle
Disconnected graph with n−1n-1 edges (not a tree)n−1n-1has a cycle somewhere despite the edge count matching

UndergraduateKey theorems

For a graph TT with nn vertices, the following are equivalent: (i) TT is connected and acyclic; (ii) TT is connected with n−1n-1 edges; (iii) TT is acyclic with n−1n-1 edges.

Why is it true?

Removing any edge from a tree disconnects it, and adding any edge creates a cycle — a tree has exactly the number of edges needed to connect everything, with no redundancy and no shortage. These three conditions each pin down that "just enough" structure from a different angle.

Proof

We show (i) connected and acyclic   ⟹  \implies (ii) connected with n−1n-1 edges   ⟹  \implies (iii) acyclic with n−1n-1 edges   ⟹  \implies (i), which chains all three together.

**(i)  ⟹  \implies(ii)**, by induction on nn. The base case n=1n=1 is immediate: a single vertex has 0=n−10 = n-1 edges. For the inductive step, take a connected acyclic graph TT on n≥2n\ge2 vertices. Since TT is finite and acyclic, it must contain a leaf vv (a vertex of degree 11): otherwise every vertex would have degree ≥2\ge 2, and following edges out of any starting vertex without immediately backtracking would eventually revisit a vertex, producing a cycle. Deleting vv and its one incident edge leaves a graph T′T' on n−1n-1 vertices that is still connected (vv had no other vertices depending on it to stay connected) and still acyclic (a subgraph of an acyclic graph is acyclic). By the inductive hypothesis, T′T' has (n−1)−1(n-1)-1 edges, so TT has (n−1)−1+1=n−1(n-1)-1+1 = n-1 edges.

**(ii)  ⟹  \implies(iii)**. Suppose TT is connected with n−1n-1 edges. If TT had a cycle, deleting one edge of that cycle would keep the graph connected (the two endpoints of the deleted edge are still joined by the rest of the cycle), giving a connected graph on nn vertices with only n−2n-2 edges. But any connected graph on nn vertices needs at least n−1n-1 edges (each new vertex beyond the first needs at least one new edge to reach it), so n−2n-2 edges cannot connect nn vertices — a contradiction. So TT has no cycle, i.e. it is acyclic.

**(iii)  ⟹  \implies(i)**. Suppose TT is acyclic with n−1n-1 edges, and let it have cc connected components. Each component is itself connected and acyclic, so by the already-proven direction (i)  ⟹  \implies(ii) applied to each component separately, a component with nin_i vertices has ni−1n_i - 1 edges. Summing over all components, the total edge count is ∑i(ni−1)=n−c\sum_i (n_i - 1) = n - c. Since the total is given as n−1n-1, we get n−c=n−1n - c = n - 1, so c=1c=1: TT is connected.

The number of distinct labeled trees on the vertex set {1,…,n}\{1,\dots,n\} (for n≥2n\ge2) is nn−2n^{n-2}.

Why is it true?

It answers a purely combinatorial question — how many different ways can nn distinguishable points be wired into a tree-shaped network — with a strikingly simple closed formula, and the encoding trick used to prove it (Prüfer sequences) turns "count the trees" into "count the sequences," a much easier problem.

Proof

Fix the vertex set {1,…,n}\{1,\dots,n\} and, for n≥2n\ge2, define the Prüfer sequence of a labeled tree TT: repeatedly find the leaf with the smallest label, write down the label of its unique neighbor, and delete that leaf; stop once 22 vertices remain. This records n−2n-2 labels (one per deletion, since we start with nn vertices and stop at 22), so every labeled tree produces a sequence in {1,…,n}n−2\{1,\dots,n\}^{n-2}.

There are exactly nn−2n^{n-2} such sequences, since each of the n−2n-2 positions is independently any of the nn labels. It remains to show this encoding is a bijection, so that labeled trees are in exact one-to-one correspondence with these sequences.

The key fact is that in the Prüfer sequence, label ii appears exactly deg⁡(i)−1\deg(i)-1 times: every time one of ii's neighbors is deleted as a leaf, ii is written down once, and ii itself is only ever deleted (contributing no writes) once all but one of its incident edges are gone. In particular, the labels that never appear in the sequence are exactly the original leaves. This lets us decode: given a sequence (a1,…,an−2)(a_1,\dots,a_{n-2}), repeatedly take the smallest label not currently used as a remaining sequence entry or already attached, join it by an edge to the first remaining sequence entry, remove that label from further consideration and remove that entry from the sequence; after all n−2n-2 entries are consumed, join the 22 labels left over by a final edge.

This decoding procedure exactly reverses the encoding step by step — at each stage it identifies the same smallest-label leaf and the same neighbor that the encoder would have recorded, by induction on the number of vertices remaining. So encoding and decoding are mutually inverse, giving a bijection between labeled trees on nn vertices and sequences in {1,…,n}n−2\{1,\dots,n\}^{n-2}. Since there are nn−2n^{n-2} such sequences, there are exactly nn−2n^{n-2} labeled trees on nn vertices.

UndergraduateReal-World Applications and Worked Examples

Trees are the backbone of hierarchical and routing structures everywhere: file systems, phylogenetic trees in biology, decision trees in machine learning, and binary search trees and Huffman codes in computer science. One of the most direct engineering uses is network design: when a company must connect a set of offices, sensors, or computers with cables or wireless links at minimum cost, the cheapest connected layout is always a spanning tree — connecting them with a cycle would waste money on a redundant link. Kruskal's algorithm finds this cheapest tree by a simple greedy rule: sort all possible links by cost, and add each one in turn unless it would close a cycle.

Example: Minimum-cost cabling via Kruskal's algorithm

Five relay towers need to be connected by fiber cable at minimum total length. The possible links and their lengths (km) are: {1,3}:1\{1,3\}:1, {2,3}:2\{2,3\}:2, {4,5}:2\{4,5\}:2, {1,2}:4\{1,2\}:4, {2,4}:5\{2,4\}:5, {2,5}:6\{2,5\}:6, {3,4}:8\{3,4\}:8, {3,5}:10\{3,5\}:10. Find the minimum total cable length needed to connect all five towers.

Solution

Sort the 88 candidate links by increasing length: {1,3}:1\{1,3\}:1, {2,3}:2\{2,3\}:2, {4,5}:2\{4,5\}:2, {1,2}:4\{1,2\}:4, {2,4}:5\{2,4\}:5, {2,5}:6\{2,5\}:6, {3,4}:8\{3,4\}:8, {3,5}:10\{3,5\}:10. Kruskal's algorithm scans this list once, adding a link unless both of its endpoints are already connected by previously chosen links (which would create a cycle).

Add {1,3}:1\{1,3\}:1 (towers 1,31,3 were separate, now joined). Add {2,3}:2\{2,3\}:2 (joins tower 22 to the {1,3}\{1,3\} group, now {1,2,3}\{1,2,3\}). Add {4,5}:2\{4,5\}:2 (towers 4,54,5 were separate, now joined into {4,5}\{4,5\}).

Skip {1,2}:4\{1,2\}:4: both 11 and 22 are already in the group {1,2,3}\{1,2,3\}, so adding this link would close a cycle. Add {2,4}:5\{2,4\}:5: this joins the two remaining groups {1,2,3}\{1,2,3\} and {4,5}\{4,5\} into one group of all 55 towers.

At this point 44 links have been added, matching n−1=5−1=4n-1 = 5-1=4 edges for a tree on 55 vertices, so the algorithm stops (every later link would only create a cycle). The total minimum cable length is 1+2+2+5=101+2+2+5 = 10 km.

Example: Counting possible backbone topologies

A telecom company plans to connect 66 distinguishable relay stations with a loop-free (tree-shaped) backbone network, with no constraint yet on which pairs get linked. How many structurally different labeled tree topologies are possible?

Solution

By Cayley's formula, the number of labeled trees on nn vertices is nn−2n^{n-2}. Here n=6n=6, so the count is 66−2=646^{6-2} = 6^4.

Computing, 64=6×6×6×6=12966^4 = 6\times6\times6\times6 = 1296. So there are 12961296 structurally different tree topologies connecting the 66 stations.

For a network planner, this huge design space is exactly why nobody enumerates all 12961296 possibilities by hand: instead, an optimization procedure like Kruskal's algorithm (Theorem 1's edge-counting property guarantees any candidate has exactly 6−1=56-1=5 links) picks out the one cheapest tree directly from the link costs, without ever listing the others.

A tree has 2323 vertices. How many edges does it have?

By Cayley's formula, how many distinct labeled trees are there on 55 vertices?

A cable company must connect 99 offices with the cheapest possible network (no redundant links). Using Kruskal's algorithm on the sorted link costs, how many links will the final network have?

A graph has 1010 vertices and 99 edges, but is split into 22 separate connected pieces (one of which contains a cycle). Which characterization from Theorem 1 does this graph fail, even though its edge count matches n−1n-1?

References

  1. Reinhard Diestel (2017). Graph Theory
  2. David R. Karger, Philip N. Klein, Robert E. Tarjan (1995). A randomized linear-time algorithm to find minimum spanning trees · DOI:10.1145/201019.201022
  3. Aaron Schild (2017). An almost-linear time algorithm for uniform random spanning tree generation · arXiv:1711.06455 [preprint, not peer-reviewed]