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

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

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.
More About Recursion Chapter 8 Section 2. Recall Recursion is a programming technique where a method calls itself. Recursion uses the IF/ELSE control.
CS1010: Programming Methodology
AE1APS Algorithmic Problem Solving John Drake
CS320n –Visual Programming Class-level Methods and Inheritance – Part 1 Mike Scott (Slides 4-3-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
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.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion.
The Tower of Hanoi
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 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.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice.
Copyright © Cengage Learning. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
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.
CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Hanoi Towers Big Oh Recursion Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
CS320n – Elements of Visual Programming Sending Parameters to Event Handler Methods (Slides 5-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Recursion Colin Capham Recursion – an introduction to the concept.
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
CS320n –Visual Programming Definite / Counted Loops (Slides 7-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n –Visual Programming Problem Solving Case Study (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
UNIT 17 Recursion: Towers of Hanoi.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
Problem Solving.  Similar to Solving Math Word Problem  Read the Problem  Decide how to go about Solving the Problem  Solve the Problem  Test the.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Aumm-e-hani munir Maria umer Rakhshanda batool Faiza.
CS320n – Elements of Visual Programming List Search Mike Scott (Slides 9-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion.
Recursive Algorithm R ecursive Algorithm.
Abdulmotaleb El Saddik University of Ottawa
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
CS320n –Visual Programming
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
CS1120: Recursion.
More About Recursion Java.
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion.
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
More About Recursion Alice.
Java Software Solutions Foundations of Program Design Sixth Edition
Recursive Thinking.
Presentation transcript:

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

Visual ProgrammingAdvanced Recursion2 What We Will Do Today Look at using recursion to solve other forms of problems

Visual ProgrammingAdvanced Recursion3 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 ProgrammingAdvanced Recursion4 Directory Structure Directories Files How deep can this go?

Visual ProgrammingAdvanced Recursion5 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 ProgrammingAdvanced Recursion6 Another Example Towers of Hanoi A classic puzzle

Visual ProgrammingAdvanced Recursion7 A Towers Problem 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 Source Spare Target (coneFrom) (coneSpare) (coneTo)

Visual ProgrammingAdvanced Recursion8 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 ProgrammingAdvanced Recursion9 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

Visual ProgrammingAdvanced Recursion10 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 ProgrammingAdvanced Recursion11 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 ProgrammingAdvanced Recursion12 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 (3) 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

Visual ProgrammingAdvanced Recursion13 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 ProgrammingAdvanced Recursion14 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 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?

Visual ProgrammingAdvanced Recursion15 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

Visual ProgrammingAdvanced Recursion16 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 ProgrammingAdvanced Recursion17 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

Visual ProgrammingAdvanced Recursion18 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, disk1 move up … must be an object, cannot use the id number here

Visual ProgrammingAdvanced Recursion19 Using a Function We decided to write a function to convert the disk id number (i) to the disk name.

Visual ProgrammingAdvanced Recursion20 moveIt

Visual ProgrammingAdvanced Recursion21 Demo!