• edited by
7,656 views
9 9 votes

Consider the following C code segment

int f(int x)
{
    if(x<1) return 1;
    else return (f(x-1)+g(x));
}
int g(int x)
{
    if(x<2) return 2;
    else return (f(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

3 Answers

Best answer
21 21 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 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
0 0 votes

What future holds we don't know but we can find out by keep moving forward 

Answer:
Position:
Show:

Related questions

5 5 votes
2 answers 2 answers
7.2k
7.2k views
Arjun asked Apr 22, 2018
7,239 views
Assume $A$ and $B$ are non-zero positive integers. The following code segment:while(A!=B){ if*(A B) A -= B; else B -= A; } cout<<A; // printing the value of AComputes the...
5 5 votes
2 answers 2 answers
9.0k
9.0k views
Arjun asked Apr 22, 2018
8,965 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...
10 10 votes
5 5 answers
11.8k
11.8k views
Arjun asked Apr 22, 2018
11,845 views
The running time of an algorithm is given by: $T(n) = T(n-1) + T(n-2) - T(n-3)$, if $n 3$ = $n$, otherwiseThen what...
5 5 votes
4 answers 4 answers
4.0k
4.0k views
Arjun asked Apr 22, 2018
3,995 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})$