Master the analysis of divide-and-conquer algorithms through recursion trees, the Master Theorem, and geometric series
Divide-and-conquer algorithms split a problem, solve the pieces, and combine the results. A recurrence relation like T(n) = aT(n/b) + f(n) captures their cost, and the demos below show how to solve one.
Pick a preset like Merge Sort or Strassen, or set a, b, and d yourself. Watch where the work piles up: at the root, at the leaves, or evenly across the levels.
| Level | # Problems | Size | Work/Problem | Total Work | Visual |
|---|---|---|---|---|---|
| 0 | 2^0 = 1 | 16 | 16.0 | 16.0 | |
| 1 | 2^1 = 2 | 8 | 8.0 | 16.0 | |
| 2 | 2^2 = 4 | 4 | 4.0 | 16.0 | |
| 3 | 2^3 = 8 | 2 | 2.0 | 16.0 | |
| 4 | 2^4 = 16 | 1 | 1.0 | 16.0 |
Every divide-and-conquer recurrence T(n) = aT(n/b) + f(n) corresponds to a recursion tree. At each level, the problem splits into a subproblems of size n/b. Watch how work distributes across levels!
Pick an example or set a, b, and d, and see which of the three cases applies. Each case comes from comparing f(n) to nlogb(a).
The Master Theorem provides a powerful shortcut for recurrences of the form T(n) = aT(n/b) + f(n). By comparing the work at different levels of the recursion tree, we can immediately determine if the algorithm is O(n), O(n log n), O(n²), or something else entirely.
The Master Theorem has three cases based on comparing f(n) to nlogb(a). This determines whether work is dominated by leaves (Case 1), balanced across levels (Case 2), or dominated by the root (Case 3).
Adjust a, b, and d and watch the work per level form a geometric series with ratio r = a/bd. Notice what changes as r crosses 1.
Total work T(n) is a geometric series:
where r = a/bd is the ratio between consecutive levels, and L = logb(n) is the tree depth.
At level k, there are ak subproblems, each of size n/bk.
Work at level k = ak × (n/bk)d = nd × (a/bd)k = nd × rk
Total work = nd × Σk=0..L rk
This is a geometric series! Its sum depends on whether r < 1, r = 1, or r > 1.
The total work is a geometric series with ratio r = a/bd. When r < 1, the series converges (root dominates). When r > 1, it diverges (leaves dominate). When r = 1, all terms are equal (balanced).
Try the T(n/2) + T(n/3) preset, or add subproblems of your own sizes. The demo finds the exponent p that sets the growth rate.
The Akra-Bazzi method solves recurrences with multiple, differently-sized subproblems:
Find p where Σ ai/bip = 1, then compare f(n) to np.
The Master Theorem is a special case of Akra-Bazzi where all subproblems have the same size: T(n) = aT(n/b) + f(n).
In this case, finding p is simple: a/bp = 1 → p = logb(a).
Akra-Bazzi handles more general recurrences where subproblems may have different sizes, like T(n) = T(n/2) + T(n/3) + n.
What if subproblems have different sizes? The Akra-Bazzi method handles T(n) = T(n/2) + T(n/3) + f(n) by finding p where the sum of ai/bip equals 1.
Pick Fibonacci or another preset and look at the roots of its characteristic equation. The largest root sets the growth rate — for Fibonacci, the golden ratio.
For linear homogeneous recurrences, the closed-form solution comes from solving a polynomial equation. The roots determine the growth rate.
For recurrences like Fibonacci (T(n) = T(n-1) + T(n-2)), the characteristic equation method finds closed-form solutions. The roots determine exponential growth rates like the golden ratio.
Spot a mistake or have an idea?