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.
Reviewed by: AI editorial process; not yet individually human-reviewed
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, and to show each step clearly. The MST is the most common network optimisation question in the HSC, where you cut the total cost of a network down to the smallest it can be.
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).
Watch Prim's build the tree, stage by stage
The diagram above shows the finished minimum spanning tree. Here is the same graph with Prim's algorithm run one edge at a time. This lets you watch the tree grow and follow the reasoning at each step. Tree edges are heavy and in colour, and vertices already in the tree are ringed.
Stage 1, start the tree. Pick any starting vertex, say . The edges leaving are () and (); the cheaper is , so the tree grows to take in .
Stage 2, add the cheapest edge to a new vertex. Look at every edge from the tree to a vertex not yet in it: (), (), (), (). The cheapest is (), so joins.
Stage 3, keep growing. From the candidates are (), (), (), (), (). The cheapest is (), so joins. The expensive edges and are never the cheapest option, so they are never used.
Stage 4, connect the last vertex. Only remains. The edges reaching it are () and (); take the cheaper, . Every vertex is now connected, so the tree is finished: .
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.
Is the minimum spanning tree unique?
The total weight of the MST is always unique. Every minimum spanning tree of a graph adds up to the same total. The exact set of edges is also unique when all the edge weights are different. If two or more edges share a weight, there can be more than one MST. This happens because a tie can be broken in different ways, picking different edges, but each tree still has the same minimum total. So if you and a classmate pick different equal-weight edges, you can both be correct, as long as the totals match and each tree is valid.
Cycle check (Kruskal's)
An edge creates a cycle if both of its endpoints are already linked, either directly or through edges you have already chosen. For small graphs you can usually see this by eye. For larger graphs, keep track of which vertices sit in which connected piece (called a component) as you add edges.
Exam-style practice questions
Practice questions written in the style of NESA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
2022 HSC-style5 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-style4 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.
Practice questions
Original practice questions graded from foundation to exam level, each with a full worked solution. Try them before revealing the solution.
foundation2 marksThree towns , and can be joined by direct cable along three possible routes with lengths (in km) , and . Find the minimum spanning tree and state its total length.Show worked solution →
List the edges. There are three possible cables: , , . With towns the spanning tree needs edges.
Add the cheapest edges that do not form a cycle (Kruskal's). Sorted by length: , , .
- Add (): the tree is .
- Add (): this brings in , so all three towns are joined.
- The edge () would now close the triangle --, making a cycle, so it is skipped.
State the result. The minimum spanning tree uses and .
Check. Two edges join all towns with no cycle, and km is cheaper than any tree using the km route. The MST length is km.
foundation3 marksA weighted network has four vertices , , , joined in a square by the edges , , and . Use Kruskal's algorithm to find the minimum spanning tree and its total weight.Show worked solution →
Sort the edges by weight. The four edges in increasing order are , , , . A tree on vertices has edges.
Add edges in order, skipping any that make a cycle (Kruskal's).
- Add (): tree .
- Add (): tree , two separate pieces but no cycle.
- Add (): this links the two pieces, so all four vertices are connected.
- Stop: edges are in, so () is not needed (it would form a cycle).
Total weight.
Check. Three edges connect all vertices with no cycle. Leaving out the most expensive edge () is what makes the tree minimal, so the MST weight is .
foundation3 marksA network on vertices , , , has edges , , , and . Find a minimum spanning tree and its total weight.Show worked solution →
Count the edges needed. With vertices the spanning tree has edges. Sort the five edges: , , , , .
Apply Kruskal's algorithm.
- Add (): tree .
- Add (): brings in , joining it to and .
- Add (): brings in , so all four vertices are connected.
- The next edge () would close a cycle --, so skip it; () is also not needed.
Total weight.
Check. Three edges, all vertices joined, no cycle. The two unused edges ( and ) are both heavier than every chosen edge, confirming the tree is minimal. The MST weight is .
foundation4 marksFour water tanks , , , can be joined by pipe along any of the six possible routes, with lengths (in metres) , , , , and . Find the minimum spanning tree and the least total length of pipe needed to connect all four tanks.Show worked solution →
Set up. Every pair of tanks can be joined, so there are possible pipes. A spanning tree on tanks needs pipes. Sort by length: , , , , , .
Apply Kruskal's algorithm.
- Add (): tree .
- Add (): brings in .
- Add (): brings in , so all four tanks are joined.
- Stop: pipes are in. The edges (), () and () would each close a cycle, so they are skipped.
Least total length.
Check. Three pipes connect all four tanks with no loop, and the three cheapest usable pipes were taken. The minimum length of pipe is m.
core4 marksA network has five vertices with edges , , , , and . Use Prim's algorithm starting from vertex to find the minimum spanning tree. List the edges in the order they are added and give the total weight.Show worked solution →
- Start the tree at (Prim's)
- The tree begins as . At each stage take the cheapest edge from a tree vertex to a vertex not yet included.
- Stage 1
- Edges leaving are () and (). Cheapest is (); joins. Tree .
- Stage 2
- Crossing edges from are (), (), (). Cheapest is (); joins. Tree .
- Stage 3
- Crossing edges are (), (), (), (). Cheapest is (); joins. Tree .
- Stage 4
- Only remains. Crossing edges are () and (). Cheaper is (); joins. All five vertices are now connected.
- Result
- Edges added in order: , , , .
Check. Four edges join all vertices with no cycle. The expensive edge () was never the cheapest crossing edge, so it is left out, confirming the MST weight is .
core4 marksA council plans to lay fibre to five towns , , , , . The possible cable routes have lengths (in km) , , , , , and . Using Prim's algorithm starting from town , find the minimum spanning tree and the least total length of cable.Show worked solution →
- Begin at (Prim's)
- Tree . Each stage adds the cheapest cable from a connected town to an unconnected town.
- Stage 1
- From : (), (). Cheapest is (); joins. Tree .
- Stage 2
- Crossing cables: (), (), (), (). Cheapest is (); joins. Tree .
- Stage 3
- Crossing cables: (), (), (), (). Cheapest is (); joins. Tree .
- Stage 4
- Only remains. Crossing cables: (), (), (). Cheapest is (); joins.
- Result
- Cables laid: , , , .
Check. Four cables connect all towns with no loop. The most expensive route () is never used. The least total length of cable is km.
core5 marksSix pumping stations , , , , , are linked by pipes with lengths , , , , , , , and . Use Kruskal's algorithm to find the minimum spanning tree. Show which edges are added and which are skipped, then state the total length.Show worked solution →
Sort all pipes by length. , , , , , , , , . A tree on stations needs pipes.
Apply Kruskal's, checking each edge for a cycle.
- Add (): tree .
- Add (): tree .
- Add (): brings in .
- Add (): links the piece to the piece.
- Skip (): both and are already connected, so this makes a cycle.
- Add (): brings in , the last station. Five pipes are now placed.
- Remaining (), (), () would all make cycles, so skip them.
Total length.
Check. Five pipes connect all stations with no cycle. Where two edges tied at , made a cycle and did not, so was the correct choice. The MST length is .
core5 marksA weighted network on five vertices has edges , , , , , , and . Find a minimum spanning tree using Kruskal's algorithm and state its total weight.Show worked solution →
Sort the edges. , , , , , , , . A tree on vertices needs edges.
Apply Kruskal's algorithm.
- Add (): tree .
- Add (): tree , two separate pieces.
- Add (): brings in with and .
- Add (): links the piece to the piece, so all five vertices are joined.
- Stop: edges are in. The edges (), (), () and () would each form a cycle, so they are skipped.
Total weight.
Check. Four edges connect all vertices with no cycle, and the algorithm stopped before reaching the tied edges, so neither was needed. The MST weight is .
exam6 marksA telecommunications company will lay cable to connect seven towns , , , , , , . The possible routes (in km) are , , , , , , , , , and . (a) Use Kruskal's algorithm to find a minimum spanning tree, listing the edges added in order. (b) State the least total length of cable. (c) Explain why your answer is the minimum even though two different edges share the weight .Show worked solution →
(a) Sort the routes (Kruskal's). , , , , , , , , , , . A tree on towns needs cables.
Add the cheapest route each time, skipping any that closes a cycle:
- Add (); add (); add (); add ().
- Skip (): and are already joined, so it makes a cycle.
- Add (): brings in .
- Add (): brings in , the last town. Six cables are now placed.
Edges added in order: , , , , , .
(b) Least total length.
(c) Why it is the minimum. The two edges of weight , and , connect different new towns ( and ), so both are needed and neither makes a cycle. There is no tie-break to resolve here, and the heavier routes (), (), () and () all close cycles, so the total cannot be reduced.
Check. Six cables join all towns with no loop. The least total length of cable is km.
exam6 marksA power network connects eight substations , , , , , , , with lines of length , , , , , , , , , , , and . (a) Find the minimum spanning tree using Kruskal's algorithm. (b) State the total length. (c) The edges and both have weight , and and both have weight . Does the choice between tied edges change the total length? Explain.Show worked solution →
(a) Sort and apply Kruskal's. , , , , , , , , , , , , . A tree on substations needs lines.
- Add (); add (); add ().
- Add (): brings in . Add (): starts a separate piece.
- Add (): links the piece to the piece.
- Skip (): and are now in the same piece, so it makes a cycle.
- Add (): links in the piece, so all eight substations are joined. Seven lines placed; the rest close cycles and are skipped.
Edges used: , , , , , , .
(b) Total length.
(c) Effect of the ties. For the pair and (both ), each joins a different part of the network, so both are needed. For the pair and (both ), closes a cycle while does not, so must be chosen. In every case the total is fixed: the MST total weight is always unique, only the exact edges can differ when a tie genuinely offers two cycle-free choices.
Check. Seven lines connect all substations with no cycle. The total length is .
exam7 marksAn engineer connects six sites , , , , , with cable. The available routes (in km) are , , , , , , , and . (a) Find the minimum spanning tree and its total length. (b) A contractor proposes instead to use the routes , , , and . Show that this is a valid spanning tree, find its total length, and state how much cable the minimum spanning tree saves.Show worked solution →
(a) Apply Kruskal's algorithm. Sort the routes: , , , , , , , , . A tree on sites needs cables.
- Add (); add (); add ().
- Add (): links the piece to the piece.
- Add (): links the piece in, so all six sites are connected. Five cables placed.
- Skip (), (), (), (): each would close a cycle.
Minimum spanning tree: , , , , .
(b) Test the proposed tree. The proposed routes are , , , , . Trace the connections: ----- links all six sites in a single chain, using edges with no repeated site, so it is a valid spanning tree (connected, edges, no cycle).
Compare. The proposed tree uses () where the MST uses the cheaper (), so it is km longer.
Check. Both networks connect all sites with cables. The MST total is km, which is km less than the proposed km, so the minimum spanning tree saves km.
