← Year 12: Networks

NSWMaths Standard 2Syllabus dot point

What is a minimum spanning tree, and how is it found using Prim's or Kruskal's algorithm?

Find a minimum spanning tree for a weighted graph using Prim's or Kruskal's algorithm

A focused answer to the HSC Maths Standard 2 dot point on minimum spanning trees. Definition of a spanning tree and minimum spanning tree, step-by-step Prim's and Kruskal's algorithms, and worked examples for utility networks and rural Australian infrastructure planning.

Generated by Claude OpusReviewed by Better Tuition Academy8 min answer

Have a quick question? Jump to the Q&A page

What this dot point is asking

NESA wants you to find a minimum spanning tree of a weighted graph using either Prim's or Kruskal's algorithm, showing each step clearly. The MST is the most common network optimisation question in the HSC.

The answer

Spanning tree

A spanning tree of a connected graph is a subgraph that:

  • includes every vertex of the original graph,
  • is connected (every pair of vertices linked by a path),
  • has no cycles.

A spanning tree on nn vertices always has exactly nβˆ’1n - 1 edges.

Minimum spanning tree (MST)

A minimum spanning tree is a spanning tree with the smallest total edge weight among all possible spanning trees of the graph.

For a weighted graph (where each edge has a cost, distance or time), the MST connects all vertices using the least total weight.

Applications: laying the shortest total length of pipe, cable, fibre, or road to connect a set of locations.

Prim's algorithm

Weighted graph with minimum spanning tree highlighted A graph with five vertices A, B, C, D, E and seven weighted edges. The minimum spanning tree, drawn in heavier accent-coloured stroke, contains AC weight 2, CD weight 3, AB weight 4, and DE weight 6. Non-tree edges BC weight 5, BD weight 10, CE weight 7 are dashed and faded. Total MST weight 15. 4 2 5 10 3 7 6 A B C D E MST (accent stroke): AC(2) + CD(3) + AB(4) + DE(6) = 15

Prim's algorithm builds the MST one vertex at a time, growing outward from a chosen starting vertex:

  1. Pick any starting vertex; this is the initial tree.
  2. From all edges that connect a tree vertex to a non-tree vertex, choose the one with the smallest weight.
  3. Add that edge and the new vertex to the tree.
  4. Repeat until all vertices are in the tree.

Prim's adds nβˆ’1n - 1 edges in total (one per added vertex after the first).

Kruskal's algorithm

Kruskal's algorithm sorts all edges and adds them in order, skipping any that create a cycle:

  1. List all edges in order of increasing weight.
  2. Take the cheapest unused edge; if it does not create a cycle with edges already chosen, add it.
  3. Repeat until nβˆ’1n - 1 edges are added.

Both algorithms always produce a minimum spanning tree, though the exact edges may differ if there are ties in weight.

Which to use

  • Prim's is easier when there are many edges (you only consider edges from the current tree).
  • Kruskal's is easier when edges are pre-sorted (just go down the list, checking for cycles).

The HSC accepts either; show your working clearly so the marker can follow which algorithm you used.

Cycle check (Kruskal's)

An edge creates a cycle if both endpoints are already connected (directly or via existing tree edges). For small graphs you can spot this visually; for larger graphs, keep track of which vertices are in which component as you add edges.

Past exam questions, worked

Real questions from past NESA papers on this dot point, with our answer explainer.

2022 HSC Q225 marksA network has 66 vertices and weighted edges connecting them. Use Prim's algorithm starting from vertex AA to find a minimum spanning tree. Show your work. (Weights given in the question.)
Show worked answer β†’

Prim's algorithm steps:

Step 1: Start with AA. Add the cheapest edge from AA to any other vertex, say AA-CC with weight 33.

Step 2: From the current tree {A,C}\{A, C\}, find the cheapest edge connecting to a vertex not yet in the tree. Say CC-DD with weight 44.

Step 3: Continue. From {A,C,D}\{A, C, D\}, the cheapest external edge might be DD-BB with weight 55.

Step 4: From {A,B,C,D}\{A, B, C, D\}, the cheapest external edge might be BB-EE with weight 66.

Step 5: From {A,B,C,D,E}\{A, B, C, D, E\}, the cheapest external edge might be EE-FF with weight 77.

Final MST: edges AC,CD,DB,BE,EFAC, CD, DB, BE, EF, total weight 3+4+5+6+7=253 + 4 + 5 + 6 + 7 = 25.

Markers reward a clear step-by-step working, the edges in order added, no edges that form cycles, and the final total weight. The exact edges depend on the network in the question.

2023 HSC Q244 marksA water utility wants to connect 55 towns with the minimum total length of pipeline. The possible pipeline routes and lengths (in km) form a weighted graph. Find the minimum spanning tree and its total length.
Show worked answer β†’

Sort all edges by weight (Kruskal's algorithm). Add them in order from smallest to largest, but skip any edge that would create a cycle.

For 55 towns, the MST has 5βˆ’1=45 - 1 = 4 edges.

Worked example with hypothetical weights: edges sorted AB(8),CD(10),AC(12),BD(15),DE(18),…AB(8), CD(10), AC(12), BD(15), DE(18), \ldots.

Add AB(8)AB(8): tree {AB}\{AB\}.

Add CD(10)CD(10): tree {AB,CD}\{AB, CD\}. Disconnected, but no cycle yet.

Add AC(12)AC(12): tree {AB,CD,AC}\{AB, CD, AC\}. Now connects the two components.

Skip BD(15)BD(15) if it would create a cycle in {ABCD}\{ABCD\}. (It does: AA-BB-DD and AA-CC-DD both exist.) Skip.

Add DE(18)DE(18): tree {AB,AC,CD,DE}\{AB, AC, CD, DE\}. All 55 vertices connected.

Total: 8+10+12+18=488 + 10 + 12 + 18 = 48 km.

Markers reward sorting, the cycle-check at each step, and the total weight with units.

Related dot points