Minimum Spanning Trees
Revise Minimum Spanning Trees for Further Decision Mathematics 1 WDM11 (AS Level) — revision notes and instant AI marking. Free to start.
Minimum Spanning Trees
The big idea: when you need to connect a bunch of places together as cheaply as possible — with no wasted extra connections and no wasted loops — you're looking for a minimum spanning tree, and Kruskal's and Prim's algorithms are just two different systematic ways of building one.
- A network is a weighted graph — vertices (nodes) joined by edges (arcs) that carry a "weight" like distance, cost, or time.
- Networks can be represented as adjacency matrices (counts of direct connections) or distance matrices (weights of connections).
- A spanning tree connects every vertex in a network using the fewest possible edges, with no cycles.
- A minimum spanning tree (MST) is the spanning tree with the smallest possible total weight.
- Kruskal's algorithm builds the MST by sorting all edges by weight and greedily adding the cheapest one that doesn't create a cycle — it works well from a list of edges or a graph.
- Prim's algorithm builds the MST by growing outward from a single starting vertex, always adding the cheapest edge that connects the "grown" tree to a new vertex — it works well from a graph a matrix.
- Both algorithms always find minimum spanning tree — they might select edges in a different order, but the total weight will be the same.
- The number of edges in any spanning tree of a network with
nvertices is alwaysn − 1.
What is a network?
Think of a network as a map. The dots on the map are called vertices (or nodes) — these might represent towns, computers, or buildings. The lines connecting the dots are called edges (or arcs) — these represent a road, a cable, a flight route, whatever links two places together.
What makes a network special (compared to just any graph) is that every edge has a weight attached to it — a number representing something real, like distance in km, cost in £, or time in minutes. That's why a network is formally called a weighted graph.
Imagine a road map of five towns where every road has a "toll price" written on it. That map — towns as dots, roads as lines, toll prices as weights — a network. Everything in this chapter is really just: "what's the cheapest way to connect all five towns using some of these roads?"
Adjacency matrix vs. distance matrix
A matrix is just a table used to store all the connection information about a network in a grid, instead of drawing it out. There are two matrix types you need to tell apart:
Adjacency matrix
This tells you how many direct connections exist between each pair of vertices — it does care about weight, only about "is there a link, and if so how many?" Every vertex is listed as both a row heading and a column heading, so the matrix is always square.
- An entry of
0means no direct connection between that pair of vertices. - In a simple graph, entries are only ever 0 or 1.
- A loop (an edge that starts and ends at the same vertex) shows up on the leading diagonal (top-left to bottom-right). In an matrix this value is
2, because you can travel round the loop in two directions. In a matrix it's1, because the loop only goes one way. - An undirected graph's adjacency matrix is always symmetrical about the leading diagonal — the connection from A to B is identical to the connection from B to A.
Distance matrix
This is the one you'll use for MST problems. Instead of counting connections, each cell holds the weight of the edge between that pair of vertices (distance, cost, time — whatever the network measures). An empty cell means there's no direct edge between those two vertices at all.
A directed network's distance matrix will be symmetrical, because travelling A→B might cost something different from travelling B→A. When you're asked to draw a network from a distance matrix, check carefully: if the two "mirror" cells between a pair of vertices hold different numbers, you need two separate directed edges between them, each labelled with its own weight and arrow.
Given the adjacency matrix below (for graph with vertices A–E), the value 2 on E's row/column diagonal tells you E has an undirected loop. The value 0 in row B, column C tells you there's no direct edge B→C.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 1 | 1 | 0 | 1 |
| B | 1 | 0 | 0 | 0 | 0 |
| C | 1 | 0 | 1 | 1 | 1 |
| D | 0 | 0 | 0 | 0 | 1 |
| E | 2 | 1 | 1 | 1 | 2 |
Spanning tree vs. minimum spanning tree
A spanning tree of a network is a subset of the edges that:
- connects every single vertex in the network (nothing gets left out), and
- contains no cycles (no loops — you can't go round in a circle and come back to where you started using only tree edges).
Because a tree with n vertices and no cycles always has exactly n − 1 edges, there's a really useful check built in: if you ever end up with more than n − 1 edges, you've made a mistake (you've allowed a cycle in). If you end up with fewer, you haven't finished — some vertex still isn't connected.
There are usually possible spanning trees for a given network. The minimum spanning tree (MST) is simply the one out of all those possibilities whose edges add up to the smallest total weight.
MSTs model real situations like: laying the least amount of cable to connect every building on a campus to the network, or building the cheapest possible set of roads that still lets you get from any town to any other town. You don't need possible road — just enough to connect everything, as cheaply as possible.
How it works
Kruskal's algorithm takes a completely different approach from "building outward" — instead, it looks at every edge in the whole network at once, sorted from cheapest to most expensive, and just keeps grabbing the next cheapest one . "Safe" means it doesn't close a loop with the edges you've already picked.
The steps
- Step 1: List every edge in the network in order of increasing weight (cheapest first).
- Step 2: Select the edge of least weight. Add it to your tree.
- Step 3: Look at the next-cheapest edge that you haven't already considered.
- If adding it would create a cycle with edges already in your tree → reject it.
- Otherwise → add it to your tree.
- Step 4: Repeat Step 3 until all vertices are connected (i.e. until you have
n − 1edges). - Step 5: List the edges of your finished MST in the order you added them — this is what examiners want to see as "working."
Kruskal's algorithm doesn't care where in the network an edge is — it can pick up an edge on the "far side" of the network before it's even touched the "near side." This means your tree can be split into several disconnected pieces partway through the algorithm — that's completely normal and expected. It only needs to be one single connected tree by the very end.
Worked example
Consider this network with vertices A, B, C, D, E, F, G, H and these edges: AB(4), AD(9), AE(11), BC(6), BF(7), CD(8), CG(15), DH(2), EF(5), EH(1), FG(5), GH(6).
Prim's algorithm using a graph
Where Kruskal's algorithm scans the network for the cheapest safe edge anywhere, Prim's algorithm is much more local: it starts at one vertex and grows the tree outward like a plant, always reaching for the cheapest edge that's directly touching the tree it's already built.
The steps (using a graph)
- Step 1: Start at any vertex and choose the edge of least weight that's connected to it.
- Step 2: Choose the edge of least weight that is incident (connected) to any vertex already in the tree, but which connects to a vertex not yet in the tree. (If there's a tie for cheapest, either can be picked.)
- Step 3: Repeat Step 2 until every vertex has been added.
Prim's tree is always one single connected blob at every stage — it can never be split into separate pieces the way Kruskal's can. Every new edge must attach directly onto the tree you already have, like adding a new branch onto a growing plant, never planting a second plant somewhere else in the garden.
Worked example (using a graph)
Network with vertices A, B, C, D, E and edges: AB(15), AC(22), AD(18), AE(25), BC(13), BD(17), BE(24), CD(30), CE(12), DE(21). Starting at A:
At each step you must check every edge coming out of the whole current tree — not just edges from the vertex you added last! For example after adding C, the next cheapest option could come from A, B, C — you always compare against the entire tree so far.
Prim's algorithm using a matrix
This is the version you'll use when the network is given as a table rather than a drawing — and it's the method examiners often expect for matrix-based questions, since Kruskal's is awkward to apply directly from a table.
The steps (using a matrix)
- Step 1: Pick any starting vertex. Cross out the values in the column for that vertex, and label its row with the number
1. - Step 2: Circle the lowest value in any cell along that labelled row. Add that edge to your tree. Cross out the remaining values in the column of the cell you just circled.
- Step 3: Label the row that corresponds to the same vertex as the column you just crossed out, with the next number (2, then 3, and so on).
- Step 4: Circle the lowest value in any cell along any labelled row (there may now be several labelled rows to check). Add the edge, cross out that column.
- Step 5: Repeat Steps 3–4 until every vertex has been added to the tree.
Crossing out the column stops you from ever selecting an edge that leads back a vertex that's already safely in your tree — which is exactly how the matrix method avoids creating cycles, without you having to manually check for loops the way you do in Kruskal's.
Worked example
Distance matrix for A–E (same network as above), starting at A:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | – | 15 | 22 | 18 | 25 |
| B | 15 | – | 13 | 17 | 24 |
| C | 22 | 13 | – | 30 | 12 |
| D | 18 | 17 | 30 | – | 21 |
| E | 25 | 24 | 12 | 21 | – |
Choosing the right algorithm
Both algorithms are guaranteed to find minimum spanning tree for the network — they might pick edges in a different order, and even choose slightly different tied-weight edges, but the total weight of the finished tree will always come out the same.
- Prim's algorithm can be used on a network given as a graph or as a matrix. If your question gives you a table/matrix, Prim's is the natural choice.
- Kruskal's algorithm works best when the network is given as a graph, or as a straightforward list of edges. If you only have a matrix, you'd normally sketch the graph first before applying Kruskal's.
- Kruskal's lets you add arcs in any order across the whole network — the tree can be split into separate unconnected pieces mid-algorithm. Prim's always builds one connected blob from the start.
- Prim's is sometimes considered more efficient, because it doesn't require sorting every edge up front, and it doesn't need an explicit cycle-check at each step (the "cross out the column" trick handles that automatically).
A common question style says "certain edges be included in the network" (e.g. because a road is already built). The trick: draw those required edges in first, then complete the rest of the tree using Kruskal's algorithm on the remaining edges — this is much easier to adapt than Prim's when specific starting edges are forced on you.
Read the full Minimum Spanning Trees notes free
That's the preview — create a free account to read the rest, plus flashcards and practice questions with instant AI marking. No credit card.
Unlock the full notes free →