Skip to main content

list manipulation - Partition a set into $k$ non-empty subsets


The Stirling number of the second kind is the number of ways to partition a set of $n$ objects into $k$ non-empty subsets. In Mathematica, this is implemented as StirlingS2. How can I enumerate all the sets? Ideally I would like to get a list of lists, where each list contains $k$ lists.


The question Partition a set into subsets of size k seems relevant.




Answer



<< Combinatorica`
KSetPartitions[{a, b, c}, 2]
(*
{{{a}, {b, c}}, {{a, b}, {c}}, {{a, c}, {b}}}
*)

StirlingS2[3, 2]
(* 3 *)

Comments