dynamic - Unbounded Knapsack to minimize the total value -
i trying find solution minimum knapsack problem (least profitable set of items such total weight of selected items @ least capacity c.)
i trying modify following maximum knapsack problem min version without success.
could solve problem?
public class unboundedknapsack { public static int filltable(int capacity, int[] weight, int[] value) { int i, j; int[][] maxvalue = new int[value.length][capacity + 1]; (j = 0; j < weight[0]; j++) { maxvalue[0][j] = 0; } (j = 0; j <= capacity; j++) { maxvalue[0][j] = value[0] * (j / weight[0]); } (i = 1; < value.length; i++) { (j = 0; j < weight[i]; j++) { maxvalue[i][j] = maxvalue[i - 1][j]; } (j = weight[i]; j <= capacity; j++) { maxvalue[i][j] = math.max(maxvalue[i - 1][j], value[i] + maxvalue[i][j - weight[i]]); } } return maxvalue[value.length - 1][capacity]; } }
Comments
Post a Comment