1 / 46

Recursion breaks a problem into several smaller instances of the same problem.

Recursion breaks a problem into several smaller instances of the same problem. Some recursive solutions are impractical because they are so inefficient. Recursive Solutions. Figure 2.1 A recursive solution. How to define the problem in terms of a smaller problem of the same type

bondm
Download Presentation

Recursion breaks a problem into several smaller instances of the same problem.

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 breaks a problem into several smaller instances of the same problem. Some recursive solutions are impractical because they are so inefficient. Recursive Solutions

  2. Figure 2.1 A recursive solution

  3. How to define the problem in terms of a smaller problem of the same type How each recursive call diminishes the size of the problem What instance of the problem can serve as the base case As the problem size diminishes, the base case is reached Issues for Constructing Recursive Solutions

  4. Factorial(n) = n*(n-1)*(n-2)*…*1 for n>0 = n*factorial(n-1) Factorial(0) = 1 Factorial of n

  5. public static int fact(int n) { // --------------------------------------------------- // Computes the factorial of a nonnegative integer. // Precondition: n must be greater than or equal to 0. // Postcondition: Returns the factorial of n. // --------------------------------------------------- if (n == 0) { return 1; } else { return n * fact(n-1); } // end if } // end fact Factorial of n

  6. Figure 2.2 fact(3)

  7. Figure 2.3 A box

  8. Figure 2.4 The beginning of the box trace

  9. Figure 2.5a Box trace of fact(3)

  10. Figure 2.5b Box trace of fact(3)

  11. Figure 2.5c Box trace of fact(3)

  12. Problem: write a string in reverse order Base case: write the empty string backward Strip away the last character or Strip away the first character Writing a String Backward

  13. Figure 2.6 A recursive solution

  14. public static void writeBackward(String s, int size) { // --------------------------------------------------- // Writes a character string backward. // Precondition: The string s contains size // characters, where size >= 0. // Postcondition: s is written backward, but remains // unchanged. // --------------------------------------------------- if (size > 0) { // write the last character System.out.println(s.substring(size-1, size)); // write the rest of the string backward writeBackward(s, size-1); // Point A } // end if // size == 0 is the base case - do nothing } // end writeBackward

  15. Figure 2.7a Box trace of writeBackward(“cat”, 3)

  16. Figure 2.7c Box trace of writeBackward(“cat”, 3)

  17. writeBackward2(s) System.out.println(“Enter writeBackward2, string: “ + s); if (the string s is empty) { Do nothing -- this is the base case } else { writeBackward2 (s minus its first character) // Point A System.out.println(“About to write first character of “ + “string: “ + s); Write the first character of s } // end if System.out.println(“Leave writeBackward2, string: “ + s);

  18. Figure 2.9a Box trace of writeBackward2(“cat”, 3) in pseudocode

  19. Figure 2.9b Box trace of writeBackward2(“cat”, 3) in pseudocode

  20. Figure 2.9c Box trace of writeBackward2(“cat”, 3) in pseudocode

  21. Figure 2.9d Box trace of writeBackward2(“cat”, 3) in pseudocode

  22. Figure 2.9e Box trace of writeBackward2(“cat”, 3) in pseudocode

  23. Problem: Count the number of rabbits assuming the following Rabbits never die. A pair of original rabbits. A pair of rabbits gives birth every month, exactly two months after they are born. Rabbits are always born in male-female pair. rabbit(n) = rabbit(n-1) + rabbit(n-2) Multiplying Rabbits (The Fibonacci Sequence)

  24. Figure 2.10 Recursive solution to the rabbit problem

  25. Figure 2.11 Recursive calls that rabbit(7) generates

  26. public static int binarySearch(int anArray[], int first, int last, int value) { // Searches the array items anArray[first] through // anArray[last] for value by using a binary search. // Precondition: 0 <= first, last <= SIZE-1, where // SIZE is the maximum size of the array, and // anArray[first] <= anArray[first+1] <= ... <= // anArray[last]. // Postcondition: If value is in the array, the method // returns the index of the array item that equals value; // otherwise the method returns -1. int index; if (first > last) { index = -1; // value not in original array } Binary Search

  27. else { // Invariant: If value is in anArray, // anArray[first] <= value <= anArray[last] int mid = (first + last)/2; if (value == anArray[mid]) { index = mid; // value found at anArray[mid] } else if (value < anArray[mid]) { // point X index = binarySearch(anArray,first,mid-1, value); } else { // point Y index = binarySearch(anArray,mid+1,last,value); } // end if } // end if return index; } // end binarySearch

  28. Figure 2.15 Box traces of binarySearch with anArray = <1,5,9,12,15,21,29,31>: a) a successful search for 9; b) an unsuccessful search for 6

  29. Figure 2.16 Box trace with reference to an array

  30. Figure 2.17 A sample array

  31. Selecting a pivot item in the array Arranging, or partitioning, the items in the array about this pivot item Recursively applying the strategy to one of the partitions Find the kth Smallest Item in an Arbitrary Array

  32. Figure 2.18 A partition about a pivot

  33. Three poles: A (the source), B (the destination), and C (the spare) with n disks The disks are of different sizes Move the disks from pole A to pole B, using pole C. A disk could be placed only on top of a disk larger than itself. The Towers of Hanoi

  34. Figure 2.19a and b a) The initial state; b) move n - 1 disks from A to C

  35. Figure 2.19c and d c) move one disk from A to B; d) move n - 1 disks from C to B

  36. public static void solveTowers(int count, char source, char destination, char spare) { if (count == 1) { System.out.println("Move top disk from pole " + source + " to pole " + destination); } else { solveTowers(count-1, source, spare, destination); // X solveTowers(1, source, destination, spare); // Y solveTowers(count-1, spare, destination, source); // Z } // end if } // end solveTowers The Towers of Hanoi

  37. Figure 2.20 The order of recursive calls that results from solveTowers(3, A, B, C)

  38. Figure 2.21a Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

  39. Figure 2.21b Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

  40. Figure 2.21c Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

  41. Figure 2.21d Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

  42. Figure 2.21e Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

  43. The overhead associated with method calls The inherent inefficiency of some recursive algorithms Recursion and Efficiency

  44. static int iterativeRabbit(int n) { // Iterative solution to the rabbit problem. // initialize base cases: int previous = 1; // initially rabbit(1) int current = 1; // initially rabbit(2) int next = 1; // result when n is 1 or 2 // compute next rabbit values when n >= 3 for (int i = 3; i <= n; i++) { // current is rabbit(i-1), previous is rabbit(i-2) next = current + previous; // rabbit(i) previous = current; // get ready for current = next; // next iteration } // end for return next; } // end iterativeRabbit Iterative Solution for Counting Rabbits

  45. public static void writeBackward(String s, int size) { if (size > 0) { // write the last character System.out.println(s.substring(size-1, size)); writeBackward(s, size - 1); // write rest } // end if } // end writeBackward public static void writeBackward(String s, int size) { // Iterative version. while (size > 0) { System.out.println(s.substring(size-1, size)); --size; } // end while } // end writeBackward Iterative Solution for Writing String Backward

More Related