Edits: In fact it is a set partition problem.
I have a set as follows:
s = {"a", "b", "c", "d", "e", "f"}
I want to divide the set into 2 groups such that each group has exactly $3$ elements. We want to generate all solutions. Note that we treat ({"a", "b", "c"}|{"d", "e", "f"}) and ({"d", "e", "f"}|{"a", "b", "c"}) as the same solution.
So first I used the Subsets
function to generate all 3-subsets and then filtered for duplicate solutions.
s3 = Subsets[s, {3}]
DeleteDuplicates[s3, Union[#1] == Union[Complement[s, #2]] &]
Out[*]: {{"a", "b", "c"}, {"a", "b", "d"}, {"a", "b", "e"}, {"a", "b",
"f"}, {"a", "c", "d"}, {"a", "c", "e"}, {"a", "c", "f"}, {"a", "d",
"e"}, {"a", "d", "f"}, {"a", "e", "f"}}
But I feel like the solution is a bit inefficient. In fact, we only need half of all 3-subsets. I wonder if we can only keep one representative among same solutions when generating the 3-subsets.
Edits: In fact, the problem can be generalized as follows: Given a list of $n$ elements, partition it into $m$ groups, and generate all possible solutions. Similarly, same solutions are generated only once.
Subsets
provides them:Subsets[s, {Length[s]/2}, Binomial[Length[s], Length[s]/2]/2]
. $\endgroup$First /@ Gather[s3, DisjointQ]
? $\endgroup$