Recursion Trees
A substitution proof needs a guess, and a recursion tree is the standard way to make one. The tree draws every call of the recursion as a node, labelled with the work that call does outside its own recursive calls. Summing the labels level by level turns the recurrence into an ordinary sum, often a geometric series. The answer it gives is usually right, but because the drawing tends to gloss over floors, ceilings and the last level, the careful habit is: guess with the tree, prove with substitution.
The Divide-and-Conquer Recurrences lesson sketches the idea for the balanced case. This lesson does the bookkeeping in full and then handles a tree whose branches have different lengths.
Building the tree
Take with , and let be a power of 2. The root is the top-level call: it costs by itself and has two children of size . Each child costs and has two children of size , and so on until the size reaches 1.
level 0 n^2 level cost n^2
/ \
level 1 (n/2)^2 (n/2)^2 n^2 / 2
/ \ / \
level 2 (n/4)^2 (n/4)^2 (n/4)^2 (n/4)^2 n^2 / 4
... ...
level i 2^i nodes, each (n/2^i)^2 n^2 / 2^i
...
level lg n n leaves, each T(1) = 1 nThree numbers describe any such tree for :
- Depth. The size at level is , which reaches 1 at . So there are levels.
- Nodes per level. Every node has children, so level has nodes, and the bottom level has leaves.
- Cost of level . .
For the tree above, level costs , so
The level costs form a decreasing geometric series, whose sum is at most a constant times its first term, so : the root alone does a constant fraction of all the work.
Three shapes of tree
The same bookkeeping gives three typical pictures. The program prints the level costs for three recurrences with .
def level_costs(a, f, n):
costs, size, nodes = [], n, 1
while size >= 1:
costs.append(nodes * f(size))
size, nodes = size // 2, nodes * a
return costs
n = 64
for name, a, f in [("2T(n/2) + n^2", 2, lambda m: m * m),
("2T(n/2) + n ", 2, lambda m: m),
("8T(n/2) + n^2", 8, lambda m: m * m)]:
c = level_costs(a, f, n)
print(f"{name}: {c} total {sum(c)}")2T(n/2) + n^2: [4096, 2048, 1024, 512, 256, 128, 64] total 8128
2T(n/2) + n : [64, 64, 64, 64, 64, 64, 64] total 448
8T(n/2) + n^2: [4096, 8192, 16384, 32768, 65536, 131072, 262144] total 520192
- Decreasing (first line): each level costs half the one above, the sum is dominated by the root, .
- Equal (second line): all levels cost , so . Here .
- Increasing (third line): each level costs twice the one above, the sum is dominated by the leaves, . Indeed the total is just under .
The geometric series facts that make this work:
In words: a geometric sum with ratio below 1 is of its first term, and one with ratio above 1 is of its last term. The master theorem is these three pictures turned into a theorem.
An unbalanced tree
Now a recurrence the master theorem does not cover, because the two subproblems have different sizes:
Such recurrences arise when a split is lopsided, for example a quicksort whose pivot always lands at the one-third mark. Draw the tree. The root costs ; its children cost and , together again ; the four grandchildren cost , , and , together again . As long as no branch has reached the bottom, every level costs exactly .
The branches do not all have the same length, though. The path that always takes the third shrinks fastest and reaches size 1 after levels; the path that always takes two thirds is the longest, with levels. So:
- the first levels are full and cost each, giving at least ;
- there are at most levels, each costing at most , giving at most about .
Both bounds are (logarithms of different bases differ by a constant factor), so the guess is . The program builds the tree for with integer sizes and prints each level.
n = 729
level, depth = [n], 0
print("depth nodes cost of level")
while level:
print(f"{depth:5} {len(level):5} {sum(level):6}")
level = [s for x in level if x >= 3 for s in (x // 3, x - x // 3)]
depth += 1depth nodes cost of level
0 1 729
1 2 729
2 4 729
3 8 729
4 16 729
5 32 729
6 64 729
7 114 716
8 158 661
9 186 561
10 162 407
11 112 251
12 92 164
13 26 51
14 18 29
15 2 4
16 2 3
The first seven levels (depths 0 to 6, that is of them) cost exactly 729. After that the short branches have stopped, the levels thin out, and the deepest branch ends at depth 16; for comparison .
Proving the guess
Now the substitution method turns the guess into a theorem. Suppose for all smaller . Then, ignoring rounding,
The lower bound follows the same way with . So the constant in front of should approach about . A numerical check with floors (subproblems and , base cost 1 below 3):
import math
from functools import lru_cache
@lru_cache(None)
def T(n):
if n < 3:
return 1
return T(n // 3) + T(n - n // 3) + n
print("predicted constant 1 / (lg 3 - 2/3) = %.4f" % (1 / (math.log2(3) - 2 / 3)))
for n in [10**2, 10**3, 10**4, 10**5, 10**6]:
print(f"n = {n:8} T(n) / (n lg n) = {T(n) / (n * math.log2(n)):.4f}")predicted constant 1 / (lg 3 - 2/3) = 1.0890
n = 100 T(n) / (n lg n) = 1.1409
n = 1000 T(n) / (n lg n) = 1.1183
n = 10000 T(n) / (n lg n) = 1.1098
n = 100000 T(n) / (n lg n) = 1.1041
n = 1000000 T(n) / (n lg n) = 1.1011
The ratio creeps down towards the predicted constant, exactly what the proof says. The tree supplied the guess , the proof supplied the certainty, and the computation caught no mistake in either.
Common mistake: in an unbalanced tree, assuming every level costs all the way down and multiplying by the longest depth gives a valid upper bound but not a lower bound. The lower bound has to use the shortest depth, the last level that is still full.
Test yourself
In the recursion tree of , how many nodes are on level , and what does each cost?
- nodes, each
- nodes, each
- nodes, each
- nodes, each
The level costs of a recursion tree are . What is the order of the total?
For , which depth gives the lower bound ?
- , the longest branch
- , the depth down to which every level is still full
Why is a recursion tree usually followed by a substitution proof?
- Because trees only work for powers of 2
- Because the tree is a quick way to guess, but it tends to gloss over floors, ceilings and the last levels
- Because the tree always overestimates
- Because the master theorem requires it