Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Advanced

Dijkstra's Shortest Path Algorithm

4.3.6.1 Dijkstra's shortest path algorithm

Aligned to the AQA 7517 specification

Level
Advanced
Reading time
7 min
Published
6 June 2026
Updated
1 July 2026
On this page
  1. 1.The Shortest Path Problem
  2. 2.How Dijkstra's Algorithm Works
  3. 3.Setting Up the Trace
  4. 4.Step-by-Step Trace
  5. 5.Reading the Result: Reconstructing the Shortest Path
  6. 6.Applications and Limitations
  7. 7.Common Exam Mistakes

Key takeaways

  • Dijkstra's algorithm solves the single-source shortest path problem on weighted graphs with non-negative edge weights, finding the shortest path from one source to every other vertex.
  • Each step selects the unvisited vertex with the smallest tentative distance, relaxes its unvisited neighbours, then marks it permanently visited so its distance is confirmed.
  • The greedy property holds only because edge weights are non-negative: once a vertex is permanently visited, no shorter path to it can be found later.
  • The shortest path is reconstructed by working backwards through the predecessor (via) column from the destination to the source.
  • Dijkstra works on unweighted graphs but is unnecessarily slow; BFS achieves the same result in O(V + E) time, and negative edge weights require Bellman-Ford, which is not required by AQA 7517.

The Shortest Path Problem

Breadth-first search finds the shortest path by edge count in an unweighted graph. When edges carry different weights — distances, costs, travel times — the path with the fewest edges is not necessarily the cheapest or fastest.

Example: given three routes from A to D:

  • A → D directly: 1 edge, weight 20
  • A → B → D: 2 edges, total weight 14
  • A → C → D: 2 edges, total weight 5

BFS minimises edge count, so it returns A → D (1 edge — the fewest). But A → D costs 20. The genuinely shortest path by total weight is A → C → D at cost 5 — a 2-edge route that BFS would not select, because BFS does not consider edge weights at all. Dijkstra's algorithm solves this by tracking accumulated cost rather than hop count.

Dijkstra's algorithm solves the single-source shortest path problem on weighted graphs with non-negative edge weights. Given one source vertex, it finds the shortest path from that source to every other vertex in the graph.

In AQA exams, you will not be asked to recall the steps of Dijkstra's algorithm from memory. You will be given a partially completed working table and asked to continue or complete the trace. Understanding the logic of each step — not rote recall — is what the exam tests.

How Dijkstra's Algorithm Works

The algorithm maintains a tentative distance for each vertex: the shortest distance found so far from the source. Initially, only the source has a known distance (zero); all others are set to infinity.

At each step, the algorithm:

  1. Selects the unvisited vertex with the smallest current tentative distance — call it the current vertex
  2. For each unvisited neighbour of the current vertex: calculates the distance from the source via the current vertex. If this is smaller than the neighbour's recorded tentative distance, updates it
  3. Marks the current vertex as permanently visited — its recorded distance is now confirmed as the shortest possible

The algorithm terminates when all vertices have been permanently visited (or when the destination vertex is confirmed, if only one shortest path is needed).

Why this works — the greedy property: Once a vertex is marked permanently visited, no shorter path to it can be discovered later. This holds as long as all edge weights are non-negative. A vertex with a tentative distance of 5 cannot be reached more cheaply via a vertex with tentative distance 8, because all edges from that vertex add further non-negative weight.

Setting Up the Trace

The worked trace uses this weighted undirected graph:

EdgeWeight
A–B4
A–C2
B–C1
B–D5
C–D8
C–E10
D–E2

Goal: find the shortest path from A to every other vertex.

Initial state:

VertexABCDE
Tentative distance0
Visited

The source vertex A starts with distance 0. All others begin at infinity — meaning no path is yet known.

Tracking predecessors: alongside each tentative distance, record which vertex the update came from. This allows the shortest path to be reconstructed at the end by working backwards.

Step-by-Step Trace

Iteration 1 — Current: A (distance 0)

Unvisited neighbours of A: B (via A = 0+4 = 4), C (via A = 0+2 = 2). Update both. Mark A visited.

Iteration 2 — Current: C (distance 2, smallest unvisited)

Unvisited neighbours of C: B (via C = 2+1 = 3 < 4 → update), D (via C = 2+8 = 10), E (via C = 2+10 = 12). Mark C visited.

Iteration 3 — Current: B (distance 3, smallest unvisited)

Unvisited neighbours of B: D (via B = 3+5 = 8 < 10 → update). Mark B visited.

Iteration 4 — Current: D (distance 8, smallest unvisited)

Unvisited neighbours of D: E (via D = 8+2 = 10 < 12 → update). Mark D visited.

Iteration 5 — Current: E (distance 10, smallest unvisited)

No unvisited neighbours. Mark E visited. All vertices visited — algorithm terminates.

After iterationABCDE
Initial0
Visit A4 (via A)2 (via A)
Visit C3 (via C)10 (via C)12 (via C)
Visit B8 (via B)12
Visit D10 (via D)
Visit E

Studying this for an exam?

Generate a personalised learning path for this subject. Free to get started.

Create a learning path

Reading the Result: Reconstructing the Shortest Path

The final shortest distances from A: B = 3, C = 2, D = 8, E = 10.

To find the actual path to a destination, work backwards through the predecessor chain:

Shortest path from A to E (distance 10):

  • E was updated via D → predecessor of E is D
  • D was updated (in iteration 3) via B → predecessor of D is B
  • B was updated (in iteration 2) via C → predecessor of B is C
  • C was updated (in iteration 1) via A → predecessor of C is A

Path: A → C → B → D → E (total weight: 2+1+5+2 = 10 ✓)

Check that no alternative path is shorter:

  • A → B → D → E = 4+5+2 = 11
  • A → C → E = 2+10 = 12
  • A → C → D → E = 2+8+2 = 12

The algorithm found the optimal route.

Applications and Limitations

Applications of Dijkstra's algorithm:

DomainUse
GPS navigationFinding the fastest route between locations on a road network
Network routingRouting data packets via the lowest-latency path (e.g., OSPF protocol)
LogisticsMinimising delivery distances across a distribution network
RoboticsPathfinding for autonomous movement around obstacles

Limitations:

Dijkstra's algorithm only works with non-negative edge weights. If any edge has a negative weight, the greedy guarantee breaks: a vertex marked as permanently visited could later be reached more cheaply via a path that includes negative-weight edges. A different algorithm (Bellman-Ford) is needed for negative weights, but this is not required by AQA 7517.

Additionally, Dijkstra finds shortest paths in a weighted graph. If all edge weights are equal, BFS achieves the same result more efficiently.

Common Exam Mistakes

1. Selecting the wrong current vertex

At each step, select the unvisited vertex with the smallest current tentative distance. A common error is selecting the vertex with the smallest edge weight from the most recently visited vertex instead. Always scan all unvisited vertices and pick the globally smallest tentative distance.

2. Updating already-visited vertices

Once a vertex is marked permanently visited, its distance is final and must not be changed. Only update tentative distances for unvisited neighbours. Revisiting confirmed vertices produces incorrect results.

3. Forgetting to compare before updating

A tentative distance should only be updated if the new path is strictly shorter than the current recorded value. If the new distance equals or exceeds the existing tentative distance, make no change.

4. Misreading the predecessor chain

When reconstructing the path, work backwards from the destination using the predecessor (via) column. Students who try to trace forwards from the source often pick up incorrect branches. Write the predecessor of each vertex as you complete each row of the trace table.

5. Applying Dijkstra to unweighted graphs

Dijkstra works correctly on unweighted graphs but is unnecessarily slow. BFS achieves the same result in time. If an exam question gives an unweighted graph, the expected algorithm is BFS, not Dijkstra.

Key terms

Dijkstra's algorithm
A greedy algorithm that solves the single-source shortest path problem on weighted graphs with non-negative edge weights.
Single-source shortest path
The problem of finding the shortest path from one given source vertex to every other vertex in the graph.
Tentative distance
The shortest distance found so far from the source to a vertex; initially zero for the source and infinity for all others.
Current vertex
At each step, the unvisited vertex with the smallest tentative distance, whose neighbours are then examined.
Permanently visited
A vertex whose recorded shortest distance has been confirmed as the smallest possible and must not be changed again.
Greedy property
The principle that once a vertex is permanently visited, no shorter path to it can be discovered later, which holds only for non-negative edge weights.
Predecessor
The vertex an update came from, recorded alongside each tentative distance so the shortest path can be reconstructed backwards.

Frequently asked questions

Because the greedy guarantee depends on it: once a vertex is marked permanently visited, no shorter path to it can be discovered later only as long as all edges add further non-negative weight. With a negative edge a confirmed vertex could later be reached more cheaply, breaking the guarantee, so Bellman-Ford is needed instead (not required by AQA 7517).

Work backwards from the destination through the predecessor (via) column, which records which vertex each tentative distance came from, until you reach the source. Tracing forwards from the source often picks up incorrect branches, so always reconstruct backwards.

No. AQA exams do not ask you to recall the steps from memory. You are given a partially completed working table and asked to continue or complete the trace, so the exam tests your understanding of the logic of each step rather than rote recall.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Sorting Algorithms

Next

Abstraction and Automation

Related lessons

5 min

Lesson

Graphs

A-Level Computer Science · AQA 7517

1 month ago

8 min

Lesson

Binary Search Trees

A-Level Computer Science · AQA 7517

1 month ago

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.