3
$\begingroup$

I'd like to find all the partitions (each subset of a partition should contain 2 elements) of a set composed by an even number of elements. For example, given $A=\lbrace 1,2,3,4,5,6 \rbrace$, I'd like to see the partitions:

$$ \lbrace \lbrace 1,2 \rbrace, \lbrace 3,4 \rbrace, \lbrace 5,6 \rbrace \rbrace \\ \lbrace \lbrace 1,3 \rbrace, \lbrace 2,4 \rbrace, \lbrace 5,6 \rbrace \rbrace \\ \ldots $$

etc.

$\endgroup$
2
  • 2
    $\begingroup$ Perhaps Partition[#,2]&/@Permutations@Range@6? $\endgroup$
    – Hausdorff
    Commented Jan 19, 2022 at 14:11
  • $\begingroup$ Hi @Hausdorff can you post an answer with the needed code please? $\endgroup$ Commented Jan 19, 2022 at 15:02

4 Answers 4

2
$\begingroup$

Too complex ,but work.

result6 = 
 Sort /@ (Map[
      Sort] /@ (Partition[#, 2] & /@ Permutations[Range[6]])) // 
  DeleteDuplicates
result6//Length
Grid[result6, Dividers -> {False, All}]

15

enter image description here

result8 = 
  Sort /@ (Map[
       Sort] /@ (Partition[#, 2] & /@ Permutations[Range[8]])) // 
   DeleteDuplicates;
reslut8//Length
Grid[Partition[result8, 3], Dividers -> {All, All}]

105

For general n=2k,the answer should be (n-1)!!,but I don't know how to list it in a simple way.

Table[(2 k - 1)!!, {k, 1, 5}]

{1, 3, 15, 105, 945}

$\endgroup$
3
  • $\begingroup$ Hello @cvgmt thank you very much for your helpful answer. The second part of the code is about which case? $\endgroup$ Commented Jan 19, 2022 at 14:53
  • $\begingroup$ @GennaroArguzzi For n=8 there are (n-1)!!=7!!=7*5*3*1=105 partitions. $\endgroup$
    – cvgmt
    Commented Jan 19, 2022 at 14:55
  • $\begingroup$ yes and also the result8 code. $\endgroup$ Commented Jan 19, 2022 at 14:55
4
$\begingroup$
ClearAll[addPair, pairPartitions]

addPair[n_][{parts : {_, _} ..}] := Append[{parts}, #] & /@ 
  Subsets[Complement[Range @ n, parts], {2}]

pairPartitions[n_] := DeleteDuplicatesBy[Sort] @
  Nest[Catenate @* Map[addPair[n]], List /@ Thread[{1, Range[2, n]}], n/2 - 1]

Examples:

pairPartitions[6] // Grid[#, Dividers -> {None, All}]&

enter image description here

Length @ pairPartitions[#] & /@ {2, 4, 6, 8, 10, 12}
{1, 3, 15, 105, 945, 10395}
$\endgroup$
1
  • $\begingroup$ perfect, very nice! $\endgroup$ Commented Jan 19, 2022 at 15:15
2
$\begingroup$
ClearAll[twoPartitions]
twoPartitions[n_] := Select[Union@@# == Range[n]&]@Fold[Subsets, Range@n, {{2}, {n/2}}]

Examples:

twoPartitions[6] // Grid[#, Dividers -> {None, All}]&

enter image description here

Length @ twoPartitions[#] & /@ {2, 4, 6, 8, 10}
 {1, 3, 15, 105, 945}
$\endgroup$
1
  • $\begingroup$ Ciao @kglr can you organize the result in a table as in the cvgmt answer please? $\endgroup$ Commented Jan 19, 2022 at 15:10
1
$\begingroup$

Something much more efficient with a brain teasing code.

Clear[pairs]
pairs[0] = {{}};
pairs[n_] := 
 Flatten[(d |-> 
     Prepend[Partition[Complement[Range[n], d][[Flatten@#]], 2], 
        d] & /@ pairs[n - 2]) /@ ({1, #} & /@ Rest@Range[n]), 1]

Timings:

Table[Length[pairs[n]] // Timing, {n, 0, 14, 2}] // Column

{0.,1}
{0.,1}
{0.,3}
{0.,15}
{0.015625,105}
{0.046875,945}
{0.625,10395}
{8.53125,135135}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.