- Asked 2/14
- 2071 Bhadra · 4 marks
- 2069 Bhadra · 4 marks
Solve TOH for 3 disks
Short answer
For 3 disks (1 smallest, 3 largest) from A to C using B: move 1 A→C, 2 A→B, 1 C→B, 3 A→C, 1 B→A, 2 B→C, 1 A→C. This follows the recursive pattern: top 2 to B, largest to C, then 2 to C. Total moves 7.
Explain the TOH problem and show the steps to solve it using recursion for 3 disks.
Problem
Three pegs A (source), B (auxiliary), C (destination); n disks on A, smallest on top. Move all to C, one disk at a time, only the top disk of a peg, and never a larger disk on a smaller one.
Recursive idea
TOH(n, from, to, aux): (1) TOH(n-1, from, aux, to); (2) move disk n from from to to; (3) TOH(n-1, aux, to, from). For n = 1 just move the disk.
Steps for 3 disks (1 smallest, 3 largest)
TOH(3,A,C,B) = TOH(2,A,B,C), move 3: A→C, TOH(2,B,C,A)
- TOH(2,A,B,C) = TOH(1,A,C,B) [1: A→C], 2: A→B, TOH(1,C,B,A) [3: C→B]
- TOH(2,B,C,A) = TOH(1,B,A,C) [5: B→A], 6: B→C, TOH(1,A,C,B) [7: A→C]
| Move | Disk | From | To | Pegs after the move (A / B / C) |
|---|---|---|---|---|
| 1 | 1 | A | C | 3,2 / - / 1 |
| 2 | 2 | A | B | 3 / 2 / 1 |
| 3 | 1 | C | B | 3 / 2,1 / - |
| 4 | 3 | A | C | - / 2,1 / 3 |
| 5 | 1 | B | A | 1 / 2 / 3 |
| 6 | 2 | B | C | 1 / - / 3,2 |
| 7 | 1 | A | C | - / - / 3,2,1 |
Start : A: 3 2 1 | B: - | C: -
Move 4 : A: - | B: 2 1 | C: 3 (largest disk placed)
Finish : A: - | B: - | C: 3 2 1
Total moves = 2³ − 1 = 7.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Tree recursion and TOH with n disks · Data Structure and Algorithm, chapter 5
- TOH problem with solution for n disks · Data Structure and Algorithm, chapter 5 · asked 2×
- TOH for 3 disks and recursion principle · Data Structure and Algorithm, chapter 5
- TOH algorithm and recursion tree · Data Structure and Algorithm, chapter 5 · asked 2×
- Recursion properties, Fibonacci, TOH algorithm · Data Structure and Algorithm, chapter 5
- Recursion, Fibonacci and TOH · Data Structure and Algorithm, chapter 5