Tower of Hanoi Problem in DSA Rust - 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.
fn tower_of_hanoi(n: u32, from: char, to: char, aux: char) {
if n == 1 {
println!("Move disk 1 from {} to {}", from, to);
return;
}
tower_of_hanoi(n - 1, from, aux, to);
println!("Move disk {} from {} to {}", n, from, to);
tower_of_hanoi(n - 1, aux, to, from);
}
This code moves n disks from one peg to another using recursion and an auxiliary peg.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls that break the problem into smaller parts.
- How many times: Each call makes two smaller recursive calls plus one move operation.
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 we add a disk.
Time Complexity: O(2^n)
This means the time needed doubles with each additional disk, growing very fast as n increases.
[X] Wrong: "The time grows linearly because we just move disks one by one."
[OK] Correct: Each disk move depends on moving smaller stacks multiple times, causing exponential growth, not just a simple count.
Understanding this problem helps you think about recursion and exponential growth, skills useful for many algorithm challenges.
"What if we used iteration instead of 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 AQuick Check:
2^3 - 1 = 7 [OK]
- Calculating 2*n - 1 instead of 2^n - 1
- Confusing number of disks with moves
- Choosing the next integer after 7
Solution
Step 1: Identify correct parameter types
The function needs number of disks (u32) and three rods (chars) to represent pegs.Step 2: Check function signature options
fn tower_of_hanoi(n: u32, from: char, to: char, aux: char) uses u32 for disks and char for rods, matching common Tower of Hanoi implementations.Final Answer:
fn tower_of_hanoi(n: u32, from: char, to: char, aux: char) -> Option BQuick Check:
Correct parameter types and count [OK]
- Using String instead of char for rods
- Missing auxiliary rod parameter
- Using signed integer for disk count
tower_of_hanoi(2, 'A', 'C', 'B')?Solution
Step 1: Understand the recursive steps for 2 disks
Move disk 1 from source to auxiliary, move disk 2 from source to target, then move disk 1 from auxiliary to target.Step 2: Apply to given rods
From 'A' to 'B' (aux), then 'A' to 'C' (target), then 'B' to 'C' (target).Final Answer:
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C -> Option CQuick Check:
Correct move order for 2 disks [OK]
- Swapping auxiliary and target rods
- Reversing move order
- Confusing disk numbers
fn tower_of_hanoi(n: u32, from: char, to: char, aux: char) {
if n == 0 {
return;
}
tower_of_hanoi(n - 1, from, to, aux);
println!("Move disk {} from {} to {}", n, from, to);
tower_of_hanoi(n - 1, aux, to, from);
}Solution
Step 1: Recall standard Tower of Hanoi recursion
To move n disks from src to dst using aux: move n-1 src->aux using dst; move n src->dst; move n-1 aux->dst using src.Step 2: Check first recursive call
Code has tower_of_hanoi(n-1, from, to, aux); but should be tower_of_hanoi(n-1, from, aux, to); dst and aux swapped in first call.Step 3: Other parts correct
Base case n==0 fine for u32; println! syntax correct; second recursive call correct.Final Answer:
The recursive calls have incorrect rod parameters swapped -> Option AQuick Check:
First call: dst/aux swapped [OK]
- Confusing base case condition
- Mixing up source, target, auxiliary rods
- Expecting return value in void function
Solution
Step 1: Understand Tower of Hanoi rod requirements
The classic Tower of Hanoi problem requires 3 rods to move disks following the rules.Step 2: Check if 2 rods suffice
With only 2 rods, it is impossible to move all disks without breaking the rules (no larger disk on smaller disk).Final Answer:
Impossible to solve with 2 rods -> Option DQuick Check:
Minimum rods needed = 3 [OK]
- Applying formula 2^n - 1 without checking rods
- Assuming fewer rods still work
- Confusing rods with disks
Ready to prove it?
Pass the test and this pattern moves unknown
