TIMED REVISION · 1-MINUTE PANIC SHEETS

Algorithms 10-Minute Rapid Revision

For the last few minutes before the exam hall: one core rule per topic, the formulas you must remember, and the traps that cost marks.

PANIC SHEET 01 · Asymptotic Notations & Recurrences

Asymptotic Bounds, Order of Growth & Growth Hierarchy

Full Lesson →
CORE INVARIANT ANCHOR:

“f(n) = Theta(g(n)) iff 0 < lim_{n -> inf} [f(n) / g(n)] < inf. If ratio is 0 => f(n) = o(g(n)). If ratio is inf => f(n) = omega(g(n)).”

Must-Remember Principles:
  • —Big-O (Upper Bound): 0 <= f(n) <= c * g(n) for all n >= n0.
  • —Big-Omega (Lower Bound): 0 <= c * g(n) <= f(n) for all n >= n0.
  • —Big-Theta (Tight Bound): 0 <= c1 * g(n) <= f(n) <= c2 * g(n) for all n >= n0.
  • —Little-o (Strict Upper Bound): lim_{n -> inf} [f(n) / g(n)] = 0.
  • —Little-omega (Strict Lower Bound): lim_{n -> inf} [f(n) / g(n)] = inf.
  • —Growth Hierarchy: 1 < log(log n) < log n < (log n)^k < n^epsilon < sqrt(n) < n < n log n < n^2 < n^k < 2^(sqrt(n)) < 2^n < 3^n < n! < n^n.
  • —Stirling's Formula: n! approx sqrt(2 * pi * n) * (n / e)^n => log(n!) = Theta(n log n).
Critical Exam Pitfalls:
  • —f(n) = O(g(n)) DOES NOT imply 2^{f(n)} = O(2^{g(n)}). Counterexample: f(n) = 2n, g(n) = n. 2^{2n} = 4^n != O(2^n).
  • —Little-o and Little-omega are NOT reflexive: f(n) != o(f(n)) and f(n) != omega(f(n)).
  • —log(n!) = Theta(n log n), but n! = o(n^n) because (n/e)^n grows strictly slower than n^n.
PANIC SHEET 02 · Asymptotic Notations & Recurrences

Master Theorem, Recurrences & Advanced Recurrence Solvers

Full Lesson →
CORE INVARIANT ANCHOR:

“For T(n) = a*T(n/b) + f(n), compare f(n) with the critical exponent n^{log_b a}.”

Must-Remember Principles:
  • —Case 1 (Leaves Dominate): If f(n) = O(n^{log_b a - epsilon}) for epsilon > 0 => T(n) = Theta(n^{log_b a}).
  • —Case 2 (Even Balance): If f(n) = Theta(n^{log_b a} * log^k n) for k >= 0 => T(n) = Theta(n^{log_b a} * log^{k+1} n).
  • —Case 2 Extended: If f(n) = Theta(n^{log_b a} / log n) (k = -1) => T(n) = Theta(n^{log_b a} * log(log n)).
  • —Case 3 (Root Dominates): If f(n) = Omega(n^{log_b a + epsilon}) AND regularity condition a*f(n/b) <= c*f(n) for c < 1 => T(n) = Theta(f(n)).
  • —Square Root Recurrences: T(n) = 2T(sqrt(n)) + 1 => let n = 2^m => S(m) = 2S(m/2) + 1 => Theta(log n).
Critical Exam Pitfalls:
  • —Master Theorem FAILS when: a is not constant, b <= 1, or difference between f(n) and n^{log_b a} is not polynomial (e.g., T(n) = 2T(n/2) + n/log n cannot use Case 1 or 3).
  • —In Case 3, regularity condition MUST be verified; without it, Master Theorem does not apply.
PANIC SHEET 03 · Sorting, Selection & Priority Queues

Merge Sort, Quick Sort & Linear-Time Selection

Full Lesson →
CORE INVARIANT ANCHOR:

“Merge Sort: Always Theta(n log n), stable, O(n) space. Quick Sort: O(n log n) expected, O(n^2) worst case, in-place, unstable.”

Must-Remember Principles:
  • —Merge Sort Inversion Counting: Number of pairs (i, j) with i < j and A[i] > A[j] computed in O(n log n) during merge step.
  • —Quick Sort Worst Case: Occurs when partition is maximally unbalanced (0 : n-1 split at every level), yielding T(n) = T(n-1) + Theta(n) = O(n^2).
  • —Randomized Quick Sort: Expected comparisons E[C] = 2n ln n + O(n) = Theta(n log n) for ANY input array.
  • —QuickSelect (Hoare): Average time O(n), worst case O(n^2).
  • —Median-of-Medians (BFPRT): Groups of 5 guarantee worst-case T(n) <= T(n/5) + T(7n/10) + O(n) = O(n). Groups of 3 fail (yield O(n log n)).
Critical Exam Pitfalls:
  • —Lomuto partition uses the last element as pivot and makes more swaps than Hoare partition; Hoare uses two converging pointers.
  • —Even an extreme 99:1 unbalanced split at every level still yields Theta(n log n) running time because recursion tree depth is log_{100/99} n = O(log n).
PANIC SHEET 04 · Sorting, Selection & Priority Queues

Sorting Bounds, Non-Comparison Sorting & Binary Heaps

Full Lesson →
CORE INVARIANT ANCHOR:

“Any comparison sort requires Omega(n log n) worst-case comparisons. Non-comparison sorts (Counting, Radix) achieve O(n) by using key values as indices.”

Must-Remember Principles:
  • —Decision Tree Lower Bound: Height h >= ceil(log2(n!)) = Omega(n log n).
  • —Counting Sort: Time Theta(n + k), space Theta(n + k). Stable when traversed right-to-left.
  • —Radix Sort: Time Theta(d * (n + b)) where d is number of digits in base b. Requires stable sub-sort.
  • —Bucket Sort: Expected time O(n) when input is uniformly distributed over [0, 1).
  • —Binary Max-Heap: Complete binary tree in array. Parent = floor(i/2), Left = 2i, Right = 2i+1.
  • —Build-Max-Heap: Runs in strictly O(n) linear time, NOT O(n log n), because sum_{h=0}^inf (h / 2^h) = 2.
  • —Heap Sort: In-place, O(n log n) best/average/worst, unstable.
Critical Exam Pitfalls:
  • —Build-Heap is O(n), but inserting n elements one-by-one into an initially empty heap takes O(n log n).
  • —Radix Sort is NOT comparison-based; it does not violate the Omega(n log n) lower bound.
PANIC SHEET 05 · Greedy Strategies & Dynamic Programming

Greedy Choice, Huffman Coding & Interval Scheduling

Full Lesson →
CORE INVARIANT ANCHOR:

“Greedy algorithms make locally optimal choices. Correctness requires proving both the Greedy Choice Property (via exchange argument) and Optimal Substructure.”

Must-Remember Principles:
  • —Activity Selection: Sort by finish time f_i in ascending order. Pick next compatible activity. Time O(n log n).
  • —Fractional Knapsack: Sort items by value density (v_i / w_i) in descending order. Greedy achieves optimal in O(n log n).
  • —Huffman Tree Structure: For n alphabet symbols, Huffman generates a strictly full binary tree with n leaves and n - 1 internal nodes (total 2n - 1 nodes).
  • —Average Huffman Length: L = sum_{i=1}^n f_i * length(c_i).
  • —Optimality of Huffman: No prefix-free binary code can achieve smaller expected length than Huffman code.
Critical Exam Pitfalls:
  • —Greedy strategy FAILS for 0/1 Knapsack because items cannot be divided; taking the highest v_i / w_i item can preclude taking two items of greater combined value.
  • —Huffman tree is NOT unique (ties in frequency produce different tree shapes), but the optimal expected bit length is strictly invariant.
PANIC SHEET 06 · Greedy Strategies & Dynamic Programming

Minimum Spanning Trees: Kruskal, Prim & Cut Properties

Full Lesson →
CORE INVARIANT ANCHOR:

“Cut Property: The strictly light edge crossing any cut belongs to every MST. Cycle Property: The strictly heaviest edge in any cycle cannot belong to any MST.”

Must-Remember Principles:
  • —MST Weight Uniqueness: If all edge weights in G are distinct, G has a UNIQUE minimum spanning tree.
  • —Kruskal's Algorithm: Sort edges O(E log E) + Disjoint Set Union (union-by-rank, path compression) O(E * alpha(V)). Overall O(E log V).
  • —Prim's Algorithm: Grows a single tree. Time O(V^2) with matrix, O((V + E) log V) with binary heap, O(E + V log V) with Fibonacci heap.
  • —Inverse Ackermann Function: alpha(V) <= 4 for all practical universe sizes.
Critical Exam Pitfalls:
  • —Distinct edge weights guarantee a unique MST, but unique MST DOES NOT require distinct edge weights.
  • —Decreasing the weight of an edge ALREADY in the MST leaves the MST unchanged (weights decrease, but edges remain).
  • —Increasing the weight of an edge NOT in the MST leaves the MST unchanged.
PANIC SHEET 07 · Greedy Strategies & Dynamic Programming

Dynamic Programming: 0/1 Knapsack, LCS & LIS

Full Lesson →
CORE INVARIANT ANCHOR:

“0/1 Knapsack: DP[i][w] = max(DP[i-1][w], val[i] + DP[i-1][w - wt[i]]). Time O(n*W) is Pseudo-Polynomial.”

Must-Remember Principles:
  • —0/1 Knapsack Space Optimization: 1D array DP[w] filled right-to-left (from W down to wt[i]) achieves O(W) space.
  • —LCS Recurrence: If X[i] == Y[j] => 1 + LCS[i-1][j-1]; Else max(LCS[i-1][j], LCS[i][j-1]). Time O(m*n).
  • —Longest Increasing Subsequence (LIS): O(n^2) via standard DP; O(n log n) via Patience Sorting (tails array with binary search lower_bound).
  • —Pseudo-Polynomial Time: O(n*W) is polynomial in numerical value of W, but EXPONENTIAL in input length log(W) bits.
Critical Exam Pitfalls:
  • —In 1D space-optimized 0/1 Knapsack, you MUST traverse capacity from W down to wt[i]. Traversing left-to-right solves the Unbounded Knapsack problem instead!
  • —LCS table has size (m + 1) x (n + 1); base cases are row 0 and column 0 initialized to 0.
PANIC SHEET 08 · Greedy Strategies & Dynamic Programming

Matrix Chain Multiplication & Optimization Problems

Full Lesson →
CORE INVARIANT ANCHOR:

“Matrix Chain: M[i, j] = min_{i <= k < j} { M[i, k] + M[k+1, j] + p_{i-1}*p_k*p_j }. Time O(n^3), Space O(n^2).”

Must-Remember Principles:
  • —Scalar Multiplications for A(p x q) and B(q x r): Cost is p * q * r multiplications.
  • —Number of Parenthesizations: Catalan number C_{n-1} = (1 / n) * binom(2n - 2, n - 1) = Omega(4^n / n^{1.5}).
  • —DP on DAGs: Shortest / Longest paths in a DAG can be solved in linear time O(V + E) by evaluating vertices in Topological Order.
  • —Held-Karp Traveling Salesperson: Solved via DP with bitmask in O(n^2 * 2^n) time.
Critical Exam Pitfalls:
  • —In matrix chain dimension vector p = [p0, p1, ..., pn], matrix A_i has dimension p_{i-1} x p_i (1-indexed).
  • —The base case M[i, i] = 0 (a single matrix requires 0 multiplications).
PANIC SHEET 09 · Graph Traversals & Shortest Paths

Graph Traversals, Topological Sort & Strongly Connected Components

Full Lesson →
CORE INVARIANT ANCHOR:

“A directed graph has a cycle iff DFS discovers a BACK EDGE. A DAG can be topologically sorted in reverse order of DFS finish times.”

Must-Remember Principles:
  • —BFS Time Complexity: Theta(V + E) with adjacency list. Finds shortest paths in unweighted graphs.
  • —Parenthesis Theorem: For any u, v in DFS, intervals [d[u], f[u]] and [d[v], f[v]] are either completely disjoint or one contains the other.
  • —Edge Classification (Directed): Tree Edge (unvisited), Back Edge (ancestor on stack: d[v] < d[u] < f[u] < f[v]), Forward Edge (descendant: d[u] < d[v] < f[v] < f[u]), Cross Edge (d[v] < f[v] < d[u] < f[u]).
  • —Undirected DFS Edges: Only TREE and BACK edges exist (no forward or cross edges).
  • —Topological Sort: Valid ONLY on DAGs. Computed in O(V + E) via DFS finish times or Kahn's in-degree queue.
  • —Kosaraju SCC: 1. DFS on G (record finish times); 2. Transpose G to G^T; 3. DFS on G^T in decreasing finish time order. Time O(V + E).
Critical Exam Pitfalls:
  • —In undirected graphs, an edge leading directly to the immediate parent vertex on the recursion stack is NOT a back edge; it is the tree edge traversed in reverse.
  • —If Kahn's algorithm terminates with vertices still having in-degree > 0, the graph contains a cycle.
PANIC SHEET 10 · Graph Traversals & Shortest Paths

Shortest Paths: Dijkstra, Bellman-Ford, Floyd-Warshall & Johnson

Full Lesson →
CORE INVARIANT ANCHOR:

“Dijkstra: O((V+E) log V), non-negative weights only. Bellman-Ford: O(V*E), handles negative weights and detects negative cycles. Floyd-Warshall: O(V^3) APSP.”

Must-Remember Principles:
  • —Dijkstra Greedy Invariant: When vertex u is extracted from min-heap, dist[u] = delta(s, u) is permanently finalized.
  • —Why Dijkstra Fails on Negative Edges: Negative edges violate greedy choice; adding constant C to all edges fails because paths with more edges are penalized.
  • —Bellman-Ford Relaxation: Relaxes all E edges V - 1 times. A V-th iteration that strictly decreases any distance proves a negative cycle reachable from s.
  • —Floyd-Warshall DP: D^{(k)}[i, j] = min(D^{(k-1)}[i, j], D^{(k-1)}[i, k] + D^{(k-1)}[k, j]). Negative cycle iff D[i, i] < 0.
  • —Johnson's Algorithm: Reweights edges using Bellman-Ford potentials h(v) so all w'(u, v) >= 0. Runs Dijkstra V times. Time O(V^2 log V + V*E).
Critical Exam Pitfalls:
  • —Dijkstra DOES NOT work with negative weights even if there are no negative cycles.
  • —Bellman-Ford detects negative cycles ONLY if they are reachable from the chosen source vertex s.
PANIC SHEET 11 · Computational Complexity & NP-Completeness

Complexity Classes P, NP, NP-Complete & Reductions

Full Lesson →
CORE INVARIANT ANCHOR:

“Language L is NP-Complete iff: 1. L in NP, and 2. L is NP-Hard (every problem in NP reduces to L in polynomial time).”

Must-Remember Principles:
  • —Class P: Decision problems solvable by a Deterministic Turing Machine in polynomial time O(n^k).
  • —Class NP: Decision problems verifiable by a polynomial-time verifier with a polynomial certificate (or solvable by Non-Deterministic TM in poly time).
  • —Class co-NP: Decision problems whose complement is in NP.
  • —Polynomial Reduction A <=_p B: A reduces to B in polynomial time. If B in P => A in P. If A is NP-Hard => B is NP-Hard.
  • —DIRECTION OF REDUCTION: To prove problem X is NP-Hard, reduce FROM a KNOWN NP-Hard problem Y TO X (Y <=_p X).
  • —Cook-Levin Theorem: Circuit-SAT and 3-SAT are NP-Complete.
  • —Classic Reductions: 3-SAT <=_p Clique <=_p Vertex Cover <=_p Independent Set; 3-SAT <=_p Subset Sum <=_p Partition <=_p 0/1 Knapsack.
Critical Exam Pitfalls:
  • —REDUCTION DIRECTION ERROR: Proving X <=_p 3-SAT proves that X is in NP, NOT that X is NP-Hard! To prove X is NP-Hard, you must prove 3-SAT <=_p X.
  • —NP does NOT stand for 'Non-Polynomial'; it stands for 'Nondeterministic Polynomial'.
  • —P subseteq NP is known, but whether P = NP remains the greatest open problem in computer science.