\documentclass{article}
\usepackage{amssymb, mathrsfs, enumitem}
\usepackage{tikz-cd}
\usepackage{multicol}
\usepackage{graphicx}
\usepackage{color}
\usepackage{amsthm}
\usepackage{hyperref}
\usepackage{verbatim}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{amsmath, epsfig, colortbl}
\usepackage[breakable, skins]{tcolorbox}
\setlength{\parskip}{8pt}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{cor}{Corollary}
\theoremstyle{definition}
\newtheorem{prop}{Proposition}
\newtheorem{claim}{Claim}
\newtheorem{fact}{Fact}
\newtheorem{example}{Example}
\newtheorem{definition}{Definition}
\newenvironment{solution} {\paragraph{Solution.}}{\hfill$\square$}
\renewenvironment{proof} {\paragraph{Proof.}}{\hfill$\square$}
\renewcommand{\today}{}
\captionsetup{margin=.7in, format=hang}

\newtcolorbox{examplebox}{ breakable, enhanced}
\newtcolorbox{gptbox}{ breakable, enhanced,  colback  = blue!10}

\addtolength{\topmargin}{-.875in}
\addtolength{\oddsidemargin}{-.8in}
\addtolength{\textwidth}{1.6in}
\addtolength{\textheight}{1.75in}

\newcommand{\mc}{\mathcal}
\newcommand{\abs}[1]{\left| #1\right|}
\newcommand{\la}{\Leftarrow}
\newcommand{\ra}{\Rightarrow}
\renewcommand{\iff}{\Leftrightarrow}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\floor}[1]{\left\lfloor #1\right\rfloor}
\newcommand{\ceil}[1]{\left\lceil #1\right\rceil}

\newcommand{\ex}[1]{\begin{examplebox}\begin{example} #1 \end{example}\end{examplebox}}

\newcommand{\eps}{\epsilon}
\renewcommand{\P}{\mathcal{P}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\inv}[1]{#1^{-1}}
\newcommand{\ba}{\backslash}

\def\cents{\hbox{\rm\rlap/c}}




\begin{document}
\title{Math 127: Combinatorics}
\author{Mary Radcliffe}
\maketitle
In these notes, we will apply the ideas from the \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes} to combinatorial counting. We can see, to some extent, the entirety of this document as examples of finite counting as described previously.

\section{Combinatorics Fundamentals}

We begin by defining some fundamental numbers we will use in combinatorial counting. We have already seen one such number: $n!$. Let's connect that number to some combinatorial objects.

\begin{definition}
Let $X$ be a finite set. A {\it permutation} of $X$ is an ordering of the elements of $X$.

A {\it permutation} of $[n]$ is sometimes referred to as a permutation of $n$. The set of all permutations of $n$ is denoted by $S_n$.
\end{definition}

\ex{\label{ex:perm3}Let $n=3$. Then there are $6=3!$ permutations of $n$, namely:
\[123,\ 132,\ 213,\ 231,\ 312,\ 321.\]
}

\ex{\label{ex:perm4}Let $n=4$. Then there are $24=4!$ permutations of $n$, namely:
\[1234,\ 1324,\ 2134,\ 2314,\ 3124,\ 3214, 1243,\ 1342,\ 2143,\ 2341,\ 3142,\ 3241, \]\[1423,\ 1432,\ 2413,\ 2431,\ 3412,\ 3421, 4123,\ 4132,\ 4213,\ 4231,\ 4312,\ 4321.\]
}

Now, in general, how many permutations of $n$ are there; that is, what is $|S_n|$? We have seen that for $n=3$ and $n=4$, $|S_n|=n!$. But is this true in general? Well, yes!

\begin{theorem}
Let $n\in\N$. Then $|S_n|=n!$.
\end{theorem}

We leave the proof of this theorem as an exercise. It can be proven by induction on $n$. Indeed, notice that in the list of permutations of 4 in Example \ref{ex:perm4}, if we take the 4s out of the listed permutations, we see exactly the permutations of 3 listed in Example \ref{ex:perm3}, 4 times over. For each permutation of 3, there are 4 places where we can insert the 4, so each permutation of 3 gives rise to 4 different permutations of 4. Generalizing this idea to arbitrary $n, n+1$ will yield the inductive step of the proof.

Now, for our second fundamental combinatorial object.

\begin{definition}
Let $X$ be a finite set. Define 
\[ {X\choose k} = \{A\subseteq X\ | \ |A|=k\}.\] We often read this as ``${X\choose k}$ is the set of all $k$-element subsets of $X$."
\end{definition}

\ex{Let $X=\{a, b, c\}$. Then we have
\[{X\choose 2} = \{\{a, b\}, \{a, c\}, \{b, c\}\}.\]
}

\ex{Let $X=[4]$. Then we have
\[ {X\choose 3} = \{\{1, 2, 3\}, \{1,2,4\},\{1,3,4\},\{2,3,4\}\}.\]
}

Of interest, of course, is how large ${X\choose k}$ is.

\begin{theorem}\label{T:choose}
Let $X$ be a finite set with $|X|=n$. For any $k$ with $0\leq k\leq n$, we have
\[\left|{X\choose k}\right| = \frac{n!}{k!(n-k)!}.\]
\end{theorem}

\begin{proof}
We work by induction on $n$. First, if $n=0$, we need only consider the case that $k=0$. Note that if $n=0$, then $X=\emptyset$, and $X$ has precisely one 0-element subset, namely $\emptyset$. Hence, it is immediate that $\left|{\emptyset\choose 0}\right|=1$. Moreover, recalling that $0!=1$ by definition, we have that $\frac{n!}{k!(n-k)!}=\frac{0!}{0!0!}=1$, and hence the result holds.

Now, suppose that for some $n\in\N$, it is known that if $X$ is a set of size $n$, then for any $k$ with $0\leq k\leq n$, we have $\left|{X\choose k}\right| = \frac{n!}{k!(n-k)!}$.

Suppose $|X|=n+1$. Notice that for $k=0$, the result is trivial. 

Let $1\leq k\leq n+1$. Fix an element $x\in X$. Let \[Y=\{A\subseteq X\ | \ |A|=k \hbox{ and }x\in A\},\] and let \[Z=\{A\subseteq X\ | \ |A|=k\hbox{ and }x\notin A\}.\] Notice that by definition, ${X\choose k} = Y\cup Z$, and $Y\cap Z=\emptyset$. Therefore, by Lemma 2 from \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes}, we have that $\left|{X\choose k}\right| = |Y|+|Z|$.

Define $X'=X\ba\{x\}$, so that $|X'|=n$. Now, define $f:Y\to {X'\choose k-1}$ by $f(A)=A\ba\{x\}$. Note that this function is well defined, since if $A\in Y$, then $A$ is a $k$-element subset of $X$ containing $x$, and thus removing $x$ from $A$ yields a $k-1$ element subset, all of whose elements must come from $X'$. We claim that $f$ is bijective. Indeed, for injectivity, suppose that $f(A)=f(B)$. Then $A\ba\{x\}=B\ba \{x\}$, and hence $A=(A\ba\{x\})\cup \{x\}=(B\ba\{x\})\cup\{x\}=B$, so $f$ is injective. Moreover, if $A'\in {X'\choose k-1}$, then $A'\cup\{x\}\in Y$, and moreover $f(A'\cup\{x\})=A'$. Hence, $f$ is a bijection, and by Theorem 1 in \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes}, we have that $|Y|=\left|{X'\choose k-1}\right|=\frac{n!}{(k-1)!(n-(k-1))!}$ by the inductive hypothesis.

Similarly, define $g:Z\to {X'\choose k}$ by $g(A)=A$. This is also a bijection; the proof of bijectivity is left as an exercise. Hence, by Theorem 1 in \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes}, we have that $|Z|=\left|{X'\choose k}\right|=\frac{n!}{(k)!(n-k)!}$ by the inductive hypothesis.

Putting this together, we have that 
\begin{eqnarray*}
\left|{X\choose k}\right| & = & |Y|+|Z|\\
& = & \frac{n!}{(k-1)!(n-(k-1))!}+\frac{n!}{(k)!(n-k)!}\\
& = & \frac{n!}{(k-1)!(n-k)!}\left(\frac{1}{n-k+1}+\frac{1}{k}\right)\\
& = & \frac{n!}{(k-1)!(n-k)!}\left(\frac{k}{k(n-k+1)}+\frac{n-k+1}{k(n-k+1)}\right)\\
& = & \frac{n!}{(k-1)!(n-k)!}\left(\frac{n+1}{k(n-k+1)}\right)\\
& = & \frac{(n+1)!}{k!(n+1-k)!},
\end{eqnarray*}
as desired.

Therefore, by induction, the result holds for all $n$ and for all $k$ with $0\leq k\leq n$.
\end{proof}

This quantity is important enough that it gets its own name.

\begin{definition}
For any $n$, and for any $0\leq k\leq n$, the quantity $\frac{n!}{k!(n-k)!}$ is denoted by ${n\choose k}$, and is referred to as a {\it binomial coefficient.}

If $k<0$ or $k>n$, we take ${n\choose k}=0$.
\end{definition}

This last part for binomial coefficients can be viewed through the lens of subsets. Indeed, we proved in Theorem \ref{T:choose} that ${n\choose k}$ is the number of $k$-element subsets of a set of size $n$. If $k<0$, there are no such subsets; what is a subset of $X$ of size $-1$? Likewise, if $k>n$, there are no such subsets. Hence, we take the convention that these binomial coefficients are 0.

In proving Theorem \ref{T:choose}, we incidentally proved a useful property of binomial coefficients, known as Pascal's Identity.

\begin{theorem}[Pascal's Identity]
Let $n, k\in\N$. Then 
\[{n+1\choose k}={n\choose k}+{n\choose k-1}.\]
\end{theorem}

Another useful, and obvious, property of binomial coefficients is their symmetry:

\begin{theorem}
Let $n, k\in\N$. Then
\[{n\choose k}={n\choose n-k}.\]
\end{theorem}

This Theorem barely deserves to be called a Theorem, since it is true by definition; if we simply commute the numbers in the denominator of the definition of the binomial coefficient, the theorem is proven.

Let's take a look at some counting examples using binomial coefficients.

\ex{In a standard deck of cards, there are 52 cards. How many different hands of 5 cards can be dealt to a poker player?

\begin{solution}
A 5-card hand can be seen as a 5-element subset of the 52 card deck. Hence, the question about how many different hands of cards can be dealt is the same as asking how many 5-element subsets a set of 52 elements has. By Theorem \ref{T:choose}, we have that there are
\[{52\choose 5} = \frac{52!}{5!47!} = \frac{52\cdot 51\cdot 50\cdot 49\cdot 48}{5!} = 2598960\]
possible hands.
\end{solution}
}

\ex{In a standard deck of cards, there are 52 cards and 4 suits: clubs, hearts, diamonds, and spades. Each suit appears on 13 different cards, and each card has exactly one suit.

How many 5-card hands can be dealt from the deck that do NOT have at least one card from each suit?

\begin{solution}
Let $Y$ denote the set of 5-card hands that do not have at least one card from each suit.

Define the following four sets:
\[X_c=\{5\hbox{-card hands that do not have a club}\}, X_h=\{5\hbox{-card hands that do not have a heart}\},\]
\[X_d=\{5\hbox{-card hands that do not have a diamond}\}, X_s=\{5\hbox{-card hands that do not have a spade}\}.\]

Note that $Y=X_c\cup X_h\cup X_d\cup X_s$, so we wish to determine $|X_c\cup X_h\cup X_d\cup X_s|$. We will use Theorem \ref{T:choose} and the Principle of Inclusion-Exclusion, as detailed in the previous notes.

First, note that if we wish to construct a hand with no clubs, we need only choose any 5 cards from the $52-13=39$ non-clubs in the deck. Hence, $|X_c|={39\choose 5}$. This is symmetric in each suit, so $|X_h|=|X_d|=|X_s|={39\choose 5}$ also. 

For Inclusion-Exclusion, we now consider the intersections. As with the above, the sizes of the intersections will be symmetric, regardless of what suits we consider.

Consider first $X_c\cap X_h$. This is the set of hands that do not have clubs or hearts in them. If we wish to exclude two suits, say, clubs and hearts, we must choose our hand from the $52-26=26$ cards in the deck that are neither clubs nor hearts. Hence, $|X_c\cap X_h|={26\choose 5}$.

Now consider $X_c\cap X_h\cap X_d$. This is the set of hands that do not have clubs, hearts, or diamonds, so they are chosen exclusively from the spades. Hence, $|X_c\cap X_h\cap X_d|={13\choose 5}$.

Finally, consider $X_c\cap X_h\cap X_d\cap X_s$. This is the set of hands that do not have clubs, hearts, diamonds, or spades. No hands can be like this! 

Putting these pieces together with Inclusion-Exclusion, we have that 
\begin{eqnarray*}
|Y| &=& |X_c\cup X_h\cup X_d\cup X_s|\\
& = & |X_c|+|X_h|+|X_d|+|X_s|-|X_c\cap X_h|-|X_c\cap X_d|-|X_c\cap X_s|-|X_h\cap X_d|-|X_h\cap X_s|\\
&&-|X_d\cap X_s|+|X_c\cap X_h\cap X_d|+|X_c\cap X_h\cap X_s|+|X_c\cap X_d\cap X_s|+|X_h\cap X_d\cap X_s|\\
&&-|X_c\cap X_h\cap X_d\cap X_s|\\
& = & 4{39\choose 5}-6{26\choose 5}+4{13\choose 5}-0\\
& = & 1913496.
\end{eqnarray*}
\end{solution}
}

As perhaps an aside, but an important one, we note that the binomial coefficients can show up not just in combinatorial counting, but in other contexts as well. A very important theorem using binomial coefficients (and from whence comes their name) is the Binomial Theorem, below, whose proof is a homework exercise.

\begin{theorem}[Binomial Theorem]\label{T:binom}
Let $x, y\in\R$, and $n\in\N$. Then
\[(x+y)^n = \sum_{k=0}^n {n\choose k}x^ky^{n-k}.\]
\end{theorem}

The proof of this theorem can be done using induction on $n$, together with Pascal's Identity. 

Now, the proof of Theorem \ref{T:choose} relied upon breaking the set we wish to count into two sets, which were disjoint. This allowed us to use Lemma 2 from the \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes}. In general, this strategy of counting a set by considering disjoint subsets is fairly useful, as we shall see. As such, we have some fancy names for these kinds of things.

\begin{definition}
Let $X$ be a set. A {\it pairwise disjoint finite partition} of $X$ (usually referred to simply as a {\it finite partition}, or even more simply as a {\it partition}) is a collection of sets $\{Y_1, Y_2, \dots, Y_k\}$ such that
\begin{itemize}
\item $Y_i\subseteq X$ for all $1\leq i\leq k$, 
\item The collection of sets $\{Y_1, Y_2, \dots, Y_k\}$ are pairwise disjoint (see HW6, problem 5), and 
\item $\displaystyle\bigcup_{i=1}^k Y_i = X$.
\end{itemize}
The members of the partition are referred to as the {\it blocks} of the partition.
\end{definition}

What we have seen already in Theorem \ref{T:choose} and in Lemma 2 from the \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes} is an example of a finite partition; in particular, we have used finite partitions containing exactly two subsets. In general, however, this strategy can be extended: in order to count a set, we can partition the set into subsets that we already know how to count, and then add them up. In order to formally use this theorem, we have the following extension of Lemma 2 from the \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes}, whose proof is left as an exercise but can easily be done by induction on $k$.

\begin{theorem}\label{T:partition}
Let $X$ be a set. If $\{Y_1, Y_2, \dots, Y_k\}$ is a finite partition of $X$, then $|X|=\displaystyle\sum_{i=1}^k |Y_i|$.
\end{theorem}

\ex{\label{ex:functions}Let $n, m\in\N$. How many functions are there from $[n]$ to $[m]$?

\begin{solution}
In order to solve this problem, we need to do two things. First, we need to determine what we think the answer is, then prove that the answer we came up with is correct.

To do those things, let's analyze the problem a little. Because we're working with $n, m\in \N$, there is often the possibility that we might work by induction. Indeed, there is an apparent inductive structure available here: if we have a function $f:[n]\to[m]$, then $f|_{[n-1]}$ induces a function from $[n-1]$ to $[m]$. So we could consider partitioning the set of functions based upon how they look at the last element. 

Ok, let's put some notation on this. Let $X_{n, m} = \{f:[n]\to[m]\}$, so our goal here is to determine $|X_{n, m}|$. For $1\leq k\leq m$, define $Y_k = \{f\in X_{n, m}\ | \ f(n)=k\}$. Notice that each element in $Y_k$ is completely characterized by its values on $[n-1]$, since each element in $Y_k$ has the same value on $n$. Hence, we have that for $1\leq k\leq m$, $Y_k$ is in bijection with $X_{n-1, m}$. 

Moreover, for each $f\in X_{n, m}$ we have that $f(n)=k$ for some $k\in[m]$ by definition, so $f\in Y_k$ for some $k\in [m]$. Since $Y_k\subseteq X_{n, m}$ by definition, we thus obtain that $X_{n, m} = \cup_{k=1}^m Y_k$. Moreover, it is plain to see, by definition, that the $Y_k$ are pairwise disjoint.

Therefore, by Theorem \ref{T:partition} and the observation that $Y_k$ is in bijection with $X_{n-1, m}$, \begin{equation}\label{E:recursion}|X_{n, m}| = \sum_{k=1}^m |Y_k|=\sum_{k=1}^m |X_{n-1, m}| = m|X_{n-1, m}|.\end{equation}

Finally, we observe that $|X_{1, m}|=m$. This is clear, since we are looking for a function into $[m]$ that only maps 1 element. There are $m$ choices for the image of 1, and hence there are $m$ functions.

Ok, so if $|X_{1, m}|=m$, and every time we increase $n$ by 1 we multiply by a factor of $m$, as shown in Equation \eqref{E:recursion}, it seems that $|X_{n, m}| = m^n$. The formal proof follows.

\begin{claim}
The number of functions from $[n]$ to $[m]$ is $m^n$.
\end{claim}

\begin{proof}
We work by induction on $n$. Fix $m\in\N$.

In the case that $n=1$, the result is immediate: there are $m$ different functions that map $1$ to $[m]$.

Suppose the result is known for some $n-1\in\N$. 

Consider the case of functions from $[n]$ to $[m]$. By partitioning functions into $\{Y_1, Y_2, \dots, Y_m\}$ as detailed above, we observe that the number of functions from $[n]$ to $[m]$ is $m$ times the number of functions from $[n-1]$ to $[m]$. By the inductive hypothesis, then, there are $m\cdot m^{n-1}=m^n$ functions from $[n]$ to $[m]$.

Hence, by induction, the result holds for all $n$.
\end{proof}
\end{solution}
}

Perhaps you are reading this proof thinking, well, that seems dumb. Why don't we just note that there are $m$ places we could map the number 1, and then there are $m$ places we could map the number 2, etc., so that in total we have $n$ choices to make, all of which have $m$ outcomes? Yeah, you're totally right, that would be really a lot more efficient than the nonsense above. The problem is, how do you justify that you are allowed to analyze the situation in this way? That is, how do you know that when you make a bunch of choices, you can just multiply together the number of outcomes of those choices and everything is cool?

Well, you don't. Until now.

\begin{theorem}\label{T:product}Let $X_1, X_2, \dots, X_n$ be finite sets. Then
\[ |X_1\times X_2\times \dots\times X_n| = |X_1||X_2|\dots |X_n|.\]
\end{theorem}

To be sure this is clear, let's just be very careful that we understand what is meant by $X_1\times X_2\times \dots\times X_n$. Recall that $X_1\times X_2$ is just the set of ordered pairs, having the first element from $X_1$ and the second element from $X_2$. Here, $X_1\times X_2\times \dots\times X_n$ is the set of ordered $n$-tuples, having the $k^{\textrm{th}}$ element from $X_k$ for each $k\in[n]$.

We omit the proof of this theorem, leaving it as an exercise if you are interested. Again, the proof can proceed by induction on $n$, using Theorem 6 from  the \href{http://math.cmu.edu/~mradclif/teaching/127S19/Notes/Finite%20Cardinality.pdf}{Finite Cardinality Notes} as the base case. 

So, how does this apply to the previous example? Let's work it again, using this new theorem.

\addtocounter{example}{-1}
\ex{\textbf{Revisited.} Let $n, m\in\N$. How many functions are there from $[n]$ to $[m]$?

\begin{solution}
Let $X_{n, m}$ denote the set of functions from $[n]\to [m]$. We claim that $X_{n, m}$ is in bijection with the $n$-fold product $[m]\times [m]\times\dots\times[m]$.

Indeed, define a function $H:[m]\times [m]\times\dots\times[m]\to X_{n, m}$ by $h(k_1, k_2, \dots, k_n)=f$, where $f(i)=k_i$ for each $i$. That is to say, $f(1)$ is $k_1$, $f(2)$ is $k_2$, etc. This is performing the role of ``making a choice'' for each element in $[n]$; the choice is represented by $k_i$. It is plain to see that $h$ is a bijection; every function $f:[n]\to [m]$ can be uniquely represented by specifying its function values in an ordered $n$-tuple.

Therefore, $|X_{n, m}|=|[m]\times [m]\times\dots\times[m]|=m^n$ by Theorem \ref{T:product}.
\end{solution}
}

In general, when using this theorem, you need not appeal so directly to the theorem. It's ok just to think about how a count can be structured using independent choices. Consider:

\ex{At Pinkberry Frozen Yogurt, there are 36 flavors of frozen yogurt, 6 types of fruit toppings, 12 kinds of chocolate and candy toppings, and 5 kinds of sauce toppings. (It's true. I checked \href{http://www.pinkberry.com/menu/}{their menu}.) How many different yogurt cups can you make that have two kinds of yogurt, one kind of fruit topping, two kinds of candy topping, and one kind of sauce topping?

\begin{solution}
Let's construct our yogurt cup, one piece at a time.

If we want two kinds of yogurt, and there are 36 kinds available, the number of ways to select yogurt is exactly ${36\choose 2}=630$. Think of this as picking two yogurts out of the set of 36.

If we want one kind of fruit, and there are 6 kinds available, there are 6 ways to choose fruit.

If we want two kinds of candy, and there are 12 kinds available, there are exactly ${12\choose 2}=66$ ways to choose candy.

If we want one kind of sauce, and there are 5 kinds available, there are exactly 5 ways to choose sauce.

Altogether, this makes $630\cdot 6\cdot 66\cdot 5=1247400$ different yogurt cups.
\end{solution}
}

This solution uses the result of Theorem \ref{T:product} without explicitly appealing to it.

\section{Counting in Two Ways}

Counting in two ways is a technique that is used for problems that, on their face, are not actually about counting. In general, counting in two ways is used to prove identities (in our case, often involving binomial coefficients). Before we outline the procedure in general, let's look at an example to understand what we're talking about.

\ex{\label{ex:ctw}Prove that for all $n, k, \ell\in\N$, ${n\choose k}{k\choose \ell}={n\choose \ell}{n-\ell\choose k-\ell}$.

\begin{proof}
Suppose we have $n$ people standing in a line. To $k$ of those people, we will hand out flags. There are $\ell$ blue flags and $k-\ell$ red flags. Let us count the number of ways we can distribute the flags in two different ways.

First, we could select all the people that we wish to hand flags to. This can be done in ${n\choose k}$ ways. Then, among the $k$ chosen people, we select $\ell$ people to hand blue flags to. The rest get red flags. This can be done in ${k\choose \ell}$ ways. Therefore, there are ${n\choose k}{k\choose\ell}$ different ways to pass out the flags.

Counting in another way, we could first select $\ell$ people to hold blue flags. This can be done in ${n\choose \ell}$ ways. From the remaining $n-\ell$ people, we can then choose $k-\ell$ people to hold red flags. This can be done in ${n-\ell\choose k-\ell}$ ways. Therefore, there are ${n\choose \ell}{n-\ell\choose k-\ell}$ different ways to pass out the flags.

As both ${n\choose k}{k\choose\ell}$ and ${n\choose \ell}{n-\ell\choose k-\ell}$ are the number of different ways to pass out the flags, it must be the case that ${n\choose k}{k\choose \ell}={n\choose \ell}{n-\ell\choose k-\ell}$.
\end{proof}
}

Now, certainly the above problem could have been done using algebra, but there are also cases where a direct algebra approach to these types of problems is challenging.

In general, our structure for counting in two ways problems looks as follows.

\begin{gptbox}
\textbf{Counting In Two Ways}.
Type of problem: Prove $A=B$, where $A$ and $B$ are combinatorial expressions.

Solution structure:
\begin{enumerate}
\item Find a set $X$ having $|X|=A$.
\item By counting in a different way, show that $|X|=B$.
\item Conclude that $A=B$.
\end{enumerate}
\end{gptbox}
In Example \ref{ex:ctw}, the set $X$ in question is the set 
\[X=\{\hbox{ways to distribute $\ell$ blue flags and $k-\ell$ red flags to $n$ people}\}.\]
This set was chosen because it fit the problem. The thinking that you might go through to select the set probably will involve analyzing one of $A$ or $B$. In Example \ref{ex:ctw}, you can look at $A$ and think like this. First, take a set of $n$ people, and choose $k$ of them. Then, among those $k$ chosen people, designate $\ell$ of them to be different. For ease of visualization, we can think of ``choosing'' people as handing them a flag. Anyone chosen gets to hold the flag!! Then, among the flag holders, $\ell$ of them are different, so we can represent that difference with two kinds of flags.

Ultimately, this is the tricky part of counting in two ways: finding a set $X$ that will work for the given problem.

Let's take a look at some more examples.

\ex{\label{ex:2n}Prove, by counting in two ways, that $2^n=\displaystyle\sum_{k=0}^n {n\choose k}$.

Note: we could quickly prove this result by using the Binomial Theorem with $x=y=1$. Let's unquickly prove it, though, as an example of counting in two ways.

\begin{proof}
Let $X=\mathcal{P}([n])$, the set of all subsets of $[n]$. Let us consider $|X|$.

For a given subset of $[n]$, each number from $[n]$ is either included or not included. Therefore, there are two choices to be made for each number. Hence, we have $2^n$ total number of ways to construct a subset of $[n]$, so $|X|=2^n$.

On the other hand, by Theorem \ref{T:choose}, there are ${n\choose k}$ subsets of $[n]$ of size $k$. Moreover, every subset of $[n]$ has size from 0 to $n$, and hence the collection $\left\{{[n]\choose 0}, {[n]\choose 1}, \dots, {[n]\choose n}\right\}$ is a finite partition of $\mathcal{P}([n])$. By Theorem \ref{T:partition}, then, we have 
\[|X|=\sum_{k=0}^n \left|{[n]\choose k}\right| = \sum_{k=0}^n {n\choose k}.\]

Therefore, we have $2^n=\displaystyle\sum_{k=0}^n {n\choose k}.$
\end{proof}
}

\ex{Let $n\in\N$. Prove that $\sum_{k=0}^n k{n\choose k} = n2^{n-1}$.

\begin{proof}
Suppose we have $n$ people, and we wish to choose a committee having a president. We count the number of ways to do this in two ways.

First, we could designate the members of the committee, and then among those members, select a president. For each $k$, forming a committee of size $k$, and then designate one of the $k$ people to be the president can be done in $k{n\choose k}$ ways. Since the division of committees by size is clearly a finite partition of the division into committees, we thus have that the number of ways to assign the committee with president is $\sum_{k=0}^n k{n\choose k}$.

On the other hand, we could first designate the president. This can be done in $n$ ways. From the remaining $n-1$ people, each person is either in or out of the committee, so there are two choices for each person. Hence, there are $2^{n-1}$ ways to pick the non-president once the president has been chosen. Therefore, the number of ways to assign the committee with president is $n2^{n-1}$.

Taken together, we thus have $\sum_{k=0}^n k{n\choose k}=n2^{n-1}$.
\end{proof}
}


\section{Counting by Bijection}
Recall, from previous notes, that we developed the following theorem:
\vspace{-.1in}
\[\hbox{If $X$ and $Y$ are finite sets, then $|X|=|Y|$ if and only if there is a bijection $f:X\to Y$.}\]

Counting by bijection leverages this idea to count difficult sets by constructing a bijection to a more convenient set. Let's take a look at some examples.

\ex{Let $X$ be a finite set. Determine $|\P(X)|$.

We note that we could proceed, on this problem, using the result of Example \ref{ex:2n}, and conclude that $|\P(X)|=2^{|X|}$. However, let's illustrate this using a bijection.

\begin{solution}
Let $\mathcal{S}=\{f:X\to\{0,1\}\}$, the set of functions from $X$ to $\{0,1\}$. Using Example \ref{ex:functions}, we know that $|\mathcal{S}|=2^{|X|}$. Let's demonstrate a bijection $F :\P(X)\to\mathcal{S}$.

Given $A\in\P(X)$, take $F(A)\in\mathcal{S}$ to be the function $f_A$, defined by 
\[f_A(x)=\left\{\begin{array}{ll} 1 & x\in A\\ 0 & x\notin A\end{array}\right. .\]
Let's prove that $F$ is a bijection.

{\bf Injectivity:} Let $A, B\in \P(X)$ with $A\neq B$. Then there exists some $x\in X$ such that $x$ is in exactly one of $A, B$; wolog, say $x\in A$ and $x\notin B$. Then $f_A(x)=1$, but $f_B(x)=0$. Therefore, $f_A\neq f_B$.

{\bf Surjectivity:} Let $f:X\to \{0, 1\}$ be a function. Let $A=\inv f(1)$. Then we have $f_A(x)=1$ iff $x\in \inv f(1)$ iff $f(x)=1$, and hence $f_A\equiv f$. Therefore, $F(A)=f$, and thus $F$ is surjective.

Since $F$ is a bijection, we thus know that $|\mathcal{S}|=|\P(X)|$, and hence $|\P(X)|=2^{|X|}$.
\end{solution}
}

\ex{How many 5-digit sequences can be made with the numbers $\{1, 1, 1, 2, 5\}$?

\begin{solution}
Note that a sequence in these digits is completely determined by the position of $2$ and $5$. For example, if you know that 2 is in the $4^{\textrm{th}}$ position and 5 is in the $2^{\textrm{nd}}$ position, you know that the sequence is $15121$. 

Let $\mathcal{S}$ be the set of 5-digit sequences that can be made with the numbers $\{1, 1, 1, 2, 5\}$, and let $X$ be the set of ordered pairs $(a, b)$ such that $a, b\in[5]$ and $a\neq b$. That is to say, 
\[X=[5]\times [5]\ \ba\ \{(a,b)\in [5]\times [5]\ | \ a=b\}.\]
Now, $|X|=5\cdot 5 - 5=20$. 

Let $F:\mathcal{S}\to X$ be defined by $F(s) = (a, b)$, where $a$ is the position of 2 in the string $s$ and $b$ is the position of 5 in the string $s$. This is clearly a bijection (if you don't believe me, prove it!) and hence $|\mathcal{S}|=|X|$. Therefore, there are 20 such strings.
\end{solution}
}

This solution is much nicer than trying to count the strings using inclusion-exclusion. 
\end{document}