Chapter 2 · Simulation

Recurrences and the Master Theorem

← Chapter 2

T(n) = a·T(n/b) + n^k

Recursion Tree

nodes · subproblem size · level work

Recurrences and the Master Theorem

Explore recurrences of the form T(n) = aT(n/b) + f(n), compare the levels of the recursion tree, and classify the recurrence with the Master Theorem.

How to use

  • Enter a (number of subproblems, a ≥ 1), b (shrink factor, b > 1), k (non-recursive work f(n) = nk) and a sample input size n, then press Solve Recurrence.
  • The left panel shows the critical exponent logba, the Master Theorem case and the predicted bound.
  • The recursion tree has one row for each level i: ai subproblems of size n/bi each (small squares; at most 30 are drawn, “…” means there are more) and the total work at that level ai·(n/bi)k. The bar compares the level's work with the heaviest level. At most 10 levels are drawn.

Master Theorem

Compare f(n) = nk with nlogba:

  • Case 1 (k < logba): the recursive work dominates; the work per level grows toward the leaves; T(n) = Θ(nlogba).
  • Case 2 (k = logba): every level contributes the same amount of work; T(n) = Θ(nk log n).
  • Case 3 (k > logba): the root dominates; the work per level shrinks toward the leaves; T(n) = Θ(nk). Case 3 needs the regularity condition a·f(n/b) ≤ c·f(n) for some c < 1; for f(n) = nk it always holds with c = a/bk.

Examples

Recurrencea, b, kBound
Binary search1, 2, 0Θ(log n)
Merge sort2, 2, 1Θ(n log n)
Karatsuba3, 2, 1Θ(n1.585)
Strassen7, 2, 2Θ(n2.807)