1 / 87

Recursion

Recursion. Chapter 7. Chapter Objectives. To understand how to think recursively To learn how to trace a recursive method Show how recursion is used in math formulas To learn how to write recursive algorithms and methods for searching arrays

ninon
Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion Chapter 7

  2. Chapter Objectives • To understand how to think recursively • To learn how to trace a recursive method • Show how recursion is used in math formulas • To learn how to write recursive algorithms and methods for searching arrays • To learn about recursive data structures and recursive methods for a LinkedList class • To understand how to use recursion to solve the Towers of Hanoi problem Chapter 7: Recursion

  3. Chapter Objectives (continued) • To understand how to use recursion to process two-dimensional images • To learn how to apply backtracking to solve search problems such as finding a path through a maze Chapter 7: Recursion

  4. Recursive Thinking • Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways • Recursion splits a problem into one or more simpler versions of itself Chapter 7: Recursion

  5. Recursive Thinking Chapter 7: Recursion

  6. Recursive Algorithm • A given problem is a candidate for recursive solution if you can define a base case and a recursive case • Base case: There is at least one case, for a small value of n, that can be solved directly • Recursive case: A problem of a given size n can be split into one or more smaller versions of the same problem Chapter 7: Recursion

  7. Steps to Design a Recursive Algorithm • Recognize the base case and provide a solution to it • Devise a strategy to • Split the problem into smaller versions of itself • Smaller versions must progress toward base case • Then to solve the original problem… • …combine the solutions of the smaller (or smallest) problems in such a way as to solve the problem Chapter 7: Recursion

  8. Recursive Algorithm to Search Array • Given: Array of n elements, sorted in increasing order • Replace problem of searching n elements by problem of searching n/2 elements • To find target element… • Look at element in the middle • If middle element equals target, then done • Else if target is less than middle element • Repeat search on first half • Else (target is greater than middle element) • Repeat search on second half Chapter 7: Recursion

  9. Recursive Algorithm to Search Array • Suppose we search for “7” in this array of 25 elements: 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 • Middle element is 41 • Since 7 is less than 41, repeat search using 1st half: 2,3,5,7,11,13,17,19,23,29,31,37 • Middle element is 13 • Since 7 is less then 13, repeat using 1st half: 2,3,5,7,11 • Middle element is 5 • Since 7 is greater than 5, repeat using 2nd half 7,11 • And so on… Chapter 7: Recursion

  10. Recursive Algorithm to Search Array • If array is empty • Return -1 • Else if middle element matches target • Return subscript of element • Else if target is less than middle element • Recursively search first half of array • Else • Recursively search second half of array Chapter 7: Recursion

  11. General Recursive algorithm • If the problem can be solved for current value of n • Solve it (base case) • Else • Recursively apply the algorithm to one or more problems with smaller value(s) of n (recursive case) Chapter 7: Recursion

  12. Steps to Design Recursive Algorithm • In general… • A base case (small n) that can be solved directly • Problem of size n can be split into smaller version(s) of the same problem (recursive case) • To design a recursive algorithm, we must • Recognize base case, and provide solution to it • Devise a strategy to split problem into smaller versions of itself • Combine solutions to smaller problems so that large problem is solved correctly Chapter 7: Recursion

  13. Recursive String Length Algorithm • How to find length of a string? • Lots of ways to do this • How about a recursive solution? • Length of empty string is 0 • Length of non-empty string is length of first character plus length of the “rest of the string” • That is, 1 + length of rest of the string • For example, length of “abcd” is 1 + length of “bcd” • And so on… Chapter 7: Recursion

  14. Recursive String Length Algorithm Chapter 7: Recursion

  15. Recursive String Length Algorithm • In Java publicstaticint length(String str) { if (str == null || str.equals("")) return0; else return1 + length(str.substring(1)); } Chapter 7: Recursion

  16. Run-Time Stack and Activation Frames • “Activation frame” pushed onto run-time stack • Run-time stack is like “scratch paper” • OS can save information (keep state) • For later use Chapter 7: Recursion

  17. Tracing Recursive String Length Method 1 Chapter 7: Recursion

  18. Proving that a Recursive Method is Correct • Proof by induction • Prove the theorem is true for the base case • Show that if the theorem is assumed true for n, then it must be true for n+1 • Proof of recursion is similar to induction • Verify that the base case is solved correctly • Verify that each recursive case progresses towards the base case • Verify that if all smaller problems are solved correctly, then the original problem is also solved correctly Chapter 7: Recursion

  19. Prove Recursive String Length is Correct • Base case? • Empty string is of length 0 • Recursive case makes progress towards base case? • String gets smaller by 1 each time • Show that if smaller problem solved correctly, then original problem is solved correctly • Smaller problem: length(str.substring(1)) • If small problem correct, then length of original string is correct: 1 + length(str.substring(1)) Chapter 7: Recursion

  20. Recursive Math Formulas • Mathematics has many naturally recursive formulas • Examples include: • Factorial • Powers • Greatest common divisor • Note that if recursive problem is too big, a stack overflow error will occur • Space available for run-time stack is limited Chapter 7: Recursion

  21. Factorial • How do you pronounce “n!” ? • As you know, n! = n  (n-1)  (n-2)  … 1 • And 0! = 1 • Recursive definition? • We have: n! = n  (n-1)! • Can easily write a recursive method for factorial… Chapter 7: Recursion

  22. Recursive Factorial Method Chapter 7: Recursion

  23. Exponentiation • If n is a non-negative integer, xn is x times itself, n times • And x0 = 1 • For example, 27 = 2  2  2  2  2  2  2 = 128 • Recursive definition? • We have: xn = x  xn-1 • Can easily write recursive method… Chapter 7: Recursion

  24. Recursive Exponentiation Method Chapter 7: Recursion

  25. Recursion Versus Iteration • What is iteration? • A loop repetition condition determines whether to repeat the loop body or exit from the loop • In recursion, the condition tests for a base case • There are similarities between recursion and iteration • You can always write an iterative solution to a problem that is solvable by recursion • You are probably more familiar with iteration • So, why bother with recursion? • Recursive code often simpler than an iterative algorithm and thus easier to write, read, and debug Chapter 7: Recursion

  26. Tail Recursion • “Tail recursion” or “last-line recursion” • Single recursive call, and it is last line of the method • All of the examples we have considered so far are examples of tail recursion • Easy to convert tail recursive method to iterative method • Example on the next slide… Chapter 7: Recursion

  27. Iterative Factorial Method Chapter 7: Recursion

  28. Efficiency of Recursion • Is recursion more or less efficient than iteration? • Recursive methods often slower than iteration: Why? • The overhead for loop repetition is smaller than the overhead for a method call and return • Recall, run-time stack • If it is easier to conceptualize an algorithm using recursion, then you should code it as a recursive method • The reduction in efficiency does not outweigh the advantage of readable code that is easy to debug Chapter 7: Recursion

  29. Fibonacci Numbers • Fibonacci numbers developed to model rabbit population • Defined as fib1 = 1, fib2 = 1, fibn = fibn-1 + fibn-2 • The first several Fibonacci numbers are 1,1,2,3,5,8,13,21,34,55,89,144,233,377,… • Easy to write a recursive method for Fibonacci numbers • See next slide… Chapter 7: Recursion

  30. Inefficient Recursive Fibonacci Method Chapter 7: Recursion

  31. Inefficient Recursive Fibonacci Method • Why is the obvious Fibonacci recursion inefficient? • Consider fibonacci(7) • Compute fibonacci(6) and fibonacci(5) • Then fibonacci(5), fibonacci(4), fibonacci(4) fibonacci(3) • Then fibonacci(4), fibonacci(3), fibonacci(3), fibonacci(2), fibonacci(3), fibonacci(2), fibonacci(2), fibonacci(1) • And so on… • Why is this inefficient? Chapter 7: Recursion

  32. Inefficient Recursive Fibonacci • How inefficient is this? • Exponential time!!! • If n = 100, need about 2100 activation frames Chapter 7: Recursion

  33. Efficient Recursive Fibonacci Method • An O(n) Fibonacci algorithm… • If we know current and previous Fibonacci numbers • Then next Fibonacci number is current + previous • Method fibo • 1st argument is current Fibonacci (fibCurrent) • 2nd argument is previous Fibonacci (fibPrevious) • 3rd argument is n • When n = 1, base case, so return fibCurrent • Otherwise, return fibo(fibCurrent + fibPrevious, fibCurrent, n - 1) • Start by computing: fibo(1, 0, n) Chapter 7: Recursion

  34. Efficient Recursive Fibonacci Method Chapter 7: Recursion

  35. Efficient Recursive Fibonacci Method How efficient? Chapter 7: Recursion

  36. Dynamic Programming • We want to find “best” stagecoach route from A to J • Where ”best” == shortest distance Chapter 7: Recursion

  37. Dynamic Programming • Let F(X) be shortest distance from A to X • Note that, for example, F(J) = min{F(H) + 3, F(I) + 4} • This provides a recursive method to find F(J)… Chapter 7: Recursion

  38. Dynamic Programming • F(A) = 0 0 Chapter 7: Recursion

  39. Dynamic Programming • F(B) = 2 • F(C) = 4 • F(D) = 3 2 4 0 3 Chapter 7: Recursion

  40. Dynamic Programming • F(E) = min{F(B) + 7, F(C) + 3, F(D) + 4} = min{9,7,7} = 7 • F(F) = min{F(B) + 4, F(C) + 2, F(D) + 1} = min{6,6,4} = 4 • F(G) = min{F(B) + 6, F(C) + 4, F(D) + 5} = min{8,8,8} = 8 7 2 4 4 0 3 8 Chapter 7: Recursion

  41. Dynamic Programming • F(H) = min{F(E) + 1, F(F) + 6, F(G) + 3} = min{8,10,11} = 8 • F(I) = min{F(E) + 4, F(F) + 3, F(G) + 3} = min{11,7,11} = 7 7 2 8 4 4 0 7 3 8 Chapter 7: Recursion

  42. Dynamic Programming • F(J) = min{F(H) + 3, F(I) + 4} = min{11,11} = 11 7 2 8 11 4 4 0 7 3 8 Chapter 7: Recursion

  43. Dynamic Programming • The shortest path(s) from A to J have distance 11 • In this example, the shortest path is not unique • How to find best path? • As opposed to shortest distance 7 2 8 11 4 4 0 7 3 8 Chapter 7: Recursion

  44. Recursive Array Search • Searching an array can be accomplished using recursion • Simplest way to search is a linear search • Examine one element at a time starting with the first element and ending with the last • Base case for recursive search is an empty array • Result is negative one • Another base case would be when the array element being examined matches the target • Recursive step is to search the rest of the array, excluding the element just examined Chapter 7: Recursion

  45. Algorithm for Recursive Linear Array Search Chapter 7: Recursion

  46. Implementation of Recursive Linear Search Chapter 7: Recursion

  47. Design of a Binary Search Algorithm • Binary search can be performed only on an array that has been sorted • We assume array is in increasing order • Stop cases • The array is empty • Element being examined matches the target • Check the middle element for a match with the target • Throw away the half of the array • Throw away the half that the target cannot be in Chapter 7: Recursion

  48. Binary Search Algorithm Chapter 7: Recursion

  49. Binary Search Example Chapter 7: Recursion

  50. Implementation of Binary Search Chapter 7: Recursion

More Related