• edited by
10,429 views
41 41 votes

A recursive program to compute Fibonacci numbers is shown below. Assume you are also given an array $f [ 0\ldots m]$ with all elements initialized to $0.$

fib(n) {
    if (n > M) error ();
    if (n == 0) return 1;
    if (n == 1)return 1;
    if (▭)________________________(1)
        return ▭__________________(2)
    t = fib(n - 1) + fib(n - 2);
    ▭_____________________________(3)
    return t;
}
  1. Fill in the boxes with expressions/statement to make $fib()$ store and reuse computed Fibonacci values. Write the box number and the corresponding contents in your answer book.
  2. What is the time complexity of the resulting program when computing $fib(n)?$

5 Answers

Best answer
54 54 votes

Array $f$ is used to store the $fib()$ values calculated in order to save repeated calls. Since $n = 0$ and $n = 1$ are special cases we can store $fib(2)$ to $f[0], fib(3)$ to $f[1]$ and so on. The missing code completed would be:


if (f[n - 2] != 0){
    return f[n-2];
}
t = fib(n-1) + fib(n-2);
f[n-2] = t;
return t;

In this code, $fib(i)$ will do a recursion only once as once $fib(i)$ is calculated it is stored in array. So, the time complexity for $fib(n)$ would be $\Theta(n)$. 

PS: We can also store $fib(n)$ in $f(n)$, the above code just saves $2$ elements' space in the array. 

• edited by
6 6 votes

int fib(int n)

{

  if (n>m)

       printf('error');

  if(n==0)

       return 1;

  if(n==1)

      return 1;

  if (f[n]!=0)

      return (f[n-1]+f[n-2]);\\\ this is for reuse

  t=fib(n-1)+fib(n-2);

  f[n]=t ; \\ this for store the value

  return t;

}

In Array f[0...m] store all the value of  Fibonacci numbers 0 to m

time complexity is O(n)

• edited by
4 4 votes
A.
(1) f[n]
(2) f[n];
(3) f[n] = t;

B. O(n)
Explanation: if(f[n]) { return f[n];} is executed when f[n] ≠ 0 
(because every non-zero value in C/C++ conditional statement is true). 
So it is meant to return the value of f[n] containing the desired value which was done 
in some previous call of fib(n) at the statement f[n] = t;

See my full code at http://ideone.com/SZRX0j. 
3 3 votes

1. Its a simple algorithm design for Fibonacci using Dynamic Programming

Missing Code:

 if (f[n] != 0){ 

 return f[n];

 } 

t = fib(n-1) + fib(n-2); 

f[n] = t; 

return t;

2. Unique calls in Fibonacci will be n, so time complexity will be O(n).

 

 

 

 

 

 

 

0 0 votes
Since the base cases n=0 and n=1 are already handled by the early if statements, the function only reaches the lower array logic when n >= 2. By using f[n-2], we map the values efficiently without wasting the first two array slots. As the array f[] is explicitly given in the question, our goal is to use it to store and reuse calculated values, which is the exact definition of Memoization (a Top-Down Dynamic Programming approach). This simple array check completely reduces the execution time from a slow exponential (O(2^n)) down to an optimal linear (Theta(n))
Position:
Show:

Related questions

51 51 votes
8 answers 8 answers
17.9k
17.9k views
Kathleen asked Sep 14, 2014
17,927 views
A multiset is an unordered collection of elements where elements may repeat any number of times. The size of a multiset is the number of elements in it, counting repetiti...
52 52 votes
7 answers 7 answers
21.7k
21.7k views
Kathleen asked Sep 14, 2014
21,676 views
An array contains four occurrences of $0$, five occurrences of $1$, and three occurrences of $2$ in any order. The array is to be sorted using swap operations (elements t...
8 8 votes
3 answers 3 answers
4.3k
4.3k views
Kathleen asked Sep 14, 2014
4,266 views
Consider the following program is pseudo-Pascal syntaxprogram main; var x: integer; procedure Q (z: integer); begin z := z+x; writeln(z); end; procedure P (y: integer);...
14 14 votes
2 2 answers
4.4k
4.4k views
Kathleen asked Sep 14, 2014
4,440 views
Consider a bank database with only one relation transaction (transno, acctno, date, amount)The amount attribute value is positive for deposits and negative for withdrawa...