Title: The Hanoi Tower Problem
1The Hanoi Tower Problem
Problem There are n disks of different
diameters on three pegs, labeled as Peg 1, Peg 2,
as Peg 3. At the beginning, all n disks are on
Peg 1, arranged in an increasing order of their
diameter from top down. The question is to move
all disks to Peg 3 according to the following
rules 1. Each time, only one disk on top of a
peg is moved to the top of another peg. 2. No
disk can be put on top of a smaller disk.
2Start
Peg 1
Peg 3
Peg 2
End
Peg 1
Peg 3
Peg 2
3Solving Idea
The base case If there is no disk (n 0),
nothing has to be done. Reduction Now suppose
the number of disks is al least one. Ignore the
largest disk, we have n 1 disks. A smaller
case. Recursion The smaller case can be solved
by a recursive call to the same algorithm since
this reduction can eventually be reduced to the
base case. Building up the solution Move n
1disks from the source peg to the intermediate
peg, move one disk from the source peg to the
destination peg, and finally, move n 1 disks
from the intermediate peg to the destination peg.
4The Algorithm
GIVEN N (a positive integer) S, I, D (the
source peg, intermediate peg, and the destination
peg) RESULT Output the steps of
movements HEADER Hanoi (N, S, I, D) BODY test
N gt 0? false true Ø Hanoi (N -
1, S, D, I) output "S -gt D" Hanoi (N - 1,
I, S, D)
5Java Implementation
public static void hanoi (int n, int s, int i,
int d) if (n gt 0) hanoi (n -
1, s, d, i) System.out.println (s "
-gt " d) hanoi (n - 1, i, s, d)
6How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3)
7How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2)
8How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
9How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
10How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
11How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
12How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
13How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
14How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
15How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
16How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
17How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
18How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
19How it works when n 3, s 1, i 2, d 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -gt
3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -gt 2
hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -gt 3
hanoi(1,1,2,3) hanoi(0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)
hanoi(0,1,3,2) 1 -gt 3 3 -gt 2 2 -gt 1 1
-gt 3 hanoi(0,2,1,3) hanoi (0,1,3,2)
hanoi(0,3,2,1) hanoi(0,2,1,3)