More About Recursion Alice. A second form of recursion A second form of recursion is used when the solution to a problem depends on the ability to break.

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

More About Recursion Chapter 8 Section 2. Recall Recursion is a programming technique where a method calls itself. Recursion uses the IF/ELSE control.
AE1APS Algorithmic Problem Solving John Drake
Class-level Methods Alice. World / Class Method World method A general method that may refer to multiple objects; not closely associated with any particular.
Class-level Methods and Inheritance Part 1 Alice.
Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
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.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion.
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.
More About Recursion Alice. A second form of recursion A second form of recursion is used when the solution to a problem depends on the ability to break.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
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.
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.
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,
What we will do today Learn about functions in Alice.
Functions Alice.
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.
CS320n –Visual Programming Advanced Recursion (Slides 8-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Hanoi Towers Big Oh Recursion Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Creating An Animation Program Alice. Recall from last lecture We began the animation creation process We introduced the concept of storyboard We will.
Parameters Alice. A beetle band Our task is to create an animation for a bug band as an advertisement for their next concert.
Parameters Alice. Overview The need for more flexible methods Passing a parameter to a method Demos Using the Alice interface to write code for a parameter.
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.
Problem Solving.  Similar to Solving Math Word Problem  Read the Problem  Decide how to go about Solving the Problem  Solve the Problem  Test the.
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
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 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Understanding Recursion
Functions Sec 51 Web Design.
Learn about functions in Alice
Abdulmotaleb El Saddik University of Ottawa
Looping and Random Numbers
Functions Sec 8-11 Web Design.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Class-level Methods Alice.
More About Recursion Java.
Chapter 11 Recursion.
Functions Alice.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion.
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
More About Recursion Alice.
Java Software Solutions Foundations of Program Design Sixth Edition
Functions Alice.
Class-level Methods and Inheritance
Functions Alice.
Functions Alice.
Class-level Methods and Inheritance Part 2
Presentation transcript:

More About Recursion Alice

A second form of recursion A second form of recursion is used when the solution to a problem depends on the ability to break a problem down into smaller and smaller sub-problems. Let's look at an example…

A Towers Problem The challenge is to move all the disks from the source cone to the target cone. Move 1 disk at a time A larger disk may never be on top of a smaller disk Source Spare Target (coneFrom) (coneSpare) (coneTo)

Initial world The disks are instances of the Torus class. (A torus is a doughnut shaped object.) Each cone is positioned exactly 1 meter from its nearest neighbor. Other than the bottom disk, each disk is positioned exactly 0.1 meter above the disk below.

Identifying the disks To make it easier to describe our solution, we give each disk an id number and a name. id number name 1 disk1 2 disk2 3 disk3 4 disk4

Solving the problem Our solution will use the Principle of wishful thinking assume we could solve a smaller version of the same problem if we could solve the smaller version, it would make it easier to solve this problem. Base case – the simplest possible version of this problem, one that can obviously be solved.

Wishful thinking in practice Assume I could move 3 of the disks to the spare cone. Then I could move the 4 th disk (base case) to the target cone.

Storyboard To solve the towers problem, we need to know howmany disks we have and which cone is the source, the target, and the spare: World.towers: Parameters: howmany, source, target, spare If howmany is equal to 1 move it (the smallest disk) from the source to the target Else (1) call towers to move howmany-1 disks from source to spare (using target as spare) (2) move it (disk # howmany) from the source to the target (3) call towers to move howmany-1 disks from the spare to the target (using the source as the spare) base case – move 1 disk a smaller problem -- recursively move the rest of the disks

towers The base case occurs when howmany equals 1, just move the disk. Two recursive calls are used to solve the smaller problem (moving howmany-1 disks), which helps us solve the bigger problem (moving howmany disks).

Moving a disk A challenge in this animation is how to move a disk from one tower to another. In the towers method, we assumed that we had a method named moveIt that would accomplish the task. To write the moveIt method, we need to know: What are the parameters to send in to our method? What steps need to occur? How high to raise the disk object? How far (forward/back) to move it?

moveIt Storyboard The parameters are: whichdisk – the disk id number fromcone – the source cone tocone – the target cone A storyboard describing the steps is: moveIt: Parameters: whichdisk, fromcone, tocone Do in order Lift the disk up above the top of the fromcone Move it (forward or back) to a location above the tocone Lower the disk down onto the tocone

Nested Ifs The disk id number is used to determine which disk to move up move over move down This means that nested Ifs must be used three times! (The code on this slide is for just one move.)

Using an expression We noticed that the distance each disk has to move up (and then back down) is 0.3 meters more than 0.1 * the id number (whichdisk). We could use an expression to compute the distance for the move up and move down instructions. move the appropriate disk *whichdisk

Problem The problem with this nifty math expression is that we need to have the disk's name to write a move instruction. For example, disk1 move up … must be an object, cannot use the id number here

Using a question We decided to write a question to convert the disk id number (i) to the disk name.

moveIt

Demo!

Assignment Read Chapter 8-2, A Second Form of Recursion Read Tips & Techniques 8, Camera and Animation Controls Lab 8-2