Combinatorics [00FP]
- April 24, 2026
Introduction to combinatorics [00FQ]
- April 24, 2026
Combinatorics is the area of mathematics about counting, that is, determining the sizes of sets. This sounds like it would be trivial or uninteresting, but it turns out to be deep and fascinating, with many connections to other areas of mathematics.
You may have memorized a few combinatorics formulas in high school (permutations, combinations, …) but we are going to learn some basic underlying principles—if you understand the principles involved, you can easily re-derive the formulas as needed.
The product rule [00FR]
- April 24, 2026
The first principle we will consider is the product rule.
Remark Counting vs choosing [00GP]
- May 25, 2026
Note, instead of just talking about the sizes of sets we will often talk in terms of choosing elements of a set. The number of elements in a set is the same as the number of ways to choose an element of the set, so in some sense this makes no difference; but for some reason it is often helpful to think in terms of actively choosing elements rather than just passively counting them.
Principle The Product Rule [00FS]
- April 24, 2026
Suppose choosing an item can be decomposed into two independent choices, with c_1 ways to make the first choice, and c_2 ways to make the second choice. Then the total number of ways to choose overall is the product c_1 \times c_2.
Definition Independence [00FT]
- April 24, 2026
Here, independent means that you have the same set of options for both choices no matter what you pick. In other words, choosing a certain option for one of the choices cannot affect which options are available for the other choice.
Remark [00FU]
- April 24, 2026
We can visualize the situation by drawing a rectangular grid, where each row corresponds to one option for the first choice, and each column corresponds to an option for the second choice. Then each grid square corresponds to a unique combination of choices. The fact that the choices are independent corresponds to the fact that the picture is perfectly rectangular.
This really boils down to the fact, which we saw before when learning about sets, that |A \times B| = |A| \times |B|.
Example Counting breakfasts [00FV]
- April 24, 2026
There are three kinds of cereal and seven kinds of fruit. For breakfast, you must choose one cereal and one fruit. How many breakfast choices do you have?
The product rule clearly applies here: the answer is 3 \times 7 = 21 choices.
Example Counting breakfasts (non-example) [00FW]
- April 24, 2026
This is a non-example. Suppose we have the same scenario as the previous problem, except that when you choose oatmeal, you are limited to having only strawberries or blueberries.
In this case the product rule does not apply (at least not directly) since the choices are no longer independent.
Example Counting strings [00FX]
- April 24, 2026
How many 3-letter strings are there using the letters A–Z? Examples of such strings include AAA, HDX, CAR, and so on.
The number of two-letter strings is 26 \times 26 since there are two independent choices: which letter to choose as the first letter, and which to choose as the second. Then the number of 3-letter strings is the number of ways to pick a two-letter string, times the number of ways to pick a third letter, for a total of (26 \times 26) \times 26 = 26^3 = 17576.
Similarly, there are 26^4 four-letter strings, 26^5 five-letter strings, and in general there are 26^k k-letter strings. This reasoning shows that we can generalize the product rule to a product of n choices instead of just two. If we have n independent choices to make, we just multiply all of them to compute the total number of choices.
Example Counting subsets [00FZ]
- April 27, 2026
How many subsets are there of an n-element set?
In other words, what is the cardinality of a power set? We already know the answer to this question, but let's see how the product rule applies. When choosing a subset, for each of the n elements we have two choices: whether to include it in the subset or not. All these choices are independent, because each element is allowed to be in the subset or not, regardless of which other elements are or are not members of the subset. Hence, by the product rule, the total number of choices is 2 \times 2 \times \dots \times 2 = 2^n.
If we identify each subset with a bit string of length n indicating which elements are in the subset and which are out, we can see a connection to the previous example: there are 2^n bit strings of length n, since we can independently choose each bit to be either 0 or 1.
Example Counting soup schedules [00G0]
- April 27, 2026
Suppose we have 10 cans of soup (all different) and we want to eat them all, one per day over the next 10 days. In how many ways could we do this? Note this is equivalent to asking how many ways we could order the cans of soup; imagine we first put them in a particular order and then each day just eat the next can in the line.
We might at first think the answer is 10^{10}, since there are 10 days and 10 choices of soup on each day. However, the choices are not independent: once I choose a particular can of soup I can’t choose it again on a subsequent day. (10^{10} is the answer to the related problem of how many soup-eating schedules we can make, if each day we go to the store to buy some soup, and the store carries 10 different kinds. Assuming the store has enough stock, the choices are now independent, since getting a particular soup on one day does not prevent us from picking the same soup again another day.)
However, we can conceive of this problem in terms of independent choices if we change our point of view a little bit. On the first day, I have 10 choices. On the second day, I can no longer choose the soup I ate on the first day, but I can freely choose any soup I want out of the 9 that are left. On the third day, I can choose any of the 8 that remain, and so on. In total, then, I have 10 \cdot 9 \cdot 8 \cdot \dots \cdot 1 = 10! = 3628800 ways to do this.
More generally, there are n(n-1)(n-2) \cdots 1 = n! ways to put n things in a particular order, that is, there are n! permutations of n things.
Example Counting truncated soup schedules [00G1]
- April 27, 2026
What if I have 10 different cans of soup, as in the previous scenario, but now I only want to eat soup for the next four days?
As before, I have 10 choices on the first day, 9 choices on the second day, and so on, for a total of 10 \cdot 9 \cdot 8 \cdot 7 = 5040 ways. Note this can also be written as 10 \cdot 9 \cdot 8 \cdot 7 = \frac {10 \cdot 9 \cdot 8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}{6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1} = \frac {10!}{6!}. In general, if I have n things and want to pick out k of them in a particular order, I have n \cdot (n-1) \cdot \dots \cdot (n-k+1) = \frac {n!}{(n-k)!} ways to do it.
Example Counting functions [00G2]
- April 27, 2026
Given finite sets A and B, how many functions f : A \to B are there?
Consider how we might go about choosing a particular function f : A \to B. A function from A to B must specify a particular element of B for each element of A. So for the first element a_1 \in A, we can choose any element b \in B to which f should send a_1. Likewise, for a_2 \in A, we can independently choose any element of B to which f should send a_2, and so on for every element of A. (These choices are all independent because for any old function, we don’t care if it sends multiple a_i to the same b, or misses some b’s, or whatever.) So for each element of a, we get to make an independent choice of size |B|; hence, by the product rule, the total number of ways we could choose a function is \underbrace {|B| \times |B| \times \dots \times |B|}_{|A|} = |B|^{|A|}.
Bonus: can you count the number of one-to-one functions f : A \to B? (Counting onto functions is much harder!)
The addition rule [00G3]
- April 28, 2026
The next principle we will consider I like to call the addition rule.
Principle The Addition Rule [00G4]
- April 28, 2026
Suppose choosing something means either making one choice in c_1 ways or making another choice in c_2 ways, and the choices do not overlap at all. Then the total number of choices is c_1 + c_2.
In other words, when A and B are disjoint, |A \cup B| = |A| + |B|.
Example Counting lunches [00G5]
- April 28, 2026
You want to order a single item for lunch. Restaurant A has 13 things on its menu, and Restaurant B has 24 on its menu. How many different choices do you have for lunch, if the two restuarants have completely different menus?
Assuming the restaurant menus do not overlap at all, the answer is simply 13 + 24 = 37.
(What if the menus do overlap? We will come back to this question.)
Example Counting soup schedules of varying length [00G7]
- April 29, 2026
If we have ten different soups, and want to make a schedule of which to eat for at most four days, how many ways can we do it?
In this case we can simply note that we can either choose to eat soup for one day, or for two days, or three, or four. These choices do not overlap, since schedules with different numbers of days are obviously different. Hence, the total is 10 + 10 \cdot 9 + 10 \cdot 9 \cdot 8 + 10 \cdot 9 \cdot 8 \cdot 7.
There is another way we could think about this. On the first day, we have ten choices of what soup to pick. For the remaining three days, we can either choose to stop eating soup (in exactly one way), or we can choose from among the nine remaining soups, then decide on a schedule for the last two days; and so on. So the total is 10 \cdot (1 + 9 \cdot (1 + 8 \cdot (1 + 7))).
Counting binary trees [00G6]
- April 29, 2026
Counting the number of binary trees with a certain number of branch nodes provides a great example putting together many things we have seen so far (the product and addition rules, and recursively defined sets and functions).
Here are all the binary trees with zero, one, two, and three branch nodes:
We can see that there are five different binary trees with three branch nodes. How many will there be with four branch nodes? We are quickly approaching the limit of what we will be able to count while being confident that we have not missed any.
Consider the set of five trees with three branch nodes shown above. Of course, every tree has one branch node at its root; then we have two more branch nodes that we have to put somewhere. We can break the trees into three subsets, depending on how many branch nodes we put to the left and right of the root:
The trees on the left have two branch nodes on the left side of the root and none on the right; the tree in the middle has one branch node on either side; and the trees on the right have none on the left and two on the right. It’s clear that these are the only possibilities, and every tree has to fall into exactly one of these categories. If we put two branch nodes on the left, we have two ways to do it (since there are two different trees with two branch nodes), and only one way to put zero on the right (there is only one tree with zero branch nodes). Similarly, we have one way to make a tree with one branch node to either side, and two ways to put two on the right. In general, we can compute the number of ways we have to make a tree with j nodes on the left and k nodes on the right using the product rule: since the left and right trees can be chosen independently, it is just the number of trees with j branch nodes times the number of trees with k branch nodes.
Put together, this tells us that if T(n) represents the number of binary trees with n branch nodes, then T(3) = T(2)T(0) + T(1)T(1) + T(0)T(2). And sure enough, T(0) = T(1) = 1 and T(2) = 2, so this equals 2 \cdot 1 + 1 \cdot 1 + 1 \cdot 2 = 5, as it should.
Using the same pattern we can compute the number of binary trees with four branch nodes: T(4) = T(3) T(0) + T(2) T(1) + T(1) T(2) + T(0) T(3) = 5 + 2 + 2 + 5 = 14.
In general, we can define T(n) recursively as \begin {align*} T(0) &= 1 \\ T(n) &= \sum _{k=0}^{n-1} T(n-k-1)T(k) \end {align*} The resulting sequence, known as the Catalan numbers, is extremely famous: 1, 1, 2, 5, 14, 42, 132, 429, 1430, \dots
The subtraction rule [00G8]
- April 29, 2026
The next principle to consider, I like to call the subtraction rule. In a sense, though, it is just a more sophisticated version of the addition rule.
Principle The Subtraction Rule [00G9]
- April 29, 2026
If choosing something means making one choice in c_1 ways OR another in c_2 ways, but the choices overlap, the total number of choices is c_1 + c_2 - (\text {the number of choices in common}).
Another way to write this, which we have seen before, is |A \cup B| = |A| + |B| - |A \cap B|.
Example Counting lunches, again [00GA]
- April 29, 2026
You again want to order a single item for lunch; restaurant A has 13 things on its menu, and Restaurant B has 24 on its menu. However, there are 9 items which the two restaurants have in common. How many items can you order for your lunch?
Adding 13 + 24 gives the total number of items on both menus, but it has counted the shared items twice. Subtracting the number of shared items gives the correct total: 13 + 24 - 9 = 28.
Example Counting bit strings [00GB]
- April 29, 2026
How many strings of 8 bits either start with a 1 or end with 00?
There are 2^7 strings of 8 bits that start with a 1: the first bit is fixed, and we can independently choose each remaining bit to be 0 or 1.
Similarly, there are 2^6 strings of 8 bits that end with 00.
We cannot simply add these numbers, because of overlap: there are some strings of bits that both start with 1 and end with 00. In fact there are 2^5 such strings, since three bits are fixed and we can freely choose the other 5. Thus, the total number of bit strings that start with 1 or end with 00 is 2^7 + 2^6 - 2^5 = 160.
Remark ★ Principle of inclusion-exclusion [00GC]
- April 29, 2026
What if we have three (or more) overlapping sets of choices? It is actually possible to come up with a general formula for this situation, known as the Principle of Inclusion-Exclusion.
For 3 sets of choices, the formula looks like this: |A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C| That is, to count the size of the union A \cup B \cup C, we can add their sizes; but this counts the overlapping regions more than once, so we can subtract each overlap. However, the overlap between all three sets got counted three times, then subtracted three times, so we have to finally add it back once.
In general, the cardinality of an arbitrary union of sets is equal to the sum of all the individual cardinalities, minus the cardinalities of all intersections of exactly two of the sets, plus the cardinalities of all intersections of exactly three sets, minus the intersections of four sets, ... and so on. Proving this is outside the scope of this class, but it's actually not a difficult proof—it requires use of some set operation properties such as associativity of union, distributivity of intersection over union, as well as induction on the number of sets.
The division rule and binomial coefficients [00GD]
- April 29, 2026
Example Counting soup schedules, again [00GF]
- April 29, 2026
Recall the problem with soup, where we have a bunch of different kinds of soup and are trying to count the number of different schedules we could make for ourselves. For example, if we have 10 different kinds of soup and want to eat soup for 4 days, there are 10 \cdot 9 \cdot 8 \cdot 7 different soup schedules we can make.
Now consider a slightly different question: in how many ways can we choose which four soups to eat, if we don’t care about the order? In this case 10 \cdot 9 \cdot 8 \cdot 7 is clearly too big, since there are many different schedules which have the same four soups in different orders.
In fact, for any set of four soups, we know there will always be exactly 4! = 24 schedules containing those four soups in some order. So 10 \cdot 9 \cdot 8 \cdot 7 is too big by exactly a factor of 24. Hence the number of different sets of four soups we can choose is \frac {10 \cdot 9 \cdot 8 \cdot 7}{24} = 210.
Principle The Division Rule [00GE]
- April 29, 2026
Suppose we have n total choices, but we really want to think of some of them as being equivalent. If the choices always come in groups of exactly k choices which are all equivalent (i.e. each equivalence class is of size exactly k), then the number of “really different” choices (the number of equivalence classes) is n/k.
Example Counting seating arrangements [00GG]
- April 29, 2026
How many ways are there to seat 5 people around a table, if we only care about who is sitting to the left and right of who, and not about the specific seats they are in?
One way to answer this question is by first considering that there are 5! ways to assign each person to a specific seat. However, given a specific assignment of each person to a seat, if we have everyone stand up and move one seat to the right, we get a different seat assignment, but as a seating arrangement it should be considered the same, since we only care who is sitting next to who. In fact, for every specific seating arrangement there are four other arrangements which are equivalent, for a total of five equivalent arrangements (corresponding to the five possible rotations around the table). Thus, by the division rule, there are 5! / 5 = 24 different seating arrangements.
Note we can also see this by first insisting that person A must always sit in a specific chair; since specific chairs don’t matter, they might as well. Then we have four choices for who to seat the right of person A, then three choices for who to seat to the right of them, and so on, for a total of 4! choices.
Example Counting seating arrangements again [00GI]
What if we again want to seat 5 people around a table, but this time we only care who is sitting next to who, but left vs right doesn't matter?
As we know from the previous example, there are 24 ways to seat people if we consider seating arrangements equivalent that are rotations of one another. Now, for any seating arrangement we also consider it equivalent to its mirror image. Each arrangement always has exactly one mirror image, so we just need to divide by 2: the number of different arrangements is 12.
Definition Binomial coefficients [00GH]
- April 29, 2026
Let's return to our example with soup to generalize it. In that case, we were concerned with picking a subset of 4 soups out of a total of 10. More generally, if we want to choose k things out of n—that is, we want to count the number of size-k subsets of a set of size n—we first of all have n \cdot (n-1) \cdot \dots \cdot (n-k+1) = \frac {n!}{(n-k)!} ways to choose k things in a specific order. But rearranging the k things into a different order is not supposed to make any difference, so for each different subset of k things there are k! orderings which should all be considered equivalent. Thus, by the division rule, there are \frac {n!}{k!(n-k)!} size-k subsets of a set of size n. This is such a common idea that it has a special notation: \binom {n}{k} = \frac {n!}{k!(n-k)!} is called a binomial coefficient, and we pronounce it “n choose k”. There is a special notation for them because they show up all over the place in mathematics (not just in combinatorics).
Example Counting bit strings with exactly 3 ones [00GJ]
- April 29, 2026
How many 8-bit binary strings are there with exactly three 1’s and five 0’s?
This is equivalent to picking three out of the eight bit positions to set to 1, so it is \binom {8}{3} = \frac {8!}{3! 5!} = \frac {8 \cdot 7 \cdot 6}{3 \cdot 2 \cdot 1} = 56.
Alternatively, we could pick five out of the eight bit positions to set to 0: \binom {8}{5} = \frac {8!}{5! 3!} = 56 as well.
Example Counting bit strings with at least 3 ones [00GK]
- April 29, 2026
How many 8-bit binary strings have at least 3 bits set to 1?
It is easiest to compute this by subtracting the strings we don’t want to count from the total: 2^8 - \binom 8 2 - \binom 8 1 - \binom 8 0 = 256 - 28 - 8 - 1 = 219.
Alternatively, we could just add up all the possibilities: \binom {8}{3} + \binom {8}{4} + \binom {8}{5} + \binom {8}{6} + \binom {8}{7} + \binom {8}{8} = 56 + 70 + 56 + 28 + 8 + 1 = 219.
Binomial coefficient properties [00GL]
Here is a table of the first few binomial coefficients, with n along the left side, and k along the top.
\begin {array}{c|cccccc} & 0 & 1 & 2 & 3 & 4 & 5 \\ \hline 0 & 1 & & & & & \\ 1 & 1 & 1 & & & & \\ 2 & 1 & 2 & 1 & & & \\ 3 & 1 & 3 & 3 & 1 & & \\ 4 & 1 & 4 & 6 & 4 & 1 & \\ 5 & 1 & 5 & 10& 10& 5 & 1 \\ \end {array}
(The blank entries are 0, since they represent clearly impossible things like “the number of ways to pick 4 things out of a set of size 2”.) This is called Pascal's triangle and is rather well-known (you may very well have seen it before). Some properties of binomial coefficients:
- \displaystyle \binom {n}{0} = \binom {n}{n} = 1. We can prove this either by looking at the formula (\frac {n!}{0!n!} = 1) or by thinking about what it means combinatorially: there is exactly one way to choose nothing, or everything, from a set of size n.
- \displaystyle \binom {n}{1} = n. Again, we can see this from the formula, or by reasoning that if we want to pick exactly one thing out of a set of size n, we have n ways to do it, one for each element.
- \displaystyle \binom {n}{k} = \binom {n}{n-k} (each row of Pascal's triangle is a palindrome). In the formula, k! and (n-k)! just switch places; combinatorially, we have the same number of ways to choose k things as we do to choose which n-k things we are not going to choose. In other words, instead of thinking in terms of “choosing k things” we can think of it in terms of “splitting a set of size n into two subsets of sizes k and n-k”.
- \displaystyle \binom {n}{k} = \binom {n-1}{k} + \binom {n-1}{k-1} (each entry in Pascal's triangle is the sum of the entry above it and the entry above and to the left). This is the rule for constructing Pascal’s Triangle: each entry is the sum of the entry above it and the one above and to the left. I leave it as a challenge for you to prove this either from the formula or by thinking about it combinatorially.
- \displaystyle \sum _{k=0}^n \binom {n}{k} = 2^n (the sum of the nth row of Pascal's triangle is 2^n). These are just two different ways to count all the subsets of a set of size n.