Thread: how to print used values for knapsack problem

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    how to print used values for knapsack problem

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You have an if / else, both of which contain return statements.

    So yes, your code is never reached.

    Consider something like
    Code:
    int taken = takeWeights[amount-1] > limit;
    int trueKnapsack;
    int falseKnapsack;
    if ( taken ) {
      trueKnapsack = knapsack(amount-1, limit, takeWeights, values); 
    } else {
      falseKnapsack = 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) );
    }
    
    // now you can print something...
    
    // Then do
    if ( taken ) {
      return trueKnapsack;
    } else {
      return falseKnapsack;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Knapsack problem
    By viblic in forum C Programming
    Replies: 8
    Last Post: 12-04-2011, 02:58 PM
  2. 0-1 knapsack problem
    By madennn in forum C Programming
    Replies: 2
    Last Post: 03-29-2011, 07:08 AM
  3. knapsack problem?
    By nimitzhunter in forum General Discussions
    Replies: 7
    Last Post: 01-28-2011, 01:16 PM
  4. knapsack problem
    By fnfn in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2007, 03:14 PM
  5. Knapsack Problem
    By Fork in forum C Programming
    Replies: 4
    Last Post: 11-13-2006, 12:42 PM

Tags for this Thread