The Hanoi Tower Problem

Slides:



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

The Towers of Hanoi or Apocalypse When?.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
CS1010: Programming Methodology
The Algorithmic problems?
More Recursion: Permutations and Towers of Hanoi COP 3502.
2.3 Recursion do automated demo of towers of hanoi before class
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.
Use of Computer Technology in Education Course Management Systems Learning Management Systems Classroom Teaching Technology Modeling and Simulation and.
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)
Representational Choices The Towers of Hanoi Problem.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Recursion Gordon College CPS212
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.
The Tower of Hanoi
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.
Department of Computer Engineering Recursive Problem Solving Computer Programming for International Engineers.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
CS Final Project 2010 Rohan Paramesh. The Programs  Implementing 3 programs each in C# and Python  Student Scheduler  Assigns courses and students,
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
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.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
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.
ACM programming contest Introduction + Recursion.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Chapter 6.1: Recurrence Relations Discrete Mathematical Structures: Theory and Applications.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks.
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
UNIT 17 Recursion: Towers of Hanoi.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
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.
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.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Recursion.
Recursion.
Programming in Java: lecture 10
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)
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Chapter 14: Recursion Starting Out with C++ Early Objects
CS1120: Recursion.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Tower of Hanoi Algorithm
Recursion.
CSC 143 Recursion.
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Tower of Hanoi problem: Move the pile of rings from one peg to another
Presentation transcript:

The Hanoi Tower Problem Problem: There are n disks of different diameters on three pegs, labeled as Peg 1, Peg 2, as Peg 3. At the beginning, all n disks are on Peg 1, arranged in an increasing order of their diameter from top down. The question is to move all disks to Peg 3 according to the following rules: 1. Each time, only one disk on top of a peg is moved to the top of another peg. 2. No disk can be put on top of a smaller disk.

Start Peg 1 Peg 2 Peg 3 End Peg 1 Peg 2 Peg 3

Solving Idea The base case: If there is no disk (n = 0), nothing has to be done. Reduction: Now suppose the number of disks is at least one. Ignore the largest disk, we have n – 1 disks. A smaller case. Recursion: The smaller case can be solved by a recursive call to the same algorithm since this reduction can eventually be reduced to the base case. Building up the solution: Move n – 1disks from the source peg to the intermediate peg, move one disk from the source peg to the destination peg, and finally, move n – 1 disks from the intermediate peg to the destination peg.

The Algorithm GIVEN: N (a positive integer) S, I, D (the source peg, intermediate peg, and the destination peg) RESULT: Output the steps of movements HEADER Hanoi (N, S, I, D) BODY test: N > 0? false true Ø Hanoi (N - 1, S, D, I) output: "S -> D" Hanoi (N - 1, I, S, D)

Java Implementation public static void hanoi (int n, int s, int i, int d) { if (n > 0) hanoi (n - 1, s, d, i); System.out.println (s + " -> " + d); hanoi (n - 1, i, s, d); }

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

How it works when n = 3, s = 1, i = 2, d = 3? hanoi(3,1,2,3) hanoi(2,1,3,2) 1 -> 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> 3 3 -> 2 2 -> 1 1 -> 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)