Towers of Hanoi recurrence M(n) = 2M(n-1) + 1 solved step by step

Count the disk moves of Towers of Hanoi: M(n) = 2M(n-1) + 1, solved by backward substitution, forward chaining and the characteristic polynomial. Answer 2ⁿ − 1 ∈ Θ(2ⁿ).

The question

Consider the recursive algorithm below. Algorithm Hanoi (n, from, to, via) IF n == 0 RETURN ELSE: Hanoi(n-1, from, via, to) move one disk from → to Hanoi(n-1, via, to, from) END Count the disk moves. Part 1: Write the recurrence for the worst case and solve it by backward substitution, forward chaining and the characteristic polynomial (Big O). Part 2: Find the best case (Big Ω). Part 3: Find Big Θ.

The recurrence and backward substitution

The basic operation here is the one done most: moving a disk. Count moves with a recurrence. Direct solution: with n == 0 there is nothing to move.

Conquer: the first call moves n-1 disks out of the way: M(n-1).

Combine: the largest disk moves once.

Conquer again: the second call moves the n-1 disks back on top: another M(n-1).

Together: M(n) = 2M(n-1) + 1, M(0) = 0.

Step 1 · Draft: the recurrence for n-1, n-2, n-3.

These are what we plug in.

Step 2 · Applying the draft: replace M(n-1). The 2 in front multiplies everything inside.

Replace M(n-2):

Replace M(n-3):

Step 3 · General equation: after k substitutions, 2^kM(n-k) plus the powers of 2 from 2^0 up to 2^k-1.

The summation rule for powers: sum from i=0 to k-1 of 2^i = 2^k − 1.

Make the inside the base case: n − k = 0, so k = n. M(n) = 2^n·M(0) + 2^n − 1 = 2^n − 1. So ∈ O(2^n).

The rest

Next: the same recurrence solved by forward chaining and by the characteristic polynomial, then Big Ω and Big Θ, with a free account.