110 likes | 280 Views
Recursion. Suppose you have a problem involving N data points. Recursive solution of such problem is a follows: If the problem can be solved directly for N points then solve it Else Solve the problem for N-1 and combine this smaller solution with the case for the current point.
E N D
Recursion Suppose you have a problem involving N data points. Recursive solution of such problem is a follows: If the problem can be solved directly for N points then solve it Else Solve the problem for N-1 and combine this smaller solution with the case for the current point.
Recursion: Example #1 Factorial call stack Factorial(5) Factorial(4) Factorial(3) Factorial(2) Factorial(1) int Factorial(int N) { if ( N == 1 ) return 1; else return Factorial(N – 1)*N; }
Recursive Algorithm: Requirements There has to be a special (i.e. base) case for small N that can be solved directly; The problem for large N in principle must be separable into problems involving smaller Ns.
Recursive Algorithm: Design Recognize base case for small N that can be solved directly and provide a solution for it; Devise a strategy for splitting the problem into smaller parts; Devise a strategy for combining solutions obtained by solving smaller problems.
Recursive Algorithm: Verification Correctness of a recursive algorithms can be proved by induction: Prove that the solution for the base case (N=1) is correct; Suppose that the solution for N-th case is correct; Prove that the solution for N+1 case is also correct.
Verification Example Sum S(N) of arithmetic progression 1+2+3+…N is N*(N+1)/2 base case: S(N=1)=1*(1+1)/2=1 - OK Suppose S(N)=N*(N+1)/2 S(N+1)=(N+1)*(N+2)/2 = (N+1)*N/2 + N+1 = S(N) + N+1- OK
Recursion vs. Iteration Recursion is equivalent to iteration and in principle any recursive algorithm can be replaced with iterative one. (-) When compared to looping recursion creates overhead associated with function invocation. (-) Improper (unterminated) recursion may result in stack overflow. The why use recursion? (+) Some algorithms are easier to conceptualize with the aid of recursion. (+) For such algorithms recursive code is simpler and cleaner than iterative / looping code.
Recursion: Example #2 Binary Search: suppose you have an ordered vector; how can you find a particular value in it? Scan the vector from the first to the last element until you find the value - O(N), slow Check vector element in the middle and if it is greater than the value we are looking for then recursively scan the upper half of the vector else recursively scan the lower half – O(logN), fast
Binary Search template<class T> int BinarySearch(const vector<T>& aVector, const T& value, int startPos, int length) { // Empty vector? if ( length == 0 ) return -1; // Base case if ( length == 1 ) if ( aVector[startPos] == value ) return startPos; // value found, return index else return -1; // value not found // Scan upper half? if ( aVector[startPos + length/2] > value ) return BinarySearch(aVector, value, startPos, length/2); // Else scan lower half else return BinarySearch(aVector, value, startPos + length/2, length - length/2); }
Exercise: Sorted List Write a program that reads 5 strings from cin and stores them in vector in sorted order: The program must use BinarySearch function to find appropriate insertion index Modify BinarySearch function accordingly
Assignment Read chapter 7, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.