ENع
←Recurrences
Lesson 13 · Discrete maths

Recurrences: describing a value by smaller cases

14 min12 steps5 questionsIntermediate
About this lesson

Many algorithms are easiest to describe by how a big case builds on smaller ones. Write the Towers of Hanoi as a recurrence, fill its table from the base case up, spot the closed form 2ⁿ − 1 and prove it by induction. Then count merge sort's work with M(n) = 2M(n/2) + n and see why it comes to n log₂ n.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 13 · Interactive
01 / 12

A puzzle that repeats

Towers of Hanoi: move a stack of n disks to another peg, one disk at a time, never putting a big disk on a smaller one. Call the fewest moves T(n). The table will hold T(1) to T(5).

>>> def T(n):
...     if n == 1: return 1
...     return 2*T(n-1) + 1
...
>>> [T(n) for n in range(1, 6)]
[1, 3, 7, 15, 31]
Loading figure…
or ← →
Read the lesson step by step
  1. A puzzle that repeats

    Towers of Hanoi: move a stack of n disks to another peg, one disk at a time, never putting a big disk on a smaller one. Call the fewest moves T(n). The table will hold T(1) to T(5).

    >>> def T(n):
    ...     if n == 1: return 1
    ...     return 2*T(n-1) + 1
    ...
    >>> [T(n) for n in range(1, 6)]
    [1, 3, 7, 15, 31]
  2. The rule: a recurrence

    To move n disks, move the top n−1 aside, move the biggest disk, then move the n−1 back on top. So T(n) = 2T(n−1) + 1. A rule that defines a value from smaller cases of itself is a recurrence.

    >>> t1 = 1                  # T(1)
    >>> 2*t1 + 1               # T(2) = 2T(1) + 1
    3
  3. The base case

    The rule alone never stops: T(5) needs T(4), which needs T(3), and so on forever. A base case ends the chain. One disk takes one move, so T(1) = 1. Line 2 of the code is that base case.

    >>> def T(n):
    ...     if n == 1: return 1   # the base case
    ...     return 2*T(n-1) + 1
    ...
    >>> T(1), T(2)
    (1, 3)
  4. Unroll from the bottom

    Now build upward. Feed T(1) into the rule: T(2) = 2 × 1 + 1 = 3. Feed that in: T(3) = 2 × 3 + 1 = 7. Each new value needs only the one before it.

    >>> 2*1 + 1, 2*3 + 1        # T(2), T(3)
    (3, 7)
  5. Keep going

    T(4) = 2 × 7 + 1 = 15 and T(5) = 2 × 15 + 1 = 31. The table now reads 1, 3, 7, 15, 31. This "plug and chug" works, but reaching T(64) takes 63 rounds of the rule.

    >>> 2*7 + 1, 2*15 + 1       # T(4), T(5)
    (15, 31)
  6. Spot the pattern

    Add 1 to each value: 2, 4, 8, 16, 32. Those are the powers of 2. So we guess T(n) = 2ⁿ − 1. A formula with no T on its right side is called a closed form.

    >>> [t + 1 for t in [1, 3, 7, 15, 31]]
    [2, 4, 8, 16, 32]
    >>> [2**n for n in range(1, 6)]
    [2, 4, 8, 16, 32]
  7. Prove it: the base case

    A pattern in five values is only a guess. Proof by induction has two parts. Part one, the base case: the formula gives 2¹ − 1 = 1, and T(1) = 1. They agree.

    >>> 2**1 - 1                # the formula at n = 1
    1
  8. Prove it: the step

    Part two: assume T(n−1) = 2ⁿ⁻¹ − 1. The rule gives T(n) = 2(2ⁿ⁻¹ − 1) + 1 = 2ⁿ − 2 + 1 = 2ⁿ − 1. Each case passes the formula to the next, so it holds for every n ≥ 1.

    >>> n = 10
    >>> 2*(2**(n-1) - 1) + 1 == 2**n - 1
    True
    >>> 2**10 - 1              # T(10) without unrolling
    1023
  9. Why a closed form helps

    The closed form answers at once. 64 disks need 2⁶⁴ − 1, about 1.8 × 10¹⁹ moves. At one move per second that is about 585 billion years. Each extra disk doubles the work: exponential growth.

    >>> 2**64 - 1
    18446744073709551615
    >>> (2**64 - 1) / (365.2425 * 24 * 3600)   # years at 1 move/s
    584554049253.8555
  10. Divide and conquer

    Merge sort splits n items into two halves, sorts each half, then merges them. Count n units of work per merge: M(n) = 2M(n/2) + n. One item is already sorted, so the base case is M(1) = 0.

    >>> def M(n):
    ...     if n == 1: return 0
    ...     return 2*M(n//2) + n
    ...
    >>> M(1), M(2)
    (0, 2)
  11. Unroll by halving

    Use sizes that halve cleanly: 2, 4, 8, 16. M(2) = 2 × 0 + 2 = 2, M(4) = 2 × 2 + 4 = 8, M(8) = 2 × 8 + 8 = 24, M(16) = 2 × 24 + 16 = 64.

    >>> def M(n):
    ...     if n == 1: return 0
    ...     return 2*M(n//2) + n
    ...
    >>> [M(n) for n in (1, 2, 4, 8, 16)]
    [0, 2, 8, 24, 64]
  12. Count the levels

    Halving 16 reaches 1 after log₂ 16 = 4 levels, and each level does 16 work in total. So M(n) = n log₂ n: 16 × 4 = 64. That grows far slower than Hanoi's 2ⁿ − 1.

    >>> import math
    >>> [n * int(math.log2(n)) for n in (1, 2, 4, 8, 16)]
    [0, 2, 8, 24, 64]
    >>> 16 * math.log2(16)
    64.0

Quiz Unlocks after the last step.