Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice."— Presentation transcript:

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

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

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

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

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

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

7 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

8 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

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

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

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

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

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

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

15 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

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

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

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

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

20 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

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

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

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

24 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

25 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

26 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


Download ppt "Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice."

Similar presentations


Ads by Google