Search

Search papers and lessons

Tree (Graph Theory)

Level 2 · 10 min read

A tree is a graph that is connected and has no cycles, so there is exactly one route between any two vertices.

On this page

  • Colour = topic mix
  • Discrete math
  • ReadReady to readNot yet

TL;DR

A tree is a graph in which every two vertices are joined by exactly one path. Equivalently, it is a graph that is connected (you can reach every vertex from every other) and has no cycles (no route that leaves a vertex and returns to it without repeating an edge). Trees are the simplest connected graphs, and they show up wherever a structure has one root and branches, with no loops.

Builds on: Graph · Leads to: Minimum Spanning Tree (the cheapest tree that connects a weighted graph), Breadth-First Search (a search that builds a tree as it goes)

Where it's used

Family trees and org charts. A family tree has one ancestor line per person and no person who is their own ancestor. An org chart in which every employee has exactly one manager, and the chief executive has none, is a tree with the chief executive at the root. The shape tells you who reports to whom without any extra rule.

File systems. A folder contains files and other folders, and each item lives in exactly one folder. Drawing folders as vertices and "contains" as edges gives a tree rooted at the drive. A link that lets a folder appear inside itself would create a cycle, and most file systems forbid it for that reason.

Networks that must stay connected at minimum cost. Telephone, power and water networks often connect many sites with the fewest possible cables. A cable layout that connects every site and has no spare loop is a tree. The question of which tree is cheapest leads to the Minimum Spanning Tree.

How it works

Start with the smallest interesting graph and add one rule at a time.

  1. Start with a single vertex. It is connected (there is nothing to reach) and has no edges, so no cycle. It is a tree.

  2. Add a vertex and one edge. Take vertices A and B and join them with one edge. Every vertex can reach the other, and there is no cycle. This is a tree with 2 vertices and 1 edge.

  3. Add a new vertex joined to one existing vertex. Add C and join it to B. Now A–B–C is a path. Every pair is still joined by exactly one route. Still a tree: 3 vertices, 2 edges.

  4. Try to add an edge between two vertices that already have a route. Join A and C directly. Now there are two routes from A to C: the direct edge, and A–B–C. Two routes mean there is a cycle A–B–C–A. The graph is no longer a tree.

  5. Check the count. Each step that added a new vertex added one edge. A tree on n vertices always has n − 1 edges. This is a useful test: a connected graph with n − 1 edges is a tree, and so is an acyclic graph with n − 1 edges.

The figure below shows the same two steps. The first graph is a tree. Adding the dotted edge creates a cycle.

flowchart LR
  subgraph T[Tree: A-B, B-C]
    A1((A)) --- B1((B))
    B1 --- C1((C))
  end
  subgraph N[Not a tree: add A-C]
    A2((A)) --- B2((B))
    B2 --- C2((C))
    A2 -.- C2
  end

The math

Definition: Tree

A tree is a graph G with vertex set V and edge set E such that G is connected and contains no cycle. A cycle is a closed walk that starts and ends at the same vertex, uses at least three distinct edges, and repeats no edge. Connected means that for every pair of vertices there is a path between them, a walk with no repeated vertex.

Step 4 in symbols: unique paths

If G is a tree and u and v are two of its vertices, there is exactly one path from u to v. To see why, suppose there were two different paths. Following them until they first separate and then first meet again would produce a cycle, which a tree does not contain. So the path is unique.

The converse also holds: if every pair of vertices is joined by exactly one path, the graph is connected and has no cycle, so it is a tree.

Step 5 in symbols: the edge count

For a tree with n vertices, the number of edges is

∣E∣=n−1|E| = n - 1

Here |E| is the number of edges and n is the number of vertices. The reason is that a tree with at least two vertices always has a leaf, which is a vertex of degree 1 (it touches exactly one edge). Removing a leaf and its edge leaves a smaller tree, so the count drops by one vertex and one edge each time. Repeating this until one vertex remains gives n − 1 edges in total.

As a check, the path with 4 vertices has 3 edges: 4 − 1 = 3.

Worked examples

Example
Tiny: is this graph a tree?

Vertices A, B, C, D. Edges: AB, BC, CD.

  1. Count the vertices: n = 4.
  2. Count the edges: 3.
  3. Check the count against the formula: 4 − 1 = 3. It matches.
  4. Check connectedness: A reaches B, B reaches C, C reaches D. Every vertex can reach every other along the path, so the graph is connected.
  5. Check for cycles: the only way back to a vertex is along the same edge, so there is no cycle.
  6. Conclusion: the graph is connected with no cycle, so it is a tree. It is the path A–B–C–D.
Example
Standard: a tree with one extra edge

Vertices 1 to 5. Edges: 12, 13, 24, 25.

  1. Count the vertices: n = 5. Count the edges: 4. The formula gives 5 − 1 = 4, which matches.
  2. Check connectedness. Start at 1: it reaches 2 and 3. From 2 it reaches 4 and 5. Every vertex is reached, so the graph is connected.
  3. Check for cycles. A cycle would need a closed route. The only route through 2 goes 1–2–4 or 1–2–5, and neither returns to 1 without repeating an edge. There is no cycle.
  4. Conclusion: this is a tree. It has one root, 1, with children 2 and 3, and 2 has children 4 and 5.
  5. Now add the edge 34. The new route 1–2–4–3–1 closes a cycle. The graph now has 5 edges on 5 vertices, which is more than n − 1, so it cannot be a tree. The count and the cycle agree.
Example
Realistic: an org chart with a dotted line

A team has 7 people. The chief executive, P1, has no manager. Each of P2 to P7 has exactly one manager, and the managers are chosen so that every reporting line leads up to P1.

  1. Count the reporting lines: each of the 6 people who have a manager contributes one line. So there are 6 edges on 7 vertices. The formula n − 1 gives 6. It matches.
  2. Check connectedness. Every person reaches P1 by following manager links upward, so every pair is connected through P1.
  3. Check for cycles. A cycle would mean someone reports, through a chain of managers, to themselves. Since each person has one manager, the chain from any person upward never revisits a person and ends at P1.
  4. Conclusion: the reporting structure is a tree rooted at P1.
  5. Try it: P5 is given a second, dotted-line manager, P3, while keeping their original manager, P2. Is the reporting structure still a tree? Answer: no. There are now 7 reporting lines on 7 people, which is more than n − 1 = 6. P5 also has two routes up to P1 (through P2 and through P3), so there is a cycle.

Common mistakes and tradeoffs

It is tempting to think that any graph with no cycle is a tree. In fact a graph with no cycle can be disconnected. Two separate paths, A–B and C–D, have no cycles, but they are not a tree because A cannot reach C. A tree must be both connected and acyclic. (The name for an acyclic graph that may be disconnected is a forest, and a forest is a set of trees.)

It is tempting to think that a tree must have a root. In fact the definition has no root. A tree is an unrooted object, and choosing a vertex as the root is a convenience that makes parent-child language possible. The same tree can be rooted at any of its vertices.

It is tempting to think that more edges always means a better network. In fact the minimum number of edges that keeps a network connected is n − 1, and a tree achieves it. Extra edges add redundancy, which is valuable against failure, but each extra edge also creates a cycle. Choose a tree when you want the cheapest connected layout, and add cycles only when you need backup routes.

Use the tree structure when a hierarchy or a single route between any two points is what you need. Do not use it when the relationships genuinely form loops, such as roads in a city grid or friendships in a social network. Those are general graphs.

History

In 1847 Gustav Kirchhoff used trees to analyze electrical networks. He showed that the currents in a circuit could be found by choosing a tree that touched every vertex without loops, which made the analysis systematic. In 1857 Arthur Cayley studied trees as a way to count chemical molecules with a given set of atoms, since molecules without rings have a tree shape. His counting problem became a standard result, now called Cayley's formula, about how many labeled trees exist on n vertices. Trees became a central object of graph theory in the twentieth century because they are the simplest structure that connects everything without redundancy, and because algorithms for search and for finding cheap connections are easiest to state and prove on them.

Recap

  • A graph is a set of vertices and edges. A path is a walk with no repeated vertex. A cycle is a closed walk with no repeated edge that returns to its start.
  • A tree is a graph that is connected and has no cycle.
  • In a tree, every pair of vertices is joined by exactly one path.
  • A tree on n vertices has exactly n − 1 edges, and any connected graph with n − 1 edges is a tree.
  • Trees are the basis for hierarchies, search, and cheapest-connection problems. Read Minimum Spanning Tree next to see how the cheapest tree is chosen, and Breadth-First Search to see how a search builds one.

Check yourself

QuestionRecall: what two properties must a graph have to be a tree?Show answer

It must be connected, so every vertex can be reached from every other, and it must contain no cycle.

QuestionComputation: a tree has 9 vertices. How many edges does it have?Show answer

A tree on n vertices has n − 1 edges. Here n = 9, so 9 − 1 = 8 edges.

QuestionWhy does a tree have exactly one path between any two vertices?Show answer

If there were two different paths between the same two vertices, following them until they split and rejoin would form a cycle. A tree has no cycle, so there can be only one path.

QuestionApplied: a network engineer connects 6 offices with 6 cables and no office is cut off. Is the layout a tree? Explain using the edge count.Show answer

No. A tree on 6 vertices has 6 − 1 = 5 edges. Six cables on six offices is one more than a tree allows, so the layout must contain a cycle. The extra cable provides a backup route, but the layout is not a tree.

Sources