The Hanoi Tower Problem - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

The Hanoi Tower Problem

Description:

Problem: There are n disks of different diameters on three pegs, labeled as Peg ... all n disks are on Peg 1, arranged in an increasing order of their diameter from ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 20
Provided by: Li169
Category:

less

Transcript and Presenter's Notes

Title: The Hanoi Tower Problem


1
The 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.
2
Start
Peg 1
Peg 3
Peg 2
End
Peg 1
Peg 3
Peg 2
3
Solving 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.
4
The 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)
5
Java 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)

6
How 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)
7
How 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)
8
How 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)
9
How 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)
10
How 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)
11
How 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)
12
How 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)
13
How 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)
14
How 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)
15
How 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)
16
How 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)
17
How 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)
18
How 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)
19
How 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)
Write a Comment
User Comments (0)
About PowerShow.com