# Probability of finding a fit for bin packing

Given that I know the total available space for a set of bins, and the number of bins, I'm trying to determine how likely it is that an item of size $n$ will fit into one of the bins.

As an example, I figured that if I need to fit a size 4 item and I have 4 bins, then as long as the total available space is > 12, at least one bin must also have room for a size 4 item. However, if the total size is 12, the free space in the bins could have the configuration $[3,3,3,3]$, and the item would not fit in any of the bins. So the interesting cases are the ones where the total size is between $n$ (item size) and $(n-1)*b$ where $b$ is the bin count.

I'm trying to determine the probability of success.

I've figured out that the number of ways 12 "spaces" can be distributed across 4 bins is similar to putting $n$ balls into $k$ bins and is given by $$\binom{n+k-1}{k-1} .$$

So in the above example, this means the possible configurations for a total size of 12 and 4 bins is $$\binom{15}{3}.$$ which yields 455 possible configurations. Of these, clearly only one is incompatible (3 slots in each bin), so the probability of failure is $1/455$, and thus success is $454/455$.

EDIT: I realized that the bins need to have max sizes as well, so for example if the free space is 12, but the max bin size is 10, then the $[12,0,0,0]$ solution is not possible and must not be counted. This means I can use the same approach for finding the possible distributions as the failing ones, only with different max bin sizes.

And this is where I'm completely stuck, I'm not able to generalize this. When the total size is 11 for example, there are more than one case which "fails", namely $[2,3,3,3]$, $[3,2,3,3]$,$[3,3,2,3]$ and $[3,3,3,2]$.

I think that to count the "failing" cases I'm asking how many ways you can distribute $n$ balls into $k$ bins, given that no bin can exceed a max size $m$, where the $m$ is my item size - 1.

This seems to be answered in Number of ways to put $n$ unlabeled balls in $k$ bins with a max of $m$ balls in each bin, but I can't figure what to plug into the $r$ and $r2$ for the last formula there.

Also, there might be some much easier way of figuring out the probability :)

-
You're assuming that the bins fill completely the space correct? – Jeremy Oct 10 '12 at 20:18
@Jeremy: Yes, the total space is the sum of free space in the bins. – Hisnessness Oct 10 '12 at 20:26

If you have $n$ space and $k$ bins into which you wish to place an object of size $j$, you need to determine the number of ways in which there is at least one box of size at least $j$.
For the "at least one box of size $j$" portion, we have $$\binom{k+n-j-2}{n-j-1}$$ since we are holding one box of size $j$ fixed and so finding the number of solutions to $x_1+x_2+ \cdots +x_{k-1} = n-j$.
And to find the number of ways to have a box of size at least $j$, you could sum up $$\sum_{i=0}^{n-j}{\binom{n-(j-i)+k-2}{n-(j-i)-1}}$$ but this would be overcounting and you'd have to take out the duplicates.