More About Recursion Alice.

Slides:



Advertisements
Similar presentations
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.
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.
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.
Introducing While loops (and random numbers too) Alice.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
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.
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.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Humorous Asides “A journey begins with single step”
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,
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.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
Programming: Simple Control Structures
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
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.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
List Search Alice. Common Uses of Lists Iterating through a list of several like items to accomplish the same task with each item. As in the previous.
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 Quiz Show Programming Terms. 2 Alice - 3D Virtual Programming Vocabulary Quiz Board Chapter 1 Chapter 2a Chapter 2b Chapter 3 Chapter 4 $100 $200 $300.
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 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
CprE 185: Intro to Problem Solving (using C)
Functions Sec 51 Web Design.
Abdulmotaleb El Saddik University of Ottawa
Recursion Alice.
While: Indefinite Loops
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.
Recursion Alice.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Parameters Alice.
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Programming: Simple Control Structures
Fundamentals of Programming
Programming: Simple Control Structures
More About Recursion Java.
Programming: Simple Control Structures
Programming: Simple Control Structures
Chapter 11 Recursion.
Functions Alice.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Programming: Simple Control Structures
The structure of programming
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Thinking procedurally
Java Software Solutions Foundations of Program Design Sixth Edition
Recursive Thinking.
Parameters Alice.
Functions Alice.
Programming: Simple Control Structures
Class-level Methods and Inheritance
Functions Alice.
Functions Alice.
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 Source Spare Target (coneFrom) (coneSpare) (coneTo) 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

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 4th 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: towers Parameters: howmany, source, target, spare If howmany is equal to 1 move it (the smallest disk) from the source to the target Else Do in order call towers to move howmany-1 disks from source to spare (using target as spare) move it (disk # howmany) from the source to the target 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 Two recursive calls are used in this method.

Moving a disk A challenge in this animation is how to move a disk from one tower to another. In the storyboard for 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

Demo Ch8Lec2Towers-V1 Concepts illustrated in this example The base case is written as an If statement If the base case is not true, then instructions are executed to move 1 step closer to the base case and the method calls itself. Repeated self-calls eventually work down to the base case and the recursion ends.

Klutzy The moveIt method contains three sets of nested If statements The disk id number is used to determine which disk to move up move over move down The code is somewhat klutzy. In other words, the code is not elegant!

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 0.3 + 0.1 *whichdisk

Problem The problem with trying to use 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

Demo Ch08Lec2Towers-V2 Concepts illustrated in this example Condensing the program code to make it more elegant may lead to other problems. A conversion function is one that has a parameter to receive an argument of one data type and return some equivalent value, possibly of a different data type. In this example, we wrote our own function to convert the disk id number (i) to the disk name

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

Lab Chapter 8 Lecture 2 Lab