1

Is there any function or library that efficiently partitions a set of n elements into non empty sets in Clojure?

For example, there are five ways the numbers {1,2,3} can be partitioned: {{1},{2},{3}}, {{1,2},{3}}, {{1,3},{2}}, {{1},{2,3}}, and {{1,2,3}}

Is there a library for doing so in Clojure or Java, or an efficient algorithm? Deriving the number of these combinations is called a Bell number, if it is of any help.

Dijenek
  • 11
  • 2
  • Are you asking for algorithm for partitioning a set of X elements into Y mostly even sized sets? Or are you asking for code to generate all combinations of a set of X elements? – Andreas Oct 11 '18 at 21:49
  • @Andreas Neither. I am asking for a code which generates all possible nonempty partitions of a set of n elements. You have an example. A set {1,2,3} would result into 5 partitioned sets, {{1} {2} {3}} where the first resulting set has 3 subsets {1}, {2} and {3}, each containing an element from the original set. Then it results in {{1,2},{3}} containing two subsets {1,2} and {3} and so on. – Dijenek Oct 11 '18 at 21:56
  • @Andreas It is not the same. Creating all combinations is not the same as partitioning. I am not asking for (1,2,3) (2,3,1) and so on, which is combination. I am actually asking for the partitions, meaning different subsets within those. – Dijenek Oct 11 '18 at 22:04
  • There is a library in Clojure providing implementation of this operation: https://github.com/clojure/math.combinatorics/. You can use for example `(partitions #{1 2 3})`. – Piotrek Bzdyl Oct 11 '18 at 22:10
  • @PiotrekBzdyl Thank you Piotrek. I found it! – Dijenek Oct 11 '18 at 22:45

0 Answers0