Quick revision
One exam-ready summary for every lesson, module by module, plus the Module 4 complexity sheet. Read it the night before; tap a title to open the full lesson.
Module 1 · Basic Concepts, Stacks and Queues
Data structures: definitions and classification
- Data structure = organisation of data + allowed operations.
- Primitive vs non-primitive; linear (array, stack, queue, list) vs non-linear (tree, graph); static vs dynamic.
- Operations: traverse, search, insert, delete, sort, merge.
Data abstraction and ADTs
- ADT = objects + operations, specified independently of representation.
- Specification: function name, arguments, result — in words.
- Algorithm properties: Finiteness, Input, Definiteness, Effectiveness, Output (FIDEO).
Performance analysis: time and space complexity
- S(P) = c + Sp(I): fixed part + instance part.
- Step count table: s/e × frequency, then add.
- +1 loops → n; ×2 or ÷2 loops → log n; nested → multiply.
- Best / worst / average case; worst case is the usual answer.
Asymptotic notations
- O: f ≤ c·g after n₀ (ceiling). Ω: f ≥ c·g after n₀ (floor). Θ: c₁g ≤ f ≤ c₂g (sandwich).
- Always state c and n₀.
- Polynomial of degree m → Θ(nᵐ).
- Order: 1 < log n < n < n log n < n² < n³ < 2ⁿ < n!
Polynomial representation using arrays
- Store non-zero terms as (coef, expon) in decreasing exponent order.
- Add: bigger exponent first; equal → add coefficients; skip a zero sum.
- Copy leftovers. O(m + n).
Sparse matrix — tuple representation
- Triples <row, col, value>, row-major; a[0] = <rows, cols, terms>.
- Transpose: for each column c, copy matching terms with row/col swapped → O(cols·terms).
- Fast transpose: rowTerms + startingPos → O(cols + terms).
Array storage: row-major and column-major
- Row-major (C): B + (i·n + j)·w.
- Column-major (Fortran): B + (j·m + i)·w.
- Subtract lower bounds if indices do not start at 0.
- The last element has the same address in both layouts.
Stack ADT and array implementation
- LIFO; one end (top). Empty: top = −1. Full: top = MAX − 1.
- Push: check full → top++ → stack[top] = x.
- Pop: check empty → item = stack[top] → top-- → return item.
- All operations O(1).
Multiple stacks in one array
- Two stacks grow towards each other: top1 = −1, top2 = MAX.
- Full when top1 + 1 = top2.
- n stacks: boundary[] and top[]; full when top[i] = boundary[i+1]; shift to make room.
Queue ADT (linear queue)
- FIFO: insert at rear, delete at front.
- Empty: front = −1. Full (linear): rear = MAX − 1.
- Insert: check full → (first? front = rear = 0 : rear++) → q[rear] = x.
- Delete: check empty → item = q[front] → (only one? reset to −1 : front++).
- Weakness: space before front is wasted → circular queue.
Circular queues
- Wrap with (i + 1) mod MAX.
- Empty: front = −1. Full: (rear + 1) mod MAX = front.
- Count = (rear − front + MAX) mod MAX + 1.
- O(1) operations, no wasted space.
Double-ended queues
- Four operations: insertFront, insertRear, deleteFront, deleteRear.
- Front moves back with (front − 1 + MAX) mod MAX; rear moves back with (rear − 1 + MAX) mod MAX.
- Input-restricted: insert at one end only. Output-restricted: delete at one end only.
- All O(1).
Infix to postfix conversion
- Operands → output; ‘(’ → push; ‘)’ → pop until ‘(’.
- Operator: pop while top has higher precedence (or equal and left-assoc), then push.
- End: pop everything.
- Show the Symbol | Stack | Postfix table — it earns the marks.
- O(n).
Evaluating postfix expressions
- Operand → push. Operator → b = pop, a = pop, push a op b.
- Answer = the single value left.
- Show a Symbol | Action | Stack table.
- O(n).
Module 2 · Linked List and Memory Management
Singly linked lists and their operations
- Node = data + link; self-referential struct.
- Front insert: new→link = head; head = new. O(1).
- End insert: walk to the last node; last→link = new. O(n).
- Delete: keep prev; prev→link = p→link; free(p).
- Arrays: fast access. Lists: fast insert/delete and flexible size.
Stacks and queues using linked lists
- Stack: push = insert at head, pop = delete head.
- Queue: insert at rear, delete at front; reset rear when the queue becomes empty.
- Overflow only when malloc fails. All O(1).
Polynomials using linked lists
- Node: coef, expon, link; terms in decreasing exponent order.
- Compare exponents: bigger → copy; equal → add (skip if 0); copy leftovers.
- O(m + n).
Doubly linked lists
- Node: prev, data, next.
- Front insert: new→next = head; head→prev = new; head = new.
- Reverse display: go to the last node, follow prev.
- Delete a node in O(1) given its address.
Circular linked lists
- last→link = first.
- Front: new→link = last→link; last→link = new.
- End: same, then last = new.
- Traverse with do-while until back at first.
Memory allocation: first-fit, best-fit, worst-fit
- First-fit: first hole big enough. Best-fit: smallest big-enough hole. Worst-fit: largest hole.
- Leftover stays free; free() coalesces neighbours.
- Draw a table: request × scheme, show the leftover after every step, and state which schemes succeed.
Garbage collection and compaction
- Garbage = allocated but unreachable memory.
- Mark & sweep: mark from roots → sweep unmarked to the free list.
- Reference counting fails on cycles.
- Compaction: move live blocks together → one big hole; update pointers.
Module 3 · Trees and Graphs
Trees: terminology and representation
- Root, parent, child, sibling, leaf, degree (node / tree), level, height, ancestor, forest.
- List form: (A (B (E, F), C, D (…))).
- k-ary nodes waste n(k − 1) + 1 links.
- Left-child right-sibling → binary tree.
Binary trees: types and properties
- Binary tree: empty, or root + left subtree + right subtree.
- Full (Horowitz) = 2^k − 1 nodes; complete = filled left to right; skewed = one child per node.
- 2^(i−1) per level; 2^k − 1 total; n₀ = n₂ + 1.
Binary tree representation
- Array (1-based): children 2i, 2i + 1; parent ⌊i/2⌋.
- Linked: left, data, right; n + 1 NULL links.
- Arrays suit complete trees (heaps); links suit everything else.
Tree operations and traversals
- Pre NLR, In LNR, Post LRN, Level order with a queue.
- Recursive 4-line algorithms; O(n).
- Inorder of a BST = sorted order.
Expression trees
- Leaves = operands, internal nodes = operators.
- Root = the operator applied last.
- In/pre/post-order → infix/prefix/postfix.
- From postfix: stack of subtrees, pop right first.
Binary search trees
- Left < node < right.
- Search/insert: compare and go left or right. O(h).
- Delete: leaf → remove; one child → bypass; two children → copy the inorder successor, then delete it.
- Inorder traversal = sorted keys.
Binary heaps and priority queues
- Complete tree + heap order.
- Insert: put at the end, bubble up. Delete: last → root, sift down.
- Heapify from ⌊n/2⌋ − 1 down to 0: O(n).
- Priority queue = the ADT; heap = the usual implementation.
Graph definitions
- G = (V, E); directed vs undirected; weighted.
- Degree, path, simple path, cycle, connected, strongly connected, component.
- Σ degrees = 2|E|; complete graph has n(n − 1)/2 edges.
Graph representation
- Matrix: n × n, a[i][j] = 1 (or weight); symmetric if undirected; O(n²) space.
- List: array of linked lists; O(n + e) space.
- Dense → matrix; sparse → list.
Depth-first and breadth-first search
- BFS: queue, level by level; mark on enqueue.
- DFS: recursion/stack, go deep, backtrack.
- Ties: smallest vertex first (as the question says).
- O(n + e) with lists, O(n²) with a matrix.
Single source all destinations (Dijkstra)
- Greedy: lock the closest unvisited vertex, relax its edges.
- dist[v] = min(dist[v], dist[u] + w).
- Non-negative weights only.
- Show a table with one row per locked vertex, then list the paths.
Module 4 · Sorting and Searching
Selection sort
- Select minimum of a[i..n−1], swap into a[i].
- n(n−1)/2 comparisons → O(n²) in all cases; ≤ n−1 swaps.
- In-place, not stable.
Insertion sort
- Invariant: a[0..i−1] sorted.
- Best O(n), average/worst O(n²); space O(1).
- Stable, in-place, adaptive, online.
Quick sort
- Partition → pivot final → recurse on both sides.
- Best/average O(n log n), worst O(n²); in-place, unstable.
- State the pivot rule you use.
Merge sort
- Divide (split at mid), conquer (sort halves), combine (merge).
- O(n log n) all cases; O(n) extra space; stable.
Heap sort
- Build max-heap (O(n)), then n−1 × (swap root to end + heapify).
- O(n log n) all cases, in-place, unstable.
Radix sort
- d passes, units → tens → hundreds.
- Distribute into 10 FIFO buckets, collect 0 → 9.
- O(d(n + 10)), stable, not comparison-based.
Linear search
Sequential scan; O(n) worst/average, O(1) best; no precondition.
Binary search
Sorted array required; O(log n); at most ⌊log₂ n⌋ + 1 comparisons.
Hashing and hash functions
- Division: k mod m (m prime).
- Mid-square: middle r digits of k².
- Folding: add r-digit parts (shift/boundary).
- Digit analysis: keep the well-distributed digit positions of a known key set.
Collision resolution: linear probing, quadratic probing, double hashing
- Linear (h+i), quadratic (h+i²), double (h+i·h₂) — all mod m.
- Clustering: primary (linear), secondary (quadratic), minimal (double).
- Delete with tombstones.
Open hashing (separate chaining)
Array of linked lists; average O(1 + α); easy deletion; worst case O(n).
Module 4 complexity sheet
| Algorithm | Best | Average | Worst | Extra space | Stable | In-place |
|---|---|---|---|---|---|---|
| Selection sort | O(n²) | O(n²) | O(n²) | O(1) | No | Yes |
| Insertion sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Quick sort | O(n log n) | O(n log n) | O(n²) | O(log n) stack avg | No | Yes |
| Merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Heap sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | Yes |
| Radix sort (d digits, base r) | O(d(n+r)) | O(d(n+r)) | O(d(n+r)) | O(n+r) | Yes | No |
| Linear search | O(1) | O(n) | O(n) | O(1) | — | — |
| Binary search | O(1) | O(log n) | O(log n) | O(1) | — | — |
| Bubble sort (enrichment, with flag) | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |