About the project • For the Movie class Recursion I – In equals be sure to test all cases • When the other object is not a Movie • When the other object is null Methods that call themselves • When the other object is a Movie and some of it’s members are null. Reminder Recursive Functions • 1 st Exam • A recursive function is a function that is – Wednesday defined in terms of itself. – Will cover – Example: factorial • Inheritance } • Exceptions ! { • I/O = 1 if n 1 – 10-20 questions = n – Variety of question types − n * ( n 1 )! otherwise • Short answer • Fill in the code • Step through the code • Perhaps some multiple choice Recursive Functions Recursive Methods • A recursive method is one that can call • Example: Factorial itself 4! = 4 * 3! = 4 * (3 * 2!) Non-recursive Recursive methodA() { methodB() { = 4 * (3 * (2 * 1!)) … … = 4 * (3 * (2 * (1 * 1)))) methodB (); methodB (); = 24 … … } } 1
Recursive Methods Recursive Methods • Let’s code a recursive function for factorial: • Let’s code a recursive function for factorial: if N = 1 • factorial(N) = 1 = N * factorial (N-1) } otherwise ! { = 1 if n 1 = n − n * ( n 1 )! otherwise Recursive Methods Recursive Methods • Example: int fact = factorial (4); • Let’s code a recursive function for factorial: – factorial(4) = 4 * factorial(3) – = 4 * (3 * factorial(2)) public int factorial (int N) – = 4 * (3 * (2 * factorial(1))) { – = 4 * (3 * (2 * (1 * 1))) if (N == 1) return 1; – = 4 * (3 * (2 * 1)) else return N * factorial (N-1); – = 4 * (3 * 2) } – = 4 * 6 – = 24 Calling recursive functions Calling recursive functions N=4 4 * 6 = 24 public int factorial (int N) { • But can’t recursion go on forever? N=2 if (N==1) return 1; public int factorial (int N) { else return N * factorial(N-1) if (N==1) return 1; } else return N * factorial(N-1) 3 * 2 = 6 } N=3 public int factorial (int N) { 1 N=1 if (N==1) return 1; 2 * 1 = 2 else return N * factorial(N-1) public int factorial (int N) { } if (N==1) return 1; else return N * factorial(N-1) } 2
Calling recursive functions Components of a recursive methods • But can’t recursion go on forever? • Three necessary components for a – Yes, unless we insure that the recursion will recursive method: stop for a given condition 1. A test to stop or continue the recursion 2. An end case that stops the recursion public int factorial (int N) 3. A recursive call that continues the recursion. { if (N == 1) return 1; else return N * factorial (N-1); } Iterative Solutions Components of a recursive methods 1. Test 2. Stop • Each recursive solution has a corresponding iterative solution that doesn’t use recursion. public int factorial (int N) { – int factorial2 (int N) { if (N == 1) return 1; int fact = 1; else return N * factorial (N-1); for (i=1; i <= N; i++) fact *= } i; return i; 3. Continue } Iterative Solutions Another example • Then why use recursion? • Fibonacci Numbers: – Many times, the iterative solutions are far more = 1 if n 1 , 2 complex than the recursive solutions. = fib ( n ) − + − – Many times, it is easy and natural to express a fib ( n 2 ) fib ( n 1 ) otherwise solution using recursion. – Questions? 3
Another example Another example � recursive function for fibonacci: � Not a good candidate for recursion public int fib (int N) public int fib (int N) { { if (N <= 2) return 1; if (N <= 2) return 1; else else return fib(N-1) + fib (N-2) return fib(N-1) + fib (N-2) } } Duplicate effort When not to use recursion Recursion using arrays • The recursive solution results in duplicate • findsum – use recursion to find the sum of computation. values in an array x of size n. … 0 n-2 n -1 • Questions? Sum = value at n-1 + sum of values from 0 to n-2 Recursion using arrays Calling recursive functions • findsum – use recursion to find the sum of • But can’t recursion go on forever? values in an array x of size n. – findsum (x, n) = x[n-1] + findsum(x, n-1) 4
Recursion using arrays Recursion using arrays • findsum – use recursion to find the sum of • findsum – use recursion to find the sum of values in an array x of size n. values in an array x of size n. – findsum (x, n) = x[n] + findsum(x, n-1) public int findsum (int[] x, int n) – The recursion stops when n = 1 { if (n == 1) return x[0] • The sum of an array of one element is the value of the single element else • findsum (x, 1) = x[0] return x[n-1] + findsum(x, n-1) } Recursion using arrays Recursion using arrays return x[3] + 15 = 18 1. Test 2. Stop 6 7 2 3 findsum (x,4) call findsum (x,3) public int findsum (int[] x, int n) return x[2] + 13 = 15 6 7 2 { if (n == 1) return x[0] call findsum (x,2) return x[1] + 6 = 13 else 6 7 return x[n-1] + findsum(x, n-1) call findsum (x,1) } return x[0] = 6 6 3. Continue Questions? One last example • search() – given an array of ints, x , with • Let’s try one more example length n , return a boolean flag indicating if a given integer i is in the array 5
One last example One last example • search() – return whether a given public boolean search(int x[], int n, int i) integer, i, is in an array x of length n { if (x[n-1] == i) return true; … else 0 n-2 n -1 return search (x, n-1, i); If x[n-1] = = i then return true } otherwise perform a search on the remaining n-1 elements of the array One last example One last example • search() – return whether a given • But what happens when i is not in x? integer, i, is in an array x of length n – The recursion stops when either • i is found • The length n is equal to 0 – i will never be found in an array of length 0 – In this case, false will be returned. One last example One last example public boolean search(int x[], int n, int 1. Test 2. Stop i) { public boolean search(int x[], int n, int i) { if (n == 0) if (n == 0) return false; return false; else if (x[n-1] == i) else if (x[n-1] == i) return true; return true; else else return search (x, n-1, i); return search (x, n-1, i); } } 3. Continue 6
One last example Search (x, 4, 2) return true • Can you map out the search calling 1 2 3 4 search (x,4,2) sequence for?: call search(x,3,2) return true – x = [1,2,3,4] i=2 1 2 3 – x = [1,2,3,4] i=5 call search(x,2,2) return true 1 2 Search (x, 4, 6) search return false • Questions on search? 1 2 3 4 search (x,4,6) return false call search(x,3,6) 1 2 3 return false call search(x,2,6) 1 2 return false call search(x,1,6) return false 1 call search(x,0,6) When to use recursion Summary 1. A recursive solution is natural and easy to • Recursion – When a method calls itself understand • Components of a recursive method 2. When the recursive solution does not – Test result in excess computation – Stop 3. The equivalent iterative solution is more – Continue complex. • When to use recursion. 7
Next time • The Tower of Hanoi. • Questions? 8
Recommend
More recommend