[英]Partition array into K subsets of same sum value
trying to figure out following problem: 试图找出以下问题:
Given a set S of N positive integers the task is to divide them into K subsets such that the sum of the elements values in every of the K subsets is equal.
给定一组S个N个正整数,任务是将它们分为K个子集,以使K个子集中的每个子集的元素值之和相等。
I want to do this with a set of values not more than 10 integers, with values not bigger than 10 , and less than 5 subsets. 我想使用一组不超过10个整数,不大于10个且小于5个子集的值来做到这一点。 All integers need to be distributed, and only perfect solutions (meaning all subsets are equal, no approximations) are accepted.
所有整数都需要分布,并且只接受完美解(意味着所有子集相等,没有近似值)。 I want to solve it recursively using backtracking .
我想使用backtracking递归解决它。 Most ressources I found online were using other approaches I did not understand, using bitmasks or something, or only being for two subsets rather than K subsets.
我在网上找到的大多数资源都在使用我不了解的其他方法,使用位掩码或其他方法,或者仅用于两个子集而不是K个子集。
My first idea was to 我的第一个想法是
Unfortunately I am really struggling with this, especially with implementing the backtrack and retrying new combinations. 不幸的是,我确实为此感到挣扎,尤其是在实施回溯和重试新组合时。
Any help is appreciated! 任何帮助表示赞赏!
the given set: S with N elements has 2^N subsets. 给定的集合:具有N个元素的S具有2 ^ N个子集。 (well explained here: https://www.mathsisfun.com/activity/subsets.html ) A partition is is a grouping of the set's elements into non-empty subsets, in such a way that every element is included in one and only one of the subsets.
(在此处进行了很好的解释: https : //www.mathsisfun.com/activity/subsets.html )分区是将集合的元素分组为非空子集的方式,每个元素都包含在一个且仅一个元素中子集之一。 The total number of partitions of an n-element set is the Bell number Bn.
一个n元素集的分区总数为贝尔数Bn。
A solution for this problem can be implemented as follows: 可以按以下方式实现此问题的解决方案:
1) create all possible partitions of the set S, called P(S). 1)创建集合S的所有可能的分区 ,称为P(S)。
2) loop over P(S) and filter out if the sum of the elements values in every subsets do not match. 2)遍历P(S)并过滤掉每个子集中的元素值之和是否不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.