In this guide
← Maths AI guides

Geometry & trigonometry · AI HL

Minimum spanning trees and shortest paths

Use Kruskal, Prim and Dijkstra correctly, and distinguish connecting an entire network from travelling between two points.

Course focus: AI HL · First-assessment-2021 course. The sections distinguish the mathematical idea, the calculation and its interpretation.

Before you start: Weighted graphs, connectivity, paths and simple arithmetic.

Start with a small example

Build it from the beginning

HL content

A shortest path connects chosen endpoints cheaply. A minimum spanning tree connects every vertex with minimum total edge weight and no cycles. The objectives differ, so the best path from one vertex need not form the minimum spanning tree.

A first walkthrough

A triangle has weights AB=2, BC=3 and AC=8. Find its minimum spanning tree.

  1. Take AB, the smallest edge.
  2. Add BC; all three vertices are now connected without a cycle.
  3. The weight is 5. Adding AC would make a cycle and is unnecessary.
Try the practice ladder after learning the methods ↓

The foundations behind the method

Understand what the symbols mean

Connecting everyone and making one journey cheaply are distinct optimisation problems. A minimum spanning tree is a network-wide selection of links. A shortest path is an ordered route from a source. Understanding the objective explains why a tree can be optimal infrastructure without providing optimal journeys between all locations.

How the ideas connect

Kruskal and Prim exploit cut and cycle structure to build a minimum tree. Dijkstra instead updates cumulative source distances. Their greedy decisions are different: a cheap edge is useful for a tree only when it connects components, and a cheap last edge is useful for a route only when its whole prefix is also cheap.

01

Start with the idea

Build your understanding

Coverage

A spanning solution must include every vertex, even when its cheapest useful connection is relatively expensive.

Cumulative cost

A shortest-path label is a complete route cost from the source, not the weight of the most recent edge.

Compare the approaches

Choose by the information in the question, rather than the formula you recognise first.

Prim or Kruskal

When it helpsUse for minimum total cost to connect all vertices.

Check before using itDo not mistake a minimum spanning tree for a shortest-route network.

Dijkstra

When it helpsUse for shortest paths with nonnegative weights.

Check before using itA negative edge removes the usual guarantee; use a suitable alternative or the method specified by the task.

Your method plan

  1. Identify whether the objective is a tree or a route.
  2. Record each eligible selection or cumulative-distance update.
  3. Check coverage or reconstruct the route and total.
02

Learn the methods

Learn step by step

1. Two optimisation objectives

A shortest-path problem minimizes the cost of one route between specified vertices. A minimum spanning tree connects every vertex with minimum total edge weight, using no cycles. It does not necessarily contain the shortest route between every pair. State whether the task builds infrastructure or chooses a journey. Both use weights but answer different questions, so do not select an algorithm simply because the network has numbers on its edges.

Worked example: A minimum network is not the quickest pairwise journey

A triangle has AB=4, BC=3 and AC=5. Compare its minimum spanning tree with its shortest A-to-C route.

  1. Kruskal chooses BC=3 and AB=4, reaching all vertices without a cycle.
  2. The tree total is 7. Its route from A to C travels through B and also costs 7.
  3. The original direct edge AC costs 5, so it is the shortest A-to-C route.
  4. There is no contradiction: the tree minimizes the total infrastructure needed to connect all vertices, not every journey through the original network.

2. What a spanning tree must satisfy

A tree is connected and has no cycles. For n vertices, any tree has n−1 edges. A spanning tree includes every vertex of the original connected graph. Positive weights are common in infrastructure examples, but the spanning-tree algorithms also work with zero or negative weights. Equal weights can produce several different trees with the same minimum total. An edge count alone is insufficient: the chosen edges must also connect all vertices without cycling.

Tree edge count

Why this formula works

Begin with one vertex and no edges. Each tree edge that adds a new vertex increases both the vertex count and edge count by one without making a cycle. Reaching n vertices therefore uses n minus one edges. Connectivity and absence of cycles are still necessary checks.

The formula

\(|E_{\mathrm{tree}}|=n-1\).

Quick example

A spanning tree on six vertices has five edges, not six. Five arbitrary selected edges do not necessarily form a tree.

3. Kruskal grows a forest

Order all edges by increasing weight. Add the next cheapest edge unless it creates a cycle with those already chosen. Stop when every vertex is connected with n−1 selected edges. Several separate components may exist during the process. Keep rejected-cycle decisions visible in a worked table; simply listing the cheapest n−1 edges can fail because they may form a cycle and omit a vertex. Ties can be resolved consistently in any order.

A cycle must be rejected before the last vertex is reached

ABCD12346
In this network Kruskal selects AB=1 and BC=2, rejects AC=3 because of a cycle, then selects CD=4. The minimum spanning-tree total is 7; D still needs a connection even though a cheaper edge was available inside the existing component.
Worked example: Kruskal on a small network

Edges are AB=1, BC=2, AC=3, CD=4, BD=6. Find a minimum spanning tree.

  1. Choose AB, then BC; the growing component contains A,B,C.
  2. Reject AC because it would close the A–B–C–A cycle.
  3. Choose CD to reach D. Three edges connect all four vertices.
  4. The total is 1+2+4=7. Choosing the three smallest edges would have omitted D.

4. Prim grows one connected component

Start at any vertex. At each step add a cheapest edge linking the current tree to a vertex outside it. Edges entirely inside the current tree are not eligible even if cheap. Continue until every vertex is reached. Prim’s starting point can change the chosen tree when equal alternatives exist but does not change the minimum achievable total. Record the growing vertex set as a way to check each eligible connection.

5. Dijkstra finalises distance labels

For nonnegative edge weights, give the source distance zero and all others an initially infinite or unknown tentative distance. Repeatedly select the unvisited vertex with smallest tentative label, finalize it, and update neighbours through it. Store predecessor information to reconstruct a route. Adding an edge weight to a distance label is different from choosing the lightest edge alone. Negative edges invalidate the standard algorithm’s guarantee.

Distance relaxation

Why this formula works

A route through u has its known source-to-u cost plus the final edge cost to v. Compare this candidate with the best currently known route to v. With nonnegative weights, the least tentative label can be finalized because any later detour cannot lower it through a longer unvisited prefix.

The formula

\(d(v)\leftarrow\min\{d(v),d(u)+w(u,v)\}\).

Quick example

If d(u)=7 and the edge to v weighs 3, a current d(v)=12 improves to 10, with u recorded as the new predecessor.

Worked example: A shortest route is a separate calculation

In the same network, find a shortest A-to-D route.

  1. Start A at 0. Initial labels are B=1 and C=3.
  2. Finalize B and consider B–C: its candidate cost is 3, tied with the direct A–C route; D gets label 7 via B.
  3. Finalize C and update D via C to 3+4=7, another tie.
  4. A–B–D, A–C–D and A–B–C–D all cost 7. A reported route must be one of the feasible equal-cost choices.

6. Verify optimality and meaning

Check that a spanning solution connects all vertices, uses no cycles and has the stated weight. For a shortest route, reconstruct the predecessor chain and sum its edges rather than reporting only a distance label. A route can have equal-cost alternatives. Keep weights in their original units: a distance-minimizing route need not be quickest if speeds differ. An algorithm’s optimality applies to the graph model, not automatically to an incomplete picture of the real network.

Quick formula reference

For revision once you understand the methods above. Check the conditions before using a formula.

Tree edge count

\(|E_{\mathrm{tree}}|=n-1\).

Return to the explanation →

Distance relaxation

\(d(v)\leftarrow\min\{d(v),d(u)+w(u,v)\}\).

Return to the explanation →

Learn to diagnose an error

Spot the mistake

The statement below is incorrect. Identify the missing condition or reasoning before opening the explanation.

Incorrect reasoning

A minimum spanning tree contains the shortest path between every pair of vertices.

Explain and repair the mistake

For a triangle with AB=2, BC=2 and AC=3, the minimum tree uses AB and BC, total 4. Its A-to-C route costs 4, while the direct original edge costs 3. Tree optimality concerns total network weight, not every pairwise journey.

03

Connect and apply

Put the ideas together

A more demanding application of this guide’s methods. This is a learning stage; any additional HL content is labelled separately.

Worked application

A cheaper last edge can still lose

A destination can be reached through U with prefix cost 9 and final edge 1, or through V with prefix cost 3 and final edge 5. Which is shorter?

  1. The route through U costs 9+1=10.
  2. The route through V costs 3+5=8.
  3. The larger final edge still gives a smaller complete route cost.
  4. Dijkstra compares cumulative costs and would retain the route through V among these candidates.
Pause and explain

What does this show about choosing the smallest incident edge at the destination?

Check your reasoning

That local edge choice ignores the cost of reaching its other endpoint. A shortest-path decision must compare complete source-to-destination costs, not one edge in isolation.

Common mistakes and how to avoid them

  • For a triangle with AB=2, BC=2 and AC=3, the minimum tree uses AB and BC, total 4. Its A-to-C route costs 4, while the direct original edge costs 3. Tree optimality concerns total network weight, not every pairwise journey.
  • Do not mistake a minimum spanning tree for a shortest-route network.
  • A negative edge removes the usual guarantee; use a suitable alternative or the method specified by the task.
04

Practise independently

Build confidence, then apply it

Try each question before opening its hint or solution. If a method is new, return to the walkthrough, then try again. These learning stages describe the practice sequence; additional HL content is labelled separately.

  1. 1

    Build confidence

    HL extension

    Find the shortest path from A to C.

    Need a starting point?

    Compare the direct edge with the route through B.

    Show the worked solution
    1. The direct route costs 8; A-B-C costs 5.
    2. The shortest path is A-B-C with weight 5.
  2. 2

    Apply and explain

    HL extension

    Can Dijkstra be used without qualification on a network with negative edge weights?

    Need a starting point?

    Recall the assumption behind fixing a current best distance.

    Show the worked solution
    1. No: the usual Dijkstra algorithm requires nonnegative edge weights.
    2. A later negative edge can invalidate an already fixed distance.
  3. 3

    Check your understanding

    How many edges are in a tree spanning seven vertices?

    Show the worked solution

    Six. The selected edges must also connect every vertex and contain no cycle; merely choosing six arbitrary edges does not establish that the result is a spanning tree.

  4. 4

    Use the method

    In Kruskal, why may the third-smallest edge need to be rejected?

    Show the worked solution

    It may form a cycle among already connected vertices. The algorithm selects the cheapest eligible edge, not blindly the first n−1 edges in sorted order; the chosen forest must keep gaining useful connections.

Finished? Use the linked question bank below to practise selecting the method without a guide telling you which rule to use.

Using these methods in exam questions

AI Paper 1

GDC required

Short-response questions still need mathematical setup and interpretation. Choose the correct tool, enter the model or data carefully, and check units and rounding.

AI Paper 2

GDC required

Extended-response questions connect several steps. Keep intermediate values at full precision and explain how each result answers the context.

For this topic: Sketch and label the relevant triangle or curve. Show which angle and radius a formula uses, check degree or radian mode, and give length, area and volume in the correct units.

HL only · GDC required

Paper 3: build a connected argument

Both AA HL and AI HL Paper 3 contain two extended problem-solving questions. A question develops a theme, so keep useful earlier results, test small cases and explain any proposed general rule. A calculator pattern can suggest a result; your mathematical reasoning must support it.

  1. Identify what the first part establishes and record the conditions under which it holds.
  2. When a later part introduces a new definition or model, connect it to methods you already know rather than assuming a new formula must have been memorised.
  3. Check the conclusion against an earlier numerical case and explain its meaning or limitation.

For the first-assessment-2021 courses. IB assessment reference ↗

Find a specific method or formula

Now practise the method

If you opened this guide from a question, return to that tab and try the method without looking at the example. Your answer stays in the question tab.

Practise this subtopic →

Want a broader practice session? Explore IB Maths AI HL practice questions →Try free IB Maths exam-style questions and worked solutions →

Original Arc Practice teaching notes for Maths AI. Core and HL sections are distinguished above. These notes follow the first-assessment-2021 course and are not an official IB publication or a complete syllabus checklist. IB course reference. Report a correction.