# Is there a Mathematica function that generates all ordered partitions?

My book defines a length $$k$$ ordered partition of $$I_n$$ as a sequence of $$k$$ disjoint, possibly empty subsets of $$\{1,\dots, n\}$$ that union up to $$\{1,\dots, n\}$$. Is there a mathematica function that generates all length $$k$$ ordered partitions of $$I_n$$?

• Welcome. Can I just ask the following: to which book are you referring? Can you give a link? Can you give an MWE (minimal working example) not in code necessarily, even in LaTeX just so we can see with what sort of lists we are dealing here and what the expected outcome is. Many thanks!
– kcr
Feb 25 at 22:31
• Is your problem similar or related to 263459 ?
– Syed
Feb 25 at 22:34
• I think you're going to need to create your own function, and I think for anything but tiny numbers for n, you're going to be generating large outputs. Having said that, you can get started by looking at IntegerPartitions[n,{k}]. This will sort of show the the "shape" of the problem you're dealing with. It ignores the empty subset issue, so that's a problem. And you'll need to do further processing to get actual subsets. I wrote up a function that will produce the subsets, but it doesn't completely answer your question. I can post if you're interested, but there will be work left to do. Feb 25 at 22:46

You can use the resource function KSetPartitions as follows:

kSP = ResourceFunction["KSetPartitions"];

Join @@ (kSP[Range[n], #] & /@ Range[k]),
{Automatic, k}, {{{}}}]


Examples:

orderedPartitions[5, 2] // Column


orderedPartitions[4, 3] // Column


• nice! I need to play with resource functions more. Feb 25 at 23:58