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.

Slides:



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

CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu.
C++ Programming:. Program Design Including
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 20 Recursion.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
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 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
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. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
Recursion Chapter 5.
Data Structures Using C++ 2E Chapter 6 Recursion.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
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.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Recursion Colin Capham Recursion – an introduction to the concept.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
Recursion Continued Divide and Conquer Dynamic Programming.
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.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Recursion To understand recursion, you first have to understand recursion.
Review of Recursion What is a Recursive Method?
Recursion.
Chapter 15 Recursion.
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
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.
CS1120: Recursion.
Chapter 8: Recursion Java Software Solutions
Review of Recursion What is a Recursive Method?
The Hanoi Tower Problem
Chapter 8: Recursion Java Software Solutions
CSC 143 Recursion.
Review of Recursion What is a Recursive Method?
Review of Recursion What is a Recursive Method?
Presentation transcript:

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 from first peg Goal: Move disks from first peg to third peg. to third peg. Limitations: Move one disk at a time. Limitations: Move one disk at a time. Forbidden larger disk on Forbidden larger disk on top of smaller ones. top of smaller ones.

Recursive Solutions: To move n>1 disks from peg1 to peg3, first move recursively n-1 disks from peg1 to peg2, then move the largest disk directly from peg1 to peg3, and finally move n-1 disks from peg2 to peg3. To move n>1 disks from peg1 to peg3, first move recursively n-1 disks from peg1 to peg2, then move the largest disk directly from peg1 to peg3, and finally move n-1 disks from peg2 to peg3.

Recursive Solution to the Tower of Hanoi Puzzle

II. Complexity Number of moves M(n) depends on n only: For n=1: M(1) = 1 For n>1: M(n) = 2 M(n-1) + 1 = 2 [2M(n-2) +1] + 1 = 2 [2M(n-2) +1] + 1 = 2 2 M(n-2) = 2 2 M(n-2) = 2 2 [2M(n-3) + 1] = 2 2 [2M(n-3) + 1] = 2 3 M(n-3) = 2 3 M(n-3) ………. After i substitutions, we get: M(n) = 2 i M (n-i) + 2 i i-2 + … = 2 i M(n-i) + 2 i - 1 Since the initial condition is specified for n=1, which is achieved for i = n-1, we get: M(n) = 2 n-1 M(n-(n-1)) + 2 n-1 -1 = 2 n-1 M(1) + 2 n-1 -1 = 2 n - 1

An Exponential Algorithm: M (n) = 2 n -1 n M (n)

III. The Nature of Recursion Memory Use in Recursive Calls A recursive function calls itself. Using the recursive divide-and- conquer technique, we can solve a problem by dividing it into two or more simpler problems and applying the same technique again to these simpler tasks. Eventually, the sub-problems become simple enough to be solved directly and the recursive descent ends. The solution to the original problem then is composed from the solutions to the simpler parts. A storage area that can grow dynamically, such as the run-time stack, is necessary to implement recursion, because multiple activation records for the same function must exist simultaneously. If a recursive function calls itself five times, six stack frames will exist simultaneously for it, each holding the parameters for one of the active calls. Each time one of the calls returns, its stack frames is discarded and control goes back to the prior invocation. A recursive function calls itself. Using the recursive divide-and- conquer technique, we can solve a problem by dividing it into two or more simpler problems and applying the same technique again to these simpler tasks. Eventually, the sub-problems become simple enough to be solved directly and the recursive descent ends. The solution to the original problem then is composed from the solutions to the simpler parts. A storage area that can grow dynamically, such as the run-time stack, is necessary to implement recursion, because multiple activation records for the same function must exist simultaneously. If a recursive function calls itself five times, six stack frames will exist simultaneously for it, each holding the parameters for one of the active calls. Each time one of the calls returns, its stack frames is discarded and control goes back to the prior invocation.

Trace of Tower (A, C, B, 3)

IV. Java Code Highlights The program extends Java Applet class, implements ActionListenner and Runnable abstract classes. Used rectangle to represent disks and pegs. Used two-demission structured array to hold (x, y) values for each disk on each peg. values for each disk on each peg. Used two-demission structured array to hold colors for each disk on each peg. Used thread for animation. Sleeping time between each move is one second. Used recursive calls to move disks.

V. Conclusions Because the intrinsic difficulty of the problem, although this is an exponential algorithm, which will run for an unimaginably long time even for moderate n value, this still is a most efficient algorithm possible for this problem. It takes 17 min to move 10 disks at rate of 1 disk/sec. It takes 291 hours to move 20 disks at rate of 1 disk/sec. It takes 291 hours to move 20 disks at rate of 1 disk/sec. It takes 136 years to move 32 disks at rate of 1 disk/sec. It takes 136 years to move 32 disks at rate of 1 disk/sec. If you want to move 256 disks, you will end this world !!!