Back

Solution: 2016 Fall Midterm - 13

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$

When running $\Fib(12)$, how many calls are there to $\Fib(8)$?
(a)
4
(b)
5
(c)
6
(d)
7

Solution

We can draw a recursive tree to see how many calls there are to $ \text{FIB}(8) $.

image

We can see that there are 5 calls to $ \text{FIB}(8) $.