I am looking for a nice way of constructing all possible pairings of a given list. Currently I am using the brute force approach of sieving the 2-partitions of all permutations.
Pairings[list_List]:=Block[{unis,ret},
unis=Table[Unique[],Length[list]];
ret=DeleteDuplicates@Map[Sort,Partition[#,{2}]&/@Permutations[unis],2];
ret /. MapThread[Rule,{unis,list}]
];
Examples:
Pairings[{a,b,c,d,e,f}]
{{{a, b}, {c, d}, {e, f}}, {{a, b}, {c, e}, {d, f}}, {{a, b}, {c, f}, {d, e}}, {{a, c}, {b, d}, {e, f}}, {{a, c}, {b, e}, {d, f}}, {{a, c}, {b, f}, {d, e}}, {{a, d}, {b, c}, {e, f}}, {{a, d}, {b, e}, {c, f}}, {{a, d}, {b, f}, {c, e}}, {{a, e}, {b, c}, {d, f}}, {{a, e}, {b, d}, {c, f}}, {{a, e}, {b, f}, {c, d}}, {{a, f}, {b, c}, {d, e}}, {{a, f}, {b, d}, {c, e}}, {{a, f}, {b, e}, {c, d}}}
Pairings[{x,x,x,x}]
{{{x, x}, {x, x}}, {{x, x}, {x, x}}, {{x, x}, {x, x}}}
Although it gives the correct result, I am quite certain that this is not the optimal approach. Maybe there is something like this in the Combinatorica package, which I didn't find?
Tuples? – Oleksandr R. Jan 30 '16 at 21:18