Skip to content

Recurrences and divide and conquer

Write the recurrence for the running time of a recursive function, solve it by unrolling and by a recursion tree, and check the closed form.

It opens on

A sorted list of 8 numbers is searched by probing the middle and keeping one half, again and again. At most how many probes, and how many for 16 numbers?

Why it matters

The running time of binary search, merge sort and every divide and conquer algorithm is first an equation in terms of itself, and solving it tells whether a recursive program takes log n steps, n log n steps, or more calls than there are atoms in a computer's memory.

Good to know first

  • Big-O, Big-Omega and Big-Theta: Θ gives a tight bound, so Θ(log n) is not Θ(n)
  • Analyzing loops: a loop that halves its variable runs log₂ n times
  • Recursion and the call stack: a recursive call is a frame, and total made n + 1 calls on n nodes
  • Linear and binary search: binary search compares the middle item and keeps one half

Step by step

  1. From code to a recurrence

    Write the recurrence T(n) = T(n/2) + 1 for recursive binary search from its code, and use it to find the worst-case probes for small n.

    The first probe is a[3], which is 12, and 12 is less than 40, so the call keeps indexes 4 to 7, which is 4 numbers. The second probe is a[5], which is 23, still less than 40, so it keeps indexes 6 and 7, which is 2 numbers.

    A common mix-up

    Doubling the list doubles the probes, so 16 numbers need 8 probes.

    In fact: Each probe discards half the list, so T(2n) = T(n) + 1 and 16 numbers need only 5.

  2. Unrolling

    Solve a recurrence by unrolling it to the base case, giving T(n) = log₂ n + 1 for binary search and T(n) = n for a one-call-per-item function, and check each closed form.

    T(n) equals T(n/2) + 1. The recurrence also says T(n/2) equals T(n/4) + 1, so T(n) equals T(n/4) + 1 + 1, which is T(n/4) + 2. One more substitution gives T(n/8) + 3.

    the first rows of the unrolling

    A common mix-up

    Unrolling is finished after two or three substitutions.

    In fact: It ends only at the base case, so we need the pattern after k substitutions and the k that reaches T(1) or T(0).

  3. A recursion tree for two calls

    Solve T(n) = 2T(n/2) + n with T(1) = 0 by a recursion tree to get n log₂ n, and use the shortcut for recurrences of the form aT(n/b) + nᵈ.

    The root does a pass of 4 steps. Its two children are calls on 2 items, each does 2 steps, so the middle level costs 2 + 2 = 4 in all. The four leaves are calls on 1 item and cost nothing, and the whole tree costs 4 + 4 + 0 = 8 steps.

    A common mix-up

    Two calls on halves make T(n) = 2T(n/2) + n only Θ(n), since the halves add up to n.

    In fact: Every one of the log₂ n levels costs n, so the total is n log₂ n.

  4. When the tree explodes

    Write and solve the recurrence for the calls of naive fib, C(n) = C(n − 1) + C(n − 2) + 1, and say why it is exponential and why the shortcut does not cover it.

    C(2) = C(1) + C(0) + 1 = 1 + 1 + 1 = 3, a call on 2 and its two children.

    A common mix-up

    Fib(n) is as cheap as a loop of n steps, since it only adds two numbers per call.

    In fact: The two calls repeat the same smaller calls, so the number of calls is 2 fib(n + 1) − 1, about 1.6 times more for each step up in n.

Try it

The same search on a list of 32 numbers. Using the recurrence, what are T(32) and the sizes of what is left after each probe?Answer

T(32) = 6; the sizes go 32, 16, 8, 4, 2, 1

Hint: Build up from T(1) = 1 by adding 1

Unroll T(n) = T(n/2) + 3 with T(1) = 2, and find T(16).Answer

T(n) = 3 log₂ n + 2, so T(16) = 14

Hint: After k substitutions it is T(n/2ᵏ) + 3k

Using a tree, find T(64) for T(n) = 2T(n/2) + n with T(1) = 0.Answer

6 levels of 64 each, 384

Hint: Log₂ 64 = 6

How many calls does fib(5) make, using the recurrence from C(4) = 9 and C(3) = 5?Answer

C(5) = 9 + 5 + 1 = 15

Hint: Add 1 for the call itself

The third probe is a[6], which is 38, again less than 40. How many numbers are left after it, and what happens at the next probe?Answer

One number is left, a[7] = 56, which is more than 40, so the left half is empty and the next call returns −1 after 4 probes in all.

Recurrences and divide and conquer

30 minutes every month. No card.