Foundations of Software Design Fall 2002 Marti Hearst

Post on 19-Jan-2016

25 views 3 download

description

Foundations of Software Design Fall 2002 Marti Hearst. Lecture 14: Intro to Recursion. Recursion. An algorithmic technique in which a function, in order to accomplish a task, calls itself with some part of the task. Recursion. A method that invokes itself Examples: - PowerPoint PPT Presentation

Transcript of Foundations of Software Design Fall 2002 Marti Hearst

1

Foundations of Software DesignFall 2002Marti 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 ExampleLet’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 argumentto 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 4th frame to the 3rd

frame• The 3rd frame passes this to the 2nd frame• The 2nd frame passes this to the 1st frame• And “true” is the final result.

7From Goodrich & Tamassia

8

Recursion vs. Iterationadding 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)