Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.

Similar presentations


Presentation on theme: "Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem."— Presentation transcript:

1 Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem Solving with JAVA: Walls and Mirrors Carrano / Prichard Recursion: The Mirrors

2 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Recursion breaks a problem into several smaller instances of the same problem. Some recursive solutions are impractical because they are so inefficient. Recursive Solutions

3 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.1 A recursive solution

4 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

5 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Factorial(n) = n*(n-1)*(n-2)*…*1 for n>0 = n*factorial(n-1) Factorial(0) = 1 Factorial of n

6 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Factorial of n 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

7 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.2 fact(3)

8 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.3 A box

9 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.4 The beginning of the box trace

10 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.5a Box trace of fact(3)

11 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.5b Box trace of fact(3)

12 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.5c Box trace of fact(3)

13 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

14 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.6 A recursive solution

15 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

16 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.7a Box trace of writeBackward(“cat”, 3)

17 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.7c Box trace of writeBackward(“cat”, 3)

18 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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);

19 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.9a Box trace of writeBackward2(“cat”, 3) in pseudocode

20 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.9b Box trace of writeBackward2(“cat”, 3) in pseudocode

21 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.9c Box trace of writeBackward2(“cat”, 3) in pseudocode

22 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.9d Box trace of writeBackward2(“cat”, 3) in pseudocode

23 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.9e Box trace of writeBackward2(“cat”, 3) in pseudocode

24 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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)

25 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.10 Recursive solution to the rabbit problem

26 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.11 Recursive calls that rabbit(7) generates

27 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

28 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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 return index; } // end binarySearch

29 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.15 Box traces of binarySearch with anArray = : a) a successful search for 9; b) an unsuccessful search for 6

30 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.16 Box trace with reference to an array

31 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.17 A sample array

32 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

33 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.18 A partition about a pivot

34 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

35 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.19a and b a) The initial state; b) move n - 1 disks from A to C

36 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.19c and d c) move one disk from A to B; d) move n - 1 disks from C to B

37 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

38 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.20 The order of recursive calls that results from solveTowers(3, A, B, C)

39 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.21a Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

40 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.21b Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

41 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.21c Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

42 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.21d Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

43 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 2.21e Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

44 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley The overhead associated with method calls The inherent inefficiency of some recursive algorithms Recursion and Efficiency

45 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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

46 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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


Download ppt "Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem."

Similar presentations


Ads by Google