Skip to main content

Minimum spanning trees and Prim's algorithm for HSC Maths Standard 2

Syllabus dot point

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

HSCMaths Standard 2Year 12: Networks12 min read

Quick answer

A minimum spanning tree connects all nn vertices of a weighted graph with n−1n - 1 edges and the smallest total weight; find it using Prim's (grow from a starting vertex) or Kruskal's (sort edges and add the cheapest that does not create a cycle); both give the same total weight.

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Exam-style questions
  4. Practice questions

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 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.

Key fact

A spanning tree of a graph on nn vertices always has exactly n−1n - 1 edges and no cycles, and the minimum spanning tree is the one with the least total weight. Prim's and Kruskal's both always find a minimum spanning tree. Its total weight is unique; only the exact set of edges can differ, and only when two or more edges tie in weight.

Prim's algorithm

Weighted graph with its minimum spanning tree highlighted A weighted graph with five vertices A, B, C, D, E and seven edges. The minimum spanning tree is drawn in the heavier accent colour and contains AC 2, CD 3, AB 4 and DE 6, total 15. The non-tree edges BC 5, BD 10 and CE 7 are drawn muted. 2 3 4 6 5 10 7 A B C D E MST = AC 2 + CD 3 + AB 4 + DE 6 = 15 (accent); muted edges are not used.

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).

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 AA. The edges leaving AA are ABAB (44) and ACAC (22); the cheaper is ACAC, so the tree grows to take in CC.

Prim's algorithm: start the treePrim's tree after one edge: starting at A, the cheapest edge AC of weight 2 is added, so A and C are in the tree.23465107ABCDEStep 1Start at A. Cheapest edge from A is AC (2). A and C are now in the tree.

Stage 2, add the cheapest edge to a new vertex. Look at every edge from the tree {A,C}\{A, C\} to a vertex not yet in it: ABAB (44), BCBC (55), CDCD (33), CECE (77). The cheapest is CDCD (33), so DD joins.

Prim's algorithm: add CDPrim's tree after two edges: from A and C the cheapest edge to a new vertex is CD of weight 3, so D joins.23465107ABCDEStep 2From {A, C}, the cheapest edge to a new vertex is CD (3). D joins.

Stage 3, keep growing. From {A,C,D}\{A, C, D\} the candidates are ABAB (44), BCBC (55), CECE (77), DEDE (66), BDBD (1010). The cheapest is ABAB (44), so BB joins. The expensive edges BCBC and BDBD are never the cheapest option, so they are never used.

Prim's algorithm: add ABPrim's tree after three edges: from A, C and D the cheapest edge to a new vertex is AB of weight 4, so B joins.23465107ABCDEStep 3From {A, C, D}, the cheapest edge to a new vertex is AB (4). B joins.

Stage 4, connect the last vertex. Only EE remains. The edges reaching it are DEDE (66) and CECE (77); take the cheaper, DEDE. Every vertex is now connected, so the tree is finished: AC+CD+AB+DE=2+3+4+6=15AC + CD + AB + DE = 2 + 3 + 4 + 6 = 15.

Prim's algorithm: the finished treeThe completed minimum spanning tree AC, CD, AB, DE of total weight 15, with all five vertices connected.23465107ABCDEStep 4Only E left: add DE (6). Tree complete: 2 + 3 + 4 + 6 = 15.

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.

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.

Worked example

Find the MST of a 5-vertex network with Prim's

A network has 55 vertices and the following edge weights:

Edge Weight
ABAB 44
ACAC 22
BCBC 55
BDBD 1010
CDCD 33
CECE 77
DEDE 66

Step 1, start from vertex AA and add the cheapest outgoing edge: The tree begins at {A}\{A\}. The only edges from AA are ABAB (weight 44) and ACAC (weight 22). The cheapest is ACAC, so add it.

Tree: {A,C}\{A, C\}. Edges used: ACAC.

Step 2, grow the tree to vertex DD: From the current tree {A,C}\{A, C\}, the candidate edges to unvisited vertices are: ABAB (weight 44), CDCD (weight 33), CECE (weight 77), BDBD (weight 1010). The cheapest is CDCD at weight 33.

Tree: {A,C,D}\{A, C, D\}. Edges used: AC,CDAC, CD.

Step 3, add the next cheapest edge to a new vertex: Candidates from {A,C,D}\{A, C, D\}: ABAB (weight 44), CECE (weight 77), DEDE (weight 66), BDBD (weight 1010). The cheapest is ABAB at weight 44.

Tree: {A,B,C,D}\{A, B, C, D\}. Edges used: AC,CD,ABAC, CD, AB.

Step 4, connect the final vertex EE: Candidates from {A,B,C,D}\{A, B, C, D\}: DEDE (weight 66), CECE (weight 77), BDBD skipped (both endpoints in tree). The cheapest is DEDE at weight 66.

Tree: {A,B,C,D,E}\{A, B, C, D, E\}. All five vertices connected.

Final MST edges: AC,CD,AB,DEAC, CD, AB, DE. Total weight: 2+3+4+6=152 + 3 + 4 + 6 = 15.

Find the MST with Kruskal's on the same network

Sort all edges by weight: Sorted: AC(2),CD(3),AB(4),BC(5),DE(6),CE(7),BD(10)AC(2), CD(3), AB(4), BC(5), DE(6), CE(7), BD(10).

Add edges in order, skipping any that create a cycle:

  • Add ACAC (weight 22): no cycle. Tree: {AC}\{AC\}.
  • Add CDCD (weight 33): connects DD to the existing component. Tree: {AC,CD}\{AC, CD\}.
  • Add ABAB (weight 44): connects BB to the existing component. Tree: {AC,CD,AB}\{AC, CD, AB\}.
  • Skip BCBC (weight 55): both BB and CC are already connected via AA-CC and AA-BB, so adding BCBC would create a cycle.
  • Add DEDE (weight 66): connects EE, the last unconnected vertex. Tree: {AC,CD,AB,DE}\{AC, CD, AB, DE\}. All five vertices connected; done.

Total weight: 2+3+4+6=152 + 3 + 4 + 6 = 15. Same result as Prim's.

Australian context

A regional NSW council wants to lay fibre optic cable connecting 66 small towns. The possible routes follow existing roads with known lengths, giving 99 candidate edges between the 66 town vertices. The MST gives the minimum total cable length needed: 6−1=56 - 1 = 5 edges, with the total length determined by the specific weights.

Either algorithm finds the same optimal network. This model also applies directly to water mains, electricity distribution and gas pipelines, where one reliable connection between each pair of locations is sufficient.

Common traps
Adding too many edges
An MST on nn vertices has exactly n−1n - 1 edges. If you have more, you have a cycle.
Not checking for cycles in Kruskal's
The cheapest unused edge sometimes creates a cycle. Skip those.
Skipping the work
Markers want to see each step: which edge added, which considered and skipped. A bare final tree without working loses marks.
Wrong total weight
Sum all the chosen edge weights at the end.
Disconnected MST
A spanning tree must connect every vertex. If your n−1n - 1 edges form a forest (disconnected components), you have made a mistake.
Exam technique

Show the order in which you add edges, not just the final tree. For Prim's, list each cheapest connecting edge as you take it; for Kruskal's, list the sorted edges and mark each one added or skipped (skipped because it would make a cycle). State the total weight with units at the end. If two edges tie, either choice is fine as long as the tree has n−1n - 1 edges, is connected, and has no cycle.

Note

Picture a map of towns that all need to be joined by one cheap thing, like a water pipe or a cable. Each possible link has a cost, and you want every town reached for as little money as possible. A minimum spanning tree is the cheapest set of links that ties them all together. You never add a link that loops back to a town already reached, because that would just waste money. The two methods are simply two safe ways to keep picking the cheapest sensible link until everyone is joined.

Exam-style questions

Questions in the style of NESA exam questions on this dot point, each with a worked answer. They are written by ExamExplained unless tagged "Past paper"; the year shows the paper a question is modelled on.

2022 HSC-style5 marks
A network has vertices AA to FF and weighted edges AB=8AB = 8, AC=3AC = 3, BC=9BC = 9, BD=5BD = 5, BE=6BE = 6, CD=4CD = 4, CE=10CE = 10, DE=11DE = 11, DF=12DF = 12 and EF=7EF = 7. Use Prim's algorithm starting from vertex AA to find a minimum spanning tree. Show your work.
Show worked answer →

Prim's algorithm steps:

Step 1: Start with AA. The edges from AA are AB=8AB = 8 and AC=3AC = 3, so add AA-CC (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: CD=4CD = 4 (beating AB=8AB = 8, BC=9BC = 9, CE=10CE = 10).

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

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

Step 5: From {A,B,C,D,E}\{A, B, C, D, E\}, the cheapest external edge is EE-FF with weight 77 (not DF=12DF = 12).

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.

2023 HSC-style4 marks
A water utility wants to connect 55 towns with the minimum total length of pipeline. The possible pipeline routes between towns AA to EE have lengths (in km) AB=8AB = 8, AC=12AC = 12, BD=15BD = 15, BE=20BE = 20, CD=10CD = 10, CE=22CE = 22 and DE=18DE = 18. 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.

Edges sorted: AB(8),CD(10),AC(12),BD(15),DE(18),BE(20),CE(22)AB(8), CD(10), AC(12), BD(15), DE(18), BE(20), CE(22).

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: BB and DD are already joined by BB-AA-CC-DD.)

Add DE(18)DE(18): tree {AB,AC,CD,DE}\{AB, AC, CD, DE\}. All 55 vertices connected, so BEBE and CECE are not needed.

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.

Practice questions

Original practice questions graded from foundation to exam level, each with a full worked solution. Try them before revealing the solution.

foundation2 marks
Three towns AA, BB and CC can be joined by direct cable along three possible routes with lengths (in km) AB=5AB = 5, BC=8BC = 8 and AC=12AC = 12. Find the minimum spanning tree and state its total length.
Show worked solution →

List the edges. There are three possible cables: AB=5AB = 5, BC=8BC = 8, AC=12AC = 12. With 33 towns the spanning tree needs 3−1=23 - 1 = 2 edges.

Add the cheapest edges that do not form a cycle (Kruskal's). Sorted by length: AB(5)AB(5), BC(8)BC(8), AC(12)AC(12).

  • Add ABAB (55): the tree is {AB}\{AB\}.
  • Add BCBC (88): this brings in CC, so all three towns are joined.
  • The edge ACAC (1212) would now close the triangle AA-BB-CC, making a cycle, so it is skipped.

State the result. The minimum spanning tree uses ABAB and BCBC.

Total=5+8=13 km.\text{Total} = 5 + 8 = 13 \text{ km}.

Check. Two edges join all 33 towns with no cycle, and 1313 km is cheaper than any tree using the 1212 km route. The MST length is 1313 km.

foundation3 marks
A weighted network has four vertices PP, QQ, RR, SS joined in a square by the edges PQ=4PQ = 4, QR=6QR = 6, RS=3RS = 3 and PS=9PS = 9. 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 RS(3)RS(3), PQ(4)PQ(4), QR(6)QR(6), PS(9)PS(9). A tree on 44 vertices has 4−1=34 - 1 = 3 edges.

Add edges in order, skipping any that make a cycle (Kruskal's).

  • Add RSRS (33): tree {RS}\{RS\}.
  • Add PQPQ (44): tree {RS,PQ}\{RS, PQ\}, two separate pieces but no cycle.
  • Add QRQR (66): this links the two pieces, so all four vertices are connected.
  • Stop: 33 edges are in, so PSPS (99) is not needed (it would form a cycle).

Total weight.

Total=3+4+6=13.\text{Total} = 3 + 4 + 6 = 13.

Check. Three edges connect all 44 vertices with no cycle. Leaving out the most expensive edge PSPS (99) is what makes the tree minimal, so the MST weight is 1313.

foundation3 marks
A network on vertices AA, BB, CC, DD has edges AB=6AB = 6, AC=5AC = 5, AD=7AD = 7, CD=4CD = 4 and BC=9BC = 9. Find a minimum spanning tree and its total weight.
Show worked solution →

Count the edges needed. With 44 vertices the spanning tree has 4−1=34 - 1 = 3 edges. Sort the five edges: CD(4)CD(4), AC(5)AC(5), AB(6)AB(6), AD(7)AD(7), BC(9)BC(9).

Apply Kruskal's algorithm.

  • Add CDCD (44): tree {CD}\{CD\}.
  • Add ACAC (55): brings in AA, joining it to CC and DD.
  • Add ABAB (66): brings in BB, so all four vertices are connected.
  • The next edge ADAD (77) would close a cycle AA-CC-DD, so skip it; BCBC (99) is also not needed.

Total weight.

Total=4+5+6=15.\text{Total} = 4 + 5 + 6 = 15.

Check. Three edges, all vertices joined, no cycle. The two unused edges (77 and 99) are both heavier than every chosen edge, confirming the tree is minimal. The MST weight is 1515.

foundation4 marks
Four water tanks AA, BB, CC, DD can be joined by pipe along any of the six possible routes, with lengths (in metres) AB=7AB = 7, AC=5AC = 5, AD=12AD = 12, BC=9BC = 9, BD=8BD = 8 and CD=6CD = 6. 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 66 possible pipes. A spanning tree on 44 tanks needs 4−1=34 - 1 = 3 pipes. Sort by length: AC(5)AC(5), CD(6)CD(6), AB(7)AB(7), BD(8)BD(8), BC(9)BC(9), AD(12)AD(12).

Apply Kruskal's algorithm.

  • Add ACAC (55): tree {AC}\{AC\}.
  • Add CDCD (66): brings in DD.
  • Add ABAB (77): brings in BB, so all four tanks are joined.
  • Stop: 33 pipes are in. The edges BDBD (88), BCBC (99) and ADAD (1212) would each close a cycle, so they are skipped.

Least total length.

Total=5+6+7=18 m.\text{Total} = 5 + 6 + 7 = 18 \text{ m}.

Check. Three pipes connect all four tanks with no loop, and the three cheapest usable pipes were taken. The minimum length of pipe is 1818 m.

core4 marks
A network has five vertices with edges AB=3AB = 3, BC=7BC = 7, CD=5CD = 5, DE=4DE = 4, AE=9AE = 9 and BD=6BD = 6. Use Prim's algorithm starting from vertex AA 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 AA (Prim's)
The tree begins as {A}\{A\}. At each stage take the cheapest edge from a tree vertex to a vertex not yet included.
Stage 1
Edges leaving AA are ABAB (33) and AEAE (99). Cheapest is ABAB (33); BB joins. Tree {A,B}\{A, B\}.
Stage 2
Crossing edges from {A,B}\{A, B\} are AEAE (99), BCBC (77), BDBD (66). Cheapest is BDBD (66); DD joins. Tree {A,B,D}\{A, B, D\}.
Stage 3
Crossing edges are AEAE (99), BCBC (77), CDCD (55), DEDE (44). Cheapest is DEDE (44); EE joins. Tree {A,B,D,E}\{A, B, D, E\}.
Stage 4
Only CC remains. Crossing edges are BCBC (77) and CDCD (55). Cheaper is CDCD (55); CC joins. All five vertices are now connected.
Result
Edges added in order: ABAB, BDBD, DEDE, CDCD.

Total=3+6+4+5=18.\text{Total} = 3 + 6 + 4 + 5 = 18.

Check. Four edges join all 55 vertices with no cycle. The expensive edge AEAE (99) was never the cheapest crossing edge, so it is left out, confirming the MST weight is 1818.

core4 marks
A council plans to lay fibre to five towns AA, BB, CC, DD, EE. The possible cable routes have lengths (in km) AB=8AB = 8, AC=5AC = 5, BC=10BC = 10, BD=9BD = 9, CD=7CD = 7, CE=6CE = 6 and DE=4DE = 4. Using Prim's algorithm starting from town AA, find the minimum spanning tree and the least total length of cable.
Show worked solution →
Begin at AA (Prim's)
Tree {A}\{A\}. Each stage adds the cheapest cable from a connected town to an unconnected town.
Stage 1
From AA: ABAB (88), ACAC (55). Cheapest is ACAC (55); CC joins. Tree {A,C}\{A, C\}.
Stage 2
Crossing cables: ABAB (88), BCBC (1010), CDCD (77), CECE (66). Cheapest is CECE (66); EE joins. Tree {A,C,E}\{A, C, E\}.
Stage 3
Crossing cables: ABAB (88), BCBC (1010), CDCD (77), DEDE (44). Cheapest is DEDE (44); DD joins. Tree {A,C,D,E}\{A, C, D, E\}.
Stage 4
Only BB remains. Crossing cables: ABAB (88), BCBC (1010), BDBD (99). Cheapest is ABAB (88); BB joins.
Result
Cables laid: ACAC, CECE, DEDE, ABAB.

Total=5+6+4+8=23 km.\text{Total} = 5 + 6 + 4 + 8 = 23 \text{ km}.

Check. Four cables connect all 55 towns with no loop. The most expensive route BCBC (1010) is never used. The least total length of cable is 2323 km.

core5 marks
Six pumping stations AA, BB, CC, DD, EE, FF are linked by pipes with lengths AB=4AB = 4, AC=6AC = 6, BC=2BC = 2, BD=5BD = 5, CD=8CD = 8, CE=7CE = 7, DE=3DE = 3, DF=9DF = 9 and EF=6EF = 6. 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. BC(2)BC(2), DE(3)DE(3), AB(4)AB(4), BD(5)BD(5), AC(6)AC(6), EF(6)EF(6), CE(7)CE(7), CD(8)CD(8), DF(9)DF(9). A tree on 66 stations needs 6−1=56 - 1 = 5 pipes.

Apply Kruskal's, checking each edge for a cycle.

  • Add BCBC (22): tree {BC}\{BC\}.
  • Add DEDE (33): tree {BC,DE}\{BC, DE\}.
  • Add ABAB (44): brings in AA.
  • Add BDBD (55): links the {A,B,C}\{A, B, C\} piece to the {D,E}\{D, E\} piece.
  • Skip ACAC (66): both AA and CC are already connected, so this makes a cycle.
  • Add EFEF (66): brings in FF, the last station. Five pipes are now placed.
  • Remaining CECE (77), CDCD (88), DFDF (99) would all make cycles, so skip them.

Total length.

Total=2+3+4+5+6=20.\text{Total} = 2 + 3 + 4 + 5 + 6 = 20.

Check. Five pipes connect all 66 stations with no cycle. Where two edges tied at 66, ACAC made a cycle and EFEF did not, so EFEF was the correct choice. The MST length is 2020.

core5 marks
A weighted network on five vertices has edges AB=6AB = 6, AC=9AC = 9, AD=11AD = 11, BC=8BC = 8, BD=9BD = 9, CD=7CD = 7, CE=10CE = 10 and DE=5DE = 5. Find a minimum spanning tree using Kruskal's algorithm and state its total weight.
Show worked solution →

Sort the edges. DE(5)DE(5), AB(6)AB(6), CD(7)CD(7), BC(8)BC(8), AC(9)AC(9), BD(9)BD(9), CE(10)CE(10), AD(11)AD(11). A tree on 55 vertices needs 5−1=45 - 1 = 4 edges.

Apply Kruskal's algorithm.

  • Add DEDE (55): tree {DE}\{DE\}.
  • Add ABAB (66): tree {DE,AB}\{DE, AB\}, two separate pieces.
  • Add CDCD (77): brings CC in with DD and EE.
  • Add BCBC (88): links the {A,B}\{A, B\} piece to the {C,D,E}\{C, D, E\} piece, so all five vertices are joined.
  • Stop: 44 edges are in. The edges ACAC (99), BDBD (99), CECE (1010) and ADAD (1111) would each form a cycle, so they are skipped.

Total weight.

Total=5+6+7+8=26.\text{Total} = 5 + 6 + 7 + 8 = 26.

Check. Four edges connect all 55 vertices with no cycle, and the algorithm stopped before reaching the tied 99 edges, so neither was needed. The MST weight is 2626.

exam6 marks
A telecommunications company will lay cable to connect seven towns AA, BB, CC, DD, EE, FF, GG. The possible routes (in km) are AB=12AB = 12, AC=7AC = 7, BC=9BC = 9, BD=10BD = 10, CD=6CD = 6, CE=8CE = 8, DE=5DE = 5, DF=11DF = 11, EF=4EF = 4, EG=13EG = 13 and FG=9FG = 9. (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 99.
Show worked solution →

(a) Sort the routes (Kruskal's). EF(4)EF(4), DE(5)DE(5), CD(6)CD(6), AC(7)AC(7), CE(8)CE(8), BC(9)BC(9), FG(9)FG(9), BD(10)BD(10), DF(11)DF(11), AB(12)AB(12), EG(13)EG(13). A tree on 77 towns needs 7−1=67 - 1 = 6 cables.

Add the cheapest route each time, skipping any that closes a cycle:

  • Add EFEF (44); add DEDE (55); add CDCD (66); add ACAC (77).
  • Skip CECE (88): CC and EE are already joined, so it makes a cycle.
  • Add BCBC (99): brings in BB.
  • Add FGFG (99): brings in GG, the last town. Six cables are now placed.

Edges added in order: EFEF, DEDE, CDCD, ACAC, BCBC, FGFG.

(b) Least total length.

Total=4+5+6+7+9+9=40 km.\text{Total} = 4 + 5 + 6 + 7 + 9 + 9 = 40 \text{ km}.

(c) Why it is the minimum. The two edges of weight 99, BCBC and FGFG, connect different new towns (BB and GG), so both are needed and neither makes a cycle. There is no tie-break to resolve here, and the heavier routes BDBD (1010), DFDF (1111), ABAB (1212) and EGEG (1313) all close cycles, so the total cannot be reduced.

Check. Six cables join all 77 towns with no loop. The least total length of cable is 4040 km.

exam6 marks
A power network connects eight substations AA, BB, CC, DD, EE, FF, GG, HH with lines of length AB=6AB = 6, AC=9AC = 9, BC=4BC = 4, BD=8BD = 8, CD=7CD = 7, CE=12CE = 12, DE=5DE = 5, DF=10DF = 10, EF=3EF = 3, EG=14EG = 14, FG=8FG = 8, FH=11FH = 11 and GH=6GH = 6. (a) Find the minimum spanning tree using Kruskal's algorithm. (b) State the total length. (c) The edges ABAB and GHGH both have weight 66, and BDBD and FGFG both have weight 88. Does the choice between tied edges change the total length? Explain.
Show worked solution →

(a) Sort and apply Kruskal's. EF(3)EF(3), BC(4)BC(4), DE(5)DE(5), AB(6)AB(6), GH(6)GH(6), CD(7)CD(7), BD(8)BD(8), FG(8)FG(8), AC(9)AC(9), DF(10)DF(10), FH(11)FH(11), CE(12)CE(12), EG(14)EG(14). A tree on 88 substations needs 8−1=78 - 1 = 7 lines.

  • Add EFEF (33); add BCBC (44); add DEDE (55).
  • Add ABAB (66): brings in AA. Add GHGH (66): starts a separate {G,H}\{G, H\} piece.
  • Add CDCD (77): links the {A,B,C}\{A, B, C\} piece to the {D,E,F}\{D, E, F\} piece.
  • Skip BDBD (88): BB and DD are now in the same piece, so it makes a cycle.
  • Add FGFG (88): links in the {G,H}\{G, H\} piece, so all eight substations are joined. Seven lines placed; the rest close cycles and are skipped.

Edges used: EFEF, BCBC, DEDE, ABAB, GHGH, CDCD, FGFG.

(b) Total length.

Total=3+4+5+6+6+7+8=39.\text{Total} = 3 + 4 + 5 + 6 + 6 + 7 + 8 = 39.

(c) Effect of the ties. For the pair ABAB and GHGH (both 66), each joins a different part of the network, so both are needed. For the pair BDBD and FGFG (both 88), BDBD closes a cycle while FGFG does not, so FGFG 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 88 substations with no cycle. The total length is 3939.

exam7 marks
An engineer connects six sites AA, BB, CC, DD, EE, FF with cable. The available routes (in km) are AB=7AB = 7, AC=9AC = 9, AF=14AF = 14, BC=10BC = 10, BD=15BD = 15, CD=11CD = 11, CF=2CF = 2, DE=6DE = 6 and EF=9EF = 9. (a) Find the minimum spanning tree and its total length. (b) A contractor proposes instead to use the routes ABAB, BCBC, CFCF, DEDE and EFEF. 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: CF(2)CF(2), DE(6)DE(6), AB(7)AB(7), AC(9)AC(9), EF(9)EF(9), BC(10)BC(10), CD(11)CD(11), AF(14)AF(14), BD(15)BD(15). A tree on 66 sites needs 6−1=56 - 1 = 5 cables.

  • Add CFCF (22); add DEDE (66); add ABAB (77).
  • Add ACAC (99): links the {A,B}\{A, B\} piece to the {C,F}\{C, F\} piece.
  • Add EFEF (99): links the {D,E}\{D, E\} piece in, so all six sites are connected. Five cables placed.
  • Skip BCBC (1010), CDCD (1111), AFAF (1414), BDBD (1515): each would close a cycle.

Minimum spanning tree: CFCF, DEDE, ABAB, ACAC, EFEF.

Total=2+6+7+9+9=33 km.\text{Total} = 2 + 6 + 7 + 9 + 9 = 33 \text{ km}.

(b) Test the proposed tree. The proposed routes are ABAB, BCBC, CFCF, DEDE, EFEF. Trace the connections: AA-BB-CC-FF-EE-DD links all six sites in a single chain, using 55 edges with no repeated site, so it is a valid spanning tree (connected, 6−1=56 - 1 = 5 edges, no cycle).

Proposed=AB+BC+CF+DE+EF=7+10+2+6+9=34 km.\text{Proposed} = AB + BC + CF + DE + EF = 7 + 10 + 2 + 6 + 9 = 34 \text{ km}.

Compare. The proposed tree uses BCBC (1010) where the MST uses the cheaper ACAC (99), so it is 11 km longer.

Saving=34−33=1 km.\text{Saving} = 34 - 33 = 1 \text{ km}.

Check. Both networks connect all 66 sites with 55 cables. The MST total is 3333 km, which is 11 km less than the proposed 3434 km, so the minimum spanning tree saves 11 km.

Practise this

Sources & how we know this

ExamExplained