📄 Paper 1 · 4.3 Algorithms
⭐ Pro
4.3.6 Dijkstra's Shortest Path Algorithm
AQA 7517 · A-Level Computer Science · ~20 min read

Dijkstra's Shortest Path Algorithm

Dijkstra's algorithm finds the shortest path from a single source vertex to all other vertices in a weighted graph with non-negative edge weights. It uses a greedy approach.

Algorithm

// Dijkstra's algorithm
1. Assign distance 0 to source, ∞ to all others
2. Add all vertices to unvisited set
3. WHILE unvisited set is not empty:
   a. Select vertex U with smallest tentative distance
   b. Mark U as visited (remove from unvisited)
   c. FOR each unvisited neighbour V of U:
        newDist ← dist[U] + weight(U,V)
        IF newDist < dist[V] THEN
          dist[V] ← newDist
          prev[V] ← U          // Record previous node
        END IF
4. Return dist[] and prev[]

Worked Trace

// Graph (undirected, weighted):
// A-B:4, A-C:2, B-C:1, B-D:5, C-D:8, C-E:10, D-E:2

// Source: A
// Initial: dist={A:0, B:∞, C:∞, D:∞, E:∞}

Step 1: Visit A (dist=0)
   → B: 0+4=4  → update dist[B]=4, prev[B]=A
   → C: 0+2=2  → update dist[C]=2, prev[C]=A
   dist={A:0, B:4, C:2, D:∞, E:∞}

Step 2: Visit C (smallest unvisited dist=2)
   → B: 2+1=3 < 4 → update dist[B]=3, prev[B]=C
   → D: 2+8=10 → update dist[D]=10, prev[D]=C
   → E: 2+10=12 → update dist[E]=12, prev[E]=C
   dist={A:0, B:3, C:2✓, D:10, E:12}

Step 3: Visit B (smallest unvisited dist=3)
   → D: 3+5=8 < 10 → update dist[D]=8, prev[D]=B
   dist={A:0, B:3✓, C:2✓, D:8, E:12}

Step 4: Visit D (smallest unvisited dist=8)
   → E: 8+2=10 < 12 → update dist[E]=10, prev[E]=D
   dist={A:0, B:3✓, C:2✓, D:8✓, E:10}

Step 5: Visit E (dist=10) — done

// Shortest paths from A:
// A→B: 3 (via C) | A→C: 2 | A→D: 8 (via C→B→D) | A→E: 10 (via C→B→D→E)

Priority Queue Optimisation

Using a min-heap (priority queue) to always extract the vertex with smallest distance efficiently, the time complexity is O((V + E) log V) where V = vertices and E = edges.

Limitations

  • Does not work with negative edge weights (use Bellman-Ford for that)
  • Only finds shortest path from one source

Applications

  • GPS navigation (shortest route)
  • Network routing protocols (e.g. OSPF)
  • Social networks (degrees of separation)
  • Games (pathfinding for NPCs)
Exam tip: AQA expects you to trace Dijkstra's step by step using a table. Show: current node, updated distances, and which node was visited at each step. Remember: always select the unvisited vertex with the smallest current distance. Dijkstra's uses a greedy approach — once a vertex is visited, its shortest distance is finalised. It does NOT work with negative weights.
▶
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.3.6 Dijkstra's Algorithm

8 questions · instantly marked · AQA 7517 standard

Q1What does Dijkstra's algorithm find and what type of graph does it work on?[2]
✅ Mark scheme
Mark scheme
Dijkstra's finds the shortest path from a single source vertex to all other vertices [1]; it works on weighted graphs with non-negative edge weights [1].
Q2At the start of Dijkstra's algorithm, what distance is assigned to (a) the source vertex and (b) all other vertices?[2]
✅ Mark scheme
Mark scheme
(a) Source vertex: 0 [1]; (b) All other vertices: infinity (∞) [1].
Q3In each iteration of Dijkstra's, which vertex is selected next?[1]
✅ Mark scheme
Mark scheme
The unvisited vertex with the smallest (minimum) tentative distance [1].
Q4Trace Dijkstra's algorithm on the graph: A-B:6, A-C:3, B-D:2, C-B:1, C-D:5. Source: A. Show a step-by-step table.[5]
✅ Mark scheme
Mark scheme
Init: A=0,B=∞,C=∞,D=∞ [1]; Visit A: B=6(A), C=3(A) [1]; Visit C (min=3): B=min(6,3+1)=4(C), D=min(∞,3+5)=8(C) [1]; Visit B (min=4): D=min(8,4+2)=6(B) [1]; Visit D (min=6) done. Shortest: A→D=6 via A→C→B→D [1].
Q5Why does Dijkstra's algorithm NOT work with negative edge weights?[2]
✅ Mark scheme
Mark scheme
Dijkstra's greedy approach assumes that once a vertex is visited, its shortest path is finalised [1]; a negative edge discovered later could provide a shorter path via an already-visited vertex, invalidating this assumption [1].
Q6What is the purpose of the 'prev' (or 'parent') array in Dijkstra's algorithm?[2]
✅ Mark scheme
Mark scheme
The prev array stores the previous vertex on the shortest path to each vertex [1]; by tracing back through prev from the destination to the source, the actual shortest path can be reconstructed [1].
Q7Give two real-world applications of Dijkstra's algorithm.[2]
✅ Mark scheme
Mark scheme
Any two from: GPS/satnav route planning [1]; network routing protocols (OSPF) [1]; social network degrees of separation; game NPC pathfinding.
Q8Dijkstra's algorithm is described as 'greedy'. What does this mean in this context?[2]
✅ Mark scheme
Mark scheme
A greedy algorithm makes the locally optimal choice at each step [1]; Dijkstra's always selects the unvisited vertex with the smallest known distance — the locally optimal choice — without reconsidering visited vertices [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Dijkstra's

10 questions · 10 minutes

← 4.3.5 Sorting
24 of 70 · AQA 7517
4.4.1a Problem Solving →