and now for something completely different: recursion

Post on 19-Dec-2015

221 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

And now for something completely different: recursion

OverviewRecursive ThinkingRecursive JavaExample: maze searchAnalysis of recursive algorithms

Recursion p. 2/17

Recursive ThinkingA journey of 1000 miles begins with a single step.

– Ancient proverb

Algorithm: when you have a big job to do, divide it into parts. Pick the easiest one. Can you do it easily? If yes, do it. If not, divide it into parts. Pick the easiest one. Can you do it easily? If yes, do it, If not …

Recursion p. 3/17

Three requirements for recursion1. A self-similar problem2. A version of the problem that can be solved without

recursion (the “base case”)3. A way to move from a “larger” problem to a smaller one

Recursion p. 4/17

Using recursionCan we use recursion to: Search a list? Print a list? Draw a spiral? Draw a set of concentric circles? Compile a program?Why or why not?

Recursion p. 5/17

Recursive JavaConsider the example on p. 189 of your text.What’s the base case?How does the code move from a larger to a

smaller problem?In what way is this problem “self-similar”?

Recursion p. 6/17

When to use recursionWhen a problem meets the requirements for a

recursive solution, should you always use recursion? Always use a loop? How do you justify your decision?

Stay tuned …

Recursion p. 7/17

Example: maze searchConsider the maze search example on pp. 193—196.Where are the recursive call(s)?What are the base cases?How does the problem get smaller?Hand-trace the program through the first few

recursive calls. Check with your neighbor and see what they have. Then reconsider the above questions.

Recursion p. 8/17

Debugging recursionThe bug in the following code (if any) is:public boolean search (T item, MyList<T> list) {

this.search(item, list.getRest());

} // Note: // MyList may or may not be orderedA. There is no base caseB. The problem is not self-similarC. There is a base case, but the problem does not

get smallerD. Some other bugE. There are no bugs

Recursion p. 9/17

Debugging recursion (2)The bug in the following code (if any) is:public boolean search (T item, MyList<T> list) {

if (list == null) return false;

else if (list.getFirst()==item)return true;

else return (this.search(item, list);

}

A. There is no base caseB. The problem is not self-similarC. There is a base case, but the problem does not get smallerD. Some other bugE. There are no bugs

Recursion p. 10/17

Debugging recursion (3)The bug in the following code (if any) is:public boolean search (T item, MyList<T> list) {

if (list == null) return false;

else if (list.getFirst()==item)return true;

else return (this.search(item, list.getRest());

}

A. There is no base caseB. The problem is not self-similarC. There is a base case, but the problem does not get smallerD. Some other bugE. There are no bugs

Recursion p. 11/17

Analyzing recursive algorithmspublic int factorial (int n) {

if (n==0) return 1;

else if (n>0)

return n*factorial(n-1);

else throw InvalidInputException(“Negative input”);

}

The Big-Oh time complexity of this method is:A. O(1)B. O(log2n)

C. O(n)D.O(n2)E. None of the above

Recursion p. 12/17

Analyzing recursive algorithmsRewrite the factorial method using a loop instead of

recursion:

public int factorial (int n) {

if (n==0) return 1;

else if (n>0)

return n*factorial(n-1);

else throw InvalidInputException(“Negative input”);

}

What is the time complexity of your iterative method?

Recursion p. 13/17

Analyzing recursive algorithmspublic int fibonacci (int n) {

if (n==1) return 1;

else if (n==2) return 1;

else if (n>2) return fibonacci(n-1)+fibonacci(n-2);

else throw

InvalidInputException(“Must be positive”);

}

The Big-Oh time complexity of this method is:A. O(1)B. O(log2n)

C. O(n)D.O(n2)E. None of the above Recursion p. 14/17

Analyzing recursive algorithmsCan we rewrite this method without recursion?

public int fibonacci (int n) {

if (n==1) return 1;

else if (n==2) return 1;

else if (n>2) return fibonacci(n-1)+fibonacci(n-2);

else throw

InvalidInputException(“Must be positive”);

}

Recursion p. 15/17

Recursion: a summaryWhat are the three requirements for using

recursion?Given a problem, can you determine whether it

can be solved with recursion?Can you analyze a recursive program?

Recursion p. 16/17

Coming attractionsNext week, we’ll review searching and sortingThen we’ll put recursion, searching, and

sorting together in the context of our next data structure, Trees

 Homework: read chapter 8 (or equivalent)

Recursion p. 17/17

top related