[英]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
背包问题进行变型,并通过动态编程来解决这两个变化:
W
items in my sack) 将所有权重设置为等于1(因此最终我将在麻袋中得到W
物品) 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=2
, i,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.