Skip to main content

Trees, minimum spanning trees and Prim's algorithm: VCE General Mathematics Unit 4 Networks and decision mathematics

Syllabus dot point

“Identify trees and spanning trees, determine a minimum spanning tree of a weighted connected graph by inspection or by Prim's algorithm (from a network or a table), and use minimum spanning trees to solve minimal connector problems”

14 min read

Quick answer

A tree is a connected graph with no cycles; with nn vertices it has exactly n−1n - 1 edges and its degrees add to 2(n−1)2(n - 1). A spanning tree is a tree using every vertex of a graph. The minimum spanning tree has the least total weight. Prim's algorithm: start anywhere, then repeatedly add the cheapest edge that joins the tree to a new vertex, until n−1n - 1 edges are in. By inspection: add the cheapest edges overall, skipping any that make a cycle. Both give the same minimum 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

The Study Design's "Trees and minimum connector problems" topic has three parts: "trees and spanning trees", "minimum spanning trees in a weighted connected graph and their determination by inspection or by Prim's algorithm", and "use of minimal spanning trees to solve minimal connector problems". A minimal connector problem asks for the cheapest (or shortest) set of links that joins every location: cabling buildings, piping houses, sealing roads between towns.

You need to:

  1. recognise a tree and know its properties;
  2. find a spanning tree of a graph;
  3. find a minimum spanning tree by inspection or by Prim's algorithm, from a network diagram or a table, and give its weight;
  4. handle variations: an edge that must be included, an unknown weight, a new edge.

These are usually one-mark multiple-choice questions (2023 Examination 1 Question 35, 74%74\%; 2025 Examination 1 Question 35, 65%65\%) or a short part of an Examination 2 networks question.

The answer

Trees

A tree is a connected graph with no cycles. Every tree with nn vertices has:

  • exactly n−1n - 1 edges;
  • exactly one path between any two vertices;
  • a sum of degrees of 2(n−1)2(n - 1), because every edge adds 2 to the degree sum;
  • at least two vertices of degree 1 (if n≥2n \ge 2).

Removing any edge from a tree disconnects it (every edge is a bridge), and adding any edge between two of its vertices creates exactly one cycle. For planar graphs, a tree has one face, so Euler's formula v+f=e+2v + f = e + 2 becomes n+1=(n−1)+2n + 1 = (n - 1) + 2, which checks out.

Spanning trees

A spanning tree of a connected graph is a subgraph that includes every vertex and is a tree. A graph usually has many spanning trees. If the graph has nn vertices and ee edges, a spanning tree keeps n−1n - 1 of them, so e−(n−1)e - (n - 1) edges are removed, each one breaking a cycle without disconnecting anything. When you draw one, use only edges from the original graph (in 2025 Examination 2 Question 15d, 81%81\% succeeded; some lost the mark by adding an edge that was not in the graph).

Minimum spanning trees

In a weighted graph, a minimum spanning tree is a spanning tree with the smallest total weight. It is the answer to every minimal connector problem. There may be more than one minimum spanning tree when some weights are equal, but they all have the same total.

A weighted network and its minimum spanning treeSeven vertices A to G with twelve weighted edges: AB 7, AC 5, BC 9, BD 8, CD 6, BE 12, DE 5, CF 11, DF 7, EG 6, FG 9 and EF 10. The six edges of the minimum spanning tree are drawn thick: AC 5, CD 6, DE 5, EG 6, AB 7 and DF 7, in the order Prim's algorithm adds them when it starts at A. Their total weight is 36.759861251176910ABCDEFGPrim from A adds AC (5), CD (6), DE (5), EG (6), AB (7), DF (7): total 36

Prim's algorithm

  1. Start at any vertex. It is the tree so far.
  2. Look at every edge that joins a vertex in the tree to a vertex not yet in the tree. Add the one with the smallest weight (if there is a tie, either will do).
  3. Repeat step 2 until all nn vertices are in, which takes n−1n - 1 edges.

Because each new edge always goes to a new vertex, Prim's algorithm can never create a cycle. The starting vertex can change the order in which edges are added, but not the total.

In the figure, starting at AA: AC=5AC = 5; then from {A,C}\{A, C\} the cheapest is CD=6CD = 6; then DE=5DE = 5; then EG=6EG = 6; then AB=7AB = 7 and DF=7DF = 7 (tied, either first). Total 5+6+5+6+7+7=365 + 6 + 5 + 6 + 7 + 7 = 36.

Finding it by inspection

For small networks (and most multiple-choice questions), list the edges from smallest to largest and add them one at a time, skipping any edge that would complete a cycle, until you have n−1n - 1 edges. This greedy method always gives a minimum spanning tree. It adds edges in a different order from Prim's, so use Prim's whenever a question asks for "the order in which Prim's algorithm adds the edges".

Prim's algorithm from a table

Networks are often given as a table of weights, with a dash for "no direct link". The method is unchanged:

  1. Circle the starting vertex. Its row lists the edges from it.
  2. Among the rows of all vertices already chosen, pick the smallest weight leading to a vertex not yet chosen.
  3. Add that vertex and repeat, ignoring entries between two chosen vertices.

Record each edge as you add it; the list is both your working and the answer.

Forced edges, unknown weights and new edges

  • An edge must be included (an existing road, a fixed link): put it in first, then carry on choosing the cheapest edges that avoid cycles. The total can only stay the same or rise.
  • An unknown weight ww is in the tree (2025 Examination 1 Question 35): include ww first, find the other n−2n - 2 edges, and write the length as a number plus ww.
  • A new edge is proposed: it enters the tree only if it is lighter than the heaviest edge on the cycle it would create with the tree.

Minimum spanning tree or shortest path?

A minimum spanning tree connects all vertices at least total cost. The route it gives between two particular vertices is often not the shortest route between them. In the figure, the tree route from BB to GG is 7+5+6+5+6=297 + 5 + 6 + 5 + 6 = 29, but the direct path BB-EE-GG is only 12+6=1812 + 6 = 18. "Connect everything" means minimum spanning tree; "quickest way from here to there" means shortest path (Dijkstra's algorithm).

Worked examples: Prim's algorithm, tables and variations

Prim's algorithm on the network in the figure

Find the minimum spanning tree starting from GG, and check the total.

Step 1
Tree {G}\{G\}. Edges out: EG=6EG = 6, FG=9FG = 9. Add EG=6EG = 6.
Step 2
Tree {G,E}\{G, E\}. Edges out: FG=9FG = 9, DE=5DE = 5, BE=12BE = 12, EF=10EF = 10. Add DE=5DE = 5.
Step 3
Tree {G,E,D}\{G, E, D\}. Edges out: FGFG 9, BEBE 12, EFEF 10, BDBD 8, CDCD 6, DFDF 7. Add CD=6CD = 6.
Step 4
Tree {G,E,D,C}\{G, E, D, C\}. New options from CC: ACAC 5, BCBC 9, CFCF 11. Add AC=5AC = 5.
Step 5
Tree {G,E,D,C,A}\{G, E, D, C, A\}. Options include ABAB 7, DFDF 7, BDBD 8. Add AB=7AB = 7 (tie with DFDF).
Step 6
Only FF remains: DF=7DF = 7 is cheapest (against FGFG 9, EFEF 10, CFCF 11). Add DFDF.
Answer
Edges EGEG, DEDE, CDCD, ACAC, ABAB, DFDF: six edges for seven vertices, total 6+5+6+5+7+7=366 + 5 + 6 + 5 + 7 + 7 = 36, the same as starting from AA.

From a table, with a forced edge

Road lengths (km) between five towns, dash for no possible road:

PP QQ RR SS TT
PP 14 9 20 -
QQ 14 12 - 8
RR 9 12 11 15
SS 20 - 11 10
TT - 8 15 10

Step 1: start at PP. Row PP: smallest is PR=9PR = 9.

Step 2: rows PP and RR. Candidates to new towns: PQPQ 14, PSPS 20, RQRQ 12, RSRS 11, RTRT 15. Add RS=11RS = 11.

Step 3: rows PP, RR, SS. New options from SS: STST 10. Smallest overall is ST=10ST = 10. Add TT.

Step 4: last town QQ. Options PQPQ 14, RQRQ 12, TQTQ 8. Add QT=8QT = 8.

Answer. PRPR, RSRS, STST, QTQT: minimum length 9+11+10+8=389 + 11 + 10 + 8 = 38 km. If the existing road PS=20PS = 20 must be kept, include it first and continue with QT=8QT = 8, PR=9PR = 9, ST=10ST = 10 (now RSRS would close a cycle): total 47 km, 9 km more.

An unknown weight in the tree

A network has vertices VV, WW, XX, YY, ZZ and edges VW=4VW = 4, VX=kVX = k, WX=6WX = 6, WY=3WY = 3, XY=5XY = 5, XZ=7XZ = 7, YZ=8YZ = 8. The minimum spanning tree includes edge VXVX. Find its length in terms of kk, and the largest possible whole-number value of kk.

Step 1: include VXVX, then the cheapest edges without cycles
WY=3WY = 3, VW=4VW = 4 (joins WW to VV), then XY=5XY = 5 would close the cycle VV-WW-YY-XX-VV, so skip it. WX=6WX = 6 would also close a cycle. XZ=7XZ = 7 adds ZZ.
Step 2: total
Edges VXVX, WYWY, VWVW, XZXZ: length k+3+4+7=14+kk + 3 + 4 + 7 = 14 + k.
Step 3: when is VXVX really in the tree
Without VXVX, XX would join by XY=5XY = 5 (tree WYWY, VWVW, XYXY, XZXZ, weight 19). VXVX is used only if k+14≤19k + 14 \le 19, that is k≤5k \le 5. Largest whole number: k=5k = 5 (a tie, where both trees weigh 19).
Common traps
Wrong number of edges
A spanning tree of nn vertices has exactly n−1n - 1 edges. Count before you add up.
Closing a cycle by inspection
When choosing the cheapest edges overall, check each new edge does not join two vertices already connected.
Treating inspection order as Prim's order
If asked for Prim's order, only edges touching the current tree are allowed at each step.
Reading a table's dash as zero
A dash means there is no edge.

Using an edge that is not in the graph when drawing a spanning tree.

Confusing a minimum spanning tree with a shortest path. The tree minimises the total of all edges, not the distance between two chosen vertices.

Forgetting a forced edge or ww in the total. Include it and add its weight.

Exam technique

For multiple-choice minimum spanning tree questions, write n−1n - 1 first, then list the chosen weights in increasing order and tick them off on the diagram; if you have too many or too few, recheck for cycles. For Prim's questions, write each step as "tree so far: cheapest edge out" so the marker can follow your order. Always give the total with units, and in context say what it means ("the minimum length of cable needed is 38 km").

Note

A tree is a network with no loops, so there is exactly one way between any two points and it always has one fewer link than points. When you need to join every place with the least cable, road or pipe, you want the cheapest tree that reaches everywhere: the minimum spanning tree. Prim's method grows it one step at a time, always adding the cheapest link to a place not yet connected. The result joins everything cheaply, but it is not meant to be the fastest route between any two particular places.

Exam-style questions

Questions in the style of VCAA 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.

2023 VCAA-style1 mark
A graph has five vertices joined in a single chain by four edges, with no other edges. How many of the following five statements are true? The graph is a tree. The graph is connected. The graph contains a path. The graph contains a cycle. The sum of the degrees of the vertices is eight. A. 1 B. 2 C. 3 D. 4 E. 5
Show worked answer →
  • Tree: true. It is connected and has no cycle (5 vertices, 5−1=45 - 1 = 4 edges).
  • Connected: true. Every vertex can be reached from every other.
  • Contains a path: true. Any route along the chain is a path (no repeated vertices).
  • Contains a cycle: false. A tree never contains a cycle.
  • Sum of degrees is eight: true. The sum of the degrees is 2×2 \times the number of edges =2×4=8= 2 \times 4 = 8.

Four statements are true: option D (56%56\% correct; 30%30\% chose C, usually by rejecting "contains a path" or miscounting degrees).

Source: VCAA 2023 General Mathematics Examination 1, Question 33, and the 2023 examination report. (The paper shows the graph as a drawing; it is described in words here.)

2023 VCAA-style1 mark
A weighted graph has six vertices and seven edges. Labelling the vertices PP to UU (the paper leaves them unlabelled), the edges are PQ=10PQ = 10, PR=9PR = 9, RS=6RS = 6, ST=7ST = 7, QU=8QU = 8, UT=12UT = 12 and QT=15QT = 15. The weight of the minimum spanning tree is A. 30 B. 32 C. 40 D. 42 E. 52
Show worked answer →

Six vertices, so the tree has 6−1=56 - 1 = 5 edges. By inspection, take the smallest edges in turn and skip any that would make a cycle:

RS=6RS = 6, ST=7ST = 7, QU=8QU = 8, PR=9PR = 9 and PQ=10PQ = 10. These five join all six vertices with no cycle (UT=12UT = 12 and QT=15QT = 15 would each close a cycle).

Weight =6+7+8+9+10=40= 6 + 7 + 8 + 9 + 10 = 40: option C (74%74\% correct). Prim's algorithm from any vertex gives the same total.

Source: VCAA 2023 General Mathematics Examination 1, Question 35, and the 2023 examination report.

2025 VCAA-style1 mark
A weighted graph has twelve vertices. Labelling them AA to LL (unlabelled in the paper), its edges are AB=5AB = 5, AI=6AI = 6, BJ=8BJ = 8, BC=6BC = 6, CD=4CD = 4, DE=7DE = 7, BF=wBF = w, EG=4EG = 4, EF=2EF = 2, GH=6GH = 6, FH=9FH = 9, GK=5GK = 5, IJ=3IJ = 3, JK=6JK = 6, KL=5KL = 5, LH=4LH = 4 and IF=11IF = 11. The minimum spanning tree contains the edge with weight ww. The length of this minimum spanning tree is A. 37+w37 + w B. 44+w44 + w C. 48+w48 + w D. 49+w49 + w
Show worked answer →

Twelve vertices need 12−1=1112 - 1 = 11 edges. The edge ww is in the tree, so include it first, then add the smallest remaining edges that do not form a cycle:

EF=2EF = 2, IJ=3IJ = 3, CD=4CD = 4, EG=4EG = 4, LH=4LH = 4, AB=5AB = 5, GK=5GK = 5, KL=5KL = 5, AI=6AI = 6, BC=6BC = 6.

Skipped: the other 6s (GHGH and JKJK) and DE=7DE = 7, BJ=8BJ = 8, FH=9FH = 9, IF=11IF = 11, since each would close a cycle.

Length =w+2+3+4+4+4+5+5+5+6+6=44+w= w + 2 + 3 + 4 + 4 + 4 + 5 + 5 + 5 + 6 + 6 = 44 + w: option B (65%65\% correct).

Source: VCAA 2025 General Mathematics Examination 1, Question 35, and the 2025 examination report. (Vertex labels added here; the paper shows the graph.)

Practice questions

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

foundation1 mark
A connected network has 7 vertices. A spanning tree of this network must contain exactly how many edges? A. 5 B. 6 C. 7 D. 8
Show worked solution →

A spanning tree of a connected network with nn vertices has exactly n−1n - 1 edges. Here 7−1=67 - 1 = 6: option B. More than 6 edges would create a cycle; fewer would leave a vertex disconnected.

foundation2 marks
A connected graph has 9 vertices and 14 edges. (a) How many edges must be removed to leave a spanning tree? (b) What is the sum of the degrees of the vertices of that spanning tree?
Show worked solution →

(a) A spanning tree has 9−1=89 - 1 = 8 edges, so remove 14−8=614 - 8 = 6 edges (each chosen to break a cycle without disconnecting the graph). (1 mark)

(b) Sum of degrees =2×8=16= 2 \times 8 = 16. (1 mark)

core3 marks
A council must lay cable connecting five sites PP, QQ, RR, SS, TT. The costs (in thousands of dollars) are PQ=7PQ = 7, PR=5PR = 5, QR=6QR = 6, QS=9QS = 9, RS=4RS = 4, RT=8RT = 8, ST=3ST = 3. (a) Apply Prim's algorithm starting from PP, listing the edges in the order they are added. (b) Find the minimum total cost.
Show worked solution →

(a) From PP: cheapest edge is PR=5PR = 5. From {P,R}\{P, R\}: RS=4RS = 4 (add SS). From {P,R,S}\{P, R, S\}: ST=3ST = 3 (add TT). Only QQ remains, via PQ=7PQ = 7, QR=6QR = 6 or QS=9QS = 9: add QR=6QR = 6. Order: PRPR, RSRS, STST, QRQR. (2 marks)

(b) 5+4+3+6=185 + 4 + 3 + 6 = 18, so $18 000. The tree has 5−1=45 - 1 = 4 edges and no cycle. (1 mark)

core2 marks
A network has edges JK=4JK = 4, JL=6JL = 6, KL=3KL = 3, KM=7KM = 7, LM=5LM = 5, LN=9LN = 9, MN=2MN = 2, MO=8MO = 8 and NO=4NO = 4. Find the minimum spanning tree and its weight using Prim's algorithm from JJ.
Show worked solution →

From JJ: JK=4JK = 4. From {J,K}\{J, K\}: KL=3KL = 3. From {J,K,L}\{J, K, L\}: LM=5LM = 5 (JL=6JL = 6 now joins two tree vertices, so it is not a candidate, and KM=7KM = 7 and LN=9LN = 9 are dearer). From {J,K,L,M}\{J, K, L, M\}: MN=2MN = 2. Last, OO: NO=4NO = 4 (cheaper than MO=8MO = 8). (1 mark)

Tree: JKJK, KLKL, LMLM, MNMN, NONO, weight 4+3+5+2+4=184 + 3 + 5 + 2 + 4 = 18. (1 mark)

exam3 marks
Five towns are linked by possible roads (km). A dash means no direct road is possible. PP: QQ 14, RR 9, SS 20, TT -. QQ: RR 12, SS -, TT 8. RR: SS 11, TT 15. SS: TT 10. (a) Use Prim's algorithm from PP to find the minimum length of road needed to connect all five towns. (b) An old road PSPS must be kept as part of the network. Find the new minimum total length and the increase.
Show worked solution →

(a) Start at PP: smallest from PP is PR=9PR = 9. From {P,R}\{P, R\}: options PQPQ 14, PSPS 20, RQRQ 12, RSRS 11, RTRT 15; add RS=11RS = 11. From {P,R,S}\{P, R, S\}: ST=10ST = 10 is smallest; add TT. From {P,R,S,T}\{P, R, S, T\}: QT=8QT = 8; add QQ. Total 9+11+10+8=389 + 11 + 10 + 8 = 38 km. (2 marks)

(b) Include PS=20PS = 20 first, then add the cheapest edges that avoid cycles: QT=8QT = 8, PR=9PR = 9, ST=10ST = 10 (RS=11RS = 11 would now close the cycle PP-RR-SS). Total 20+8+9+10=4720 + 8 + 9 + 10 = 47 km, an increase of 47−38=947 - 38 = 9 km. (1 mark)

exam2 marks
In the seven-vertex network drawn on this page (minimum spanning tree weight 36), a new edge BGBG of weight xx is proposed. For which values of xx would the minimum spanning tree change, and what would its new weight be?
Show worked solution →

In the current tree the route from BB to GG is BB-AA-CC-DD-EE-GG, with edges 77, 55, 66, 55, 66. Adding BGBG creates a cycle with this route, and the tree improves only if BGBG is lighter than the heaviest edge on it, AB=7AB = 7. (1 mark)

So for x<7x < 7 the new tree swaps ABAB for BGBG, with weight 36−7+x=29+x36 - 7 + x = 29 + x. For x≥7x \ge 7 the tree (or its weight, if x=7x = 7) is unchanged. (1 mark)

Practise this

Sources & how we know this

ExamExplained