40
Recursion, pt. 3: The Towers of Hanoi

Recursion, pt. 3:

  • Upload
    shirin

  • View
    55

  • Download
    1

Embed Size (px)

DESCRIPTION

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: - PowerPoint PPT Presentation

Citation preview

Page 1: Recursion, pt. 3:

Recursion, pt. 3:

The Towers of Hanoi

Page 2: Recursion, pt. 3:

Towers of Hanoi• One classic recursive problem is that

of the Towers of Hanoi.

Page 3: Recursion, pt. 3:

Towers of Hanoi• The goal in this problem: to move

the entire tower from the left-most peg to the right-most peg.

Page 4: Recursion, pt. 3:

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.

Page 5: Recursion, pt. 3:

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.

Page 6: Recursion, pt. 3:

Towers of Hanoi• Given these rules, how can we solve

the problem?

Page 7: Recursion, pt. 3:

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

Page 8: Recursion, pt. 3:

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

Page 9: Recursion, pt. 3:

Towers of Hanoi• The most simple only way to reach

that stage is from this one…

Page 10: Recursion, pt. 3:

Towers of Hanoi• … which can be reached from this

stage, and so forth.

Page 11: Recursion, pt. 3:

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.

Page 12: Recursion, pt. 3:

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.

Page 13: Recursion, pt. 3:

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.

Page 14: Recursion, pt. 3:

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.

Page 15: Recursion, pt. 3:

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.

Page 16: Recursion, pt. 3:

Recursive Step• Note how the process works for this

case… Destination

SpareSource

Page 17: Recursion, pt. 3:

Recursive Step• …as well as for this case (moving the

top 4 disks)… Destination

SpareSource

Page 18: Recursion, pt. 3:

Recursive Step• …as well as for this case (moving the

top 3 disks)…Source

Destination

Spare

Page 19: Recursion, pt. 3:

Recursive Step• …which gets us here …

Page 20: Recursion, pt. 3:

Recursive Step• …then here …

Page 21: Recursion, pt. 3:

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.

DestinationSpare Source

Page 22: Recursion, pt. 3:

Recursive Step• (Top 4 disks now moved out of the

way)

Page 23: Recursion, pt. 3:

Recursive Step• Move bottom disk

Destination

SpareSource

Page 24: Recursion, pt. 3:

Recursive Step• Now move top 4 disks from spare to

destination DestinationSpare Source

Page 25: Recursion, pt. 3:

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

Page 26: Recursion, pt. 3:

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.

Page 27: Recursion, pt. 3:

Recursive Step• In order to move the full tower from

the left peg to the right…

Page 28: Recursion, pt. 3:

Recursive Step• …we move the top n-1 to the spare

tower.– Thus, the spare and destination towers

get interchanged, conceptually.

Page 29: Recursion, pt. 3:

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.

Page 30: Recursion, pt. 3:

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

Page 31: Recursion, pt. 3:

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.

Page 32: Recursion, pt. 3:

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.

Page 33: Recursion, pt. 3:

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.

Page 34: Recursion, pt. 3:

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.

Page 35: Recursion, pt. 3:

Towers of Hanoi• The towers are effectively stacks!– Yeah, there’s a reason I’ve been talking

about “stacks of rings.”

Page 36: Recursion, pt. 3:

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.

Page 37: Recursion, pt. 3:

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.

Page 38: Recursion, pt. 3:

The Core Algorithmvoid 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?

Page 39: Recursion, pt. 3:

The Core Algorithmvoid 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);

}

Page 40: Recursion, pt. 3:

Homework• Implement Towers of Hanoi as a

group– Display tower states at each step– Allow for single step– Due Wednesday