
Our base case is K(0) yielding a value of 0 because no item has a weight ≤ 0.įor this problem we should be able to use a simple 1-dimensional table (array) from w 1 to W in length. We use the max() function to ensure we select the subproblem parameters that yield the highest value. What we're doing here is trying all possibilities for items to add while factoring in the weight capacity reduction incurred by that item. So basically, each subproblem will operate on a smaller and smaller weight limit and we'll try our items available against that smaller limit. K( w) = max value attainable with a total weight ≤ w Given that, we can define our subproblem as: Let w be a weight less than our max weight W. Since the grocery store has lots of stock available, it's fine to pick the same item multiple times.įirst let's define our subproblem. I call this the "Grocery Store" variant because I like to think of it as being like Supermarket Sweep where participants race to fill a shopping cart with the highest valued items possible. The first variation of the knapsack problem allows us to repeatedly select the same item and place it in the bag.

Repeated Selection (Grocery Store Variant) We want to choose the optimal combination of items from such that we maximize the total value of our items without exceeding the maximum weight limit W.įor the sake of the problems below, we'll consider the following knapsack and collection of items: Each of these n items has a value v that can be selected from the array v 1.v n.Each of these n items has a weight w that can be selected from the array w 1.w n.A knapsack that can hold a total weight W.Let's restate the problem a bit more formally this time. Now back to our regularly scheduled programming. This was a pretty simple example of Dynamic Programming, but we will use these same thought processes and techniques to solve the knapsack problem. Now look at the array T below to help visualize this: Here T represents a smaller subproblem - all of the indices prior to the current one. What about element 2? Do we need to loop over them all again for each one? Using Dynamic Programming we can do this a bit more efficiently using an additional array T to memoize intermediate values. Now let's say we want to know the prefix sum up to element 5.

Simple enough, just loop over and add up the values before it. Say we want to do a prefix sum across the array and we're specifically interested in element 4 (highlighted in red). Results of smaller subproblems are memoized, or stored for later use by the subsequent larger subproblems. At it's most basic, Dynamic Programming is an algorithm design technique that involves identifying subproblems within the overall problem and solving them starting with the smallest one. You may have heard the term "dynamic programming" come up during interview prep or be familiar with it from an algorithms class you took in the past. Items can be selected at most once (the museum variation)īefore we dive in, though, let's first talk briefly about what Dynamic Programming entails.ĭetour: Brief Introduction to Dynamic Programming.Items can be selected repeatedly (the grocery store variation).In this post, we'll explain two variations of the knapsack problem: It's one of the most well studied combinatorial optimization problems and a popular introduction to dynamic programming. You want to fill the backpack with the most valuable combination of items without overburdening it and going over the weight limit.

You have a set of items at your disposal, each being worth a different value and having a different weight. Consider a backpack (or "knapsack") that can hold up to a certain amount of weight.
