About

Mathematical Induction & Tower of Hanoi

Mathematical Induction and Recurrence Relations

Mathematical induction is an indispensable proof technique in algorithm design and analysis. It is used to prove that a property P(n)P(n) holds for all positive integers n≥n0n \ge n_0.

The Principle of Mathematical Induction

  1. Basis Step (Base Case): Verify that P(n0)P(n_0) is true (usually for n0=1n_0 = 1 or 00).
  2. Inductive Hypothesis: Assume that P(k)P(k) is true for an arbitrary integer k≥n0k \ge n_0.
  3. Inductive Step: Prove that if P(k)P(k) is true, then P(k+1)P(k+1) must also be true:
P(k)  ⟹  P(k+1)P(k) \implies P(k+1)

By mathematical induction, P(n)P(n) is true for all integers n≥n0n \ge n_0.


Case Study: The Tower of Hanoi

The Tower of Hanoi is a classic algorithmic puzzle designed by French mathematician Édouard Lucas in 1883.

Problem Definition & Constraints

You are given three pegs: Source (AA), Auxiliary (BB), and Destination (CC), along with nn disks of distinct sizes initially stacked on Peg AA in decreasing order of size (largest at the bottom).

Diagram loads as you scroll.

Recursive Algorithm

Hanoi(n, source, auxiliary, destination):
    if n == 1 then:
        move disk 1 from source to destination
        return

    // Step 1: Move top n-1 disks from source to auxiliary
    Hanoi(n - 1, source, destination, auxiliary)

    // Step 2: Move the largest disk directly to destination
    move disk n from source to destination

    // Step 3: Move the n-1 disks from auxiliary to destination
    Hanoi(n - 1, auxiliary, source, destination)

Step-by-Step Move Tracing

3-Disk Trace (n=3n = 3, Minimum Moves =23−1=7= 2^3 - 1 = 7)

Move #DiskFrom PegTo PegState After Move (A,B,C)(A, B, C)
0---A=[3,2,1],B=[],C=[]A = [3, 2, 1], B = [], C = []
1Disk 1AACCA=[3,2],B=[],C=[1]A = [3, 2], B = [], C = [1]
2Disk 2AABBA=[3],B=[2],C=[1]A = [3], B = [2], C = [1]
3Disk 1CCBBA=[3],B=[2,1],C=[]A = [3], B = [2, 1], C = []
4Disk 3AACCA=[],B=[2,1],C=[3]A = [], B = [2, 1], C = [3]
5Disk 1BBAAA=[1],B=[2],C=[3]A = [1], B = [2], C = [3]
6Disk 2BBCCA=[1],B=[],C=[3,2]A = [1], B = [], C = [3, 2]
7Disk 1AACCA=[],B=[],C=[3,2,1]A = [], B = [], C = [3, 2, 1]

4-Disk Trace (n=4n = 4, Minimum Moves =24−1=15= 2^4 - 1 = 15)

Move #DiskActionDescription
1Disk 1A→BA \to BMove disk 1 out of the way
2Disk 2A→CA \to CMove disk 2
3Disk 1B→CB \to CStack disk 1 on 2
4Disk 3A→BA \to BMove disk 3 to peg BB
5Disk 1C→AC \to AUnstack disk 1
6Disk 2C→BC \to BStack disk 2 on 3
7Disk 1A→BA \to BSubproblem n=3n=3 to peg BB complete
8Disk 4A→CA \to CLargest disk 4 moves to final destination CC
9Disk 1B→CB \to CBegin transferring n=3n=3 from BB to CC
10Disk 2B→AB \to AMove disk 2
11Disk 1C→AC \to AStack disk 1 on 2
12Disk 3B→CB \to CMove disk 3 to CC
13Disk 1A→BA \to BUnstack disk 1
14Disk 2A→CA \to CStack disk 2 on 3
15Disk 1B→CB \to CAll 4 disks successfully on destination peg CC

Recurrence Relation and Proof

Let M(n)M(n) be the number of moves needed for nn disks. From the recursive algorithm:

M(n)=2M(n−1)+1for n>1,M(1)=1M(n) = 2 M(n - 1) + 1 \quad \text{for } n > 1, \quad M(1) = 1

Solving by Backward Substitution

M(n)=2M(n−1)+1=2[2M(n−2)+1]+1=22M(n−2)+2+1=22[2M(n−3)+1]+2+1=23M(n−3)+22+21+20  ⋮=2n−1M(1)+∑i=0n−22i=2n−1(1)+(2n−1−1)=2n−1\begin{aligned} M(n) &= 2 M(n-1) + 1 \\ &= 2 [2 M(n-2) + 1] + 1 = 2^2 M(n-2) + 2 + 1 \\ &= 2^2 [2 M(n-3) + 1] + 2 + 1 = 2^3 M(n-3) + 2^2 + 2^1 + 2^0 \\ &\ \ \vdots \\ &= 2^{n-1} M(1) + \sum_{i=0}^{n-2} 2^i = 2^{n-1} (1) + (2^{n-1} - 1) = 2^n - 1 \end{aligned}

Proof by Mathematical Induction

M(k+1)=2M(k)+1=2(2k−1)+1(by inductive hypothesis)=2k+1−2+1=2k+1−1\begin{aligned} M(k+1) &= 2 M(k) + 1 \\ &= 2(2^k - 1) + 1 \quad \text{(by inductive hypothesis)} \\ &= 2^{k+1} - 2 + 1 \\ &= 2^{k+1} - 1 \end{aligned}

Thus, the formula holds for k+1k+1. By induction, the minimum number of moves for nn disks is exactly 2n−1∈Θ(2n)2^n - 1 \in \Theta(2^n).


Exercises

Exercise 1

If a computer can execute 10910^9 moves per second, how long would it take to solve the Tower of Hanoi for n=64n = 64 disks?

Show solution ↓
Solution

Total moves required:

264−1≈1.84467×1019 moves2^{64} - 1 \approx 1.84467 \times 10^{19} \text{ moves}

Time in seconds:

1.84467×1019109≈1.84467×1010 seconds\frac{1.84467 \times 10^{19}}{10^9} \approx 1.84467 \times 10^{10} \text{ seconds}

Converting to years (1 year≈3.1536×107 s1 \text{ year} \approx 3.1536 \times 10^7 \text{ s}):

Time≈1.84467×10103.1536×107≈584.9 years\text{Time} \approx \frac{1.84467 \times 10^{10}}{3.1536 \times 10^7} \approx 584.9 \text{ years}

Even at 1 billion moves per second, it would take over 584 years!

Exercise 2

Prove by mathematical induction that for all n≥1n \ge 1:

1+3+5+⋯+(2n−1)=n21 + 3 + 5 + \dots + (2n - 1) = n^2
Show solution ↓
Solution
  • Base Case (n=1n = 1): 2(1)−1=12(1) - 1 = 1, and 12=11^2 = 1. Holds.
  • Inductive Hypothesis: Assume 1+3+⋯+(2k−1)=k21 + 3 + \dots + (2k - 1) = k^2 for k≥1k \ge 1.
  • Inductive Step: Show the equation holds for k+1k + 1:
[1+3+⋯+(2k−1)]+(2(k+1)−1)=k2+(2k+1)=(k+1)2\begin{aligned} [1 + 3 + \dots + (2k - 1)] + (2(k+1) - 1) &= k^2 + (2k + 1) \\ &= (k + 1)^2 \end{aligned}

Therefore, the statement holds for k+1k + 1, completing the proof.