GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 1(x)

Recursive Fibonacci call count

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

GATE 1991, Q1(x) 2 marks · Other
Report issue

Asymptotic Analysis & Recurrences

Question 1(x), GATE 1991: fib(0)=fib(1)=1; for n≥2, fib(n)=fib(n−1)+fib(n−2), using ordinary recursive calls. Count all calls evaluating fib(7), including the initial call.
Question 1(x), GATE 1991: fib(0)=fib(1)=1; for n≥2, fib(n)=fib(n−1)+fib(n−2), using ordinary recursive calls. Count all calls evaluating fib(7), including the initial call.
Tap to enlarge
Answer & solution

Answer: 41 (no official key was published)

Step through it (23 steps)

Let C(n) count function invocations. Each base case requires one call, so C(0)=C(1)=1. A nonbase invocation counts itself plus both recursive subtrees: C(n)=1+C(n−1)+C(n−2). Successive values for n=2, …, 7 are 3, 5, 9, 15, 25, 41. This count includes repeated evaluation of identical arguments, as the supplied recursion does not memoize them. It is different from the numeric return value fib(7)=21; counting only leaves or only additions would also answer a different question.

Original paper, page 1