Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.

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?.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
The Algorithmic problems?
More Recursion: Permutations and Towers of Hanoi COP 3502.
COSC 2006 Data Structures I Recursion III
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
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.
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. 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.
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.
Recursion.
Review of Recursion. Definition: Recursion - The process of a function calling itself.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Storage Classes, Scope, and Recursion Lecture 6.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
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.
Module #20 - Recurrences 1 Ch. 7 Recurrence Relations Rosen 6 th ed., §§7.1.
Data Structures Recursion Phil Tayco Slide version 1.0 Mar. 8, 2015.
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 Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
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.
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 Continued Divide and Conquer Dynamic Programming.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower 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.
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.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Aumm-e-hani munir Maria umer Rakhshanda batool Faiza.
Recursion Chapter 2 Objectives Upon completion you will be able to:
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Tower of Hanoi problem: Move the pile of rings from one peg to another
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Stacks & Recursion.
Abdulmotaleb El Saddik University of Ottawa
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.
Recursion Output Input
Fundamentals of Programming
Chapter 8: Recursion Java Software Solutions
Recursion Data Structures.
Stacks & Recursion.
Tower of Hanoi problem: Move the pile of rings from one peg to another
The Hanoi Tower Problem
Chapter 8: Recursion Java Software Solutions
Stacks & Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Tower of Hanoi problem: Move the pile of rings from one peg to another
Presentation transcript:

Recursion, pt. 3: The Towers of Hanoi

Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.

Towers of Hanoi The goal in this problem: to move the entire tower from the left-most peg to the right-most peg.

Towers of Hanoi Rules: 1.A larger ring may never be on top of a smaller ring. 2.Only one ring may be moved at a time.

Towers of Hanoi Rules: 3.Rings may only be removed from the top of one tower and placed on the top of a second tower.

Towers of Hanoi Given these rules, how can we solve the problem?

Towers of Hanoi To move the tower fully to the right…

Recursive Step we’ll have to go through this stage.

Towers of Hanoi The most simple only way to reach that stage is from this one…

Towers of Hanoi … which can be reached from this stage, and so forth.

Recursive Step What common property do these moves have? – Our goal is first to move the bottom- most ring of the stack to the destination tower. – To do this, we must move the rest of the stack to the spare tower.

Recursive Step Note what we’re saying here. – “To do this, we must move the rest of the stack to the spare tower.” In other words, to move the entire stack from the left to the right tower… will require the ability to move almost the entire stack from the left to the spare tower.

Recursive Step To move the stack of rings… requires being able to move (a smaller portion of) the stack. Note that this involves a reduction – our stack size will reduce by one upon each such reduction.

Recursive Step Combining these two facts together, we have a perfect candidate for recursion. – We can solve the problem in terms of a reduced form of itself. – We’ll move the stack from one tower to another by… moving (most of) the stack from one tower to another. Recursion.

Recursive Step So, in order… 1.Move the top n-1 rings from the source to the spare tower. 2.Move the bottom (the leftover 1 ring) from the source to the destination tower. 3.Relocate those n-1 rings we had placed on the spare tower over to the destination tower.

Recursive Step Note how the process works for this case… Destination Spare Source

Recursive Step …as well as for this case (moving the top 4 disks)… Destination Spare Source

Recursive Step …as well as for this case (moving the top 3 disks)… Source Destination Spare

Recursive Step …which gets us here …

Recursive Step …then here …

Recursive Step …as well as for this case. – The difference is that the “destination” tower and “spare” are different here than it was for the other situations. Destination Spare Source

Recursive Step (Top 4 disks now moved out of the way)

Recursive Step Move bottom disk Destination Spare Source

Recursive Step Now move top 4 disks from spare to destination Destination Spare Source

Towers of Hanoi To move the tower fully to the right

Recursive Step So, while we know the general idea for reducing the problem, there’s the problem of towers serving different purposes at different stages.

Recursive Step In order to move the full tower from the left peg to the right…

Recursive Step …we move the top n-1 to the spare tower. – Thus, the spare and destination towers get interchanged, conceptually.

Towers of Hanoi The method needs to access all of the towers at every frame. – What changes is the name by which we will reference each tower. – Hmm. Reference. Remember, the changes to each tower should propagate across every frame of the method.

Pseudocode void hanoi(int n, Tower* src, Tower* spr, Tower* dst) { hanoi(n-1, src, dst, spr); Ring p = src->takeRing(); dst->placeRing(p); hanoi(n-1, spr, src, dst); } Note: “Ring” and “Tower” haven’t been defined or analyzed yet.

References Will this work? – This is part of the beauty of passing objects by reference. Changes to each tower will persist, and the name by which we call each Tower will change appropriately for the method.

References This is a major benefit of using pointers! – The whole “by reference” thing can be troublesome when starting out, since it can cause collateral damage. – However, when references are fully understood, that “collateral” damage may be easily focused, channeled, and controlled for great benefit.

Towers of Hanoi What’s left to examine? – Ah yes. “Ring” and “Tower” still haven’t yet been defined. – Let’s look at the towers first.

Towers of Hanoi What would be a proper representation for each tower? – We are placing rings on the top of each tower and removing them from the top.

Towers of Hanoi The towers are effectively stacks! – Yeah, there’s a reason I’ve been talking about “stacks of rings.”

Towers of Hanoi The towers are effectively stacks! – We can model this for now by using a vector; elements should only be removed from and added at the vector’s end.

Towers of Hanoi As for the rings, they could easily be represented by integers, or by a custom class. For our in-class implementation here, let’s use a custom class.

The Core Algorithm void hanoi(int n, Tower* src, Tower* spr, Tower* dst) { hanoi(n-1, src, dst, spr); dst->placeRing(src->takeRing()); hanoi(n-1, spr, src, dst); } Wait… something’s missing… – What’s our base case?

The Core Algorithm void hanoi(int n, Tower* src, Tower* spr, Tower* dst) { if(n > 1) hanoi(n-1, src, dst, spr); dst->placeRing(src->takeRing()); if(n > 1) hanoi(n-1, spr, src, dst); }

Homework Implement Towers of Hanoi as a group – Display tower states at each step – Allow for single step – Due Wednesday

An Aside Regarding a few things you’ll see in the code… – Note the “callback” parameter. It’s a parameter that receives a function (pointer) as an argument! How about that syntax for the parameter? – We’ll cover this (and other, similar techniques) more formally at a later time.