Re: Finding all n-partitions of a set

*To*: mathgroup at smc.vnet.net*Subject*: [mg98745] Re: [mg98707] Finding all n-partitions of a set*From*: Leonid Shifrin <lshifr at gmail.com>*Date*: Fri, 17 Apr 2009 04:30:41 -0400 (EDT)*References*: <200904160818.EAA17706@smc.vnet.net>

Hi Joe, You can load the DiscreteMath`Combinatorica` package (or, just Combinatorica`, in v.6.0+). In[1] = <<DiscreteMath`Combinatorica` It has the KSetPartitions function. Then, the following does what you want: In[2] = Union@Map[Sort, KSetPartitions[{a, b, a, b}, 2], 2] Out[2] = {{{a}, {a, b, b}}, {{b}, {a, a, b}}, {{a, a}, {b, b}}, {{a, b}, {a, b}}} Map[Sort, ...] is used to bring the partitions which are different only in the order of their elements, to the same form, so that duplicates can be eliminated by Union. Note that we Map Sort on both level 1 and level 2, and this does what we need because Map maps on several levels in a depth-first manner. Regards, Leonid On Thu, Apr 16, 2009 at 1:18 AM, Joe.Mapasapam <mapasapam at gmail.com> wrote: > Please how can I find all n-partions of a set ? > > > Say, i want partitions of the set (no order) {a,a,b,b} into 2, > > so we have > { > {{a,a,b,},{b}}, > {{a,b,b},{a}}, > {{a,a},{b,b}}, > {{a,b},{a,b}} > } > > is there already a built in function in mathematica ? > > i need so n can be any number > >

**References**:**Finding all n-partitions of a set***From:*"Joe.Mapasapam" <mapasapam@gmail.com>