Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.

Slides:



Advertisements
Similar presentations
1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Advertisements

Nested If Statements While Loops
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
Recursion. Binary search example postponed to end of lecture.
Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Chapter 2: Algorithm Discovery and Design
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
Recursion.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 2: Algorithm Discovery and Design
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice.
Chapter 5 Conditionally Executing Instructions
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Karel J. Robot Chapter 6 Instructions That Repeat.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
Function Recursion to understand recursion you must understand recursion.
Karel J. Robot Chapter 6 Instructions That Repeat.
Recursion Powerful Tool
Identify the Appropriate Method for Handling Repetition
Recursion Version 1.0.
to understand recursion you must understand recursion
Karel – Primitive Instructions
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Recursion DRILL: Please take out your notes on Recursion
Introduction To Repetition The for loop
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Karel – Primitive Instructions
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
to understand recursion you must understand recursion
Nested If Statements While Loops
Iteration (Loops) Loops repeat a set of instructions
Karel – Primitive Instructions
Presentation transcript:

Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and not all do) – within a new method we can send a simpler form of the same message – using the same method name Recursion is just another control structure, another programming tool to add to your collection

The Lost Beeper Mine problem There is corner somewhere in the robot world that has a large number of beepers. The only clue to the location of this treasure is that somewhere, directly ahead of the robot, is a beeper. (There is no intervening wall segments.) The Lost Mine is directly north of the beeper, a distance equal to the number of moves the robot makes to get from its current position to the beeper. Download Prospector.zip demo.Prospector.zip

How does this work? Finding the beeper? void findBeeper() { if (! nextToABeeper() ) // robot not next to beeper { move(); // go to next corner findBeeper(); // is the beeper here? } In order to find the beeper, we define the method findBeeper() that calls itself to help finish the search This is a recursive definition

Recursive Definitions We do not define a method in terms of precisely itself – We define a thing in terms of a simpler, or smaller version of itself – Here we define findBeeper() as either “nothing” (if the robot is on the corner with a beeper) or “ move(); findBeeper() ” – It is also necessary to know that a beeper does lie in the robot’s path, or we fall into infinite recursion – Every call results in a separate instance of the instruction findBeeper();

Lost Beeper Mine, again So we have a sense of how we found the position-marker beeper How did we get to the mine? At every place we called a simpler version of findMine(), we were leaving some unfinished work behind A recursive method, as any other method. Must complete its task before control is returned to the call-site of the method (where the method was called from)

findMine() void findMaine() { if (nextToABeeper()) { // found beeper turnLeft() // we know the mine is north of us } else { move(); // still looking for beeper findMine(); // next method call (call-site) move(); // unfinished business } The “unfinished business” must be completed for each recursive call before each method is done. It is actually this unfinished business that correctly propels us to the lost mine

Process for writing recursive methods Step 1: Consider the stopping condition (the base case). – What is the simplest case of the problem that can be solved? – (already on the beeper) Step 2: What does the robot do in the base case? – (turnLeft) Step 3:Find a way to solve a small piece of the larger problem if not the base case – Reducing the problem in the general case – The general case is that the beeper has not been found – So move and check again Step 4: Make sure the reduction leads to the base case – The move takes the robot one step closer to the beeper

Iteration vs Recursion An iterative loop completes each cycle before beginning the next one A recursive method begins a new instance before completing the current one – The current instance is temporarily suspended – The second instance might be suspended as it calls another – Each successive instance must be completed in turn, last to first Each recursive method is supposed to make minimal progress toward the base case, it will never be controoled by a loop, but by some form of the IF instruction

Tail Recursion Tail recursion: Form of recursion in which the recursive call is the last instruction in the method void pickAllBeepers() { if (nextToABeeper()) { pickBeeper(); pickAllBeepers();// recursive call }

Tail Recursion and WHILE loops void pickAllBeepers() { if (nextToABeeper()) { pickBeeper(); pickAllBeepers();// recursive call } Execution equivalent with void pickAllBeepers() { while (nextToABeeper()) { pickBeeper(); }

WHILE loop a special form of recursion A formal definition of the WHILE loop We are going to discuss and analyze instructions (the meta-level), not use them – W is the while statement – T is the test – L is the instruction list The formal definition of W is – W == if (T){L; W;} – Notice that the WHILE statement W is defined in terms of itself

Loop verification An informal way to reason about loop correctness – Show that instruction works when the initial situation results in the test being false – Show that each time the loop body is executed, the robot’s new situation is a sompler and similar version of the old situation We need a formal way of thinking about loops to verify that they are correct

Loop Invariants A key concept Definition: an assertion which is true after each iteration of a loop – Assertion: something that can be demonstrated as being true or false For our purposes (the study of robots) – Loop invariants will be assertions about the robot’s world

After one iteration we consider: Has the direction the robot is facing changed? (How?) Has the robot’s relative position in the world changed? (How?) – This may involve thinking about wall segments Has the number of beepers in the beeper bag changed? (How?) Has the number and location of other beepers in the world changed? (How?)

clearAllBeepersToTheWall() while (frontIsCLear()) { move(); pickBeeper(); } What can we assert? – Direction is unchanged – Position advanced one corner – New corner has one less beeper – Beeper-bag has one more beeper Bold, italic assertions are interesting – Interesting means which items are important for the problem being solved

Interesting assertions A loop invariant captures the interesting change during one iteration of the loop clearAllBeepersToTheWall() invariant: – Robot has advanced one corner and removed one beeper from that corner What else have we learned? – When the loop ends the front will be blocked Therefore, after the loop, the robot will have removed one beeper from each corner it has passed, and the robot’s front is blocked – As long as each corner has one beeper on it, the loop has solved the problem of picking up beepers to the wall

Uses for loop invariants For analysis (we have done this) – Powerful tools in aiding our understanding of how a loop is operating and what it will cause a robot to do – Key: How does the robot change the state of the world? – Once we know the invariant, we can use it and the terminating condition to determine if the loop solves the problem For building loops (we will do this next) – What instructions do we want to use in a loop body?

findBeeper(): Problem statement A robot is searching for a beeper that is an unknown distance directly in front of it and there may be one-block long wall segments in the way What should be true when the loop terminates? – Next to a beeper What is the invariant – The robot has moved one and only one block forward so that each corner can be inspected

findBeeper(): First Pass While (! nextToABeeper()) { move(); } Terminating condition is correct Problem with the body – Will this maintain the invariant? – But what situations may be true in the world that could cause error- shutoffs?

findBeeper(): Second Pass While (! nextToABeeper()) { if (frontIsClear()) { move(); } else { avoidWall(); } Terminating condition is correct Does this correct the body? – Will this maintain the invariant? – Does this handle the problem of the wall segments ? In designing avoidWall() we must keep the loop invariant in mind – Make sure the robot advances only one corner