Upcoming SlideShare
×

# Dc8c4f010f40.hanoi.towers

1,406 views

Published on

Published in: Technology, Travel
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,406
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
48
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Dc8c4f010f40.hanoi.towers

1. 1. The Towers of Hanoi or Apocalypse When ?
2. 2. A Legend <ul><li>Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi. </li></ul>Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order:
3. 3. A Legend ( Ct’d ) <ul><li>The priests were to transfer the disks from the first needle to the second needle, using the third as necessary. </li></ul>But they could only move one disk at a time , and could never put a larger disk on top of a smaller one . When they completed this task, the world would end !
4. 4. To Illustrate <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>Since we can only move one disk at a time, we move the top disk from A to B.
5. 5. Example <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>We then move the top disk from A to C.
6. 6. Example ( Ct’d ) <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>We then move the top disk from B to C.
7. 7. Example ( Ct’d ) <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>We then move the top disk from A to B.
8. 8. Example ( Ct’d ) <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>We then move the top disk from C to A.
9. 9. Example ( Ct’d ) <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>We then move the top disk from C to B.
10. 10. Example ( Ct’d ) <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>We then move the top disk from A to B.
11. 11. Example ( Ct’d ) <ul><li>For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... </li></ul>and we’re done! The problem gets more difficult as the number of disks increases...
12. 12. Our Problem <ul><li>Today’s problem is to write a program that generates the instructions for the priests to follow in moving the disks. </li></ul>While quite difficult to solve iteratively, this problem has a simple and elegant recursive solution.
13. 13. Analysis <ul><li>For flexibility, let’s allow the user to enter the number of disks for which they wish a set of instructions: </li></ul>/* hanoi.cpp * ... */ void Move(int n, char src, char dest, char aux); int main() { cout << “nnThe Hanoi Towers!nn” << “Enter how many disks: “; int numDisks; cin >> numDisks; Move(numDisks, ‘A’, ‘B’, ‘C’); }
14. 14. Analysis (Ct’d) <ul><li>Our task, then is to write function Move() that does all the work: </li></ul>/* hanoi.cpp * ... */ void Move(int n, char src, char dest, char aux); int main() { cout << “nnThe Hanoi Towers!nn” << “Enter how many disks: “; int numDisks; cin >> numDisks; Move(numDisks, ‘A’, ‘B’, ‘C’); }
15. 15. Design <ul><li>Basis: What is an instance of the problem that is trivial? </li></ul><ul><li> n == 1 </li></ul>Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest .
16. 16. Design <ul><li>Basis: What is an instance of the problem that is trivial? </li></ul><ul><li> n == 1 </li></ul>Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest .
17. 17. Design ( Ct’d ) <ul><li>Induction Step: n > 1 </li></ul><ul><li> How can recursion help us out? </li></ul>a. Recursively move n-1 disks from src to aux .
18. 18. Design ( Ct’d ) <ul><li>Induction Step: n > 1 </li></ul><ul><li> How can recursion help us out? </li></ul>b. Move the one remaining disk from src to dest .
19. 19. Design ( Ct’d ) <ul><li>Induction Step: n > 1 </li></ul><ul><li> How can recursion help us out? </li></ul>c. Recursively move n-1 disks from aux to dest ...
20. 20. Design ( Ct’d ) <ul><li>Induction Step: n > 1 </li></ul><ul><li> How can recursion help us out? </li></ul>d. We’re done!
21. 21. Algorithm <ul><li>We can combine these steps into the following algorithm: </li></ul><ul><ul><li>0. Receive n, src, dest, aux . </li></ul></ul><ul><ul><li>1. If n > 1: </li></ul></ul><ul><ul><li>a. Move( n-1, src, aux, dest ); </li></ul></ul><ul><ul><li>b. Move(1, src, dest, aux ); </li></ul></ul><ul><ul><li>c. Move( n-1, aux, dest, src ); </li></ul></ul><ul><ul><li>Else </li></ul></ul><ul><ul><li>Display “Move the top disk from “, src , “ to “, dest . </li></ul></ul><ul><ul><li>End if. </li></ul></ul>
22. 22. Coding <ul><li>// ... </li></ul><ul><li>void Move(int n, char src, char dest, char aux) </li></ul><ul><li>{ </li></ul><ul><li>if (n > 1) </li></ul><ul><li>{ </li></ul><ul><li>Move(n-1, src, aux, dest); </li></ul><ul><li>Move(1, src, dest, aux); </li></ul><ul><li>Move(n-1, aux, dest, src); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>cout << “Move the top disk from “ </li></ul><ul><li><< src << “ to “ << dest << endl; </li></ul><ul><li>} </li></ul>
23. 23. Testing <ul><li>The Hanoi Towers </li></ul><ul><li>Enter how many disks: 1 </li></ul><ul><li>Move the top disk from A to B </li></ul>
24. 24. Testing ( Ct’d ) <ul><li>The Hanoi Towers </li></ul><ul><li>Enter how many disks: 2 </li></ul><ul><li>Move the top disk from A to C </li></ul><ul><li>Move the top disk from A to B </li></ul><ul><li>Move the top disk from C to B </li></ul>
25. 25. Testing ( Ct’d ) <ul><li>The Hanoi Towers </li></ul><ul><li>Enter how many disks: 3 </li></ul><ul><li>Move the top disk from A to B </li></ul><ul><li>Move the top disk from A to C </li></ul><ul><li>Move the top disk from B to C </li></ul><ul><li>Move the top disk from A to B </li></ul><ul><li>Move the top disk from C to A </li></ul><ul><li>Move the top disk from C to B </li></ul><ul><li>Move the top disk from A to B </li></ul>
26. 26. Testing ( Ct’d ) <ul><li>The Hanoi Towers </li></ul><ul><li>Enter how many disks: 4 </li></ul><ul><li>move a disk from needle A to needle B </li></ul><ul><li>move a disk from needle C to needle B </li></ul><ul><li>move a disk from needle A to needle C </li></ul><ul><li>move a disk from needle B to needle A </li></ul><ul><li>move a disk from needle B to needle C </li></ul><ul><li>move a disk from needle A to needle C </li></ul><ul><li>move a disk from needle A to needle B </li></ul><ul><li>move a disk from needle C to needle B </li></ul><ul><li>move a disk from needle C to needle A </li></ul><ul><li>move a disk from needle B to needle A </li></ul><ul><li>move a disk from needle C to needle B </li></ul><ul><li>move a disk from needle A to needle C </li></ul><ul><li>move a disk from needle A to needle B </li></ul><ul><li>move a disk from needle C to needle B </li></ul>
27. 27. Analysis <ul><li>Let’s see how many moves” it takes to solve this problem, as a function of n , the number of disks to be moved. </li></ul><ul><ul><li>n Number of disk-moves required </li></ul></ul><ul><ul><li>1 1 </li></ul></ul><ul><ul><li>2 3 </li></ul></ul><ul><ul><li>3 7 </li></ul></ul><ul><ul><li>4 15 </li></ul></ul><ul><ul><li>5 31 </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>i 2 i -1 </li></ul></ul><ul><ul><li>64 2 64 -1 (a big number) </li></ul></ul>
28. 28. Analysis ( Ct’d ) <ul><li>How big? </li></ul><ul><li>Suppose that our computer and “super-printer” can generate and print 1,048,576 (2 20 ) instructions/second. </li></ul><ul><li>How long will it take to print the priest’s instructions? </li></ul><ul><li>There are 2 64 instructions to print. </li></ul><ul><ul><li>Then it will take 2 64 /2 20 = 2 44 seconds to print them. </li></ul></ul><ul><li>1 minute == 60 seconds. </li></ul><ul><ul><li>Let’s take 64 = 2 6 as an approximation of 60. </li></ul></ul><ul><ul><li>Then it will take  2 44 / 2 6 = 2 38 minutes to print them. </li></ul></ul>
29. 29. Analysis ( Ct’d ) <ul><li>Hmm. 2 38 minutes is hard to grasp. Let’s keep going... </li></ul><ul><li>1 hour == 60 minutes. </li></ul><ul><ul><li>Let’s take 64 = 2 6 as an approximation of 60. </li></ul></ul><ul><ul><li>Then it will take  2 38 / 2 6 = 2 32 hours to print them. </li></ul></ul><ul><li>1 day == 24 hours. </li></ul><ul><ul><li>Let’s take 32 = 2 5 as an approximation of 24. </li></ul></ul><ul><ul><li>Then it will take  2 32 / 2 5 = 2 27 days to print them. </li></ul></ul>
30. 30. Analysis ( Ct’d ) <ul><li>Hmm. 2 27 days is hard to grasp. Let’s keep going... </li></ul><ul><li>1 year == 365 days. </li></ul><ul><ul><li>Let’s take 512 = 2 9 as an approximation of 365. </li></ul></ul><ul><ul><li>Then it will take  2 27 / 2 9 = 2 18 years to print them. </li></ul></ul><ul><li>1 century == 100 years. </li></ul><ul><ul><li>Let’s take 128 = 2 7 as an approximation of 100. </li></ul></ul><ul><ul><li>Then it will take  2 18 / 2 7 = 2 11 centuries to print them. </li></ul></ul>
31. 31. Analysis ( Ct’d ) <ul><li>Hmm. 2 11 centuries is hard to grasp. Let’s keep going... </li></ul><ul><li>1 millenium == 10 centuries. </li></ul><ul><ul><li>Let’s take 16 = 2 4 as an approximation of 10. </li></ul></ul><ul><ul><li>Then it will take  2 11 / 2 4 = 2 7 = 128 millenia just to print the priest’s instructions (assuming our computer doesn’t crash, in which case we have to start all over again). </li></ul></ul><ul><li>How fast can the priests actually move the disks? </li></ul><ul><li>I’ll leave it to you to calculate the data of the apocalypse... </li></ul>
32. 32. Summary <ul><li>Recursion is a valuable tool that allows some problems to be solved in an elegant and efficient manner. </li></ul><ul><li>Functions can sometimes require more than one recursive call in order to accomplish their task. </li></ul><ul><li>There are problems for which we can design a solution, but the nature of the problem makes solving it effectively uncomputable . </li></ul>