Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.

Similar presentations


Presentation on theme: "Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi."— 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: 1.A larger ring may never be on top of a smaller ring. 2.Only one ring may be moved at a time.

5 Towers of Hanoi Rules: 3.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… 1.Move the top n-1 rings from the source to the spare tower. 2.Move the bottom (the leftover 1 ring) from the source to the destination tower. 3.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. – The difference is that the “destination” tower and “spare” are different here than it was for the other situations.

19 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.

20 Recursive Step In order to move the full tower from the left peg to the right…

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

22 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.

23 Pseudocode void hanoi(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.

24 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.

25 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.

26 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.

27 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.

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

29 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.

30 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.

31 The Core Algorithm void hanoi(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?

32 The Core Algorithm void hanoi(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); }

33 Homework Implement Towers of Hanoi as a group – Display tower states at each step – Allow for single step – Due Wednesday

34 An Aside Regarding a few things you’ll see in the code… – Note the “callback” parameter. It’s a parameter that receives a function (pointer) as an argument! How about that syntax for the parameter? – We’ll cover this (and other, similar techniques) more formally at a later time.


Download ppt "Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi."

Similar presentations


Ads by Google