Upload
chandan-sharma
View
227
Download
0
Embed Size (px)
Citation preview
8/2/2019 15925 Hanoi Towers
1/33
The Towers of HanoiThe Towers of Hanoi
8/2/2019 15925 Hanoi Towers
2/33
A LegendA Legend
Legend has it that there were three diamond needles setLegend has it that there were three diamond needles set
into the floor of the temple of Brahma in Hanoi.into the floor of the temple of Brahma in Hanoi.
Stacked upon the leftmost needle were 64 golden disks,Stacked upon the leftmost needle were 64 golden disks,each a different size, stacked in concentric order:each a different size, stacked in concentric order:
8/2/2019 15925 Hanoi Towers
3/33
A Legend (A Legend (CtdCtd))
The priests were to transfer the disks from the first needleThe priests were to transfer the disks from the first needle
to the second needle, using the third as necessary.to the second needle, using the third as necessary.
But they couldBut they could only moveonly moveone disk at a timeone disk at a time, and could, and could
never put a larger disk on top of a smaller onenever put a larger disk on top of a smaller one..
When they completed this task,When they completed this task, the world would endthe world would end!!
8/2/2019 15925 Hanoi Towers
4/33
To IllustrateTo Illustrate
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
Since we can only move one disk at a time, we move theSince we can only move one disk at a time, we move the
top disk from A to B.top disk from A to B.
8/2/2019 15925 Hanoi Towers
5/33
ExampleExample
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
We then move the top disk from A to C.We then move the top disk from A to C.
8/2/2019 15925 Hanoi Towers
6/33
Example (Example (CtdCtd))
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
We then move the top disk from B to C.We then move the top disk from B to C.
8/2/2019 15925 Hanoi Towers
7/33
Example (Example (CtdCtd))
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
We then move the top disk from A to B.We then move the top disk from A to B.
8/2/2019 15925 Hanoi Towers
8/33
Example (Example (CtdCtd))
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
We then move the top disk from C to A.We then move the top disk from C to A.
8/2/2019 15925 Hanoi Towers
9/33
Example (Example (CtdCtd))
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
We then move the top disk from C to B.We then move the top disk from C to B.
8/2/2019 15925 Hanoi Towers
10/33
Example (Example (CtdCtd))
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
We then move the top disk from A to B.We then move the top disk from A to B.
8/2/2019 15925 Hanoi Towers
11/33
Example (Example (CtdCtd))
For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and well
refer to the three needles as A, B, and C...refer to the three needles as A, B, and C...
and were done!and were done!
The problem gets more difficult as the number of disksThe problem gets more difficult as the number of disks
increases...increases...
8/2/2019 15925 Hanoi Towers
12/33
Our ProblemOur Problem
Todays problem is to write a program that generates theTodays problem is to write a program that generates the
instructions for the priests to follow in moving the disks.instructions for the priests to follow in moving the disks.
While quite difficult to solve iteratively, this problem hasWhile quite difficult to solve iteratively, this problem has
a simple and eleganta simple and elegant recursiverecursivesolution.solution.
8/2/2019 15925 Hanoi Towers
13/33
AnalysisAnalysis
For flexibility, lets allow the user to enter the number ofFor flexibility, lets allow the user to enter the number of
disks for which they wish a set of instructions:disks for which they wish a set of instructions:
/* hanoi.cpp* ...*/
void Move(int n, char src, char dest, char aux);
int main(){
cout numDisks;
Move(numDisks, A, B, C);}
8/2/2019 15925 Hanoi Towers
14/33
Analysis (Ctd)Analysis (Ctd)
Our task, then is to write function Move() that does allOur task, then is to write function Move() that does all
the work:the work:
/* hanoi.cpp* ...*/
void Move(int n, char src, char dest, char aux);
int main(){
cout numDisks;
Move(numDisks, A, B, C);}
8/2/2019 15925 Hanoi Towers
15/33
DesignDesign
Basis: What is an instance of the problem that is trivial?Basis: What is an instance of the problem that is trivial?
n == 1n == 1
Since this base case could occur when the disk is on anySince this base case could occur when the disk is on any
needle, we simply output the instruction to move theneedle, we simply output the instruction to move the
top disk fromtop disk from srcsrctoto destdest..
8/2/2019 15925 Hanoi Towers
16/33
DesignDesign
Basis: What is an instance of the problem that is trivial?Basis: What is an instance of the problem that is trivial?
n == 1n == 1
Since this base case could occur when the disk is on anySince this base case could occur when the disk is on any
needle, we simply output the instruction to move theneedle, we simply output the instruction to move the
top disk fromtop disk from srcsrctoto destdest..
8/2/2019 15925 Hanoi Towers
17/33
Design (Design (CtdCtd))
Induction Step: n > 1Induction Step: n > 1
How can recursion help us out?How can recursion help us out?
a.a. RecursivelyRecursively move n-1 disks frommove n-1 disks from srcsrctoto auxaux..
8/2/2019 15925 Hanoi Towers
18/33
Design (Design (CtdCtd))
Induction Step: n > 1Induction Step: n > 1
How can recursion help us out?How can recursion help us out?
b. Move the one remaining disk fromb. Move the one remaining disk from srcsrctoto destdest..
8/2/2019 15925 Hanoi Towers
19/33
Design (Design (CtdCtd))
Induction Step: n > 1Induction Step: n > 1
How can recursion help us out?How can recursion help us out?
c.c. RecursivelyRecursivelymove n-1 disks frommove n-1 disks from auxauxtoto destdest......
8/2/2019 15925 Hanoi Towers
20/33
Design (Design (CtdCtd))
Induction Step: n > 1Induction Step: n > 1
How can recursion help us out?How can recursion help us out?
d. Were done!d. Were done!
8/2/2019 15925 Hanoi Towers
21/33
AlgorithmAlgorithm
We can combine these steps into the following algorithm:We can combine these steps into the following algorithm:
0.0. ReceiveReceive n, src, dest, auxn, src, dest, aux..
1.1. IfIfnn> 1:> 1:
a. Move(a. Move(n-1, src, aux, destn-1, src, aux, dest););
b. Move(1,b. Move(1, src, dest, auxsrc, dest, aux););
c. Move(c. Move(n-1, aux, dest, srcn-1, aux, dest, src););
ElseElse
Display Move the top disk from ,Display Move the top disk from , srcsrc, to ,, to , destdest..
End if.End if.
8/2/2019 15925 Hanoi Towers
22/33
CodingCoding
// ...
void Move(int n, char src, char dest, char aux){if (n > 1){
Move(n-1, src, aux, dest);Move(1, src, dest, aux);Move(n-1, aux, dest, src);
}elsecout
8/2/2019 15925 Hanoi Towers
23/33
Recursion tree:
The order of recursive calls that results from
solveTowers(3,A,B,C)
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
24/33
[email protected] AA BB CC
AA BB CC
AA BB CC
Figure 2.21aFigure 2.21a
Box trace ofsolveTowers(3, A, B, C)
AA BB CC
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
25/33
Figure 2.21bFigure 2.21b
Box trace ofsolveTowers(3, A, B, C)
AA BB CC
AA BB CC
AA BB CC
AA BB CC
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
26/33
Figure 2.21cFigure 2.21c
Box trace ofsolveTowers(3, A, B, C)
AA BB CC
AA BB CC
AA BB CC
AA BB CC
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
27/33
Figure 2.21dFigure 2.21d
Box trace ofsolveTowers(3, A, B, C)
AA BB CC
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
28/33
Figure 2.21eFigure 2.21e
Box trace ofsolveTowers(3, A, B, C)
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
29/33
TestingTestingThe Hanoi Towers
Enter how many disks: 1Move the top disk from A to B
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
30/33
Testing (Testing (CtdCtd))The Hanoi Towers
Enter how many disks: 2Move the top disk from A to CMove the top disk from A to BMove the top disk from C to B
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
31/33
Testing (Testing (CtdCtd))The Hanoi Towers
Enter how many disks: 3Move the top disk from A to BMove the top disk from A to CMove the top disk from B to CMove the top disk from A to B
Move the top disk from C to AMove the top disk from C to BMove the top disk from A to B
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
32/33
Testing (Testing (CtdCtd))The Hanoi Towers
Enter how many disks: 4
move a disk from needle A to needle B
move a disk from needle C to needle B
move a disk from needle A to needle C
move a disk from needle B to needle Amove a disk from needle B to needle C
move a disk from needle A to needle C
move a disk from needle A to needle B
move a disk from needle C to needle B
move a disk from needle C to needle A
move a disk from needle B to needle Amove a disk from needle C to needle B
move a disk from needle A to needle C
move a disk from needle A to needle B
move a disk from needle C to needle B
mailto:[email protected]:[email protected]8/2/2019 15925 Hanoi Towers
33/33
adams@calvin edu
AnalysisAnalysis
Lets see how many moves it takes to solve this problem,Lets see how many moves it takes to solve this problem,as a function ofas a function ofnn, the number of disks to be moved., the number of disks to be moved.
nn Number of disk-moves requiredNumber of disk-moves required
11 1122 33
33 77
44 1515
55 3131
......
ii 22ii-1-1
6464 226464 1 (a big number)1 (a big number)
mailto:[email protected]:[email protected]