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.

Slides:



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

Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
Copyright, Joseph Bergin
Nested If Statements While Loops
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
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.
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.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
Robot? What’s a Robot? Introducing Karel-the-Robot.
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.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Karel the Robot A Gentle Introduction to the Art of Programming.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
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.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
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.
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.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
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 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
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.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Function Recursion to understand recursion you must understand recursion.
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
Karel J. Robot Chapter 6 Instructions That Repeat.
CS 106A, Lecture 3 Problem-solving with Karel
Karel – Primitive Instructions
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.
Copyright © 2008 by Helene G. Kershner
Recursion CSC 202.
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
Karel – Primitive Instructions
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
Karel J Robot OOP approach to learning computer science
Karel the Robot – Making Decisions
Karel J Robot OOP approach to learning computer science
Nested If Statements While Loops
Iteration (Loops) Loops repeat a set of instructions
Karel – Primitive Instructions
Presentation transcript:

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 reaching your goal accomplished by a method invoking itself! –so, if it invokes itself - then let’s think about that - how do you think it’s going to stop???

2 Ch. 7 Iteration public void pickAll() { while (nextToABeeper()) pickBeeper(); } Recursion public void pickAll() { if (! nextToABeeper() return; pickBeeper(); pickAll(); } This recursive definition goes like this: Are there any beepers left on this corner? No - then we’re done. Yes - then pick one beeper and then pickAll. can you write this another way?

3 Ch. 7 The key to recursion –we don’t define a problem in terms of exactly itself - we define the problem in terms of a smaller/simpler (yet very closely related) problem - also, we define the simplest/smallest situation separately. –in the previous problem we define the simplest situation to be: there are no beepers on the corner - this becomes our base case –otherwise, we have more work to do - so we solve a tiny part of the problem (pickBeeper) and then invoke ourself to finish the smaller (yet related) problem - this becomes our general/recursive case. Recursion

4 Ch. 7 You Try! write a recursive method called goToSouthWall –precondition: bot is already facing south - there are no walls in the way –postcondition: bot is at the south wall still facing south - it is still on the same avenue What are preconditions & postconditions? the things that need to be true in order for the method to perform its task - the contract between the client and the method what will be true after the method has finished executing - only guaranteed if the preconditions are met

5 Ch. 7 Writing Recursive Methods - the Process The process for writing recursive robot instructions is very similar to that for writing loops: Step 1: Consider the stopping condition (also called the base case)--what is the simplest case of the problem that can be solved? In the pickAll problem, the simplest, or base, case is when the robot is already on an empty corner.

6 Ch. 7 the Process - cont’d Step 2: What does the robot have to do in the base case? In this example there's nothing to do. Step 3: Find a way to solve a small piece of the larger problem if not in the base case. This is called "reducing the problem in the general case." In the pickAll problem, the general case is when the robot is on a corner with one or more beepers and the reduction is to pick up a beeper.

7 Ch. 7 the Process - cont’d Step 4: Make sure the reduction leads to the base case. Again, in the above example of pickAll, by picking up one beeper at a time, the robot must eventually clear the corner of beepers, regardless of the original number present.

8 Ch. 7 Iteration vs. Recursion An iterative loop must complete each iteration before beginning the next one. A recursive method typically begins a new instance before completing the current one. When that happens, the current instance is temporarily suspended, pending the completion of the new instance. Of course, this new instance might not complete before generating another one. Each successive instance must be completed in turn, last to first.

9 Ch. 7 Iteration vs. Recursion Since EACH recursive instance is supposed to make some (often minimal) progress toward the base case, we should not use loops to control recursive calls. Thus, we will usually see an IF or an IF/ELSE in the body of a recursive method, but not a WHILE.

10 Ch. 7 Follow the steps and write the method Use recursion to move a robot named Karel to a beeper. Follow the steps presented earlier: Step 1: What is the base case? –Karel is on the beeper. Step 2: What does the robot do in the base case? –Nothing. Step 3: What is the general case? –The robot is not on the beeper.

11 Ch. 7 writing the recursive method - cont’d Step 4: What is the reduction (what do we do in the general case)? –Move toward the beeper and make the recursive call. Does the reduction lead to termination? Yes, assuming the beeper is directly in front of the robot, the distance will get shorter by one block for each recursive call. By the way, this “assumption” becomes the precondition(i.e., there is a beeper somewhere in front of the bot).

12 Ch. 7 Why Recursion? there are problems which are much easier to solve using recursion Problem: the “Lost Beeper Mine” –Karel is facing East and there is a beeper somewhere in front of her. Directly North of that beeper there is a “Beeper Mine” (a corner with a large number of beepers on it). The Beeper Mine is exactly the same number of corners North of the single beeper as Karel is away from the single beeper at the beginning. Get Karel to the Beeper Mine! Try this without recursion.

13 Ch. 7 let’s write it Step 1: What is the base case? –Karel is on the beeper Step 2: What does Karel have to do in the base case? –turnLeft (this will face Karel north) Step 3: What is the general case? –Karel is not on the beeper

14 Ch. 7 let’s write it Step 4: What is the reduction? –Move one block forward, make the recursive call and have Karel execute a second move after the recursive call. This second move will be executed in all instances but the base case, causing Karel to make as many moves north after the base case as it did in getting to the base case. Now, code it before we go on…

15 Ch. 7 Lost Beeper Mine solution public void findMine() { } let’s trace the execution