Skip to main content
edited tags
Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Tweeted twitter.com/#!/StackMma/status/226309961624457218
added 39 characters in body
Source Link
DavidC
  • 16.9k
  • 1
  • 43
  • 96

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 

matrix output


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]}] 

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.

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 

matrix output


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]}] 

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 

matrix output


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]}] 
edited tags
Link
more general output
Source Link
DavidC
  • 16.9k
  • 1
  • 43
  • 96
Loading
added 1 characters in body
Source Link
DavidC
  • 16.9k
  • 1
  • 43
  • 96
Loading
permutations of myList; deleted 1 characters in body
Source Link
DavidC
  • 16.9k
  • 1
  • 43
  • 96
Loading
Source Link
DavidC
  • 16.9k
  • 1
  • 43
  • 96
Loading