Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation 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 (Ctd) 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 well 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 well refer to the three needles as A, B, and C... We then move the top disk from A to C.

6 Example (Ctd) For simplicity, suppose there were just 3 disks, and well refer to the three needles as A, B, and C... We then move the top disk from B to C.

7 Example (Ctd) For simplicity, suppose there were just 3 disks, and well refer to the three needles as A, B, and C... We then move the top disk from A to B.

8 Example (Ctd) For simplicity, suppose there were just 3 disks, and well refer to the three needles as A, B, and C... We then move the top disk from C to A.

9 Example (Ctd) For simplicity, suppose there were just 3 disks, and well refer to the three needles as A, B, and C... We then move the top disk from C to B.

10 Example (Ctd) For simplicity, suppose there were just 3 disks, and well refer to the three needles as A, B, and C... We then move the top disk from A to B.

11 Example (Ctd) For simplicity, suppose there were just 3 disks, and well refer to the three needles as A, B, and C... and were done! The problem gets more difficult as the number of disks increases...

12 Our Problem Todays 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, lets 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 << \n\nThe Hanoi Towers!\n\n << Enter how many disks: ; int numDisks; cin >> numDisks; Move(numDisks, A, B, C); }

14 Analysis (Ctd) 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 << \n\nThe Hanoi Towers!\n\n << 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 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 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 (Ctd) Induction Step: n > 1 How can recursion help us out? a. Recursively move n-1 disks from src to aux.

18 Design (Ctd) Induction Step: n > 1 How can recursion help us out? b. Move the one remaining disk from src to dest.

19 Design (Ctd) Induction Step: n > 1 How can recursion help us out? c. Recursively move n-1 disks from aux to dest...

20 Design (Ctd) Induction Step: n > 1 How can recursion help us out? d. Were 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 (Ctd) 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 (Ctd) 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 (Ctd) 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 Lets see how many moves it takes to solve this problem, as a function of n, the number of disks to be moved. nNumber of disk-moves required i2 i (a big number)

28 Analysis (Ctd) 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 priests instructions? There are 2 64 instructions to print.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.1 minute == 60 seconds. –Lets 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 (Ctd) Hmm minutes is hard to grasp. Lets keep going... 1 hour == 60 minutes.1 hour == 60 minutes. –Lets 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.1 day == 24 hours. –Lets 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 (Ctd) Hmm days is hard to grasp. Lets keep going... 1 year == 365 days.1 year == 365 days. –Lets 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.1 century == 100 years. –Lets 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 (Ctd) Hmm centuries is hard to grasp. Lets keep going... 1 millenium == 10 centuries.1 millenium == 10 centuries. –Lets 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 priests instructions (assuming our computer doesnt crash, in which case we have to start all over again). How fast can the priests actually move the disks? Ill 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.


Download ppt "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."

Similar presentations


Ads by Google