\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{latexsym}
\usepackage{verbatim}
\usepackage[pdftex]{graphicx}
\usepackage{wasysym}
\usepackage{url}
\usepackage{mathrsfs}
\usepackage{cancel}
\usepackage{hyperref}

\hypersetup{colorlinks=true}
\setlength{\oddsidemargin}{.25in}
\setlength{\evensidemargin}{.25in}
\setlength{\textwidth}{6in}
\setlength{\topmargin}{-0.4in}
\setlength{\textheight}{8.5in}

\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\input{preamble.tex}

\begin{document}

\lecture{3: Inclusion-Exclusion}{Week 4}

These notes are shorter than normal, because we used Monday's class to collectively re-evaluate the course!

\section{Inclusion-Exclusion}

A problem many of you have seen before is the following:
\begin{question}
Suppose you have a finite set $X$, along with three subsets $A, B, C$ of $X$.  Can you express the size of the set $\displaystyle X \setminus \left( A \cup B \cup C\right)$ in terms of the sizes of the sets $A, B, C, X$ and their intersections?
\end{question}
\begin{answer}

This is relatively easy:\ we claim that
\begin{align*}
\left|X \setminus \left( A \cup B \cup C\right)\right| = |X| - |A| - |B| - |C| + |A \cap B| + |A \cap C| + |B \cap C| - |A \cap B \cap C|.
\end{align*}

This is not too hard to see; take any $x \in X$, and consider the following two cases.
\begin{enumerate}
\item $x$ is not in $A \cup B \cup C$.  In this case, then $x$ is ``counted'' once in the right sum: it contributes 1 to the $|X|$ count, and never shows up in any of the other terms, as it is not in any of $A,B,C$ or their intersections.
\item $x$ is in one of $A, B ,C$, but not two of these sets.  Then $x$ contributes 1 to the $|X|$ count, $-1$ to whichever of the sets $A,B,C$ $x$ is in, and never shows up in any of the other sets (as it is not in any of the intersections.)  So $x$ contributes a 0 in total.
\item $x$ is in two of $A, B ,C$, but not all three of these sets.  Then $x$ contributes 1 to the $|X|$ count, $2 \cdot (-1)$ from the two sets $A,B,C$ that $x$ is in, a 1 from whichever pairwise intersection $x$ is in, and never shows up in any of the other two intersections or the three-way intersection.  So $x$ contributes a 0 in total again!
\item Finally, $x$ could be in all of $A, B, C$.  Then $x$ contributes a 1 to the $|X|$ count, $3 \cdot (-1)$ from the three $|A|, |B|, |C|$ counts, $3 \cdot (+1)$ from the three pairwise intersections $|A \cap B| , |A \cap C| , |B \cap C|$, and $-1$ from the $|A \cap B \cap C|$ term.  Again, this $x$ contributes a 0 in total.
\end{enumerate}

So, the right hand sum adds up a 1 for every $x \in X \setminus \left( A \cup B \cup C\right)$ and a 0 for every other $x$; in other words, the right hand side gives us the size of $X \setminus \left( A \cup B \cup C\right)$, as claimed!
\end{answer}

We can generalize this as follows:
\begin{question}
Suppose you have a finite set $X$, along with $n$ subsets $A_1, A_2, \ldots A_n$ of $X$.  Can you express the size of the set $\displaystyle X \setminus \left( A_1 \cup A_2 \cup \ldots \cup A_n \right)$ in terms of the sizes of the sets $X, A_1, \ldots A_n$ and their intersections?
\end{question}
\begin{answer}
We do this with the same methods as above, once we build up some notation.  First, for any $k$, let the sum
\begin{align*}
\sum_{1 \leq i_1 < i_2 < \ldots < i_k \leq n} |A_{i_1} \cap A_{i_2} \cap \ldots \cap A_{i_k}|
\end{align*}
denote the sum over all sets of $k$ integers $i_1 < \ldots <i_k$ of the size $|A_{i_1} \cap A_{i_2} \cap \ldots \cap A_{i_k}|$.

This way to index a sum may look odd, but it's describing fairly intuitive; what it's trying to capture is ``let's count all of the $k$-size subsets of $\{A_1, \ldots A_n\}$ exactly once.'' For instance, let's consider what happens for $n=4, k=2$; then our sum is
\begin{align*}
\sum_{1 \leq i_1 < i_2 \leq 4} |A_{i_1} \cap A_{i_2}| = |A_1 \cap A_2| + |A_1 \cap A_3| + |A_1 \cap A_4| + |A_2 \cap A_3| + |A_2 \cap A_4| + |A_3 \cap A_4|.
\end{align*}
Similarly, if $n=5, k=4$, then our sum is 
\begin{align*}
\sum_{1 \leq i_1 < i_2 < i_3 < i_4 \leq 5} |A_{i_1} \cap A_{i_2} \cap A_{i_3} \cap A_{i_4}| = &|A_1 \cap A_2 \cap A_3 \cap A_4| + |A_1 \cap A_2 \cap A_3 \cap A_5|\\
+&|A_1 \cap A_2 \cap A_4 \cap A_5| + |A_1 \cap A_3 \cap A_4 \cap A_5|\\
+&|A_2 \cap A_3 \cap A_4 \cap A_5|. \\
\end{align*}

With this said, I claim that our result from our earlier question generalizes as follows: 
\begin{align*}
\left|X \setminus \left( \bigcup_{k=1}^n A_k\right)\right| &= |X| + \sum_{k=1}^n \left( (-1)^k \cdot\sum_{1 \leq i_1 < \ldots < i_k \leq n}   |A_{i_1} \cap \ldots \cap A_{i_k}| \right),\\
\end{align*}
or, if we expand the sum at right,
\begin{align*} 
\left|X \setminus \left( \bigcup_{k=1}^n A_k\right)\right| &= |X| - \left( \sum_{1 \leq i_1 \leq n}   |A_{i_1}| \right) +  \left( \sum_{1 \leq i_1 < i_2 \leq n}   |A_{i_1} \cap  A_{i_2}| \right)\\
&\hphantom{= |X| ~} - \left( \sum_{1 \leq i_1 <i_2 < i_3\leq n}   |A_{i_1} \cap A_{i_2} \cap A_{i_3}| \right) +  \left( \sum_{1 \leq i_1 < i_2 <  i_3 < i_4 \leq n}   |A_{i_1} \cap A_{i_2} \cap A_{i_3} \cap A_{i_4}| \right)\\
&\hphantom{= |X| ~} - \ldots
\end{align*}

Our proof goes in a similar fashion to before:
\begin{enumerate}
\item $x$ is not in $ \bigcup_{k=1}^n A_k$.  In this case, then $x$ is ``counted'' once in the right sum: it contributes 1 to the $|X|$ count, and never shows up in any of the other terms, as it is not in any of $A,B,C$ or their intersections.
\item $x$ is in $l$ of the $A_i$ sets: to be specific, let's say that $x \in A_{i_1}, \ldots A_{i_l}$ for some indices $i_1 < \ldots < i_l$, and that $x$ is not in any of the other $A_j$ sets.

In this setting, then $x$ shows up in the following:
\begin{itemize}
\item $x$ contributes a 1 to the $|X|$ term's size.
\item $x$ is in $l$ of the $A_i$ terms, and thus contributes $(-1) \cdot l$ from the $\displaystyle \sum_{1 \leq i_1 \leq n}   |A_{i_1}|$ terms.
\item $x$ is in $\binom{l}{2}$ of the $A_i \cap A_j$ terms, because it's in $l$ sets total and there are $\binom{l}{2}$ many ways to pick out pairs of these sets to intersect.  Therefore, it contributes $(+1) \cdot \binom{l}{2}$ from the $\displaystyle\sum_{1 \leq i_1 < i_2 \leq n}   |A_{i_1} \cap  A_{i_2}| $ terms.
\item Similarly, $x$ is in $\binom{l}{3}$ of the $A_i \cap A_j \cap A_k$ terms, because it's in $l$ sets total and there are $\binom{l}{3}$ many ways to pick out triples of these sets to intersect.  Therefore, it contributes $(-1) \cdot \binom{l}{3}$ from the $ \displaystyle\sum_{1 \leq i_1 <i_2 < i_3\leq n}   |A_{i_1} \cap A_{i_2} \cap A_{i_3}|$ terms.
\item \ldots
\item In general, for any $k \leq l$, $x$ is in $\binom{l}{k}$ of the $A_{i_1} \cap \ldots \cap A_{i_k}$ terms, as it is in $l$ sets total and there are $\binom{l}{k}$ many ways to pick out $k$ sets from the $l$ sets in total.  Therefore, in total, it contributes $(-1)^k \cdot \binom{l}{k}$ from the $\displaystyle(-1)^k \cdot\sum_{1 \leq i_1 < \ldots < i_k \leq n}   |A_{i_1} \cap \ldots \cap A_{i_k}|$ terms.
\end{itemize}
So, in total, $x$ contributes
\begin{align*}
& 1 - l + \binom{l}{2} - \binom{l}{3} + \ldots + (-1)^k\binom{l}{k}\\
=& \sum_{k=0}^l \binom{l}{k} (-1)^k
\end{align*}
to the right-hand side.

If we remember the binomial theorem, which says that $\displaystyle (x+y)^l = \sum_{k=0}^l \binom{l}{k} x^ky^{l-k}$, and plug in $x = -1, y = 1$, we get
\begin{align*}
(-1 + 1)^l =  \sum_{k=0}^l \binom{l}{k} (-1)^k.
\end{align*}
But $(-1 + 1)^l = 0$; so we actually have that $x$ contributes 0 to the sum!
\end{enumerate}

So, our sum adds up a 1 for every $x \in X \setminus \left(\bigcup_{k=1}^n A_k \right)$ and a 0 for every other $x$; in other words, the right hand side gives us the size of $X \setminus \left(\bigcup_{k=1}^n A_k\right)$, as claimed!
\end{answer}

Roughly speaking, our strategy here was the following:
\begin{itemize}
\item  To find the size of the union of a bunch of sets, we first added up the size of all of these sets; this overcounts, however, because elements in more than one set get counted multiple times!
\item To correct for this overcount, we then ``fix'' things by subtracting off the size of all of our pairwise intersections.  But this results in an undercount, as elements in more than two sets get counted by these pairwise intersections too many times!
\item To correct for \textbf{this} undercount, we then add in the size of all of our triple-intersections; but this too results in an overcount \ldots
\item But if we keep doing this, at the end we will have (magically) counted everything correctly!
\end{itemize} 

This idea is an incredibly useful one --- to get an exact count, we can just use ``at least'' counts and repeatedly add and subtract off errors to get the right result at the end!  It also should sound familiar, because this is basically exactly why we invented the sieve method!  

In fact, we can use sieves to answer our problem above efficiently:
\begin{answer}
Let
\begin{itemize}
\item $\Omega = $ a set $X$, 
\item $A_1, \ldots A_n$ be a collection of subsets of $X$, and
\item $P = \{P_1, \ldots P_n\}$, where an element $x \in X$ satisfies property $p_i$ if and only if $x \in A_i$.
\end{itemize}
For any $S \subset P$, let $A(S)$ denote the number of elements of $X$ that satisfy all of the properties of $S$ (and perhaps others.)  Then, if $S = \{p_{i_1}, \ldots p_{i_k}\}$, we have that
\begin{align*}
A(S) = |A_{i_1} \cap \ldots \cap A_{i_k} |,
\end{align*}
as saying that an element satisfies $p_{i_1}, \ldots p_{i_k}$ just means that it's in every one of the sets $A_{i_1} \cap \ldots \cap A_{i_k}$.

In the last week's notes, we defined 
\begin{align*}
n_r = \sum_{|S| = r} A(S), \qquad N(x) = \sum_{r=0}^\infty n_r x^r,
\end{align*}
and showed that if
\begin{align*}
e_t = \textrm{number of elements with }t\textrm{ elements}, \qquad E(x) = \sum_{t=0}^\infty e_t x^t,
\end{align*}
then $N(x-1) = E(x)$.


In our problem above, we were interested in the size of the set $\displaystyle X \setminus \left( A_1 \cup A_2 \cup \ldots \cup A_n \right)$; in other words, we wanted the number of elements in $X$ that have precisely 0 properties, as these are all of the elements in $X$ that are not in any of the $A_i$'s!

On one hand, we know that $e_0$ is this quantity by definition.  On the other, we know that $e_0 = E(0)$ because plugging in 0 to any power series kills off all of its non-constant terms and leaves only the constant term; therefore, because $E(0) = N(-1)$, we can use the fact that for any $1 \leq r \leq n$, we have
\begin{align*}
n_r = \sum_{S \subseteq P, |S| = r} A(S) = \sum_{1 \leq i_1 < \ldots < i_r \leq n}  |A_{i_1} \cap \ldots \cap A_{i_r} |,
\end{align*}
while $n_0 = |X|$ (because every element has at least no properties) and $n_{r} = 0$ for $r > n$ (because it is impossible to have more than $n$ properties.)

Therefore, 
\begin{align*}
N(x)  = |X| + \sum_{r=1}^n  x^r \left( \sum_{1 \leq i_1 < \ldots < i_r \leq n}  |A_{i_1} \cap \ldots \cap A_{i_r} |\right) 
\end{align*}
and therefore that
\begin{align*}
e_0 = E(0) = N(-1)  = |X| + \sum_{r=1}^n  (-1)^r \left( \sum_{1 \leq i_1 < \ldots < i_r \leq n}  |A_{i_1} \cap \ldots \cap A_{i_r} |\right).
\end{align*}

This is our answer from before; however, unlike before we didn't have to do any clever counting tricks!  This is the power of generating functions; there are methods here that ``automagically'' perform tricks that otherwise require lots of cleverness!  
\end{answer}

We give a second example of the use of sieves to answer inclusion-exclusion flavored problems here:
\begin{question}
For any two natural numbers $n, d $, we say that $n$ and $d$ are \textbf{relatively prime} if $gcd(n,d) = 1$; that is, if $n, d$ have no common factors.

For any $n \in \mathbb{N}$, let $\varphi(n)$ denote the \textbf{Euler phi function} that sends $n$ to the number of positive integers $d$ such that $d$ is relatively prime to $n$.  For example, $\varphi(6) = 2,$ as $\{1, 5\}$ are the two numbers in $\{1,2,3,4,5,6\}$ that are relatively prime to $6$.  Similarly, $\varphi(p) = p-1$ for any prime $p$, as no number less than $p$ has a nontrivial factor in common with $p$.

What is a nice formula for $\varphi(n)$ in terms of $n$?
\end{question}
\begin{answer}
We answer this with sieves.  Write $n = p_1^{k_1} \cdot \ldots \cdot p_l^{k_l}$, for distinct prime numbers $p_1, \ldots p_l$.  Set
\begin{itemize}
\item $\Omega = \{1, 2, \ldots n\}$, and
\item $P = \{\rho_1, \ldots \rho_n\}$, where an element $d$ satisfies property $\rho_i$ if and only if $p_i$ is a factor of $d$.
\end{itemize}
As before, we are interested in the number of elements in $\{1, \ldots n\}$ with \textbf{no} properties, as an element has no properties if and only if it has no factors in common with $n$ --- that is, if it is relatively prime to $n$.

Again, we start by calculating $A(S)$; I claim that if $S = \{\rho_{i_1}, \ldots \rho_{i_k}\}$, we have that
\begin{align*}
A(S) = \frac{n}{p_{i_1}\cdot p_{i_2} \cdot \ldots \cdot p_{i_k}}.
\end{align*}
This is because satisfying a property $\rho_i$ is the same thing as being a multiple of $p_i$; therefore, the number of things that satisfy properties  $\{\rho_{i_1}, \ldots \rho_{i_k}\}$ is just the collection of all multiples of $p_{i_1}\cdot p_{i_2} \cdot \ldots \cdot p_{i_k}$.  There are precisely $\frac{n}{p_{i_1} \cdot p_{i_k}}$ of these elements in $\{1,2,\ldots n\}$ (think about this if you don't see why,) which justifies our claim!

Therefore, we have that for any $1 \leq r \leq n$, we have
\begin{align*}
n_r = \sum_{S \subseteq P, |S| = r} A(S) = \sum_{\{\rho_{i_1}, \ldots \rho_{i_r}\} \subseteq P}  \frac{n}{p_{i_1}\cdot p_{i_2} \cdot \ldots \cdot p_{i_k}},
\end{align*}
while $n_0 = n$ (because every element has at least no properties) and $n_{r} = 0$ for $r > n$ (because it is impossible to have more than $n$ properties.)

Therefore, 
\begin{align*}
N(x)  = n + \sum_{r=1}^n  x^r \left( \sum_{\{\rho_{i_1}, \ldots \rho_{i_r}\} \subseteq P}  \frac{n}{p_{i_1}\cdot p_{i_2} \cdot \ldots \cdot p_{i_k}}\right),
\end{align*}
and therefore that
\begin{align*}
e_0 = E(0) = N(-1)  = n+ \sum_{r=1}^n  (-1)^r \left( \sum_{\{\rho_{i_1}, \ldots \rho_{i_r}\} \subseteq P}  \frac{n}{p_{i_1}\cdot p_{i_2} \cdot \ldots \cdot p_{i_k}}\right).
\end{align*}

Some clever factoring on the right hand side gives us
\begin{align*}
&n+ \sum_{r=1}^n  (-1)^r \left( \sum_{\{\rho_{i_1}, \ldots \rho_{i_r}\} \subseteq P}  \frac{n}{p_{i_1}\cdot p_{i_2} \cdot \ldots \cdot p_{i_k}}\right) \\
=& n \left( 1 - \left( \sum_{1 \leq i_1 \leq n} \frac{n}{p_i}\right) + \left( \sum_{1 \leq i_1 < i_2 \leq n} \frac{n}{p_{i_1}p_{i_2}}\right) - \left( \sum_{1 \leq i_1 < i_2 < i_3 \leq n} \frac{n}{p_{i_1}p_{i_2}p_{i_3}}\right) + \ldots \right)\\
=& n \cdot \prod_{i=1}^n \left(1 - \frac{1}{p_i} \right).
\end{align*}

In other words, we've proven that for any $n$, the Euler $\varphi$-function has the closed form
\begin{align*}
\varphi(n) = n \cdot \prod_{i=1}^n \left(1 - \frac{1}{p_i} \right).
\end{align*}
Cool!

To test it out, let's try finding $\varphi(35)$.  On one hand, we can see that
\begin{align*}
\varphi(35) &= 35 \cdot \left(1 - \frac{1}{5}\right) \left( 1 - \frac{1}{7}\right) \\
&= 35 \cdot \frac{4}{5} \cdot \frac{6}{7}\\
&= 24.
\end{align*}

On the other hand, we can just list all of the numbers from 1 to 35 and strike out all of the ones that have factors in common with $35$:
\begin{align*}
1, 2, 3, 4, \cancel{5}, 6, \cancel{7}, 8, 9, \cancel{10}, 11, 12, 13, \cancel{14}, \cancel{15}, 16 ,17, 18, 19, \\\cancel{20}, \cancel{21}, 22, 23, 24, \cancel{25}, 26, 27, \cancel{28}, 29, \cancel{30}, 31, 32, 33, 34, \cancel{35}
\end{align*}
There are 24 numbers left over, as claimed!

\end{answer}



\end{document}