简体   繁体   English

将数组划分为具有相同总和值的K个子集

[英]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 我的第一个想法是

  1. Sort the set by ascending order, check all base cases (eg an even distribution is not possible), calculate the average value all subsets have to have so that all subsets are equal. 按升序对集合进行排序,检查所有基本情况(例如,不可能实现均匀分布),计算所有子集必须具有的平均值,以使所有子集相等。
  2. Going through each subset, filling each (starting with the biggest values first) until that average value (meaning theyre full) is achieved. 遍历每个子集,填充每个子集(首先从最大值开始),直到获得平均值(表示它们已满)。
  3. If the average value for a subset can't be met (undistributed values are too big etc.), go back and try another combination for the previous subset . 如果无法满足某个子集的平均值(未分配的值太大等), 请返回并为先前的子集尝试另一种组合
  4. Keep going back if dead ends are encountered. 如果遇到死胡同,请继续前进。
  5. stop if all dead ends have been encountered or a perfect solution was found. 如果遇到所有死角或找到了完美的解决方案,请停止。

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.

相关问题 给定一个数组,找到总和为值k的所有子集 - Given an array, find all subsets which sum to value k 在数组中找到具有相同总和的两个子集 - Find the two subsets with the same sum in an array 回溯并找到等于某个值k的所有数组子集 - Backtracking and finding all subsets of array that equal some value k 将数组分割成k个连续的子数组,使得每个子数组的和的按位AND最大 - partition of array into k contiguous subarrays such that bitwise AND of sum of each subarray is maximum 给定一组n个整数,返回总和为0的k个元素的所有子集 - given a set of n integers, return all subsets of k elements that sum to 0 所有可能的数组子集的乘积之和 - Sum of product of all possible subsets of array 如何查找数组中包含相等总和的子集: - How to find subsets that contains equal sum in an array: 给定一个整数数组和一个总和,任务是找出给定数组的子集是否存在总和等于给定总和的子集 - Given an array of integers and a sum, the task is to find if there exists a subsets of given array with sum equal to given sum 打印出一个递归等于给定和的数组中的所有子集 - Print out all subsets in an array that equal an given sum recursively 递归打印数组中等于给定总和的所有子集 - Print all subsets in an array that equal to a given sum recursively
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM