简体   繁体   English

解决背包的变体,其中物品的价值取决于麻袋中已有的物品

[英]Solving a variation of the knapsack in which the value of an item depends on items that are already in the sack

I'm trying to solve the following problem: 我正在尝试解决以下问题:
I have a set of N items, where each pair of items have a mutual score, and I need to select a combination of W items such that the overall score is the greatest. 我有一组N项目,其中每对项目都有一个共同得分,我需要选择W项目的组合,以使总得分最高。
The overall score of items i,j,k for example is 例如,项目i,j,k总分是

score(i,j) + score(i,k) + score(j,k).

In order to avoid going through all N^W possible combinations, I thought about doing a variation on the 0-1 knapsack problem and solve with dynamic programing with these 2 changes: 为了避免经历所有N^W可能的组合,我考虑过对0-1背包问题进行变型,并通过动态编程来解决这两个变化:

  1. Set all weights to equal 1 (so eventually I will get exactly W items in my sack) 将所有权重设置为等于1(因此最终我将在麻袋中得到W物品)
  2. Instead of having an array of constant value per item I will calculate the values "on the fly" according to the current item being checked and the items already in the sack at that point. 我将根据要检查的当前物料以及此时已放入袋中的物料来“即时”计算值,而不是每个物料都具有一个恒定值的数组。

I already started coding the solution with these two changes, however now that I think about it more I'm afraid that it can't be solved with dynamic programing since the "optimal substructure" property doesn't hold. 我已经开始用这两个更改对解决方案进行编码,但是现在,我想得更多了,因为“最优子结构”属性不成立,恐怕无法用动态编程来解决。
For instance, if W=3 and items i,j,k is the optimal solution, then for W=2 , i,j is not necessarily an optimal solution (according to the calculation of overall score above). 例如,如果W=3并且项i,j,k是最优解,则对于W=2i,j不一定是最优解(根据上述总分的计算)。
Does anybody have an idea how this problem can be solved with dynamic programing and not with O(N^W) brute force? 有谁知道如何通过动态编程而不是O(N^W)蛮力解决此问题?

Thanks 谢谢

Your problem is NP-hard, which means there is almost certainly no fast polynomial time algorithm to solve it, because no one has been able to come up with a polynomial time algorithm to solve an NP-hard problem. 您的问题是NP难的,这意味着几乎可以肯定没有快速的多项式时间算法可以解决它,因为没有人能够提出多项式时间算法来解决NP难的问题。 To see NP-hardness, suppose you have a graph where nodes are your indices and you define the score between i,j to be 1 if there is an edge between i and j, otherwise 0. Then if you can, in polynomial time, find the maximum score subset of nodes that have at most W nodes included, then you can, in polynomial time, figure out if there is a clique of size W in your graph. 要查看NP硬度,假设您有一个图形,其中节点是您的索引,并且如果i和j之间有边,则将i,j之间的分数定义为1,否则为0。然后,如果可以,在多项式时间内,找到最多包含W个节点的节点的最大分数子集,然后可以在多项式时间内找出图中是否有大小为W的团。 This is an NP-complete problem. 这是一个NP完全问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM