【发布时间】:2012-08-20 11:10:56
【问题描述】:
我正在尝试使用非整数值减少 Knapsack DP 算法所需的时间和空间。
http://en.wikipedia.org/wiki/Knapsack_problem#Meet-in-the-Middle_Algorithm
In particular, if the [elements] are nonnegative but not integers, we could
still use the dynamic programming algorithm by scaling and rounding (i.e. using
fixed-point arithmetic), but if the problem requires fractional digits of
precision to arrive at the correct answer, W will need to be scaled by 10^d,
and the DP algorithm will require O(W * 10^d) space and O(nW * 10^d) time.
DP 背包算法使用 [ n x W ] 矩阵,用结果填充它,但有些列永远不会被填充 - 它们不匹配对象权重的任何组合。这样一来,它们最终只会在每一行上填充零,而且只是浪费时间和空间。
如果我们使用哈希数组而不是矩阵,我们可以减少所需的时间和空间。
edit:
knapsack capacity = 2
items: [{weight:2,value:3} ]
[0 1 2]
[0 0 0]
2: [0 0 3]
^
Do we need this column?
Substitution with hash:
2: {0:0, 2:3}
在 Python 中,dict 插入有 O(n) 更坏的情况和 O(1) 的“摊销”线性时间。
我错过了什么吗?
背包 DP 算法的这种变体的复杂性是多少?
【问题讨论】:
-
我不关注。你试图如何改变它?你的字典的键/值是什么?
-
哈希将替换矩阵的“行”。键是权重,值是子集总和
-
您到底想保存什么? “行”仍然需要相同数量的元素(权重)。如果您对此有不同的想法 - 我们很乐意听到。
-
这是一个例子。背包容量 2,物品:{重量:2,价值:3}。解决方案是微不足道的,取唯一的项目。我们真的需要代表权重“1”的列吗?
-
为了降低复杂性,与总行数相比,“需要”行数必须有一个上限。但是,上限等于总行数 - 可能需要所有行,因此这显然不会降低复杂性。在一般情况下,我们必须假设大多数行都已填充,例如:容量 1000, items = {2, 3}
标签: algorithm optimization dynamic-programming knapsack-problem decimal