Consider the following 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)
Cubic
(B)
Quadratic
(C)
Exponential
(D)
Linear
Answer: (C)
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/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*f(n-2) So we can write f(n) > 2*2*f(n-2) so on.... => f(n) > (2^n)f(1) So, f(n) > Theta(2^n)
So option C is correct.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Article Tags :
Uncategorized
Recommended Articles