Backtracking Solution for 0/1 Knapsack

#include <stdio.h>
#include <stdlib.h>

// length of the weights array
int arraySize = 0;
//maximum value that can be obtained
int maxSumValues = 0;
//maximum weight that can be obtained
int sumWeightLabel = 0;
//The array of values
int *values;
//The array of weights
int *weights;
//Whether to select the flag array of weight 
int *locations;
// When the maximum value is a certain value is selected, 1 is selected, 0 is not selected
int *maxSumValuesLocations;
/**

* Compare the maximum value and save the position of the maximum value 
*/

int getMaxSumValues(){
    int weightSum = 0;
    int maxSumValues_ = 0;
    
    for(int i = 0; i < arraySize; i++){
        if(locations[i]){
            weightSum += weights[i];
            maxSumValues_ += values[i];    
        }
    }
    
    if(maxSumValues_ > maxSumValues && weightSum <= sumWeightLabel){
        maxSumValues = maxSumValues_;
        for(int i = 0; i < arraySize; i++){
            maxSumValuesLocations[i] = locations[i];
        }
    }
}
/**
*    print array and the maximum sum of values
*/ 
void printArray(){
    printf("The weights: ");
    for(int i = 0;i < arraySize; i++){
        if(maxSumValuesLocations[i] == 1)
            printf("%5d ",weights[i]);
    }
    printf("\n");
    printf("The values: ");
    for(int i = 0;i < arraySize; i++){
        if(maxSumValuesLocations[i] == 1)
            printf("%5d ",values[i]);
    }
    printf("\n");
    printf("The max sum of values: %5d\n", maxSumValues);
}
/*
* permute weights
* Backtracking

*/
void permuteWeights(int index){
    if(arraySize == index)
        return ;
    //nth item included  
    locations[index] = 1;
    getMaxSumValues();
    permuteWeights(index + 1);
    
    //not included 
    locations[index] = 0;
    getMaxSumValues();
    permuteWeights(index + 1);
}

//Initialization data first 
void initKnapsack1(){
    //printf("Please input length of Array: "); 
    //scanf("%d",size); 
    arraySize = 3;
    /*
    * for(int i = 0; i < arraySize,i++)
    *      scanf("%d", values[i]);
    */
    values =  calloc(arraySize, sizeof(int));
    weights =  calloc(arraySize, sizeof(int));
    values[0] = 60,values[1] = 100,values[2] = 120;
    weights[0] = 10,weights[1] = 20,weights[2] = 30;
    locations = calloc(arraySize, sizeof(int));
    maxSumValuesLocations = calloc(arraySize, sizeof(int));
    sumWeightLabel = 50;
    maxSumValues = 0; 
    
}
//Initialization data second
void initKnapsack2(){
    arraySize = 4;
    values =  calloc(arraySize, sizeof(int));
    weights =  calloc(arraySize, sizeof(int));
    values[0] = 40,values[1] = 100,values[2] = 50,values[3] = 60;
    weights[0] = 20,weights[1] = 10,weights[2] = 40,weights[3] = 30;
    locations = calloc(arraySize, sizeof(int));
    maxSumValuesLocations = calloc(4, sizeof(int));
    sumWeightLabel = 60;
    maxSumValues = 0;
    
}
//Free cache
void freeKnapsack(){
    free(values);
    free(weights);
    free(locations);
    free(maxSumValuesLocations);

int main(){
    initKnapsack1(); 
    permuteWeights(0);
    printArray();
    freeKnapsack();
    
    initKnapsack2(); 
    permuteWeights(0);
    printArray();
    freeKnapsack();
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值