0

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.

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. 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. 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.

My first idea was to

  1. Sort the set by ascending order, check all base cases (e.g. 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!

LuXxenatorX
  • 137
  • 2
  • 9
  • Sorry, but SO is not the homework resolving resource. Come here with your code attempts which have some particular problem – Ivan Pronin May 11 '17 at 19:22
  • 1
    I understand. For anyone who might reads this , here is a ressource that helped: http://www.geeksforgeeks.org/backttracking-set-4-subset-sum/ – LuXxenatorX May 11 '17 at 19:33
  • 1
    This is [Bin packing problem](https://en.wikipedia.org/wiki/Bin_packing_problem). – amit May 11 '17 at 19:59

1 Answers1

0

the given set: S with N elements has 2^N subsets. (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. The total number of partitions of an n-element set is the Bell number Bn.

A solution for this problem can be implemented as follows:

1) create all possible partitions of the set S, called P(S).

2) loop over P(S) and filter out if the sum of the elements values in every subsets do not match.

Avi Chalbani
  • 842
  • 7
  • 11