Download presentation

Presentation is loading. Please wait.

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 (Ct’d) 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 we’ll 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 we’ll refer to the three needles as A, B, and C... We then move the top disk from A to C.

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

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

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

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

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

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

12
Our Problem Today’s 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, let’s 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 (Ct’d) 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 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 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 (Ct’d) Induction Step: n > 1**

® How can recursion help us out? a. Recursively move n-1 disks from src to aux.

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

® How can recursion help us out? b. Move the one remaining disk from src to dest.

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

® How can recursion help us out? c. Recursively move n-1 disks from aux to dest...

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

® How can recursion help us out? d. We’re 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)**

// ... 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 (Ct’d) 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 (Ct’d) 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 C to A Move the top disk from C to B

26
**Testing (Ct’d) 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 C to needle A

27
Analysis Let’s see how many moves” it takes to solve this problem, as a function of n, the number of disks to be moved. n Number of disk-moves required 1 1 2 3 3 7 ... i 2i-1 (a big number)

28
**Analysis (Ct’d) How big?**

Suppose that our computer and “super-printer” can generate and print 1,048,576 (220) instructions/second. How long will it take to print the priest’s instructions? There are 264 instructions to print. Then it will take 264/220 = 244 seconds to print them. 1 minute == 60 seconds. Let’s take 64 = 26 as an approximation of 60. Then it will 244 / 26 = 238 minutes to print them.

29
**Analysis (Ct’d) Hmm. 238 minutes is hard to grasp. Let’s keep going...**

1 hour == 60 minutes. Let’s take 64 = 26 as an approximation of 60. Then it will 238 / 26 = 232 hours to print them. 1 day == 24 hours. Let’s take 32 = 25 as an approximation of 24. Then it will 232 / 25 = 227 days to print them.

30
**Analysis (Ct’d) Hmm. 227 days is hard to grasp. Let’s keep going...**

1 year == 365 days. Let’s take 512 = 29 as an approximation of 365. Then it will 227 / 29 = 218 years to print them. 1 century == 100 years. Let’s take 128 = 27 as an approximation of 100. Then it will 218 / 27 = 211 centuries to print them.

31
Analysis (Ct’d) Hmm. 211 centuries is hard to grasp. Let’s keep going... 1 millenium == 10 centuries. Let’s take 16 = 24 as an approximation of 10. Then it will 211 / 24 = 27 = 128 millenia just to print 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 move the disks? I’ll 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.

Similar presentations

OK

Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.

Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on mind reading computer free download Ppt on c language fundamentals ii Ppt on human chromosomes sex Ppt on understanding secularism in india Ppt on double bar graphs Ppt on electronics and telecommunication Ppt on model view controller explained Ppt on development of indian railways Ppt on agriculture in india 2013 Ppt on rime of the ancient mariner part 1 and 2