0% found this document useful (0 votes)
13 views

Algorithms Analysis and Design Lec 3

Uploaded by

c05b8d8c5b
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Algorithms Analysis and Design Lec 3

Uploaded by

c05b8d8c5b
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Analysis of Recursive Algorithms

Do Asymptotic Analysis of the following Algorithms?


long factorial (int n) {
if (n == 0)
return 1;
else
return n * factorial (n – 1);
}
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.

Forming Recurrence Relations


➢ For a given recursive method, the base case and the recursive case of its recurrence
relation correspond directly to the base case and the recursive case of the method.
Example 1: Write the recurrence relation for the following method:
public void f (int n)
{
if (n > 0)
{
System.out.println(n);
f(n-1);
}
}
The base case is reached when n = = 0. The method performs one comparison. Thus, the
number of operations when n = = 0, T(0), is constant a.
When n > 0, the method performs two basic operations (comparison and print) and then calls
itself, using ONE recursive call, with a parameter n – 1.
➢ Therefore, the recurrence relation is:
T(0) = a for some constant a
T(n) = b + T(n – 1) for some constant b
➢ In General, T(n) is usually a sum of various choices of T(n ), the cost of the recursive
subproblems, plus the cost of the work done outside the recursive calls: T(n ) = aT(f(n))
+ bT(g(n)) + . . . + c(n)
where a and b are the number of subproblems, f(n) and g(n) are subproblem sizes, and
c(n) is the cost of the work done outside the recursive calls [Note: c(n) may be a constant]
Example 2: Write the recurrence relation for the following method:
public int g(int n) {
if (n == 1)
return 2;
else
return 3 * g(n / 2) + g( n / 2) + 5;
}
➢ The base case is reached when n == 1. The method performs one comparison and one
return statement. Therefore, T(1), is some constant c.
➢ When n > 1, the method performs TWO recursive calls, each with the parameter n / 2,
and some constant # of basic operations.
Hence, the recurrence relation is:
T(1) = c for some constant c
T(n) = b + 2T(n / 2) for some constant b
Example 3: Write the recurrence relation for the following method:
long fibonacci (int n) { // Recursively calculates Fibonacci number
if( n == 1 || n == 2)
return 1;
else
return fibonacci(n – 1) + fibonacci(n – 2);
}
➢ The base case is reached when n == 1 or n == 2. The method performs one comparison
and one return statement. Therefore each of T(1) and T(2) is some constant c.
➢ When n > 2, the method performs TWO recursive calls, one with the parameter n - 1 ,
another with parameter n – 2, and some constant # of basic operations.
Hence, the recurrence relation is:
T(n) = c if n = 1 or n = 2
T(n) = T(n – 1) + T(n – 2) + b if n > 2
Example 4: Write the recurrence relation for the following method:
long power (long x, long n) {
if(n == 0)
return 1;
else if(n == 1)
return x;
else if ((n % 2) == 0)
return power (x, n/2) * power (x, n/2);
else
return x * power (x, n/2) * power (x, n/2);
}
The base case is reached when n == 0 or n == 1. The method performs one comparison and
one return statement. ThereforeT(0) and T(1) is some constant c.
At every step the problem size reduces to half the size. When the power is an odd number,
additional multiplication is involved. To work out time complexity, let us consider the worst
case, that is we assume that at every step an additional multiplication is needed. Thus total
number of operations T(n) will reduce to number of operations for n/2, that is T(n/2) with
seven additional basic operations (the odd power case)
Hence, the recurrence relation is:
T(n) = c if n = 0 or n = 1
T(n) = 2T(n /2) + b if n > 2
Solving Recurrence Relations
To solve a recurrence relation T(n) we need to derive a form of T(n) that is not a recurrence
relation. Such a form is called a closed form of the recurrence relation.
There are five methods to solve recurrence relations that represent the running time of
recursive methods:
➢ Iteration method (unrolling and summing)
➢ Substitution method (Guess the solution and verify by induction)
➢ Recursion tree method
➢ Master theorem (Master method)
➢ Using Generating functions or Characteristic equations
In this course, we will use the Iteration method and a simplified Master theorem.
Solving Recurrence Relations - Iteration method
Steps:
➢ Expand the recurrence
➢ Express the expansion as a summation by plugging the recurrence back into itself until
you see a pattern.
➢ Evaluate the summation
In evaluating the summation one or more of the following summation formulae may be used:
Arithmetic series (Arithmetic series and Special Cases of Geometric Series):
Arithmetic series: Special Cases of Geometric Series:
Analysis Of Recursive Factorial method
Example1: Form and solve the recurrence relation for the running time of factorial method
and hence determine its big-O complexity:
long factorial (int n) {
if (n == 0)
return 1;
else
return n * factorial (n – 1);
}
T(0) = c (1)
T(n) = b + T(n - 1) (2)
= b + b + T(n - 2) by substituting T(n – 1) in (2)
= b +b +b + T(n - 3) by substituting T(n – 2) in (2)

= kb + T(n - k)
The base case is reached when n – k = 0 → k = n, we then have:
T(n) = nb + T(n - n)
= bn + T(0)
= bn + c
Therefore the method factorial is O(n)
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
Expanding:
T(1) = a (1)
T(n) = T(n / 2) + b (2)
= [T(n / 22) + b] + b = T (n / 22) + 2b by substituting T(n/2) in (2)
= [T(n / 23) + b] + 2b = T(n / 23) + 3b by substituting T(n/22) in (2)
= ……..
= T( n / 2k) + kb
The base case is reached when (n/ 2k )= 1 ➔ n = 2k ➔ k = log2 n, then have:
T(n) = T(1) + b log2 n
= a + b log2 n
Therefore, Recursive Binary Search is O(log n)

You might also like