Introduction
In mathematics, a partition of a set is a grouping of its elements into nonempty subsets, in such a way that every element is included in exactly one subset. The total number of partitions of an nelement set is the Bell number.
For example. Bell number for set of 4 elements is equal 15 and newly created set contains:
[[1] [2] [3] [4]]
[[3 4] [2 1]]
[[3] [4] [2 1]]
[[2 4] [3 1]]
[[2] [4] [3 1]]
[[2 3] [4 1]]
[[2] [3] [4 1]]
[[1] [4] [3 2]]
[[1] [3] [4 2]]
[[1] [2] [4 3]]
[[4] [3 2 1]]
[[3] [4 2 1]]
[[2] [4 3 1]]
[[1] [4 3 2]]
[[4 3 2 1]]
How to calculate?
Calculation is based on recursion which is needed to calculate combinations and new sub sets. Let’s start with combination.


This method returns set of all possible combinations. For every element recursively invoke the same method without previous element and partition number minus 1, then for every element from result create combination with previous element. If this combination doesn’t exist then add it to set.
If we have all possible combinations for given set, then let’s create partitions of set.


That function returns recursively possible connections for given set and its partition number which is from N to 1 where N is maximal size of subset. Then for all possible combinations with given set and partition number counts all possible sub connections and joins them creating uniq subset. Every recurring sub set is filtered out by containsArr function.
In a word of conclusion
This is quite complex algorithm which might be probably tuned up and simplified, so if you see any improvement for that let me know! The first field where I would find improvements is checking if result already contains created sub set. Probably in the future I will back here with new idea but for my current business problem this solution is really sufficient.