Revision Notes
AQA A-Level Computer Science (7517): Optimisation algorithms – Revision Notes
Condensed AQA 7517 section 4.3.6 recall: Dijkstra's shortest path algorithm, how to lay out a trace, reading the route and its applications, with a self-test.
- Subject
- Computer Science
- Level
- A LEVELS
- Topic
- Fundamentals of algorithms
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to AQA A Level Computer Science (7517), 2015-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): AQA A Level Computer Science.
Syllabus points this page covers
7517
- 4.3.6 Optimisation algorithms
This page covers 1 of the 6 subtopics in topic 3, Fundamentals of algorithms.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (7517).
Condensed recall for 4.3.6 Optimisation algorithms, AQA A-Level Computer Science (7517): understand and trace Dijkstra’s shortest path algorithm and be aware of applications of shortest path algorithms. You are not expected to recall the steps; the exam will give them. A full worked trace is in the optimisation algorithms study guide; exam-style questions are in the optimisation algorithms practice questions.
The problem
Given a weighted graph (each edge has a cost), find the route from a start vertex to each other vertex with the lowest total weight. This is an optimisation problem.
- Unweighted graph: breadth-first search gives the fewest edges.
- Weighted graph: fewest edges is not necessarily cheapest, so use Dijkstra.
- Weights must be zero or positive.
The algorithm in plain steps
- Give the start vertex distance 0 and every other vertex infinity. No vertex is visited. No vertex has a previous vertex.
- Choose the unvisited vertex with the smallest distance. Mark it visited; its distance is now final.
- For each unvisited neighbour: calculate distance of the chosen vertex + edge weight. If this is smaller than the neighbour’s current distance, replace it and record the chosen vertex as the neighbour’s previous vertex.
- Repeat from step 2 until every vertex (or the target) is visited.
- To find a route, follow previous pointers back from the target to the start, then reverse the list.
Layout for a trace
| Step | Visited | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|
| start | none | 0 | ∞ | ∞ | ∞ | ∞ | ∞ |
| 1 | A | 0 | 4 | 2 | ∞ | ∞ | ∞ |
| 2 | C | 0 | 3 | 2 | 10 | 12 | ∞ |
| 3 | B | 0 | 3 | 2 | 8 | 12 | ∞ |
| 4 | D | 0 | 3 | 2 | 8 | 10 | 14 |
| 5 | E | 0 | 3 | 2 | 8 | 10 | 13 |
| 6 | F | 0 | 3 | 2 | 8 | 10 | 13 |
This is for the graph with edges A-B 4, A-C 2, B-C 1, B-D 5, C-D 8, C-E 10, D-E 2, D-F 6, E-F 3. The shortest distance from A to F is 13. The previous vertices are F from E, E from D, D from B, B from C, C from A, so the route is A, C, B, D, E, F.
Why it beats the fewest-edges route: A, B, D, F has only three edges but costs 4 + 5 + 6 = 15.
Rules to follow while tracing
- Pick the next vertex by smallest tentative distance, not by name or position.
- Only update a distance if the new total is strictly smaller.
- Never update a visited vertex.
- Add the edge weight to the visited vertex’s distance, not to the neighbour’s.
- If two vertices tie, use the rule in the question (for example alphabetical order). The final distances do not change.
- Write the arithmetic, for example
3 + 5 = 8.
Why choosing the smallest distance is safe
Any other route to that vertex must pass through a vertex that is at least as far from the start. Because no weight is negative, going further cannot make the total smaller. So the smallest tentative distance is already the true shortest distance. With a negative weight this argument fails.
Applications
| Application | Vertices | Weights |
|---|---|---|
| Satellite navigation | Junctions | Distance or journey time |
| Network routing | Routers | Delay, cost or link speed |
| Robot or game movement | Map positions | Terrain cost or distance |
| Travel planning | Stations or airports | Fare or travel time |
| Delivery planning | Depots and customers | Distance or cost |
Always say what the weights represent and that the algorithm finds the route with the lowest total weight.
What the algorithm gives you
- The shortest distance from the start to every other vertex in one run.
- The route to each vertex, if previous vertices are recorded.
- It can stop early once the target has been visited.
Exam traps
- Wrong vertex chosen next: by name, not by smallest distance.
- Wrong sum: adding the weight to the neighbour’s distance.
- Updating visited vertices.
- Distance without route, or route without distance: give both if asked.
- Counting edges instead of totalling weights.
- Using BFS for a weighted graph and claiming it is shortest.
Key terms
| Term | Definition |
|---|---|
| Weighted graph | A graph in which each edge has a numerical cost |
| Weight | The cost attached to an edge, such as distance or time |
| Shortest path | The route with the smallest total weight |
| Tentative distance | The best distance found so far for a vertex |
| Previous vertex | The vertex from which the best known route reaches a vertex |
| Optimisation | Finding the best solution among many possible solutions |
| Dijkstra’s algorithm | An algorithm that finds the shortest path from one vertex to all others in a graph with non-negative weights |
Self-test
- What does Dijkstra’s algorithm find?
- What distance does the start vertex begin with?
- What distance does every other vertex begin with?
- How is the next vertex to visit chosen?
- What condition must be true before a distance is updated?
- Why can Dijkstra’s algorithm fail with a negative weight?
- Edges: S-A 3, S-B 5, A-B 1. What is the shortest distance from S to B, and by what route?
- Why is breadth-first search not suitable for finding the cheapest route in a weighted graph?
- Name two applications of shortest path algorithms.
- How do you recover the route once the algorithm has finished?
Answers
- The shortest (lowest total weight) path from a start vertex to every other vertex.
-
- Infinity.
- It is the unvisited vertex with the smallest tentative distance.
- The new distance (visited vertex’s distance plus the edge weight) must be smaller than the current distance.
- A negative edge could make a longer-looking route cheaper later, after a vertex’s distance has already been fixed.
- 4, by S to A to B (3 + 1 = 4, which is less than the direct 5).
- It counts edges and ignores their weights.
- Any two: satellite navigation; network routing; robot or game path finding; travel or delivery planning.
- Follow the previous-vertex records back from the target to the start and reverse the list.
More resources on this topic
Related resources
-
Study guides
AQA A-Level Computer Science (7517): Reverse Polish
Deeper guide to AQA 7517 section 4.3.3 Reverse Polish: converting simple infix expressions to Reverse Polish notation and back, evaluating with a stack, and why it is used.
Computer Science · AQA · A Level
Read guide -
Practice questions
AQA A-Level Computer Science (7517): Reverse Polish – Practice Questions
Original AQA 7517 section 4.3.3 questions with answers: converting between infix and Reverse Polish notation and evaluating with a stack.
Computer Science · AQA · A Level
Try questions -
Revision notes
AQA A-Level Computer Science (7517): Reverse Polish – Revision Notes
Condensed AQA 7517 section 4.3.3 recall: infix to Reverse Polish conversion both ways, evaluating with a stack, why RPN is used and a self-test.
Computer Science · AQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.