GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 83

Cost of duplicated recursive calls

Algorithms · Asymptotic Analysis & Recurrences · Full GATE CSE 2004 paper

GATE 2004, Q83 2 marks · MCQ
Report issue

Asymptotic Analysis & Recurrences

Question 83, GATE 2004: For positive integer n, find the time complexity of:
int recursive(int n) {
  if(n==1) return 1;
  else return recursive(n-1)+recursive(n-1);
}
Use the ordinary execution model without memoization; the source prose says “a>0” although the code parameter is n.
Options:
(A) O(n)
(B) O(n log n)
(C) O(n²)
(D) O(2^n)
Question 83, GATE 2004: For positive integer n, find the time complexity of:
int recursive(int n) {
  if(n==1) return 1;
  else return recursive(n-1)+recursive(n-1);
}
Use the ordinary execution model without memoization; the source prose says “a>0” although the code parameter is n.
Options:
(A) O(n)
(B) O(n log n)
(C) O(n²)
(D) O(2^n)
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. For positive integer n, each nonbase invocation makes two independent calls at n−1. With the usual unit-cost model and no memoization, T(n)=2T(n−1)+Θ(1), T(1)=Θ(1).

2. The recursion tree has 2n−1 leaves and 2n−1 total calls, yielding Θ(2n), and hence D. The identical arguments do not make the two evaluations a single call in this abstract execution model; integer overflow and compiler transformations are outside the model.

3. Teaching check: At recursion depth k there are 2k calls, until depth n−1. Adding the geometric series gives 2n−1 calls. The depth is only n, but depth alone is not total work when the recursion branches.

Original paper, page 12