Out of all the DP solutions I have checked out for 0/1 knapsack and unbounded knapsack, solution approaches are always defined like this :
0/1 knapsack : Maximise total value by either taking n-th item, or excluding n-th item. For example, 0/1 knapsack
unbounded knapsack : Maximise total value by considering n-th item as the last picked item, or (n-1) item as last picked one etc, etc. For example, unbounded knapsack
But unbounded knapsack can also be solved using logic of 0/1 knapsack with a minor tweak. What I mean is, that for 0/1 knapsack, we do the following (using the code from first link):
return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),
knapSack(W, wt, val, n-1)
);
Notice how in the case when we are including wt[n-1]
we are reducing the size of array by 1. This implies that wt[n-1]
is now exhausted and hence, cannot be used again. But if in unbounded case, we don't reduce the array size by 1 (which would mean wt[n-1]
can be used again), following slightly modified recurrence relation works fine:
return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n),
knapSack(W, wt, val, n-1)
);
Why is this approach for unbounded knapsack never mentioned anywhere then? Actually here it specifically says, we cannot use same logic as 0/1 knapsack for the unbounded one. Excerpt from that link :
Observation:
I can never exhaust any item because there are an unbounded supply of items.
Therefore:
The technique used in the 0,1 knapsack problem cannot be used.
But I am not able to disprove that my above mentioned solution won't work. This idea came from coin-change problem, where you have to count number of ways to make change for a given amount, assuming infinite supply of coins.
So my question is why the approach that I have proposed here, never used for unbounded knapsack or at least never mentioned anywhere? Can anyone please help me in proving or disproving this approach? Thanks in advance!