Time complexity of the program

Consider the following C functions:

int f1(int n) {
    if(n == 0 || n == 1)
        return n;
    else 
        return (2 * f1(n-1) + 3 * f1(n-2));
}

      

I need to find the running time f1 (n)

My decision: -

The repetition ratio for runtime f1 (n) can be written as

T(n) = T(n-1) + T(n-2) + c
Where c is a constant
Also T(0) =  T(1) = O(1) {Order of 1 (Constant Time)} 

      

Then I used the recursion tree method to solve this recursion relationship

            ---
             |                   n  -------------------- cost = c     
             |                /     \
             |              n-1      n-2 ---------------- cost = 2c
             |             /  \      /   \
             |           n-2  n-3   n-3  n-4 ------------ cost = 4c
(n-1) levels |           /                 \
             |         ......................
             |        ........................
             |       .........................\
             |      ..........................n-2k
             |      /
            ---    n-k                     

      

The left auxiliary tree continues to

n-k = 1 => k = n-1

      

Thus, the asymptotic upper bound turns out to be equal to

c+2c+2^2c+2^3c+....+2^(n-1)c
    = Big-Oh(2^n)

      

Likewise, the right subtree goes to

n-2k = 1 => k = (n-1)/2

      

Thus, the asymptotic lower bound turns out to be equal to

c+2c+2^2c+2^3c+....+2^((n-1)/2)c
   = Big-Omega(2^(n/2))

      

Since the upper and lower bounds differ by function (and not a constant value)

Upper bound = 2^n =  2^(n/2) * 2^(n/2)
Lower bound = 2^(n/2)

      

so in my opinion I cannot write T (n) = Theta (2 ^ n)

But the answer to this question is given as time complexity = Theta (2 ^ n)

What am I doing wrong?

+3


source to share


2 answers


Repetition is equivalent to Fibonacci number , there is a lot of information about this repetition on wikipedia. It is true that Fibonacci are in O (2 ^ n) and in Omega (2 ^ (n / 2)). There are related questions that mention these estimates, as well as the bounded bound ~ ฮธ (1.6n).



+2


source


Only the last level is calculated. Another layer just calls the next layer.



0


source







All Articles