Item i can’t be part of the solution, since its size is bigger than the knapsack’s current limit.
Again, we go through all the items and:Outer if: ??≤?. This means that the size of the item can fit in the current size of the knapsack and we should consider its possible maximum value.Outer else: ??>?.Then the item k can be in the solution, and we choose the case with greater value. Item k can’t be part of the solution, since if it was, the total size would be >s, which is unacceptableSecond case: ?_?≤?. Because when s = 0, this means that we can’t put anything in the knapsack.It means, that the best subset of ?_? that has total weight w is:The best subset of ?_(?−1) that has total weight ?,orThe best subset of ?_(?−1) that has total weight s−?_? plus the item ?The best subset of ?_?that has the total size ?, either contains item k or not.First case: ?_?>?. We go through all the items from 1 to n and if the knapsack’s size is equal to 0, which is “V” its corresponding values is again 0. Because when i = 0, this means that we are not taking any item.Second for-loop: i=1 ?? ?. We go through all the possible sizes of our knapsack until S and if item i is equal to 0, which is “V” its corresponding maximum value is of course, 0. Then the item k can be in the solution, and we choose the case with greater value.It means, that the best subset of ?_? that has total weight w is:The best subset of ?_(?−1) that has total weight ?,orThe best subset of ?_(?−1) that has total weight s−?_? plus the item ?The best subset of ?_?that has the total size ?, either contains item k or not.First case: ?_?>?. Item k can’t be part of the solution, since if it was, the total size would be >s, which is unacceptableSecond case: ??≤?. This means that the best subset of ?? that has the total size ?,can either contains item k or not.First case: ??>?.Since there are nitems, there are 2? possible combinations of itemsWe need to go through all combinations to find the one with maximum value with total size We want maximizing our chance to get more points.If there was partial credit that was proportional to the amount of work done (e.g., one hour spent on problem C earns you 2.5 points), that is what we call now the Fractional Knapsack the best approach is to work on problems in order of points/hour (a greedy strategy). Say the value and time for the problem set are as follows… And say you have a total of 15 hours – the knapsack – : which parts should you do? With the 0-1 Knapsack, you need to know which parts you should do to get the best total value possible. Each part has a “value” (in points) and a “size” (time in hours to complete). Imagine you have a problem set with different parts labelled A through G.Solved with dynamic programming2.Items are divisible: you can take any fraction of an item. Items are indivisible: you either take an item or not.