VIEWS: 19 PAGES: 19 CATEGORY: Engineering POSTED ON: 6/3/2012 Public Domain
Recursion • A recursive definition is one which uses the word or concept being defined in the definition itself • All recursive definitions have to have a non-recursive part, called the base case; otherwise, the recursion will never get terminated and will fall in infinite loop. • We focus only finite recursion. Recursive Definitions Contain two parts: Base case: We must always have some base case, which can be solved without recursion. Making progress: For the cases that are to be solved recursively, the recursive call must always make progress toward a base case. Recursive Algorithm An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Problem: Computing an; a 0, n>0 Solution: an = a * an-1 for n>0 ; a0 = 1 Linear Recursion • Base cases: – Begin by testing for a set of base cases. • Recur once: – Linear recursion occurs whenever there is one recursive call for each non-base case. Example of Linear Recursion Factorial(n) fact(n) = if (n==1) return 1; else return n * fact(n-1); 120 fact(5) 5* fact(4) 24 4* fact(3) 6 3* fact(2) 2 2* fact(1) 1 1 Recursively defined operations A recursive definition of multiplication of two positive integers m and n is 1. mult(1) = m 2. mult(n) = mult(n-1) + m for n ≥ 2 Recursively defined sequences A sequence T represents a list of objects in some order. Example: 1. T(1) = 1 2. T(n) = T(n-1) + 3 for n ≥ 2 A Recursive Squaring Method Algorithm Power(x, n): Input: A number x and integer n = 0 Output: The value xn 1 if n 0 if n = 0 then power( x, n) x power( x, (n 1) / 2) if n 0 is odd return 1 power( x, n / 2) if n 0 is even if n is odd then y = Power(x, (n - 1)/ 2) return x · y ·y else y = Power(x, n/ 2) return y · y Analyzing the Recursive Squaring Method Algorithm Power(x, n): Input: A number x and integer n = 0 Output: The value xn if n = 0 then Each time we make a return 1 recursive call we halve the value of n; hence, we make if n is odd then log n recursive calls. That y = Power(x, (n - 1)/ 2) is, this method runs in return x · y · y O(log n) time. else y = Power(x, n/ 2) return y · y Reversing an Array Algorithm ReverseArray(A, i, j): Input: An array A and nonnegative integer indices i and j Output: The reversal of the elements in A starting at index i and ending at j if i < j then Swap A[i] and A[ j] ReverseArray(A, i + 1, j - 1) return Tail Recursion • Tail recursion occurs when a linearly recursive method makes its recursive call as its last step. • Such methods can be easily converted to non-recursive methods (which saves some resource). • Example: Algorithm IterativeReverseArray(A, i, j ) Input: An array A and nonnegative integer indices i and j Output: The reversal of the elements in A starting at index i and ending at j while i < j do Swap A[i ] and A[ j ] i =i+1 j =j-1 return Recurrence The expression: c n 1 T ( n) n 2T cn n 1 2 is a recurrence. – Recurrence: an equation that describes a function in terms of its value on smaller functions Solving Recurrences • Substitution method • Iteration method • Master method Solving Recurrences The substitution method – “making a good guess method” – Guess the form of the answer, then use induction to find the constants and show that solution works – Examples: T(n) = T(n/2) + 1 Binary Recursion Binary recursion occurs whenever there are two recursive calls for each non-base case. Computing Fibanacci Numbers • Fibonacci numbers are defined recursively: F0 = 0 F1 = 1 Fi = Fi-1 + Fi-2 for i > 1. • Recursive algorithm: Algorithm BinaryFib(k): Input: Nonnegative integer k Output: The kth Fibonacci number Fk if k = 1 then return k else return BinaryFib(k - 1) + BinaryFib(k - 2) Fibonacci Calls fib(6) fib(5) fib(4) fib(4) fib(3) fib(3) fib(2) fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) Too many duplicate calls! A Better Fibonacci Algorithm • Use of linear recursion: Algorithm LinearFibonacci(k): Input: A nonnegative integer k Output: Pair of Fibonacci numbers (Fk, Fk-1) if k = 1 then return (k, 0) else (i, j) = LinearFibonacci(k - 1) return (i +j, i) • Runs in O(k) time. An iterative function for Fibonacci int fib(int n) { int fibn = 0, fibn1 = 0, fibn2 = 1; if (n < 2) return n; else { for( int i = 2; i ≤ n; i++ ) { fibn = fibn1 + fibn2; fibn1 = fibn2; fibn2 = fibn; } return fibn; } }