MatheramaMatherama
← Combinatorics
Combinatorics·Section 9·14 min read

Distribution Problems (Balls and Boxes)

Almost every counting problem in combinatorics can be viewed as distributing nn objects (balls) into kk groups (boxes). To choose the right formula without hesitation, always ask two decisive questions first:

  1. Are the objects distinct or identical?
  2. Are the boxes distinct (distinguishable) or identical (indistinguishable)?

Within each of these four quadrants, the answer also depends on whether boxes may remain empty, must be non-empty, or have fixed capacities—and whether the order of objects inside each box matters. Keep this four-quadrant roadmap in mind as we derive each case systematically:

Four-Quadrant Roadmap of Balls-and-Boxes Distribution Problems


1. Distribution of Distinct Objects into Distinct Boxes§

Suppose we have nn distinct objects and kk distinct boxes—imagine putting numbered stickers Box 1, Box 2, ..., Box k on them so every box is uniquely identifiable.

Case A: Order Inside Each Box Does Not Matter§

1. Each box can contain any number of objects (empty boxes allowed): A box may stay completely empty, or a single box may receive all nn objects. Each of the nn distinct objects independently chooses any one of the kk boxes (Box 1, Box 2, ..., or Box k). By the Multiplication Principle:

k×k×k×⋯×k⏟n times=kn\underbrace{k \times k \times k \times \dots \times k}_{n \text{ times}} = k^n

2. Each box must contain at least one object (n≥kn \ge k): Now no box may be empty. Apply the Principle of Inclusion–Exclusion (Section 08):

Let UU be all knk^n unrestricted distributions, and let ArA_r (1≤r≤k1 \le r \le k) be the set of distributions in which Box r is empty. We want none of the boxes to be empty, i.e., ∣A1′∩A2′∩⋯∩Ak′∣|A_1' \cap A_2' \cap \dots \cap A_k'|.

  • If rr particular boxes are forced to stay empty, all nn objects must be placed into the remaining k−rk - r boxes in (k−r)n(k - r)^n ways, and there are (kr)\binom{k}{r} ways to choose those rr empty boxes.

By PIE:

∣A1′∩A2′∩⋯∩Ak′∣=kn−(k1)(k−1)n+(k2)(k−2)n−⋯+(−1)k(kk)(k−k)n=∑r=0k(−1)r(kr)(k−r)n|A_1' \cap A_2' \cap \dots \cap A_k'| = k^n - \binom{k}{1}(k - 1)^n + \binom{k}{2}(k - 2)^n - \dots + (-1)^k \binom{k}{k}(k - k)^n = \sum_{r=0}^{k} (-1)^r \binom{k}{r} (k - r)^n

3. Each box has a fixed capacity (n1+n2+⋯+nk=nn_1 + n_2 + \dots + n_k = n): Suppose Box 1 must receive exactly n1n_1 objects, Box 2 exactly n2n_2 objects, ..., and Box k exactly nkn_k objects, where n1+n2+⋯+nk=nn_1 + n_2 + \dots + n_k = n.

  • Choose n1n_1 objects out of nn for Box 1 in (nn1)\binom{n}{n_1} ways.
  • Choose n2n_2 objects from the remaining n−n1n - n_1 for Box 2 in (n−n1n2)\binom{n - n_1}{n_2} ways.
  • Continue until the last nkn_k objects go to Box k in (nknk)=1\binom{n_k}{n_k} = 1 way.

Multiplying and canceling consecutive factorials produces the exact same multinomial formula we met in MISSISSIPPI (Section 06):

(nn1)(n−n1n2)⋯(nknk)=n!n1! n2! n3! ⋯ nk!\binom{n}{n_1}\binom{n - n_1}{n_2}\cdots\binom{n_k}{n_k} = \frac{n!}{n_1!\, n_2!\, n_3!\, \cdots \, n_k!}

Case B: Order Inside Each Box Also Matters (Bank Cash Counters)§

Now add one further condition: the objects placed inside each box are also arranged in order—like nn distinct customers forming queues at kk distinct bank cash counters.

1. Each counter may have any number of customers (empty queues allowed): In Section 03, we counted this using the rising factorial: Person 11 has kk choices, Person 22 has k+1k + 1, ..., and Person nn has k+n−1k + n - 1, giving:

k(k+1)(k+2)⋯(k+n−1)=(n+k−1)!(k−1)!=n+k−1Pnk(k + 1)(k + 2)\cdots(k + n - 1) = \frac{(n + k - 1)!}{(k - 1)!} = {}^{n + k - 1} P_n

We can also derive this visually using identical wooden wedges (analogous to binary strings): arrange all nn distinct people and k−1k - 1 identical red wooden wedges in one long row. The k−1k - 1 wedges slice the row into kk ordered queues—the segment before the first wedge goes to Counter 11 in that exact order, the segment between the first and second wedges goes to Counter 22, and so on!

Arranging People into Ordered Queues at Bank Counters Using Identical Wooden Wedges

We are permuting n+k−1n + k - 1 items in which k−1k - 1 wedges are identical, giving (n+k−1)!(k−1)!\frac{(n + k - 1)!}{(k - 1)!} ways.

2. Each counter must have at least one customer (n≥kn \ge k): Using wooden wedges, a counter is empty if and only if two wedges sit next to each other or a wedge sits at either end of the line. Therefore:

  • Arrange all nn distinct customers in a single row in n!n! ways.
  • This creates n−1n - 1 interior gaps between consecutive customers. Choose k−1k - 1 of these n−1n - 1 gaps to insert the k−1k - 1 identical wedges in (n−1k−1)\binom{n - 1}{k - 1} ways.

By the Multiplication Principle:

n!(n−1k−1)n! \binom{n - 1}{k - 1}

3. Each counter has a fixed queue length (n1+⋯+nk=nn_1 + \dots + n_k = n): Here the positions of the wedges are completely fixed by the required capacities—only the nn people permute among the nn ordered slots, giving simply n!n! ways.


2. Distribution of Identical Objects into Distinct Boxes§

Now suppose we have nn identical objects (such as 100100 identical oranges) and kk distinct boxes (Box 1, ..., Box k). Because the oranges are indistinguishable, only the number of oranges xix_i in Box i matters, where:

x1+x2+x3+⋯+xk=nx_1 + x_2 + x_3 + \dots + x_k = n

This is identical to the integral equation solved in Section 07 (Stars and Bars):

  • Each box may contain any number of objects (xi≥0x_i \ge 0):
(n+k−1k−1)\binom{n + k - 1}{k - 1}
  • Each box must contain at least one object (xi≥1x_i \ge 1, n≥kn \ge k):
(n−1k−1)\binom{n - 1}{k - 1}

3. Distribution of Distinct Objects into Identical Boxes§

Before counting distributions into identical boxes, pause and make sure you know when boxes are truly identical.

When Are Two Boxes Truly Identical?
Boxes are identical only if we have no way to distinguish one from another—no labels, no distinct spatial positions, and no different capacities! For instance, a box of capacity 55 is automatically distinguishable from a box of capacity 1010. Likewise, four rows of chairs in a classroom may all have the exact same capacity, yet they are distinct because Row 11 (front), Row 22, Row 33, and Row 44 (back) occupy different physical locations in the room.

A. Identical Boxes of Equal Fixed Capacity§

Suppose we distribute nknk distinct objects into kk identical boxes, each having the exact same fixed capacity of nn objects.

To see the effect of removing box labels, take 66 distinct objects {a,b,c,d,e,f}\{a, b, c, d, e, f\} and 33 identical boxes of capacity 22. Look at one single distribution into identical boxes: {{a,b},  {c,d},  {e,f}}\{\{a, b\},\; \{c, d\},\; \{e, f\}\}.

If we now attach three distinct stickers—Box 1, Box 2, and Box 3—onto these three boxes, we can place the stickers in 3!=63! = 6 different ways, turning one identical-box distribution into 66 distinct-box distributions:

Attaching 3! = 6 Distinct Box Labels to One Identical-Box Distribution

In general, let xx be the number of distributions of nknk distinct objects into kk identical boxes of capacity nn. For every such distribution, attaching kk distinct box stickers in k!k! ways generates all distinct-box distributions:

x×k!=(nk)!n! n!⋯n!⏟k times=(nk)!(n!)k  ⟹  x=(nk)!(n!)k k!x \times k! = \frac{(nk)!}{\underbrace{n!\,n!\cdots n!}_{k \text{ times}}} = \frac{(nk)!}{(n!)^k} \implies x = \frac{(nk)!}{(n!)^k \, k!}

Remember the simple rule: when kk boxes of equal capacity are identical instead of distinct, divide by k!k!.

Example 1 (Handshakes vs. Numbered Tables): At a party of 100100 guests, all guests divide into 5050 pairs. In how many ways can this happen if: (a) they simply pair up to shake hands, (b) they sit in pairs at 5050 tables placed around the banquet hall?

Solution:

(a) Forming 5050 handshake pairs: Each pair is an unlabeled group of capacity 22—all 5050 groups are identical! Thus:

100!(2!)50 50!=100!250 50!\frac{100!}{(2!)^{50}\,50!} = \frac{100!}{2^{50}\,50!}

(b) Sitting in pairs at 5050 tables in the hall: Now each table is distinguishable by its physical position in the hall (near the entrance, near the stage, etc.), so the 5050 boxes are distinct! We do not divide by 50!50!:

100!250\frac{100!}{2^{50}}

B. Mixed Fixed Capacities§

Suppose there are no labels on the boxes, yet some boxes share one capacity and other boxes share a different capacity—for example, 55 boxes of capacity 2525 and 22 boxes of capacity 1010 (total objects =5×25+2×10=145= 5 \times 25 + 2 \times 10 = 145).

The 55 boxes of capacity 2525 are identical to one another (divide by 5!5!), and the 22 boxes of capacity 1010 are identical to one another (divide by 2!2!), while a 2525-object box can never be confused with a 1010-object box. Thus the number of distributions is:

145!(25!)5(10!)2⋅5! 2!\frac{145!}{(25!)^5 (10!)^2 \cdot 5!\,2!}

C. Arbitrary Non-Empty Groups: Stirling Numbers of the Second Kind§

Now drop fixed capacities completely: suppose we wish to partition nn distinct objects into rr non-empty identical (unlabeled) groups of any non-zero sizes.

To appreciate the subtlety, divide 44 distinct objects {a,b,c,d}\{a, b, c, d\} into 22 non-empty unlabeled groups by direct enumeration:

{a},  {b,c,d}{b},  {a,c,d}{c},  {a,b,d}{d},  {a,b,c}\{a\},\; \{b, c, d\} \qquad \{b\},\; \{a, c, d\} \qquad \{c\},\; \{a, b, d\} \qquad \{d\},\; \{a, b, c\} {a,b},  {c,d}{a,c},  {b,d}{a,d},  {b,c}\{a, b\},\; \{c, d\} \qquad \{a, c\},\; \{b, d\} \qquad \{a, d\},\; \{b, c\}

There are 77 ways in total. Do not let your brain treat the left group as "Group 1" and the right group as "Group 2"! If you did, you would think swapping {a,b}\{a, b\} and {c,d}\{c, d\} creates an eighth way. Because the groups have no names, {{a,b},{c,d}}\{\{a, b\}, \{c, d\}\} and {{c,d},{a,b}}\{\{c, d\}, \{a, b\}\} are the exact same partition.

Definition (Stirling Number of the Second Kind): Denoted S(n,r)S(n, r) (or {nr}\left\{\begin{matrix} n \\ r \end{matrix}\right\}), the Stirling number of the second kind is the number of ways to distribute nn distinct objects into rr non-empty identical boxes (1≤r≤n1 \le r \le n).

Let us calculate key special cases directly from first principles:

  • S(n,1)=1S(n, 1) = 1: All nn objects go into the single box.
  • S(n,n)=1S(n, n) = 1: Each object sits alone in its own box.
  • S(n,2)=2n−1−1S(n, 2) = 2^{n - 1} - 1: Temporarily name the two boxes Group I and Group II. Each of the nn objects has 22 choices (2n2^n ways); subtracting the 22 cases where one box is empty leaves 2n−22^n - 2 distributions into labeled groups. Removing the two labels divides by 2!=22! = 2, giving:
S(n,2)=2n−22=2n−1−1S(n, 2) = \frac{2^n - 2}{2} = 2^{n - 1} - 1

(Checking n=4n = 4: S(4,2)=23−1=7S(4, 2) = 2^3 - 1 = 7, matching our enumeration!)

  • S(n,n−1)=(n2)S(n, n - 1) = \binom{n}{2}: To split nn objects into n−1n - 1 non-empty groups, exactly one group must contain a pair of 22 objects while the remaining n−2n - 2 groups are singletons. Choose the pair in (n2)\binom{n}{2} ways.
  • S(n,n−2)=(n3)+3(n4)S(n, n - 2) = \binom{n}{3} + 3\binom{n}{4}: Splitting nn objects into n−2n - 2 non-empty groups occurs in two disjoint size patterns:
    1. One group of size 33 and n−3n - 3 singletons: Choose the 33 objects in (n3)\binom{n}{3} ways.
    2. Two groups of size 22 and n−4n - 4 singletons: Choose 44 objects in (n4)\binom{n}{4} ways and divide them into 22 identical pairs in 4!(2!)2 2!=3\frac{4!}{(2!)^2\,2!} = 3 ways, giving 3(n4)3\binom{n}{4} ways.

Theorem (Explicit PIE Formula for S(n,r)S(n, r)): Attaching r!r! distinct labels to the rr non-empty identical boxes yields all non-empty distributions into rr distinct boxes. By the Division Principle:

S(n,r)=1r!∑j=0r(−1)j(rj)(r−j)nS(n, r) = \frac{1}{r!} \sum_{j=0}^{r} (-1)^j \binom{r}{j} (r - j)^n

Theorem (Recurrence Relation for S(n,r)S(n, r)): For all 1<r<n1 < r < n:

S(n,r)=r⋅S(n−1,r)+S(n−1,r−1)S(n, r) = r \cdot S(n - 1, r) + S(n - 1, r - 1)

Combinatorial Proof: Single out one specific object xx among the nn objects. In any partition of the nn objects into rr non-empty unlabeled groups, either:

  1. xx forms a singleton group {x}\{x\} by itself: The remaining n−1n - 1 objects must be partitioned into r−1r - 1 non-empty groups, which can be done in S(n−1,r−1)S(n - 1, r - 1) ways.
  2. xx shares a group with at least one other object: First partition the other n−1n - 1 objects into rr non-empty groups in S(n−1,r)S(n - 1, r) ways. Because those rr groups now hold distinct objects, they are distinguishable! Hence xx has rr choices of which group to join, giving r⋅S(n−1,r)r \cdot S(n - 1, r) ways.

Adding the two disjoint cases proves S(n,r)=r S(n−1,r)+S(n−1,r−1)S(n, r) = r\,S(n - 1, r) + S(n - 1, r - 1). ■\blacksquare

Here are the first five rows of the Stirling triangle built directly from this recurrence:

n\rn \backslash r 11 22 33 44 55 Row Sum BnB_n
11 11 11
22 11 11 22
33 11 33 11 55
44 11 77 66 11 1515
55 11 1515 2525 1010 11 5252

Bell Numbers (Empty Identical Boxes Allowed)§

If nn distinct objects are placed into kk identical boxes where empty boxes are allowed, the number of occupied boxes can be 1,2,…,min⁡(n,k)1, 2, \dots, \min(n, k), yielding ∑r=1min⁡(n,k)S(n,r)\sum_{r=1}^{\min(n, k)} S(n, r) ways.

When k≥nk \ge n, this sum counts the total number of partitions of an nn-element set into any number of non-empty blocks. This famous sequence is the nn-th Bell Number, BnB_n:

Bn=∑r=1nS(n,r)B_n = \sum_{r=1}^{n} S(n, r)

(As we will see in Section 10, BnB_n also counts the exact number of equivalence relations on an nn-element set!)


4. Distribution of Identical Objects into Identical Boxes (Integer Partitions)§

In the fourth and final quadrant, both the nn objects and the kk boxes are identical. Neither object identities nor box order matter—only the sorted multiset of box sizes distinguishes one distribution from another!

Definition (Integer Partition): Writing a positive integer nn as a non-increasing sum of rr positive integers:

n=λ1+λ2+⋯+λr,λ1≥λ2≥⋯≥λr≥1n = \lambda_1 + \lambda_2 + \dots + \lambda_r, \qquad \lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_r \ge 1

is called an integer partition of nn into rr parts, and the number of such partitions is denoted pr(n)p_r(n).

  • Each of kk identical boxes non-empty: pk(n)p_k(n) ways.
  • Empty boxes allowed: ∑r=1min⁡(n,k)pr(n)\sum_{r=1}^{\min(n, k)} p_r(n) ways (partitions into at most kk parts). When k≥nk \ge n, this is the total partition function p(n)p(n).

No simple closed-form formula exists for pr(n)p_r(n) or p(n)p(n); for small nn, list non-increasing tuples systematically.

Example 2: In how many ways can 66 identical marbles be distributed into 33 identical bowls if: (a) no bowl is empty, (b) bowls may be empty?

Solution:

(a) Non-empty (p3(6)p_3(6)): List all non-increasing triples of positive integers summing to 66: (4,1,1)(4, 1, 1), (3,2,1)(3, 2, 1), and (2,2,2)(2, 2, 2). Thus p3(6)=3p_3(6) = 3.

(b) Empty bowls allowed (p1(6)+p2(6)+p3(6)p_1(6) + p_2(6) + p_3(6)):

  • 11 non-empty bowl: (6)  ⟹  1(6) \implies 1 way.
  • 22 non-empty bowls: (5,1),  (4,2),  (3,3)  ⟹  3(5, 1),\; (4, 2),\; (3, 3) \implies 3 ways.
  • 33 non-empty bowls: 33 ways from part (a).

Total ways =1+3+3=7= 1 + 3 + 3 = 7.


Master Summary of Distribution Problems§

Objects (nn) Boxes (kk) Empty Boxes Allowed Every Box Non-Empty (n≥kn \ge k)
Distinct Distinct (unordered inside) knk^n k! S(n,k)=∑r=0k(−1)r(kr)(k−r)nk!\,S(n, k) = \sum_{r=0}^k (-1)^r \binom{k}{r}(k - r)^n
Distinct Distinct (ordered inside) kn‾=(n+k−1)!(k−1)!k^{\overline{n}} = \frac{(n + k - 1)!}{(k - 1)!} n!(n−1k−1)n! \binom{n - 1}{k - 1}
Identical Distinct (n+k−1k−1)\binom{n + k - 1}{k - 1} (n−1k−1)\binom{n - 1}{k - 1}
Distinct Identical ∑r=1min⁡(n,k)S(n,r)\sum_{r=1}^{\min(n, k)} S(n, r) (BnB_n if k≥nk \ge n) S(n,k)S(n, k)
Identical Identical ∑r=1min⁡(n,k)pr(n)\sum_{r=1}^{\min(n, k)} p_r(n) (p(n)p(n) if k≥nk \ge n) pk(n)p_k(n)
Section 9 of 11 · Combinatorics

Mark this section complete when you have worked through its proofs.