The Towers of Hanoi or Apocalypse When?.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
CS1010: Programming Methodology
The Tower Of Hanoi Edouard Lucas
More Recursion: Permutations and Towers of Hanoi COP 3502.
COSC 2006 Data Structures I Recursion III
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion … just in case you didn’t love loops enough …
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion Gordon College CPS212
Recursion!. Question Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?
Review of Recursion. Definition: Recursion - The process of a function calling itself.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
When confronted with a new problem there are two questions you should ask: 1. Is this problem like, or a special case of, a problem that I already know.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
© Calvin College, recursion n. See recursion. See also tail recursion. - The Jargon Dictionary, v
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
1 Storage Classes, Scope, and Recursion Lecture 6.
RECURSION Lecture 6 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursion Jordi Cortadella Department of Computer Science.
Data Structures Recursion Phil Tayco Slide version 1.0 Mar. 8, 2015.
Recursion CMPE231, Spring 2012 Aleaxander G. Chefranov 1.
JAVA Advanced Control Structures. Objectives Be able to use a switch statement for selective execution. Be able to implement repetition using alternative.
Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CHAPTER 4 RECURSION. BASICALLY, A FUNCTION IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Chapter 6.1: Recurrence Relations Discrete Mathematical Structures: Theory and Applications.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
1 Examples of Recursion Instructor: Mainak Chaudhuri
Recursion Colin Capham Recursion – an introduction to the concept.
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
UNIT 17 Recursion: Towers of Hanoi.
Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 2 Objectives Upon completion you will be able to:
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Chapter 4 Stacks
Recursion Chapter 12.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Jordi Cortadella Department of Computer Science
Recursive Definitions
ECE 103 Engineering Programming
Recursion Data Structures.
When Will The World End “Temple of Brahma” legend from the Indian city of Benares Priests must move 64 gold disks from one end of the temple to the other.
The Hanoi Tower Problem
Recursion Definition: A method that calls itself. Examples
Recursion When performing a repetitive task either: a loop recursion
Recursion: The Mirrors
Recursion.
CSC 143 Recursion.
1A Recursively Defined Functions
Self-Referencing Functions
Presentation transcript:

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. Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order:

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!

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.

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.

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.

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.

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.

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.

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.

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

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.

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

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

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.

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.

Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? a. Recursively move n-1 disks from src to aux.

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

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

Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? d. We’re done!

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.

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;

Testing The Hanoi Towers Enter how many disks: 1 Move the top disk from A to B

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

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

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

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 4 15 5 31 ... i 2i-1 64 264-1 (a big number)

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 take @ 244 / 26 = 238 minutes to print them.

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 take @ 238 / 26 = 232 hours to print them. 1 day == 24 hours. Let’s take 32 = 25 as an approximation of 24. Then it will take @ 232 / 25 = 227 days to print them.

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 take @ 227 / 29 = 218 years to print them. 1 century == 100 years. Let’s take 128 = 27 as an approximation of 100. Then it will take @ 218 / 27 = 211 centuries to print them.

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 take @ 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...

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.