How to get all sections of a list in Mathematica?

By splitting a list, I mean a set of subsets of list items, so that the intersection of any single pair of subsets is empty, and the union of all subsets is equal to the original list.

For example, if my input list is {1,Ο€,x} , then I need a function that returns

 { {{1},{Ο€},{x}}, {{1,Ο€},{x}}, {{1,x},{Ο€}}, {{1},{x,Ο€}}, {{1,Ο€,x}} } 
+8
wolfram-mathematica
source share
2 answers

Using adapted code from http://mathforum.org/advanced/robertd/bell.html

 BellList[1] = {{{1}}}; BellList[n_Integer?Positive] := Join @@ (ReplaceList[#, {{b___, {S__}, a___} :> {b, {S, n}, a}, {S__} :> {S, {n}}} ] & /@ BellList[n - 1]) s = {a, b, c, d, e}; bell = BellList@Length@s /. n_Integer :> s[[n]] 

Or, unsurprisingly, the Combinatorica package has this feature ( SetPartitions ) already!

 Needs["Combinatorica`"] comb = SetPartitions[{a, b, c, d, e}] 

check that both of them return the same result (but in different orders)

 Complement[bell, comb] == {} Sort@bell == Sort@comb (* Both of the above return True *) 
+12
source share

I would start with a Powerset set (use Subsets[x] ), and then filter out those where Union[x] of the set is not source.

A bit slow, but I find it intuitive.

+2
source share

All Articles