Back

Solution: 2015 Fall Final - 9

Author: Michiel Smid

Question

Consider the following recursive algorithm $\Fib$, which takes as input an integer $n \geq 0$:

$\mathbf{Algorithm}\ \Fib(n)\mathrm{:}$
$\mathbf{if}\ n = 0\ \mathrm{or}\ n = 1$
$\mathbf{then}\ f = n$
$\mathbf{else}\ f = \Fib(n - 1) + \Fib(n - 2)$
$\mathbf{endif};$
$\mathbf{return}\ f$

If we run algorithm $\Fib(18)$, how many calls are there to $\Fib(14)$?
(a)
7
(b)
5
(c)
6
(d)
4

Solution

We can draw a recursive tree to visualize the number of calls to $FIB(14)$ when running $FIB(18)$.

image

There are 5 calls to $FIB(14)$ when running $FIB(18)$.