EuraStudy
An algorithm is a precise, finite sequence of steps that solves a problem, and this chapter builds the standard toolkit every computer scientist must know by heart: traversing graphs and trees, searching for a value, sorting a list, and finding the shortest path with Dijkstra's algorithm. For each you must be able to state what it does, trace it step by step on given data, and reason about how its running time grows with the size of the input.
5 sections~18 min reading time3 competenciesLevel Standard 2 · Advanced 3
basic level
AS-Level expects linear and binary search, bubble sort, and the idea of a tree traversal.
higher level
The full A-Level adds depth-first and breadth-first graph traversal, pre/in/post-order tree traversal and expression trees, merge sort, and Dijkstra's shortest-path algorithm, with reasoning about their complexity.
Reading depth: In depth
Text size: Standard
A graph for traversal
Trace a breadth-first traversal of the graph (edges A-B, A-C, B-D, C-D, D-E) from A, visiting neighbours alphabetically. Show the queue after each vertex is visited.
Mark A visited. Enqueue its neighbours B, C. Queue: [B, C].
Dequeue B (front). Its unvisited neighbour is D; enqueue it. Queue: [C, D].
Dequeue C. Its neighbour D is already queued/visited, so nothing new. Queue: [D].
Dequeue D; enqueue its unvisited neighbour E. Queue: [E]. Dequeue E; no new neighbours. Queue empty.
Result: Visit order A, B, C, D, E. BFS reaches E in the fewest hops (A-B-D-E or A-C-D-E, three edges) - the shortest unweighted path.
Typical mistakes
Active revision
For the graph with edges A-B, A-C, B-D, C-D, D-E, give the breadth-first and the depth-first visit order starting at A, visiting neighbours in alphabetical order, and show the queue (BFS) and stack (DFS) after visiting A.
Active recall
Recall the key points — then reveal.
Sources: AQA A-level Computer Science 7517 specification (AQA)
An expression tree for (3 + 4) x 5
Evaluate the Reverse Polish expression 3 4 + 5 x using a stack. Show the stack after each token.
Push 3, then push 4. Stack (base to top): [3, 4].
Pop 4 and 3, compute , push the result. Stack: [7].
Push the operand 5. Stack: [7, 5].
Pop 5 and 7, compute , push it. Stack: [35].
Result: The single value left on the stack, 35, is the answer. RPN evaluation needs no brackets - the stack enforces the order that the tree's shape recorded.
Typical mistakes
Active revision
Draw the expression tree for , give its Reverse Polish form by a post-order traversal, and evaluate that RPN expression using a stack, showing the stack after each token.
Active recall
Recall the key points — then reveal.
Sources: AQA A-level Computer Science 7517 specification (AQA)
Comparing search growth rates
Midpoint index
The integer midpoint of the current search bounds; binary search compares the target with the element here.
Why binary search is O(log n)
Halving elements times leaves at most ; the search ends when that reaches 1, after about steps.
Search the sorted list [3, 7, 11, 15, 19, 23, 27, 31] (indices 0-7) for 23 by binary search. Give low, high, mid and the comparison at each step.
low = 0, high = 7, mid = . Element at 3 is 15. , so search the right half: low = 4.
low = 4, high = 7, mid = . Element at 5 is 23. - found.
Result: Found at index 5 in 2 comparisons. Linear search would have taken 6 comparisons to reach the same element; the advantage grows with list size.
Typical mistakes
Active revision
The sorted list is [3, 7, 11, 15, 19, 23, 27, 31]. Trace a binary search for the value 23, giving low, high, mid and the comparison at each step, and state the number of comparisons made.
Active recall
Recall the key points — then reveal.
Sources: GCE AS and A level subject content for computer science (Department for Education)
Merge sort: the divide phase
Sorting complexities
Bubble sort makes about comparisons; merge sort halves times with work per level.
Perform one full left-to-right pass of bubble sort on [5, 1, 4, 2], comparing adjacent pairs and swapping if the left is larger. Show each comparison.
, so swap. List becomes [1, 5, 4, 2].
, so swap. List becomes [1, 4, 5, 2].
, so swap. List becomes [1, 4, 2, 5].
Result: After one pass the largest value 5 has bubbled to the end: [1, 4, 2, 5]. The next pass ignores the final element and continues until a pass makes no swaps.
Typical mistakes
Active revision
Trace one full pass of bubble sort on [5, 1, 4, 2], showing each comparison and swap and the list after the pass. Then show how merge sort splits and merges [38, 27, 43, 3] to sort it.
Active recall
Recall the key points — then reveal.
Sources: AQA A-level Computer Science 7517 specification (AQA)
A weighted graph for Dijkstra's algorithm
The relaxation step
When a shorter route to is found through the just-settled , the tentative distance to is lowered.
The distance table trace
Using the weighted graph (source A), find the shortest distance and path from A to C.
dist(A) = 0. Relax A's edges: dist(B) = 6, dist(D) = 1. Closest unsettled is D.
dist(D) = 1. Relax: B via D = (improves 6 to 3); E via D = . Predecessor of B and E is D.
dist(B) = 3 (tie with E; settle either). Relax: C via B = ; E stays 3. Predecessor of C is B.
dist(E) = 3 relaxes C via E to (no improvement). Finally settle C at 8.
Result: Shortest distance A to C is 8. Following predecessors C <- B <- D <- A gives the shortest path A - D - B - C.
Typical mistakes
Active revision
Using the weighted graph opposite (source A), trace Dijkstra's algorithm as a distance table, give the shortest distance from A to C, and state the shortest path itself using the predecessors.
Active recall
Recall the key points — then reveal.
Sources: AQA A-level Computer Science 7517 specification (AQA)
References & sources
Department for Education