Last Updated :
08 May, 2019
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?
(A) Linear
(B) Exponential
(C) Quadratic
(D) Cubic
Answer: (B)
Explanation: f(n) = f(n-1) + g(n) ---- 1
g(n) = f(n-1) + g(n/2) ---- 2
Putting the value of g(n) in equation 1,
f(n) = 2*f(n-1) + g(n) + g(n/2)
So, we can derive the below equation,
f(n) > 2f(n-1)
=> f(n) > 2*2*f(n-2) ---- because f(n) > 2*f(n-1), so, f(n-1) > 2*2*f(n-2).... so on
=> f(n) > (2^n)f(1) --- here \'^\' denotes the exponent.
So, f(n) > Theta(2^n)
So, option B is true, exponential growth for f(x).
Quiz of this Question
Share your thoughts in the comments
Please Login to comment...