2

我想找到一组加权元素的所有可能组合,其中它们的权重之和恰好等于给定的权重 W

假设我想从集合中选择 k 个元素{ 'A', 'B', 'C', 'D', 'E' }whereweights = {'A':2, 'B':1, 'C':3, 'D':2, 'E':1}W = 4.

那么这将产生: ('A','B','E') ('A','D') ('B','C') ('B','D','E') ('C','E')

我意识到蛮力的方法是找到给定集合的所有排列(with itertools.permutations)并用 W 的加权和拼接出前 k 个元素。但我正在处理每组至少 20 个元素,这将是计算上的昂贵的。

我认为使用背包的变体会有所帮助,其中只考虑重量(而不是价值)并且重量总和必须等于W(不低于)。

我想在 python 中实现这一点,但任何 cs-theory 提示都会有所帮助。优雅加分!

4

3 回答 3

3

循环遍历所有n!排列太昂贵了。相反,生成所有 2^ n个子集。

from itertools import chain, combinations

def weight(A):
    return sum(weights[x] for x in A)

# Copied from example at http://docs.python.org/library/itertools.html
def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in xrange(len(s) + 1))

[x for x in powerset({'A', 'B', 'C', 'D', 'E'}) if weight(x) == W]

产量

[('A', 'D'), ('C', 'B'), ('C', 'E'), ('A', 'B', 'E'), ('B', 'E', 'D')]

通过将列表推导的返回部分更改为tuple(sorted(x)),或将list调用powerset替换为 ,可以将其转换为已排序的元组sorted

于 2011-11-04T14:11:33.407 回答
1

您对此类集合中的项目数量有上限吗?如果您这样做并且最多约为 40,那么在 Knapsack 上的 Wikipedia 页面中描述的“中间相遇”算法可能非常简单,并且比蛮力计算的复杂性要低得多。

注意:使用比 Python dict 更节省内存的数据结构,这也适用于更大的集合。一个有效的实现应该很容易处理大小为 60 的集合。

这是一个示例实现:

from collections import defaultdict
from itertools import chain, combinations, product

# taken from the docs of the itertools module
def powerset(iterable):
     "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
     s = list(iterable)
     return chain.from_iterable(combinations(s, r) for r in xrange(len(s) + 1))

def gen_sums(weights):
    """Given a set of weights, generate a sum --> subsets mapping.

    For each posible sum, this will create a list of subsets of weights
    with that sum.

    >>> gen_sums({'A':1, 'B':1})
    {0: [()], 1: [('A',), ('B',)], 2: [('A', 'B')]}
    """
    sums = defaultdict(list)
    for weight_items in powerset(weights.items()):
        if not weight_items:
            sums[0].append(())
        else:
            keys, weights = zip(*weight_items)
            sums[sum(weights)].append(keys)
    return dict(sums)

def meet_in_the_middle(weights, target_sum):
    """Find subsets of the given weights with the desired sum.

    This uses a simplified meet-in-the-middle algorithm.

    >>> weights = {'A':2, 'B':1, 'C':3, 'D':2, 'E':1}
    >>> list(meet_in_the_middle(weights, 4))
    [('B', 'E', 'D'), ('A', 'D'), ('A', 'B', 'E'), ('C', 'B'), ('C', 'E')]
    """
    # split weights into two groups
    weights_list = weights.items()
    weights_set1 = dict(weights_list[:len(weights)//2])
    weights_set2 = dict(weights_list[len(weights_set1):])

    # generate sum --> subsets mapping for each group of weights,
    # and sort the groups in descending order
    set1_sums = sorted(gen_sums(set1).items())
    set2_sums = sorted(gen_sums(set2).items(), reverse=True)

    # run over the first sorted list, meanwhile going through the
    # second list and looking for exact matches
    set2_sums = iter(set2_sums)
    try:
        set2_sum, subsets2 = set2_sums.next()
        for set1_sum, subsets1 in set1_sums:
            set2_target_sum = target_sum - set1_sum
            while set2_sum > set2_target_sum:
                set2_sum, subsets2 = set2_sums.next()
            if set2_sum == set2_target_sum:
                for subset1, subset2 in product(subsets1, subsets2):
                    yield subset1 + subset2
    except StopIteration: # done iterating over set2_sums
        pass
于 2011-11-04T15:12:27.770 回答
0

有效地做到这一点(有点)的诀窍是使用前 k 个项目创建具有相同权重的元素集。

从 k=0 处的空集开始,然后使用 k-1 中的组合创建 k 组合。除非您可以有负权重,否则您可以修剪总权重大于 W 的组合。

以下是使用您的示例的结果:

comb[k,w] 是使用前 k 个元素的总权重为 w 的元素集合。
大括号用于集合。
S+e 是通过将元素 e 添加到 S 的每个成员而创建的集合的集合。

comb[0,0]={}
comb[1,0]={comb[0,0]}
comb[1,2]={comb[0,0]+'A'}
comb[2,0]={comb[1,0]}
comb[2,1]={comb[1,0]+'B'}
comb[2,2]={comb[1,2]}
comb[2,3]={comb[1,2]'B'}
comb[3,0]={comb[2,0]}
comb[3,1]={comb[2,1]}
comb[3,2]={comb[2,2]}
comb[3,3]={comb[2,3],comb[2,0]+'C'}
comb[3,4]={comb[2,3]+'C'}
comb[4,0]={comb[3,0]}
comb[4,1]={comb[3,1]}
comb[4,2]={comb[3,2],comb[3,0]+'D'}
comb[4,3]={comb[3,3],comb[3,1]+'D'}
comb[4,4]={comb[3,4],comb[3,2]+'D'}
comb[5,0]={comb[4,0]}
comb[5,1]={comb[4,1],comb[4,0]+'E'}
comb[5,2]={comb[4,2],comb[4,1]+'E'}
comb[5,3]={comb[4,3],comb[4,2]+'E'}
comb[5,4]={comb[4,4],comb[4,3]+'E'}

答案是comb[5,4],它简化为:

{
  {{'B'}+'C'},
  {{'A'}+'D'},
  {
    {{'A'}+'B'},
    {'C'},
    {'B'}+'D'             
  }+'E'
}

给出所有的组合。

于 2011-11-04T14:58:59.003 回答