Revision Notes
OxfordAQA A-Level Computer Science (9645): Graph algorithms – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.11.1: breadth-first and depth-first search, their uses, Dijkstra's algorithm, trace methods and a self-test.
- Subject
- Computer Science
- Level
- A LEVEL
- Topic
- Topic 11 – Advanced algorithms
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA A Level Computer Science (9645), 2024-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA A Level Computer Science.
Syllabus points this page covers
9645 (A Level)
- 3.11.1 Graph algorithms
This page covers 1 of the 2 subtopics in topic 11, Advanced algorithms.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (9645).
These notes condense sub-topic 3.11.1 Graph algorithms of OxfordAQA International AS and A-level Computer Science (9645), Version 1.1 (A-level content in topic 3.11 Advanced algorithms). For explanations, code and full traces, see the graph algorithms study guide; then practise with the graph algorithms practice questions. Code is Python.
What the specification requires
- Trace breadth-first and depth-first search, and describe typical applications. Breadth-first: shortest path for an unweighted graph. Depth-first: navigating a maze.
- Implement breadth-first and depth-first search in program code.
- Understand and trace Dijkstra’s shortest path algorithm. You will not be expected to recall its steps or write code for it unless it is given on the paper.
- Be aware of applications of shortest path algorithms.
Key terms
| Term | Meaning |
|---|---|
| Traversal | Visiting each reachable vertex of a graph once |
| Visited | A vertex already processed; remembered so it is not processed again |
| BFS | Visits layer by layer from the start, using a queue |
| DFS | Goes as deep as possible, then backtracks, using a stack |
| Backtrack | Return to the previous vertex when there is nowhere new to go |
| Shortest path | The path of lowest total weight (or fewest edges if unweighted) |
| Distance | In Dijkstra’s algorithm, the best total weight found so far from the start |
Breadth-first search (queue)
- Mark the start visited and enqueue it.
- While the queue is not empty: dequeue a vertex; for each unvisited neighbour, mark it visited and enqueue it.
def bfs(g, start):
visited = [start]
queue = deque([start])
while queue:
v = queue.popleft()
for n in g[v]:
if n not in visited:
visited.append(n)
queue.append(n)
return visited
Depth-first search (stack or recursion)
def dfs(g, v, visited=None):
if visited is None:
visited = []
visited.append(v)
for n in g[v]:
if n not in visited:
dfs(g, n, visited)
return visited
With an explicit stack: pop a vertex; if it is unvisited, mark it and push its unvisited neighbours (in reverse order to visit the first neighbour first).
Worked reminder: both searches from P
Graph: P: Q, R; Q: P, S, T; R: P, T; S: Q, U; T: Q, R, U; U: S, T.
| Vertex removed | Newly visited | Queue afterwards |
|---|---|---|
| (start) | P | P |
| P | Q, R | Q, R |
| Q | S, T | R, S, T |
| R | none | S, T |
| S | U | T, U |
| T | none | U |
| U | none | empty |
BFS order: P, Q, R, S, T, U. DFS order (neighbours in alphabetical order): P, then Q, then S, then U, then T (from U), then R (from T). DFS order: P, Q, S, U, T, R.
Uses
| Algorithm | Typical use | Why |
|---|---|---|
| BFS | Shortest path in an unweighted graph | It reaches vertices in order of the number of edges from the start |
| DFS | Navigating a maze | It follows one passage to the end and backtracks from dead ends |
| Dijkstra | Satnav, network routing, journey planning | It finds the lowest total weight in a weighted graph |
Dijkstra’s algorithm: trace method
- Start vertex distance 0; all others infinity.
- Choose the unfinished vertex with the smallest distance; mark it finished.
- For each unfinished neighbour, compare the current distance with (this vertex’s distance + edge weight). If smaller, update it and record the route.
- Repeat until all are finished.
Worked reminder. Edges: S-A 3, S-B 6, A-B 2, A-C 4, B-C 1, B-D 7, C-D 3; start S.
| Finished | S | A | B | C | D |
|---|---|---|---|---|---|
| start | 0 | ∞ | ∞ | ∞ | ∞ |
| S | 0 | 3 | 6 | ∞ | ∞ |
| A (3) | 0 | 3 | 5 | 7 | ∞ |
| B (5) | 0 | 3 | 5 | 6 | 12 |
| C (6) | 0 | 3 | 5 | 6 | 9 |
| D (9) | 0 | 3 | 5 | 6 | 9 |
Route to D: S, A, B, C, D, with cost 3 + 2 + 1 + 3 = 9. The route is found by following the “came from” links back from D.
DFS trace in words (graph from P)
- Visit P. Its first neighbour is Q: visit Q.
- Q’s neighbours are P (visited), S and T. Go to S.
- S’s neighbours are Q (visited) and U. Go to U.
- U’s neighbours are S (visited) and T. Go to T.
- T’s neighbours are Q and U (visited) and R. Go to R.
- R’s neighbours P and T are both visited. Backtrack through T, U, S and Q to P.
- P has no unvisited neighbours left. Finished.
Order: P, Q, S, U, T, R. Compare with BFS, which visits R second because it is a direct neighbour of P.
Explicit stack version of DFS
def dfs_stack(g, start):
visited = []
stack = [start]
while stack:
v = stack.pop()
if v not in visited:
visited.append(v)
for n in reversed(g[v]):
if n not in visited:
stack.append(n)
return visited
The neighbours are pushed in reverse so that the first neighbour is popped first. The test after pop is needed because a vertex can be pushed more than once before it is visited.
BFS against DFS at a glance
| BFS | DFS | |
|---|---|---|
| Structure | Queue (first in, first out) | Stack (last in, first out) or recursion |
| Visit order | By distance from the start, in layers | Deep along one path first |
| Finds fewest-edge path? | Yes (unweighted graph) | Not guaranteed |
| Memory pattern | Holds a whole layer at once | Holds the current path |
Choosing the right algorithm
- Fewest edges between two vertices (unweighted): BFS.
- Lowest total weight between two vertices (weighted): Dijkstra.
- Explore every route, such as in a maze: DFS.
- Is every vertex reachable from the start? Either BFS or DFS, then check that all vertices were visited.
Exam traps
- Wrong structure. BFS uses a queue and DFS uses a stack.
- Visiting twice. Mark vertices as visited when they are added (BFS) or popped (DFS).
- Order of neighbours. Use the order the question gives.
- BFS on a weighted graph. It counts edges, not weights, so it does not find the cheapest path.
- Dijkstra: wrong choice. Always pick the smallest distance among the unfinished vertices.
- Missing update. Replace a distance only if the new total is smaller.
- Distance without route. Give both if asked.
Quick self-test
- Which data structure does BFS use?
- Which data structure does DFS use?
- What does DFS do at a dead end?
- Give one use of BFS.
- Give one use of DFS.
- Why must visited vertices be recorded?
- In the worked example, what is the BFS order from P?
- What is the DFS order from P?
- What does Dijkstra’s algorithm find?
- In the Dijkstra table, why did B change from 6 to 5?
- What is the shortest route from S to D in the example?
- Give one application of a shortest path algorithm.
Answers
- A queue.
- A stack (or recursion, which uses the call stack).
- It backtracks to the previous vertex and tries another neighbour.
- Finding the shortest path (fewest edges) in an unweighted graph.
- Navigating a maze (or exploring all paths).
- A graph may contain cycles, so without a record the search could loop forever or process a vertex repeatedly.
- P, Q, R, S, T, U.
- P, Q, S, U, T, R.
- The shortest (lowest total weight) path from a start vertex to every other vertex.
- Through A the cost is 3 + 2 = 5, which is less than the direct edge of 6.
- S, A, B, C, D (cost 9).
- Satellite navigation, network routing or journey planning.
Official syllabus
OxfordAQA International AS and A-level Computer Science (9645) specification, Version 1.1, for International AS exams May/June 2025 onwards and International A-level exams May/June 2026 onwards: sub-topic 3.11.1 Graph algorithms.
Related resources
-
Study guides
OxfordAQA A-Level Computer Science (9645): Tree algorithms
OxfordAQA 9645 sub-topic 3.11.2 in depth: tracing and coding pre-order, in-order and post-order traversal, with array trees, call traces and uses.
Computer Science · OxfordAQA · A Level
Read guide -
Practice questions
OxfordAQA A-Level Computer Science (9645): Tree algorithms – Practice Questions
Ten original OxfordAQA 9645 questions on sub-topic 3.11.2 Tree algorithms: traversal traces, array trees, expression trees, code and uses, with answers.
Computer Science · OxfordAQA · A Level
Try questions -
Revision notes
OxfordAQA A-Level Computer Science (9645): Tree algorithms – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.11.2 Tree algorithms: the three traversals, code, array trees, uses, common errors and a self-test.
Computer Science · OxfordAQA · 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.