如何找到集合A的所有子集组?Python中的集合划分

3
我希望找到一种算法,可以给定一个集合A,找到所有满足以下条件的子集组:

x ∪ y ∪ .... z = A,其中x,y,... z ∈ Group

并且

∀ x,y ∈ Group: x ⊆ A,y ⊆ A,x ∩ y = ∅ = {}

并且

∀ x ∈ Group: x != ∅

注意:我希望定义清楚,我不擅长数学符号。

我制定了以下方法仅搜索两个子集的组:

from itertools import product, combinations

def my_combos(A):
  subsets = []
  for i in xrange(1, len(A)):
    subsets.append(list(combinations(A,i)))
  combos = []
  for i in xrange(1, 1+len(subsets)/2):
    combos.extend(list(product(subsets[i-1], subsets[-i])))
  if not len(A) % 2:
    combos.extend(list(combinations(subsets[len(A)/2-1], 2)))
  return [combo for combo in combos if not set(combo[0]) & set(combo[1])]

my_combos({1,2,3,4})

我得到了以下输出,它们都是由两个子集组成的组。
[
  ((1,), (2, 3, 4)), 
  ((2,), (1, 3, 4)), 
  ((3,), (1, 2, 4)), 
  ((4,), (1, 2, 3)), 
  ((1, 2), (3, 4)), 
  ((1, 3), (2, 4)), 
  ((1, 4), (2, 3))
]

......但是,由一个、三个、四个子集组成的群组....

问题:

我该如何找到一个通解?

例如以下预期输出:

my_combos({1,2,3,4})

[
  ((1,2,3,4)),
  ((1,2,3),(4,)),
  ((1,2,4),(3,)),
  ((1,3,4),(2,)),
  ((2,3,4),(1,)),
  ((1,2),(3,4)),
  ((1,3),(2,4)),
  ((1,4),(2,3)),
  ((1,2),(3,),(4,)),
  ((1,3),(2,),(4,)),
  ((1,4),(2,),(3,)),
  ((1,),(2,),(3,4)),
  ((1,),(3,),(2,4)),
  ((1,),(4,),(2,3)),
  ((1,),(4,),(2,),(3,))
]

请查看 brice 提供的关于幂集的答案。 - Cory Kramer
@Cyber,问题不同。 - Jose Ricardo Bustos M.
@Cyber 回答 brice 解决了一个我已经解决的问题,我的问题不同 :-( - Jose Ricardo Bustos M.
好的,我已经重新开放了这个问题。 - Cory Kramer
也许问题并没有被理解,因为我的英语水平比较低。 - Jose Ricardo Bustos M.
显示剩余4条评论
1个回答

11
解决方案:
def partitions(A):
    if not A:
        yield []
    else:
        a, *R = A
        for partition in partitions(R):
            yield partition + [[a]]
            for i, subset in enumerate(partition):
                yield partition[:i] + [subset + [a]] + partition[i+1:]

解释:

  • 空集只有一个空的分区。
  • 对于一个非空集合,取出一个元素,然后对于剩下元素的每个分区,将该元素作为它自己的子集添加进去,或者将它添加到其中一个分区的子集中。
  • 注意,分区实际上是一组集合。我将它表示为列表的列表,因为这样更快,而且我不想使用不好看的frozensets。元组更快,而且问题要求使用它们,但是我受不了单元素元组中的逗号。

测试输出:

for partition in partitions({1, 2, 3, 4}):
    print(partition)

[[4], [3], [2], [1]]
[[4, 1], [3], [2]]
[[4], [3, 1], [2]]
[[4], [3], [2, 1]]
[[4, 2], [3], [1]]
[[4, 2, 1], [3]]
[[4, 2], [3, 1]]
[[4], [3, 2], [1]]
[[4, 1], [3, 2]]
[[4], [3, 2, 1]]
[[4, 3], [2], [1]]
[[4, 3, 1], [2]]
[[4, 3], [2, 1]]
[[4, 3, 2], [1]]
[[4, 3, 2, 1]]

在一台相对较弱的笔记本电脑上进行速度测试并输出结果:

from time import time
print('elements partitions seconds')
for n in range(14):
    t0 = time()
    number = sum(1 for partition in partitions(range(n)))
    print('{:5}{:10}{:11.2f}'.format(n, number, time() - t0))

elements partitions seconds
    0         1       0.00
    1         1       0.00
    2         2       0.00
    3         5       0.00
    4        15       0.00
    5        52       0.00
    6       203       0.00
    7       877       0.00
    8      4140       0.06
    9     21147       0.07
   10    115975       0.36
   11    678570       2.20
   12   4213597      13.56
   13  27644437      87.59

我通过访问OEIS页面确认了这些分区数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接