Skip to the workspace
Pebellslearn by building
Analysis II: Recurrences, Amortisation & Randomness  ›  Lesson

Recurrences & the Recursion Tree

Recurrences and the Master Theorem 32 minTurn recursive code into a recurrence, then solve it by adding up the recursion tree level by level

dsa / Recurrences & the Recursion Tree

Explore each scene at your own pace.

01 · From code to a recurrence

From code to a recurrence

Recurrences & the Recursion Tree

Definition
A recurrence is an equation that gives an algorithm's running time for input size n in terms of its running time on smaller inputs plus the work done outside the recursive calls, and the master theorem gives its growth rate for common divide-and-conquer shapes.
In plain words
Draw the calls as a tree and write each call's own work on its box. Add up each row, then add up the rows. Either the top row, every row equally, or the bottom row wins, and the master theorem tells you which.

In short: read a recurrence straight off recursive code — one term per recursive call, plus one term f(n) for everything else the call does — then draw it as a tree. Every node's own work is its badge; add the badges on each level, then add the levels. Three shapes decide the outcome: levels that shrink (the root dominates), tie (n^k log n), or grow (the leaves decide). The master theorem is just a one-line rule for spotting which shape you have.

Merge sort's recursion tree: a root badged 8, two children badged 4 each, four leaves badged 2 each — every level totals 8, and 3 levels times 8 is the 24 total writes

In the merge sort lesson you looked at a split tree and concluded "n work per level, log n levels, so O(n log n)". That argument was about one picture. Tunebox runs a dozen recursive functions, and you need a method that gives the cost of any of them.

This section builds Tunebox's cost lab: instruments that measure the code you already wrote and check it against what you proved on paper. The first instrument is the method itself: turn the code into a recurrence, then solve it with a recursion tree.

From code to a recurrence

A recurrence gives a function's cost in terms of its cost on smaller inputs. Read it off the code with two rules:

  1. Every recursive call becomes a term T(size of that call).
  2. Everything else the body does (the loops, the merge, the slicing) becomes one extra term, written f(n).

Here is your merge sort from section 3, annotated:

def merge_sort(items):
    if len(items) <= 1:                  # base case: constant work
        return list(items)
    mid = len(items) // 2
    left = merge_sort(items[:mid])       # T(n/2)
    right = merge_sort(items[mid:])      # T(n/2)
    return merge(left, right)            # about n steps: f(n) = n

Two calls on half the list, plus a merge that touches all n items:

T(n)=2 T(n/2)+n,T(1)=1T(n) = 2\,T(n/2) + n, \qquad T(1) = 1

The same reading works for everything you wrote earlier:

your code recursive calls other work recurrence
binary search (section 3) one, on half a comparison T(n)=T(n/2)+1T(n) = T(n/2) + 1
merge sort two, on halves the merge T(n)=2T(n/2)+nT(n) = 2T(n/2) + n
quick sort, sorted input, last pivot one, on n − 1 the partition T(n)=T(n−1)+nT(n) = T(n-1) + n
naive Fibonacci (section 6) two, on n−1 and n−2 an addition T(n)=T(n−1)+T(n−2)+1T(n) = T(n-1) + T(n-2) + 1

Try one you have not seen. This function loops over its input, then makes three calls, each on half of it:

What does this print?
work = 0

def split3(n):
    global work
    if n <= 1:
        return
    for _ in range(n):
        work += 1
    split3(n // 2)
    split3(n // 2)
    split3(n // 2)

split3(8)
print(work)

Getting the number of calls right matters more than anything else. This model of binary search gets it wrong:

broken — fix it

Binary search on 1,024 titles looks at about 11 titles, so this should print 11. It prints 2047 because the model thinks the search visits both halves.

Scene 1 of 10
Project context and further resources
You're building a piece ofTunebox — the cost lab
This piece — level_work(): Adds up a recursion tree one level at a time, so the shape of the cost is visible.
Scenario Tunebox's engineers argue about whether a new recursive feature will scale. The level ledger draws its recursion tree as numbers, so everyone can see which levels carry the cost.
level_work.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
level_work(n, a, b, k) → listThe total work on each level of the recursion tree, root first.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.