

Then every time we call the recursion we first check the table to see if the solution was computed already. Top-down dynamic programming means that we’ll use an intuitive and recursive algorithm to solve the problem, but instead of simply returning the computer values from our function we’ll first store it in an auxiliary table. When you have this scenario (i.e., optimal sub-structure and overlapping sub-problems) you know what you can use the dynamic programming technique, which basically involved storing the solutions to each sub-problem, so that you just need to compute them once.Įnough talk though, let’s see some code. That is because the sub-problems are not independent. One problem that will arise is the re-computation of sub-problems over and over again (which is called overlapping sub-problems). Once you solve this sub-problem you just need to call another recursion, adjusting two things: the item you are working with and the weight you still have available. With this smaller sub-problem you’ll basically need to decide between two things: to take the item (in which case you get the value of the item but lose capacity in proportion to its weight) or to not take the item (in which case you don’t get any value but don’t lose any weight either). That is, instead of thinking with all the items at the same time, we think about having only one item and a certain size available in the knapsack. We can break the problem into smaller sub-problems (which is called optimal sub-structure in computer science) and solve it recursively (i.e., divide and conquer). The other extension of this problem is Polynomial Time Approximation Algorithm (PTAS) which is well explained in. This signifies that the constraint are not complex and one can follow a Greedy approach to solve this problem. Once n grows slightly, this approach becomes unfeasible. Two problems are considered: Linear knapsack problem (LKP) - the objective function and constraint(s) are linear. Since this is a small problem set it’s not difficult to see the answer is the vase and the painting, for a total value of $90, but if there were more items computing the answer wouldn’t be so easy.Ī brute force approach (i.e., testing all item combinations and keeping the one with the highest value) would take 2^n, where n is the number of items.


A mirror that weights 5 pounds and is worth 10 dollars.A painting that weights 4 pounds and is worth 40 dollars.A silver nugget that weights 6 pounds and is worth 30 dollars.A vase that weights 3 pounds and is worth 50 dollars.Here’s the description: Given a set of items, each with a weight and a value, determine which items you should pick to maximize the value while keeping the overall weight smaller than the limit of your knapsack (i.e., a backpack).įor example, suppose you are a thief and you invaded a house. The Knapsack problem is probably one of the most interesting and most popular in computer science, especially when we talk about dynamic programming.
