Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in 1883. The game starts by having few discs stacked.

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.
The Towers of Hanoi or Apocalypse When?.
CS1010: Programming Methodology
The Tower Of Hanoi Edouard Lucas
The Algorithmic problems?
Introduction to Algorithms
More Recursion: Permutations and Towers of Hanoi COP 3502.
AE1APS Algorithmic Problem Solving John Drake
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
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.
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Recursion.
FIT FIT1002 Computer Programming Unit 20 Recursion.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 10.1 – 10.4.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © Cengage Learning. All rights reserved.
Department of Computer Engineering Recursive Problem Solving Computer Programming for International Engineers.
Advanced Counting Techniques
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Chapter Objectives To understand how to think recursively
Recursion Jordi Cortadella Department of Computer Science.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Upon completion, the world will end…
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Examples of Recursion Instructor: Mainak Chaudhuri
Recursion Colin Capham Recursion – an introduction to the concept.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
UNIT 17 Recursion: Towers of Hanoi.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
1 In this puzzle, the player begins with n disks of decreasing diameter placed one on top of the other on one of three pegs of the game board. The player.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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.
Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Introduction to Recurrence Relations
Java Lecture Recursion 29th feb 2005
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Copyright © Cengage Learning. All rights reserved.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in 1883. The game starts by having few discs stacked in increasing order of size. The number of discs can vary, but there are only three pegs.

Tower of Hanoi The Objective is to transfer the entire tower to one of the other pegs. However you can only move one disk at a time and you can never stack a larger disk onto a smaller disk. Try to solve it in fewest possible moves.

Tower of Hanoi Solution To get a better understanding for the general algorithm used to solve the Tower of Hanoi, try to solve the puzzle with a small amount of disks, 3 or 4, and once you master that , you can solve the same puzzle with more discs with the following algorithm.

Tower of Hanoi How to solve the 4 pegs

Recursive Solution for the Tower of Hanoi with algorithm Let’s call the three peg Src(Source), Aux(Auxiliary) and st(Destination). Move the top N – 1 disks from the Source to Auxiliary tower Move the Nth disk from Source to Destination tower Move the N – 1 disks from Auxiliary tower to Destination tower. Transferring the top N – 1 disks from Source to Auxiliary tower can again be thought of as a fresh problem and can be solved in the same manner. So once you master solving Tower of Hanoi with three disks, you can solve it with any number of disks with the above algorithm.

Tower of Hanoi The puzzle is well known to students of Computer Science since it appears in virtually any introductory text on data structures or algorithms. A function solve with four arguments (number of disks) and three pegs (source, intermediary and destination) could look like this. Solve (N, Src, Aux, Dst) If N is 0 Exit Else solve (N-1, Src, Dst, Aux) Move from Src to Dst Solve(N -1 , Aux, Src, Dst)

Tower of Hanoi This actually serves as the definition of the function Solve. The function is recursive in that it calls itself repeatedly with decreasing values of N until a terminating condition (in our case N = 0) has been met. To me the sheer simplicity of the solution is breathtaking. For N =3 it translates into 1. Move from Src to Dst 2. Move from Src to Aux 3. Move from Dst to Aux 4. Move from Src to Dst 5. Move from Aux to Src 6. Move from Aux to Dst 7. Move from Src to Dst Of course "Move" means moving the topmost disk.

Tower of Hanoi For N = 4 we get the following sequence 1. Move from Src to Aux 2. Move from Src to Dst 3. Move from Aux to Dst 4. Move from Src to Aux 5. Move from Dst to Src 6. Move from Dst to Aux 7. Move from Src to Aux 8. Move from Src to Dst 9. Move from Aux to Dst 10. Move from Aux to Src 11. Move from Dst to Src 12. Move from Aux to Dst 13. Move from Src to Aux 14. Move from Src to Dst 15. Move from Aux to Dst

Tower of Hanoi How many moves will it take to transfer n disks from the left post to the right post? the recursive pattern can help us generate more numbers to find an explicit (non-recursive) pattern. Here's how to find the number of moves needed to transfer larger numbers of disks from post A to post C, remembering that M = the number of moves needed to transfer n-1 disks from post A to post C: for 1 disk it takes 1 move to transfer 1 disk from post A to post C; for 2 disks, it will take 3 moves:    2M + 1 = 2(1)  + 1 =  3 for 3 disks, it will take 7 moves:    2M + 1 = 2(3)  + 1 =  7 for 4 disks, it will take 15 moves:   2M + 1 = 2(7)  + 1 = 15 for 5 disks, it will take 31 moves:   2M + 1 = 2(15) + 1 = 31 for 6 disks... ?

Tower of Hanoi Explicit Pattern Number of Disks         Number of Moves         1                          1         2                          3         3                          7         4                         15         5                         31     Powers of two help reveal the pattern: Number of Disks (n)     Number of Moves         1                 2^1 - 1 = 2 - 1 = 1         2                 2^2 - 1 = 4 - 1 = 3         3                 2^3 - 1 = 8 - 1 = 7         4                 2^4 - 1 = 16 - 1 = 15         5                 2^5 - 1 = 32 - 1 = 31

Program to solve tower of Hanoi problem import java.util.Stack; public class TowersOfHanoi { public static void main(String[] args) Tower towerSource = new Tower("1"); Tower towerDestination = new Tower("2"); Tower towerHelper = new Tower("3"); towerSource.stack.push(4); towerSource.stack.push(3); towerSource.stack.push(2); towerSource.stack.push(1); move(towerSource.stack.size(), towerSource, towerDestination, towerHelper); }

private static void move(int size, Tower towerSource, Tower towerDestination, Tower towerHelper) { if (towerSource.stack.isEmpty()) return; if (size == 1) System.out.println("Move " + towerSource.stack.peek() + " from tower " + towerSource.name + " to tower " + towerDestination.name); towerDestination.stack.push(towerSource.stack.pop()); } move(size - 1, towerSource, towerHelper, towerDestination); move(1, towerSource, towerDestination, towerHelper); move(size - 1, towerHelper, towerDestination, towerSource);

private static class Tower { public String name; public Stack < integer > stack = new Stack < integer >(); public Tower(String name) this.name = name; }