Knapsack
General analysis and formulation
In the Knapsack problem, we assume we are a thief that has a knapsack that can hold a maximum weight of . He now goes into a house and sees n items of value and with a weight of . Which items should the thief take to get the maximum profit?
1. Identifying the sub-problems Think of it as suffixes of items, this means what are we going to decide about item i? If we think about it in a normal way, then we would get that we or pick to item, or leave it. So if we pick it we get an extra value, but if we leave it we just go to the next item.
This however is NOT correct! Because we also have a constraint, which says that we can not have a weight more than the capacity S which our knapsack can hold.
2. Guessing Is item i in the subset or not? --> 2 choices
Here we can see we have 2 choices:
Include in the subset
Do not include in the subset
3. Relating subproblems to a solution
Coding our solution
Bottom-up
To code our solution we can again think of the top down way and the bottom up way. Let us start with the top-down way since this will explain us properly on how the solution works. So for the base case (which is on 0 items and total capacity of 0) this will return 0 (maximum value that we can put in our knapsack).
Now for the rest of the cases we will just go through the solutions. Every time making sure that we do not exceed our total capacity W.
Top-Down
Last updated
Was this helpful?