Wolfram Research

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

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

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

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

In[3]:=
StirlingS2[5, 2]
Out[3]=

Scope

Here is a compact way to see the blocks:

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

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information