Minimum Spanning Trees

A minimum spanning tree (MST) of a connected weighted graph is a spanning tree whose edge weights add up to the smallest possible total. It connects every vertex with no cycles, using n - 1 edges. Kruskal’s algorithm and Prim’s algorithm both find one by greedily choosing the cheapest safe edge.

Before this lesson: Trees in Graph Theory, Graph Theory Basics and Dijkstra’s Algorithm

What is a minimum spanning tree?

A spanning tree of a connected graph is a subgraph that includes every vertex and is a tree. Since it is a tree with n vertices, it always has exactly n - 1 edges. Most graphs have many spanning trees: the graph in the figure below has 55 of them.

In a weighted graph, the weight of a spanning tree is the sum of its edge weights. A minimum spanning tree is a spanning tree with the smallest weight. Think of connecting towns with cable: every town must be reached, a loop of cable would waste money, and you want the cheapest network.

Both algorithms in this lesson are greedy: at each step they take the cheapest edge that is still allowed. For many problems a greedy choice can go wrong, but for minimum spanning trees it is always correct.

A weighted graph with vertices A to F and edges AB 6, AC 3, BC 4, BD 2, CD 5, CE 7, DE 8, DF 9 and EF 1. The minimum spanning tree edges EF, BD, AC, BC and CE are drawn as thick solid blue lines; the other edges AB, CD, DE and DF are thin dashed gray lines. The total weight of the tree is 17.
The minimum spanning tree (thick blue edges) has 5 edges and total weight 1 + 2 + 3 + 4 + 7 = 17.

Kruskal’s and Prim’s algorithms

Kruskal’s algorithm

  1. Sort all edges from lowest weight to highest.
  2. Go through the edges in that order. Add an edge if it does not form a cycle with the edges already chosen; otherwise skip it.
  3. Stop when n - 1 edges have been chosen.

An edge forms a cycle exactly when its two endpoints are already joined by chosen edges. So while Kruskal’s algorithm runs, the chosen edges form a forest, and each new edge joins two of its trees.

Prim’s algorithm

  1. Start with any one vertex as the tree.
  2. Among all edges with one end in the tree and one end outside it, add the cheapest one, together with its outside vertex.
  3. Repeat until every vertex is in the tree.

Prim’s algorithm keeps a single growing tree, much like Dijkstra’s algorithm. The difference is the number it compares: Prim’s uses only the weight of the new edge, while Dijkstra’s uses the total distance from the start.

Why the greedy choice works

Two facts make both algorithms correct when all the edge weights are different.

  • Cut property: split the vertices into two groups. The cheapest edge joining the two groups is in every minimum spanning tree.
  • Cycle property: the most expensive edge on any cycle is in no minimum spanning tree.

Every edge Prim’s algorithm adds is the cheapest edge across the cut between the tree and the other vertices. Every edge Kruskal’s algorithm adds is the cheapest edge leaving one of the forest’s trees, and every edge it skips is the heaviest edge on the cycle it would have made. When all the weights are different, the minimum spanning tree is unique. With ties there may be several, but they all have the same total weight.

Minimum spanning tree examples

Example 1: Kruskal’s algorithm

Find a minimum spanning tree of the graph in the figure with Kruskal’s algorithm. Its edges are AB 6, AC 3, BC 4, BD 2, CD 5, CE 7, DE 8, DF 9 and EF 1.

n = 6 \Rightarrow n - 1 = 5

The tree needs 5 edges.

1, 2, 3, 4, 5, 6, 7, 8, 9

Sort the weights: EF, BD, AC, BC, CD, AB, CE, DE, DF.

EF = 1

Add EF. Chosen: {E, F}.

BD = 2

Add BD: B and D are not yet joined.

AC = 3

Add AC: A and C are not yet joined.

BC = 4

Add BC: it joins {A, C} and {B, D} into {A, B, C, D}.

CD = 5 \;\text{skip}

Skip CD: C and D are already joined through B, so it would make the cycle C, B, D.

AB = 6 \;\text{skip}

Skip AB: A and B are already joined through C.

CE = 7

Add CE: it joins {A, B, C, D} and {E, F}.

1 + 2 + 3 + 4 + 7 = 17

Five edges are chosen, so stop and add the weights.

The minimum spanning tree is EF, BD, AC, BC, CE with total weight 17. The edges DE and DF never need to be checked.

Example 2: Prim’s algorithm

Find a minimum spanning tree of the same graph with Prim’s algorithm, starting at A.

\{A\}

Start the tree at A.

AC = 3 \lt AB = 6

Edges leaving the tree: AB 6, AC 3. Add AC.

BC = 4

Edges leaving {A, C}: AB 6, BC 4, CD 5, CE 7. Add BC.

BD = 2

Edges leaving {A, B, C}: BD 2, CD 5, CE 7. Add BD.

CE = 7

Edges leaving {A, B, C, D}: CE 7, DE 8, DF 9. Add CE.

EF = 1

Edges leaving {A, B, C, D, E}: DF 9, EF 1. Add EF.

3 + 4 + 2 + 7 + 1 = 17

Every vertex is in the tree. Add the weights.

Prim’s algorithm chooses the edges in a different order (AC, BC, BD, CE, EF), but it finds the same tree with weight 17. That has to happen here, because all the weights are different.

Example 3: Ties and more than one MST

A square ABCD has sides AB, BC, CD and DA of weight 2 and a diagonal AC of weight 3. Find a minimum spanning tree, and decide whether it is unique.

n - 1 = 3

Four vertices, so the tree needs 3 edges.

AB = 2,\; BC = 2

Kruskal’s algorithm may take the weight-2 edges in any order. Add AB and BC.

CD = 2

Add CD: D is not yet joined. Now 3 edges are chosen.

2 + 2 + 2 = 6

Add the weights.

4 \;\text{trees}

Any 3 of the 4 sides form a spanning tree of weight 6, so there are 4 minimum spanning trees.

The minimum weight is 6, but the tree is not unique. The diagonal AC is in none of them: it is the heaviest edge on the cycle A, B, C, A.

Example 4: Using the cut and cycle properties

Without running an algorithm, show that CE must be in every minimum spanning tree of the graph in the figure, and that DF cannot be in any.

\{A, B, C, D\} \;\text{and}\; \{E, F\}

Split the vertices into two groups.

CE = 7,\; DE = 8,\; DF = 9

List the edges joining the two groups.

CE = 7 \;\text{is cheapest}

By the cut property, CE is in every MST.

D \to E \to F \to D

Find a cycle that contains DF.

DF = 9 \gt DE = 8 \gt EF = 1

DF is the heaviest edge on that cycle.

DF \notin \text{MST}

By the cycle property, DF is in no MST.

Both facts match Example 1: CE was added, and DF was never needed.

Example 5: Wiring a campus (word problem)

A college wants to connect five buildings with fiber cable: the library (L), gym (G), science hall (S), dorm (D) and arts center (A). The table gives the cost of each possible link in thousands of dollars. Find the cheapest network that connects every building.

LinkCostLinkCost
L–G12G–D7
L–S8G–A14
L–D15S–D11
L–A10S–A6
G–S9D–A13
\{L\}

Use Prim’s algorithm, starting at the library.

LS = 8

Links from L: LG 12, LS 8, LD 15, LA 10. Add LS.

SA = 6

Links from {L, S}: SA 6 is the cheapest. Add SA.

SG = 9

Links from {L, S, A}: SG 9, SD 11, LG 12, AD 13, AG 14, LD 15. Add SG.

GD = 7

Links from {L, S, A, G}: GD 7, SD 11, DA 13, LD 15. Add GD.

8 + 6 + 9 + 7 = 30

All five buildings are connected. Add the costs.

The cheapest network uses the links L–S, S–A, S–G and G–D and costs $30,000. Kruskal’s algorithm gives the same four links (SA 6, GD 7, LS 8, GS 9).

Common mistakes

MistakeExample of the errorFix
Adding an edge that closes a cycle in Kruskal’s algorithmIn Example 1, CD (5) is added after BD and BC.Before adding an edge, check whether its endpoints are already joined by chosen edges.
Taking the cheapest edge anywhere in Prim’s algorithmStarting at A, EF (1) is added first.Prim’s algorithm only uses edges with exactly one end in the current tree.
Choosing the wrong number of edgesA spanning tree of 6 vertices is given 6 edges.A spanning tree of n vertices always has n - 1 edges.
Using an MST to find a shortest pathIn Example 1, the MST route from A to D is A, C, B, D, of length 9, but the route A, C, D has length 8.An MST minimizes the total network cost, not the distance between two vertices. Use Dijkstra’s algorithm for shortest paths.
Assuming the MST is always uniqueIn Example 3, only one of the 4 trees is reported as “the” MST.With tied weights there can be several MSTs, all with the same total weight.
Running the algorithms on a disconnected graphPrim’s algorithm stops before reaching every vertex.A disconnected graph has no spanning tree; Kruskal’s algorithm then gives a minimum spanning forest.

Related ideas

Maximum spanning trees. Running Kruskal’s algorithm from the highest weight down gives a spanning tree with the largest total weight. This is useful when weights measure capacity or reliability instead of cost.

Speed. Kruskal’s algorithm spends most of its time sorting, about e \log e steps for e edges. Prim’s algorithm with a priority queue, the same structure used in Dijkstra’s algorithm, also runs in about e \log n steps. Kruskal’s is often easier by hand on sparse graphs; Prim’s is easier when the costs are given as a table.

Traveling salesperson tours. A minimum spanning tree weighs no more than the cheapest round trip through all the vertices, because removing one edge of a tour leaves a spanning tree. This gives a quick lower bound for the Hamilton circuit problems in the traveling salesperson section.

Counting spanning trees. A cycle with n vertices has exactly n spanning trees (remove any one edge), and the complete graph K_n has n^{n-2} of them by Cayley’s formula, from trees in graph theory.

Practice problems

Problems 2, 3, 8 and 9 use the graph H with vertices P, Q, R, S, T and edges PQ 7, PR 5, QR 4, QS 6, RS 8, RT 9 and ST 3.

  1. How many edges does a spanning tree of a connected graph with 12 vertices have?
  2. Use Kruskal’s algorithm to find a minimum spanning tree of H and its weight.
  3. Use Prim’s algorithm starting at T to find a minimum spanning tree of H. List the edges in the order they are added.
  4. A connected graph has 7 vertices and 15 edges. How many edges must be removed to leave a spanning tree?
  5. How many spanning trees does a cycle with 6 vertices have?
  6. A triangle has edges of weight 4, 7 and 9. Which edge is left out of the minimum spanning tree, and what is its weight?
  7. A triangle has three edges of weight 5. How many minimum spanning trees does it have, and what is their weight?
  8. Find a maximum spanning tree of H and its weight.
  9. Use the cycle property to show that PQ is not in the minimum spanning tree of H.
  10. Four villages W, X, Y, Z need water pipes. The costs in thousands of dollars are W–X 10, W–Y 14, W–Z 9, X–Y 8, X–Z 12 and Y–Z 11. Find the cheapest way to connect all four villages.
Show answers

1.

n - 1 = 12 - 1 = 11

A spanning tree of n vertices has n - 1 edges.

The spanning tree has 11 edges.

2.

3, 4, 5, 6, 7, 8, 9

Sort the edges: ST, QR, PR, QS, PQ, RS, RT.

ST = 3,\; QR = 4

Add ST and QR.

PR = 5

Add PR: P is not yet joined.

QS = 6

Add QS: it joins {P, Q, R} and {S, T}.

3 + 4 + 5 + 6 = 18

Four edges for 5 vertices, so stop. Add the weights.

The MST is ST, QR, PR, QS with weight 18.

3.

TS = 3

Edges from T: TS 3, TR 9. Add TS.

SQ = 6

Edges from {S, T}: SQ 6, SR 8, TR 9. Add SQ.

QR = 4

Edges from {Q, S, T}: QR 4, QP 7, SR 8, TR 9. Add QR.

RP = 5

Edges from {Q, R, S, T}: RP 5, QP 7. Add RP.

3 + 6 + 4 + 5 = 18

Add the weights.

The order is TS, SQ, QR, RP, and the weight is 18, the same tree as in problem 2.

4.

n - 1 = 6

A spanning tree of 7 vertices has 6 edges.

15 - 6 = 9

Subtract from the number of edges.

9 edges must be removed.

5.

\text{remove 1 of 6 edges}

Removing any one edge of a cycle leaves a path, which is a spanning tree.

A 6-vertex cycle has 6 spanning trees.

6.

9 \gt 7 \gt 4

The edge of weight 9 is the heaviest edge on the only cycle.

4 + 7 = 11

The MST keeps the other two edges.

The weight-9 edge is left out, and the MST has weight 11.

7.

5 + 5 = 10

Any 2 of the 3 edges form a spanning tree of weight 10.

\binom{3}{2} = 3

Count the ways to choose 2 of the 3 edges with combinations.

There are 3 minimum spanning trees, each of weight 10.

8.

9, 8, 7, 6, 5, 4, 3

Sort the edges from highest to lowest: RT, RS, PQ, QS, PR, QR, ST.

RT = 9,\; RS = 8

Add RT and RS.

PQ = 7

Add PQ: it starts a new piece {P, Q}.

QS = 6

Add QS: it joins {P, Q} and {R, S, T}.

9 + 8 + 7 + 6 = 30

Four edges, so stop. Add the weights.

The maximum spanning tree is RT, RS, PQ, QS with weight 30.

9.

P \to Q \to R \to P

PQ lies on the cycle P, Q, R.

PQ = 7 \gt PR = 5 \gt QR = 4

PQ is the heaviest edge on that cycle.

PQ \notin \text{MST}

By the cycle property, PQ is in no MST.

This matches problem 2, where PQ was skipped.

10.

8, 9, 10, 11, 12, 14

Sort the costs: XY, WZ, WX, YZ, XZ, WY.

XY = 8,\; WZ = 9

Add XY and WZ.

WX = 10

Add WX: it joins {W, Z} and {X, Y}.

8 + 9 + 10 = 27

Three pipes connect all four villages. Add the costs.

Build X–Y, W–Z and W–X for a total of $27,000.

Minimum spanning tree FAQ

What is a minimum spanning tree?

It is a set of edges that connects every vertex of a weighted graph with no cycles and the smallest possible total weight. For n vertices it always has n - 1 edges.

What is the difference between Prim’s and Kruskal’s algorithms?

Kruskal’s algorithm sorts all the edges and adds the cheapest one that does not make a cycle, so its chosen edges can sit in separate pieces until the end. Prim’s algorithm grows one tree from a starting vertex, always adding the cheapest edge that leaves the tree.

Is the minimum spanning tree unique?

It is unique when all the edge weights are different. With equal weights there can be several minimum spanning trees, but they all have the same total weight.

Does a minimum spanning tree give the shortest path between two vertices?

No. It minimizes the total weight of the whole network, and the path it gives between two vertices can be longer than the shortest one. Use Dijkstra’s algorithm for shortest paths.

Summary

  • A spanning tree uses every vertex, has no cycles and has n - 1 edges; a minimum spanning tree has the smallest total weight.
  • Kruskal’s algorithm adds edges from cheapest to most expensive, skipping any edge that would form a cycle.
  • Prim’s algorithm grows one tree, always adding the cheapest edge that leaves it.
  • The cut property and the cycle property explain why both greedy algorithms are correct.
  • With all weights different the MST is unique; with ties there may be several of the same weight.

Previous lesson: Trees in Graph Theory

Next lesson: Tree Traversal

Back to the Discrete Math Index