<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sourav Saraf</title>
    <description>The latest articles on DEV Community by Sourav Saraf (@souravsaraf2000).</description>
    <link>https://dev.to/souravsaraf2000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F560277%2F0f0304ce-20f1-4fae-b99c-06950fa10049.jpeg</url>
      <title>DEV Community: Sourav Saraf</title>
      <link>https://dev.to/souravsaraf2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/souravsaraf2000"/>
    <language>en</language>
    <item>
      <title>Dynamic Programming</title>
      <dc:creator>Sourav Saraf</dc:creator>
      <pubDate>Fri, 26 Feb 2021 05:09:22 +0000</pubDate>
      <link>https://dev.to/edualgo/dynamic-programming-d62</link>
      <guid>https://dev.to/edualgo/dynamic-programming-d62</guid>
      <description>&lt;p&gt;Hello Folks,&lt;br&gt;
In this blog we are trying to provide you a flavour of what actually Dynamic Programming is and how to use it.&lt;br&gt;
To provide clear understanding we will be referring to a very common problem named &lt;strong&gt;Fibonacci Series&lt;/strong&gt; with which we are well aware of.&lt;br&gt;
There are multiple ways of solving the problem. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Recursion&lt;/li&gt;
&lt;li&gt;Top Down DP&lt;/li&gt;
&lt;li&gt;Bottom Up DP&lt;/li&gt;
&lt;li&gt;Space Optimised Approach.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will be discussing each approach in details.&lt;/p&gt;

&lt;p&gt;So, Let's Start!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Recursion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We know that in Recursion, a function calls itself multiple times till it hits the base case to solve a particular problem. If we initially try to understand how exactly is recursion working then it will be a problem and we may get confused and may not find a way to solve the problem. Initially we should be focusing on large problem and how it can be solved. Then we should define a function and tell it where to stop i.e specify the base case and write code to solve the larger problem and leave the rest in hands of recursion. &lt;br&gt;
Like for the Fibonacci series we know that 1st term is 1 and nth term is sum of previous 2 terms i.e n-1 and n-2. So lets write that and leave the rest to recursion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int fibonacci(int n)
{
    if(n==1 || n==0)
        return n;
    return fibonacci(n-1)+fibonacci(n-2);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above function is going to work absolutely fine but &lt;strong&gt;is this an an optimal approach&lt;/strong&gt;? &lt;/p&gt;

&lt;p&gt;Lets try to understand it. Suppose using above function we are trying to find the 5th Fibonacci term. The above code will generate the following recursion tree.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1jhwg14gerjqh6vdp6e.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1jhwg14gerjqh6vdp6e.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that recursion tree repeats for some elements a number of times (Highlighted in &lt;strong&gt;Red&lt;/strong&gt;) i.e same work is done again and again. The number of repetition will be even more higher for large numbers. Also the above code has time complexity of O(2^n) which is exponential and is really very bad. &lt;/p&gt;

&lt;p&gt;So the answer to above question is &lt;strong&gt;NO&lt;/strong&gt;. This is not an optimal approach.&lt;/p&gt;

&lt;p&gt;Nothing to worry about 😊. Dynamic Programming will sort it out.&lt;/p&gt;

&lt;p&gt;So what's Dynamic Programming then? &lt;/p&gt;

&lt;p&gt;A quote by &lt;strong&gt;Winston Churchill&lt;/strong&gt; says:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Those who fail to learn from history are condemned to repeat it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is what Dynamic Programming does. We store the already calculated value so that it can be used as and when required and re-calculation can be avoided.&lt;/p&gt;

&lt;p&gt;Let's Discuss them!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Top Down DP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we start from large sub-problem and reach base case it is called Top Down DP Approach. In this approach we use &lt;strong&gt;Recursion + Memoization&lt;/strong&gt; i.e calculate the value using recursion and store it in some array so that we don't have to re-calculate. &lt;br&gt;
Lets see how can Fibonacci Series problem can be solved with this approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int dp[100]={0}; // I am assuming size to be 100. It can be anything
int fibonacci(int n)
{
    if(n==0||n==1)
        return n;
    if(dp[n]!=0)
        return dp[n]; //If the value has already been calculated, return it
    dp[n] = fibonacci(n-1)+fibonacci(n-2);
    return dp[n];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you say about the above code ? Far better then that of recursion (of course not in length :)). &lt;br&gt;
So, &lt;strong&gt;the time complexity of above code is O(N) and Space Complexity of O(N) is also required&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Bottom Up DP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this approach we start from base case and reach the required solution. No recursion is needed in this approach.&lt;/p&gt;

&lt;p&gt;Have a look at the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int fibonacci(int n)
{
    int dp[100]={0}; // I am assuming size to be 100. It can be anything
    dp[1]=1; //We need to provide starting values
    for(int i=2;i&amp;lt;=n;i++)
    {
        dp[i]=dp[i-1]+dp[i-2];
    }
    return dp[n];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time and Space Complexity remains the same as above i.e O(N).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope that the above explanation and code gave you some idea and flavour of what Dynamic Programming is and how it works. Don't worry. I have not forgotten the 4th section. Its &lt;strong&gt;not&lt;/strong&gt; something very much related to DP so I will discuss it below 😊.&lt;/p&gt;

&lt;p&gt;So from the above discussion we can conclude that DP highly optimised the code and saves computational resources like memory. &lt;br&gt;
A huge amount of practice is required for achieving expertization in Dynamic Programming and having knowledge of other algorithms may also help in reaching an optimal DP Solution.&lt;/p&gt;

&lt;p&gt;Now its time for Space Optimised approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Space Optimised Approach&lt;/strong&gt;&lt;br&gt;
This approach may not necessarily work for all problems as it works for Fibonacci Sequence. In this approach we can find the nth element even without using any array!&lt;/p&gt;

&lt;p&gt;Have a look.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int fibonacci(int n)
{
    int a=0,b=1,c;
    for(int i=2;i&amp;lt;=n;i++)
    {
        c=a+b;
        a=b;
        b=c;
    }
    return c;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, lets end our blog here.&lt;br&gt;
Thanks to all the readers for their time. Any feedbacks are always welcome. Hope to see all of you in my upcoming blogs as well.&lt;br&gt;
Thank You 😊.&lt;/p&gt;

</description>
      <category>help</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
