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$?
-
$\begingroup$ 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! $\endgroup$– user49048Feb 25, 2022 at 22:31
-
1$\begingroup$ Is your problem similar or related to 263459 ? $\endgroup$– SyedFeb 25, 2022 at 22:34
-
$\begingroup$ 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. $\endgroup$– lericrFeb 25, 2022 at 22:46
1 Answer
You can use the resource function KSetPartitions as follows:
kSP = ResourceFunction["KSetPartitions"];
orderedPartitions[n_, k_] := PadLeft[
Join @@ (kSP[Range[n], #] & /@ Range[k]),
{Automatic, k}, {{{}}}]
Examples:
orderedPartitions[5, 2] // Column
orderedPartitions[4, 3] // Column
-
$\begingroup$ nice! I need to play with resource functions more. $\endgroup$– lericrFeb 25, 2022 at 23:58