more about recursion alice. a second form of recursion a second form of recursion is used when the...
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/1.jpg)
More About Recursion
Alice
![Page 2: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/2.jpg)
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…
![Page 3: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/3.jpg)
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)
![Page 4: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/4.jpg)
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.
![Page 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/6.jpg)
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.
![Page 7: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/7.jpg)
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.
![Page 8: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/8.jpg)
StoryboardTo 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.
![Page 9: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/9.jpg)
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?
![Page 10: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/10.jpg)
moveIt StoryboardThe 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
![Page 11: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/11.jpg)
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.
![Page 12: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/12.jpg)
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!
![Page 13: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/14.jpg)
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
![Page 15: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/15.jpg)
Demo
Ch08Lec2Towers-V2Concepts 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
![Page 16: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/16.jpg)
Assignment
Read Chapter 8 Section 2, A Second Form of Recursion
Read Tips & Techniques 8, Camera and Animation Controls
![Page 17: 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](https://reader030.vdocument.in/reader030/viewer/2022032522/56649d6a5503460f94a48da3/html5/thumbnails/17.jpg)
Lab
Chapter 8 Lecture 2 Lab