Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice.
Post on 24-Dec-2015
220 Views
Preview:
Transcript
Recursion 2
Learning Goals
• Introduce the concept of recursion– Indefinite iteration– Breaking a problem into smaller sub-problems
Recursion 3
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.
Recursion 4
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.
Recursion 5
Recursion• 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.
Recursion 6
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.
Recursion 7
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
Recursion 8
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
Recursion 9
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.
Recursion 10
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.
Recursion 11
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.
Recursion 12
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…
Recursion 13
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)
Recursion 14
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.
Recursion 15
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
Recursion 16
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.
Recursion 17
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.
Recursion 18
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.
Recursion 19
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?
Recursion 20
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
Recursion 21
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.
Recursion 22
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!
Recursion 23
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
Recursion 24
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
Recursion 25
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
top related