Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 int Factorial(int N) { if ( N == 1 ) return 1; else return Factorial(N – 1)*N; } Recursion: Example #1 Factorial(5) Factorial call stack Factorial(4) Factorial(3) Factorial(2) Factorial(1)

3 1)There has to be a special (i.e. base) case for small N that can be solved directly; 2)The problem for large N in principle must be separable into problems involving smaller Ns. Recursive Algorithm: Requirements

4 1)Recognize base case for small N that can be solved directly and provide a solution for it; 2)Devise a strategy for splitting the problem into smaller parts; 3)Devise a strategy for combining solutions obtained by solving smaller problems. Recursive Algorithm: Design

5 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. Recursive Algorithm: Verification

6 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 Verification Example

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

8 Binary Search: suppose you have an ordered vector; how can you find a particular value in it? 1)Scan the vector from the first to the last element until you find the value - O(N), slow  2)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 Recursion: Example #2

9 template int BinarySearch(const vector & 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); } Binary Search

10 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 Exercise: Sorted List

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


Download ppt "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."

Similar presentations


Ads by Google