If the supply ( w_i - q_i where q_i is the quantity that we took from the total amount of item i) of that item is exhausted and we can still carry more ( weightLimit > currWeight), we take as much as possible of the item with the next greatest value per pound, and so forth, until we reach our weight limit. Obeying a greedy strategy, the thief begins by taking as much as possible of the item with the greatest value per pound. To solve the fractional problem, we first compute the value per pound v_i = b_i / w_i for each item i.
FRACTIONAL KNAPSACK PLUS
consider that if we remove a weight w of one item j from the optimal load, the remaining load must be the most valuable load weighing at most W - w that the thief can take from the n-1 original items plus w_j - w pounds of item j (the remains of item j). Explanation: fractional knapsack problem is also called continuous knapsack problem.If we remove item j from this load, the remaining load must be the most valuable load weighing at most W - w_j that the thief can take from the n-1 original items excluding item j.For the 0-1 problem, consider the most valuable load that weighs at most W pounds. The most important point is that we can take the fraction of the last item to completely fill our bag (if adding a whole item exceeds W).
You can think of an item in the 0-1 knapsack problem as being like a gold ingot and an item in the fractional knapsack problem as more like gold dust.īoth knapsack problems exhibit the optimal-substructure property. In this setting, the thief can take fractions of items, rather than having to make a binary (0-1) choice for each item.