Classical algorithm of dynamic programming (knapsack problem)

Question: There are currently four items, the total capacity of the backpack is 8, what is the maximum value that the backpack can hold

Item number: 1 2 3 4

Item volume: 2 3 4 5

Item value: 3 4 5 6

Number\Capacity 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 3 3 3 3 3 3 3
2 0 0 3 4 4 7 7 7 7
3 0 0 3 4 5 7 8 9 9
4 0 0 3 4 5 7 8 9  

Ideas for filling in the form:

  1. If the current item cannot be loaded, the best combination of the first n items is the same as the best combination of the first n-1 items.
  2. Can load the current item
    1. Load the current item, reserve the corresponding space for the current item in the backpack, the best combination of the first n-1 items plus the value of the current item, is the total value
    2. If the current item is not loaded, then the previous The best combination of n items is the same as the best combination of the first n-1 items
    3. Select the larger value of 1 and 2 as the value of the current best combination

Back to the backpack problem:
In the case of maximizing the total value of the backpack, which items are contained in the backpack

Analysis: The current value is 10. If item 4 is not installed, then the current value (10) should be the same as the total value of the first three items (9). Obviously 10 and 9 are different, so item 4 was put in.

Summary: Looking back from back to front, if the value of the best combination of the first n items is the same as the value of the best combination of the first n-1 items, it means that the nth item has not been loaded into the backpack. Otherwise, it is loaded into a backpack.

Code

// Dynamic programming

/* 物品编号  1   2   3   4
   体积     2   3   4   5
   价值     3   4   5   6*/

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

int weight[5] = {0, 2, 3, 4, 5};
int value[5] = {0, 3, 4, 5, 6};
int dp[5][9] = {0};
int object[5];

int max(int x, int y){
    return x>y?x:y;
}

void printDp() {
    
    for(int i=0;i<5;i++) {
        for (int j=0; j<9; j++) {
            printf("%d\t",dp[i][j]);
        }
        printf("\n");
    }
        
}

int dpWrite() {

    memset(dp,0,sizeof(dp));
    for (size_t i = 1; i < 5; i++) //物品编号
    {
        for (size_t j = 1; j < 9; j++) // 背包容量
        {
            if(weight[i]>j) //物品放不下
                dp[i][j] = dp[i-1][j]; 
            else
                dp[i][j]= max(dp[i-1][j], value[i] + dp[i-1][j-weight[i]]);
        }
        
    }
    printDp();
}

// 背包回溯问题
void Find(int i, int j) {
    if (i == 0) {
        for (int ii=0; ii<5; ii++) {
            printf("%d ",object[ii]);
        }
        return;
    }
    // 没装入背包
    if (dp[i][j] == dp[i - 1][j]) {
        object[i] = 0;
        Find(i-1, j);
    }
    // 装入背包
    else if (dp[i][j] == value[i] + dp[i - 1][j - weight[i]]) {
        object[i] = 1;
        Find(i-1, j-weight[i]);
    }
}

int main() {
    dpWrite();
    Find(4, 8);
    printf("\n(%d, %d)===>[",4, 8);
    for (int i=0; i<5; ++i) {
        printf("%d ", object[i]);
    }
    printf("]\n");
}

 

Guess you like

Origin blog.csdn.net/weixin_44937328/article/details/115350692