20. Combinatorics

Combinatorics is the art of counting without counting. It is a fundamental mathematical task to determine how many things there are in a given collection, and when the collection is large, it can be tedious or infeasible to count the elements individually. Moreover, when the collection is described in terms of a changing parameter (say, a natural number, n), we would like a formula that tells us how the number of objects depends on that parameter. In this chapter we will set up a foundation for achieving this goal, and learn some of the tricks of the trade.

20.1. Finite Sets and Cardinality

It will be helpful, for every natural number n, to have a canonical set of elements of size n. To that end, we will choose the set

[n]={mm<n}={0,1,,n1}.

We used the same notation, [n], to describe equivalence classes with respect to an equivalence relation, but hopefully our intended meaning will always be clear from the context.

A set A of elements is said to be finite if there is a bijection from [n] to A for some n. In that case, we would like to say that A has n elements, or that the set A has cardinality n, and write |A|=n. But to do so, we need to know that when A is finite, there is a unique n with the property above.

Suppose there are bijections from both [m] and [n] to A. Composing the first bijection with the inverse of the second, we get a bijection from [m] to [n]. It seems intuitively clear that this implies m=n, but our goal is to prove this from the fundamental properties of sets, functions, and the natural numbers.

So suppose, for the sake of contradiction, mn. Without loss of generality, we can assume m>n (why?). In particular, there is an injective function f from [m] to [n]. Since m>n, mn+1, and so we can restrict f to get an injective function from [n+1] to [n]. The next theorem shows that this cannot happen.


Theorem. For any natural number n, there is no injective function from [n+1] to [n].

Proof. By induction on n. The theorem is clear when n=0, because [1]={0} and [0]=. If f were an injective function from [1] to [0], we would have f(0), which is impossible.

So suppose the claim is true for n, and suppose f is an injective function from [n+2] to [n+1]. We consider two cases.

In the first case, suppose n is not in the image of f. Then f maps [n+2] to [n], and restricting the domain, we have an injective function from [n+1] to [n], contradicting the inductive hypothesis.

In the second case, there is some m<n+2 such that f(m)=n. The idea is to alter f slightly to get an injective function from [n+1] to [n], again contradicting the inductive hypothesis. If m=n+1, which is to say it is the last element of [n+2] that is mapped to the last element of [n+1], we can just restrict f to [n+1]. The fact that f was injective implies that all the elements in [n+1] are mapped to n.

Otherwise, define f:[n+1][n] by

f(i)={f(i)if imf(n+1)if i=m.

In other words, we map m to the value that n+1 was mapped to. Since f is injective, f(n+1)f(m), and so f(n+1)<n, as required. It is not hard to check that f is injective, so we have the contradiction we were after.


This theorem is known as the “pigeonhole principle.” It implies that if n+1 pigeons inhabit n holes, then at least one hole has more than one pigeon. The principle implies that for every finite set A, there is a unique n such that there is a bijection from [n] to A, and we can define the cardinality of A to be that n.

We now introduce the notation iAf(i) and iAf(i) for sums and products over finite sets. If A={a0,,an1}, then iAf(i) is defined to be f(a0)++f(an1), and similarly for products. Formally, what we are doing is choosing a bijection g:[n]A and defining iAf(i) to be j<nf(g(j)). It takes some work to show that this makes sense, which is to say, the answer we get doesn’t depend on which bijection we choose. We will just take this fact for granted here.

20.2. Counting Principles

Here is a basic counting principle.


Theorem. Let A and B be disjoint finite sets. Then |AB|=|A|+|B|.

Proof. Suppose f:[m]A and g:[n]B are bijections. Define h:[m+n]AB by

h(i)={f(i)if i<mg(im)if mi<m+n.

To see that h is surjective, note that every k in AB can be written as either k=f(i) for some i[m] or k=g(j) for some j[n]. In the first case, k=f(i)=h(i), and in the second case, k=g(j)=h(m+j).

It is not hard to show that h is also injective. Suppose h(i)=h(j). If h(i) is in A, then it is not in the range of g, and so we must have h(i)=f(i) and h(j)=f(j). Then f(i)=f(j), the injectivity of f implies that i=j. If h(i) is instead in B, the argument it similar.


The proof only spells out our basic intuitions: if you want to list all of the elements of AB, you can list all the elements of A and then all the elements of B. And if A and B have no elements in common, then to count the elements of AB, you can count the elements of A and then continue counting the elements of B. Once you are comfortable translating the intuitive argument into a precise mathematical proof (and mathematicians generally are), you can use the more intuitive descriptions (and mathematicians generally do).

Here is another basic counting principle:


Theorem. Let A and B be finite sets. Then |A×B|=|A||B|.


Notice that this time we are counting the number of ordered pairs (a,b) with aA and bB. The exercises ask you to give a detailed proof of this theorem. There are at least two ways to go about it. The first is to start with bijections f:[m]A and g:[n]B and describe an explicit bijection h:[mn]A×B. The second is to fix m, say, and use induction on n and the previous counting principle. Notice that if U and V are any sets and w is not in V, we have

U×(V{w})=(U×V)(U×{w}),

and the two sets in this union are disjoint.

Just as we have notions of union iIAi and intersection iIAi for indexed families of sets, it is useful to have a notion of a product iIAi. We can think of an element a of this product as a function which, for each element iI, returns an element aiAi. For example, when I={1,2,3}, an element of iIAi is just a triple a1,a2,a3 with a1A1, a2A2, and a3A3. This is essentially the same as A1×A2×A3, up to the fiddly details as to whether we represent a triple as a function or with iterated pairing (a1,(a2,a3)).


Theorem. Let I be a finite index set, and let (Ai)iI be a family of finite sets. Then:

  • If each pair of sets Ai, Aj are disjoint, then |iIAi|=iI|Ai|.

  • |iIAi|=iI|Ai|.

Proof. By induction on |I|, using the previous counting principles.


We can already use these principles to carry out basic calculations.


Example. The dessert menu at a restaurant has four flavors of ice cream, two kinds of cake, and three kinds of pie. How many dessert choices are there?

Solution. 4+2+3=9, the cardinality of the union of the three disjoint sets.

Example. The menu at a diner has 6 choices of appetizers, 7 choices of entrée, and 5 choices of dessert. How many choices of three-course dinners are there?

Solution. A three-course dinner is a triple consisting of an appetizer, an entrée, and a dessert. There are therefore 675=210 options.


A special case of the previous counting principles arises when all the sets have the same size. If I has cardinality k and each Ai has cardinality n, then the cardinality of iIAi is kn if the sets are pairwise disjoint, and the cardinality of iIAi is nk.


Example. A deck of playing cards has four suits (diamonds, hearts, spades, and clubs) and 13 cards in each suit, for a total of 413=52.

Example. A binary string of length n is a sequence of n many 0’s and 1’s. We can think of this as an element of

{0,1}n=i<n{0,1},

so there are 2n many binary strings of length n.


There is another counting principle that is almost too obvious to mention: if A is a finite set and there is a bijection between A and B, then B is also finite, and |A|=|B|.


Example. Consider the power set of [n], that is, the collection of all subsets of {0,1,2,,n1}. There is a one-to-one correspondence between subsets and binary strings of length n, where element i of the string is 1 if i is in the set and 0 otherwise. As a result, we have |P([n])|=2n.


20.3. Ordered Selections

Let S be a finite set, which we will think of as being a set of options, such as items on a menu or books that can be selected from a shelf. We now turn to a family of problems in combinatorics that involves making repeated selections from that set of options. In each case, there are finitely many selections, and the order counts: there is a first choice, a second one, a third one, and so on.

In the first variant of the problem, you are allowed to repeat a choice. For example, if you are choosing 3 flavors from a list of 31 ice cream flavors, you can choose “chocolate, vanilla, chocolate.” This is known as ordered selection with repetition. If you are making k choices from among n options in S, such a selection is essentially a tuple (a0,a1,,ak1), where each ai is one of the n elements in S. In other words, the set of ways of making k selections from S with repetition is the set Sk, and we have seen in the last section that if S has cardinality n, the set Sk has cardinality nk.


Theorem. Let S be a set of n elements. Then the number of ways of making k selections from S with repetition allowed is nk.

Example. How many three-letter strings (like “xyz,” “qqa,” …) can be formed using the twenty-six letters of the alphabet?

Solution. We have to make three selections from a set of 26 elements, for a total of 263=17,576 possibilities.


Suppose instead we wish to make k ordered selections, but we are not allowed to repeat ourselves. This would arise, from example, if a museum had 26 paintings in its storeroom, and has to select three of them to put on display, ordered from left to right along a wall. There are 26 choices for the first position. Once we have made that choice, 25 remain for the second position, and then 24 remain for the third. So it seems clear that there are 262524 arrangements overall.

Let us try to frame the problem in mathematical terms. We can think of an ordered selection of k elements from a set S without repetition as being an injective function f from [k] to S. The element f(0) is the first choice; f(1) is the second choice, which has to be distinct from f(0); f(2) is the third choice, which has to be distinct from f(0) and f(1); and so on.


Theorem. Let A and B be finite sets, with |A|=k and |B|=n, and kn. The number of injective functions from A to B is n(n1)(nk+1).

Proof. Using induction on k, we will show that for every A, B, and nk, the claim holds. When k=0 there is only one injective function, namely the function with empty domain. Suppose A has cardinality k+1, let a0 be any element of A. Then any injective function from A to B can be obtained by choosing an element b0 for the image of a0, and then choosing an injective function from A{a0} to B{b0}. There are n choices of b0, and since |A{a0}|=n1 and |B{b0}|=k1, there are (n1)(nk+1) choices of the injective function, by the inductive hypothesis.

Theorem. Let S be a finite set, with |S|=n. Then the number of ways of making k selections from S without repetition allowed is n(n1)(nk+1).

Proof. This is just a restatement of the previous theorem, where A=[k] and B=S.


If A is a finite set, a bijection f from A to A is also called a permutation of A. The previous theorem shows that if |A|=n then the number of permutations of A is n(n1)1. This quantity comes up so often that it has a name, n factorial, and a special notation, n!. If we think of the elements of A listed in some order, a permutation of A is essentially an ordered selection of n elements from A without repetition: we choose where to map the first element, then the second element, and so on. It is a useful convention to take 0! to be equal to 1.

The more general case where we are choosing only k elements from a set A is called a k-permutation of A. The theorem above says that the number of k-permutations of an n-element set is equal to n!/(nk)!, because if you expand the numerator and denominator into products and cancel, you get exactly the n(n1)(nk+1). This number is often denoted P(n,k) or Pnk, or some similar variant. So we have P(n,k)=n!/(nk)!. Notice that the expression on the right side of the equality provides an efficient way of writing the value of P(n,k), but an inefficient way of calculating it.

20.4. Combinations and Binomial Coefficients

In the last section, we calculated the number of ways in which a museum could arrange three paintings along a wall, chosen from among 26 paintings in its storeroom. By the final observation in the previous section, we can write this number as 26!/23!.

Suppose now we want to calculate the number of ways that a museum can choose three paintings from its storeroom to put on display, where we do not care about the order. In other words, if a, b, and c are paintings, we do not want to distinguish between choosing a then b then c and choosing c then b then a. When we were arranging paintings along all wall, it made sense to consider these two different arrangements, but if we only care about the set of elements we end up with at the end, the order that we choose them does not matter.

The problem is that each set of three paintings will be counted multiple times. In fact, each one will be counted six times: there are 3!=6 permutations of the set {a,b,c}, for example. So to count the number of outcomes we simply need to divide by 6. In other words, the number we want is 26!3!23!.

There is nothing special about the numbers 26 and 3. The same formula holds for what we will call unordered selections of k elements from a set of n elements, or k-combinations from an n-element set. Our goal is once again to describe the situation in precise mathematical terms, at which point we will be able to state the formula as a theorem.

In fact, describing the situation in more mathematical terms is quite easy to do. If S is a set of n elements, an unordered selection of k elements from S is just a subset of S that has cardinality k.


Theorem. Let S be any set with cardinality n, and let kn. Then the number of subsets of S of cardinality k is n!k!(nk)!.

Proof. Let U be the set of unordered selections of k elements from S, let V be the set of permutations of [k], and let W be the set of ordered selections of k elements from S. There is a bijection between U×V and W, as follows. Suppose we assign to every k-element subset {a0,,ak1} of S some way of listing the elements, as shown. Then given any such set and any permutation f of [k], we get an ordered the ordered selection (af(0),af(1),,af(k1)). Any ordered selection arises from such a subset and a suitable permutation, so the mapping is surjective. And a different set or a different permutation results in a different ordered selection, so the mapping is injective.

By the counting principles, we have

P(n,k)=|W|=|U×V|=|U||V|=|U|k!,

so we have |U|=P(n,k)/k!=n!k!(nk)!.

Example. Someone is going on vacation and wants to choose three outfits from ten in their closet to pack in their suitcase. How many choices do they have?

Solution. 10!3!7!=1098321=120.


The number of unordered selections of k elements from a set of size n, or, equivalently, the number of k-combinations from an n-element set, is typically denoted by (nk), C(n,k), Cnk, or something similar. We will use the first notation, because it is most common. Notice that (n0)=1 for every n; this makes sense, because there is exactly one subset of any n-element set of cardinality 0.

Here is one important property of this function.


Theorem. For every n and kn, we have (nk)=(nnk).

Proof. This is an easy calculation:

n!(nk)!(n(nk))!=n!(nk)!k!.

But it is also easy to see from the combinatorial interpretation: choosing k outfits from n to take on vacation is the same task as choosing nk outfits to leave home.


Here is another important property.


Theorem. For every n and k, if k+1n, then

(n+1k+1)=(nk+1)+(nk).

Proof. One way to understand this theorem is in terms of the combinatorial interpretation. Suppose you want to choose k+1 outfits out of n+1. Set aside one outfit, say, the blue one. Then you have two choices: you can either choose k+1 outfits from the remaining ones, with (nk+1) possibilities; or you can take the blue one, and choose k outfits from the remaining ones.

The theorem can also be proved by direct calculation. We can express the left-hand side of the equation as follows:

(n+1k+1)=(n+1)!(k+1)!((n+1)(k+1))!=(n+1)!(k+1)!(nk)!.

Similarly, we can simplify the right-hand side:

(nk+1)+(nk)=n!(k+1)!(n(k+1))!+n!k!(nk)!=n!(nk)(k+1)!(nk1)!(nk)+(k+1)n!(k+1)k!(nk)!=n!(nk)(k+1)!(nk)!+(k+1)n!(k+1)!(nk)!=n!(nk+k+1)(k+1)!(nk)!=n!(n+1)(k+1)!(nk)!=(n+1)!(k+1)!(nk)!.

Thus the left-hand side and the right-hand side are equal.


For every n, we know (n0)=(nn)=1. The previous theorem then gives a recipe to compute all the binomial coefficients: once we have determine (nk) for some n and every kn, we can determine the values of (n+1k) for every kn+1 using the recipe above. The results can be displayed graphically in what is known as Pascal’s triangle:

Specifically, if we start counting at 0, the kth element of the nth row is equal to (nk).

There is also a connection between (nk) and the polynomials (a+b)n, namely, that the kth coefficient of (a+b)n is exactly (nk). For example, we have

(a+b)4=a4+4a3b+6a2b2+4ab3+b4.

For that reason, the values (nk) are often called binomial coefficients, and the statement that

(a+b)n=kn(nk)ankbk

is known as the binomial theorem.

There are a couple of ways of seeing why this theorem holds. One is to expand the polynomial,

(a+b)n=(a+b)(a+b)(a+b)

and notice that the coefficient of the term ankbk is equal to the number of ways of taking the summand b in exactly k positions, and a in the remaining nk positions. Another way to prove the result is to use induction on n, and use the identity (n+1k+1)=(nk+1)+(nk). The details are left as an exercise.

Finally, we have considered ordered selections with and without repetitions, and unordered selections without repetitions. What about unordered selections with repetitions? In other words, given a set S with n elements, we would like to know how many ways there are of making k choices, where we can choose elements of S repeatedly, but we only care about the number of times each element was chosen, and not the order. We have the following:


The number of unordered selections of k elements from an n-element set, with repetition, is (n+k1k).


A proof of this is outlined in the exercises.

20.5. The Inclusion-Exclusion Principle

Let A and B be any two subsets of some domain, U. Then A=AB(AB), and the two sets in the union are disjoint, so we have |A|=|AB|+|AB|. This means |AB|=|A||AB|. Intuitively, this makes sense: we can count the elements of AB by counting the elements in A, and then subtracting the number of elements that are in both A and B.

Similarly, we have AB=A(BA), and the two sets on the right-hand side of this equation are disjoint, so we have

|AB|=|A|+|BA|=|A|+|B||AB|.

If we draw a Venn diagram, this makes sense: to count the elements in AB, we can add the number of elements in A to the number of elements in B, but then we have to subtract the number of elements of both.

What happen when there are three sets? To compute |ABC|, we can start by adding the number of elements in each, and then subtracting the number of elements of |AB|, |AC|, and |BC|, each of which have been double-counted. But thinking about the Venn diagram should help us realize that then we have over-corrected: each element of ABC was counted three times in the original sum, and the subtracted three times. So we need to add them back in:

|ABC|=|A|+|B|+|C||AB||AC||BC|+|ABC|.

This generalizes to any number of sets. To state the general result, suppose the sets are numbered A0,,An1. For each nonempty subset I of {0,,n1}, consider iIAi. If |I| is odd (that is, equal to 1, 3, 5, …) we want to add the cardinality of the intersection; if it is even we want to subtract it. This recipe is expressed compactly by the following formula:

|i<nAi|=I[n](1)|I|+1|iIAi|.

You are invited to try proving this as an exercise, if you are ambitious. The following example illustrates its use:


Example. Among a group of college Freshmen, 30 are taking Logic, 25 are taking History, and 20 are taking French. Moreover, 11 are taking Logic and History, 10 are taking Logic and French, 7 are taking History and French, and 3 are taking all three. How many students are taking at least one of the three classes?

Solution. Letting L, H, and F denote the sets of students taking Logic, History, and French, respectively, we have

|LHF|=30+25+2011107+3=50.

20.6. Exercises

  1. Suppose that, at a party, every two people either know each other or don’t. In other words, “x knows y” is symmetric. Also, let us ignore the complex question of whether we always know ourselves by restricting attention to the relation between distinct people; in other words, for this problem, take “x knows y” to be irreflexive as well.

    Use the pigeonhole principle (and an additional insight) to show that there must be two people who know exactly the same number of people.

  2. Show that in any set of n+1 integers, two of them are equivalent modulo n.

  3. Spell out in detail a proof of the second counting principle in Section 20.2.

  4. An ice cream parlor has 31 flavors of ice cream.

    1. Determine how many three-flavor ice-cream cones are possible, if we care about the order and repetitions are allowed. (So choosing chocolate-chocolate-vanilla scoops, from bottom to top, is different from choosing chocolate-vanilla-chocolate.)

    2. Determine how many three flavor ice-cream cones are possible, if we care about the order, but repetitions are not allowed.

    3. Determine how many three flavor ice-cream cones are possible, if we don’t care about the order, but repetitions are not allowed.

  5. A club of 10 people has to elect a president, vice president, and secretary. How many possibilities are there:

    1. if no person can hold more than one office?

    2. if anyone can hold any number of those offices?

    3. if anyone can hold up to two offices?

    4. if the president cannot hold another office, but the vice president and secretary may or may not be the same person?

  6. How many 7 digit phone numbers are there, if any 7 digits can be used? How many are there if the first digit cannot be 0?

  7. In a class of 20 kindergarten students, two are twins. How many ways are there of lining up the students, so that the twins are standing together?

  8. A woman has 8 murder mysteries sitting on her shelf, and wants to take three of them on a vacation. How many ways can she do this?

  9. In poker, a “full house” is a hand with three of one rank and two of another (for example, three kings and two fives). Determine the number of full houses that can be formed from an ordinary deck of 52 cards.

  10. We saw in Section 20.4 that

    (n+1k+1)=(nk+1)+(nk).

    Replacing k+1 by k, whenever 1kn, we have

    (n+1k)=(nk)+(nk1).

    Use this to show, by induction on n, that for every kn, that if S is any set of n elements, (nk) is the number of subsets of S with k elements.

  11. How many distinct arrangements are there of the letters in the word MISSISSIPPI?

    (Hint: this is tricky. First, suppose all the S’s, I’s, and P’s were painted different colors. Then determine how many distinct arrangements of the letters there would be. In the absence of distinguishing colors, determine how many times each configuration appeared in the first count, and divide by that number.)

  12. Prove the inclusion-exclusion principle.

  13. Use the inclusion-exclusion principle to determine the number of integers less than 100 that are divisible by 2, 3, or 5.

  14. Show that the number of unordered selections of k elements from an n-element set is (n+k1k).

    Hint: consider [n]. We need to choose some number i0 of 0’s, some number i1 of 1’s, and so on, so that i0+i1++in1=k. Suppose we assign to each such tuple a the following binary sequence: we write down i0 0’s, then a 1, then i1 0’s, then a 1, then i2 0’s, and so on. The result is a binary sequence of length n+k1 with exactly k 1’s, and such binary sequence arises from a unique tuple in this way.