Download presentation
Presentation is loading. Please wait.
1
And now for something completely different: recursion
2
Overview Recursive Thinking Recursive Java Example: maze search Analysis of recursive algorithms Recursion p. 2/17
3
Recursive Thinking A 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
4
Three requirements for recursion 1. A self-similar problem 2. 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
5
Using recursion Can 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
6
Recursive Java Consider 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
7
When to use recursion When 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
8
Example: maze search Consider 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
9
Debugging recursion The bug in the following code (if any) is: public boolean search (T item, MyList list) { this.search(item, list.getRest()); } // Note: // MyList may or may not be ordered A. There is no base case B. The problem is not self-similar C. There is a base case, but the problem does not get smaller D. Some other bug E. There are no bugs Recursion p. 9/17
10
Debugging recursion (2) The bug in the following code (if any) is: public boolean search (T item, MyList list) { if (list == null) return false; else if (list.getFirst()==item)return true; else return (this.search(item, list); } A. There is no base case B. The problem is not self-similar C. There is a base case, but the problem does not get smaller D. Some other bug E. There are no bugs Recursion p. 10/17
11
Debugging recursion (3) The bug in the following code (if any) is: public boolean search (T item, MyList 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 case B. The problem is not self-similar C. There is a base case, but the problem does not get smaller D. Some other bug E. There are no bugs Recursion p. 11/17
12
Analyzing recursive algorithms public 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(log 2 n) C. O(n) D.O(n 2 ) E. None of the above Recursion p. 12/17
13
Analyzing recursive algorithms Rewrite 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
14
Analyzing recursive algorithms 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”); } The Big-Oh time complexity of this method is: A. O(1) B. O(log 2 n) C. O(n) D.O(n 2 ) E. None of the above Recursion p. 14/17
15
Analyzing recursive algorithms Can 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
16
Recursion: a summary What 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
17
Coming attractions Next week, we’ll review searching and sorting Then 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.