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
  • 1
    $\begingroup$ Perhaps Partition[#,2]&/@Permutations@Range@6? $\endgroup$
    – Hausdorff
    Jan 19 at 14:11
  • $\begingroup$ Hi @Hausdorff can you post an answer with the needed code please? $\endgroup$ Jan 19 at 15:02

3 Answers 3

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$ Jan 19 at 14:53
  • $\begingroup$ @GennaroArguzzi For n=8 there are (n-1)!!=7!!=7*5*3*1=105 partitions. $\endgroup$
    – cvgmt
    Jan 19 at 14:55
  • $\begingroup$ yes and also the result8 code. $\endgroup$ Jan 19 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$ Jan 19 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$ Jan 19 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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