等k子集算法

发布于 2024-09-25 21:17:29 字数 329 浏览 7 评论 0原文

有谁知道相等 k 子集算法的良好且有效的算法吗?最好是可以处理 100 个元素向量的 c 或 c++,可能具有复杂性和时间估计

。 9 元素向量

x = {2,4,5,6,8,9,11,13,14}

我需要生成所有 k=3 不相交子集,总和 = 24 该算法应该检查是否有 k 个不相交的子集,每个子​​集的元素总和为 24,并按升序列出它们(子集内和子集之间),或者查看解是否不存在

解决方案

解 1:{2 8 14} { 4 9 11} {5 6 13}

解决方案 2:{2 9 13} {4 6 14} {5 8 11}

谢谢

does anyone know a good and efficient algorithm for equal k subsets algorithm ? preferably c or c++ which could handle a 100 element vector maybe with a complexity and time estimation

ex. 9 element vector

x = {2,4,5,6,8,9,11,13,14}

i need to generate all k=3 disjoint subsets with sum = 24
the algorithm should check if there are k disjoint subsets each with sum of elements 24, and list them in ascending order(in subset and between subsets) or to see if the solution doesn't exists

Solutions

solution 1: {2 8 14} {4 9 11} {5 6 13}

solution 2: {2 9 13} {4 6 14} {5 8 11}

Thanks

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

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

发布评论

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

评论(1

ㄖ落Θ余辉 2024-10-02 21:17:29

不幸的是,受限的 k 子集问题是一个难题 ...如果你想生成所有这样的k子集,你别无选择,只能评估许多可能的候选者

您可以执行一些优化来减少搜索空间。

给定一个包含整数值的域x
给定一个正整数目标 M,
给定子集的正整数 k 大小,

  1. 当 x 仅包含正整数,并给定上限 M 时,从 x 中删除大于或等于 M 的所有项目。这些不可能是子集的一部分。
  2. 类似地,对于k> 1、给定M,且x包含正整数,删除x中所有大于M + min0 + min1 ... minK的项。本质上,删除所有不可能属于子集的大值,因为即使选择小值,它们也会导致总和超过 M。
  3. 您还可以使用偶/奇排除原则来减少搜索空间。例如,k 是奇数,M 是偶数,您知道其和将包含​​三个偶数或两个奇数和一个偶数。您可以使用此信息通过消除可能属于总和一部分的 x 候选值来减少搜索空间。
  4. 对向量 x 进行排序 - 这允许您快速排除不可能包含在总和中的值。

当向量 x 包含负值时,许多优化(偶数/奇数排除除外)不再有用/有效。在这种情况下,您几乎必须进行详尽的搜索。

正如 Jilles De Wit 指出的,如果 X 包含负数,您可以将 X 中最小值的绝对值添加到X 的每个成员。这会将所有值移回正范围 - 使我上面描述的一些优化再次成为可能。然而,这要求您能够在扩大的范围内准确地表示正值。实现此目的的一种方法是在内部使用更广泛的类型(例如 long 而不是 int)来执行子集选择搜索。但是,如果您这样做,请记住在返回结果时将结果子集按相同的偏移量缩小。

Unfortunately the constrained k-subset problem is a hard problem ... and if you want to generate all such k-subsets, you have no choice but to evaluate many possible candidates.

There are a couple of optimizations you can perform to reduce the search space.

Given a domain x constaining integer values,
Given a positive integer target M,
Given a positive integer k size for the subset,

  1. When x only contains positive integers, and given a upper bound M, remove all items from x larger than or equal to M. These can't possibly be part of the subset.
  2. Similarly, for k > 1, a given M, and x containing positive integers, remove all items from x which are larger than M + min0 + min1 ... minK. Essentially, remove all of the large values which can't possibly be part of the subset since even when selecting small values they will results in a sum in excess of M.
  3. You can also use the even/odd exclusion principle to pare down your search space. For instance, of k is odd and M is even, you know that the sum will either contain three even numbers or two odd and one even. You can use this information to reduce the search space by eliminating candidate values from x that could be part of the sum.
  4. Sort the vector x - this allows you to rapidly exclude values that can't possibly be included in the sum.

Many of these optimizations (other than the even/odd exclusion) are no longer useful/valid when the vector x contains negative values. In this case, you pretty much have to do an exhaustive search.

As Jilles De Wit points out, if X contains negative numbers you could add the absolute value of the smallest value in X to each member of X. This would shift all values back into positive range - making some of the optimizations I describe above possible again. This requires, however, that you are able to accurately represent positive values in the enlarged range. One way to achieve this would be to internally use a wider type (say long instead of int) to perform the subset selection search. If you do this, however, remember to scale the results subsets back down by this same offset when you return your results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文