You are on page 1of 12

MATH 261 Enumeration 4.

4 Balls and boxes

In this chapter we are going to discuss a very helpful family of problems.

Problem. Suppose we have n balls, and k boxes. How many dierent ways
are there of distributing the balls into the boxes? }

Problem 2 comes in four varieties, depending on whether or not the balls

and boxes are distinguishable. Imagine that if the balls are distinguishable,
then they are numbered 1 to n, but if they are indistinguishable, then they
have exactly the same appearance. The same comment applies to the boxes.

Example. Suppose that we have five distinguishable balls (numbered 1 to

5), and two distinguishable boxes (numbered 1 and 2). Then these two
distributions of balls in boxes are dierent.

1 3 5 2 4 1 2 5 3 4

Box 1 Box 2 Box 1 Box 2

Notice, however, that if the balls were indistinguishable, then these distri-
butions would be identical: in both distributions, Box 1 gets three balls,
and Box 2 get two. Similarly, these two distributions are distinct:

1 3 5 2 4 2 4 1 3 5

But if the boxes were indistinguishable, then these distributions would be

the same. }

We sometimes further divide the four variants of the balls and boxes
problem by saying that each box must contain at least one ball. In addition,
we might insist that each box must contain no more than one ball, but the
answers in this case tend not to be very interesting. Thus there are twelve
potential subproblems, so this classification of problems is sometimes called
the Twelvefold Way.

25
MATH 261 Enumeration 4. Balls and boxes

Indistinguishable balls, distinguishable boxes.

Proposition 4.1. Suppose that we have n indistinguishable balls, and k
distinguishable boxes. The number of dierent ways of distributing the balls
into the boxes is
n+k 1
.
n
Proof. As in the proof of Proposition 2.6, we set up a correspondence be-
tween distributions of balls and strings of the form

| || |

In this case each of the symbols represents a ball, and the | symbols are
separators between consecutive boxes. So the string above represents the
distribution where Box 1 gets three balls, Box 2 gets one, Box 3 gets none,
Box 4 gets two, and Box 5 gets one.
Now we just need to count the number of such strings. Each string
contains n + k 1 characters (n to represent the n balls, and k 1 to
represent the separators between the k boxes). Once we have decided which
of the n + k 1 slots contains a character, then the string is completely
determined. Therefore the number of strings is equal to the number of ways
we can choose n slots from n + k 1 possibilities. This is just n+kn 1 .

x1 + + xk = n,

where n 0 is an integer, and x1 , . . . , xk are required to be non-negative

integers (that is, integers that are greater than or equal to zero). We can
think of this problem as distributing n identical balls into k distinguishable
bins. At the end of this distribution, the number of balls in Bin 1 will be x1 ,
the number of balls in Bin 2 will be x2 , and so on. Therefore the number of
solutions to the equation is

n+k 1
.
n }

Example. We have access to red, blue, and green marbles. We would like
to fill a jar with 50 marbles. How many dierent ways can we do this?
The answer is going to be the number of solutions to the equation

xred + xblue + xgreen = 50.

26
MATH 261 Enumeration 4. Balls and boxes

where xred , for example, is the number of red marbles in the jar. (So xred
must be a non-negative integer.) Therefore the answer is 50+3
50
1
= 1326.}

Next, lets look at the case that every box must contain at least one ball.

Proposition 4.2. Suppose that we have n indistinguishable balls, and k

distinguishable boxes. The number of dierent ways of distributing the balls
into the boxes so that each box contains at least one ball is

n 1
.
k 1

Proof. We use the same correspondence between distributions of balls and

strings of and | symbols that we employed in Proposition 4.1. However,
this time each box must contain at least one ball, so we cannot have two
consecutive | symbols. Nor can we start or end with a | symbol. How many
strings are there satisfying these constraints?
Think of the n symbols representing the balls lined up in a row. There are
n 1 spaces between them. We want to choose exactly k 1 of these spaces
to fill with | symbols. This way, no two | symbols fall into the same space, so
there are no consecutive | symbols. Therefore the number of strings is equal
to the number of ways we can choose k 1 spaces out of n 1 possibilities.
This is nk 11 .

Example. Consider the equation x1 + + xk = n, where x1 , . . . , xk are

constrained to be positive integers (that is, integers that are greater than
or equal to one). In the same way as an earlier example, we see that this is
like distributing n indistinguishable balls into k boxes, while insisting that
every box contains at least one ball. Therefore number of solutions to this
is nk 11 . }

Example. We want to fill a jar with 50 marbles, and the marbles are either
red, green, or blue. How many ways are there to fill the jar if we insist that
we take at least one of each colour? The answer is equal to the number of
solutions to the equation

where each variable is required to be a positive integer. Therefore the num-

ber of ways of filling the jar is 50 1
3 1 = 1176. }

27
MATH 261 Enumeration 4. Balls and boxes

Distinguishable balls, distinguishable boxes.

Proposition 4.3. Suppose that we have n distinguishable balls and k dis-
tinguishable boxes. Then there are k n dierent ways of distributing the balls
into the boxes.

Proof. For each ball we have k options when deciding which box to put it
in. Thus we have to make n choices, and for each choice we have k options.
The Multiplication Principle tells us that the total number of options is k n .
These options are all distinct, as the balls and boxes are distinguishable.

Remark. We can think of this problem using the same framework as in

Chapter 2. We are selecting boxes with repetition, where the order matters.
The first box selected is the destination of the first ball, the second box
selected is the destination of the second ball, and so on. }

Example. How many functions are there from {1, . . . , 10} to {1, . . . , 8}?
We think of choosing a function as putting balls labeled 1 to 10 into
boxes labeled 1 to 8. Putting Ball 1 into Box 4, for example, means that
we have chosen f (1) to be 4. Therefore there are 810 = 1073741824 possible
functions. }

This last example also follows from Proposition 2.2.

Example. Suppose we have a class of 30 students, and every student needs
to assigned to one of 3 tutors. How many ways are there of dividing the
class?
We have 30 balls, each labeled with the name of a student, and we are
going to distribute them into 3 boxes, each of which is labeled with the name
of a tutor. There are 330 = 205891132094649 ways of doing this. }

What if we insist that each tutor must be assigned at least one student?
The next result result shows us how to find the answer.
Proposition 4.4. Suppose that we have n distinguishable balls, and k dis-
tinguishable boxes. The number of dierent ways of distributing the balls
into the boxes so that each box contains at least one ball is

n n k n s k
k k(k 1) + (k 2) + + ( 1) (k s)n
k 2 k s
k 1
X
k 1 s k
+ + ( 1) k = ( 1) (k s)n .
k s
s=0

28
MATH 261 Enumeration 4. Balls and boxes

Proof. For 1 i k, let Ai be the set of distributions that put no balls in

box number i. Then we are interested in the distributions that are in none of
the sets A1 . . . , Ak . Thus we want to find |Ac1 \ \ Ack |. By Equation (3.3)
this is equal to
X X
|U | |Ai | + |Ai \ Aj | + +
1ik 1i<jk
X
( 1)s |Ai1 \ \ Ais | + + ( 1)k |A1 \ \ Ak | (4.1)
1i1 <<is k

where U is the set of all possible distributions.

We already know what |U | is: Proposition 4.3 says that it is k n . Suppose
that i1 < < is are distinct integers between 1 and k. We want to know
the cardinality of Ai1 \ \ Ais . But this is just the set of distributions
that leave boxes i1 , . . . , is empty. Therefore its cardinality is equal to the
number of ways of distributing the n balls among the remaining k s boxes,
and this is equal to (k s)n . There are ks = k k s ways of choosing the
integers 1 i1 < < is k from the range 1, . . . , k. By combining these
facts, we see that
X
k
|Ai1 \ \ Ais | = (k s)n .
k s
1i1 <<is k

Example. Recall that a function f from A to B is surjective, or onto, if for

every element b 2 B, there is some a 2 A such that f (a) = b. What is the
number of surjective functions from {1, . . . , 12} to {1, . . . , 4}? This is equal
to the number of ways we can assign balls labeled 1 to 12 to boxes labeled
1 to 4 in such a way that each box receives at least one ball. Therefore the
solution is
412 4 312 + 6 212 4 112 = 14676024. }

Proposition 2.4

Proposition 4.5. The number of onto functions from a set of size n to a

set of size k is
k 1
X
s k
( 1) (k s)n .
k s
s=0

29
MATH 261 Enumeration 4. Balls and boxes

Example. Suppose we have a class of 30 students, and every student needs

to assigned to one of 3 tutors. How many ways are there of dividing the
class if we insist that each tutor must have at least one student assigned to
them.
We have 30 balls, each labeled with the name of a student, and we are
going to distribute them into 3 boxes, each of which is labeled with the name
of a tutor. We insist that the boxes are non-empty. There are
330 3 230 + 3 130 = 205887910869180
ways of doing this. }

Stirling numbers. Before we continue with the balls-in-boxes problems,

we are going to discuss an important family of numbers called Stirling num-
bers.
A partition of {1, . . . , n} into k parts is a collection of k non-empty sets
that are pairwise disjoint, and whose union is equal to {1, . . . , n}. The parts
of the partition are also called blocks.
Example. There is one partition of {1, 2, 3} into one part: namely
{{1, 2, 3}}. (Note that this is a collection of one set, thus it is a set containing
a set.) There are three partitions of {1, 2, 3} into two parts:
{{1}, {2, 3}}, {{2}, {1, 3}}, and {{3}, {1, 2}}.
Finally, there is one partition of {1, 2, 3} into three parts: {{1}, {2}, {3}}.}
Definition 4.6. Suppose that n is a positive integer and 1 k n. The
number of partitions of {1, . . . , n} into k parts is said to be a Stirling number
of the second kind, and is denoted S(n, k).
Example. The previous example shows that S(3, 1) = 1, S(3, 2) = 3, and
S(3, 3) = 1. }
Remark. The notation
n
k
is often used instead of S(n, k). }
Remark. So what is a Stirling number of the first kind? You can think
of it this way: we have n people at a wedding with k circular tables. How
many ways can we arrange the people at the tables so that there is at least
one person at each table? The answer to this question is a Stirling number
of the first kind. }

30
MATH 261 Enumeration 4. Balls and boxes

(i) S(n, 1) = S(n, n) = 1.

n
(ii) If n > 1, then S(n, n 1) = 2 .

In Proposition 1.18 we showed that the binomial coefficients obey a re-

lation called a recurrence relation. Stirling numbers obey a similar rule.

Proposition 4.8. Suppose that n > 1 is an integer, and 1 < k < n. Then

S(n, k) = S(n 1, k 1) + kS(n 1, k).

Proof. Consider the partitions of {1, . . . , n} into k parts. There are two
types of such partitions: those where n is contained in a block by itself,
and those where it is not. If we have a partition of the first type, we can
construct a partition of {1, . . . , n 1} into k 1 parts by simply deleting n.
This is a one-to-one correspondence between partitions of the first type, and
partitions of {1, . . . , n 1} into k 1 parts. Thus the number of partitions
of the first type is S(n 1, k 1).
Now we consider partitions of the second type, where n is contained
in a block with other element(s). By deleting n, we produce a partition
of {1, . . . , n 1} into k parts. But there are k partitions that would have
produced the same partition of {1, . . . , n 1} after deleting n, because n
might have been contained in any of the k blocks. Therefore the number of
partitions of the second type is kS(n 1, k). The result follows.

By using Propositions 4.7(i) and 4.8, we can construct a triangle of

Stirling numbers of the second type.

1
1 1
1 3 1
1 7 6 1
1 15 25 10 1
1 31 90 65 15 1
1 63 301 350 140 21 1

The rows of this triangle are sequence A008277.

31
MATH 261 Enumeration 4. Balls and boxes

Distinguishable balls, indistinguishable boxes. Next we will consider

the case that balls are distinguishable, but boxes are not. It turns out to be
easier to first look at the case where each box must be non-empty.

Proposition 4.9. Suppose that we have n distinguishable balls, and k in-

distinguishable boxes. The number of dierent ways of distributing the balls
into the boxes so that each box contains at least one ball is
k 1
1 X k
( 1)s (k s)n .
k! k s
s=0

Proof. We know the number of distributions if we have n distinguishable

balls and k distinguishable boxes. By Proposition 4.4 the answer is
k 1
X
k
( 1)s (k s)n . (4.2)
k s
s=0

Now every distribution with distinguishable boxes corresponds to a distri-

bution with indistinguishable boxes: just imagine removing the labels from
the boxes. How many distinguishable arrangements correspond to each in-
distinguishable arrangement? Starting from a distinguishable distribution,
we could reorder the numbers on the k boxes in any way we like, and the
resulting distribution will correspond to exactly the same indistinguishable
distribution. There are k! ways of numbering the boxes, so every indistin-
guishable distribution has k! distinguishable distributions that correspond
to it. Therefore we can get the number of indistinguishable distributions by
dividing the quantity in (4.2) by k!. This provides us with the answer.
(There is an important subtlety that we have glossed over: we said that
there were k! ways to number the k boxes, and that therefore every indis-
tinguishable distribution has k! dierent distributions that correspond to
it. This is true, but only because the boxes are not empty. If there were
two empty boxes, we could swap the numbers on those boxes, and get two
identical distributions.)

Proposition 4.10. Let n be a positive integer, and assume 1 k n.

Then
k 1
1 X k
S(n, k) = ( 1)s (k s)n .
k! k s
s=0

32
MATH 261 Enumeration 4. Balls and boxes

Proof. A distribution of balls labeled 1 to n into k indistinguishable boxes,

where no box is empty, is just the same as a partition of {1, . . . , n} into k
non-empty blocks.

Now we can solve the more general problem, where boxes are allowed to
be empty.

Proposition 4.11. The number of ways of distributing n distinguishable

balls into k indistinguishable boxes is
k
X
S(n, 1) + S(n, 2) + + S(n, k) = S(n, i).
i=1

Proof. If no boxes are empty, then there are S(n, k) distributions, by Propo-
sition 4.10. If exactly one box is empty, then k 1 boxes are non-empty, so
there are S(n, k 1) such solutions. We continue in this way, until we see
that there are S(n, 1) = 1 distributions with k 1 boxes empty, and 1 box
not empty. Adding these numbers together gives us the result.

Integer partitions. We take another small detour before returning to

balls-in-boxes.
Earlier, we considered the number of solutions to equations of the form
x1 + + xk = n, where x1 , . . . , xk are constrained to be positive integers.
In this scenario, order matters. Thus, if n = 5 and k = 2, then x1 = 2 and
x2 = 3 is a dierent solution to x1 = 3 and x2 = 2. What if we ignore order?
Then we can put the variables x1 , . . . , xk into any order we like, so we may
as well assume that
x1 x2 xk .
A solution to x1 + + xk = n in which x1 , . . . , xk are positive integers
satisfying x1 x2 xk is known as an integer partition of n with k
parts.

Example. There are four partitions of 7 into 3 parts. They are

5 + 1 + 1, 4 + 2 + 1, 3 + 3 + 1, and 3 + 2 + 2. }

Definition 4.12. Suppose that n is a positive integer, and that 1 k

n. The number of integer partitions of n with k parts is denoted p(n, k).
Numbers of the form p(n, k) are called partition numbers.

33
MATH 261 Enumeration 4. Balls and boxes

p(n, n) = 1.

As with the Stirling numbers, there is a recurrence relation that governs

the partition numbers.

Proposition 4.14. Suppose that n > 1 is an integer, and 1 < k < n. Then

Proof. Consider the integer partitions of n with k parts. We divide these

into two sets, according to whether xk = 1 or not. The number of partitions
with xk = 1 is equal to the number of solutions to x1 + + xk 1 = n 1,
where x1 , . . . , xk 1 are positive integers satisfying x1 x2 xk 1 .
This is equal to p(n 1, k 1). Now consider an integer partition with
x1 x2 xk > 1. By subtracting one from each variable xi , we
create a one-to-one correspondence with integer partitions of n k into k
parts. The result follows.

Using Propositions 4.13 and 4.14, we can create a triangle of partition

numbers.
1
1 1
1 1 1
1 2 1 1
1 2 2 1 1
1 3 3 2 1 1
1 3 4 3 2 1 1
1 4 5 5 3 2 1 1

Entry number k in row number n is p(n, k). The rows of this triangle
are sequence A008284.
If n is a positive integer, then we define p(n) to be
n
X
p(n, k).
k=1

Thus p(n) is the total number of integer partitions of n. We call p(n) a

partition number.

34
MATH 261 Enumeration 4. Balls and boxes

Example. There are five possible integer partitions of 4. They are

4, 3 + 1, 2 + 2, 2 + 1 + 1, and 1 + 1 + 1 + 1.
Therefore p(4) = 5. }
There is a helpful way to visualize integer partitions. The integer parti-
tion x1 + +xk = n is represented by placing x1 dots in a row. Underneath
this we place x2 dots in a row, and so on. Thus the partition 6 + 3 + 3 + 1
of 13 is represented by

A diagram of this type is called a Ferrers graph, after Norman Macleod
Ferrers (18291903).
Ferrers graphs can make apparently difficult facts about integer parti-
tions quite straightforward to prove. By flipping the Ferrers graph, we can
prove the following result.
Proposition 4.15. The number of integer partitions of n with x1 = k is
equal to p(n, k).

Indistinguishable balls, indistinguishable boxes. By referring to par-

tition numbers, we can finish our tabulation of ball-in-boxes problems. The
answers in this section are not quite as satisfactory as some of the previ-
ous ones, because there is no neat formula which allows us to find partition
numbers.
Proposition 4.16. The number of dierent ways of distributing n indis-
tinguishable balls into k indistinguishable boxes so that each box contains at
least one ball is p(n, k).
Proof. Let xi denote the number of balls in box i. Then each xi is a positive
integer. Since the boxes can be arranged in any order we like, we can assume
that x1 x2 xk . Now there is an obvious one-to-one correspondence
between distributions of balls and integer partitions of n.
Corollary 4.17. The number of dierent ways of distributing n indistin-
guishable balls into k indistinguishable boxes is
k
X
p(n, 1) + p(n, 2) + + p(n, k) = p(n, i).
i=1

35
MATH 261 Enumeration 4. Balls and boxes

Proof. We sum over the number of boxes that are non-empty, and apply
Proposition 4.16.

We can now state a solution to all the variants of the balls in boxes
problem. Each cell in Table 1 contains a reference to the corresponding
result in these notes. Moreover, we have also collected all the solutions
to the subproblem of the balls and boxes problem where all the boxes are
required to contain at least one ball. The solutions to that subproblem are
noted in Table 2.

Balls Balls
distinguishable indistinguishable
Bins n+k 1
kn (4.3) n (4.1)
distinguishable
Bins Pk Pk
indistinguishable i=1 S(n, i) (4.11) i=1 p(n, i) (4.17)

Table 1: Solutions to the balls in boxes problems.

Balls Balls
distinguishable indistinguishable
Bins n 1
k!S(n, k) (4.4),(4.10) k 1 (4.2)
distinguishable
Bins
S(n, k) (4.10) p(n, k) (4.16)
indistinguishable

36