4_recursive

# 4_recursive - Recursion  !  !

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Recursion  !  !  Understand how the Fibonacci series is generated  Recursive Algorithms  !  Write simple recursive algorithms  !  Analyze simple recursive algorithms  !  Understand the drawbacks of recursion  !  Name other recursive algorithms and data  structures  John Edgar 2 !  What happens if you put a  pair of rabbits in a field?  Assume that rabbits take  one month to reach  maturity and that  Each pair of rabbits  produces another pair of  rabbits one month after  mating.  !  More rabbits!  !  !  John Edgar 3 !  How many pairs of rabbits  are there after 5 months?  !  Month 1: start – 1  !  Month 2: the rabbits are now  mature and can mate – 1  !  Month 3: – the first pair give  birth to two babies – 2  !  Month 4: the first pair give birth  to 2 babies, the pair born in  month 3 are now mature – 3  !  Month 5: the 3 pairs from month  4, and 2 new pairs – 5  John Edgar 4 !  After 5 months there are 5  pairs of rabbits  !  i.e. the number of pairs at 4  month: pairs: 1 1 2 1 3 2 456 3 5 8 !  While there are 3 pairs of  bunnies in month 4 only 2 of  them are able to mate  This series of numbers is  called the Fibonacci series  John Edgar 5 months (3) plus the number of  pairs at 3 months (2)  !  Why?  !  the ones alive in month 3  !  !  The nth number in the Fibonacci series, fib(n), is:  !  0 if n = 0, and 1 if n = 1  !  fib(n – 1) + fib(n – 2)  for any n > 1  !  e.g. what is fib(23)  !  Easy if we only knew fib(22) and fib(21)  !  The answer is fib(22) + fib(21)  !  What happens if we actually write a function to calculate  Fibonacci numbers like this?  John Edgar 6 !  Let's write a function just like the formula  !  fib(n) = 0 if n = 0, 1 if n = 1,   !  otherwise fib(n) = fib(n – 1) + fib(n – 2)  public static int fib(int n){ if(n == 0 || n == 1){ return n; }else{ return fib(n-1) + fib(n-2); } } Java The function  calls itself  John Edgar 7 !  The Fibonacci function is recursive  !  A recursive function calls itself  !  Each call to a recursive method results in a separate call to  !  Recursive functions are just like other functions  !  The invocation is pushed onto the call stack  !  And removed from the call stack when the end of a method  the method, with its own input  or a return statement is reached   !  Execution returns to the previous method call  John Edgar 8 public static int fib(int n) if(n == 0 || n == 1) return n else return fib(n-1) + fib(n-2) 5 fib(5)   3 2 fib(3)   1 fib(2)   fib(2)   0 fib(0)   1 fib(1)   0 fib(0)   1 fib(1)   fib(4)   2 fib(3)   1 fib(2)   1 fib(1)   0 fib(0)   John Edgar 1 1 fib(1)   1 fib(1)   9 !  When a function is called it is pushed onto the call  stack  !  This applies to each invocation of that function  !  When a recursive call is made execution switches to  that method call  !  The call stack records the line number of the previous  method where the call was made from  !  Once a method call execution finishes, returns to the  previous invocation  John Edgar 10 January 2010 Greg Mori 11 !  Recursive functions do not use loops to repeat  instructions  !  But use recursive calls, in if statements  !  Recursive functions consist of two or more cases,  there must be at least one  !  Base case, and one  !  Recursive case  John Edgar 12 !  The base case is a smaller problem with a  simpler solution  !  This problem’s solution must not be recursive  ▪  Otherwise the function may never terminate  !  There can be more than one base case  John Edgar 13 !  The recursive case is the same problem with  smaller input  !  The recursive case must include a recursive  function call  !  There can be more than one recursive case  John Edgar 14 !  Define the problem in terms of a smaller  problem of the same type  !  The recursive part  !  And the base case where the solution can be  !  e.g. return fib(n-1) + fib(n-2); easily calculated  !  This solution should not be recursive  !  e.g. if (n == 0 || n == 1) return n; John Edgar 15 !  How can the problem be defined in terms of  smaller problems of the same type?  problem size?  !  By 1, by half, …?  !  By how much does each recursive call reduce the  !  What are the base cases that can be solved  without recursion?  reduced?  !  Will a base case be reached as the problem size is  John Edgar 16 January 2010 Greg Mori 17 !  Linear Search  !  Binary Search  !  Assume sorted array  John Edgar 18 Java public int linSearch(int arr, int x){ for (int i=0; i < arr.size; i++){ if(x == arr[i]){ return i; } } //for return -1; //target not found } The algorithm searches the array one  element at a time using a for loop   John Edgar 19 !  !  Base cases  Recursive case  !  Target is found, or the end of the array is reached  !  Target not found   Java public int recLinSearch(int arr, int i, int x){ if (i >= arr.length){ return -1; } else if (x == arr[i]){ return i; } else return recLinSearch(arr, i + 1, x); } } John Edgar 20 !  Of course, if it’s a sorted array we wouldn’t do  linear search  John Edgar 21 !  Each sub‐problem searches a subarray  !  Differs only in the upper and lower array indices  !  There are two base cases  that define the subarray  !  Each sub‐problem is smaller than the last one  !  In the case of binary search, half the size  !  When the target item is found and  !  When the problem space consists of one item  ▪  Make sure that this last item is checked  John Edgar 22 Java public int binSearch( int arr, int lower, int upper, int x){ int mid = (lower + upper) / 2; if (lower > upper){ return - 1; //base case } else if(arr[mid] == x){ return mid; //second base case } else if(arr[mid] < x){ return binSearch(arr, mid + 1, upper, x); } else { //arr[mid] > target return binSearch(arr, lower, mid - 1, x); } } John Edgar 23 January 2010 Greg Mori 24 !  Recursive algorithms have more overhead  than similar iterative algorithms  stack gets full  !  Because of the repeated method calls  !  This may cause a stack overflow when the call  !  It is often useful to derive a solution using  recursion and implement it iteratively  !  Sometimes this can be quite challenging!  John Edgar 25 !  Some recursive algorithms are inherently  inefficient  !  e.g. the recursive Fibonacci algorithm which  !  Such algorithms should be implemented  repeats the same calculation again and again  !  Look at the number of times fib(2) is called  iteratively  !  Even if the solution was determined using recursion  John Edgar 26 !  It is useful to trace through the sequence of  recursive calls  !  Recursion trees can be used to determine the  !  This can be done using a recursion tree  running time of algorithms  !  Annotate the tree to indicate how much work is  performed at each level of the tree  !  And then determine how many levels of the tree  there are  John Edgar 27 January 2010 Greg Mori 28 !  Recursion is similar to induction  !  Recursion solves a problem by  !  Specifying a solution for the base case and  !  Using a recursive case to derive solutions of any  size from solutions to smaller problems  !  Induction proves a property by  !  Proving it is true for a base case and  !  Proving that it is true for some number, n, if it is  true for all numbers less than n  John Edgar 29 Java public int fact (int x){ if (x == 0){ return 1; } else return n * fact(n – 1); } } !  Prove, using induction that the algorithm returns  the values  !  fact(0) = 0! =1  !  fact(n) = n! = n * (n – 1) * … * 1 if n > 0  John Edgar 30 !  Basis: Show that the property is true for n = 0, i.e.  that fact(0) returns 1  !  This is true by definition as fact(0) is the base case of the  !  !  Establish that the property is true for an arbitrary k  implies that it is also true for k + 1  Inductive hypothesis: Assume that the property is  true for n = k, that is assume that  !  fact(k) = k * (k – 1) * (k – 2) * … * 2 * 1  algorithm and returns 1  John Edgar 31 !  Inductive conclusion: Show that the property is  true for n = k + 1, i.e., that fact(k + 1) returns  !  (k + 1) * k * (k – 1) * (k – 2) * … * 2 * 1  !  !  !  !  By definition of the function: fact(k + 1) returns  !  (k + 1) * fact(k) – the recursive case  And by the inductive hypothesis: fact(k) returns  Therefore  fact(k + 1) must return  Which completes the inductive proof  John Edgar 32 !  k * (k – 1) * (k – 2) * … * 2 * 1  !  (k + 1) * k * (k – 1) * (k – 2) * … * 2 * 1  !  Recursive sum  !  Towers of Hanoi – see text  !  Eight Queens problem – see text  !  Sorting  !  Mergesort  !  Quicksort  John Edgar 33 !  Linked Lists are recursive data structures  !  They are defined in terms of themselves  !  There are recursive solutions to many list  methods  !  List traversal can be performed recursively  !  Recursion allows elegant solutions of problems  that are hard to implement iteratively  ▪  Such as printing a list backwards  John Edgar 34 January 2010 Greg Mori 35 !  Let’s say I have 2 sorted lists of numbers  !  How can I merge them into 1 sorted list?  1 12 22 23 3 5 42 99 List 1 List 2 output 1 3 5 12 22 23 42 99 John Edgar 36 !  If I have a list of n numbers, how should I sort  them?  !  I know two things  sorted list  !  How to sort a list of 1 number  !  How to merge 2 sorted lists of numbers into 1  !  Smells like recursion  John Edgar 37 mergeSort (array) if (array is length 1) // base case, one element return the array else arr1 = mergeSort(first half of array) arr2 = mergeSort(second half of array) return merge(arr1,arr2) John Edgar 38 January 2010 Greg Mori 39 !  Recursion as a problem‐solving tool  !  Identify base case where solution is simple  !  Formulate other cases in terms of smaller case(s)  !  Recursion is not always a good  implementation strategy  !  Recursion and induction  !  Induction proves properties in a form similar to  !  Solve the same problem many times  !  Function call overhead  how recursion solves problems  John Edgar 40 !  Java Ch. 3, 6  !  C++ Ch. 2, 5  John Edgar 41 ...
View Full Document

## This note was uploaded on 04/17/2010 for the course CMPT 11151 taught by Professor Gregorymori during the Spring '10 term at Simon Fraser.

Ask a homework question - tutors are online