5

我正在寻找一种算法和/或 Python 代码来生成将一组n元素划分为零组或多组r元素和余数的所有可能方式。例如,给定一个集合:

[1,2,3,4,5]

n = 5r = 2我想得到

((1,2,3,4,5),)
((1,2),(3,4,5))
((1,3),(2,4,5))
...
((1,2),(3,4),(5,))
((1,2),(3,5),(4,))
...

换句话说,从集合中提取0组两个项目的结果,加上从集合中提取1组两个项目的结果,再加上从集合中提取2组两个项目的结果,...如果n更大,这将继续。

生成这些结果的顺序并不重要,每个单独组中元素的顺序也不重要,结果中组的顺序也不重要。(例如((1,3),(2,4,5)),等价于((3,1),(4,5,2))((2,5,4),(1,3))等。)我正在寻找的是每个不同的结果至少产生一次,最好是一次,以尽可能有效的方式。


蛮力方法是rn元素中生成所有可能的组合,然后创建任意数量的这些组合(powerset)的所有可能组,迭代它们并仅处理组中的组合在其中没有元素的组合常见的。即使是少量元素也需要很长时间(它需要迭代 2^(n!/r!(nr)!) 个组,因此复杂度是双指数的)

基于这个问题中给出的代码,这基本上是特殊情况r = 2n甚至,我想出了以下内容:

def distinct_combination_groups(iterable, r):
    tpl = tuple(iterable)
    yield (tpl,)
    if len(tpl) > r:
        for c in combinations(tpl, r):
            for g in distinct_combination_groups(set(tpl) - set(c), r):
                yield ((c,) + g)

这似乎确实产生了所有可能的结果,但它包括一些重复,当它们n相当大时,它们中的一个不平凡的数量。所以我希望有一种算法可以避免重复。

4

1 回答 1

9

这个怎么样?

from itertools import combinations

def partitions(s, r):
    """
    Generate partitions of the iterable `s` into subsets of size `r`.

    >>> list(partitions(set(range(4)), 2))
    [((0, 1), (2, 3)), ((0, 2), (1, 3)), ((0, 3), (1, 2))]
    """
    s = set(s)
    assert(len(s) % r == 0)
    if len(s) == 0:
        yield ()
        return
    first = next(iter(s))
    rest = s.difference((first,))
    for c in combinations(rest, r - 1):
        first_subset = (first,) + c
        for p in partitions(rest.difference(c), r):
            yield (first_subset,) + p

def partitions_with_remainder(s, r):
    """
    Generate partitions of the iterable `s` into subsets of size
    `r` plus a remainder.

    >>> list(partitions_with_remainder(range(4), 2))
    [((0, 1, 2, 3),), ((0, 1), (2, 3)), ((0, 2), (1, 3)), ((0, 3), (1, 2))]
    >>> list(partitions_with_remainder(range(3), 2))
    [((0, 1, 2),), ((1, 2), (0,)), ((0, 2), (1,)), ((0, 1), (2,))]
    """
    s = set(s)
    for n in xrange(len(s), -1, -r): # n is size of remainder.
        if n == 0:
            for p in partitions(s, r):
                yield p
        elif n != r:
            for remainder in combinations(s, n):
                for p in partitions(s.difference(remainder), r):
                    yield p + (remainder,)

来自 OP 的示例:

>>> pprint(list(partitions_with_remainder(range(1, 6), 2)))
[((1, 2, 3, 4, 5),),
 ((4, 5), (1, 2, 3)),
 ((3, 5), (1, 2, 4)),
 ((3, 4), (1, 2, 5)),
 ((2, 5), (1, 3, 4)),
 ((2, 4), (1, 3, 5)),
 ((2, 3), (1, 4, 5)),
 ((1, 5), (2, 3, 4)),
 ((1, 4), (2, 3, 5)),
 ((1, 3), (2, 4, 5)),
 ((1, 2), (3, 4, 5)),
 ((2, 3), (4, 5), (1,)),
 ((2, 4), (3, 5), (1,)),
 ((2, 5), (3, 4), (1,)),
 ((1, 3), (4, 5), (2,)),
 ((1, 4), (3, 5), (2,)),
 ((1, 5), (3, 4), (2,)),
 ((1, 2), (4, 5), (3,)),
 ((1, 4), (2, 5), (3,)),
 ((1, 5), (2, 4), (3,)),
 ((1, 2), (3, 5), (4,)),
 ((1, 3), (2, 5), (4,)),
 ((1, 5), (2, 3), (4,)),
 ((1, 2), (3, 4), (5,)),
 ((1, 3), (2, 4), (5,)),
 ((1, 4), (2, 3), (5,))]
于 2013-01-28T17:02:36.483 回答