Tower of Hanoi Problem in DSA C++ - 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 following code snippet.
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
// Move disk 1 from from_rod to to_rod
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
// Move disk n from from_rod to to_rod
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
This code moves n disks from one rod to another using recursion and an auxiliary rod.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls that move smaller stacks of disks.
- How many times: Each call makes two recursive calls with n-1 disks, plus one move.
Each time we add a disk, the number of moves roughly doubles and 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 we add a disk.
Time Complexity: O(2^n)
This means the time needed doubles with each extra disk, growing very fast as n increases.
[X] Wrong: "The time grows linearly with the number of disks because we just move each disk once."
[OK] Correct: Actually, disks are moved many times in smaller groups, so the total moves double with each added disk, not just increase by one.
Understanding this problem helps you think about recursion and exponential growth, which are common in many coding challenges.
"What if we changed the problem to move disks iteratively without recursion? 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 DQuick 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 the base case for recursion
The base case is when there is only 1 disk to move, so move it directly.Step 2: Check the options
if (n == 1) move disk from source to destination; correctly states the base case: if (n == 1) move disk from source to destination.Final Answer:
if (n == 1) move disk from source to destination; -> Option AQuick Check:
Base case is n == 1 [OK]
- Using n == 0 as base case
- Not defining a base case
- Using incorrect condition like n < 0
Solution
Step 1: Understand the recursive move sequence for 2 disks
Move top disk (1) from A to B, move bottom disk (2) from A to C, then move disk (1) from B to C.Step 2: Match the sequence with options
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C matches the correct sequence exactly.Final Answer:
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C -> Option BQuick Check:
Correct move order for 2 disks [OK]
- Swapping auxiliary and destination pegs
- Moving larger disk before smaller disk
- Incorrect move order
void towerOfHanoi(int n, char source, char aux, char dest) {
if (n == 1) {
cout << "Move disk 1 from " << source << " to " << dest << endl;
return;
}
towerOfHanoi(n - 1, source, dest, aux);
cout << "Move disk " << n << " from " << source << " to " << dest << endl;
towerOfHanoi(n - 1, aux, source, dest);
}Solution
Step 1: Analyze recursive calls parameters
The first recursive call should move n-1 disks from source to auxiliary using destination as auxiliary, so parameters should be (n-1, source, dest, aux).Step 2: Identify incorrect parameter order
In the code, the first call uses (n-1, source, aux, dest) which swaps aux and dest incorrectly.Final Answer:
The order of auxiliary and destination pegs is swapped in recursive calls. -> Option AQuick Check:
Correct peg order is crucial in recursion [OK]
- Swapping auxiliary and destination pegs
- Wrong base case condition
- Missing return statements
Solution
Step 1: Understand the classic formula for 3 pegs
For 3 pegs, minimum moves = 2^n - 1.Step 2: Consider 4 pegs case
With 4 pegs, the problem is more complex; fewer moves than 2^n - 1 are possible but no simple closed formula exists.Final Answer:
It is always less than 2^n - 1 moves but no exact formula is known. -> Option CQuick Check:
4 pegs reduce moves but formula unknown [OK]
- Assuming formula 2^n - 1 applies to 4 pegs
- Thinking more pegs increase moves
- Assuming quadratic moves for 4 pegs
Ready to prove it?
Pass the test and this pattern moves unknown
