<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yokesh Rana on Medium]]></title>
        <description><![CDATA[Stories by Yokesh Rana on Medium]]></description>
        <link>https://medium.com/@yokeshrana?source=rss-8e65fdea2f16------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*cI8G-h1YeRavLhSYEe-0Yg.jpeg</url>
            <title>Stories by Yokesh Rana on Medium</title>
            <link>https://medium.com/@yokeshrana?source=rss-8e65fdea2f16------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 21:27:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yokeshrana/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Knapsack Problems]]></title>
            <link>https://medium.com/algobox/knapsack-problems-4ccee7c295ea?source=rss-8e65fdea2f16------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ccee7c295ea</guid>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[knapsack-problem]]></category>
            <dc:creator><![CDATA[Yokesh Rana]]></dc:creator>
            <pubDate>Sat, 20 Feb 2021 12:30:52 GMT</pubDate>
            <atom:updated>2021-02-20T12:46:45.829Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Knapsack Problems</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3bZOzhhzAtmcYYc427m5Aw.png" /></figure><p>This is in continuation of our previous post in the series .If you have not seen that please have a look to my earlier post in the series</p><p><a href="https://medium.com/algobox/ways-to-solve-dp-2-2cb0c5842391">Ways to Solve DP 2</a></p><p>Lets discuss first what we have to do in these kind of problem . Basically here you are given a sack of some capacity say W , and you are given some items with their associated cost of filling in the knapsack .The task is to fill the knapsack in such a way in so that our profit is maximised .</p><p>There are basically three kinds of KnapSack problems we see generally</p><ul><li><strong>Fractional Knapsack</strong></li><li><strong>0–1 Knapsack</strong></li><li><strong>Unbounded Knapsack</strong></li></ul><p>Lets discuss one by one what we have in these three</p><p><strong>Fractional Knapsack</strong></p><p>Here we have a freedom to include fractional values of items ie ,we can include items by breaking them if full item inclusion not possible.</p><p>Now we move on to the solution of this variation of knapsack</p><p>First of all this is not a DP question . It is solved using Greedy approach . Let’s say in the problem if we are given items and and their corresponding weights/cost .Our algorithm to solve this is like this</p><ul><li>Calculate the ratio of value/weight for each item</li><li>Sort the item on basis of this ratio/value density.</li><li>Now take the highest ratio item and add as much as possible density item not already in the bag.</li></ul><p>Lets discuss the time complexity of the above approach</p><p>O(nlogn) for sorting and O(n) for the greedy selection</p><p><strong>0–1 Knapsack Problem</strong></p><p>Here we have condition either we can include the item as a whole or we can’t include it at all.</p><p>This can be solved using DP as we have the fixed choices either we can include the item or we can’t include it at all.</p><p>Now lets try to first make the recursive solution of the problem and then slowly move to DP . Below is the recursive code to understand how we can solve this recursively</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CYJbvP0P5h3C-GYmQAkopA.png" /><figcaption>Recursice 0–1 Knapsack</figcaption></figure><p>So now as we got the recursive solution let&#39;s try to optimise it further and make some changes here in order to eliminate extra recursive calls.</p><p>Its easy to build the Top Down Solution from recursive just by including memory and checks to check whether already solution is calculated for this subproblem or not .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B2loKCcrqo19jEnVhemqtg.png" /><figcaption>Top Down Approach Knapsack 0–1</figcaption></figure><p>We see the problem is now optimised and the complexity reduced from exponential to O(nW).</p><p>There is another way also to think of the solution just to invert the top down approach and get the bottom up solution .Lets see the bottom up solution of the above problem</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cK4PqOOfBpTc7NOcOqQTdg.png" /></figure><p><strong>Why Bottom up ?</strong></p><p>The bottom up solution is more optimized and memory friendly .We see this because of advantage of bottom up iterative approach which has no recursion stack and its implied associated memory .</p><p>The complexity will remain same here also as O(nW).</p><p>So this is all about the 0–1 knapsack problems .</p><p><strong>Unbounded Knapsack Problems</strong></p><p>This is the third variation of the knapsack problem .Here also we are restricted with either full inclusion or exclusion from knapsack .But we have one freedom that we have a choice to include the item as many times as we want .</p><p>Lets discuss more on this . In the recursive implementation of 0–1 knapsack we have seen ,we are removing one element every time when ever we are calling the recursive function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H18lnQFhSN_VE89bOoAJBw.png" /><figcaption>Recursion in 0–1 knapsack</figcaption></figure><p>Only slight little change is needed for unbounded case . Here we don’t remove the item .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EVHoyHtAKp7paNLhfiMoVQ.png" /><figcaption>Recursion in unbounded knapsack</figcaption></figure><p>I hope now you are having understanding of all types of knapsack problems and how we can approach them to solve and many other similar problem which are based on this concept .</p><p>Stay tuned for more problems .Till then Happy Coding !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ccee7c295ea" width="1" height="1" alt=""><hr><p><a href="https://medium.com/algobox/knapsack-problems-4ccee7c295ea">Knapsack Problems</a> was originally published in <a href="https://medium.com/algobox">AlgoBox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ways to Solve DP 2]]></title>
            <link>https://medium.com/algobox/ways-to-solve-dp-2-2cb0c5842391?source=rss-8e65fdea2f16------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cb0c5842391</guid>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[bottom-up]]></category>
            <dc:creator><![CDATA[Yokesh Rana]]></dc:creator>
            <pubDate>Fri, 19 Feb 2021 16:46:23 GMT</pubDate>
            <atom:updated>2021-02-19T17:03:32.908Z</atom:updated>
            <content:encoded><![CDATA[<p>This is in continuation of our previous post in the series .If you have not seen that please have a look to my earlier post in the series</p><p><a href="https://medium.com/algobox/ways-to-solve-dp-2844b513c1e1">Ways to Solve DP</a></p><p>So in previous post we have seen the Top down approach to solve the DP problem . Now we will see the other approach to solve the problem</p><p><strong>Bottom Up Approach</strong></p><ul><li>It is also called as tabularisation .</li><li>This is generally harder to think and visualize in one go but with little practice and patience anyone can master it .</li><li>The basic idea here is to turn the recursive solution around get a bottom up solution instead .Since we already know the subproblems we can start building from the base case and move up to cover other cases iteratively.</li></ul><p>Lets understand this by taking the same fibonacci problem again . So here we first focus on the base cases ie we know for fibonacci problem</p><p>fib(0) =0 , fib(1) =1 ,fib(2)=1 and so on …</p><p>So we iteratively start from the base case and use the already calculated result to calculate the further result . Lets look at the solution to this problem using bottom up approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*EQJvxrLW_Lf6LgvPRTU8cA.png" /></figure><p>Since we are iterating here from 0 -n once, our time complexity will be O(n) and our space will also be O(n), as we have created a 1D array from 0 to n. This makes our current solution comparable to the top-down solution, although without recursion. This code is likely easier to understand.</p><p>I hope now identifying the DP problem and two approaches to solve the DP problem is clear .Lets look up Knapsack problems in our next post .</p><p>Till then Stay Tuned !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cb0c5842391" width="1" height="1" alt=""><hr><p><a href="https://medium.com/algobox/ways-to-solve-dp-2-2cb0c5842391">Ways to Solve DP 2</a> was originally published in <a href="https://medium.com/algobox">AlgoBox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ways to Solve DP]]></title>
            <link>https://medium.com/algobox/ways-to-solve-dp-2844b513c1e1?source=rss-8e65fdea2f16------2</link>
            <guid isPermaLink="false">https://medium.com/p/2844b513c1e1</guid>
            <category><![CDATA[dynamic-programming]]></category>
            <dc:creator><![CDATA[Yokesh Rana]]></dc:creator>
            <pubDate>Thu, 18 Feb 2021 21:02:12 GMT</pubDate>
            <atom:updated>2021-02-19T17:04:10.384Z</atom:updated>
            <content:encoded><![CDATA[<h3>Ways to Solve DP 1</h3><p>This is in continuation of our previous post in the series .If you have not seen that please have a look to my earlier post in the series</p><p><a href="https://medium.com/algobox/cant-we-make-dp-simpler-b09e69deb1a4">Can’t we make DP simpler ?</a></p><p>So in previous post we discussed ,how to identify the DP problem .Now lets looks into the first way of solving the identified problem.</p><p><strong>Top Down Approach for solving DP problems</strong></p><ul><li>Its also called as memoization</li><li>The basic idea here is to traverse the recursive call from top to bottom and eliminate the extra overlapping recursive calls by first checking whether they are already computed or not.</li></ul><p>To understand this better lets take example of fibonacci problem . We will try to write out the fibonacci recursion from what we already know</p><p>We know fib(0) = 0 , fib(1) =1 ,fib(2) = 1 ,we can see a pattern over here</p><p>fib(n) =fib(n-1) + fib(n-2)</p><p>Now here first we will try to solve it through recursion .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*-S2CMb465VIdZjPR2b-woQ.png" /></figure><p>Now if we trace out the recursion in the recursive tree for a call of n =4 it will look like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*fgw1CI0v4S74FnQNBZq4nA.png" /></figure><p>We can see the Complexity of the approach as follows</p><p>T(n) =T(n-1) + T(n-2) + O(1)</p><p>T(n) = O(2^n)</p><p>So its and exponential complexity problem ,lets try to analyse it further</p><p>We can see there are many subproblems that are overlapping ,so in order to efficiently solve this we use DP here .So we will maintain an additional memory in the form of dp array to save the result of the function calls ,and we will only call the recursive function if we have not computed the recursive call until now .</p><p>Lets walk through the Optimized Code now .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*TKLz2vsYsoHoFy5vyLu28w.png" /></figure><p>So now if we will see ,there are no more extra recursive calls .So it will significantly reduce the time complexity to O(n).</p><p>Next we will discuss the other approach to solve the DP problem .</p><p>Stay Tuned and Happy Coding !</p><p>Follow below link to move on to next.</p><p><a href="https://medium.com/algobox/ways-to-solve-dp-2-2cb0c5842391">Ways to Solve DP 2</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2844b513c1e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/algobox/ways-to-solve-dp-2844b513c1e1">Ways to Solve DP</a> was originally published in <a href="https://medium.com/algobox">AlgoBox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can’t we make DP simpler ?]]></title>
            <link>https://medium.com/algobox/cant-we-make-dp-simpler-b09e69deb1a4?source=rss-8e65fdea2f16------2</link>
            <guid isPermaLink="false">https://medium.com/p/b09e69deb1a4</guid>
            <category><![CDATA[dynamic-programming]]></category>
            <dc:creator><![CDATA[Yokesh Rana]]></dc:creator>
            <pubDate>Thu, 18 Feb 2021 18:45:30 GMT</pubDate>
            <atom:updated>2021-02-19T16:57:16.929Z</atom:updated>
            <content:encoded><![CDATA[<h3>Can’t we make DP simpler ?</h3><figure><img alt="Fun with DP" src="https://cdn-images-1.medium.com/max/1024/0*SKMKalFGU6YENNvr" /></figure><p>Dynamic Programming is one of the trickiest topic to learn and understand when people are starting with programming .</p><p>Lets see where we can apply DP .To apply DP to a problem we have to take care some of the points.We have to first see whether the problem contains two basic property to be solved using DP.</p><ul><li>Overlapping Sub problem property</li><li>Optimal Sub Structure Property</li></ul><p>Let’s understand one by one what we mean by it .</p><p><strong>Overlapping Sub problem property</strong></p><p>Here we decide whether the two sub problems are overlapping with the other sub problems or not .If they are not overlapping we can’t use DP , and that case is simply Divide and Conquer .To understand this better we look it recursive fibonacci problem .Suppose we want to find Fibonacci(4) ,so the recursive calls can be visualised like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0uYpKgHczoiPYobC_GJELw.png" /></figure><p>So as we see the recursive tree we can see there are various overallaping subproblem ,so in DP after calculating for once we dont calculate the sub problem again .</p><p><strong>Optimal Sub Structure property</strong></p><p>Here we decide whether we can break the problem in into optimal sub problems or not and then finding the solution of the problem.Optimal here have meaning as finding max ,min ,greatest and smallest .Let us look through a example . Suppose if we want to find out Fibonacci(4) ,then it can be broken into Fibonacci(3) + Fibonacci(2) .Easiest way to imagine this is to think whether any recursive solution exist for the problem or not .</p><p>To be Specific about what all problems can be solved using DP and its variation , we can categorise them as follows</p><ul><li>0 -1 KnapSack Problem</li><li>Unbounded KnapSack Problem</li><li>Longest Common Subsequence Problem</li><li>Longest Increasing Subsequence Problem</li><li>Matrix Chain Multiplication Problem</li><li>DP on Trees</li><li>DP on Graph</li><li>And Other Variations of DP</li></ul><p><strong>How DP is different from Divide and Conquer problems ?</strong></p><p>In DP we have overlapping subproblems but in Divide and Conquer each and every solution is different and they don’t overlap with each other.</p><p><strong>So how to solve any problem satisfying conditions of DP then ?</strong></p><p>There are basically two approaches we can go .</p><ul><li>Top Down Approach</li><li>Bottom Up Approach</li></ul><p>Keep reading ,we will cover the above two approach in next post . Stay tuned .</p><p>Follow below link to move on to next.</p><p><a href="https://medium.com/algobox/ways-to-solve-dp-2844b513c1e1">Ways to Solve DP</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b09e69deb1a4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/algobox/cant-we-make-dp-simpler-b09e69deb1a4">Can’t we make DP simpler ?</a> was originally published in <a href="https://medium.com/algobox">AlgoBox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>