# Lecture 16: combinatorics

• counting the elements in a set
• finite cardinality, sum rule, product rule

## Finite cardinality

Definition: We say the cardinality (or size) of a set $$X$$ is $$n$$ (written $$|X| = n$$) if $$|X| = |\{1,2,\dots,n\}|$$. Here we are using the definition of same-cardinality defined in lecture 14; that is, there must exists a bijection between $$X$$ and $$\{1,2,3,\dots,n\}$$.

If this is the case, we say $$X$$ is finite.

## Sum rule

Claim: if $$A$$ and $$B$$ are disjoint (that is, if $$A \cap B = \emptyset$$) then $$|A| + |B| = |A \cup B|$$.

Proof: Suppose $$|A| = i$$ and $$|B| = j$$. Then there exists bijections $$f_A : \{1,\dots,i\} → A$$ and $$f_B : \{1,\dots,j\} → B$$. We can define a bijection $$f : \{1, \dots, i+j\} → A \cup B$$ as follows: on input $$n$$, if $$1 \leq n \leq i$$ then let $$f(n) ::= f_A(n)$$, and if $$i+1 \leq n \leq i+j$$ then let $$f(n) ::= f_B(n-i)$$.

We can easily check that $$f$$ is a bijection:

• it is a function because every $$n$$ is either $$\leq i$$ or $$\geq i+1$$, and not both.

• it is injective; if $$f(n_1) = f(n_2)$$ then either $$f(n_1) \in A$$, in which case both $$n_1$$ and $$n_2$$ are between $$1$$ and $$i$$, and we have $$f_A(n_1) = f(n_1) = f(n_2) = f_A(n_2)$$. This implies that $$n_1 = n_2$$ since $$f_A$$ is injective. Similarly, if $$f(n_1) \in B$$, we use the injectivity of $$f_B$$.

• it is also surjective; given $$y \in A \cup B$$, either $$y \in A$$ or $$y \in B$$. if $$y \in A$$, then (since $$f_A$$ is surjective), there exists an $$n$$ between $$1$$ and $$i$$ with $$f_A(n) = y$$. But then $$f(n) = y$$ by definition, so in this case there exists an $$n$$ with $$f(n) = y$$. On the other hand, if $$y \in B$$, then there exists some $$n \in \{1,\dots,j\}$$ with $$f_B(n) = y$$. But then $$f(i+n) = f_B(n) = y$$, so again, $$y$$ is in the image of $$f$$. Since $$y$$ is in the image in either case, we see that $$f$$ is surjective.

## Basic inclusion/exclusion

Note: We didn't cover this section in lecture, but it serves as another good example, and is a useful rule.

Claim: in general, $$|A \cup B| = |A| + |B| - |A \cap B|$$.

Note: several students pointed out that we are am using the fact that $$A \cup B = (A \setminus B) \cup (A \cap B) \cup (B \setminus A)$$, and also that these three sets are disjoint. I did not prove these facts, but they are certainly provable, using the definitions of intersection, union, and difference. For example, to show that $$A \cap B$$ and $$A \setminus B$$ are disjoint, we could do a proof by contradiction. Assume that there is some $$x$$ in $$A \cap B$$ and $$A \setminus B$$. By the definition of $$\cap$$, this means $$x \in B$$, but by the definition of $$\setminus$$, we see that $$x \notin B$$. This is a contradiction.

Proof: We could construct a bijection as we did above, but it's easier to reuse what we've already proven. Let $$|A \setminus B| = i$$, $$|A \cap B| = j$$ and $$|B \setminus A| = k$$.

Clearly (see note above) $$A = (A \setminus B) \cup (A \cap B)$$. Note these are disjoint sets, so by above, $$|A| = i + j$$. Similarly, $$|B| = j + k$$. Moreover, $$A \cup B = (A \setminus B) \cup (A \cap B) \cup (B \setminus A)$$ so $$|A \cup B| = i + j + k = (i + j) + (j + k) - j = |A| + |B| - |A \cap B|$$ as required.

## Product rule

Claim: $$|A \times B| = |A||B|$$. Note that writing $$|A||B|$$ implies that $$|A|$$ and $$|B|$$ are things we can multiply; in other words, we require $$|A| = n_a$$ for some $$n_a \in ℕ$$ and similarly $$|B| = n_b$$. In other words, $$A$$ and $$B$$ must be finite.

Proof: Since $$|A| = n_a$$ there exists a bijection $$f : A → \{1,2,3,\dots,n_a\}$$. Similarly, there exists a bijection $$g : B → \{1,2,3,\dots,n_b\}$$. We construct a bijection $$h : A \times B → \{1,2,\dots,n_an_b\}$$ as follows: $$h(a,b) := n_b(f(a)-1) + g(b)$$. $$h$$ is well-defined: since $$1 ≤ f(a) ≤ n_a$$ and $$1 ≤ g(b) ≤ n_b$$, a bit of algebra shows that $$1 ≤ h(a,b) ≤ n_bn_a$$.

To see that $$h$$ is injective, suppose $$h(a,b) = h(a',b')$$. We want to show that $$a = a'$$ and $$b = b'$$. Since $$h(a,b) = h(a',b')$$, we have $$n_b(f(a)-1) + g(b) = n_b(f(a')-1) + g(b')$$ by definition. Rearranging this gives $$g(b) - g(b') = n_b(f(a') - f(a))$$. Now, since the right hand side of this equation is a multiple of $$n_b$$, it must be either $$0$$ or $$≥ n_b$$ or $$≤ -n_b$$. But since $$1 ≤ g(b) ≤ n_b$$ and similarly for $$g(b')$$, the largest that $$g(b) - g(b')$$ can be is $$n_b - 1$$ and the smallest is $$-n_b + 1$$. Therefore the only possibility is that $$g(b) - g(b') = 0$$. Thus $$g(b) = g(b')$$; since $$g$$ is injective, this means $$b = b'$$. We can now plug this back into the equation $$g(b) - g(b') = n_b(f(a') - f(a))$$ to conclude that $$f(a') = f(a)$$, which gives $$a' = a$$, as required.

The proof that $$h$$ is surjective is left as an exercise.

## Generalized product rule

Suppose we want to compute the size of a set of elements that are constructed by some process. At each step of the process, we can chose from among several options. Suppose that the number of available options is the same regardless of the choices made earlier in the process. Then we can find the size of the set by multiplying together the number of choices.

Example: Suppose we want to count the number of ways to arrange the letters "BAR". We first choose the first letter. There are 3 possibilities. We then choose the second letter; there are only two to choose from. Finally, we choose the third letter; there is only one choice remaining. The number of strings is therefore $$3 \cdot 2 \cdot 1 = 3!$$.

Example: Suppose we want to count the number of subsets of $$\{1,2,3,4,5\}$$. We can construct a subset by first choosing whether or not to include $$1$$ (There are two choices), then whether or not to inlcude 2 (2 choices), whether or not to include 3 (2 choices) and so on. Thus there are $$2 \cdot 2 \cdots 2 = 2^5$$ possible subsets.

Example: $$|A \times B| = |A||B|$$.

Example: Suppose we want to count the number of 4-element strings formed from the 7 characters MICHAEL. There are 7 choices for the first, 6 for the second, 5 for the third, and 4 for the fourth, so there are $$7 \cdot 6 \cdot 5 \cdot 4$$ such strings. A compact way to write this is $$7!/3!$$.

Why is this justified? Each time we make a choice among $$k$$ options while constructing an element of the set $$X$$, we are subdividing $$X$$ into disjoint subsets: those elements of $$X$$ where we chose option 1, those where we chose option 2, and so on. Because future choices don't depend on the choices we made, each subset has the same size (say $$n$$), so we can use the sum rule to find the total size of $$X$$; $$X$$ is the union of $$k$$ subsets of size $$n$$, so $$|X| = n + n + n + \cdots + n = kn$$.

Note that this requires that the subsets are disjoint; if making two different choices could potentially yield the same element $$x$$ of $$X$$, then we will be double counting $$x$$; in this case the product rule will give the wrong answer.