I'm looking for straightforward way to find all the partitions of a set.
IntegerPartitions
seems to provide a useful start. But then things get a bit complicated.
Imagine we want to find all the ways to partition a list:
myList={a,b,c,d,e,f}
IntegerPartitions
gives the some breakdowns, by numbers of elements in each subset.
breakdowns=IntegerPartitions[Length[myList]]
{{6},{5,1},{4,2},{4,1,1},{3,3},{3,2,1},{3,1,1,1},{2,2,2},{2,2,1,1},{2,1,1,1,1},{1,1,1,1,1,1}}
The following function, g
, takes the list and the breakdowns, outputting some useful results (but clearly not all possible results).
g[{},_,out_]:=out
g[in_,breaks_,out_]:=g[Drop[in,First@breaks],Rest@breaks,Append[out,Take[in,First@breaks]]]
So
g[myList,#,{}]&/@IntegerPartitions[Length[myList]]//MatrixForm
I suspect that there may be some better alternatives to g
. Perhaps even a straightforward list-manipulation command.
BTW, we would need to use all permutations of myList
to ensure we have all the partitions. Permutations[myList]
would be instrumental for that:
Table[g[k,#,{}]&/@IntegerPartitions[Length[myList]],{k,Permutations[myList]}]
SetPartitions[]
not do what you want? $\endgroup$Permutations
at the bottom. $\endgroup$