如何找到集合A的所有子集组?在Python中设置分区

编程入门 行业动态 更新时间:2024-10-22 20:41:04
本文介绍了如何找到集合A的所有子集组?在Python中设置分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

∀ x∈组:x!=∅

注意:我希望很好地定义它,我对数学符号不好p>

我仅采用以下方法来搜索两个子集的组:

从itertools进口产品中组合 def my_combos(A):子集= [] 对于xrange(1,len(A))中的i:子集.append(list(combinations(A,i))) combos = [] for xrange(1,1 + len(subsets / 2))中的i: combos.extend( list(product(subsets [i-1],subsets [-i])))如果不是len(A)%2:连击。 extend(list(combinations(subsets [len(A)/ 2-1],2))) return [combo组合中的组合,如果未设置(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,))]

解决方案

解决方案:

def分区(A):如果不是,则A:收益[] 其他:a,* R = A 用于分区中的分区(R):收益分区+ [[a]] for i,枚举(分区)中的子集:收益分区[:i] + [子集+ [a]] +分区[i + 1:]

说明:

  • 仅空集具有空分区。
  • 对于非空集,请取出一个元素,然后对其余元素的每个分区,将该元素添加为其自己的子集,或将其添加到以下元素之一
  • 请注意,分区实际上是一组集合。我之所以只将其表示为列表列表,是因为这样做速度更快,并且因为我不想使用冻结效果不佳的冻结集。元组更快,问题也要问他们,但我不能忍受单元素元组的逗号。

输出测试:

用于分区中的分区({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]]

对输出进行速度测试(在相对较弱的笔记本电脑上):

从导入时间开始 print('elements partitions seconds') for range in n(14): t0 = time() number = sum(1 for partitions in partitions(range(n))) print( '{:5} {:10} {:11.2f}'。format(n,number,time()-t0)) 元素分区秒 0 1 0.00 1 1 0.00 2 2 0.00 3 5 0.00 4 15 0.00 5 52 0.00 6203 0.00 7 877 0.00 8 4140 0.06 9 21147 0.07 10115975 0.36 11 678570 2.20 12 4213597 13.56 13 27644437 87.59

我用 OEIS页面。

I want to find an algorithm that given a set A to find all groups of subsets that satisfy the following condition:

x ∪ y ∪ .... z = A, where x, y, ... z ∈ Group

and

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

and

∀ x ∈ Group: x != ∅

Note: I hope to define it well, I'm not good with math symbols

I made the following approach to search groups of two subsets only:

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})

I get the following output, these are all groups composed of two subsets

[ ((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)) ]

..... but, groups composed of one, three, four subsets ....

Question:

how may i find a general solution?

for example the following expected output:

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,)) ]

解决方案

Solution:

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:]

Explanation:

  • The empty set only has the empty partition.
  • For a non-empty set, take out one element and then for each partition of the remaining elements, add that element as its own subset or add it to one of the partition's subsets.
  • Note that a partition is really a set of sets. I only represent it as list of lists because that's faster and because I don't want to use frozensets, which don't print nicely. Tuples are faster and the question asked for them, but I can't stand the commas for single-element tuples.

Test with output:

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]]

Speed test with output (on a relatively weak laptop):

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

I confirmed those partition numbers with the OEIS page.

更多推荐

如何找到集合A的所有子集组?在Python中设置分区

本文发布于:2023-11-29 00:38:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子集   分区   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!