

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Operation costs, invariants and worked traces for a first course in algorithms Computer Science | Undergraduate (Level 5) | December 2026
WHAT IS INSIDE
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.
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 and Graphs
operation mix before choosing.
3. Trees: traversal, search trees and heaps
Binary trees are traversed in-order (left, node, right), pre-order (node, left, right) and post-order (left, right, node); in-order traversal of a binary search tree yields sorted order, which is the property most exam traces rely on. A binary search tree gives O(h) operations where h is height - O(log n) when balanced and O(n) when the insertions arrive in sorted order, which is the standard motivation for balancing.
AVL trees keep the height difference of every node at most one through rotations, and red-black trees relax this to a colour invariant that allows faster insertion; both guarantee O(log n) operations. A binary heap is a complete tree stored in an array where the parent of index i is at (i-1)/2 and the children at 2i+1 and 2i+2; sift-up on insert and sift-down on extract-min give O(log n), and build-heap from an arbitrary array is O(n) rather than O(n log n).
4. Hashing and graphs
A hash table maps keys to buckets through a hash function, resolving collisions by chaining or by open addressing with linear or quadratic probing. Load factor drives performance: chaining degrades gracefully, open addressing degrades sharply and requires resizing. A good hash function distributes keys uniformly and is cheap to compute; when keys are untrusted, a randomised or keyed hash prevents collision attacks.
Graphs are represented as adjacency lists (space O(V+E), good for sparse graphs) or adjacency matrices (space O(V squared), O(1) edge lookup, good for dense graphs). Breadth-first search explores by distance and finds shortest paths in unweighted graphs in O(V+E); depth-first search produces discovery and finish times and detects cycles. Dijkstra's algorithm finds shortest paths with non-negative weights in O((V+E) log V) with a heap; Bellman-Ford handles negative weights in O(VE) and detects negative cycles; topological ordering is possible exactly when the graph is a directed acyclic graph.
Operation Array Linked list Hash table Balanced BST Binary heap Access by index O(1) O(n) n/a n/a n/a Search by key O(n) O(n) O(1) avg O(log n) O(n) Insert O(n) O(1) at head O(1) avg O(log n) O(log n) Delete O(n) O(1) given node O(1) avg O(log n) O(log n) Min / max O(n) O(n) n/a O(log n) O(1) Ordered traversal O(n log n) sort O(n log n) sort no order O(n) in-order no order
Exam question patterns
Three shapes dominate. Trace the algorithm : show the state after each step in a table, then state the final result and the complexity - a trace without a complexity statement is half an answer. Prove or justify : state the invariant, show it holds at initialisation, during each iteration and at termination. Design and compare : give the data structure, the operations with their costs, the trade-off, and one case where the alternative would be worse.
Use complexity notation precisely: worst case, average case and space complexity are separate answers, and O(1) is not the same as 'fast'. Write code that reads as pseudocode unless the question