Question

In: Computer Science

1Set up and solve a recurrence relation for the number of times the algorithm’s basic operation...

1Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed.

2 How does this algorithm compare with the straightforward nonrecursive algorithm for computing this function?

Solutions

Expert Solution

1. A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs. Recurrences are generally used in divide-and-conquer paradigm.

Let us consider T(n) to be the running time on a problem of size n.

If the problem size is small enough, say n < c where c is a constant, the straightforward solution takes constant time, which is written as θ(1). If the division of the problem yields a number of sub-problems with size nbnb.

To solve the problem, the required time is a.T(n/b). If we consider the time required for division is D(n) and the time required for combining the results of sub-problems is C(n), the recurrence relation can be represented as −

The simplest form of a recurrence relation is the case where the next term depends only on the immediately previous term. If we denote the nnth term in the sequence by , such a recurrence relation is of the form

for some function . One such example is

A recurrence relation can also be higher order, where the term could depend not only on the previous term but also on earlier terms such as , , etc. A second order recurrence relation depends just on and and is of the form

for some function with two inputs. For example, the recurrence relation can generate the Fibonacci numbers.

To generate sequence basd on a recurrence relation, one must start with some initial values. For a first order recursion , one just needs to start with an initial value and can generate all remaining terms using the recurrence relation. For a second order recursion , one needs to begin with two values and . Higher order recurrence relations require correspondingly more initial values.

2.This is a command for fabionacci series using recursion

int fibo(int in){
 if(n <= 1){
  return n
 }else{
  return fibo(n-1) + fibo(n-2);
 }
}

This is a command for fabionacci series without using recursion

int fibo(int n){
 if(n <= 1){
  return n;
 }
 int fibo = 1;
 int fiboPrev = 1;
 for(int i = 2; i < n; ++i){
  int temp = fibo;
  fibo += fiboPrev;
  fiboPrev = temp;
 }
 return fibo;
}

You can clearly see in these commands the priors one is efficient in both time and space complexity.


Related Solutions

Find and solve a recurrence relation for the number of ways to stack n poker
Find and solve a recurrence relation for the number of ways to stack n poker chips using red, white and blue chips such that no two red chips are together. Use your solution to compute the number of ways to stack 15 poker chips.
a) Find the recurrence relation for the number of ways to arrange flags on an n...
a) Find the recurrence relation for the number of ways to arrange flags on an n foot flagpole with 1 foot high red flags, 2 feet high white flags and 1 foot high blue flags. b) solve the recurrence relation of part a
find a recurrence relation for the number of bit strings of length n that contain the...
find a recurrence relation for the number of bit strings of length n that contain the string 10. What are the initial conditions? How many bit strings of length eight contain the string 10
Solve the recurrence relation with the given initial conditions. b0 = 0, b1 = 4, bn...
Solve the recurrence relation with the given initial conditions. b0 = 0, b1 = 4, bn = 2bn ? 1 + 2bn ? 2 for n ? 2
Use generating functions to solve the following recurrence relation: an = 2an−1 + 3n , n...
Use generating functions to solve the following recurrence relation: an = 2an−1 + 3n , n ≥ 1 a0 = 2
Find a recurrence relation for the number of binary strings of length n which do not...
Find a recurrence relation for the number of binary strings of length n which do not contain the substring 010
find a recurrence relation for the number of bit strings of length n that contain two...
find a recurrence relation for the number of bit strings of length n that contain two consecutive 1s. What are the initial conditions? How many bit strings of length eight contain two consecutive 1s
Please solve the recurrence relation by finding the explicit formula of each problem. Show all work,...
Please solve the recurrence relation by finding the explicit formula of each problem. Show all work, thanks! A. ck = 6ck-1 - 9ck-2      k≥2,   c0 =1, c1=3 B. dk = 2dk-1 +k                       k≥1,   d0 =1, d1=3 C. ak = 3ak-1 + 2                         k≥1,   a0 =3 D. bk = -bk-1 + 7bk-2    k≥2,   b0 =1, b1=4
Solve the following recurrence relation (A) an = 3an-1 + 4an-2 a0 =1, a1 = 1
  Solve the following recurrence relation   (A) an = 3an-1 + 4an-2 a0 =1, a1 = 1   (B) an = 2an-1 - an-2, a0 = 1, a2= 2
- Solve the following recurrence relation : T(n) = T(αn) + T((1 − α)n) + n
- Solve the following recurrence relation : T(n) = T(αn) + T((1 − α)n) + n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT