Overview
This class covers how to derive recurrence relations from algorithms, the three methods for solving recurrences (substitution, recurrence tree/iteration, and the Master Theorem), and a brief introduction to trees. It builds directly on the divide-and-conquer algorithms already covered (merge sort, quick sort, binary search, Strassen's) and on the closest-pair problem recap. The Master Theorem is treated in depth: its form, its three cases, the regularity condition for Case 3, and the situations in which it cannot be applied. The class closes with tree-figure questions that are deferred to the next session.
Learning outcomes
- Derive a recurrence relation for an algorithm by writing the algorithm, identifying its recursive structure, and separating the recursive part from the non-recursive (merge/iteration) cost.
- Explain the components of a recursion: base case, recursive case, and (rare) initial conditions, and state why well-formed recursion must converge toward the base case.
- Solve recurrences using the substitution method (guess + induction), the recurrence tree/iteration method, and the Master Theorem.
- Apply the Master Theorem to
T(n) = a·T(n/b) + f(n)by comparingn^(log_b a)withf(n)and classifying the result into Case 1, 2, or 3. - Verify the Case 3 regularity condition
a·f(n/b) ≤ c·f(n)withc < 1. - Recognize when the Master Theorem cannot be applied (logarithmic terms, non-constant
a,a < 1,b ≤ 1, non-monotone functions, etc.). - Explain what the Master Theorem says about order of complexity and how it relates to big-O, theta, and big-omega.
- State the basic tree facts:
nnodes meansn − 1edges, height is the number of edges on the longest root-to-leaf path, and name the standard traversals.
Core concepts
Closest Pair Problem (recap)
Given a set of points, identify the pair closest to each other. Median is used for partitioning, and estimating the median requires sorting first; in 1D sorting is no issue, but in 2D the x-coordinate is preferred as the sort key. The problem can be solved iteratively or recursively via divide and conquer. This motivates the recurrence analysis that follows.
Recursion and recurrences
Recursion is "repeating itself, repeating itself, repeating itself." Divide-and-conquer algorithms (merge sort, quick sort, binary search) divide recursively until reaching the base case (in merge sort, one element). A recursion has two cases — base case (stopping criterion) and recursive case (the recursive call) — and, rarely, initial conditions that specify the function's value for small input sizes (e.g., an algorithm applies when n is 6 or above; before that a different function/initial value applies). Well-formed recursion means that after each recursive call the smaller problem tends toward the base condition; if this does not happen, the algorithm's convergence is broken. A recurrence relation (e.g., for merge sort, binary search, Strassen's) is a relation from which the exact complexity cannot be claimed directly; the solving methods below are used to estimate it. In complexity estimation we ignore corner/boundary cases — e.g., we assume the two halves of the array are always equal, though for odd n one part is one greater. A recurrence is written for n > 1 (the recursive step) with a separate statement for n = 1 (the base case); the recurrence relations given are the recursive step only, not the base condition. Illustration: take f(n) = n − 6; for values less than or equal to 6 this becomes 0, so when n > 7 a recurrence relation can be applied, for 0 < n < 7 some other case applies, and there is a base case that stops at n = 1. Such initial states occur rarely, in only a few problems. The base condition is the last condition, and algorithm complexity depends on how the recursive structure behaves.
Input order and merge sort
If the input is already sorted, merge sort's complexity is still O(n log n) — it splits and merges everything regardless, so the best case equals the worst case. This is the big contrast with insertion sort, which is O(n) on sorted input; that contrast is the reason hybrid sorts (Timsort) use insertion sort on small or nearly sorted runs.
Three methods for solving recurrences
- Substitution method (guessing the solution): guess the form of the solution, then use mathematical induction to find the constant so that the solution works; if it works, declare it the complexity. Making a good guess is a challenge and comes with experience; this method is often not considered a good approach because a solution must be guessed.
- Recurrence tree / iteration method: form a tree structure (
n, n/b, n/b, ...) as seen in divide-and-conquer algorithms; the tree runs forlog nlevels withnwork per level. Easy if you can capture the recurrence relation or the algorithm's structure, but estimating the series (e.g., log series vs. something else) is challenging. - Master theorem: see below.
Master Theorem
Form: T(n) = a·T(n/b) + f(n), given a ≥ 1 and b > 1 (if b is not greater than 1 there is no convergence). f(n) must be an asymptotically positive function.
Procedure: immediately compute n^(log_b a); say this gives n^x. Compare with f(n), say n^y. Three possibilities:
- n^x equivalent to n^y → Case 2
- n^x greater than n^y (subtract some epsilon to equate) → Case 1
- n^x less than n^y (add some epsilon to equate) → Case 3
Case 3 extra condition: a·f(n/b) ≤ c·f(n) for larger n, with c < 1, must also hold.
Results:
- Case 1 (reduce/subtract epsilon): complexity = n^(log_b a) (stated as big-O of n^(log_b a)).
- Case 2 (equal): complexity = n^(log_b a)·log n (theta).
- Case 3 (add epsilon): complexity = f(n) (theta), provided a·f(n/b) ≤ c·f(n) for large n with c < 1.
Note on epsilon: in Case 2 the epsilon condition was stated as "epsilon should be greater than 1, not greater than 0" [as spoken; flagged below]; c < 1 applies to Case 3.
Interpretation of the cases (from Q&A): Case 1 (subtracting) targets the maximum possible value of f(n) — worst case; Case 3 (adding) targets the minimum possible value of n^(log_b a) — best case; the equality case in between is the average case. The Master Theorem primarily gives the order of computational complexity; if something is theta it is obviously also big-O and big-omega. Forget the three symbols for a moment; what matters is the order. If g(n) is greater than f(n), we are in the omega case; if g(n) is less than f(n), we are in the big-O case; if they are equivalent, we apply theta. The big-O, theta and big-omega notations are the same upper-bound / lower-bound / tight-bound notations learned earlier.
Case intuition: If the cost of solving the subproblem at each level increases by a certain factor (epsilon), we are in Case 3. If solving each step is nearly equal to f(n), we are in Case 2. If in each step it decreases by a certain factor, we are in Case 1.
Why c must be strictly between 0 and 1: c can be zero or negative in principle, but it should be between 0 and 1 and should not be zero. If it is negative, then n/b is not reducing the problem size (as in well-formed recursion), the recurrence relation will never converge, and the strict less-than / greater-than conditions will not hold.
Choice of c in Case 3: The instructor took c = 1 in the Case-3 example, but the condition is c < 1. If you take the boundary case 3.5/4, that is applicable; if c has to be taken as a greater value, the Master Theorem cannot be applied there.
Real-time relevance: For an algorithm of the form a T(n/b) + f(n) that calls itself recursively, the Master Theorem gives the computational complexity. Directly looking at a T(n/b) + f(n) we cannot see how the algorithm behaves in a real scenario unless we reduce it to something like n log n, n^2 or n^3. Real examples already stated: merge sort, binary search, and the Strassen expression. There may be real business use cases, but for most of them the instructor does not think so.
When the Master Theorem cannot be applied:
- When T(n) is not monotone, like sin x, 2^x
- When a is not a constant, or a < 1
- Logarithmic terms (cannot apply directly)
- When f(n) is logarithmic, sinusoidal, a function of 2^n, or factorial n — because we cannot establish a fixed value of epsilon
- When a < 0 or b < 0
- The Master Theorem is only for recursive problems; for non-recursive algorithms we estimate directly from the loops (e.g., n^2).
Why epsilon must be fixed: In the Master Theorem the assumption is that epsilon remains fixed throughout. For logarithmic terms the epsilon is not fixed because logarithmic behavior is different — whatever we put as n + x will not be fixed. For functions where estimating epsilon is "floated" (can change its behavior), we can never apply the theorem. This is a drawback of the Master Theorem.
Aim: The whole aim is to get n^(log_b a) as close as possible to f(n) — either add epsilon, subtract epsilon, or make them almost equivalent — so it falls into Case 1, 2 or 3.
Deriving a recurrence relation from an algorithm
First write the algorithm. Then check whether there is any recursive structure. If a recursive structure exists, identify the "hidden" recurrence relation, and then solve that recurrence relation using the Master Theorem. If a problem is divided into two subproblems of equal size, the recursive part is 2 T(n/2); the cost of merging is added as the non-recursive term (e.g., order n or n^2). In exams, if a recurrence relation is given that was not taught, it will be provided; but for a real-world problem where you are making the algorithm, you have to think about how your algorithm is behaving. If in your algorithm the same function is calling again and again and again, there is a chance that a recurrence relation holds; try to identify what type of recurrence relation it is — only then can you apply the Master Theorem and other techniques.
Mixed algorithms (recursive + iterative parts)
In the Master Theorem structure T(n) = 2T(n/2) + f(n), f(n) is for the non-recursive part and T(n/2) (the 2 T(n/2) term) is for the recursive part. The same structure holds for an algorithm that has both recursive portions and non-recursive (iteration) portions. If the non-recursive part is more time-consuming than the recursive part, it will dominate, as seen in the three cases of the Master Theorem. Structurally, find which is the dominating factor — the dominating part is the complexity.
Tree basics
A tree with n nodes has n − 1 edges: a tree is connected and acyclic, so every node except the root has exactly one incoming edge. The height is the number of edges on the longest path from the root to a leaf; if height is counted in nodes instead, the same tree has height h + 1, so always check the convention.
Traversals: preorder is Root–Left–Right; inorder is Left–Root–Right; postorder is Left–Right–Root; level order is BFS by depth. Inorder traversal of a binary search tree (distinct keys) produces the keys in ascending order. Example: root A, B the left child of A, C the right child of A, D and E the children of B — preorder is A, B, D, E, C (root, then the whole left subtree, then the right subtree); inorder of the same tree is D, B, E, A, C and postorder is D, E, B, C, A.
Tree figure question
A last leaf in the figure has no sibling on the left or right. This was confirmed as correct: the tree may be the result of multiple deletions — earlier it had a sibling, but that sibling is now removed. Such a height can also occur without any right siblings. Trees will be restarted from the beginning in the next class.
Formulas and maths
T(n) = 2T(n/2) + c·n— merge sort recurrence;cis some constant, valid forn > 0, stop whenn = 1. Used to model merge sort's divide-and-conquer cost.T(n) = 2T(n−1) + 1— Tower of Hanoi recurrence; reduces by 1, not by 2; still converges, but not as fast as merge sort. Used to model the Tower of Hanoi.T(n) = 2T(⌊n/2⌋) + n, guessed solutionn log n(worst case) — substitution example. EstablishT(n) ≤ c·n log n; expand to2·c·⌊n/2⌋·log(⌊n/2⌋) + n ≤ c·n·log(n/2) + n, and using the log property reachc·n log n— the assumption is confirmed. If the initial assumption is not correct, the result changes. Used to demonstrate the substitution (guess-and-verify) method.f(n) ≤ c·g(n)— big-O condition recalled;f(n)represents the algorithm andg(n)is the known function. Used to state upper bounds.T(n) = a·T(n/b) + f(n),a ≥ 1,b > 1,f(n)asymptotically positive — Master Theorem form. Used to solve divide-and-conquer recurrences.- Compare
n^(log_b a)withf(n): - Case 1:
n^(log_b a) − epsilongivesf(n)→ complexitytheta(n^(log_b a)) - Case 2: nearly equal → theta
- Case 3:
n^(log_b a) + epsilongivesf(n)→ omega Used to classify and solve a recurrence. a f(n/b) <= c f(n)forc < 1(strictly between 0 and 1) — Case 3 regularity condition. Used to validate Case 3 before declaringtheta(f(n)).n^(log_2 7) = n^2.81— Strassen's expression (if it were 8 it would be 3, otherwise 2.81). Used as the complexity of Strassen's matrix multiplication.n^(log_2 8) = n^3— normal multiplication: each problem divided into 8 subproblems. Used as the complexity of normal matrix multiplication.T(n) = 2T(n/2) + n log nwith substitutionn = 2^m:T(2^m) = 2 T(2^(m-1)) + 2^m log_2(2^m)- Let
T(2^m) = f(m):f(m) = 2 f(m-1) + m 2^m - Expansion:
f(m) = 4 f(m-2) + (m-1) 2^m + m 2^m; continuing,f(m) = 2^m f(0) + (1 + 2 + 3 + ... + m) 2^m = 2^m f(0) + m(m+1)/2 · 2^m- Back-substituting
2^m = n,m = log_2 n:n t(1) + n · log_2 n (log_2 n + 1)/2, i.e.n log^2 n(base 2) Used to solve a recurrence the Master Theorem cannot handle. T(n) = 2 T(n/2) + f(n)— general Master Theorem form:f(n)is the non-recursive part,2 T(n/2)is the recursive part. Used to map mixed recursive/iterative algorithms onto the theorem.T(n) = 2 T(n/2) +(merge cost of ordernorn^2) — example of forming a recurrence when a problem is divided into two equal subproblems. Used when deriving a recurrence from scratch.- Merge sort on sorted input:
T(n) = O(n log n)— best case equals worst case; contrast insertion sort, which isO(n)on sorted input. Used to explain why hybrid sorts (Timsort) use insertion sort on small or nearly sorted runs. - Tree with
nnodes:edges = n − 1. Used for basic tree counting. - Height
hin edges ↔ heighth + 1in nodes — the same tree under the two conventions. Used to check which convention a question uses.
Worked examples
- T(n) = 9T(n/3) + n: a = 9, b = 3, f(n) = n. n^(log_3 9) = n². Compare with f(n) = n: n^(2−ε) equates to n¹ → subtraction → Case 1 → complexity of order n².
- T(n) = 3T(n/2) + n²: a = 3, b = 2, f(n) = n². n^(log_2 3) = n^1.58. Since f(n) = n², add epsilon (1.58 + ε) to equate → Case 3. Check a·f(n/b) ≤ c·f(n): 3·(n²/4) ≤ c·n²; with c = 1, 3/4 ≤ 1 holds. Complexity = theta(f(n)) = n².
- f(n) = n, a = 3, b = 5: n^(log_5 3) ≈ n^0.68. Compare with n → addition → Case 3. Check: a·f(n/b) = 3n/5 ≤ c·n; with c = 1 it is of course true. Complexity = theta(n).
- a = 2, b = 2: n^(log_2 2) = n¹, equal to f(n) = n → Case 2 → theta(n^(log_b a)·log n) = theta(n log n). This is the merge sort recurrence.
- a = 2, b = 2, f(n) = 1 (theta of n is 1 [as spoken]): n^(log_2 2) = n; compare with n⁰ → subtraction → Case 1 → theta(n^(log_b a)) = big-O of n.
T(n) = 2T(n/2) + n log n(Master Theorem fails):a = 2,b = 2,f(n) = n log n;n^(log_b a) = n^(log_2 2) = n^1. If we add epsilon we would getn^(1 + x)because of thelog nterm, but the epsilon is not fixed — logarithmic behavior is different — so the Master Theorem cannot be applied. Checking Case 3's condition: `a f(n/b) = 2 · (n/2) log(n/2) = n log(n/- Tree with root A, B left child of A, C right child of A, D and E children of B: preorder = A, B, D, E, C (root, then the whole left subtree B, D, E, then the right subtree C); inorder = D, B, E, A, C; postorder = D, E, B, C, A. Used to show that the three traversals of the same tree differ only in when the root is visited.
Key takeaways
- A recurrence relation is derived by writing the algorithm, spotting the recursive structure, and separating the recursive term from the merge/iteration cost.
- Substitution needs a guess plus induction; the recurrence tree is easy to draw but the series is hard to sum; the Master Theorem is the fast route when its conditions hold.
- Master Theorem: compare
n^(log_b a)withf(n)— subtract epsilon (Case 1), equal (Case 2), add epsilon (Case 3); Case 3 also needsa·f(n/b) ≤ c·f(n)withc < 1. - The theorem gives the order of complexity; theta implies big-O and big-omega.
- It fails for logarithmic
f(n), non-constant ora < 1,b ≤ 1, non-monotone functions, and anything where epsilon cannot be fixed. - Merge sort is
O(n log n)even on sorted input — best case equals worst case — unlike insertion sort, which isO(n)on sorted input; hybrid sorts such as Timsort exploit this. - A tree with
nnodes hasn − 1edges; height is the number of edges on the longest root-to-leaf path (hin edges =h + 1in nodes). - Preorder is Root–Left–Right, inorder is Left–Root–Right, postorder is Left–Right–Root, level order is BFS; inorder of a BST gives keys in ascending order.
Glossary
- Base case: the stopping condition of a recursion; the last condition, where the recursion no longer calls itself.
- Recursive case: the part of the recursion that makes the recursive call on a smaller problem.
- Initial conditions: rare extra statements fixing the function's value for small input sizes before the recurrence applies.
- Well-formed recursion: recursion in which each call shrinks the problem toward the base case, so it converges.
- Recurrence relation: an equation describing a function in terms of its value on smaller inputs; the exact complexity is not read off directly but estimated by solving it.
- Substitution method: guess the solution form, then prove it by induction and fix the constant.
- Recurrence tree / iteration method: expand the recurrence level by level into a tree, sum the work per level, and sum over
log nlevels. - Master Theorem: closed-form solution for
T(n) = a·T(n/b) + f(n)witha ≥ 1,b > 1,f(n)asymptotically positive, split into three cases by comparingn^(log_b a)withf(n). - Regularity condition:
a·f(n/b) ≤ c·f(n)for largenwithc < 1; required for Case 3. - Epsilon: the fixed factor by which
n^(log_b a)andf(n)differ in the Master Theorem; it must stay fixed for the theorem to apply. - Tree: a connected acyclic graph;
nnodes haven − 1edges and every node except the root has exactly one incoming edge. - Height: the number of edges on the longest path from the root to a leaf; counted in nodes it is
h + 1. - Preorder: traversal visiting Root–Left–Right.
- Inorder: traversal visiting Left–Root–Right; on a BST it yields keys in ascending order.
- Postorder: traversal visiting Left–Right–Root.
- Level order: traversal visiting nodes breadth-first by depth.