20200408-- algorithm knapsack problem

Problem Description:

A total capacity of backpack is V, there are N items are, by weight class i articles of weight [i], the value for the value [i]
then go to the backpack loaded, what means to that the final package goods total The maximum value. Here and installation method consists predominantly of three items:
1,0-1 backpack: each category of items can hold up to a
2, multiple backpack: the number of each type of item has a limited class i article can carry a maximum NUM [i] times
3, complete backpack: each category of items put into the bag within unlimited

一、01背包 

Analysis of ideas:

0-1 knapsack problem mainly related to two issues solved

a) Contents of the backpack maximum Solution:

Using the method of dynamic programming optimal value. Suppose that dp[N][V]to store intermediate state value dp[i][j]indicates the front i articles can be loaded to capacity j the sum of the value of the article in the backpack maximum (note the maximum value), then we need knowledge final dp[i=N][j=V]value, that is the subject of the request .

Now consider an array of dynamic programming dp [i] [j] is the state transition equation:

Suppose we have calculated the sum of the maximum value of i-1 before the capacity of the items loaded backpack is j dp[i-1][j], j fixed capacity value remains unchanged, then the first iinstallation method items are discussed below:
First, by weight of the i-th items weight [i] be less than the capacity of the job j, i.e.,

1, if weight[i]>j, of the i items certainly not charged capacity backpack j, thendp[i][j]=dp[i-1][j]

2, if weight[i]<=j,the first clear is that items can be loaded backpack capacity of j, then if we loaded the article, there are

dp[i][j]=dp[i-1][j-weight[i]]+value[i] 

The attendant problem is that we have to determine first i items to capacity after j backpack, if the total value is the largest in the backpack? In fact well determined, i.e., if installed after the first total value of the i items dp[i-1][j-weight[i]]+value[i]>没装之前的总价值最大值dp[i-1][j], the largest Ken; otherwise described items i-j need not charged capacity backpack (had a smaller total value after installed, then certainly does not need to install them)
so, the state transition equation is as follows:

dp[i][j] = (dp[i-1][j] > (dp[i-1][j-weight[i]]+value[i]))? dp[i-1][j]:(dp[i-1][j-weight[i]]+value[i]) 

NOTE: This is before the i items in the order given

b) obtaining the article ID charged backpack

Here we use the idea of inverse push to deal with, if for dp[i][j]>dp[i-1][j], then the i-th article must have been placed in a backpack, this time we re-examine dp[i-1][j-weight[i]] the number on it.

/**
     * 0-1背包问题
     * @param V 背包容量
     * @param N 物品种类
     * @param weight 物品重量
     * @param value 物品价值
     * @return
     */
    public static String ZeroOnePack(int V,int N,int[] weight,int[] value){

        //初始化动态规划数组
        int[][] dp = new int[N+1][V+1];
        //为了便于理解,将dp[i][0]和dp[0][j]均置为0,从1开始计算
        for(int i=1;i<N+1;i++){
            for(int j=1;j<V+1;j++){
                //如果第i件物品的重量大于背包容量j,则不装入背包
                //由于weight和value数组下标都是从0开始,故注意第i个物品的重量为weight[i-1],价值为value[i-1]
                if(weight[i-1] > j)
                    dp[i][j] = dp[i-1][j];
                else
                    dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-weight[i-1]]+value[i-1]);
            }
        }
        //则容量为V的背包能够装入物品的最大值为
        int maxValue = dp[N][V];
        //逆推找出装入背包的所有商品的编号
        int j=V;
        String numStr="";
        for(int i=N;i>0;i--){
            //若果dp[i][j]>dp[i-1][j],这说明第i件物品是放入背包的
            if(dp[i][j]>dp[i-1][j]){
                numStr = i+" "+numStr;
                j=j-weight[i-1];
            }
            if(j==0)
                break;
        }
        return numStr;  
    }

Self-summary

A first loop element in the array, the i-th item is placed, that is to say can be placed from 0 ~ i-1 of this article, the highest values to see which combination. j is said that the current capacity of the backpack,
if this item is greater than the current capacity of the total capacity of the backpack, then put this article certainly does not come in, a local maximum current, the current value is inappropriate in this article is, dp[i-1][j]
if we can go into it , it is more about, minus the current capacity of the total capacity, and then add this value on a without the article, comparing the maximum value, if large then update.
Here Insert Picture Description
To ensure that the local optimization.

Published 955 original articles · won praise 43 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/105392614