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) holds for all positive integers n≥n0.
The Principle of Mathematical Induction
- Basis Step (Base Case): Verify that P(n0) is true (usually for n0=1 or 0).
- Inductive Hypothesis: Assume that P(k) is true for an arbitrary integer k≥n0.
- Inductive Step: Prove that if P(k) is true, then P(k+1) must also be true:
P(k)⟹P(k+1)
By mathematical induction, P(n) is true for all integers n≥n0.
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 (A), Auxiliary (B), and Destination (C), along with n disks of distinct sizes initially stacked on Peg A in decreasing order of size (largest at the bottom).
- Goal: Move all n disks from the source peg (A) to the destination peg (C).
- Constraints:
- Only one disk may be moved at a time.
- Only the top disk on any peg can be removed and transferred.
- No larger disk may ever be placed on top of a smaller disk.
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=3, Minimum Moves =23−1=7)
| Move # | Disk | From Peg | To Peg | State After Move (A,B,C) |
|---|
| 0 | - | - | - | A=[3,2,1],B=[],C=[] |
| 1 | Disk 1 | A | C | A=[3,2],B=[],C=[1] |
| 2 | Disk 2 | A | B | A=[3],B=[2],C=[1] |
| 3 | Disk 1 | C | B | A=[3],B=[2,1],C=[] |
| 4 | Disk 3 | A | C | A=[],B=[2,1],C=[3] |
| 5 | Disk 1 | B | A | A=[1],B=[2],C=[3] |
| 6 | Disk 2 | B | C | A=[1],B=[],C=[3,2] |
| 7 | Disk 1 | A | C | A=[],B=[],C=[3,2,1] |
4-Disk Trace (n=4, Minimum Moves =24−1=15)
| Move # | Disk | Action | Description |
|---|
| 1 | Disk 1 | A→B | Move disk 1 out of the way |
| 2 | Disk 2 | A→C | Move disk 2 |
| 3 | Disk 1 | B→C | Stack disk 1 on 2 |
| 4 | Disk 3 | A→B | Move disk 3 to peg B |
| 5 | Disk 1 | C→A | Unstack disk 1 |
| 6 | Disk 2 | C→B | Stack disk 2 on 3 |
| 7 | Disk 1 | A→B | Subproblem n=3 to peg B complete |
| 8 | Disk 4 | A→C | Largest disk 4 moves to final destination C |
| 9 | Disk 1 | B→C | Begin transferring n=3 from B to C |
| 10 | Disk 2 | B→A | Move disk 2 |
| 11 | Disk 1 | C→A | Stack disk 1 on 2 |
| 12 | Disk 3 | B→C | Move disk 3 to C |
| 13 | Disk 1 | A→B | Unstack disk 1 |
| 14 | Disk 2 | A→C | Stack disk 2 on 3 |
| 15 | Disk 1 | B→C | All 4 disks successfully on destination peg C |
Recurrence Relation and Proof
Let M(n) be the number of moves needed for n disks.
From the recursive algorithm:
M(n)=2M(n−1)+1for n>1,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=0∑n−22i=2n−1(1)+(2n−1−1)=2n−1
Proof by Mathematical Induction
- Base Case (n=1): M(1)=21−1=1. The formula holds.
- Inductive Hypothesis: Assume M(k)=2k−1 for some integer k≥1.
- Inductive Step: Show M(k+1)=2k+1−1:
M(k+1)=2M(k)+1=2(2k−1)+1(by inductive hypothesis)=2k+1−2+1=2k+1−1
Thus, the formula holds for k+1. By induction, the minimum number of moves for n disks is exactly 2n−1∈Θ(2n).
- Time Complexity: Θ(2n)
- Space Complexity (Recursion Depth): Θ(n)
Exercises
Exercise 1
If a computer can execute 109 moves per second, how long would it take to solve the Tower of Hanoi for n=64 disks?
Show solution ↓Hide solution ↑
Solution
Total moves required:
264−1≈1.84467×1019 movesTime in seconds:
1091.84467×1019≈1.84467×1010 secondsConverting to years (1 year≈3.1536×107 s):
Time≈3.1536×1071.84467×1010≈584.9 yearsEven at 1 billion moves per second, it would take over 584 years!
Exercise 2
Prove by mathematical induction that for all n≥1:
1+3+5+⋯+(2n−1)=n2Show solution ↓Hide solution ↑
Solution
- Base Case (n=1): 2(1)−1=1, and 12=1. Holds.
- Inductive Hypothesis: Assume 1+3+⋯+(2k−1)=k2 for k≥1.
- Inductive Step: Show the equation holds for k+1:
[1+3+⋯+(2k−1)]+(2(k+1)−1)=k2+(2k+1)=(k+1)2Therefore, the statement holds for k+1, completing the proof.