gpt4 book ai didi

r - 将集合的分区枚举为大小相等的子集

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:10:06 25 4
gpt4 key购买 nike

我有一个集合,我想将它分成包含相同数量元素的子集。

我正在寻找一种快速算法,最好不是启发式算法。

提示:

if  n= number of elements in the main set
l= number of elements in each subset

暴力算法是:

1-x <-All combinations of n things taken l  at a time without repetition. |x|=nCl=n!/(l!*(n-l)!)
2-y <-All combinations of x things taken n at a time without repetition. |y|=xCn

3-Select subsets in y such as there is no any overlap within their elements.

答案的数量是:

n!/(l!^(n/l)*(n/l)!)

例如,如果 S={a,b,c,d} 并且如果需要具有 2 个元素的子集来划分集合 S:

集合 x 是:

   (a,b),(a,c),(a,d),(b,c),(b,d),(c,d)

集合 y(潜在答案)是:

{(a,b),(a,c)}
{(a,b),(a,d)}
{(a,b),(b,c)}
{(a,b),(b,d)}
{(a,b),(c,d)}
{(a,c),(a,d)}
{(a,c),(b,c)}
{(a,c),(b,d)}
{(a,c),(c,d)}
{(a,d),(b,c)}
{(a,d),(b,d)}
{(a,d),(c,d)}
{(b,c),(d,d)}
{(b,c),(c,d)}
{(b,d),(c,d)}

正确答案是:

S1={(a,b),(c,d)}
S2={(a,c),(b,d)}
S3={(a,d),(b,c)}

提到的算法只有在n很小时才有用。例如当:

 n=90, l=3 =>
|x|=117480
|y|=1.28827732e+318
and the number of correct answers is `2.533601e+82`.

因此,由于时间性能和内存问题,该算法在大多数情况下并不实用。

即使拥有并运行一个高效的算法也会很耗时,因为结果数量很多。例如在上面的问题中答案的数量 = 2.533601e+82

我不是集合论方面的专家,所以这可能是一个众所周知的问题。

感谢您的帮助。

最佳答案

我认为这可能很接近...希望您使用的是 java

public static void main(String[] args) {
ArrayList<String> tokens = new ArrayList(new TreeSet<>(Arrays.asList(new String[]{"a","b","c","d"})));

for (int i = 0; i < tokens.size(); i++) {
String tokenA = tokens.get(i);
for (int x = (i + 1); x < tokens.size(); x++) {
String tokenB = tokens.get(x);
//String tokenC = tokens.get(x + 1);

System.out.println(tokenA + "-" + tokenB);
}
}
}

我的输出是

run:
a-b
a-c
a-d
b-c
b-d
c-d
BUILD SUCCESSFUL (total time: 0 seconds)

关于r - 将集合的分区枚举为大小相等的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22085019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com