Suppose that $A$ is a list with $n$ elements where $n$ is even. I want to write a function that returns all pairs $(A_1,A_2)$ where the sets $A_1$ and $A_2$ each have length $\frac{n}{2}$, $A_1 \cap A_2=\emptyset$, and $A_1,A_2 \subset A$.
Answer
I'm assuming in A_1 A_2 pair, order matters:
set = Range[6];
Transpose[{#, Reverse@#}] & @ Subsets[#, {Length[#]/2}] & @ set
{{{1, 2, 3}, {4, 5, 6}}, {{1, 2, 4}, {3, 5, 6}}, {{1, 2, 5}, {3, 4,
6}}, {{1, 2, 6}, {3, 4, 5}}, {{1, 3, 4}, {2, 5, 6}}, {{1, 3,
5}, {2, 4, 6}}, {{1, 3, 6}, {2, 4, 5}}, {{1, 4, 5}, {2, 3,
6}}, {{1, 4, 6}, {2, 3, 5}}, {{1, 5, 6}, {2, 3, 4}}, {{2, 3,
4}, {1, 5, 6}}, {{2, 3, 5}, {1, 4, 6}}, {{2, 3, 6}, {1, 4,
5}}, {{2, 4, 5}, {1, 3, 6}}, {{2, 4, 6}, {1, 3, 5}}, {{2, 5,
6}, {1, 3, 4}}, {{3, 4, 5}, {1, 2, 6}}, {{3, 4, 6}, {1, 2,
5}}, {{3, 5, 6}, {1, 2, 4}}, {{4, 5, 6}, {1, 2, 3}}}
If pair isn't ordered you can take halof of above or spare some memory doing:
With[{l = Length[#]},
Transpose[{#, Reverse@#2}] & @@
Partition[Subsets[#, {l/2}], Binomial[l, l/2]/2]] &@set
Comments
Post a Comment