Tower of Hanoi Problem in DSA Python - Time & Space Complexity
We want to understand how the time needed to solve the Tower of Hanoi grows as we add more disks.
How does the number of moves change when the number of disks increases?
Analyze the time complexity of the recursive Tower of Hanoi solution.
def tower_of_hanoi(n, source, target, auxiliary):
if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n-1, source, auxiliary, target)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n-1, auxiliary, target, source)
This code moves n disks from one peg to another using recursion.
Look at the recursive calls that repeat the same process for smaller numbers of disks.
- Primary operation: Recursive calls to move n-1 disks.
- How many times: Each call makes two recursive calls plus one move.
Each time we add a disk, the number of moves roughly doubles and then adds one more move.
| Input Size (n) | Approx. Operations (Moves) |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 7 |
| 4 | 15 |
| 5 | 31 |
Pattern observation: The moves grow exponentially, doubling each time plus one extra move.
Time Complexity: O(2^n)
This means the time needed grows very fast, doubling with each additional disk.
[X] Wrong: "The time grows linearly because we just move disks one by one."
[OK] Correct: Each disk move depends on moving smaller stacks twice, so the total moves double each time, not just add one.
Understanding this problem helps you think about recursion and exponential growth, which appear often in coding challenges.
"What if we changed the problem to move disks using four pegs instead of three? How would the time complexity change?"
Practice
Solution
Step 1: Understand the formula for minimum moves
The minimum moves required for n disks is 2^n - 1.Step 2: Calculate for 3 disks
For 3 disks, minimum moves = 2^3 - 1 = 8 - 1 = 7.Final Answer:
7 -> Option BQuick Check:
2^3 - 1 = 7 [OK]
- Using n instead of 2^n - 1
- Calculating 2*n - 1 instead of 2^n - 1
- Confusing number of disks with moves
Solution
Step 1: Identify base case for recursion
The base case stops recursion when only one disk is left, i.e., n == 1.Step 2: Understand base case action
When n == 1, the disk is moved directly from source to target.Final Answer:
if n == 1: move disk -> Option DQuick Check:
Base case is n == 1 [OK]
- Using n == 0 as base case incorrectly
- Not defining a base case causing infinite recursion
- Using invalid conditions like n < 0
def hanoi(n, source, target, auxiliary):
if n == 0:
return
hanoi(n-1, source, auxiliary, target)
print(f"Move disk {n} from {source} to {target}")
hanoi(n-1, auxiliary, target, source)
hanoi(2, 'A', 'C', 'B')Solution
Step 1: Trace recursive calls for n=2
First, move disk 1 from A to B, then disk 2 from A to C, then disk 1 from B to C.Step 2: Verify print statements order
Prints match the moves: disk 1 A->B, disk 2 A->C, disk 1 B->C.Final Answer:
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C -> Option AQuick Check:
Correct move sequence for 2 disks [OK]
- Mixing source and auxiliary rods
- Printing moves in wrong order
- Confusing disk numbers
def hanoi(n, source, target, auxiliary):
if n == 1:
print(f"Move disk {n} from {source} to {target}")
else:
hanoi(n-1, source, target, auxiliary)
print(f"Move disk {n} from {source} to {target}")
hanoi(n-1, auxiliary, target, source)Solution
Step 1: Analyze recursive calls
The first recursive call incorrectly moves disks from source to target instead of auxiliary.Step 2: Correct the first recursive call
It should be hanoi(n-1, source, auxiliary, target) to move disks to auxiliary rod first.Final Answer:
Change first recursive call to hanoi(n-1, source, auxiliary, target) -> Option AQuick Check:
Correct recursive call order fixes the error [OK]
- Calling recursion with wrong target rod
- Changing base case incorrectly
- Swapping print statements unnecessarily
Solution
Step 1: Apply minimum moves formula for 3 rods
Minimum moves for n disks with 3 rods is 2^n - 1, so for 4 disks: 2^4 - 1 = 15.Step 2: Understand why moves can't be less
With 3 rods, no shortcut exists; all disks must be moved following rules, so 15 is minimum.Final Answer:
15 moves, because 2^4 - 1 is the minimum for 3 rods -> Option CQuick Check:
Minimum moves = 2^n - 1 for 3 rods [OK]
- Assuming fewer moves possible with 3 rods
- Adding extra moves unnecessarily
- Confusing number of rods with moves
Ready to prove it?
Pass the test and this pattern moves unknown
