Trees and Spanning Trees
A tree is a connected graph with no cycles: just enough edges to hold everything together, which always works out to n − 1 for n vertices. Every connected graph hides a tree inside it, and to find the cheapest one you can simply keep grabbing the lightest edge that doesn't close a loop.
What the symbols mean
- Edges
The number of edges in the tree.
- Vertices
The number of vertices in the tree.
- Vertex count
How many labeled vertices there are (Cayley's formula).
Explanation
A tree is simply a graph that connects everything with no loops, so there's exactly one way to get between any two points. The name comes from the picture: draw one from a single starting point and it branches out like a real tree, with no branches growing back together. Trees are good for building the cheapest network that still reaches everyone, and for organizing things in levels, like folders on a computer. The formulas are two short facts: (a tree always has one fewer edge than it has vertices) and (how many different trees you can draw on labeled vertices). Nothing scary, just two numbers to know.
Why exactly edges? Build the tree one edge at a time. Start with separate vertices, so pieces. Every edge you add without making a loop joins two separate pieces into one, so the number of pieces drops by exactly 1. You're done when there's one piece, and that takes edges.
That's why there are several equivalent definitions of a tree on vertices: connected with no cycles; connected with edges; no cycles with edges; exactly one path between any two vertices. A tree sits right on the edge. Remove any edge and it falls apart into two pieces; add any edge and it gains exactly one cycle, made of the new edge and the one path that already joined its ends.
Rooted trees pick one vertex as the top, and then every other vertex has a parent, the next vertex up toward the root. In a binary tree each vertex has at most two children, so each level can be twice as big as the one above, and depth allows up to leaves. Turned around, that means you can reach any of leaves in only steps, which is why binary search trees and tournament brackets are so efficient.
Spanning trees. A spanning tree of a connected graph uses all its vertices and some of its edges, just enough to stay connected. You can always find one: as long as there's a cycle, remove one of its edges, which can't disconnect anything. Cayley's formula counts the spanning trees of : .
Minimum spanning trees. Now give each edge a cost and ask for the cheapest spanning tree. Kruskal's algorithm sorts the edges by weight and adds each one unless it would create a cycle. Prim's algorithm grows a single tree from one vertex, always adding the cheapest edge leaving it. Both are greedy, meaning they grab the best-looking option at each step and never take it back. Greedy methods usually aren't guaranteed to win, but for spanning trees both always give a minimum one.
A common mistake. It's tempting to treat Kruskal like Prim and only add edges that touch what you've built so far. Kruskal doesn't care about that. It can build several separate little trees at once, as long as no edge closes a cycle, and they merge into one tree by the end.
See it
Worked examples
Weighted edges: AB 1, BC 2, AC 3, CD 4, BD 5. Find a minimum spanning tree with Kruskal's algorithm.
- 1
Go through the edges from lightest to heaviest. AB (1) and BC (2) don't close any cycle, so take both.
Practice
Type your answer and check it. Solve them all to complete the lesson.
How many edges does a tree with 25 vertices have?
How many labeled trees are there on 4 vertices (Cayley's formula)?
How many leaves does a complete binary tree of depth 5 have?
Adding one edge to a tree always creates:
A company will link 6 offices with fiber. The possible links cost AB 4, AC 3, BC 2, BD 5, CD 7, CE 6, DE 1, DF 8 and EF 9 (in tens of thousands). What does the cheapest network that connects every office cost, in the same units?
How many spanning trees does have?
The hardest one here. Get it, and you've got this lesson.
A tree has 4 vertices of degree 3, 2 vertices of degree 2, and every other vertex is a leaf. How many leaves does it have?
Where it's used
Network designers connect sites with the least cable using minimum spanning trees, and file systems, decision trees and family trees are all rooted trees.