edited by
18,966 views
40 votes
40 votes

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));
}
int f2(int n)
{
    int i;
    int X[N], Y[N], Z[N];
    X[0] = Y[0] = Z[0] = 0;
    X[1] = 1; Y[1] = 2; Z[1] = 3;
    for(i = 2; i <= n; i++){
        X[i] = Y[i-1] + Z[i-2];
        Y[i] = 2 * X[i];
        Z[i] = 3 * X[i];
    }
    return X[n];
}

The running time of $f1(n)$ and $f2(n)$ are

  1. $\Theta(n)$ and $\Theta(n)$
  2. $\Theta(2^n)$ and $\Theta(n)$
  3. $\Theta(n)$ and $\Theta(2^n)$
  4. $\Theta(2^n)$ and $\Theta(2^n)$
edited by

6 Answers

0 votes
0 votes

The complexity is related to input-size, where each call produce a binary-tree of calls

Where T(n) make 2^n calls in total ..

T(n) = T(n-1) + T(n-2) + C

T(n) = O(2^n-1) + O(2^n-2) + O(1)

O(2 ^n)

In the same fashion, you can generalize your recursive function, as a Fibonacci number

T(n) = F(n) + ( C * 2n)

Next you can use a direct formula instead of recursive way

Using a complex method known as Binet's Formula

FORM STACK OVER FLOW.
0 votes
0 votes

(B.) In f1 function calling itself 2 times in recusive nature so O(2^n) and f2 is in normal form so it is O(n)

Answer:

Related questions

41 votes
41 votes
7 answers
2
Kathleen asked Sep 12, 2014
21,820 views
We have a binary heap on $n$ elements and wish to insert $n$ more elements (not necessarily one after another) into this heap. The total time required for this is$\Theta(...
79 votes
79 votes
7 answers
3
Kathleen asked Sep 12, 2014
36,832 views
The minimum number of comparisons required to determine if an integer appears more than $\frac{n}{2}$ times in a sorted array of $n$ integers is$\Theta(n)$$\Theta(\log n)...
28 votes
28 votes
6 answers
4
Kathleen asked Sep 11, 2014
13,528 views
The most efficient algorithm for finding the number of connected components in an undirected graph on $n$ vertices and $m$ edges has time complexity$\Theta(n)$$\Theta(m)$...