Trees, Spanning Trees and Shortest Paths
Learn the properties of trees, build minimum spanning trees with Kruskal's algorithm, find shortest paths with Dijkstra's algorithm and traverse graphs.
Welcome back!
Step 1 of 11Learn
Trees
A tree is a connected graph with no cycles. Family trees, file systems, organization charts and the structure of a web page's HTML are all trees.
Trees have a remarkable list of equivalent properties. For a graph with n vertices, these are all equivalent:
- It is a tree (connected, no cycles).
- It is connected and has exactly n − 1 edges.
- It has no cycles and has exactly n − 1 edges.
- There is exactly one path between any two vertices.
- It is connected, but removing any edge disconnects it.
So a tree is the most economical way to connect n points: n − 1 edges, no redundancy. Add one edge and you create exactly one cycle; remove one and it falls apart.
A leaf is a vertex of degree 1. Every tree with at least two vertices has at least two leaves: walk along the longest path, and its endpoints can't have any other neighbors without creating a longer path or a cycle.
A rooted tree has one vertex singled out as the root, with every other vertex having a parent. In a binary tree, each vertex has at most two children. Binary search trees store sorted data so that looking up an item takes about log₂ n steps: a balanced tree holding a million items is only about 20 levels deep.
A forest is a graph with no cycles, a collection of trees. A forest with n vertices and c trees has n − c edges.
Step 2 of 11Diagram
A file system is a tree
7 vertices, 6 edges: a tree with n vertices always has n − 1 edges.
Tap a shape with to see more.
- / (root)
- home
- etc
- ana
- ben
- notes.txt
A leaf: a vertex of degree 1.
- hosts
Another leaf.
Step 3 of 11Practice · Work it out
A tree has 25 vertices. How many edges does it have?
Step 4 of 11Learn
Minimum spanning trees
A telecom company needs to connect five towns, A through E, with fiber cable. The picture shows the possible links and their costs in millions of dollars. Every town must be reachable from every other, and the total cost should be as small as possible.
The cheapest network will never contain a cycle: if it did, you could remove the most expensive edge of the cycle and stay connected for less. So the answer is a spanning tree (a tree using all the vertices) of minimum total weight, a minimum spanning tree (MST).
Kruskal's algorithm builds one greedily:
- Sort the edges from cheapest to most expensive.
- Go through them in order, adding an edge unless it would create a cycle.
- Stop when you have n − 1 edges.
For the graph shown, the edges in order are B–C (1), A–C (2), D–E (2), A–B (4), B–D (5), B–E (6), C–D (8), C–E (10).
- Add B–C (1) and A–C (2) and D–E (2).
- Skip A–B (4): A and B are already connected through C.
- Add B–D (5), joining the two groups.
That's 4 edges for 5 vertices, so stop. Total cost: 1 + 2 + 2 + 5 = 10.
Greedy choices usually don't give optimal answers, but for spanning trees they provably do. Prim's algorithm is a second greedy method: grow a single tree from any starting vertex, always adding the cheapest edge leaving it. It finds the same total.
Step 5 of 11Practice · Put in order
In what order does Kruskal's algorithm add edges to the minimum spanning tree of the graph shown? (Break the tie between the two weight-2 edges alphabetically.)
Drag the items, or use the arrow buttons, to put them in order.
- D–E (weight 2)
- B–C (weight 1)
- A–C (weight 2)
- B–D (weight 5)
Step 6 of 11Learn
Dijkstra's shortest path algorithm
Now a different question about the same graph: what's the cheapest route from A to E? A spanning tree minimizes the total network cost, not the distance between two particular towns, so this needs another algorithm.
Dijkstra's algorithm finds shortest paths from a starting vertex to all others, for graphs with nonnegative weights. It keeps a tentative distance for each vertex (0 for the start, ∞ for the rest) and repeats:
- Pick the unvisited vertex with the smallest tentative distance and mark it visited; its distance is now final.
- For each neighbor, check whether going through this vertex is shorter than its current tentative distance, and update if so.
From A in the graph shown:
- Visit A (0). Update B to 4 and C to 2.
- Visit C (2). Through C: B becomes min(4, 2 + 1) = 3, D becomes 2 + 8 = 10, E becomes 2 + 10 = 12.
- Visit B (3). Through B: D becomes min(10, 3 + 5) = 8, E becomes min(12, 3 + 6) = 9.
- Visit D (8). Through D: E would be 8 + 2 = 10, worse than 9.
- Visit E (9). Done.
The shortest route from A to E costs 9: A → C → B → E. Notice that the direct-looking A → B costs 4, but going around through C to reach B costs only 3.
GPS navigation, network routing protocols and game pathfinding all use Dijkstra's algorithm or refinements of it, such as A* search, which adds an estimate of the remaining distance to explore promising directions first.
Step 7 of 11Practice · Work it out
Using the graph shown, what is the length of the shortest path from A to D?
Step 8 of 11Tap to reveal
Searching a graph: BFS and DFS
Breadth-first search (BFS)
Explore all neighbors of the start, then all their neighbors, and so on, layer by layer, using a queue. In an unweighted graph, BFS finds the path with the fewest edges, such as "degrees of separation" in a social network.
Depth-first search (DFS)
Go as deep as possible along one branch before backing up, using a stack or recursion. DFS is used to detect cycles, solve mazes and order tasks so that every task comes after the tasks it depends on (topological sorting).
Running time
Both visit every vertex and edge once, so they run in time proportional to |V| + |E| with adjacency lists, fast enough for graphs with millions of vertices.
Spanning trees for free
The edges BFS or DFS uses to discover new vertices form a spanning tree of a connected graph, another way to see that every connected graph has one.
Step 9 of 11Note
Step 10 of 11Practice · True or false
If a graph with 10 vertices is connected and has exactly 9 edges, then it is a tree.
Step 11 of 11Practice · Choose one
What is the total weight of a minimum spanning tree for the graph shown?
Lesson complete!