CS 320 n Visual Programming Advanced Recursion Slides

  • Slides: 21
Download presentation
CS 320 n –Visual Programming Advanced Recursion (Slides 8 -2) Thanks to Wanda Dann,

CS 320 n –Visual Programming Advanced Recursion (Slides 8 -2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for. Visual slide. Programming ideas. Advanced Recursion

What We Will Do Today • Look at using recursion to solve other forms

What We Will Do Today • Look at using recursion to solve other forms of problems Visual Programming Advanced Recursion 2

A second form of recursion • A second form of recursion is used when

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. • Example – Files are stored on computers in directories. – Directories can store files and other directories (sub directories) – How do you find all the files in a directory? Visual Programming Advanced Recursion 3

Directory Structure Directories Files Visual Programming Advanced Recursion How deep can this go? 4

Directory Structure Directories Files Visual Programming Advanced Recursion How deep can this go? 4

Finding all the Files • Break the big problem up into smaller problems •

Finding all the Files • Break the big problem up into smaller problems • Find the “immediate” files in this directory • then go to each sub directory and find the files in it • To solve the big problem (find all the files in a directory) we solve smaller problems (find all the files in the sub directories) – The smaller problems are solved the same way as the big problem! Visual Programming Advanced Recursion 5

Another Example • Towers of Hanoi • A classic puzzle Visual Programming Advanced Recursion

Another Example • Towers of Hanoi • A classic puzzle Visual Programming Advanced Recursion 6

A Towers Problem Source Spare Target (cone. From) (cone. Spare) (cone. To) • The

A Towers Problem Source Spare Target (cone. From) (cone. Spare) (cone. To) • The challenge is to move all the disks from the source cone to the target cone. • RULES: – Move 1 disk at a time – A larger disk may never be on top of a smaller disk Visual Programming Advanced Recursion 7

Initial world • The disks are instances of the Torus class. (A torus is

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. • Each disk is positioned exactly 0. 1 meter above the disk below it. Visual Programming Advanced Recursion 8

Identifying the disks • To make it easier to describe our solution, we give

Identifying the disks • To make it easier to describe our solution, we give each disk an id number and a name. id number 1 2 3 4 Visual Programming Advanced Recursion name disk 1 disk 2 disk 3 disk 4 9

Solving the problem • Our solution will use the – Principle of “wishful thinking”

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. Visual Programming Advanced Recursion 10

Wishful thinking in practice Assume I could move 3 of the disks to the

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. Visual Programming Advanced Recursion 11

Storyboard • To solve the towers problem, we need to know howmany disks we

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: World. towers: Parameters: howmany, source, target, spare If howmany is equal to 1 move it (the smallest disk) from the source to the target Else (1) call towers to move howmany-1 disks from source to spare (using target as spare) (2) move it (disk # howmany) from the source to the target base case – move 1 disk a smaller problem -recursively move the rest of the disks (3) call towers to move howmany-1 disks from the spare to the target (using the source as the spare) Visual Programming Advanced Recursion 12

towers • The base case occurs when howmany equals 1, just move the disk.

towers • The base case occurs when howmany equals 1, just move the disk. • Two recursive calls are used to solve the smaller problem (moving howmany-1 disks), which helps us solve the bigger problem (moving howmany disks). Visual Programming Advanced Recursion 13

Moving a disk • A challenge in this animation is how to move a

Moving a disk • A challenge in this animation is how to move a disk from one tower to another. • In the towers method, we assumed that we had a method named move. It that would accomplish the task. To write the move. It 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? Visual Programming Advanced Recursion 14

move. It Storyboard • The parameters are: – whichdisk – the disk id number

move. It Storyboard • The parameters are: – whichdisk – the disk id number – fromcone – the source cone – tocone – the target cone • A storyboard describing the steps is: move. It: 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 Visual Programming Advanced Recursion 15

Nested Ifs • The disk id number is used to determine which disk to

Nested Ifs • The disk id number is used to determine which disk to – move up – move over – move down • This means that nested Ifs must be used three times! (The code on this slide is for just one move. ) Visual Programming Advanced Recursion 16

Using an expression • We noticed that the distance each disk has to move

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 Visual Programming Advanced Recursion 17

Problem • The problem with this nifty math expression is that we need to

Problem • The problem with this nifty math expression is that we need to have the disk's name to write a move instruction. • For example, disk 1 move up … must be an object, cannot use the id number here Visual Programming Advanced Recursion 18

Using a Function • We decided to write a function to convert the disk

Using a Function • We decided to write a function to convert the disk id number (i) to the disk name. Visual Programming Advanced Recursion 19

move. It Visual Programming Advanced Recursion 20

move. It Visual Programming Advanced Recursion 20

Demo! Visual Programming Advanced Recursion 21

Demo! Visual Programming Advanced Recursion 21