Question

In: Computer Science

. Fibonacci sequence Fn is defined as follows: F1 = 1; F2 = 1; Fn =...

. Fibonacci sequence Fn is defined as follows: F1 = 1; F2 = 1; Fn = Fn−1 + Fn−2, ∀n ≥ 3. A pseudocode is given below which returns n’th number in the Fibonacci sequence. RecursiveFibonacci(n) 1 if n = 0 2 return 0 3 elseif n = 1 or n = 2 4 return 1 5 else 6 a = RecursiveFibonacci(n − 1) 7 b = RecursiveFibonacci(n − 2) 8 return a + b Derive the complexity of the above algorithm by writing the recurrence and solving it. Use the memoization idea (of dynamic programming) to obtain a linear time algorithm for the above problem. Show Pseudo-code and complexity proof. (5 + 10)

Solutions

Expert Solution

Memoization is a technique to save the previously calculated values in an array so that we don't calculate them again.

Start with an array of size n with all elements 0.

First time a new value is calculated, add it to the array called memo. Initially check if the value exists in the array memo, if it does return the value if not calculate and add to array.

Pseudo code is here :

RecursiveFibonacci(n)
int memo[n] = {0}
if n = 0
   memo[0] = 0
   return 0
elseif n = 1 or n = 2
   memo[1] = memo[2]=1
   return 1
  
else
   if(memo[n]==0){
       a = RecursiveFibonacci(n − 1)
       b = RecursiveFibonacci(n − 2)
       memo[n] = a+b;
   }
return memo[n]


Related Solutions

0.3 The Fibonacci numbers Fn are defined by F1 = 1, F2 = 1 and for...
0.3 The Fibonacci numbers Fn are defined by F1 = 1, F2 = 1 and for n >2, Fn = F sub (n-1) + F sub (n-2). Find a formula for Fn by solving the difference equation.
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn =...
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for n ≥ 2. Use induciton to prove that F0F1 + F1F2 + · · · + F2n−1 F2n = F^2 2n for all positive integer n.
For the Fibonacci sequence, f0 = f1 = 1 and fn+1 = fn + fn−1 for...
For the Fibonacci sequence, f0 = f1 = 1 and fn+1 = fn + fn−1 for all n > 1. Prove using induction: fn+1fn−1 − f2n = (−1)n.
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 +...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 + Fn for all n ∈ N ∪ {0}. (1) Make and prove an (if and only if) conjecture about which Fibonacci numbers are multiples of 3. (2) Make a conjecture about which Fibonacci numbers are multiples of 2020. (You do not need to prove your conjecture.) How many base cases would a proof by induction of your conjecture require?
Let f1 = 1 and f2=1 and for all n>2 Let fn = fn-1+fn-2. Prove that...
Let f1 = 1 and f2=1 and for all n>2 Let fn = fn-1+fn-2. Prove that for all n, there is no prime p that divides noth fn and fn+1
The Fibonacci numbers are recursively dened by F1 = 1; F2 = 1 and for n...
The Fibonacci numbers are recursively dened by F1 = 1; F2 = 1 and for n > 1; F_(n+1) = F_n + F_(n-1): So the rst few Fibonacci Numbers are: 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; : : : There are numerous properties of the Fibonacci numbers. a) Use the principle of Strong Induction to show that all integers n > 1 and m > 0 F_(n-1)F_(m )+ F_(n)F_(m+1) = F_(n+m): Solution. (Hint: Use...
Show that if (1) F1 and F2 are connected sets, and (2) F1 ∩ F2 is...
Show that if (1) F1 and F2 are connected sets, and (2) F1 ∩ F2 is not empty, then  F1 ∪ F2 is connected. also Suppose that F is connected. Show that F¯ (the closure of F) is also connected.
The Fibonacci sequence is defined as F_1 = 1, F_2 = 1, and F_n = F_n-1...
The Fibonacci sequence is defined as F_1 = 1, F_2 = 1, and F_n = F_n-1 + F_n-2 for n >= 3. Calculate the sum F_1 + F_2 + ... + F_n using the fundamental theorem of summation.  
Let’s revisit the Fibonacci sequence, this time without an array. Recall that the sequence is defined...
Let’s revisit the Fibonacci sequence, this time without an array. Recall that the sequence is defined by the following recurrence relation: F0 = 0 F1 = 1 Fk = Fk-1 + Fk-2 So, the sequence looks like: 0, 1, 1, 2, 3, 5, 8, 13, 21, … Write a program fib.c consisting only of a main() function that prompts the user for the last Fibonacci number n to print, and then prints them from F0 to Fn. (No file input...
Compute each of the following: a. F1+F2+F3+F4+F5 b. F1+2+3+4 c. F3xF4 d. F3X4 Given that FN...
Compute each of the following: a. F1+F2+F3+F4+F5 b. F1+2+3+4 c. F3xF4 d. F3X4 Given that FN represents the Nth Fibonacci number, and that F31 =1,346, 269 and F33 = 3,524,578, find the following: a. F32 b. F34 25. Solve the quadratic equation using the quadratic formula: 3x^2-2x-11=0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT