recursion. recursive methods have: 1.a base case or termination condition that causes the method to...

13
Recursion

Upload: leon-kelly

Post on 31-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Recursion

Page 2: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Recursive Methods

HAVE:1. A base case or termination condition that

causes the method to end2. A non-base case whose actions move the

algorithm toward the base case and termination

Page 3: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

General Form:public static void recursiveMethod(){

if(base case)//something

else{

//somethingrecursiveMethod();

}}

Page 4: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Base Case

• Often times a numeric value like 0 or 1• A sentinel value• End of file

Page 5: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Example 1

• public static void stackWords()– The program will read in a list of words terminated

with a period, and then print the list in reverse order• Trace through a run of the program• Notes:– Each time stackWords() is called, a new local variable is

created– When an invocation of the method actually terminates,

the program returns to complete the most recently invoked previous call

Page 6: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Example 2

• public void drawTriangle(int n)• Produces the following output for

drawTriangle(3):***

***That’s all folks!

Page 7: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Notes/Terms/Exercises

• A method that has no pending statements following a recursive call is an example of tail recursion– Identify the examples we did that demonstrate tail

recursion• Identify the base case in each example• Infinite recursion occurs if you have no base case,

or the base case is never reached. This will eventually cause a StackOverflowError– Write a method to demonstrate infinite recursion

Page 8: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

• Write a method that returns the result of the following summation:

Page 9: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Independent Examples

1. Write a method that returns n!– Write two versions of this method, an iterative

version and a recursive version

2. Write a method that returns the nth term in the fibonacci sequence– Write two versions of this method, an iterative

version and a recursive version

Page 10: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Analyzing Recursive Algorithms

• Every recursive algorithm can be written iteratively

• Let’s examine Fibonacci’s recursive version:– How many calls to Fib() are required to find Fib(5)?– When a method call results in two or more other

method calls, this is a red flag for an exponential algorithm (very inefficient!!)

Page 11: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Rules for Recursion

1. Avoid recursion for algorithms that involve large local arrays – too many recursive calls can cause memory overflow

2. Use recursion when it significantly simplifies code3. Avoid recursion for simple iterative methods like

factorial and Fibonacci4. Recursion is especially useful for:– Branching processes– Divide and conquer algorithms like binary search (we’ll

see this soon!)

Page 12: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Recursion in Two-D Grids• Example Scenarios:

– A game board from which you must remove pieces– A maze with walls and paths from which you must try to escape– White “containers” enclosed by black “walls” into which you must “pour

paint”• Generally, you will be given a starting point (row, col), and instructions

on what to do• The recursive solution generally looks something like this:

Check that starting point is not out of rangeif(starting position satisfies some requirement)

perform some action to solve problemrecursiveCall(row + 1, col);recursiveCall(row - 1, col);recursiveCall(row, col + 1);recursiveCall(row, col - 1);

Page 13: Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm

Example

Picture a square grid that can contain either black or white cells. Two cells in the grid are part of the same “blob” if each is black and there is a sequence of moves from one cell to the other, where each move is either horizontal or vertical to an adjacent black cell. (See example).

See Image class and implement the eraseBlob method using recursion