Spanning trees and minimum spanning tree algorithms
IB Mathematics AI HLΒ· 45 min read
1. What is a Spanning Tree?β β ββββ± 10 min
Spanning Tree
A connected, acyclic subgraph of a connected undirected graph that includes every vertex from the original graph. For a graph with vertices, any spanning tree will always have exactly edges.
Example:
A spanning tree for a 4-vertex graph will always have exactly 3 edges.
Most connected graphs have multiple possible spanning trees. Spanning trees simplify the original graph by removing cycles while retaining connectivity between all vertices, which makes them useful for network design problems.
Draw one valid spanning tree for a connected graph with 4 vertices and edges: .
- 1
Count the number of vertices: , so the spanning tree needs edges.
- 2
Select 3 edges that connect all vertices with no cycles. One valid selection is edges .
- 3
Verify: all 4 vertices are included, the subgraph is connected, and there are no cycles. This is a valid spanning tree.
2. Minimum Spanning Trees (MSTs)β β ββββ± 10 min
Minimum Spanning Tree (MST)
Total weight: where is the spanning tree
For a weighted connected undirected graph, an MST is a spanning tree where the sum of edge weights is smaller than or equal to the total weight of any other spanning tree for the graph.
Example:
An MST connecting 3 cities with edge weights 2, 3, 5 has a total weight of 5, the sum of the two smallest connecting edges.
MSTs are used for real-world problems like finding the lowest-cost pipeline network connecting multiple towns, or designing a minimum-cost fiber-optic network. Any weighted connected graph has at least one MST; if all edge weights are distinct, there is exactly one MST.
A graph has 3 vertices with edge weights , , . Find the total weight of the MST.
- 1
For vertices, the MST needs edges.
- 2
Select the two smallest edges that connect all vertices without cycles: (weight 2) and (weight 3).
- 3
Sum the weights: . This is the total weight of the MST.
3. Kruskal's Algorithmβ β β βββ± 15 min
Kruskal's algorithm is a greedy algorithm that builds the MST by adding edges in order of increasing weight, skipping any edge that creates a cycle.
List all edges in order of increasing weight
Start with an empty set of edges for the MST
Add the next smallest edge if it does not create a cycle with existing edges
Repeat until you have edges (all vertices connected)
Use Kruskal's algorithm to find the MST total weight for a 4-vertex graph with edges: .
- 1
Sort edges by weight: .
- 2
Add : no cycle, current edges: , connected vertices: .
- 3
Add : no cycle, current edges: , connected vertices: .
- 4
Add : no cycle, connects D, current edges: (3 edges for 4 vertices, done).
- 5
Total weight: .
4. Prim's Algorithmβ β β βββ± 15 min
Prim's algorithm is also a greedy algorithm, but it builds the MST by starting at a single vertex and repeatedly adding the smallest edge that connects a new vertex to the existing tree.
Choose any starting vertex and add it to the MST set
Find the smallest weight edge connecting a vertex inside the MST set to a vertex outside
Add this edge and the new vertex to the MST set
Repeat until all vertices are added to the MST set
Use Prim's algorithm starting at to find the MST total weight for the same 4-vertex graph: .
- 1
Start with MST set: , no edges added.
- 2
Smallest edge from to outside is . Add and , MST set: .
- 3
Smallest edge from to outside is . Add and , MST set: .
- 4
Smallest edge from to outside is . Add and , all vertices included.
- 5
Total weight: , same result as Kruskal's algorithm.
Both algorithms produce the correct MST, but they are suited to different input formats:
Kruskal's
Sorts edges first, adds edges avoiding cycles
+ Pros: Easy for edge lists, sparse graphs
β Cons: Requires cycle checking each step
Prim's
Builds from a starting vertex, adds nearest vertex
+ Pros: No explicit cycle checking, good for adjacency matrices
β Cons: Less intuitive for beginners with arbitrary start points
5. Common Pitfalls
Wrong move:
Stopping with the wrong number of edges
Why:
Forgetting that an -vertex spanning tree always has exactly edges
Correct move:
Calculate at the start of the problem, stop once you have that many edges
Wrong move:
Adding a cycle-forming edge in Kruskal's algorithm
Why:
Forgetting to check for cycles when adding the next smallest edge
Correct move:
Always confirm the new edge connects a vertex not already in your connected component
Wrong move:
Believing starting vertex changes MST total weight in Prim's
Why:
Assuming different start points give different total weights
Correct move:
Any starting vertex will give the same total MST weight, even if edge sets differ
Wrong move:
Confusing minimum and maximum spanning trees
Why:
Selecting largest edges instead of smallest by mistake
Correct move:
Double-check the question asks for minimum, always start with the smallest weight edges
Wrong move:
Adding extra edges after all vertices are connected
Why:
Continuing to add edges instead of stopping early
Correct move:
Stop as soon as all vertices are connected, extra edges only add to the total weight and create cycles
6. Quick Reference Cheatsheet
Concept/Algorithm | Key Steps | Key Property |
|---|---|---|
Spanning Tree | Include all vertices, connected, no cycles | vertices edges |
Minimum Spanning Tree | Spanning tree with minimum total edge weight | At least one MST exists for all connected weighted graphs |
Kruskal's | Sort edges by weight, add smallest, avoid cycles, stop at edges | Best for edge lists / sparse graphs |
Prim's | Start at any vertex, add smallest edge to new vertex, repeat until all included | Best for adjacency matrices / dense graphs, no cycle check needed |
When this came up on past exams
AI-estimated based on syllabus patterns β cross-check with official past papers for accuracy. Use only as revision-focus signals.
- 2025 Β· Paper 1
MST optimization problem
- 2023 Β· Paper 2
Apply Prim's algorithm
- 2021 Β· Paper 1
Compare Kruskal and Prim methods
Going deeper
What's Next
Spanning trees and MST algorithms are core concepts for graph theory optimization in IB AI HL. They form the foundation for more advanced network problems like shortest path finding and maximum flow, which are also commonly tested in exams. MST problems often appear in both Paper 1 and Paper 2, usually paired with real-world contexts like transport network design or communications infrastructure. Practicing both algorithms until you can apply them confidently will help you earn full marks on these common exam questions.
