How does Dijkstra's shortest path algorithm work?
Dijkstra's algorithm finds the shortest path from one start node to every other node in a weighted graph. It gives the start node distance 0 and all others infinity, then repeatedly visits the unvisited node with the smallest distance. It updates each neighbour's distance if going through that node is shorter. This is AQA A Level Computer Science (7517) content. Example: Edges A-B 4, A-C 2, C-B 1. Visit A: B = 4, C = 2. Visit C: B = 2 + 1 = 3. Answer: shortest A to B is 3, via C.
Quick answer: Dijkstra's algorithm finds the shortest path from one start node to every other node in a weighted graph with no negative weights. It gives the start node a distance of 0 and every other node infinity. It then repeatedly visits the unvisited node with the smallest distance and updates its neighbours if a shorter route is found. This is A Level only content in AQA Computer Science (7517).
What the specification expects
This topic is in section 4.3.6.1 Dijkstra's shortest path algorithm of the AQA AS and A-level Computer Science specification (7517). It is A level only. The specification states:
- Understand and be able to trace Dijkstra's shortest path algorithm.
- Be aware of applications of shortest path algorithm.
- Students will not be expected to recall the steps in Dijkstra's shortest path algorithm.
Method
- Give the start node distance 0 and every other node distance infinity. Mark all nodes unvisited.
- Choose the unvisited node with the smallest distance and mark it visited.
- For each unvisited neighbour, calculate distance of current node plus edge weight. If it is smaller than the neighbour's current distance, replace it and record the current node as its previous node.
- Repeat steps 2 and 3 until all nodes are visited (or the target is visited).
- Read the route by following the previous nodes back from the target to the start.
Use this graph in the examples. Edge weights: A-B 4, A-C 2, B-C 1, B-D 5, C-D 8, C-E 10, D-E 3.
Worked example 1: Dijkstra's algorithm on a small graph
Trace Dijkstra's algorithm from A and find the shortest distance to every node.
- Start: A = 0, B, C, D, E = infinity.
- Visit A (0): B = 4, C = 2.
- Visit C (2): B via C = 2 + 1 = 3, which is less than 4, so B = 3. D = 2 + 8 = 10. E = 2 + 10 = 12.
- Visit B (3): D via B = 3 + 5 = 8, less than 10, so D = 8.
- Visit D (8): E via D = 8 + 3 = 11, less than 12, so E = 11.
- Visit E (11). All nodes are visited.
Answer: A = 0, C = 2, B = 3, D = 8, E = 11.
Worked example 2: reading the route
Using the trace above, give the shortest route from A to E.
- E's previous node is D.
- D's previous node is B.
- B's previous node is C.
- C's previous node is A.
Answer: A, C, B, D, E with a total distance of 11.
Worked example 3: why a direct edge is not always shortest
Edge A-B has weight 4. Why is it not part of the shortest route from A to B?
- The route A to C to B costs 2 + 1 = 3.
- 3 is less than 4, so the distance for B is updated when C is visited.
Answer: The shortest route from A to B is A, C, B with distance 3, not the direct edge of 4.
Worked example 4: applications
Give one application of a shortest path algorithm.
- Think of a network where edges have a cost such as distance or time.
- A route planner treats junctions as nodes and roads as weighted edges.
Answer: A satnav finds the quickest road route between two places using a shortest path algorithm.
Common mistakes
- Choosing the neighbour with the smallest edge weight instead of the unvisited node with the smallest total distance.
- Forgetting to update a distance when a shorter route is found later.
- Not recording the previous node, so the route cannot be read off.
- Using Dijkstra's algorithm with negative edge weights, which it does not handle.
FAQs
Do I need to memorise the steps?
The AQA specification says students will not be expected to recall the steps, but you must be able to trace the algorithm.
Does it work on unweighted graphs?
It does, but breadth-first search also finds the shortest path in an unweighted graph.
Want one-to-one help? Find an online Computer Science tutor with ExpertTutor.
Want one-to-one support?
One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.
Find a TutorLearn face to face in Glasgow
Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.
See the centre