edited by
4,645 views
5 votes
5 votes

Consider the following C code segment

int f(int x)
{
    if(x<1) return 1;
    else return (if(x-1)+g(x));
}
int g(int x)
{
    if(x<2) return 2;
    else return (if(x-1)+g(x/2));
}

Of the following, which best describes the growth of $f(x)$ as a function of $x$ ?

  1. Linear
  2. Exponential
  3. Quadratic
  4. Cubic
edited by

2 Answers

Best answer
15 votes
15 votes
$f(x) = f(x-1) + g(x)$

$\quad =f(x-1) + f(x-1) + g(x/2)$

$\quad = 2.f(x-1) + f(x/2 -1) + g(x/4)$

$\quad \vdots$

For simplicity I remove the second term in the expansion and then tries to get a lower bound for $f(x)$.

$f(x) > 2.f(x-1)$

$\implies f(x) >2.2.f(x-2)$

$\vdots$

$\implies f(x) > 2^{x}f(1)$

$\implies f(x) > 2^x$

So, option B is true, exponential growth for $f(x).$
selected by
2 votes
2 votes
if T(x) is time to execute f(x) then

T(x)=2T(x-1) + T($\frac{x}{2}$-1) + T($\frac{x}{2^2}$-1) + T($\frac{x}{2^3}$-1) +............+ T($\frac{x}{2^ (\log_{2}x)}$-1) + O(1)

 so approximately T(x)=2T(x-1) + O(1)

so it's exponential.i.e option B
Answer:

Related questions

1 votes
1 votes
2 answers
1
1 votes
1 votes
1 answer
2
Arjun asked Apr 22, 2018
5,202 views
An array $A$ consists of $n$ integers in locations $A[0], A , \ldots A[n-1]$. It is required to shift the elements of the array cyclically to the left by $k$ places, wher...
3 votes
3 votes
4 answers
4
Arjun asked Apr 22, 2018
2,152 views
The time complexity of computing the transitive closure of binary relation on a set of $n$ elements is known to be$O(n)$$O(n*\log(n))$$O(n^{\frac{3}{2}})$$O(n^{3})$