Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture #3 Analysis of Recursive Algorithms

Similar presentations


Presentation on theme: "Lecture #3 Analysis of Recursive Algorithms"— Presentation transcript:

1 Lecture #3 Analysis of Recursive Algorithms

2 What is a recurrence relation?
A recurrence relation, T(n), is a recursive function of an integer variable n. Like all recursive functions, it has one or more recursive cases and one or more base cases. Example: The portion of the definition that does not contain T is called the base case of the recurrence relation; the portion that contains T is called the recurrent or recursive case. Recurrence relations are useful for expressing the running times (i.e., the number of basic operations executed) of recursive algorithms The specific values of the constants such as a, b, and c (in the above recurrence) are important in determining the exact solution to the recurrence. Often however we are only concerned with finding an asymptotic upper bound on the solution. We call such a bound an asymptotic solution to the recurrence.

3 Coding the factorial function
Recursive implementation int Factorial(int n) { if (n==0) // base case return 1; else return n * Factorial(n-1); }

4 Coding the factorial function (cont.)
Iterative implementation int Factorial(int n) { int fact = 1; for(int count = 2; count <= n; count++) fact = fact * count; return fact; }

5 Recursion vs. iteration
Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code

6 What happens when a recursive function is called?
Except the fact that the calling and called functions have the same name, there is really no difference between recursive and nonrecursive calls int f(int x) { int y; if(x==0) return 1; else { y = 2 * f(x-1); return y+1; }

7 2*f(2) 2*f(1) 2*f(1) =f(0) =f(1) =f(2) =f(3)

8 Solving Recurrence Relations - Iteration method
following summation formulae may be used:

9 Solving Recurrence Relations - Iteration method (Cont’d)

10 Recurrence Relation Form
A recurrence relation is a recursive form of an equation, for example: A recurrence relation can be put into an equivalent closed form without the recursion

11 Converting Recurrence Relations
Begin by looking at a series of equations with decreasing values of n:

12 Converting Recurrence Relations
Now, we substitute back into the first equation:

13 Analysis Of Recursive Selection Sort (Cont’d)
findMinPos is O(n), and swap is O(1), therefore the recurrence relation for the running time of the selectionSort method is: T(0) = a (1) T(n) = T(n – 1) + n + c if n > (2) = [T(n-2) +(n-1) + c] + n + c = T(n-2) + (n-1) + n + 2c by substituting T(n-1) in (2) = [T(n-3) + (n-2) + c] +(n-1) + n + 2c= T(n-3) + (n-2) + (n-1) + n + 3c by substituting T(n-2) in (2) = T(n-4) + (n-3) + (n-2) + (n-1) + n + 4c = …… = T(n-k) + (n-k + 1) + (n-k + 2) + …….+ n + kc The base case is reached when n – k = 0  k = n, we then have : Therefore, Recursive Selection Sort is O(n2)

14 Analysis Of Recursive Binary Search
public int binarySearch (int target, int[] array, int low, int high) { if (low > high) return -1; else { int middle = (low + high)/2; if (array[middle] == target) return middle; else if(array[middle] < target) return binarySearch(target, array, middle + 1, high); else return binarySearch(target, array, low, middle - 1); } The recurrence relation for the running time of the method is: T(1) = a if n = 1 (one element array) T(n) = T(n / 2) + b if n > 1

15 Simplified Master Theorem
The Simplified Master Method for Solving Recurrences: Consider recurrences of the form: T(1) = 1 T(n) = aT(n/b) + knc + h for constants a ≥ 1, b > 1, c  0, k ≥ 1, and h  0 then: if a > bc if a = bc if a < bc Note: Since k and h do not affect the result, they are sometimes not included in the above recurrence

16 Simplified Master Theorem (Cont’d)
Example1: Find the big-Oh running time of the following recurrence. Use the Master Theorem: Solution: a = 3, b = 4, c = ½  a > bc  Case 1 Hence Example2: Find the big-Oh running time of the following recurrence. Use the Master Theorem: T(1) = 1 T(n) = 2T(n / 2) + n Solution: a = 2, b = 2, c = 1  a = bc  Case 2 Hence T(n) is O(n log n) Example3: Find the big-Oh running time of the following recurrence. Use the Master Theorem: T(1) = 1 T(n) = 4T(n / 2) + kn3 + h where k ≥ 1 and h  1 Solution: a = 4, b = 2, c = 3  a < bc  Case 3 Hence T(n) is O(n3)


Download ppt "Lecture #3 Analysis of Recursive Algorithms"

Similar presentations


Ads by Google