ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 选择0/1背包中的物品,其中两个物品具有相同的好处|最大化价值并最小化重量

2019-07-11 17:03:36  阅读:154  来源: 互联网

标签:java dynamic-programming knapsack-problem


在0/1背包问题中,如果两个项目具有相同的值,如何选择项目.应选择体重较轻的值,如何检查该状况?我使用动态编程有以下功能.

static int[] knapsack(int maxWeight, double[] weight, double[] value, int n) {
    //n = no. if items
    int i, w;

    double array[][] = new double[n + 1][maxWeight + 1];
    for (i = 0; i <= n; i++) {
        for (w = 0; w <= maxWeight; w++) {
            if (i == 0 || w == 0)
                array[i][w] = 0;
            else if (weight[i - 1] <= w)
                array[i][w] = max(value[i - 1] + array[i - 1][(w -(int) weight[i - 1])], array[i - 1][w]);
            else
                array[i][w] = array[i - 1][w];
            if (i != 0 || w != 0)
                System.out.print(array[i][w] + "\t");
        }
        System.out.println();
    }

    int[] selected = new int[n + 1];
    for (int j = n, wt = maxWeight; j > 0; j--) {   
        if (array[j][wt] != array[j - 1][wt]) {
            if (array[j][wt] == array[j][wt - 1]) {
                selected[j] = 0;
                break;
            }
            selected[j] = 1;
            wt = wt - (int) weight[j - 1];
        }
        else
            selected[j] = 0;
    }
    /** Print finally selected items **/
    System.out.println("\nItems selected : ");
    for (int k = 1; k < n + 1; k++)
        if (selected[k] == 1)
            System.out.print(k +" ");
        System.out.println();

        return selected;
}

对于这种情况:(i,v):( 4,45)(3,20)(5,30)(2,45),maxWeight = 5;
 如果第1项和第4项具有相同的值,则应选择权重较小的第4项.如何在上面的代码中实现这个条件.
问题陈述 :

Your goal is to determine which things to put into the
package so that the total weight is less than or equal to the package
limit and the total cost is as large as possible. You would prefer to
send a package which weights less in case there is more than one
package with the same price.

解决方法:

如果您的意思是最大限度地减少重量值.你可以检查一下

设DP [i] [j]是可以获得的最大值!

并且W [i] [j]要使用的最小重量!!

然后,

if(Current Weight > Element's Weight)
{
      if(DP[i-1][j-Weight[i]]+Value[i]>DP[i-1][j]){
             DP[i][j]=DP[i-1][j-Weight[i]]+Value[i];
             Weight[i][j]= Weight[i-1][j-Weight[i]]+Value[i]
      }
      else if(DP[i-1][j-Weight[i]]+Value[i] <  DP[i-1][j] ){
             DP[i][j]=DP[i-1][j];
             Weight[i][j]=Weight[i-1][j];
      } 
      else{                   //Note this is the tricky part elsewise the 
                              //Above conditions are simple Knapsack conditions

             DP[i][j]=DP[i-1][j];   //Both of them are equal We will get same Value . Thus we cannot maximise it in any other way!!
             Weight[i][j]=minimum ( Weight[i-1][j] ,Weight[i-1][j-Weight[i]]+A[i]);

      }
}
else
{
             DP[i][j]=DP[i-1][j];
             Weight[i][j]=Weight[i-1][j];
}

注意解决方案是微不足道的,除非第一个条件中的第三个条件!
我们需要不惜一切代价最大化乐趣!所以我们不要惹它!
但是当两种情况下的乐趣相同时,我们需要选择重量较轻的那种我们最终会为相同的背包价值增加重量!

我假设你知道背包0/1的问题,这就是为什么我没有解释第一和第二个条件!

标签:java,dynamic-programming,knapsack-problem
来源: https://codeday.me/bug/20190711/1434296.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有