impera Open Impera

A levels Further Mathematics · Decision mathematics

Minimum spanning trees

1Spanning trees

A spanning tree of a connected graph is a tree that includes every vertex. It has $n - 1$ edges for $n$ vertices.

A minimum spanning tree (MST) is a spanning tree with the smallest total weight. It solves problems such as connecting towns with the least cable or pipe.

Worked example

Counting edges

  1. A network has 6 vertices.
  2. Any spanning tree has exactly $6 - 1 = 5$ edges.
Check yourself

How many edges does a spanning tree of a network with 9 vertices have?

  • 10

  • 9

  • 8

  • 36

Show the answer

8

n − 1.

2Kruskal's algorithm

Kruskal's algorithm:

  1. List the edges in order of weight, smallest first.
  2. Take each edge in turn. Add it unless it would make a cycle; if it would, reject it.
  3. Stop when $n - 1$ edges have been chosen.
Worked example

Kruskal on the network

  1. Edges in order: BC 2, AC 3, DE 3, AB 4, DF 4, BD 5, CE 6, EF 7, CD 8.
  2. Add BC, AC, DE.
  3. Reject AB (it would make the cycle A–B–C).
  4. Add DF, then BD. That is 5 edges, so stop.
  5. MST: BC, AC, DE, DF, BD, with total weight $2 + 3 + 3 + 4 + 5 = 17$.

Kruskal's minimum spanning tree in green: BC, AC, DE, DF, BD. AB (4) was rejected because it would close the cycle A–B–C.

Check yourself

In Kruskal's algorithm, when is an edge rejected?

  • Never

  • When its weight is odd

  • When it joins two vertices of high degree

  • When it would form a cycle

Show the answer

When it would form a cycle

A tree has no cycles.

3Prim's algorithm

Prim's algorithm grows one tree from a starting vertex:

  1. Choose any starting vertex.
  2. Add the shortest edge joining a vertex in the tree to a vertex not yet in the tree.
  3. Repeat until every vertex is included.
Worked example

Prim from A

  1. Start at A. Shortest edge out: AC 3.
  2. From {A, C}: shortest edge to a new vertex is CB 2.
  3. From {A, B, C}: shortest is BD 5 (AB would not add a new vertex).
  4. From {A, B, C, D}: DE 3, then DF 4.
  5. Order: AC, CB, BD, DE, DF. Total 17, the same as Kruskal.

Prim's algorithm never needs to check for cycles, because each new edge always brings in a new vertex.

Check yourself

Why does Prim's algorithm never need to check for cycles?

  • It starts with a cycle and removes edges

  • It sorts all the edges by weight first

  • Each edge added joins the tree to a new vertex

  • It only uses edges with even weights

Show the answer

Each edge added joins the tree to a new vertex

A new vertex cannot close a cycle.

4Prim's algorithm on a distance matrix

Prim's algorithm can be carried out directly on a distance matrix, which is how a computer would do it.

  1. Choose a starting vertex; cross out its row and number its column.
  2. Look down all numbered columns for the smallest value not crossed out.
  3. That value's row gives the new vertex: cross out its row and number its column.
  4. Repeat until all rows are crossed out.
Worked example

A distance matrix

  1. Vertices P, Q, R, S with PQ 5, PR 8, PS 3, QR 4, QS 7, RS 6. Start at P.
  2. Smallest in column P: 3 (PS). Add S.
  3. Smallest in columns P and S (rows Q, R): 5 (PQ). Add Q.
  4. Smallest in columns P, S, Q (row R): 4 (QR). Add R.
  5. MST: PS, PQ, QR, total 12.
Check yourself

On a distance matrix, which rows are crossed out in Prim's algorithm?

  • No rows are ever crossed out

  • The rows of vertices already in the tree

  • Rows with the largest values in them

  • Every second row of the matrix

Show the answer

The rows of vertices already in the tree

So you only look at edges to new vertices.

5Comparing the two algorithms

Both Kruskal and Prim always give a minimum spanning tree, but they work differently:

  • Kruskal sorts all edges first and may build several separate pieces that later join up. It needs a cycle check.
  • Prim grows a single tree and suits a distance matrix.

If several edges have equal weights, there may be more than one MST, but they all have the same total weight.

Worked example

Ties

  1. If two edges of weight 4 could both be chosen next in Kruskal, either may be added first.
  2. The final total weight is the same, though the tree may differ.
Check yourself

Can a network have more than one minimum spanning tree?

  • Only for complete graphs with n vertices

  • Only if the network contains a cycle

  • Yes, if some edges have equal weights

  • No, a minimum spanning tree is always unique

Show the answer

Yes, if some edges have equal weights

The total weight is still the same.

6Using minimum spanning trees

Typical uses: laying cables, pipes or roads to connect every location at least cost.

Worked example

Adding a condition

  1. In the network, suppose edge CE must be included (for example, a road already planned).
  2. Include CE first, then continue Kruskal: BC, AC, DE, then AB (rejected: cycle), DF, then BD would make the cycle B–D–E–C–B, so it is rejected.
  3. Tree: CE, BC, AC, DE, DF, total $6 + 2 + 3 + 3 + 4 = 18$, which is 1 more than the MST.

An MST connects everything, but it does not give the shortest route between two particular vertices: that is a shortest path problem.

Check yourself

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

  • Not necessarily

  • Only for trees

  • Only if weights are equal

  • Yes, always

Show the answer

Not necessarily

Use Dijkstra's algorithm for that.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera