简体   繁体   English

将N个元素划分为k个大小的子集

[英]Dividing N elements into k sized subsets

If I have N different numbers, they are to be divided into k sized subsets such that each subset will have at least one element in common. 如果我有N个不同的数字,则将它们划分为k个大小的子集,以使每个子集具有至少一个相同的元素。 Please help me with the algorithm. 请帮助我的算法。 the output should be some thing like this if I have elements from 1 to 5 and k = 3 then the output should be: 如果我有从1到5的元素并且k = 3则输出应该是这样的,那么输出应该是:

- S0    =   {0, 1, 2}
- S1    =   {1, 3, 5}
- S2    =   {2, 4, 5}
- S3    =   {0, 3, 4}
- S4    =   {1, 4, 6}
- S5    =   {0, 5, 6}
- S6    =   {2, 3, 6}

1) Notice that if k = 1 and |S| > 1 1)注意,如果k = 1|S| > 1 |S| > 1 then this is impossible (ie k = 1 and S = {1,2} ) |S| > 1那么这是不可能的(即k = 1S = {1,2}

2) Otherwise just take the first number, add it always as the first number in each subset, fill in the subsets with remaining numbers. 2)否则,仅取第一个数字,始终将其始终添加为每个子集中的第一个数字,并用剩余数字填充子集。 If there's not enough numbers to fill out the last subset just fill it in with random numbers. 如果没有足够的数字来填写最后一个子集,只需用随机数填写。

As per your example k = 3 and S = {0,1,2,3,4,5,6} we could do: 根据您的示例, k = 3S = {0,1,2,3,4,5,6}我们可以这样做:

  • S0 = {0,1,2} (first number + next 2) S0 = {0,1,2} (第一个数字+下一个2)
  • S1 = {0,3,4} (first number + next 2 after 1,2 ) S1 = {0,3,4} (第一个数字+ 1,2之后的下一个2)
  • S2 = {0,5,6} (in this case we are lucky since we were able to fill out S2, otherwise should it be only for 0 .. 5 we could add a random number as the last one) S2 = {0,5,6} (在这种情况下,我们很幸运,因为我们能够填写S2,否则应该只为0 .. 5我们可以添加一个随机数作为最后一个)

Your example is of 7 points (0,...,6) and 7 "lines" (eg, the "line" {0,1,2}). 您的示例是7点(0,...,6)和7条“线”(例如,“线” {0,1,2})。 Note that any two lines "intersect" in a point, and any two points determine a line. 请注意,任意两条线在一个点“相交”,并且任意两条点确定一条线。 (Pick two numbers from 0,...,6 and you'll see that there's exactly one set with both those numbers.) (从0,...,6中选取两个数字,您会发现这两个数字正好是一组。)

So the design you presented has more restrictions than you described. 因此,您提出的设计比您描述的要受更多的限制。 If you just want each pair of subsets to have at least one element in common, you could put 0 into each set and get 6 C 2 subsets {0,1,2}, {0,1,3}, {0,1,4}, etc., which isn't difficult at all. 如果只希望每对子至少有一个共同的元素,则可以将0放入每个集合中,并获得6个 C 2个子集{0,1,2},{0,1,3},{0,1 ,4}等,这一点都不难。

The geometric language of points and lines isn't a coincidence. 点和线的几何语言不是巧合。 If you want a design where every pair of subsets has exactly one element in common, you probably want a projective plane . 如果要设计每一对子集都具有一个完全相同的元素,则可能需要一个投影平面 The example that you gave is called the Fano plane . 您给出的示例称为Fano平面 Projective planes are not possible for all combinations of N and k. 对于N和k的所有组合,投影平面都是不可能的。 You have to have N = m^2 + m + 1 and k = m + 1 where m is called the order of the plane. 您必须具有N = m ^ 2 + m + 1和k = m + 1,其中m称为平面的 Projective planes are easy to construct by a "vector space construction" for m prime using modular arithmetic (eg, m = 2 gives the Fano plane). 投影平面很容易通过使用模数算法的m个质数的“向量空间构造”来构造(例如m = 2给出Fano平面)。 They are slightly harder to construct for ma power of a prime, m = p^k, using a vector space construction and finite fields. 使用向量空间构造和有限域,对于质数为m = p ^ k的乘方,它们很难构建。 The details can be found in many different references. 可以在许多不同的参考文献中找到详细信息。

For other orders, very little is known, except that through brute force calculations it is known that there are no projective planes of orders 6 and 10. 对于其他阶,除通过蛮力计算得知不存在第6阶和第10阶的投影平面外,鲜为人知。

Is that what you want? 那是你要的吗? If not, there are other "combinatorial block designs" that may have the properties you want, but you have to be precise with your requirements. 如果没有,那么可能会有其他“组合块设计”具有所需的属性,但是您必须精确满足要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算具有n个元素的集合的分区数量为k个子集 - count number of partitions of a set with n elements into k subsets 高效的算法可在n个元素的数组中打印长度为k的所有子集 - Efficient algorithm to print all subsets of length k in an array of n elements 为一组 n 个元素找到 k 个不同元素子集的唯一组合 - Find unique compositions of k-distinct-element subsets for a set of n elements 将K资源公平地划分给N个人 - Dividing K resources fairly to N people 如何将集合划分为K个子集,以使子集中的元素之和最小? - How to divide a set into K subsets such that the sum of the elements in the subsets are minimal? 将数组划分为K个子集,使得使用位掩码+ DP的所有子集的总和相同 - Dividing an array into K subsets such that sum of all subsets is same using bitmasks+DP 计算最多具有k个不同元素的子集的数量 - count number of subsets with atmost k distinct elements 给定一组n个整数,列出所有可能的子集,其中sum&gt; = k - Given a set of n integers, list all possible subsets with sum>=k 给定一组&lt;= k的正整数及其n个子集,请找出哪对子集给出原始集合的并集 - Given a set of positive integers <=k and its n subsets , find union of which pairs of subsets give the original set 字典集的所有组合成 K N 大小的组 - All combinations of set of dictionaries into K N-sized groups
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM