am trying to implement a solution under java for the knapsac
am trying to implement a solution under java for the knapsack algorithm attached below. there are logic flaw withs maximize function it cannot return any value other than 0.
here is my implementation
Solution
public class Main {
public static void main(String[ ] args) {
int weights [ ] = new int [ ] {19,30,130,137,173,182,209,225,259,260};
int limit = 1028;
int upTo = weights.length;
System.out.println(maximize(weights,limit,upTo));
}
public static int maximize(int [ ] weights, int limit, int upTo){
//base case
if (limit == 0 || upto == 0)
return 0;
//if weight of items is greater than capacity then this item cannot be included in solution
else if (limit > weights[upTo-1])
return maximize(weights,limit,upTo-1);
//return maximum of 2 case number of item included and not included
else return maximize(weights,limit-weights[upTo-1],upTo-1);
}
//use this to start off the computation
public static int max(int [ ] weights, int limit)
{
return maximize(weights,limit,weights.length);
}
