Docsity

Data Structures and Algorithms: Complexity, Trees, Hashing and Graphs, Study notes of Computer Science

Toggle sidebar
bg1
Data Structures and Algorithms: Complexity, Trees, Hashing a Page 1 of 3
Data Structures and Algorithms: Complexity, Trees,
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
pf3

Partial preview of the text

Download Data Structures and Algorithms: Complexity, Trees, Hashing and Graphs and more Study notes Computer Science in PDF only on Docsity!

Data Structures and Algorithms: Complexity, Trees,

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 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