\documentclass[11pt]{article}
\usepackage{macros-course-lectures}

\usepackage{amsmath}

\usepackage{graphics}
\usepackage[pdflatex]{graphicx}

\newenvironment{verify}%
{\par{\noindent\em Verification.}\hspace{1em}\setlength{\parindent}{3ex}}%
{\qed}

\newcommand{\cuberoot}[1]{\sqrt[\leftroot{2}\uproot{4}3]{#1}}

\begin{document}

\lecture{notes13}{Functions, Counting, and the Pigeonhole
Principle}{March 16, 2000}

\section{Overview}

Reading: Rosen 4.1, 4.2; fall 97 lecture notes 12. \\
\\
New topic: Counting cardinality of a set (number of elements). \\
Used in analyzing algorithms (counting number of steps). \\
Basis for the study of probability. \\
\\
Basic method for today: \\
Correlate elements of set with elements of another, already-understood
set. \\
Correlations expressed by functions. \\
\\
Topics for this lecture: \\
2. Functions \\
3. Counting using functions \\
4. Pigeonhole principle

\section{Functions}

\subsection{Basic definitions}

\begin{definition*}
  A {\em function} $f:A \mapsto B$ is a way of relating every element
  of $A$ to exactly one element of $B$.
\end{definition*}

If $a \in A$, write $f(a)$ for the corresponding element of $B$. \\
\\
$A =$ {\em domain}, $B =$ {\em range}. \\
\\
More formally, a function $f:A \mapsto B$ is a binary relation from
$A$ to $B$ such that, for every $a \in A$, there is exactly one $b \in
B$ such that $(a,b) \in f$. \\
\\
Example: \\
Draw a function (not onto). \\
\\
Example: \\
Draw a non-function (two values for some particular domain element). \\
\\
Example: \\
In the matching algorithm, at any particular point in time, 
each boy had a current girl he was pursuing. \\
Function from boys to girls, giving current girl. \\
Inverse is not a function. \\
\\
And some examples from continuous math. \\
In each case, $f: \reals \mapsto \reals$. \\
\\
Example: \\
$f(x) = x^2 + 3x + 2$ \\
\\
Example: \\
$f(x) = |x|$ (define as $x$ if $x \geq 0$, $-x$ if $x < 0$. \\
\\
Example: \\
$f(x) = \sqrt{x}$ not a function. \\
Not uniquely defined. \\
Even if I mean the positive square root, still not a function: \\
Not defined everywhere on $\reals$. \\
\\
Example: \\
$f(x) =$ cube root of $x$ is a function.\\

\begin{definition*}
  The {\em image} of a function $f:A \mapsto B$ is the set $\{b \mid
  \exists\; a \ f(a) = b \}$ and is denoted $f(A)$.
\end{definition*}

Values actually taken on, subset of range.\\ 
\\
Example: \\
First example above, image = ...\\
\\
Example: \\
$f(x) = x^2 + 3x + 2$.\\
Draw graph.\\
Min point is $(- \frac{3}{2}, - \frac{1}{4})$.\\
Image = $[- \frac{1}{4}, \infty)$.\\
\\
Example: \\
Absolute value.\\
Image = nonnegative reals.\\
\\
Example: \\
Cube root.\\
Image = reals.

\subsection{Special kinds of functions}

Classification relevant for counting: \\
surjective, injective, bijective \\

\begin{definition*}
Surjective (``onto'') \\
Image = range.\\
$\forall\; b \in B, \exists\; a \in A (f(a) = b)$.\\
\end{definition*}

Examples: \\
Diagrams, surjective and not surjective.\\
\\
Example: \\
At end of matching algorithm, current girl function is surjective.\\
\\
Example: \\
Cube root is surjective, as a function $: \reals \mapsto \reals$.\\ 
\\
Example: \\
Absolute value not surjective as function $: \reals \mapsto \reals$.\\ 
But could regard as function $: \reals \mapsto \reals^{\geq 0}$, then surjective.\\
Depends on what's defined as the range.\\

\begin{definition*}
  Injective (``one-to-one'') \\ $\forall\; a, a' \in A, a \neq a'
  \Rightarrow f(a) \neq f(a')$ \\ 
\end{definition*}

Examples: \\
Diagrams, injective and not injective.\\
\\
Example: \\
Cube root injective.\\
\\
Example: \\
Absolute value not injective.\\

\begin{definition*}
Bijective = surjective + injective.\\
AKA one-to-one correspondence.\\
\end{definition*}

Examples: \\
Diagrams \\
Cube root

\subsection{Proving that functions are surjective and injective}

Example: \\
$f: \reals \mapsto \reals$, $f(x) = \cuberoot{x}$ \\
\\
To prove injective, can show contrapositive:
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $f$ is injective, that is, $a \neq a' \Rightarrow \cuberoot{a}
\neq \cuberoot{a'}$. \\
1. $\cuberoot{a} = \cuberoot{a'} \Rightarrow a = a'$. \\
\> 1. Assume $\cuberoot{a} = \cuberoot{a'}$. \\
\> 2. $(\cuberoot{a})^3 = (\cuberoot{a'})^3$. 
   \> \> \> Algebra (cube both sides) \\
\> 3. $a = a'$
   \> \> \> Algebra \\
\> 4. QED
   \> \> \> Implication \\
2. QED
\> \> \> \> Contrapositive
\end{tabbing}
To prove surjective, can fix any element of the range and show how to
construct a corresponding element of the domain. \\
(That's enough, because of EG and UG.)
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $f$ is surjective, that is, $\forall\; b \in \reals, \exists\; a \in \reals
(\cuberoot{a} = b)$. \\
1. Fix $b \in \reals$. \\
2. Define $a = b^3$. \\
3. $\cuberoot{a} = b$ \\
4. QED
\> \> \> \> EG, UG
\end{tabbing}
Example: \\
$f: \nats \mapsto \nats$, $f(n) = n^2$. \\
\\
Show injective, this time using a direct proof:
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $f$ is injective, that is, $n \neq n' \Rightarrow n^2 \neq
(n')^2$. \\
1. Assume $n \neq n'$. \\
2. $n > n'$ or $n' > n$ 
\> \> \> \> Basic property of $\nats$. \\
3. If $n > n'$ then $n^2 > (n')^2$. 
\> \> \> \> Algebra. \\
4. If $n < n'$ then $n^2 < (n')^2$. 
\> \> \> \> Algebra. \\
5. $n^2 \neq (n')^2$.
\> \> \> \> Cases, 3. and 4. \\
6. QED
\> \> \> \> Implication, 1. and 5.
\end{tabbing}

Not surjective.\\
Show this by exhibiting a particular value that has nothing mapping to
it (go over the quantifier manipulation):
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $f$ is not surjective, that is, %
$\neg (\forall\; b \in B, \exists\; a \in \nats, a^2 = b)$ \\ 

1. $\exists\; b \in \nats, \forall\; a \in \nats, a^2 \neq b$. \\
\> 1. $\forall\; a \in \nats [a^2 \neq 3]$. \\
\> \> 1. Fix $a \in \nats$. \\
\> \> 2. $a^2 \neq 3$ \\
\> \> \> 1. Assume $a^2 = 3$. \\
\> \> \> 2. $a$ is not rational. 
         \> Proofs earlier in term. \\
\> \> \> 3. $a \notin \nats$. \\
\> \> \> 4. QED
         \> Contradiction, 1.1.1 vs. 1.1.2.3 \\
\> \> 3. QED
      \> \> UG \\
\> 2. QED 
   \> \> \> EG \\
2. QED
\> \> \> \> Quantifier manipulation
\end{tabbing}

\section{Using Functions for Determining Cardinalities}

\begin{theorem}
\label{th:injection}
Suppose $A$ and $B$ are finite sets, $f:A \mapsto B$. 
\begin{enumerate}
\item
If $f$ is an injection then $|A| \leq |B|$.
\item
If $f$ is a surjection then $|A| \geq |B|$.
\item
If $f$ is a bijection then $|A| = |B|$.
\end{enumerate}
\end{theorem}

\begin{proof}
\begin{enumerate}
\item
Prove by letting $n$ be the number of elements in $A$, and finding $n$
distinct elements of $B$.
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $|A| \leq |B|$ \\
1. Define $n \in \nats$, $n = |A|$. \\
   Define $A = \{ a_1, a_2, \ldots, a_n \}$. \\
2. $\forall\; i \in \set{1, \ldots, n}, f(a_i) \in B$ 
\> \> \> \> Definition of function. \\
3. All the $f(a_i)$ are distinct. 
\> \> \> \> Injectivity. \\
4. $|B| \geq n$
\> \> \> \> By 2. and 3. \\
5. QED 
\> \> \> \> Algebra, 1. and 4.
\end{tabbing}

Finiteness assumption used in step 1.\\
Funny situations can arise if sets aren't finite: \\
\\
Consider $f: \nats \mapsto$ perfect squares, given by $f(n) = n^2$.\\
$f$ is injective (no 2 have same square).\\ 
But $A$ has all the elements of $B$, and many more.\\
\\
Suggests that $A$ is strictly larger than $B$.\\
Apparent paradox will be resolved in an upcoming lecture on infinite sets.

\item
Prove by letting $n$ be the number of elements in $B$, and finding $n$
distinct elements of $A$.
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $|A| \geq |B|$ \\
1. Define $n \in \nats$, $n = |B|$. \\
   Define $B = \{ b_1, b_2, \ldots, b_n \}$. \\
2. $\forall\; b_i \in B, \exists\; a_i \in A, f(a_i) = b_i$ 
\> \> \> \> Surjectivity. \\
3. $\forall\; b_i \in B$, choose $a_i \in A$ such that $f(a_i) = b_i$ 
\> \> \> \> EI \\
4. All the $a_i$ are distinct. 
\> \> \> \> Definition of function. \\
5. $|A| \geq n$
\> \> \> \> By 3. and 4. \\
6. QED 
\> \> \> \> Algebra, 1. and 5.
\end{tabbing}

\item
Could go through the same kind of proofs, but don't have to: \\
Consequence of first two parts of the theorem.
\end{enumerate}
\end{proof}

\subsection{Example: Counting subsets of a finite set}

A counting problem that can be solved by finding a bijection.\\

\begin{definition*}  
The {\em power set} of a set $A$, denoted by ${\cal P}(A)$,
is the set of all subsets of $A$,
that is, $\{ S \mid S \subseteq A \}$.\\
\end{definition*}

Also written sometimes as $2^A$.\\
Looks strange, but we'll see why.\\ 
\\
Example: $A = \{ a_1, a_2, a_3 \}$.\\
${\cal P}(A) = \{ \emptyset, \{ a_1 \}, \{ a_2 \}, \{ a_3 \}, 
\{ a_1, a_2 \}, \{ a_1, a_3 \}, \{ a_2, a_3 \}, \{ a_1, a_2, a_3 \}, \}$.\\
\\
8 elements.

\begin{theorem}
If $A$ is a finite set with $|A| = n$,
then $|{\cal P}(A)| = 2^n$.\\ 
\end{theorem}

\begin{proof}
Use bijection to a set we already know has size $2^n$: \\
the set $B$ of $n$-bit binary strings, $B = \{ b_1 b_2 \ldots b_n \mid b_i \in \{
0, 1 \} \}$.\\ 
Then Theorem...will imply $|{\cal P}(A)| = 2^n$.\\
\\
Defining bijection $f: 2^{A} \mapsto B$: \\
\\
Write $A = \{ a_1, a_2, \ldots a_n \}$.\\
\\
Let $f(C)$, where $C \subseteq A$, be the string $b_1 b_2 \ldots b_n$,
where $b_i = 1$ if and only if $a_i \in C$.\\
Use bit $i$ to encode membership of element $a_i$.\\
\\
Examples: \\
Above, if $C = \{ a_1, a_3 \}$, then $f(C) = 101$.\\
If $C = \emptyset$ then $f(C) = 000$.\\
\\
$f$ is a well-defined function~---~it's possible to encode every set in
this way.\\
Claim $f$ is a bijection.\\
Must prove injective and surjective.\\

\noindent Injective:
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $f$ is injective, that is, if $C \neq C'$ then $f(C) \neq
f(C')$. \\
1. Assume $C \neq C'$. \\
2. $\exists\; a \in C - C'$ or $\exists\; a \in C' - C$. \\
3. If $\exists\; a \in C - C'$ then $f(C) \neq f(C')$. \\
\> 1. Assume $\exists\; a \in C - C'$. \\
\> 2. Fix $a \in C - C'$. \\.
\> 3. Bit $i$ of $f(C) = 1$. \\
\> \> Bit $i$ of $f(C') = 0$. 
      \> \> Definition of $f$. \\
\> 4. $f(C) \neq f(C')$. \\
\> 5. QED 
   \> \> \> Implication. \\
4. If $\exists\; a \in C' - C$ then $f(C) \neq f(C')$. 
\> \> \> \> Analogous. \\
5. $f(C) \neq f(C')$
\> \> \> \> Cases, 3. and 4. \\
6. QED 
\> \> \> \> Implication. 
\end{tabbing}

\noindent Surjective:
\begin{tabbing}
XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXX\= \kill
Prove: $f$ is surjective, that is, for every length $n$ bit string
$b_1 b_2 \ldots b_n$, \\
$\exists\; C \subseteq A$, such that %
$f(C) = b_1 b_2 \ldots b_n$. \\
1. Fix $b_1 b_2 \ldots b_n$. \\
2. Choose $C = \{ a_i | b_i = 1 \}$. \\
3. $f(C) = b_1 b_2 \ldots b_n$ \\
4. QED
\> \> \> \> EG, UG
\end{tabbing}

\end{proof}

Exercise: Show ${\cal P}(A) = 2^n$ another way, by induction on $n$,
without using bijections. 

\subsection{Counting numbers of functions}

The ideas involved in counting the number of subsets of a set can be
generalized to help in counting the number of functions from one set
to another: \\
\\
Question: \\
How many different functions are there from finite set $A$ to finite
set $B$? \\
Say $|A| = n$, $|B| = m$.\\
\\
Example: \\
$|A| = 3$, $|B| = 2$.\\
\\
Draw all the possibilities.\\
8 of them.

\begin{theorem}
If $A$ and $B$ are finite sets, with $|A| = n$ and $|B| = m$, then the
cardinality of the set of functions from $A$ to $B$ is $m^n$.
\end{theorem}

\begin{proof}
Use bijection to $M$, the set of length $n$ strings of symbols from
the alphabet $\{ 1, \ldots, m \}$.\\
Use position $i$ to encode the value of $f(i)$.\\
\\
Example: \\
The first function above is encoded by $111$ \\
Second by $121$.\\
Etc.\\
Last is $222$.\\
\\
Prove bijection: \\
\\
Injective: \\
Same rep means same function.\\
Prove by contrapositive, showing different function means different
rep.\\
LTTR.\\
\\
Surjective: \\
Every string encodes some function.\\
LTTR.
\end{proof}

Exercise: \\
What if we wanted to count the number of injective functions from $A$
to $B$? \\
Requires $|B| \geq |A|$, by Theorem...\\
\\
Again, can see this using a bijection with a set of length $n$
strings, but now it's not all the length $n$ strings, but only those
where {\em no number is repeated}.\\
\\
How many? \\
$m (m-1) (m-2) \cdots (m-n+1)$.\\
\\
Needs $m \geq n$ to make sense.\\
\\
Return to this later.\\
\\
Exercise: \\
Number of surjective functions? \\
Needs $m \leq n$, by Theorem.\\
\\
Bijection with strings of length $n$ in which every element appears at
least once.\\
\\
Will have to come back to this later...(try it).

\section{The Pigeonhole Principle}

\subsection{The theorem}

\begin{theorem}
If $n$ pigeons are put into $m$ holes, $n > m$, then at least one hole
contains two or more pigeons.
\end{theorem}

Actually this isn't really about pigeons and holes.\\
Restate in terms of functions:

\begin{theorem}
\label{cor:pigeon}
Let $A$ and $B$ be finite sets, $|A| > |B|$.\\
If $f: A \mapsto B$ 
then there exist distinct elements $a$ and $a'$ in $A$ such that $f(a)
= f(a')$.
\end{theorem}

Why this says the same thing: \\
$A$: pigeons \\
$B$: holes \\
$f$: says where each pigeon gets put

\begin{proof}
In other words, says if $|A| > |B|$ then $f$ is not injective.\\
Follows immediately from Theorem, contrapositive.
\end{proof}

Example: \\
32 people, exist $2$ born on same day of month (1-31).\\
Pigeons: the 32 people \\
Holes:  The 31 days.\\
Rule for putting pigeons in holes: Birth date

\subsection{Example: Divisibility}

Theorem: In every set of 1000 integers, there exist two, $x$ and $y$,
such that $x-y$ is divisible by 699.\\
\\
Seems hard.\\
\\
Use Pigeonhole: \\
Try 1000 pigeons and 699 holes: \\
Pigeons:  the 1000 integers \\
Holes: ? \\
Rule: ? \\
\\
We would like to arrange things so that $x$ and $y$ in same hole
implies that $699$ divides $x-y$.\\ 
How? \\
\\
Holes: numbers $0,1,\ldots,698$ \\
\\
Rule: Integer $x$ goes into hole $r$ if $r$ is the remainder when $x$
is divided by 699.\\
\\
Pigeonhole principles says some two integers $x$ and $y$ must have the
same remainder mod 699.\\
$x \bmod 699 = y \bmod 699$.\\ 
$699$ divides $x - y$.\\ 
\\
Works for any $m, n$, $m < n$, not just 699 and 1000.\\

\subsection{Example: Weighing Balls}

Given 12 balls, know that 11 are same weight and one is different.\\
May be heavier or lighter.\\
Want to discover which is the odd ball, and if it's heavy or light.\\
\\
Do by weighings of a balance scale.\\
Can do in 3 weighings.\\
\\
Can solve by algorithm, represent with tree: \\
(Draw part, go over.)

\begin{figure}[htbp]
\centerline{%
%\psfig{figure=figures/weighing.eps,height=2.0in}
\includegraphics[height=2.0in]{figures00/weighing}
}
\caption{\em A strategy for the weighing problem can be represented by a
ternary tree.  Each internal node represents a weighing.  Each leaf
node represents a result.  A run of the algorithm corresponds to a
path from the root to a leaf.  At each internal node, we perform the
weighing associated with the node and descend to a child based on the
result.  At a leaf, we output the indicated result.}
\label{fig:weighing}
\end{figure}

Example: \\
Prove impossible to solve with 14 balls, 3 weighings.\\
\\
Pigeons: \\
Possible initial situations: 1L, 2L, .\\.. 14L, 1H, 2H, ...14H \\
28 situations \\
\\
3 comparisons yields height 3 tree, with 3 children for each internal
node.\\
\\
27 leaves.\\
\\
Holes: \\
The 27 leaves \\
\\
Rule: \\
Map each initial situation to the leaf it reaches.\\
\\
Pigeonhole principle says exist two initial situations leading to the
same leaf.\\
But leaf has only one decision.\\
So the two situations give the same answer, wrong in at least one
case.\\
\\
Example: \\
5 weighings, 122 balls \\
Yields 244 situations, 243 leaves, same problem, impossible.\\
\\
Example: \\
5 weighings, 121 balls \\
Yields 242 situations, 253 leaves, so ??? \\
\\
Well, Pigeonhole doesn't rule this out, but it also doesn't tell us
it's possible.\\
For that, we'd have to construct an algorithm.\\
LTTR \\
\\
For here, consider easier case: \\
\\
Example: 3 weighings, 13 balls \\
26 situations, 27 leaves, so Pigeonhole doesn't say impossible.\\
\\
But it turns out there's still no algorithm.\\
Reasons involve more detailed analysis: \\
Proof by casees, based on first weighing.\\
\\
First weighing can put some number $n = 1, 2, 3, 4, 5, 6$ on each pan.\\
\\
$n=1$: \\
11 balls not used.\\
If first weighing comes out equal,
leaves 22 possible situations.\\
But only 2 weighings (9 leaves, could distinguish at most 9 situations).\\
\\
$n=2, 3, 4$: \\
Same argument.\\
$n = 4$ leaves 5 balls unused, 10 situations.\\
\\
$n=6$: \\
If unbalanced, have 12 situations left (one of 6 L or one of other 6 H).\\
Too many.\\
\\
$n = 5$: \\
Same argument.\\
\\
%[[[5 weighings, 121 balls impossible.\\
%Cases based on first weighing.\\ If at most 40 on each pan, and =, have
%41 balls left, 82 situations, but only 81 remaining leaves.\\
% If at least 41 on each pan, and come out unequal, then again at
% least 82 remaining situations.\\

\subsection{Binary search}

Random (living) person in the world is chosen randomly, by game
server.\\
Players ask yes/no questions to discover who it is.\\
Fewest questions wins.\\
HM questions needed? \\
\\
Model game as a tree (draw) \\
\\
Max number of questions is max length path in tree.\\
Need each person alone at some leaf.\\
\\
Binary, so need height at least $\lceil log_2(N) \rceil$, where $N$ is number
of people (about 6 billion?).\\
About 32 questions? \\
\\
Is this sufficient? \\
Yes.\\
Unlike in weighing example, where questions were of a constrained
form, here we can ask anything we like.\\
E.g., order everyone in the world alphabetically, or some other way.\\
Ask if in first half or second, split in half repeatedly.\\
Boring questions, but does work in $\lceil log_2(N) \rceil$.\\
\\
Leads to binary search algorithm to find an element in a sorted list,
w.r.t. a total order.\\
Compare with middle element, and let result tell whether to proceed to
look (recursively) in the top or bottom piece.\\
Uses $\lceil log_2(N) \rceil$ comparisons.\\
\\
Can also show that this is approximately optimal (fudge factor because
of the = case).\\
\end{document}

Problem set:

Self-study:
Rosen 1.8
Problems: 3,7,9,11,13,15,17,19,23,31

Reading for Thursday: Rosen 4.1, 4.2

