1 / 40

Recursion, pt. 3:

Recursion, pt. 3:. The Towers of Hanoi. Towers of Hanoi. One classic recursive problem is that of the Towers of Hanoi. Towers of Hanoi. The goal in this problem: to move the entire tower from the left-most peg to the right-most peg. Towers of Hanoi. Rules:

shirin
Download Presentation

Recursion, pt. 3:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion, pt. 3: The Towers of Hanoi

  2. Towers of Hanoi • One classic recursive problem is that of the Towers of Hanoi.

  3. Towers of Hanoi • The goal in this problem: to move the entire tower from the left-most peg to the right-most peg.

  4. Towers of Hanoi • Rules: • A larger ring may never be on top of a smaller ring. • Only one ring may be moved at a time.

  5. Towers of Hanoi • Rules: • Rings may only be removed from the top of one tower and placed on the top of a second tower.

  6. Towers of Hanoi • Given these rules, how can we solve the problem?

  7. Towers of Hanoi • To move the tower fully to the right…

  8. Recursive Step • we’ll have to go through this stage.

  9. Towers of Hanoi • The most simple only way to reach that stage is from this one…

  10. Towers of Hanoi • … which can be reached from this stage, and so forth.

  11. Recursive Step • What common property do these moves have? • Our goal is first to move the bottom-most ring of the stack to the destination tower. • To do this, we must move the rest of the stack to the spare tower.

  12. Recursive Step • Note what we’re saying here. • “To do this, we must move the rest of the stack to the spare tower.” • In other words, to move the entire stack from the left to the right tower… will require the ability to move almost the entire stack from the left to the spare tower.

  13. Recursive Step • To move the stack of rings… requires being able to move (a smaller portion of) the stack. • Note that this involves a reduction – our stack size will reduce by one upon each such reduction.

  14. Recursive Step • Combining these two facts together, we have a perfect candidate for recursion. • We can solve the problem in terms of a reduced form of itself. • We’ll move the stack from one tower to another by… moving (most of) the stack from one tower to another. Recursion.

  15. Recursive Step • So, in order… • Move the top n-1 rings from the source to the spare tower. • Move the bottom (the leftover 1 ring) from the source to the destination tower. • Relocate those n-1 rings we had placed on the spare tower over to the destination tower.

  16. Recursive Step • Note how the process works for this case… Destination Spare Source

  17. Recursive Step • …as well as for this case (moving the top 4 disks)… Destination Spare Source

  18. Recursive Step • …as well as for this case (moving the top 3 disks)… Destination Spare Source

  19. Recursive Step • …which gets us here …

  20. Recursive Step • …then here …

  21. Recursive Step • …as well as for this case. • The difference is that the “destination” tower and “spare” are different here than it was for the other situations. Source Destination Spare

  22. Recursive Step • (Top 4 disks now moved out of the way)

  23. Recursive Step • Move bottom disk Destination Spare Source

  24. Recursive Step • Now move top 4 disks from spare to destination Destination Spare Source

  25. Towers of Hanoi • To move the tower fully to the right

  26. Recursive Step • So, while we know the general idea for reducing the problem, there’s the problem of towers serving different purposes at different stages.

  27. Recursive Step • In order to move the full tower from the left peg to the right…

  28. Recursive Step • …we move the top n-1 to the spare tower. • Thus, the spare and destination towers get interchanged, conceptually.

  29. Towers of Hanoi • The method needs to access all of the towers at every frame. • What changes is the name by which we will reference each tower. • Hmm. Reference. • Remember, the changes to each tower should propagate across every frame of the method.

  30. Pseudocode voidhanoi(int n, Tower* src, Tower* spr, Tower* dst) { hanoi(n-1, src, dst, spr); Ring p = src->takeRing(); dst->placeRing(p); hanoi(n-1, spr, src, dst); } • Note: “Ring” and “Tower” haven’t been defined or analyzed yet.

  31. References • Will this work? • This is part of the beauty of passing objects by reference. Changes to each tower will persist, and the name by which we call each Tower will change appropriately for the method.

  32. References • This is a major benefit of using pointers! • The whole “by reference” thing can be troublesome when starting out, since it can cause collateral damage. • However, when references are fully understood, that “collateral” damage may be easily focused, channeled, and controlled for great benefit.

  33. Towers of Hanoi • What’s left to examine? • Ah yes. “Ring” and “Tower” still haven’t yet been defined. • Let’s look at the towers first.

  34. Towers of Hanoi • What would be a proper representation for each tower? • We are placing rings on the top of each tower and removing them from the top.

  35. Towers of Hanoi • The towers are effectively stacks! • Yeah, there’s a reason I’ve been talking about “stacks of rings.”

  36. Towers of Hanoi • The towers are effectively stacks! • We can model this for now by using a vector; elements should only be removed from and added at the vector’s end.

  37. Towers of Hanoi • As for the rings, they could easily be represented by integers, or by a custom class. • For our in-class implementation here, let’s use a custom class.

  38. The Core Algorithm voidhanoi(int n, Tower* src, Tower* spr, Tower* dst) { hanoi(n-1, src, dst, spr); dst->placeRing(src->takeRing()); hanoi(n-1, spr, src, dst); } • Wait… something’s missing… • What’s our base case?

  39. The Core Algorithm voidhanoi(int n, Tower* src, Tower* spr, Tower* dst) { if(n > 1) hanoi(n-1, src, dst, spr); dst->placeRing(src->takeRing()); if(n > 1) hanoi(n-1, spr, src, dst); }

  40. Homework • Implement Towers of Hanoi as a group • Display tower states at each step • Allow for single step • Due Wednesday

More Related