Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

function y = myfib(n) if (n==1) y= 0; elseif (n==2) y=1; else y = myfib(n-2)+myfib(n-1); end Dynamic Programming is an algorithm design technique for optimization

image text in transcribed

function y = myfib(n)

if (n==1)

y= 0;

elseif (n==2)

y=1;

else

y = myfib(n-2)+myfib(n-1);

end

Dynamic Programming is an algorithm design technique for optimization problems. Similar to the divide-and-conquer method, dynamic programming solves problems by combining the solutions to sub-problems. Question: You have given a simple recursive Fibonacci code (see the attachment in the Elearn, myfib.m) Write your own dynamic programming version of recursive Fibonacci code using the pseudo code given below: myfib2(n) 1. if n = 0 return f [1] =0 2. if n = 1 return f [2] =1 3. If f[n-2] == -1 4. f[n-2] =myfib2(n-2) 5. If f(n-1) == -1 6. f[n-1] =myfib2(n-1) //Use saved results & store the nth term in table. 7. f[n] = f[n-1] + f[n-2] 8. return f[n] Keep in mind that f is the array you store the sequence. This f shall be defined global and set all elements to -1(f=-1*ones (1, n)) Compare simple recursive Fibonacci program with the one you write the dynamic version of it using various sizes of n (ex 15, 20,30, 40). You shall use embedded MATLAB function tic, toc to determine the time it takes for both algorithms. Discuss the time complexity of each method and compare them. Which one works faster and what is the time complexity of it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Question 1 (a2) What is the reaction force Dx in [N]?

Answered: 1 week ago