% This is lecture no. 1 of MH1301/: Discrete Mathematics.

\chapter{Basic Counting}%
\label{cha:basic_counting}

\section{Definitions}%
\label{sec:definitions}

\begin{definition}
	Set.
	A set is an \textbf{unordered} collection of \textbf{distinct} elements.
\end{definition}
\begin{exmp}
	Sets that are the same:
	\[
		\{a,b,c\} = \{b,c,a\} = \{c,a,b\} = \{a,b,c,a\}
	.\]
\end{exmp}
\begin{definition}
	Cardinality.
	The size or \textbf{cardinality} of a set $S$, denoted by $|S|$, is the number of elements in the set $S$.
\end{definition}
Counting is the task of finding the cardinality of a given set.
\begin{definition}
	Sequence.
	A \textbf{sequence} is an ordered collection of elements that are not necessarily distinct.
\end{definition}
\begin{exmp}
	Sequences that are not the same:
	\[
		(a,b,c)\neq (b,a,c)\neq(a,b,a)
	.\]
\end{exmp}

\section{Counting Rules}%
\label{sec:counting_rules}

\subsection{Product Rule}%
\label{sub:product_rule}

\begin{definition}
	Product Rule.
	Given a set $S$ of \textbf{sequences} of length $k$ where
	there are $n_1$ possible first entries,
	$n_2$ possible second entries \textbf{for each} first entry,
	$n_3$ possible third entries \textbf{for each combination} of first and second entries,
	and so on up to $n_{k}$,
	the \textbf{cardinality}  of $S$ is evaluated by:
	\[
		|S| = \prod_{i=1}^{k} n_{i} = n_1\times n_2\times \dotsb\times n_{k}
	.\]
\end{definition}
\begin{exmp}
	How many different license plates are available if each plate contains a sequence of three letters followed by three digits?
	\begin{sol}
		There are 26 choices for each of the three letters and 10 choices for each of the three digits.
		By the product rule, there are a total of:
		\[
			26^{3}\times 10^{3}= 17,576,000
		\]
		possible license plates.
	\end{sol}
\end{exmp}
\subsection{Functions}%
\label{sub:functions}

Recall that a function $f:X\mapsto Y$ is a \textbf{bijective} function if every element in $Y$ is mapped from \textbf{exactly} one element in $X$.
This means that both $X$ and $Y$ have the same cardinality.
In other words:
\[
	f:X\mapsto Y\text{ is bijective }\implies |X|=|Y|
.\]
This property makes bijective functions very useful in counting.
\bigbreak

\begin{exmp}
	How many $n$ bit binary sequences are there?
	\begin{sol}
		Each entry of the sequence has two possibilities: 0, or 1.
		By the product rule there are therefore a total of $2\times 2\times\dotsb\times 2=2^{n}$ such sequences.
	\end{sol}
\end{exmp}
\bigbreak

\begin{exmp}
	How many different subsets of a finite set $S=\{1,\dotsc,n\}$ are there?
	\begin{sol}
		Construct a bijective function from the subsets of $S$ to the set of $n$ bit binary sequences where the subset $\{s_1,s_2,\dotsc,s_{k}\}$ maps to the bit sequence that has ones in positions $s_1,\dotsc,s_{k}$ and zeros in all the other positions.
		For example, if $n=7$:
		\[
			\{3,4,6\} \mapsto \{0,0,1,1,0,1,0\}
		.\]

		Clearly every subset of $S$ maps to some $n$ bit sequence.
		Since we know that the number of $n$ bit sequences is $2^{n}$, by the mapping rule, the number of subsets of $S$ must also be $2^{n}$.
	\end{sol}
\end{exmp}

\subsection{Sum Rule}%
\label{sub:sum_rule}

\begin{thrm}
	Sum Rule.
	If $A_1,A_2,\dotsc,A_k$ are \textbf{disjoint} sets, then:
	\[
	|A_1\cup A_2\cup\dotsb\cup A_k| = |A_1| + |A_2| + \dotsb + |A_k|
	.\]
	
\end{thrm}

\begin{exmp}
Passwords on a computer are of six to eight characters long, where each character is either an uppercase letter or a digit.
How many possible passwords are there?
\begin{sol}
	Let $X$ be the total number of possible passwords, and $X_6,X_7,X_8$ denote the number of possible passwords of length 6, 7, and 8 respectively.
	By the sum rule, $X=X_6+X_7+X_8$.
	By the product rule:
	\begin{align*}
		X_6 & = (26+10)^{6} = 36^{6}\\
		X_7 & = (26+10)^{7} = 36^{7}\\
		X_8 & = (26+10)^{8} = 36^{8}
	\end{align*}
	So the total number of possible passwords is:
	\[
	36^{6} + 36^{7} + 36^{8} = 2,901,650,853,888\approx 2.9\cdot 10^{12}
	.\]
\end{sol}
\end{exmp}

\subsection{Inclusion Exclusion Principle}%
\label{sub:inclusion_exclusion_principle}

\begin{thrm}
	Inclusion Exclusion Principle.
	Given two sets $A_1$ and $A_2$, not necessarily disjoint, the number of elements in the union of these sets is
	\[
	|A_1\cup A_2| = |A_1| + |A_2| - |A_1\cap A_2|
	.\]
\end{thrm}
\begin{exmp}
	How many 0-1 bit strings of length 8 either start with a 1 bit or end with the two bits 00?
	\begin{sol}
		Let $A_1$ be the set of strings that start with a 1 bit.
		By the product rule:
		\[
		|A_1| = 2^{7} = 128
		.\]
		Let $A_2$ be the set of strings that end with 00.
		By the product rule:
		\[
		|A_2|=2^{6}=64
		.\]
		$A_1\cap A_2$ denotes the set of strings that start with 1 \textbf{and} end with 00.
		By the product rule:
		\[
		|A_1\cap A_2|=2^{5}=32
		.\]
		By the inclusion exclusion principle, the final answer is:
		\[
		128 + 64 - 32 = 160
		.\]
	\end{sol}
\end{exmp}

\noindent
The principle can be extended to problems including more than 2 sets:
\begin{align*}
	|A\cup B\cup C| = & |A| + |B| + |C|\\
			  &-|A\cap B| - |A\cap C| - |B\cup C|\\
			  &+|A\cap B\cap C|
\end{align*}
