算法:将一组元素分成两组的所有可能方法?

发布于 2024-11-29 02:36:18 字数 643 浏览 1 评论 0原文

我在集合 U 中有 n 个元素(假设由大小为 n 的数组表示)。我想找到将集合 U 分为两个集合 A 和 B 的所有可能方法,其中 |A| + |B| = n.

例如,如果 U = {a,b,c,d},则组合将为:

  1. A = {a} -- B = {b,c,d}
  2. A = {b} -- B = {a, c,d}
  3. A = {c} -- B = {a,b,d}
  4. A = {d} -- B = {a,b,c}
  5. A = {a,b} -- B = {c, d}
  6. A = {a,c} -- B = {b,d}
  7. A = {a,d} -- B = {b,c}

请注意,以下两种情况被视为相等,只应计算其中一种:

情况 1:A = {a,b} -- B = {c,d}

情况2: A = {c,d} -- B = {a,b}

另请注意,集合 A 或 B 都不能为空。

我考虑实现它的方法是仅跟踪数组中的索引并逐步移动它们。索引的数量将等于集合 A 中的元素数量,集合 B 将包含所有剩余的未索引元素。

我想知道是否有人知道更好的实现。我正在寻求更高的效率,因为此代码将在相当大的数据集上执行。

谢谢!

I have n elements in a set U (lets assume represented by an array of size n). I want to find all possible ways of dividing the set U into two sets A and B, where |A| + |B| = n.

So for example, if U = {a,b,c,d}, the combinations would be:

  1. A = {a} -- B = {b,c,d}
  2. A = {b} -- B = {a,c,d}
  3. A = {c} -- B = {a,b,d}
  4. A = {d} -- B = {a,b,c}
  5. A = {a,b} -- B = {c,d}
  6. A = {a,c} -- B = {b,d}
  7. A = {a,d} -- B = {b,c}

Note that the following two cases are considered equal and only one should be computed:

Case 1: A = {a,b} -- B = {c,d}

Case 2: A = {c,d} -- B = {a,b}

Also note that none of the sets A or B can be empty.

The way I'm thinking of implementing it is by just keeping track of indices in the array and moving them step by step. The number of indices will be equal to the number of elements in the set A, and set B will contain all the remaining un-indexed elements.

I was wondering if anyone knew of a better implementation. Im looking for better efficiency because this code will be executed on a fairly large set of data.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-12-06 02:36:19

取 1 到 2^(n-1) 之间的所有整数(不包括 1 到 2^(n-1))。因此,如果 n = 4,则为从 1 到 7 的整数。

这些数字中的每一个以二进制形式表示,代表集合 A 中存在的元素。集合 B 由其余元素组成。请注意,由于我们只需要 2^(n-1),而不是 2^n,因此始终为集合 B 设置高位;我们总是将第一个元素放入集合 B 中,因为您希望顺序无关紧要。

Take all the integers from 1 to 2^(n-1), non-inclusive. So if n = 4, the integers from 1 to 7.

Each of these numbers, written in binary, represents the elements present in set A. Set B consists of the remaining elements. Note that since we're only going to 2^(n-1), not 2^n, the high bit is always set for set B; we're always putting the first element in set B, since you want order not to matter.

御守 2024-12-06 02:36:19

如果它有助于澄清 dfan 的方法在做什么,这里是 1-7 的整数、整数的位字符串以及用于拆分 my_list=['a', 'b', 'c', 'd 的相应组']。位串中的 3 个字符对应于 my_list 的索引 1、2 和 3。如果该位为 1,则该元素进入第一个列表。索引 0 ('a') 处的元素始终位于第二组中。

1 001 ['d'] ['a', 'b', 'c']
2 010 ['c'] ['a', 'b', 'd']
3 011 ['c', 'd'] ['a', 'b']
4 100 ['b'] ['a', 'c', 'd']
5 101 ['b', 'd'] ['a', 'c']
6 110 ['b', 'c'] ['a', 'd']
7 111 ['b', 'c', 'd'] ['a']

In case it helps clarify what dfan's method is doing, here are the integers from 1-7, the bit strings of the integers, and the corresponding groups for splitting my_list=['a', 'b', 'c', 'd']. The 3 characters in the bit string correspond to indices 1, 2, and 3 of my_list. If the bit is 1 that element goes into the first list. The element at index 0 ('a'), always goes in the second group.

1 001 ['d'] ['a', 'b', 'c']
2 010 ['c'] ['a', 'b', 'd']
3 011 ['c', 'd'] ['a', 'b']
4 100 ['b'] ['a', 'c', 'd']
5 101 ['b', 'd'] ['a', 'c']
6 110 ['b', 'c'] ['a', 'd']
7 111 ['b', 'c', 'd'] ['a']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文