Skip to main content

Dynamic Programming optimizations for fibonacci series

 

  • the sum of preceding two number.

for example, f(n) = { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , ……………..}

mathematical function: f(n) = f(n-1) + f(n-2) , where n>=2 and f(0) = 0 , f(1) = 1

  • Sample program:

/* simple recursive program for Fibonacci series */

int fibonacci(int n)

{

if (n<=1)

return n;

return fibonacci(n-1)+fibonacci(n-2);

}

  • Overlapping problem:


In the above tree diagram we can see (for underlined subtrees) that the left subtree of fib(5) and right subtree of fib(4) are repeated likewise other subtrees are also repeated , to avoid this we use dynamic programming (here Memoization technique particularly).

  • Optimized function code to reduce time complexity:

int fibonacci(int n)

{

if (table [n] == -1)

{

if (n<=1)

table [n] = n;

else

table [n] = fibonacci(n-1)+fibonacci(n-2);

}

return table [n];

}

In above problem we created a lookup table to save values which are calculated first and use them again if required rather than recalculating it, it will reduce much time for execution of a code .

Time required for calculating fibonacci(40):

  1. Recursive function: approx 14 secs.
  2. Using Memoization technique: approx 0.17 secs.

Comments

Popular posts from this blog

Experience Of Summer Internship From Red Hat, Inc.

  About the Author:  Varad Parlikar, a student of Deogiri Institute Of Engineering And Management Studies, Babasaheb Ambedkar Marathwada University, shares how the summer internship helped him to improve his skills in Open Source Domain . He also shares snippets of his Summer Internship experience from Redhat Inc. This year I have had the incredible opportunity to intern for the Redhat Inc.I was in the third year of engineering curriculum of computer science stream,The hunt for the summer internship began.I had always wanted to work with Redhat Group,So I decided to appear for the summer internship program from Redhat,Pune.I submitted my resume for the same and I got shortlisted for the summer internship program at Redhat,Pune.So I travelled for the same to pune as per the schedule of summer internship program. The selection process started off with an aptitude test after which the shortlisted students were interviewed by them.I was asked questions on basic linux knowledge and...

Foreman,A Open Source Project

  1. What is Foreman? Foreman is an open source project that helps system administrators manage servers throughout their lifecycle, from provisioning and configuration to orchestration and monitoring. Using Puppet, Chef, Salt, Ansible and Foreman’s smart proxy architecture, you can easily automate repetitive tasks, quickly deploy applications, and proactively manage change, both on-premise with VMs and bare-metal or in the cloud. Foreman provide comprehensive, interaction facilities including a web frontend, CLI and RESTful API which enables you to build higher level business logic on top of a solid foundation. The Foreman interface is very easy to use and user friendly. As your organization grows, so does your workload — and the IT resources required to manage it. There is no “one-size-fits-all” system management solution, but a centralized, open source tool such as  Foreman  can help you manage your company’s IT assets by provisioning, maintaining, and updating hosts th...

CRM, a vast ocean of code I saw

  https://crm.zoho.com/ Head over to the Zoho CRM and see for yourself, well for the first time you will not feel it but as you go deep in it, you will realise the small small things that this CRM has implemented then you will get amazed by this amazing product. As a programmer, I got the chance to look at the vast ocean of code of this ZOHO CRM and experience like I was swimming in the ocean not knowing which way goes to where , I mean literally I was amazed how it has expanded,how it created little little things for its customers, the feeling of debugging it was on another level. I am grateful for the opportunity given by ZOHO to me for working in such a vast product. This CRM that I worked in won many awards, it had gone into Paul Greenberg’s (PG) CRM Watchlist 2022 award against major players like Adobe, Microsoft, Oracle, SAP, Salesforce, and ServiceNow with the highest score and distinction , PCMag.com awards “Business Choice Awards 2019” to Zoho CRM, beating 4 times winner —...