Function Repository Resource:

KSetPartitions

Source Notebook

Give all possible ways to partition a set into a given number of subsets, ignoring order of blocks and within blocks

Contributed by: Wolfram Staff

ResourceFunction["KSetPartitions"][set,k]

returns the list of set partitions of set with k blocks.

ResourceFunction["KSetPartitions"][n,k]

returns the list of set partitions of {1,2,…, n} with k blocks.

Details and Options

A set partition of a set S is an unordered set of nonempty disjoint subsets of S (called blocks) whose union is S.
Both the order of the blocks and the order within each block are ignored.

Examples

Basic Examples (2) 

There are 15 set partitions of a five-element set into two subsets:

In[1]:=
ksp = ResourceFunction["KSetPartitions"][{a, b, c, d, e}, 2]
Out[1]=

The number of such set partitions is given by a Stirling number of the second kind:

In[2]:=
Length@ksp
Out[2]=
In[3]:=
StirlingS2[5, 2]
Out[3]=

Scope (1) 

Here is a compact way to see the blocks:

In[4]:=
Map[Row, ResourceFunction["KSetPartitions"][6, 3], {2}]
Out[4]=

Publisher

George Beck

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 11 March 2019

Related Resources

License Information