Re: Help on Partitions, Again!!!

• To: mathgroup at smc.vnet.net
• Subject: [mg24645] Re: [mg24636] Help on Partitions, Again!!!
• From: "Carl K. Woll" <carlw at u.washington.edu>
• Date: Mon, 31 Jul 2000 09:23:17 -0400 (EDT)
• References: <200007282124.RAA02104@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Jose,

Here's one idea.

Clear[allpartitions, partitions, subsetsplit]

partitions[a_List, spec_] := Union[Sort /@ allpartitions[a, spec]]

allpartitions[a_List, {n_}] := {{a}}
allpartitions[a_List, spec_] := Module[{},
tmp = subsetsplit[a, spec[[1]]];
Flatten[
Outer[Prepend, allpartitions[#[[2]], Rest[spec]], {#[[1]]}, 1] &
/@ tmp,
2]]

subsetsplit[a_, n_] := Module[{len = Length[a]},
i[0] = 0;
Flatten[
Table[
h[a[[Table[i[k], {k, n}]]], Delete[a, Table[{i[k]}, {k, n}]]],
Evaluate[Sequence @@ Table[{i[k], i[k - 1] + 1, len - n + k},
{k, n}]]
],
n - 1]]

Basically, subsetsplit forms h[a,b], where a is a subset of a certain
length, and b is the remaining elements from the list allpairings. The
function subsetsplit is similar in functionality to the function
KSubsets, except that subsetsplit also includes the expression b.

The function allpairings begins by calling subsetsplit to form all
possible h[a,b], and then applying allpairings to b, and then hooking up
a to each of the sets formed by applying allpairings to b. The function
allpairings will treat {{i1,i2},{i3,i4}} as different from
{{i3,i4},{i1,i2}}, so the final function pairings removes all these
duplicates.

For your test case we have

partitions[{i1, i2, i3, i4, i5, i6}, {3, 3}]

{{{i1, i2, i3}, {i4, i5, i6}},

{{i1, i2, i4}, {i3, i5, i6}},

{{i1, i2, i5}, {i3, i4, i6}},

{{i1, i2, i6}, {i3, i4, i5}},

{{i1, i3, i4}, {i2, i5, i6}},

{{i1, i3, i5}, {i2, i4, i6}},

{{i1, i3, i6}, {i2, i4, i5}},

{{i1, i4, i5}, {i2, i3, i6}},

{{i1, i4, i6}, {i2, i3, i5}},

{{i1, i5, i6}, {i2, i3, i4}}}

Carl

> Hello, MathGroup
> First of all, thanks for your attention.
> To be more specific:
> It's not too dificult to calculate the solution of the problem:
> How many ways, can the set {A,B,C,D,E,F} be separeted into two parts
> with three elements in each?
>  Answer:   x = 6!/(2!.3!.3!) = 10
>  I'm looking for a function to generate all the partitions using
> Mathematica 3.0 .
> I'm not sure, but I think the package Combinatorica doesn't have a
> function to do this.
> For example, I'm trying to think up a function f  like this one:
>
> In[ ] = f [ {A,B,C,D,E,F},{3,3}]
> Out [ ] = { { {A,B,C},{D,E,F} }, { {
> A,B,F},{C,D,E}},...................} and so on.
> In [ ] = Length[%]
> Out [ ] = 10
>