I am trying to do the pseudocode for the partition problem below in bruteforce.
a set of integers X and an integer k (k >1). Find k subsets of X such that the numbers in each subset sum to the same amount and no two subsets have an element in common, or conclude that no such k subsets exist. The problem is NP-Complete
For example, with X = {2, 5, 4, 9, 1, 7, 6, 8} and k = 3, a possible solution would be: {2, 5, 7}, {4, 9, 1}, {6, 8} because all of them sum up to 14.
for exhaustive search I know typically we would have to search every possible solution and see if the target is similar. But as this is partition problem this could be tricky.
The algorithm brute force :
Subset= X.sum/K //I had a guess this would make the parition
For int i==1; I <subset; i++ // this would change partition if not found in the first one
If (j=0; I<n; i++)
Sum == s[i]
If sum == target
Display “found”
Else
“not found”
I will start with the comment provided by @m69. Since you know that all elements must be used then you know that the total sum of your partitions will be equal to the total sum of the whole set. Adding in the knowledge that each partition has the same sum then you can determine for
k
partitions each will need to have a sum oftotalSum / k
. This provides a quick way to detect many sets that cannot be partitioned intok
subsets. This code might look something like this:Now it's time to start building some partitions. I recommend using a recursive solution. So we will start with a function that takes an array and the sum that each partition of that array is supposed to have.
There will be two base cases for our recursion. The first is that if the array given has a total sum equal to the partition sum then our partitioning is successful. In this case we will return the array.
The other base case is if the sum of the array is less than the target sum of the partition then this attempt has failed. In this case we return null to indicate that the given partition does not work.
Now to write the recursive step. For this step we want to pick an element from the array and build every partition that sums to the target which includes this number. The largest element in the array is the best element to do this with as it will have the fewest possible partitions. Then for each partition in that set we want to remove the elements in it from the larger array and then run the function again with that smaller array.
This recursive function will return a successful partiton, or if none exists it will return null.
Here is an example in JavaScript that asssumes positive array elements. The algorithm pops the stack and outputs the result if it is valid by checking the count of completed parts; otherwise, it takes each array element in turn and adds another set of parameters to the stack, one where the array element is the first addition to an empty part, and one where it's added in turn to each of the parts yet filled. (For convenience,
result
accrues as a string where the part index precedes each array element.)Output: