i'm trying to figure out how to print all used weights, the functions used of knapsack.
Code:
int knapsack(int amount,int limit, int takeWeights[], int values[]){
int used[SIZE];
int i,j,k,l,take,dontake;
if (amount == 0 || limit == 0) // stopping condition
return 0;
if (takeWeights[amount-1] > limit) // if thief took an item that increase the total weight over his limit,
return knapsack(amount-1, limit, takeWeights, values); // skip to next item on list
else return max(values[amount-1]+knapsack(amount-1,limit-takeWeights[amount-1],takeWeights,values),// compares between two items, and takes the item
knapsack(amount-1,limit, takeWeights, values) );
above is the standart recursive function for getting the max value that can be obtained.
my idea for my question was as following:
Code:
take = values[amount-1]+knapsack(amount-1,limit-takeWeights[amount-1],takeWeights,values);
dontake = knapsack(amount-1,limit, takeWeights, values);
if(take){
for(i=0;takeWeights[i] && limit>=0;i++)
limit = limit-takeWeights[i];
printf("Item #%d of weight %d and value %d \n", i, takeWeights[i], values[j]);
which is directly written under the first part of the code.
but it doesn't seem to work.
after debugging, i realized that it isn't working because it's not reaching it at all.
i'm a begginer, so i sturggle to understand what's the problem.
your help will be much appreciated