The classic algorithm is as follows:
for i in items:
for w in possible total weights (downwards):
if w is achievable with maximum value v:
(w + weight[i]) is also achievable with at least value (v + value[i])
The approach here will be a small change:
for c in classes:
for w in possible total weights (downwards):
if w is achievable with maximum value v:
for i in items of class c:
(w + weight[i]) is also achievable with at least value (v + value[i])
In your code, the changes will be as follows:
, . , , , value weight , numberOfClasses numberOfClassItems .
/" > , , 1 (w = 2, v = 3) (w = 3, v = 5), 2: (w = 1, v = 1), (w = 4, v = 1) (w = 1, v = 4). :
totalNumberOfItems = 5,
numberOfClasses = 2,
numberOfClassItems = [2, 3],
value = [[3, 5], [1, 1, 4]]
weight = [[2, 3], [1, 4, 1]].
, 0. 1, , .
for (currentItem) for (currentClass). optimum solution currentClass currentItem.
option2 : :
double option2 = Integer.MIN_VALUE;
for (currentItem = 1; currentItem <= numberOfClassItems[currentClass];
currentItem++){
if(weight[currentClass][currentItem] <= currentWeightUnit){
option2 = Math.max (option2, value[currentClass][currentItem] +
optimum[currentClass - 1][currentWeightUnit -
weight[currentClass][currentItem]]);
}
}
, solution int boolean: , , - (0 -1), option1 - .