Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
The naive recursion fib(n) = fib(n − 1) + fib(n − 2), with base cases fib(0) = 0 and fib(1) = 1, is started by calling fib(9). Counting the very first call, how many calls to fib happen in total?
- A. 67
- B. 109
- C. 55
- D. 34
Correct answer: B. 109
Let C(n) be the number of calls: C(n) = 1 + C(n − 1) + C(n − 2) with C(0) = C(1) = 1, which gives C(n) = 2·F(n + 1) − 1. The number of calls is not the Fibonacci value itself.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- The naive recursion fib(n) = fib(n − 1) + fib(n − 2), with base cases fib(0) = 0 and fib(1) = 1, is started by calling fib(10). Counting…
- The naive recursion fib(n) = fib(n − 1) + fib(n − 2), with base cases fib(0) = 0 and fib(1) = 1, is started by calling fib(11). Counting…
- A recurrence T(n) = 2·T(n/4) + n^2 with constant base case. By the master theorem, which tight bound describes T(n)?
- A recurrence T(n) = 8·T(n/3) + n^2 with constant base case. By the master theorem, which tight bound describes T(n)?
- A recurrence T(n) = 5·T(n/3) + n^2 with constant base case. By the master theorem, which tight bound describes T(n)?
- A recurrence T(n) = 8·T(n/4) + n^2 with constant base case. By the master theorem, which tight bound describes T(n)?