% This is a TeX file
% It is essentially a programming language for mathematics documents
% You can ignore this first portion of the file, as it is essentially a bunch of "include" statements.
\documentclass[10pt]{article}

\usepackage{pictex, latexsym, graphicx,amsmath,amssymb,amsbsy,amsfonts,amsthm,verbatim}
\usepackage{graphics}
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage{fancyhdr}
\usepackage{array}

\usepackage{algorithm,algorithmic}
\usepackage{multirow}

\setlength{\parskip}{0.5em}
\setlength{\parindent}{0em}
\setlength{\voffset}{-0.5in}
\setlength{\headsep}{0.25in}

% The following denotes a counter for the problems.
\newcounter{problem}
\newcommand{\problem}{\textbf{Problem \refstepcounter{problem}.}}

% These are macro shortcuts! Helpful!
\def\vu{\mathbf{u}}
\def\vx{\mathbf{x}}
\def\vy{\mathbf{y}}
\def\vz{\mathbf{z}}
\def\vb{\mathbf{b}}
\def\vv{\mathbf{v}}
\def\vw{\mathbf{w}}

\renewcommand{\implies}{\rightarrow}
%\newcommand{\lnot}{\neg}
\renewcommand{\lor}{\vee}
\renewcommand{\land}{\wedge}
\newcommand{\xor}{\oplus}
\newcommand{\symmdiff}{\triangle}
\renewcommand{\iff}{\leftrightarrow}
\newcommand{\TRUE}{\mathbf{T}}
\newcommand{\FALSE}{\mathbf{F}}
\newcommand{\universe}{\mathcal{U}}


\def\N{\mathbb{N}}
\def\Q{\mathbb{Q}}
\def\C{\mathbb{C}}
\def\Z{\mathbb{Z}}
\def\R{\mathbb{R}}
\def\cP{\mathcal{P}}



\begin{document}
\pagestyle{fancyplain}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NOW you can start typing your text that will appear in the PDF!!!

% Note that anything after a '%' is a comment!

% Anything immediately after a '\' is a command. 
% Some denote formatting, some are for special characters.
% The '$' symbol starts and ends "math mode". Use this only for mathematical statements
% In math mode, any sequence of letters is meant to be a list of variables, so use "\sin x" for the sine of x, which will look much different from "sin x"

\lhead{}
\rhead{}
\chead{COM S 330 --- Lecture Notes --- Week of Mar 2--6}

\renewcommand{\arraystretch}{1.25}



\section{Monday, March 2}


\subsection{Rosen 5.3: Recursive Definitions and Structural Induction}

\textbf{Reading:} Rosen 5.3,  Ducks 10.5, LLM 10.1, 


\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Structural Induction}

Let $S$ be a set with a \emph{basis step} $B \subseteq S$ and a list of \emph{constructive steps} $c_1,\dots, c_k$ where each $c_i$ is a function that takes value(s) from $S$ and outputs a value in $S$, then the recursive definition of $S$ includes ``If $x_1,\dots,x_{\ell_i} \in S$, then $c_i(x_1,\dots,x_{\ell_i})$ is an element of $S$.''
Let $P$ be a property. 
To prove ``For all $s \in S$, $P(s)$ is true,'' use the following steps:

\textbf{Basis Step:} Prove that for all elements $s$ in the basis $B$ $P(s)$ is true.

\textbf{Constructive Step:} Prove that for each constructive function $c_i$ that when $P(s_j)$ holds for values $s_1,\dots,s_{\ell_i} \in S$, then $P(s)$ is true for $s = c_i(s_1,\dots,s_{\ell_i})$.
}
}
\end{quote}




\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Equality of Sets Using Recursive Definition}

Let $S$ be a set with a \emph{basis step} $B \subseteq S$ and a list of \emph{constructive steps} $c_1,\dots, c_k$ where each $c_i$ is a function that takes value(s) from $S$ and outputs a value in $S$, then the recursive definition of $S$ includes ``If $x_1,\dots,x_{\ell_i} \in S$, then $c_i(x_1,\dots,x_{\ell_i})$ is an element of $S$.''
Let $T$ be a set defined using conventional methods.

To prove $S = T$, use the following steps:

($S \subseteq T$) Use \emph{structural induction} to prove the property $s \in T$ for all elements $s \in S$.

($T \subseteq S$) Use \emph{(strong) induction} to prove the property $t \in S$ for all elements $t \in T$. 
}
}
\end{quote}

\textbf{Note:} To show $T \subseteq S$ by induction, you will need to define a function $f : T \to \N$ where $f(t)$ is roughly a measurement of how many recursive steps are required in the recursive definition of $S$ to prove that $t \in S$. See the following examples for help:

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $0 \in S$, and (Recursive Step) If $x \in S$, then $x + 1 \in S$. Prove that $S = \N$.

\begin{proof}
($S \subseteq \N$) By structural induction.

(Basis) $0 \in \N$.

(Constructive Step) Let $x \in S$ and suppose that $x \in \N$. Then $x + 1$ is a natural number and $x + 1 \in \N$.

($\N \subseteq S$) We will show that every number $n \in N$ is in $S$ using induction on $n$.

Case $n = 0$: $0 \in S$ by the basis step.

(Induction Hypothesis) Suppose that some number $n \in \N$ is in $S$.

Case $n + 1$: Since $n \in S$, the constructive step implies that $n + 1 \in S$.
\end{proof}

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $2 \in S$, and (Recursive Step) If $x \in S$ and $y \in S$, then $xy \in S$. Prove that $S = \{ 2^i : i \geq 1\}$.

\begin{proof}
Let $T =  \{ 2^i : i \geq 1\}$.

($S \subseteq T$) We use structural induction.

(Basis Step) $2  = 2^1 \in T$.

(Constructive Step) Suppose that $x, y \in S$ have the form $x = 2^i$ for some $i \geq 1$ and $y = 2^j$ for some $j \geq 1$. Then $xy = 2^{i+j} \in T$.

($T \subseteq S$) We use induction on $i \geq 1$ to show that $2^i \in S$.

Case $i = 1$: $2^1 = 2 \in S$ by the basis step.

(Induction Hypothesis) Suppose that for some $i \geq 1$ the number $2^i$ is in $S$.

Case $i+1$: $2^i$ is in $S$ and $2 \in S$ by the basis step. Thus, $2^i\cdot 2 = 2^{i+1}$ is in $S$. 
\end{proof}

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $1 \in S$, and (Recursive Step) If $x \in S$, then $x+x \in S$ and $\frac{1}{x} \in S$. Prove that $S = \{ 2^i : i \in \Z \}$. 


\begin{proof}
Let $T =  \{ 2^i : i \in Z\}$.

($S \subseteq T$) We use structural induction.

(Basis Step) $1  = 2^0 \in T$.

(Constructive Step) Suppose that $x \in S$ has the form $x = 2^i$ for some $i \in Z$.
Then $x + x = 2x = 2^{i+1} \in T$.
Also, $\frac{1}{x} = 2^{-i} \in T$.

($T \subseteq S$) We use induction on $i \geq 0$ to show that $2^i \in S$ and $2^{-i} \in S$.

Case $i = 0$: $2^0 = 1 \in S$ by the basis step.

(Induction Hypothesis) Suppose that for some $i \geq 0$ the number $2^i$ is in $S$.

Case $i+1$: By the induction hypothesis  $2^i$ is in $S$, then $2^i + 2^i = 2^{i+1} \in S$. Further, then $\frac{1}{2^{i+1}} = 2^{-(i+1)} \in S$.
\end{proof}

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $0 \in S$, and (Recursive Step) If $x \in S$, then $x+1 \in S$, $-x \in S$, and if $x > 0$ then $\frac{1}{x} \in S$. Prove that $S = \Q$.  (Hint: To show that $\Q \subseteq S$, let $\frac{p}{q}$ be a fraction where $p$ and $q$ have no common divisors other than 1 and $p \geq 0$ and $q > 0$. Use strong induction on $p+q \geq 1$ to show that each such fraction $\frac{p}{q}$ is in $S$; use either $\frac{p}{q}-1$ or $\frac{q}{p}-1$ in the induction step.)

\begin{proof}
($S \subseteq \Q$) We use structural induction.

(Basis Step) $0 \in \Q$.

(Constructive Step) Let $x \in S$ and suppose $x \in \Q$. Then $x + 1 \in \Q$, $-x \in \Q$ and if $x > 0$ then $\frac{1}{x} \in \Q$.

($\Q \subseteq S$) We use \emph{strong} induction on $|p|+|q| \geq 1$ to show that each fraction $\frac{p}{q} \in \Q$ is in $S$.

Case $|p|+|q| = 1$: Since $q \neq 0$, it must be that $p = 0$ and $q = \pm 1$ so $\frac{p}{q} = 0 \in S$ by the basis step.

(Induction Hypothesis) Let $N > 0$ and suppose that for all fractions $\frac{p}{q}$ with $0 \leq |p| + |q| < N$ we have $\frac{p}{q} \in S$.

Case $N$: Let $\frac{p}{q}$ be a fraction with $|p| + |q| = N$.
We first suppose that $p > 0$ and $q > 0$ (we will handle the case $\frac{p}{q} < 0$ later).
If $p > q$, then the fraction $\frac{p-q}{q}$ has $|p-q| + |q| < N$, so $\frac{p-q}{q} \in S$ by the induction hypothesis.
By the constructive step $\frac{p-q}{q} + 1 \in S$ and $\frac{p}{q} = \frac{p-q}{q} + 1$, we have $\frac{p}{q} \in S$.
If $p < q$, then consider the fraction $\frac{q-p}{p}$. Since $|q-p| + |p| < N$, $\frac{q-p}{q} \in S$ by the induction hypothesis.
Then $\frac{q-p}{p} + 1 \in S$ by the constructive step, and $\frac{q-p}{p} + 1 = \frac{q}{p}$.
Since $\frac{q}{p}\in S$, the constructive step implies that $\frac{p}{q}$ is in $S$.
Since $\frac{p}{q} \in S$, the constructive step implies that $-\frac{p}{q} \in S$.
\end{proof}

\textbf{Ex:} Let $\Sigma = \{0,1\}$. Define $S \subseteq \Sigma^*$ as (Basis) $w \in S$ where $w$ is the empty word, and (Recursive Step) If $x_1\dots x_n \in S$ then $x_1\dots x_n0\in S$ and for every $k \geq 0$, the string $x_1\dots x_n1\underbrace{0\dots0}_{\text{$k$ zeroes}}1 \in S$. Prove that $S = \{ \vx \in \Sigma^* : \text{there are an even number of $1$'s in $\vx$} \}$.

\begin{proof}
Let $T = \{ \vx \in \Sigma^* : \text{there are an even number of $1$'s in $\vx$} \}$.

($S \subseteq T$) We use structural induction.

(Basis) The empty word $w$ has zero 1's, so it is in $T$.

(Constructive Step) Let $\vx \in S$ and suppose that $\vx\in T$. Thus $\vx$ has an even number of 1's.
The string $\vx0$ also has an even number of 1's, so $\vx0 \in T$.
For any $k \geq 0$, the string $\vx10\dots01$ of length $k+2$ has two more 1's than $\vx$ and hence has an even number of 1's and is in $T$.

($T \subseteq S$) We use strong induction on the length of a string $\vx \in T$.

Case $|\vx| = 0$: $\vx$ is the empty word, so $\vx \in S$ by the basis step.

(Induction Hypothesis) Let $N > 0$ and suppose that for all $\vx \in T$ with $0 \leq |\vx| < N$ we have $\vx \in S$.

Case $|\vx| = N$: Let $\vx \in T$ with $|\vx| = N$.
Let $\vx = x_1\dots x_N$.
If $x_N = 0$, then the string $x_1\dots x_{N-1}$ has the same number of 1's as $\vx$, so $x_1\dots x_{N-1} \in T$ and has smaller length.
Therefore, $x_1\dots x_{N-1} \in S$ and by the constructive step we have $\vx \in S$.

If $x_N = 1$, then since $\vx$ has an even number of 1's there is another position $n < N$ where $x_n = 1$. 
Select $n$ to be the maximum value $n < N$ where $x_n = 1$.
Thus, for all $i$ where $n < i < N$ we have $x_i = 0$.
The string $x_1\dots x_{n-1}$ has length strictly less than $N$ and has two fewer 1's than $\vx$.
Therefore $x_1\dots x_{n-1} \in T$ and by the induction hypothesis $x_1\dots x_{n-1} \in S$.
By the constructive step, $\vx = x_1\dots x_{n-1}1\underbrace{0\dots 0}_{N-n-1\text{ zeroes}}1$ is also in $S$.
\end{proof}

\textbf{Ex:} Let $\Sigma = \{0,1\}$. Define $S \subseteq \Sigma^*$ as (Basis) $0 \in S$, $1\in S$ and  $w \in S$ where $w$ is the empty word, and (Recursive Step) If $\vx \in S$, then $0\vx0\in S$ and $1\vx1\in S$. Prove that $S$ is the set of \emph{palindromes}: strings $x_1\dots x_n$ where $x_1\dots x_n = x_n\dots x_1$.

\begin{proof}
Let $P = \{ x_1\dots x_n : x_1\dots x_n \in \Sigma^*, x_i = x_{n-i+1} \text{ for all $i \in \{1,\dots,n\}$}\}$.

$(S \subset P)$ We use structural induction.

(Basis Step) $w$, $0$, and $1$ are all palindromes in $P$.

(Constructive Step) Let $\vx \in S$ and suppose that $\vx \in P$.
Let $\vx = x_1\dots x_n$ and hence $x_i = x_{n-i+1}$ for all $i \in \{1,\dots,n\}$.
By the constructive step, for each letter $a \in \Sigma$ the string $ax_1\dots x_na$ is in $S$.
Note that if $y_1\dots y_{n+2} = ax_1\dots x_na$ then $y_1 = a = y_n$ and for $i \in \{1,\dots,n-1\}$ we have $y_i = x_{i-1} = x_{n-(i-1)+1} = x_{n-i} = y_{n-i+1}$. 
Therefore $a\vx a \in P$. 

($P\subseteq S$) We use strong induction on the length of a string $\vx \in P$.

Case $|\vx| = 0$: $\vx$ is the empty word $w$, and by the basis step $w \in S$.

Case $|\vx| = 1$: $\vx$ is a single letter $a \in \Sigma$, and by the basis step $a \in S$.

(Induction Hypothesis) Let $N > 1$ and suppose that for all strings $\vx \in P$ where $0 \leq |\vx| < N$ we have $\vx \in S$.

Case $|\vx| = N$: Let $\vx = x_1\dots  x_n$.
Since $\vx \in N$, we have $x_1 = x_n$.
Consider $\vy = y_1\dots y_{n-2} = x_2\dots x_{n-1}$.
Note that $y_{i} = x_{i+1} = x_{n-(i+1)+1} = x_{n-i} = y_{(n-2)-i+1}$.
Therefore $\vy \in P$ and $|\vy| = N-2$.
By the induction hypothesis $\vy \in S$ and by the constructive step with $a = x_1$ we have $\vx = a\vy a \in S$.
\end{proof}


\textbf{Ex$^*$:} Let $\Sigma = \{0,1\}$. Define $S \subseteq \Sigma^*$ and $T \subseteq \Sigma^*$ as (Basis) $w \in S$ where $w$ is the empty word and $1 \in T$, and (Recursive Step) If $x_1\dots x_n \in S$ then $x_1\dots x_n0\in S$ and $x_1\dots x_n1 \in T$; if $x_1\dots x_n \in T$ then $x_1\dots x_n0\in T$ and $x_1\dots x_n1 \in T$. Prove that $S = \{ \vx \in \Sigma^* : \text{there are an even number of $1$'s in $\vx$} \}$ and $T = \{ \vx \in \Sigma^* : \text{there are an odd number of $1$'s in $\vx$} \}$.

\textbf{Ex$^*$:} (See LLM 6.2) Let $\Sigma = \{ [, ]\}$. Define $M \subseteq \Sigma^*$ as (Basis) $w \in M$ where $w$ is the empty word, and (Recursive Step) If $\vx \in M$ and $\vy \in M$, then $\vx\cdot \vy \in M$ ($\vx \cdot \vy$ is the concatenation of the strings) and $[\vx] \in M$. Prove that the set $M$ is equal to the set of \emph{correctly-nested brackets}, that is in a string $x_1\dots x_n$ the opening brackets $[$ are paired with closing brackets $]$ such that if $(x_i,x_j)$ is such a pair $([,])$ then $i < j$, and if $(x_i,x_j)$ and $(x_a,x_b)$ are two distinct pairs where $i < a$ then $b < j$.



\textbf{Recommended Homework:} Rosen 5.3: 1--4, 5--6, 7--8, 12--19, 20--21, 23--25, 26--27, 28$^*$, 32, 36$^*$, 40$^*$, 47$^*$, 48--52, 55., 60--62, 63--65.





\clearpage
\section{Wednesday, March 4th}
\subsection{Rosen 6.1: The Basics of Counting}

\textbf{Reading:} Rosen 6.1, Ducks 1.3, 1.5, 1.7, LLM 14.3, 14.4

\textbf{Ex:} How many ways can we select a pair of 2 students from this class? (A: $n(n-1) / 2$.)

\textbf{Ex:} How many ways can we select a group of 3 students from this class? (A: $n(n-1)(n-2)/(2\cdot 3)$.)

\textbf{Ex:} How many ways can we select a group of 2 people from this class, which may or may not include the instructor? (A: $n + n(n-1)/2$)

\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Product Rule}

Suppose that a procedure can be broken down into a sequence of two tasks.
If there are $n_1$ ways to complete the first task and $n_2$ ways to complete the second task (regardless of the first task), then there are $n_1n_2$ ways to complete the procedure.
}
}
\end{quote}

\textbf{Ex:} How many ways can we elect a president and vice-president from a class of $n$ students?

There are $n$ students who could be elected president.
After electing the president, $n-1$ students remain to be elected vice-president.
Thus there are $n(n-1)$ ways to elect a president and vice-president.

\textbf{Ex:} How many binary strings are there of length 2? length 3? length 7? length $k$?

There are 2 ways to select the first letter of a string.
There are 2 ways to select the second letter of a string.
Thus, there are $2\cdot 2 = 4$ binary strings of length 2.

There are 4 ways to select the first two letters of a string.
There are 2 ways to select the third letter of a string.
Thus, there are $4 \cdot 2 = 8$ binary strings of length 3.

...

There are $2^k$ ways to select the first $k$ letters of a string.
There are $2$ ways to select the $(k+1)$st letter of a string.
Thus, there are $2^k\cdot 2 = 2^{k+1}$ binary strings of length $k+1$.

\textbf{Ex:} Let $A$ and $B$ be sets with $|A| = n$ and $|B| = m$. How many functions are there from $A$ to $B$?

There are $n$ choices to make and each choice has $m$ possible outcomes.
Thus there are $m^n$ possible functions.

\textbf{Ex:} Let $A$ and $B$ be sets with $|A| = n$ and $|B| = m$. How many \emph{injective} functions are there from $A$ to $B$?

There are $n$ choices to make, in order.
In the $i$th choice, $i-1$ previous choices have been made, so we only have $m - (i-1) = m-i+1$ possible choices remaining.
Thus, we have $m(m-1)(m-2)\cdots(m-n+1) = \prod_{i=1}^n (m-i+1)$ possible injective functions.


\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Strict Product Rule}

Let $A_1,\dots,A_n$ be finite sets. Then,
\[
\left|\prod_{i=1}^n A_i\right| =  |A_1\times A_2\times \cdots\times A_n| = |A_1|\cdot|A_2|\cdots|A_n| = \prod_{i=1}^n|A_i|.
\]
}
}
\end{quote}

\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Sum Rule}

If a procedure can complete in $n_1$ ways or $n_2$ ways, but the first $n_1$ ways are different than the other $n_2$ ways, then the procedure can complete in $n_1 +n_2$ ways.
}
}
\end{quote}

\textbf{Ex:} how many ways can we elect a president and vice-president of a class of $n$ students, where the teacher could be elected president?

If the teacher is elected president, then there are $n$ ways a student could be elected vice-president.
If the teacher is not elected president, then there are $n(n-1)$ ways two students could be elected president and vice-president.
Thus, there are $n + n(n-1) = n^2$ possible ways to elect in this fashion.




\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Strict Sum Rule}

Let $A_1,\dots,A_n$ be finite sets. 
If $A_i \cap A_j = \varnothing$ for all $i < j$, then,
\[
\left|\bigcup_{i=1}^n A_i\right| = |A_1 \cup A_2 \cup \cdots \cup A_n| = |A_1| + |A_2| + \cdots + |A_n| = \sum_{i=1}^n |A_i|.
\]
}
}
\end{quote}

\textbf{Ex:} Suppose you are creating a password policy.
You want people to enter a password of length 8, but it can only use lowercase letters and numbers, but it must have at least one number.
How many ways can someone enter a password?

Let $P$ be the set of acceptable passwords.
If $X$ is the set of all passwords of length 8 using lowercase letters and numbers, and $Y$ is the set of passwords of length 8 using only lowercase letters, then $P = X \setminus Y$. (It is not obvious how the sum rule applies here!)
But also, $(X \setminus Y) \cup Y = X$.
Thus, $|P| + |Y| = |X|$ by the sum rule, so $|P| = |X| - |Y|$.
Since there are 36 choices for each letter of an 8-character string in $X$, $|X| = 36^8$.
Since there are 26 choices for each letter of an 8-character string in $Y$, $|Y| = 26^8$.
Therefore, $|P| = 36^8 - 26^8$. This is a big number (but not big enough to avoid brute-force attacks!)



\textbf{Ex:} How many 8-letter passwords are there that use lowercase letters and at least \emph{two} numbers?

Let $P$ be the set of acceptable passwords.
Let $L$ be the set of passwords using only lowercase letters (recall $|L| = 26^8$.)
For $i \in \{1,\dots,8\}$ let $N_i$ be the set of passwords using lowercase letters in all positions except the $i$th position is a number.
$|N_i| = 26^7\cdot 10^1$.
Then $N = \cup_{i=1}^8 N_i$ is the set of passwords using exactly one letter.
Then by the sum rule $|N| = 8\cdot 26^7\cdot 10^1$.
Let $X$ be all strings of length 8 using lowercase letters and numbers (recall $|X| = 36^8$).
Then $P = X \setminus (N \cup L)$.
By the sum rule, $|N\cup L| = |N| + |L| = 26^8 + 8\cdot 26^7\cdot 10^1 = 81\cdot26^7$.
By the sum rule, $P = |X| - |N\cup L| = 36^8 - 81\cdot26^7$.

\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Subtraction Rule (Inclusion-Exclusion for Two Sets)}

Let $A_1$ and $A_2$ be sets.
\[
|A_1 \cup A_2| = |A_1| + |A_2| - |A_1\cap A_2|.
\]
}
}
\end{quote}







\clearpage
\section{Friday, March 6th}


\begin{quote}
\fbox{
\parbox{4in}{
\textbf{Division Rule}

If a process has $k$ tasks, where the $i$th task has $n_i$ distinct options (regardless of previous choices) \emph{and} each outcome of the process appears exactly $d$ times, then the number of \emph{distinct} outcomes is $\frac{1}{d} \prod_{i=1}^k n_i$.
}
}
\end{quote}

\textbf{Ex:} How many ways can we select a pair of students from a class of $n$ students?

There are $n$ ways to select the first student.
There are $n-1$ ways to select the second student.
There are $2$ ways each pair can be picked (depending on order).
Thus, there are $n(n-1)/2$ possible pairs.

\textbf{Ex:} How many ways can we select a group of 3 students from a class of $n$ students?

There are $n$ ways to select the first student.
There are $n-1$ ways to select the second student.
There are $n-2$ ways to select the third student.
For each group of 3 students there are 3 students who could be picked first, then 2 students who could be picked second, and 1 student left at the end.
Thus each group of 3 students appears $3 \cdot 2 \cdot 1 = 6$ times.
Thus, there are $n(n-1)(n-2)/6$ possible groups of 3.



\textbf{Def:} The \emph{factorial function} $n!$ is given by the product $n! = \prod_{i=1}^n i = n(n-1)(n-2)\cdots 3\cdot 2 \cdot 1$.

\textbf{NOTE:} $0! = 1$, as the empty product equals 1. This has an important reason, as below!

\textbf{Ex:} How many ways can we rearrange the string ``hello''?

There are five letters.
If we select without replacement, we have 5 options, then 4 options, 3, 2, 1, giving $5\cdot 4\cdot 3 \cdot 2 \cdot 1 = 120$ possibilities.
However, since the letter `l' appears twice, each string will be selected twice, depending on the order we selected the two `l's.
Thus, there are 60 possible rearrangements.




\subsection{Rosen 6.3: Permutations and Combinations}

[Note: we are skipping 6.2 for now, but will revisit it soon.]

\textbf{Reading:} Rosen 6.3, Ducks 6.1--6.5, LLM  14.3, 14.4

\textbf{Ex:} $n$ people run a race. How many ways can we award medals for 1st, 2nd, and 3rd place? (A: $n(n-1)(n-2)$.)

\textbf{Ex:} $n$ people run a race. How many ways can the runners be ordered at the end of the race? (A: $n(n-1)(n-2)(n-3)\cdots (3)(2)(1) = n!$.)

\textbf{Def:} A \emph{permutation} of a set of distinct objects is an ordered arrangement of these objects.
An ordered arrangement of $r$ objects (from a possibly larger set) is called an \emph{$r$-permutation}.

\textbf{Note:} The example about how many ways can $n$ runners finish the race is ``How many permutations are there of $n$ objects?''
The example about how many ways can we award the top three medals for a race among $n$ people is ``How many $3$-permutations are there of $n$ objects?"

\textbf{Ex:} How many permutations are there of 0 objects? (A: 1. Don't order anything.)


\textbf{Thm:} Let $P(n,r)$ be the number of $r$-permutations from a set of $n$ distinct objects.
\[
	P(n,r) = \prod_{i=1}^r(n-i+1) = n(n-1)\cdots (n-r+1).
\]

\begin{proof}
We will select $r$ objects $a_1, \dots, a_r$ using $r$ tasks.
The first task has $n$ choices for $a_1$.
The second task has $n-1$ choices for $a_2$.
In general, the $i$th task has $n-i+1$ choices for $a_i$.
By the product rule, there are $n(n-1)\cdots (n-i+1)\cdots (n-r+1)$ ways to complete this process (and each result is distinct).
\end{proof}

\textbf{Cor:} If $0 \leq r \leq n$, then $P(n,r) = \frac{n!}{(n-r)!}$.

\begin{proof}
Note that $\frac{n!}{(n-r)!} = \frac{n(n-1)(n-2)\cdots(n-r+1)(n-r)\cdots (3)(2)(1)}{(n-r)\cdots (3)(2)(1)}$, so the terms in the denominator cancel with terms from the numerator to result in $n(n-1)\cdots (n-r+1) = P(n,r)$.
\end{proof}


\textbf{Ex:} The word ``facetious'' is a \emph{regular panvowel} word, as every vowel appears exactly once and in order. How many ways can we rearrange ``facetious'' while keeping the vowels in the same order?

Here is our two-step process: First rearrange the letters in any way. (9 letters, so $9!$ ways)
Then, rearrange the vowels to be in the correct order (1 way to do this!)
The result is a rearrangement with the vowels in the correct order. 
(Each output word appears $5!$ ways, as there are $5!$ different arrangements of the vowels; each arrangement gives a word after the first step that will result in this output word during the second step.)
Thus, by the division rule there are $\frac{9!}{5!} = 9(8)(7)(6)$ possible arrangements.

\textit{An alternate method:} 
There are 9 places the `f' can go.
There are 8 places the 'c' can go.
There are 7 places the 't' can go.
There are 6 places the 's' can go.
Now, the remaining 5 places must have the vowels in order.
Thus, by the product rule there are $9(8)(7)(6)$ possible arrangements.

We will frequently see that there are multiple ways to count the same thing! This is GOOD!


\textbf{Def:} An unordered selection of objects from a set of distinct objects is a \emph{combination} (also known as a subset).
An unordered selection of $k$ distinct objects from a set of distinct objets is an \emph{$k$-combination} (also known as a subset of size $k$).

\textbf{Ex:} How many $3$-combinations are there among a set of $n$ people?

A: There are $n(n-1)(n-2)$ 3-permutations of $n$ people.
If we ``forget'' about the order we selected the three people, then each 3-combination appears $3\cdot 2\cdot 1$ times.
Thus, there are $\frac{n(n-1)(n-2)}{6}$ 3-combinations among $n$ people.

\textbf{Thm:} Let $C(n,k)$ be the number of $k$-combinations from a set of $n$ distinct objects.
\[
	C(n,k) = \frac{n!}{k!\cdot (n-k)!}.
\]
\textbf{Note:} We will frequently use the \emph{binomial coefficient} $\binom{n}{k}$, spoken as ``$n$ choose $k$,'' to represent $\binom{n}{k} = C(n,k) = \frac{n!}{k!(n-k)!}$.

\begin{proof}
There are $\frac{n!}{(n-k)!}$ $k$-permutations of $n$ objects.
If we forget about the order, each $k$-combination appears in $k!$ different orders.
Thus, by the division principle, there are $\frac{n!}{k!(n-k)!}$ possible $k$-combinations from a set of $n$ objects.
\end{proof}

\textbf{Cor:} $P(n,k) = k!\cdot \binom{n}{k} = k!\cdot C(n,k)$. 

\begin{proof}
While this follows from the algebraic representation, notice that we can produce a $k$-permutation of $n$ objects using the following process:
First select a $k$-combination (in $\binom{n}{k}$ possible ways), then select a permutation of those objects (in $k!$ ways).
\end{proof}

\begin{center}
\begin{tabular}[h]{c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c}
&&&&&&&1&&&&&&&\\
&&&&&&1&&1&&&&&&\\
&&&&&1&&2&&1&&&&&\\
&&&&1&&3&&3&&1&&&&\\
&&&1&&4&&6&&4&&1&&&\\
&&1&&5&&10&&10&&5&&1&&\\
&1&&6&&15&&20&&15&&6&&1&\\
1&&7&&21&&35&&35&&21&&7&&1\\
\end{tabular}

\textit{Pascal's Triangle} forms the binomial coefficients $\binom{n}{k}$.\\
 ($n$ is the row number from the top, $k$ is the number of steps from the left).
\end{center}

\textbf{Thm:} Let $n \geq k > 0$. Then $\binom{n}{k} = \binom{n-1}{k} + \binom{n-1}{k-1}$.

\begin{proof}
Recall that $\binom{n}{k}$ is the number of ways to select a $k$-combination from a set of $n$ distinct objects.

Let $A$ be a set of $n$ distinct objects and let $a \in A$.
There are $\binom{n-1}{k}$ $k$-combinations in $A \setminus \{a\}$, and hence $\binom{n-1}{k}$ $k$-combinations of $A$ that do not contain $a$.
There are $\binom{n-1}{k-1}$ $(k-1)$-combinations in $A\setminus \{a\}$, and if we add $a$ to such a combination we get a $k$-combination of $A$ containing $a$.
By the sum rule, there are $\binom{n-1}{k} + \binom{n-1}{k-1}$ $k$-combinations of elements in $A$.

By double counting $\binom{n}{k} = \binom{n-1}{k} + \binom{n-1}{k-1}$.
\end{proof}





\end{document} 
