1 / 11

Recursion

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.

temple
Télécharger la présentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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; }

  3. 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.

  4. 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.

  5. 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.

  6. 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

  7. 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.

  8. 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 

  9. 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); }

  10. 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

  11. Assignment Read chapter 7, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.

More Related