Graph Theory, Step by Step

Free course

Chapter 1, lesson 5 of 5

Subtitles

Trees

The most useful graphs of all: connected, with no cycles.

Written lesson

6 min read

Four towns need roads. Every town must be reachable from every other one, and you want to build as few roads as possible. Three roads are always enough, and there are sixteen different ways to choose them. With ten towns there are one hundred million ways. Yet every single one of them has the same shape: a tree.

This lesson covers what a tree is, why a tree always has exactly one edge fewer than it has vertices, and where trees appear in the real world. It closes the first chapter of the course.

What is a tree?

A tree is a connected graph with no cycles.

Connected means you can reach every vertex. No cycles means you can never walk around a loop and come back without retracing an edge.

The tree for this lesson has eight vertices, 1 to 8, and seven edges: 1-3, 2-3, 3-4, 4-5, 5-6, 5-7 and 7-8.

A tree with eight vertices; its leaves are 1, 2, 6 and 8.A tree with eight vertices; its leaves are 1, 2, 6 and 8.

A vertex with degree one is a leaf. This tree has four leaves: 1, 2, 6 and 8. A graph with no cycles that comes in several pieces is a forest; every piece of a forest is a tree.

Many ways to say tree

Trees are special because many different descriptions turn out to mean exactly the same thing. For a graph with n vertices, each of these statements is equivalent to "it is a tree":

  1. Between any two vertices there is exactly one path. From 1 to 8 the only path is 1, 3, 4, 5, 7, 8.
  2. It is connected, and every edge is a bridge. Remove any edge, say 4-5, and the tree splits in two.
  3. It has no cycles, and adding any new edge closes a cycle. Join 6 to 8 and the loop 5, 6, 8, 7 appears.
  4. It is connected and has n − 1 edges.
  5. It has no cycles and has n − 1 edges.

Adding the edge 6-8 closes the cycle 5, 6, 8, 7.Adding the edge 6-8 closes the cycle 5, 6, 8, 7.

The first three follow from lesson 3. Two different paths between the same vertices would join into a cycle, so "no cycles" means "at most one path", and "connected" means "at least one". Every edge is a bridge exactly when no edge lies on a cycle. And a new edge uv closes a cycle with the path that already joins u and v. Statements 4 and 5 are the surprising ones, and they rest on the key fact below.

In words: a tree is minimally connected (lose any edge and it falls apart) and maximally acyclic (add any edge and a cycle appears).

Always n − 1 edges

A tree with n vertices always has exactly n − 1 edges.

The proof uses leaves.

Step 1: every tree with at least two vertices has at least two leaves. Take a longest path in the tree and look at one of its ends. If that end had a second neighbour, either the neighbour is already on the path (and we would have a cycle) or it is not (and we could make the path longer). Both are impossible, so the end is a leaf, and so is the other end.

Step 2: prune. Remove a leaf together with its edge. What is left is still connected (nothing passed through a leaf) and still has no cycles, so it is a tree with one vertex fewer and one edge fewer.

Keep pruning until one vertex is left, with no edges at all. Every step removed one vertex and one edge, so the tree started with exactly one edge fewer than vertices.

Pruning leaves one at a time removes a vertex and an edge at each step.Pruning leaves one at a time removes a vertex and an edge at each step.

This gives a quick test: a connected graph with n − 1 edges is a tree. But you need both conditions. Four vertices and three edges can still hide a triangle, with the fourth vertex left out on its own.

Spanning trees

Take any connected graph and keep deleting edges that lie on a cycle. By lesson 3, an edge on a cycle is never a bridge, so the graph never falls apart. When no cycles are left, what remains is a tree that still reaches every vertex. It is called a spanning tree: the smallest skeleton that keeps a graph in one piece. So every connected graph has a spanning tree, and it always has n − 1 edges.

Back to the four towns. With every pair of towns joinable, there are 6 possible roads and 20 ways to pick 3 of them. Four of those choices form a triangle and leave one town out. The other 16 are exactly the spanning trees.

Of the 20 ways to choose three roads, 4 are triangles and 16 are spanning trees.Of the 20 ways to choose three roads, 4 are triangles and 16 are spanning trees.

In 1889 Arthur Cayley gave the general rule: with n labelled vertices there are nⁿ⁻² different trees. Four towns give 4² = 16, five give 5³ = 125, and ten give 10⁸, one hundred million.

When the roads have costs, you want the cheapest spanning tree. That is the minimum spanning tree problem, and Kruskal's algorithm solves it by taking the cheapest roads first and skipping any road that would close a cycle.

Trees in the world

Trees are everywhere. The folders on your computer form a tree: every folder sits inside exactly one parent. Rivers work the same way: small streams flow into bigger ones, and each stream joins exactly one larger river. Seen from the space station, a river system branches just like a tree.

Trees also appear inside algorithms. When you explore a graph from one vertex, the edges you use to discover new vertices form a spanning tree. That is where the next chapter begins.

Key ideas

  • A tree is a connected graph with no cycles. Between any two vertices there is exactly one path, and every edge is a bridge.
  • A tree with n vertices has n − 1 edges and, if n ≥ 2, at least two leaves. A forest is a graph whose pieces are all trees.
  • Every connected graph contains a spanning tree, and there are nⁿ⁻² labelled trees on n vertices.

Check yourself

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

2. A forest has 20 vertices and 17 edges. How many trees is it made of?

3. Is a connected graph with 6 vertices and 6 edges a tree?

Practice on the platform

The trees guide proves all of this step by step and goes much further into counting trees, and the minimum spanning trees guide picks up where this lesson stops. In the Kruskal visualizer, draw the four towns with all six roads and run the algorithm. It keeps A-B, B-C and C-D, rejects A-C because it would close a loop, and stops as soon as every town is connected: three roads, one fewer than the four towns.

Up next

Course content

0 of 20 watched

Chapter 1Graph Theory Foundations

Chapter 2Exploring a GraphChapter 1 quiz first

Chapter 3Shortest PathsChapter 2 quiz first

Chapter 4Connecting CheaplyChapter 3 quiz first

Chapter 5Hard ProblemsChapter 4 quiz first

Chapter 6Network FlowsChapter 5 quiz first