Generating Functions in Combinatorics: 1 Selecting R Objects Out of N
Generating Functions in Combinatorics: 1 Selecting R Objects Out of N
Generating Functions in Combinatorics: 1 Selecting R Objects Out of N
c Jan Vrbik
There are two basic issues in Combinatorics; here we give a brief introduction
to each.
1
1.2 Repetition allowed, order irrelevant
This time, we have to replace each factor of the (1 + a) type in (1) by
1
1 + a + a2 + a3 + ... =
1−a
Example 2 For n = 5 and r = 3 we get
1
≃ ... +
(1 − a)(1 − b)(1 − c)(1 − d)(1 − e)
a3 + b3 + c3 + d3 + e3 + abc + abd + acd + bcd + abe + ace + ade + bce + bde + cde
+ae2 + be2 + ce2 + de2 + ab2 + a2 b + ac2 + a2 c + ad2 + bc2 + a2 d + b2 c + bd2
+b2 d + cd2 + c2 d + a2 e + b2 e + c2 e + d2 e + ...
where ≃ indicates a multivariate Taylor expansion (in terms of a, b, c, d and e)
and, on the RHS, we list only the cubic terms (a2 c means: select two as and
one c).
The corresponding generating function yields
1
≃ 1 + 5x + 15x2 + 35x3 + ...
(1 − x)5
implying that there are 5 ways of selecting one letter, 15 ways of selecting two
and 35 ways (listed above) to select three (not necessarily distinct) letters, etc.
This means that a general formula for the number of ways to select r ob-
jects out of n (in this particular manner) is the coefficient of xr in the Taylor
(generalized Binomial) expansion of (1 − x)−n , namely
−n (n + r − 1)! n+r−1
(−1)r = = (3)
r r!(n − 1)! r
This is also the number of way of permuting r circles and n − 1 bars (each such
permutation yielding a unique selections of letters, e.g. | | || corresponds
to a0 b1 c2 d0 e0 , etc.).
2
Expanding
x x2 x3
exp(4x) = 1 + 4 · + 42 · + 43 · + ..
1! 2! 3!
tells us that there are 4 ways of doing this when selecting one letter (out of 4),
42 ways when selecting two, 43 when selecting three, etc.
The general formula for the number of possible ways to do this (creating
distinct r-letter ‘words’ based on n-letter ‘alphabet’) is
nr (4)
This can be derived more directly by considering how to fill in r ‘blanks’, each
with a choice of one of n letters.
3
2 Partitioning m objects into k groups
This time, we need to specify whether
m+k−1 m+k−1
= (6)
m k−1
Should we not allowing empty cells, we can put one object into each cell
first (there is just one way of doing that) and then use the previous formula
for placing the remaining m − k objects into k cells, getting
m−1 m−1
=
m−k k−1
2. When the objects are distinct and their order (within each group) matters,
we first choose how many objects to put in each group - which can be done
using (6). Then we fill in the ‘blanks’, one by one, each time selecting a
4
specific object. Since there are m! possible ways to do the latter, each
resulting in a different result (partition), the final answer is
k+m−1 (k + m − 1)!
m! · = (7)
m (k − 1)!
Not allowing empty cells similarly results in
m−1
m! · (8)
k−1
3. When objects are distinct and their order within each group irrelevant,
all we have to do is to choose, for each object, the group to place it in
(having k choices every time). So, the answer is provided by (4) whose n
is now the number of groups (= k) and r is the number of objects (= m),
getting
km
To make sure that each cell contains at least one object is now more
complicated (for details, see the next section); the corresponding -
(with respect to m, for k fixed) is
k
k k
exp(x) − 1 = (−1)i exp (k − i) · x (9)
i=0
i
where exp(x) − 1 is the EGF of the 0, 1, 1, 1... sequence (indicating that
a cell is non-empty). The coefficient of xm , multiplied by m!, yields the
final answer of
k
k
(−1)i (k − i)m (10)
i=0
i
This follows from the following, well-known Taylor expansion
x2 x3 x4
exp(a · x) = 1 + a · x + a2 + a3 + a4 + ...
2 3! 4!
2.2 Interchangeable (unordered) groups
This means that any rearrangement of groups (as long as their individual ‘con-
tents’ remain the same) is not seen as a new partition. It is thus not very
meaningful (even though still possible) to allow empty groups now; we therefore
impose an extra condition that each group must contain at least one object (to
allow empty groups would simply require using the ‘non-empty’ formula with 1,
2, ... and k groups, then adding the answers).
5
2.2.2 Distinct objects, their order irrelevant
Similarly, each such solution is found k! times in (10), implying that the new
answer is
k
m def 1 k
= (−1)i (k − i)m
k k! i=0 i
This expression is called the S . The
ordinary generating function of these (with respect to m, with k fixed) is
k
x
i=1
1 − i·x
(see the next section). There is no simple explicit formula for the coefficients of
the corresponding Taylor expansion.
3 Generating Functions
These are of two distinct types.
6
stage having its own set of ‘sub-arrangements’. These must also be mapped
into integers, to yield their individual contributions to ℓ (defined as the resulting
sum; ℓ thus needs to be ‘stage-additive’). Furthermore, each ‘total’ arrangement
must be achieved by exactly one specific sequence of sub-arrangements, implying
the usual : the numbers of sub-arrangements (one
for each stage) need to be multiplied to yield the total number of arrangements.
The resulting OGF is then a simple product of the individual OGFs at each
stage.
Example 5 In how many ways can we select r different letters out of a col-
lection of n distinct letters when the resulting order is irrelevant.
In this example, selecting an ‘arrangement’ is deciding, for each of the n
letters, whether to keep it or not; each of these ‘stages’ contributes 0 or 1 (re-
spectively) to ℓ, defined as the number of letters kept. The OGF of the value
contributed to ℓ at each stage is thus 1 + x (the coefficient of each term repre-
sents the number of ways to make that choice, the exponent is the resulting ℓ
contribution), implying that
G(x) = (1 + x)n
Example 6 Find the OGF (with respect to r, keeping n fixed) for the number
of ways one can select r letters out of a collection of n distinct letters, with
unlimited duplication allowed and the resulting order irrelevant.
This time we have to decide, for each of our n letters, how many times to
include it; we can still do this in n stages. The contribution to ℓ (number of
letters selected) has the following one-stage OGF - there is just one way (the
coefficient) of duplicating a letter exactly i (the exponent of x) times, where i
must go from 0 to ∞ to cover any potential value of ℓ:
1
1 + x + x2 + x3 + x4 + ... =
1−x
implying the overall OGF of
1
(1 − x)n
Example 7 Find the OGF (with respect to m, keeping k fixed) for the number of
partitions of m distinct objects into k non-empty groups (their order irrelevant).
Note that we now let m to be the dummy variable (the old ℓ) of (12).
We assign each of the m objects to a group, one by one, by starting to fill
one group until we need another, then another, ..., until we have exactly k of
them; starting a new group is always seen as the beginning of a new stage. To
cover every possible value of m, we proceed as if this m was infinite; it is the
7
task (and beauty) of the resulting OGF have the answer for any and each finite
m.
In Stage 1, we put Object 1 into a group, then keep on adding Object 2,
.... to the same group (adding any number of extra objects, including 0, is
possible); each time we do it, we have only one choice of an object. The first-
stage OGF is thus (coefficients represent the number of choices, powers specify
the corresponding contribution to m):
x
x + x2 + x3 + ... =
1−x
In the second stage, we place the next available object to a new group, then keep
on adding the next few (potentially any number) objects to either one of the
already existing two groups; this time we have two choices for where to place
each of the additional objects, getting, for the corresponding Stage-2 OGF
x
x + 2x2 + 22 x3 + 23 x4 + ... =
1 − 2x
In Stage 3, we start yet another group, then keep on adding (any number of)
extra objects to one of the three existing groups (thus having 3 choices for each
extra object); the OGF of this stage is thus
x
x + 3x2 + 32 x3 + 33 x4 + ... =
1 − 3x
This is repeated until filling exactly k groups in this manner. Note that we have
thus covered all possibilities of creating k ‘unordered’ (if they were ordered,
we would have k choices, not one, for placing Object 1, etc. ) and non-empty
groups, using any (total) number of objects.
The overall OGF is thus
k
x
i=1
1 − i·x
The answer to our original question is the coefficient of xm in the Taylor ex-
pansion of this function.
Example 8 In how many ways can we write an integer m ≥ k as a sum of
exactly k positive integers (their order does not count).
This is equivalent to writing m as a sum of any number of positive integers,
the largest of which (there may be more than one such term) equal(s) to k. This
is based on a duality demonstrated by the following example:
9 =5+2+2 =3+3+1+1+1
where 9 is written as a sum of three terms, and also as a (‘dual’, in the one-
to-one sense) sum of terms having the largest value of 3. The equivalence of the
two can be seen (just count the dots, either horizontally or vertically) from the
following Y
• • • • •
• •
• •
8
We construct the desired OGF using the second version of the problem (largest
term(s) equal to k) by multiplying OGFs of the individual k stages. In Stage
1 we select i number of 1’s (there is one way of doing it, contributing i, which
becomes the exponent of x, to the value of m) where i can go from 0 to ∞, in
Stage 2 we similarly select any number of 2’s (i of them now contribute 2i to
the total), etc. The corresponding Stage-s contribution to the final OGF is thus
1
1 + xs + x2s + x3s + ... =
1 − xs
But note that the last stage is exceptional: we must select at least one k; its
contribution to OGF is thus
xk
xk + x2k + x3k + ... =
1 − xk
Multiplying these (altogether k) one-stage contributions yields the overall OGF
of the original question, namely
k−1
xk 1
1 − xk s=1
1 − xs
which agrees with (11). The final answer requires Taylor-expanding this function
and taking the coefficient of xm .
1+x+2x2 +3x3 +5x4 +7x5 +11x6 +14x7 +20x8 +26x9 +35x10 +44x11 +58x12 +71x13 +90x14 +110x15 +...
The answer is thus 110 (the generating function provides the answer for each
and any number of dice rolled).
9
of the f (0), f (1), f(2), .... sequence is defined as
∞
def xℓ
F (x) = f(ℓ)
ℓ!
ℓ=0
which is a product of k simple EGFs, each built using just one of the hj func-
tions.
In reverse, this enables us to interpret a product of two or more EGFs as a
single EGF of the (14) type.
• all hj functions be the same (one can then drop their subscript and call
them h),
• where h is the indicator function of a specific condition imposed on the
size of each of the k subsets (h equals to 1 when the condition is met and
to 0 otherwise).
10
This implies that the
k
ĥj
part j=1
part of (14) is the number of ordered partitions of the original set into exactly
k disjoint subsets, all of which must meet, individually, this condition. Based
on the main theorem, the corresponding EGF is equal to
∞ k
xi def
F (x) = h(i) = H(x)k (15)
i=0
i!
Example 11 What is the number of distinct r-letter words based on n-letter
alphabet (repetition allowed, order matters).
This is equivalent (but not necessarily easier than the direct approach used
earlier) to partitioning a set of the first r positive integers into n distinct subsets
of any size (thus listing the positions of Letter 1, Letter 2,... Letter n in the
corresponding r-letter word - an empty subset implies not using that letter at
all). The EGF allowing each subset to be of any size (including 0) is
x2 x3
H(x) = 1 + x + + + ... = exp(x) (16)
2 3!
The EGF which then answers our question (covering all possible values of r, for
a fixed n) is
F (x) = exp(x)n = exp(n · x)
Note that its coefficient of xr (when Taylor-expanded in x) multiplied by r!,
agrees with (4).
Example 12 Same question, but this time not allowing any duplication of let-
ters.
The only thing which changes in the solution is H(x), which must now re-
strict the size of each subset to at most one element, thus:
H(x) = 1 + x
r
The coefficient of x in the expansion of
F (x) = (1 + x)n
further multiplied by r! yields (5).
Example 13 Find the number of different partitions of a set with m distinct
elements into and ordered collection of non-empty subsets.
Since h is equal to 0 for an empty set and to 1 otherwise, we now get
x2 x3 x4
H(x) = x + + + + ... = exp(x) − 1 (17)
2 3 4!
and
k
F (x) = exp(x) − 1
The answer is m! times the coefficient of xm in the Taylor expansion of this
function, leading to (10).
11
3.2.3 Composition of two EGFs
Suppose we have an EGF (usually of the simple, indicator-function type, which
must exclude empty subsets) H(x) on a set of elements, and another EGF, say
G(x), whose ‘distinct objects’ are disjoint non-empty subsets of the original set.
It can be shown that the EGF for the number of distinct ways to combine the
two (whatever the specifics are) is the of G and H (be careful
with the order, this operation is non-commutative), namely
G (H(x))
We thus get the EGF for the number of ways ℓ elements of the original set can
be partitioned into k non-empty disjoint unordered (order is removed by the
k! denominator of G) subsets (all meeting Condition H), further multiplied by
g(k) then summed over all k from 0 to ∞.
12