1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 14: Intro to Recursion.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

The University of Adelaide, School of Computer Science
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Recursion. Binary search example postponed to end of lecture.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
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.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Sequential & Object oriented Programming
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
Creating and Using Class Methods. Definition Class Object.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Recursion. Recursive Methods  A recursive method is a method that calls itself.  General Form of Simple Recursive Methods  Every recursive method has.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Paul Ammann & Jeff Offutt
C Functions Pepper.
Methods Chapter 6.
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
i206: Lecture 13: Recursion, continued Trees
Recursion Recursive Thinking Recursive Programming
Chapter 12 Recursion (methods calling themselves)
Paul Ammann & Jeff Offutt
Chapter 8: Recursion Java Software Solutions
The Basics of Recursion
Recursion Chapter 11.
Basics of Recursion Programming with Recursion
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 18 Recursion.
Java Software Solutions Foundations of Program Design Sixth Edition
Corresponds with Chapter 5
Lecture 20 – Practice Exercises 4
Presentation transcript:

1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 14: Intro to Recursion

2 Recursion An algorithmic technique in which a function, in order to accomplish a task, calls itself with some part of the task.

3 Recursion A method that invokes itself Examples: –GNU stands for: GNU’s Not Unix (part of its own definition!) –A dog is a Collie if its name is Lassie or its mother was a Collie. Start with a given dog. Is its name Lassie? If so, done. Else is its mother a Collie? –Is the mother named Lassie? If so, done. –Else is its mother a Collie? –… and so on.

4 Recursion Example Let’s illustrate with a mother-child relationship. Definition: A dog is a collie if its name is Lassie or if its mother is Lassie, or if its mother’s mother is Lassie, or … if any of its female ancestors is Lassie. public class Dog { public String name; public Dog mother; Dog () { name = “Lassie”; mother = null; } Dog (String s, Dog d) { name = s; mother = d; } public static void main(String s[]) { Dog lassie = new Dog(); Dog fido = new Dog(“Fido”,lassie); Dog fifi = new Dog(“Fifi”, fido); Dog rover = new Dog(“Fover”, fifi); Dog grover = new Dog(“Grover”, fifi); if (grover.isCollie(grover)) { System.out.println(grover.name + “ is a collie.”); } else { System.out.println(grover.name + “ is not a collie.”); }

5 Recursion public boolean isCollie (Dog d) { if (d == null) { return false; } if (d.name == “Lassie”) { return true; } return isCollie(d.mother); } base cases recursive call: note that the argument to the function has to refine the problem A method that calls itself –A kind of iteration – Recursive methods need: A base case / terminating condition (or else it doesn’t halt) A body (containing the recursive call)

6 Recursion & the Runtime Stack Each call of the recursive method is placed on the stack. When we finally reach the base case, all the frames are popped off of the stack. –Say Grover’s mother is Fifi Rover’s mother is Fifi too Fifi’s mother is Fido Fido’s mother is Lassie –Call the isCollie() method starting with Grover Push the frame associated with each dog’s method call on the stack When we reach Lassie, we return “true” This value gets passed as the result from the 4 th frame to the 3 rd frame The 3 rd frame passes this to the 2 nd frame The 2 nd frame passes this to the 1 st frame And “true” is the final result.

7 From Goodrich & Tamassia

8 Recursion vs. Iteration adding up consecutive integers

9 Illustrated Output

10 Methods and the Java VM Stack Each time a method is called –A new copy of the method’s data is pushed on the JVM stack This data includes local variables –This applies to all methods, not only recursive ones The “return” command does two things: –Pops the data for the method off the stack Local variables disappear –Passes the return value to the calling method If there is no return statement –The method is popped –Nothing is passed to the calling method

11 Recursion & the Java VM Stack First time called –Push a stack frame with n=12 –This won’t complete until after the recursive call returns Second time called –Push a stack frame with n=11 –… and so on

12 Recursion & the Java VM Stack When a frame is pushed with n==0 –The frame is popped –The value returned is 0 –This is sent to the frame in which n==1 –It can now add the 0 to n –… and so on

13 Recursion vs. Loops We could use recursion as an alternative to a loop for our various summation problems. This does not reduce (or increase) the O(g(n)) of the problem. public static int gaussianSum (int n) { if (n <= 1) return 1; else return n + gaussianSum(n-1); }

14 Recursion Example: Drawing Fractals This is from Main CH. 8 Main Idea: –Drawing a line of a given length –Divide the line in half, and Recursively draw the line in a certain way –Boring version: Draw the half as a horizontal line –Interesting version: Choose random numbers to determine the Y coordinates, but ensure that the halves all hook up.

15 Setting up Boring Fractal

16 Recursive Part of Boring Fractal

17 Output of Boring Fractal

18 Setting up Random Fractal (nearly the same as for Boring Fractal)

19 Recursive Part of Random Fractal

20 Output of Random Fractal (1)

21 Output of Random Fractal (2)

22 Summary: Recursion Recursion is a useful problem-solving technique –We will see it again when we study trees and sorting But it takes some getting used to. To help: –Study the Main chapter (quite well-written) –Remember that each method call gets put on the stack until the base case is found; then the calls are popped off the stack. –You HAVE TO understand basic programming concepts like Method calls Returning from methods Passing parameters (formal vs actual parameters)