IB Maths AI HL Graph Theory Paper 1 & 2 Prim’s algorithm ~8 min read

Minimum Spanning Trees (Prim’s Algorithm)

Prim’s algorithm finds the same minimum spanning tree as Kruskal’s but takes a different route to get there. Instead of sorting all edges first, Prim’s starts at any one vertex and grows the tree outward, always reaching to the nearest unvisited neighbour. The big bonus: it works just as cleanly on a weighted adjacency table as on a drawn graph — making it the go-to algorithm whenever the data is in matrix form.

šŸ“˜ What you need to know

Growing the tree from a single vertex

Think of Prim’s algorithm as building a village outward from a single house. You pick a starting house, look at all the roads leaving it, and pave the cheapest one to its neighbour. Now you have two connected houses. Next, you look at every road leaving either of these two houses, pick the cheapest one that reaches a brand-new house, and pave it. Repeat until every house in the village is connected. That’s Prim’s algorithm.

At each step you only ever look at “boundary” edges — those with one end inside the growing tree and the other end outside. Edges between two vertices that are both already in the tree are ignored entirely; that’s how cycles are automatically avoided. After n − 1 steps every vertex is in the tree, and you’ve found the MST.

Growing the MST one vertex at a time 7 6 8 3 step 1 5 step 2 2 step 3 4 step 4 A 1 B 5 C 3 D 4 E 2 total = 3+5+2+4 = 14 Prim’s algorithm ā‘  Pick any starting vertex label it ā‘ , e.g. start at A ā‘” Cheapest “boundary” edge smallest edge from any selected vertex to an unselected vertex ā‘¢ Repeat until all n added total of n āˆ’ 1 edges ā‘£ Sum the edge weights tree weight = 14no cycle check ever needed
Starting at A, Prim’s adds vertices in the order A → E → C → D → B (badges 1 through 5), choosing the cheapest boundary edge at each step. The final MST has total weight 14 — the same as Kruskal’s would give on this graph.
Prim’s algorithm in symbols start with Vtree = { vstart }  ā‡’  add min-weight edge from Vtree to outside  ā‡’  repeat until |Vtree| = n selects exactly n − 1 edges; total MST weight = sum of selected edges

Prim’s on a weighted adjacency table

When the graph is given as a table (a weighted adjacency matrix), drawing it out can be slow. Prim’s algorithm has a beautifully clean version that works directly on the table — the steps are: pick a starting row → cross out its column → circle the smallest value in that row → cross out that column → label the row of the newly-reached vertex → look across all labelled rows for the next smallest. Each circled value is an edge of the MST.

Here’s the procedure laid out:

StepAction
1Pick a starting vertex. Label its row “1” and cross out that vertex’s column.
2In the labelled row(s), find the smallest uncrossed entry. Circle it. This is the next MST edge.
3Identify the column the circled value sits in — that’s the new vertex. Cross out its column and label its row with the next number.
4Repeat steps 2–3 until every row has been labelled. The labels give the order vertices joined the tree.
Why Prim’s beats Kruskal’s on tables: with a table, there’s no quick way to “see” cycles, so Kruskal’s cycle check becomes painful. Prim’s automatically avoids cycles (you never re-enter a vertex), so it slots into table form with no extra checks. If a question gives you a table, reach for Prim’s.

🧭 Recipe — Prim’s algorithm (graph or table form)

  1. Pick a starting vertex. Any vertex works; the final weight is the same.
  2. From every vertex already in the tree, find the cheapest edge that leads to a vertex not in the tree.
  3. Add that edge and its new endpoint. Record the order of selection.
  4. Repeat step 2 until all n vertices are in the tree (i.e. you have n − 1 edges).
  5. Sum the weights of the chosen edges — that’s the MST total weight. Draw the final tree.

Worked examples

WE 1

Prim’s algorithm on a small graph

Use Prim’s algorithm, starting at vertex A, to find the MST of the graph with vertices { A, B, C, D, E } and edges AB(6), AC(2), AD(7), BC(3), BD(5), BE(4), CD(8), CE(9), DE(1). State the order of vertices added and the total weight.

step 1: start at A; tree = { A } edges from A: AB(6), AC(2), AD(7) smallest: AC(2) → add C step 2: tree = { A, C } boundary edges: AB(6), AD(7), BC(3), CD(8), CE(9) smallest: BC(3) → add B step 3: tree = { A, B, C } boundary: AD(7), BD(5), BE(4), CD(8), CE(9) smallest: BE(4) → add E step 4: tree = { A, B, C, E } boundary: AD(7), BD(5), CD(8), DE(1) smallest: DE(1) → add D āœ“ done (nāˆ’1=4) total weight 2 + 3 + 4 + 1 = 10 order: A → C → B → E → D; weight = 10 edges used: AC, BC, BE, DE — note that DE(1) was the cheapest in the whole graph but was added last, because D wasn’t reachable from A directly cheaply.
WE 2

Same graph, different starting vertex

Apply Prim’s algorithm to the same graph in WE 1, but starting at vertex D instead. Verify that the total weight is unchanged.

step 1: start at D; tree = { D } edges from D: AD(7), BD(5), CD(8), DE(1) smallest: DE(1) → add E step 2: tree = { D, E } boundary: AD(7), BD(5), BE(4), CD(8), CE(9) smallest: BE(4) → add B step 3: tree = { B, D, E } boundary: AB(6), AD(7), BC(3), CD(8), CE(9) smallest: BC(3) → add C step 4: tree = { B, C, D, E } boundary: AB(6), AC(2), AD(7) smallest: AC(2) → add A āœ“ done total weight 1 + 4 + 3 + 2 = 10 order: D → E → B → C → A; weight = 10 āœ“ same edges, same total — the order changes but the MST itself doesn’t.
WE 3

Prim’s on a weighted adjacency table

The table below shows distances (km) between five towns. Use Prim’s algorithm starting at A to find the MST.

ABCDE
A851012
B8694
C56311
D10937
E124117
label row A → ā‘ ; cross out column A smallest in row A: AC = 5 → circle label row C → ā‘”; cross out column C smallest in rows A, C (uncrossed): CD = 3 → circle label row D → ā‘¢; cross out column D smallest in rows A, C, D: CB = 6 → circle label row B → ā‘£; cross out column B smallest in rows A,B,C,D: BE = 4 → circle label row E → ⑤; all rows labelled, done edges: AC(5), CD(3), CB(6), BE(4) total = 5 + 3 + 6 + 4 = 18 order: A → C → D → B → E; weight = 18 km working straight on the table is much faster than drawing the graph — no need to plot anything.
WE 4

Equal weights at a step

Use Prim’s algorithm to find the MST of a graph with edges AB(5), AC(4), AD(4), BC(2), BD(6), CD(7), starting at A. If there’s a tie, either choice is acceptable.

step 1: tree = { A } edges from A: AB(5), AC(4), AD(4) tie at 4: pick AC → add C (AD also valid) step 2: tree = { A, C } boundary: AB(5), AD(4), BC(2), CD(7) smallest: BC(2) → add B step 3: tree = { A, B, C } boundary: AD(4), BD(6), CD(7) smallest: AD(4) → add D āœ“ done total weight 4 + 2 + 4 = 10 MST = { AC, BC, AD }; weight = 10 had we picked AD instead of AC at step 1, the next steps would have given { AD, BC, AC } — same total weight of 10.
WE 5

Practical problem — water pipe network

A village engineer wants to connect five wells to a shared water supply by laying pipe between them (the shortest connections, in metres, are shown). Use Prim’s algorithm starting at well V to find the minimum total length of pipe needed.

VWXYZ
V20354015
W20253045
X35251050
Y40301022
Z15455022
start at V → ā‘ ; cross column V smallest in row V: VZ = 15 → circle label row Z → ā‘”; cross column Z smallest in rows V, Z: VW = 20 → circle label row W → ā‘¢; cross column W smallest in rows V, W, Z: WX = 25 → circle label row X → ā‘£; cross column X smallest in rows V, W, X, Z: XY = 10 → circle label row Y → ⑤; done total = 15 + 20 + 25 + 10 = 70 m minimum pipe length = 70 m spot the “look out” tip: XY = 10 was the cheapest edge in the whole table, but only joined the tree at step 4 because X wasn’t reachable cheaply until step 3.
WE 6

Larger graph — 6 vertices from a table

Apply Prim’s algorithm starting at vertex P to find the MST of the network below. State the order of vertex selection and the total weight.

PQRSTU
P3912714
Q3510811
R954613
S12104215
T78621
U141113151
start at P → ā‘  smallest in row P: PQ = 3 → add Q ā‘” rows P, Q labelled smallest uncrossed: QR = 5 → add R ā‘¢ rows P, Q, R labelled smallest uncrossed: RS = 4 → add S ā‘£ rows P, Q, R, S labelled smallest uncrossed: ST = 2 → add T ⑤ rows P, Q, R, S, T labelled smallest uncrossed: TU = 1 → add U ā‘„ total weight 3 + 5 + 4 + 2 + 1 = 15 order: P → Q → R → S → T → U; weight = 15 edges: PQ, QR, RS, ST, TU — this MST is a chain because each new vertex was always closest to the most-recently added one.

Kruskal’s vs Prim’s — which to use?

Both algorithms always find the same total MST weight; the choice depends on how the question is presented:

SituationUse
Graph is drawn out, easy to spot edges visuallyEither — Kruskal’s is often quicker for small graphs.
Data given as a weighted adjacency tablePrim’s — works directly on the table; no need to redraw.
Question specifies one methodUse that one (don’t change just to “show off”).
Large graph with many edgesPrim’s — no need to sort all edges, no cycle checks.

šŸ’” Top tips

⚠ Common mistakes

Next up — The Chinese Postman Problem. Once you can find a minimum spanning tree, the next class of network problems asks: can a postman walk along every road exactly once and return home? This is where Eulerian trails and circuits come in, and you’ll use the degrees of vertices (back from the Introduction page) to decide whether such a route exists.

Need help with Graph Theory?

Get 1-on-1 help from an IB examiner who knows exactly what Paper 1 & 2 are looking for.

Book Free Session →