the towers of hanoi or apocalypse when?. a legend legend has it that there were three diamond...

32
The Towers of Hanoi The Towers of Hanoi or or Apocalypse Apocalypse When When ? ?

Upload: deshawn-fear

Post on 29-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

The Towers of HanoiThe Towers of Hanoi

oror

Apocalypse Apocalypse WhenWhen??

Page 2: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

A LegendA Legend

Legend has it that there were three diamond needles Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi.set into the floor of the temple of Brahma in Hanoi.

Stacked upon the leftmost needle were 64 golden disks, Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order:each a different size, stacked in concentric order:

Page 3: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

A Legend (A Legend (Ct’dCt’d))

The priests were to transfer the disks from the first needle to The priests were to transfer the disks from the first needle to the second needle, using the third as necessary.the second needle, using the third as necessary.

But they could But they could only moveonly move one disk at a timeone disk at a time, and could , and could never put a larger disk on never put a larger disk on top of a smaller onetop of a smaller one..

When they completed this task, When they completed this task, the world would endthe world would end!!

Page 4: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

To IllustrateTo Illustrate

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

Since we can only move one disk at a time, we Since we can only move one disk at a time, we move the top disk from A to B.move the top disk from A to B.

Page 5: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

ExampleExample

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

We then move the top disk from A to C.We then move the top disk from A to C.

Page 6: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Example (Example (Ct’dCt’d))

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

We then move the top disk from B to C.We then move the top disk from B to C.

Page 7: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Example (Example (Ct’dCt’d))

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

We then move the top disk from A to B.We then move the top disk from A to B.

Page 8: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Example (Example (Ct’dCt’d))

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

We then move the top disk from C to A.We then move the top disk from C to A.

Page 9: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Example (Example (Ct’dCt’d))

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

We then move the top disk from C to B.We then move the top disk from C to B.

Page 10: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Example (Example (Ct’dCt’d))

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

We then move the top disk from A to B.We then move the top disk from A to B.

Page 11: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Example (Example (Ct’dCt’d))

For simplicity, suppose there were just 3 disks, and For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...we’ll refer to the three needles as A, B, and C...

and we’re done!and we’re done!

The problem gets more difficult as the number of disks increases...The problem gets more difficult as the number of disks increases...

Page 12: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Our ProblemOur Problem

Today’s problem is to write a program that generates the Today’s problem is to write a program that generates the instructions for the priests to follow in moving the disks.instructions for the priests to follow in moving the disks.

While quite difficult to solve iteratively, this problem While quite difficult to solve iteratively, this problem has a simple and elegant has a simple and elegant recursiverecursive solution. solution.

Page 13: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

AnalysisAnalysis

For flexibility, let’s allow the user to enter the number For flexibility, let’s allow the user to enter the number of disks for which they wish a set of instructions: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 << “\n\nThe Hanoi Towers!\n\n” << “Enter how many disks: “; int numDisks; cin >> numDisks;

Move(numDisks, ‘A’, ‘B’, ‘C’);}

Page 14: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Analysis (Ct’d)Analysis (Ct’d)

Our task, then is to write function Move() that does Our task, then is to write function Move() that does all the work:all the work:

/* hanoi.cpp * ... */

void Move(int n, char src, char dest, char aux);

int main(){ cout << “\n\nThe Hanoi Towers!\n\n” << “Enter how many disks: “; int numDisks; cin >> numDisks;

Move(numDisks, ‘A’, ‘B’, ‘C’);}

Page 15: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

DesignDesign

Basis: What is an instance of the problem that is trivial?Basis: What is an instance of the problem that is trivial?

n == 1n == 1

Since this base case could occur when the disk is on any needle, we Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from simply output the instruction to move the top disk from srcsrc to to destdest..

Page 16: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

DesignDesign

Basis: What is an instance of the problem that is trivial?Basis: What is an instance of the problem that is trivial?

n == 1n == 1

Since this base case could occur when the disk is on any needle, we Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from simply output the instruction to move the top disk from srcsrc to to destdest..

Page 17: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Design (Design (Ct’dCt’d))Induction Step: n > 1Induction Step: n > 1

How can recursion help us out?How can recursion help us out?

a. a. RecursivelyRecursively move n-1 disks from move n-1 disks from srcsrc to to auxaux..

Page 18: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Design (Design (Ct’dCt’d))Induction Step: n > 1Induction Step: n > 1

How can recursion help us out?How can recursion help us out?

b. Move the one remaining disk from b. Move the one remaining disk from srcsrc to to destdest..

Page 19: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Design (Design (Ct’dCt’d))Induction Step: n > 1Induction Step: n > 1

How can recursion help us out?How can recursion help us out?

c. c. RecursivelyRecursively move n-1 disks from move n-1 disks from auxaux to to destdest......

Page 20: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Design (Design (Ct’dCt’d))Induction Step: n > 1Induction Step: n > 1

How can recursion help us out?How can recursion help us out?

d. We’re done!d. We’re done!

Page 21: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

AlgorithmAlgorithmWe can combine these steps into the following algorithm:We can combine these steps into the following algorithm:

0.0.Receive Receive n, src, dest, auxn, src, dest, aux..1.1. If If nn > 1: > 1:

a. Move(a. Move(n-1, src, aux, destn-1, src, aux, dest););b. Move(1, b. Move(1, src, dest, auxsrc, dest, aux););c. Move(c. Move(n-1, aux, dest, srcn-1, aux, dest, src););ElseElseDisplay “Move the top disk from “, Display “Move the top disk from “, srcsrc, “ to “, , “ to “, destdest..End if.End if.

Page 22: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

CodingCoding

// ...

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;}

Page 23: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

TestingTesting

The Hanoi Towers

Enter how many disks: 1Move the top disk from A to B

Page 24: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Testing (Testing (Ct’dCt’d))

The Hanoi Towers

Enter how many disks: 2Move the top disk from A to CMove the top disk from A to BMove the top disk from C to B

Page 25: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Testing (Testing (Ct’dCt’d))

The Hanoi Towers

Enter how many disks: 3Move the top disk from A to BMove the top disk from A to CMove the top disk from B to CMove the top disk from A to BMove the top disk from C to AMove the top disk from C to BMove the top disk from A to B

Page 26: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Testing (Testing (Ct’dCt’d))

The Hanoi Towers

Enter how many disks: 4move a disk from needle A to needle Bmove a disk from needle C to needle Bmove a disk from needle A to needle Cmove a disk from needle B to needle Amove a disk from needle B to needle Cmove a disk from needle A to needle Cmove a disk from needle A to needle Bmove a disk from needle C to needle Bmove a disk from needle C to needle Amove a disk from needle B to needle Amove a disk from needle C to needle Bmove a disk from needle A to needle Cmove a disk from needle A to needle Bmove a disk from needle C to needle B

Page 27: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

AnalysisAnalysisLet’s see how many moves” it takes to solve this problem, as a function of Let’s see how many moves” it takes to solve this problem, as a function of nn, the number of disks to be moved., the number of disks to be moved.

nn Number of disk-moves requiredNumber of disk-moves required

11 11

22 33

33 77

44 1515

55 3131

......

ii 22ii-1-1

6464 226464-1 (a big number)-1 (a big number)

Page 28: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Analysis (Analysis (Ct’dCt’d))How big?How big?

Suppose that our computer and “super-printer” can generate and print 1,048,576 (2Suppose that our computer and “super-printer” can generate and print 1,048,576 (22020) instructions/second.) instructions/second.

How long will it take to How long will it take to printprint the priest’s instructions? the priest’s instructions?

• There are 2There are 26464 instructions to print. instructions to print. – Then it will take 2Then it will take 26464/2/22020 = 2 = 24444 secondsseconds to print them. to print them.

• 1 minute == 60 seconds. 1 minute == 60 seconds. – Let’s take 64 = 2Let’s take 64 = 266 as an approximation of 60. as an approximation of 60.

– Then it will take Then it will take 224444 / 2 / 266 = 2 = 23838 minutesminutes to print them. to print them.

Page 29: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Analysis (Analysis (Ct’dCt’d))Hmm. 2Hmm. 23838 minutes is hard to grasp. Let’s keep going... minutes is hard to grasp. Let’s keep going...

• 1 hour == 60 minutes. 1 hour == 60 minutes. – Let’s take 64 = 2Let’s take 64 = 266 as an approximation of 60. as an approximation of 60.

– Then it will take Then it will take 223838 / 2 / 266 = 2 = 23232 hourshours to print them. to print them.

• 1 day == 24 hours. 1 day == 24 hours. – Let’s take 32 = 2Let’s take 32 = 255 as an approximation of 24. as an approximation of 24.

– Then it will take Then it will take 223232 / 2 / 255 = 2 = 22727 daysdays to print them. to print them.

Page 30: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Analysis (Analysis (Ct’dCt’d))Hmm. 2Hmm. 22727 days is hard to grasp. Let’s keep going... days is hard to grasp. Let’s keep going...

• 1 year == 365 days. 1 year == 365 days. – Let’s take 512 = 2Let’s take 512 = 299 as an approximation of 365. as an approximation of 365.

– Then it will take Then it will take 222727 / 2 / 299 = 2 = 21818 yearsyears to print them. to print them.

• 1 century == 100 years. 1 century == 100 years. – Let’s take 128 = 2Let’s take 128 = 277 as an approximation of 100. as an approximation of 100.

– Then it will take Then it will take 221818 / 2 / 277 = 2 = 21111 centuriescenturies to print them. to print them.

Page 31: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

Analysis (Analysis (Ct’dCt’d))Hmm. 2Hmm. 21111 centuries is hard to grasp. Let’s keep going... centuries is hard to grasp. Let’s keep going...

• 1 millenium == 10 centuries. 1 millenium == 10 centuries. – Let’s take 16 = 2Let’s take 16 = 244 as an approximation of 10. as an approximation of 10.

– Then it will take Then it will take 221111 / 2 / 244 = 2 = 277 = = 128 millenia128 millenia just to just to printprint the priest’s instructions (assuming our computer doesn’t crash, in which case we have to start all over again). 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 How fast can the priests actually movemove the disks? the disks?

I’ll leave it to you to calculate the data of the apocalypse...I’ll leave it to you to calculate the data of the apocalypse...

Page 32: The Towers of Hanoi or Apocalypse When?. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi

SummarySummaryRecursion is a valuable tool that allows some problems to be solved in an elegant and efficient manner.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.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 There are problems for which we can design a solution, but the nature of the problem makes solving it effectively effectively uncomputableuncomputable..