# How many ways to distribute n distinct objects into r groups when only 1 group is allowed to be empty?

This one is hurting my brain. How many ways are there to distribute n distinct objects into r distinct groups of arbitrary sizes, when only one particular group is allowed (but not required) to be empty? Or, in other words, how many ways are there to spread n distinct objects across a number of labelled groups when you can put several objects into one group and don't have to use all objects?

I'm writing an algorithm where several objects have to be distributed over a number of baskets. If there are some baskets and a 0th basket to store the remainder, then it's easy to calculate the number of possible distributions when only the remainder can be an aggregate of multiple objects. Or, to put it differently, the number of possibilities of distributing the objects without using all of them when each basket can only store 1 object.

For example, if n = 7 and r = 3, then there are 7P3 = 210 ways to distribute the objects so that there is 1 object in each basket and 7-3=4 unused objects remaining in the 0th basket. However, I now need to know how to do the same thing when all baskets are allowed to contain multiple objects and require at least 1, where it is irrelevant if basket #0 (the remainder) contains objects or not.

Cheers.

• The special basket is I think not the main difficulty, it is the others non-empty. Assume all baskets must be non-empty. Then for the number of functions we can use Inclusion/Exclusion. Whatever we do we bump into Stirling Numbers of the Second Kind (please see Wikipedia). No closed form, but there are useful recurrences. – André Nicolas Sep 17 '15 at 13:57
• Right. It would require correcting for the fact that Stirling Numbers of the Second Kind assume unlabelled groups, no? – JGTP Sep 17 '15 at 14:57
• If we are dumping into $k$ labelled groups, multiply the Stirling number by $k!$. – André Nicolas Sep 17 '15 at 15:07
• So, k! S2(n,k)+(k-1)! S2(n,k-1) then? Combinatronics isn't really my thing, as you can tell. – JGTP Sep 18 '15 at 12:53
• Yes, the onto functions plus the ones that leave the special basket empty, and there are $(k-1)S(n,k-1)$ of these. Would you like to write it up as an answer? If I am informed you have done it, and you have trouble with the LaTeX, I can do the formatting. – André Nicolas Sep 18 '15 at 13:17

For a number of objects $n$ and a number of labelled clusters $k$, the number of distributions $N$ equals: $$N(n,k) = k! \cdot S_2(n,k) + (k-1)! \cdot S_2(n,k-1)$$ where $S_2(n,k)$ is the Stirling Number of the Second Kind.