Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in 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.
Recursion Alice.
AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Repetition Structures
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.
While: Indefinite Loops Sec 8-14 Web Design. Objectives The student will: Understand what an Indefinite Loop is Understand how create an indefinite loop.
CompSci 18S Recursion Dec 4, 2006 Prof. Susan Rodger.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Introducing While loops (and random numbers too) Alice.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Recursion … just in case you didn’t love loops enough …
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we know is that repetition.
Fall 2009ACS-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.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Invitation to Computer Science, Java Version, Second Edition.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Recursion Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we know is that repetition.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Mathematical Expressions, Conditional Statements, Control Structures
CS320n –Visual Programming Advanced Recursion (Slides 8-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Programming: Simple Control Structures MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Functions Sec 51 Web Design.
Abdulmotaleb El Saddik University of Ottawa
Recursion Alice.
While: Indefinite Loops
Functions Sec 8-11 Web Design.
Let's Race! Typing on the Home Row
Recursion Alice.
Doing things more than once
Fundamentals of Programming
More About Recursion Java.
Design and Implementation
Counted (For) Loops in Alice
Programming: Simple Control Structures
Functions Alice.
The structure of programming
More About Recursion Alice.
Thinking procedurally
Functions Alice.
Functions Alice.
Functions Alice.
Presentation transcript:

Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice

Recursion2 Learning Goals Introduce the concept of recursion –Indefinite iteration –Breaking a problem into smaller sub-problems

Recursion3 Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we know is that repetition is needed – For example, in a board game like chess or checkers, we don’t know exactly how many moves it will take for a player to win or lose the game – all we know is that several moves will be needed.

Recursion4 Indefinite Repetition In programs where a count of repetitions is not known (indefinite), we can use one of two repetition control mechanisms: –While statement –Recursion This session focuses on Recursion.

Recursion5 Many of the pieces we use to create a program are identified by using special words. For example, – Do in order – Do together – If/Else – Loop Recursion is not a program statement with a special word that identifies it as part of the programming language. Recursion means that a method (or a function) calls itself.

Recursion6 Example – horse race A carnival style horse race. In repeated moves, one horse is randomly selected to move forward. The selected horse “runs” straight ahead to the finish line. A horse is the winner if it gets to the finish line before any other horse. When one horse wins, the game ends.

Recursion7 Storyboard "do everything again" means that the entire method should be repeated. To do everything again, the method calls itself. race If one of the horses has won the winner says, “I won!!!” Else randomly choose one horse and move it forward a small amount do everything again

Recursion8 Stepwise Refinement race If one of the horses has won the winner says, “I won!!!” Else randomly choose one horse and move it forward a small amount call race method isGameOver? whichHorseWon?moveRandomHorseForward

Recursion9 Demo HorseRace-V1 Concepts illustrated in this example – In games, a conditional expression is often used to determine when a game is over. In this example, the game is over when one of the horses gets within 0.5 meters of the finish line. The same condition is used to determine the winner. –Random numbers is used to introduce an element of chance into a game program.

Recursion10 Testing Testing a program that uses random numbers requires extra caution. In this example, we ran the program 20 times and found that – racehorse1 won 7 times – racehorse2 won 3 times – racehorse3 won 10 times Something is wrong! Each horse should win approximately 1/3 of the time.

Recursion11 Demo HorseRace-V2 The bug in the first version of the program is that it has – nested If statements, and – a 33% probability for each If statement What we didn't consider is that if racehorse1 was not selected, then we have a 50% probability of selecting either racehorse2 or racehorse3.

Recursion12 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…

Recursion13 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)

Recursion14 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.

Recursion15 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

Recursion16 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.

Recursion17 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.

Recursion18 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.

Recursion19 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?

Recursion20 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

Recursion21 Demo Towers-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.

Recursion22 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!

Recursion23 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

Recursion24 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

Recursion25 Demo Towers-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

Recursion26 Summary You can use recursion –For indefinite repetition In place of a while loop –For problems that have simpler sub-problems Like Towers of Hanoni