Hashing and Graphs
Operation costs, invariants and worked traces for a first course in algorithms
Computer Science | Undergraduate (Level 5) | December 2026
WHAT IS INSIDE
1. Measuring cost: asymptotics and what they hide
2. Linear structures: arrays, lists, stacks and queues
3. Trees: traversal, search trees and heaps
4. Hashing and graphs
Exam question patterns
Glossary of exam-ready definitions
1. Measuring cost: asymptotics and what they hide
Big O describes an upper bound on growth, Omega a lower bound and Theta a tight bound. The
standard orderings - O(1), O(log n), O(n), O(n log n), O(n squared), O(2 to the n) - are the vocabulary
the exam expects, and each must be attached to a specific operation rather than to a data structure as a
whole: a hash table gives O(1) average lookup and O(n) worst case, and saying only 'O(1)' is
incomplete.
Asymptotics hide constants, memory layout and cache behaviour, which is why an array can beat a
linked list in practice even where the asymptotic costs are equal. Amortised analysis is a separate idea:
appending to a dynamic array is O(1) amortised although individual appends can be O(n), and
doubling capacity is what produces that result. Recurrence relations for divide-and-conquer algorithms
are solved by the master theorem, and the standard results - merge sort and heap sort at O(n log n),
quicksort at O(n log n) average and O(n squared) worst - should be known with their justification.
• Space complexity - count auxiliary space, not the input; in-place algorithms are O(1) auxiliary.
• Stability - a stable sort preserves the relative order of equal keys; merge sort is stable, heap sort and
quicksort are not.
• Comparison lower bound - comparison sorts cannot beat Omega(n log n), which is why counting
and radix sort trade space for time.
• Adversarial inputs - a hash table degenerates to O(n) if every key collides, which is why randomised
hashing exists.
2. Linear structures: arrays, lists, stacks and queues
An array gives O(1) random access and O(n) insertion or deletion in the middle, because elements after
the position must shift. A linked list reverses that trade: O(1) insertion at a known node, but O(n)
search and poor locality. Doubly linked lists add O(1) deletion given a node, at the cost of an extra
pointer per element; circular lists remove the need to test for the end.
A stack is last-in-first-out and underpins function calls, expression evaluation and depth-first search; a
queue is first-in-first-out and underpins breadth-first search and scheduling. A deque supports both
ends in O(1), and a priority queue - implemented as a binary heap - supports insert and extract-min in
O(log n). Choosing between them is a question about which operations are frequent, so state the
Data Structures and Algorithms: Complexity, Trees, Hashing a Page 1 of 3