我不得不写一个蛮力实现的背包问题。下面是伪代码:生成列表的功率集
computeMaxProfit(weight_capacity)
max_profit = 0
S = {} // Each element of S is a weight-profit pair.
while true
if the sum of the weights in S <= weight_capacity
if the sum of the profits in S > max_profit
update max_profit
if S contains all items // Then there is no next subset to generate
return max
generate the next subset S
虽然算法是非常容易实现,我没有丝毫的想法如何产生的力量集合S,并且进料将进入的每一次迭代的功率的子集while循环。
我的当前实现使用对列表持有一个项目的质量和利润:
list< pair<int, int> > weight_profit_pair;
我想产生的功率设定这个名单我computeMaxProfit功能。有没有算法来生成列表的子集?列表是否是正确的容器?
谢谢!这有很大的帮助,它真的让我在过去的4个小时中了解了子集的二进制表示。 – 2012-02-13 02:08:02