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.
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 vertices always has exactly 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
Prim's algorithm builds the MST one vertex at a time, growing outward from a chosen starting vertex:
- Pick any starting vertex; this is the initial tree.
- From all edges that connect a tree vertex to a non-tree vertex, choose the one with the smallest weight.
- Add that edge and the new vertex to the tree.
- Repeat until all vertices are in the tree.
Prim's adds 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:
- List all edges in order of increasing weight.
- Take the cheapest unused edge; if it does not create a cycle with edges already chosen, add it.
- Repeat until 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 vertices and weighted edges connecting them. Use Prim's algorithm starting from vertex 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 . Add the cheapest edge from to any other vertex, say - with weight .
Step 2: From the current tree , find the cheapest edge connecting to a vertex not yet in the tree. Say - with weight .
Step 3: Continue. From , the cheapest external edge might be - with weight .
Step 4: From , the cheapest external edge might be - with weight .
Step 5: From , the cheapest external edge might be - with weight .
Final MST: edges , total weight .
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 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 towns, the MST has edges.
Worked example with hypothetical weights: edges sorted .
Add : tree .
Add : tree . Disconnected, but no cycle yet.
Add : tree . Now connects the two components.
Skip if it would create a cycle in . (It does: -- and -- both exist.) Skip.
Add : tree . All vertices connected.
Total: km.
Markers reward sorting, the cycle-check at each step, and the total weight with units.
Related dot points
- Use network terminology including vertex, edge, weight, degree, path, cycle and directed graph to describe and analyse networks
A focused answer to the HSC Maths Standard 2 dot point on network terminology. Vertices, edges, weights, directed and undirected graphs, paths and cycles, connected and disconnected components, and worked Australian examples for transport networks and project schedules.
- Find the shortest path between two vertices in a weighted network by inspection or by systematic labelling
A focused answer to the HSC Maths Standard 2 dot point on shortest paths. Inspection method for small networks, systematic labelling for larger ones, and worked examples for road network distances and Sydney transport routes.
- Construct an activity network from a precedence table, identify the critical path and find the minimum project duration
A focused answer to the HSC Maths Standard 2 dot point on critical path analysis. Building an activity network from a precedence table, identifying paths through the network, and determining the minimum project duration via the critical (longest) path with worked Australian construction examples.