Your SlideShare is downloading. ×
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Dc8c4f010f40.hanoi.towers

1,056

Published on

Published in: Technology, Travel
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,056
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×