1 / 136

Chapter 19 : Recursion

Chapter 19 : Recursion. Objectives. After studying this chapter you should understand the following: recursion, and the relationship between recursive and iterative algorithms; the design of the quick sort algorithm; indirect recursion; backtracking and the class of problems it solves;

umed
Download Presentation

Chapter 19 : 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. Chapter 19 : Recursion

  2. Objectives • After studying this chapter you should understand the following: • recursion, and the relationship between recursive and iterative algorithms; • the design of the quick sort algorithm; • indirect recursion; • backtracking and the class of problems it solves; • object recursion and its class structure. NH-Chapter 19

  3. Objectives • Also, you should be able to: • write simple recursive algorithms; • use backtracking in the solution to problems; • structure class definitions to use object recursion. NH-Chapter 19

  4. Iterative Algorithms • Algorithm • Specifies a step in the solution process. • Step is iterated. • After each iteration, we are closer to solution. • Solution is reached after a finite number of iterations. NH-Chapter 19

  5. Recursion • Algorithms • Solve a trivial, basic case of problem, • Solution to general case is reduced to one that is a step closer to basic case. NH-Chapter 19

  6. Iteration v.s. Recursion • Reducing general case to a easier case roughly corresponds to a single iterative step. • Reaching base case stops recursion as exit condition stops iteration. NH-Chapter 19

  7. Iteration v.s. Recursion • In an iterative solution • step toward solution until while condition is false. • explicitly drive repetition with a loop. • In a recursive solution, • reduce or “unwind” problem until base case. • write a solution for base case and for reduction step. NH-Chapter 19

  8. Recursive solution form if ( trivial case ) solve directly else solve in terms of a slightly easier case NH-Chapter 19

  9. Sorting a list • Trivial case • size of list is one or zero. • General case • Size of list is greater than 1. • Slightly easier problem • reduce general case to sort a list with one fewer element. NH-Chapter 19

  10. Sorting a list if ( list is empty or has one element ) solution is easy: do nothing else sort the list, assuming a way of sorting a listwith one fewer element is available NH-Chapter 19

  11. Recursive algorithm implementation • Trivial case • Solved directly. • General case • algorithm invokesitself to solve a slightly reduced case. • solution is built from solution of slightly reduced case. NH-Chapter 19

  12. Recursive algorithm execution • Results in a chain of self-calls, • each a slightly easier problem to solve that previous. • Finally method is invoked with trivial case. NH-Chapter 19

  13. Recursive algorithm correctness • Must guarantee that general case will eventually reduce to basic case. NH-Chapter 19

  14. Example: Exponentiation public static int power (int number, int exponent) The specified number raised to the specified power. require:exponent >= 0 NH-Chapter 19

  15. Exponentiation iterative solution public static int power (int number, int exponent) { int result = 1; int count = 0; while (count != exponent) { result = number * result; count = count + 1; } return result; } • Invariant: result equals number raised to the count power. • Exponent requirement to be >= 0 ensures iteration will terminate. NH-Chapter 19

  16. Exponentiation recursive solution • Base cases: raise integer to the power 0. • General case: raise integer to the power n, n is an integer and n > 0. • Reduction step: raising a number to the power n-1. NH-Chapter 19

  17. Exponentiation recursive solution • Compute number to the power n, assuming we have already computed number to the power n-1. • If we have numbern-1, • multiply this value by number to get numbern. • number n-1 is gotten by a self-call: power (number, n-1) NH-Chapter 19

  18. Exponentiation recursive solution /** * The specified number raised to the specified power. * require: exponent >= 0 */ public static int power (int number, int exponent) { int result; if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; } NH-Chapter 19

  19. Tracing recursion: invoking power(2,3) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 3 exponent result NH-Chapter 19

  20. Tracing recursion : invoking power(2,3) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 3 exponent result NH-Chapter 19

  21. Tracing recursion : invoking power(2,3) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 3 exponent result invoke NH-Chapter 19

  22. Tracing recursion : invoking power(2,2) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 2 exponent result NH-Chapter 19

  23. Tracing recursion : invoking power(2,2) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 2 exponent result NH-Chapter 19

  24. Tracing recursion : invoking power(2,2) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 2 exponent result invoke NH-Chapter 19

  25. Tracing recursion : invoking power(2,1) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 1 exponent result NH-Chapter 19

  26. Tracing recursion : invoking power(2,1) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 1 exponent result NH-Chapter 19

  27. Tracing recursion : invoking power(2,1) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 1 exponent result invoke NH-Chapter 19

  28. Tracing recursion : invoking power(2,0) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 0 exponent result NH-Chapter 19

  29. Tracing recursion : invoking power(2,0) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 0 exponent result 1 NH-Chapter 19

  30. Tracing recursion : invoking power(2,0) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 0 exponent result 1 NH-Chapter 19

  31. Tracing recursion:resuming power(2,1) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 1 exponent result Result of previous call 1 NH-Chapter 19

  32. Tracing recursion :resuming power(2,1) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 1 exponent result 2 NH-Chapter 19

  33. Tracing recursion :resuming power(2,1) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 1 exponent result 2 NH-Chapter 19

  34. Tracing recursion :resuming power(2,2) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 2 exponent result Result of previous call 2 NH-Chapter 19

  35. Tracing recursion :resuming power(2,2) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 2 exponent result 4 NH-Chapter 19

  36. Tracing recursion :resuming power(2,2) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 2 exponent result 4 NH-Chapter 19

  37. Tracing recursion :resuming power(2,3) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 3 exponent result Result of previous call 4 NH-Chapter 19

  38. Tracing recursion :resuming power(2,3) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 3 exponent result 8 NH-Chapter 19

  39. Tracing recursion :resuming power(2,3) if (exponent == 0) result = 1; else result = number * power(number,exponent-1); return result; number 2 3 exponent result 8 NH-Chapter 19

  40. Finding the minimum element: Recursive version public int minFinalExam (List<Student> students) The lowest final exam grades of the specified Students. require:students.size() > 0 NH-Chapter 19

  41. Finding the minimum element: Recursive version • Base case: find smallest on list containing one element. • General case: find smallest on list with n elements, where n > 1. • Reducing general case: find smallest on list with n-1 elements. NH-Chapter 19

  42. Finding the minimum element: Recursive version • how to find smallest of n elements, assuming we know how to find smallest of n-1 elements. • Find smallest of the n-1 elements after first. • Find smallest of this and the first. NH-Chapter 19

  43. Finding the minimum element: Recursive version private int minFinalExam (List<Student> students,int first) The lowest final exam grades of Students on the list with indexes greater than or equal to first. require: 0 <= first && first < students.size() public int minFinalExam (List<Student> students) { return minFinalExam(students,0); } NH-Chapter 19

  44. Finding the minimum element: Recursive version private int minFinalExam (List<Student> students,int first) { int smallest; int gradeOfFirst = students.get(first).finalExam(); if (first == students.size()-1) { smallest = gradeOfFirst; // the base case else {// the general case: int minOfRest = minFinalExam(students,first+1); if (minOfRest < gradeOfFirst) smallest = minOfRest; else smallest = gradeOfFirst; } return smallest; } NH-Chapter 19

  45. Selection sort: recursive version • Base cases: sort an empty list or with 1 element. • General case: sort a list containing n elements, n > 1. • Reducing general case: sort a list containing n-1 elements. NH-Chapter 19

  46. Selection sort: recursive version • Find the smallest element and put it in first place. • Sort the remaining n-1 elements. • Note: sorting the remaining n-1 elements, refers to a segment of the list first remaining n-1 elements NH-Chapter 19

  47. Selection sort: recursive version private static <Element> void selectionSort (List<Element>list,int first,Order<Element>order) { if (first < list.size()) { find the smallest element and put it first; sort the remaining n-1 elements } } NH-Chapter 19

  48. Selection sort: recursive version private static <Element> void selectionSort (List<Element>list,int first,Order<Element>order) { if (first < list.size()) { int small = smallestOf(list,first,list.size()-1,order); interchange(list,first,small); selectionSort(list,first+1,order); } } NH-Chapter 19

  49. Selection sort: recursive version public static <Element> void selectionSort ( List<Element>list,Order<Element>order){ selectionSort(list,0,order); } NH-Chapter 19

  50. Towers of Hanoi • move stack of disks from starting peg to one of the other pegs. Disks are moved one at a time, and a disk can never be placed on top of a smaller one. NH-Chapter 19

More Related