Skip to main content
EnglandComputer ScienceSyllabus dot point

How does Dijkstra's algorithm find the shortest path in a weighted graph?

Understand Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex, the role of a priority queue, and its applications.

A focused answer to AQA A-Level Computer Science 4.3.4, covering Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex in a weighted graph, the role of a priority queue, and its applications.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. What Dijkstra's algorithm does
  3. How it works
  4. Applications

What this dot point is asking

AQA wants you to describe Dijkstra's shortest path algorithm, explain how it finds the lowest-cost path from a start vertex through a weighted graph, identify the role of a priority queue, and give applications, including tracing the algorithm on a small graph.

What Dijkstra's algorithm does

How it works

The key insight is that once the closest unvisited vertex is chosen, no later route can be shorter, because every remaining route would have to pass through a vertex that is already at least as far away. This is why a finalised vertex is never reconsidered, and it is also why negative weights break the algorithm. The predecessor records let you reconstruct the actual path by working back from the destination to the source.

Applications

Dijkstra's algorithm underpins satnav and mapping route-finding (weights are distance or travel time), network routing protocols that find least-cost paths for data packets, transport planning, and any problem of finding the cheapest or fastest route through a weighted network.

In each application the abstract graph maps onto something concrete: in a satnav the vertices are junctions and the edge weights are road lengths or expected travel times, so the lowest-cost path is the fastest route; in a network the vertices are routers and the weights are link costs, so the least-cost path is where packets should be forwarded. Because the weights can model anything additive and non-negative, the same algorithm solves a wide family of problems just by relabelling what a weight means.

It is worth contrasting Dijkstra with the unweighted breadth-first search, since the specification covers both. If every edge had weight 1, Dijkstra would visit vertices in exactly the same order as breadth-first search, because the closest vertex is always the one fewest edges away. Dijkstra generalises this to arbitrary non-negative weights by always finalising the cheapest-so-far vertex rather than the nearest in edge count. The reason negative weights break it is that finalising a vertex assumes no cheaper route can appear later, but a negative edge encountered afterwards could undercut a distance already treated as final, so the guarantee fails. For graphs with negative weights a different algorithm is needed, which is why the non-negative condition is always stated.

Exam-style practice questions

Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AQA 20195 marksA weighted graph has vertices A, B, C and D. The edges and weights are A to B = 1, A to C = 4, B to C = 2, B to D = 5, C to D = 1. Use Dijkstra's algorithm from A to find the shortest distance to D, showing the tentative distances as they are updated.
Show worked answer β†’

Initialise distances: A = 0, B = ∞\infty, C = ∞\infty, D = ∞\infty.

Visit A (distance 0): relax A to B = 1, A to C = 4. Distances: B = 1, C = 4, D = ∞\infty.

Visit B (smallest unvisited, distance 1): relax B to C gives 1+2=3<41 + 2 = 3 < 4, update C = 3; B to D gives 1+5=61 + 5 = 6, update D = 6.

Visit C (distance 3): relax C to D gives 3+1=4<63 + 1 = 4 < 6, update D = 4.

Visit D (distance 4): no further updates. Shortest distance A to D = 4, via A, B, C, D.

Markers reward correct initialisation, choosing the smallest unvisited vertex each step, each relaxation that updates a shorter distance, and the final answer of 4.

AQA 20223 marksExplain the role of the priority queue in Dijkstra's algorithm and state one real-world application of the algorithm.
Show worked answer β†’

At each step Dijkstra's algorithm must select the unvisited vertex with the smallest tentative distance, because that vertex's shortest distance is now final and can be used to relax its neighbours. A priority queue stores the unvisited vertices ordered by tentative distance so that this minimum can be retrieved efficiently rather than searching the whole set each time, which improves performance on large graphs.

A real-world application is satnav or mapping route-finding, where road segments have weights such as distance or travel time and the algorithm finds the least-cost route. Network routing is another valid example.

Markers reward explaining that the queue lets the minimum-distance vertex be selected efficiently and a valid application.

Related dot points

Sources & how we know this