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$
    – user49048
    Feb 25, 2022 at 22:31
  • 1
    $\begingroup$ Is your problem similar or related to 263459 ? $\endgroup$
    – Syed
    Feb 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$
    – lericr
    Feb 25, 2022 at 22:46

1 Answer 1


You can use the resource function KSetPartitions as follows:

kSP = ResourceFunction["KSetPartitions"]; 

orderedPartitions[n_, k_] := PadLeft[
   Join @@ (kSP[Range[n], #] & /@ Range[k]), 
   {Automatic, k}, {{{}}}]


orderedPartitions[5, 2] // Column

enter image description here

orderedPartitions[4, 3] // Column

enter image description here

  • $\begingroup$ nice! I need to play with resource functions more. $\endgroup$
    – lericr
    Feb 25, 2022 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.