% AUTOGENERATED FILE; you probably want to edit the .tex.in file, not this.
\documentclass[letter,12pt]{article}
\usepackage[margin=0.5in,hmargin=4cm]{geometry}
\pagestyle{headings}
\usepackage{amssymb,amsmath,hyperref,enumerate}
\usepackage{parskip}
\usepackage[all,cmtip]{xy}
\parindent=0pt
\newcommand{\defn}{\underline}
\newcommand{\qed}{\hfill\ensuremath{\Box}}
\begin{document}
\title{Math 3U03 Combinatorics}
\author{Martin Bays}
\maketitle
Notes from a course at McMaster University given by Martin Bays in Winter 2015.

Released under the Creative Commons Share-alike CC SA licence
\mbox{}\\ \mbox{\hspace{2em}}{\tt http://creativecommons.org/licenses/sa/1.0/}
\\by Martin Bays {\tt mbays@sdf.org} 2015.

The textbook for the course was ``Introductory Combinatorics'', 5th edition, by
Richard A.~Brualdi.
\section{Nim}

Nim: finitely many piles of coins; a move comprises removing a positive number
of coins from a single pile; a player loses if they can't move.

\paragraph{Remark:}
\mbox{}\\    For any nim position $P$, either it can be won by the player with the move,
\\    or it can be won by the player without the move.

    i.e. one of the two players has a "winning strategy", a way to play which
    guarantees a win.

\newcommand{\caret}{\mathbin{\char`\^}}
The "nim sum", $n \oplus m$, of natural numbers $n$ and $m$ is the result of writing
the binary expansions of $n$ and $m$ and "adding without carrying". (In computer
science, this is called "XORing the bitstrings"; in many programming
languages, it's written as "{\tt n\char`\^ m}".)

\paragraph{Theorem:}
\mbox{}\\    The player without the move can win from the Nim position with piles of
    sizes $n_1,...,n_k$
    iff $n_1 \oplus n_2 \oplus ... \oplus n_k = 0$
\paragraph{Proof:}
\mbox{}\\    Suppose inductively that this is true for all nim positions with fewer
    coins involved.

    First, suppose
\mbox{}\\ \mbox{\hspace{2em}}$n_1 \oplus n_2 \oplus ... \oplus n_k = b \neq  0$.
\\    We show that we can win if we have the move.

    Consider binary expansions.
\\    Some $n_i$ has a 1 in the same position as the leading 1 of $b$,
\\    so
\mbox{}\\ \mbox{\hspace{2em}}$n_i \oplus b < n_i$.

    So we can move by taking coins from the ith pile so as to leave $n_i (+)
    b$ coins in that pile.

    Then in the new position, the nim sum of the pile sizes is
\mbox{}\\ \mbox{\hspace{2em}}$n_1 \oplus ... \oplus n_{i-1} \oplus n_i \oplus b \oplus n_{i+1} \oplus ... \oplus n_k$
\mbox{}\\ \mbox{\hspace{2em}}$= b \oplus b$
\mbox{}\\ \mbox{\hspace{2em}}$= 0$

    So by the induction hypothesis, the player without the move wins from
    here. But that's us!

    Now suppose 
\mbox{}\\ \mbox{\hspace{2em}}$n_1 \oplus n_2 \oplus ... \oplus n_k = 0$
\\    and we don't have the move.

    If our opponent can't move, we've won.
\\    Else, suppose they move by taking coins from the ith pile, leaving $m < n_i$.
\\    But then $m \oplus n_i \neq  0$, so
\mbox{}\\ \mbox{\hspace{2em}}$n_1 \oplus ... \oplus m \oplus ... \oplus n_k \neq  n_1 \oplus ... \oplus n_i \oplus ... n_k = 0$,
\\    so by the induction hypothesis, we're left with a position won by the
    player with the move, which is us.
\section{Counting}

\paragraph{Example:}
\mbox{}\\    If we draw 37 circles in the plane, such that every two circles intersect
    in two points and such that no three circles intersect in the same point
    ("mutually overlapping circles in general position"), how many regions in
    the plane do we get (counting the region outside all the circles as one of
    the regions)?
\paragraph{Solution:}
\mbox{}\\    Let $h_n$ be the number of regions with $n$ circles.
\\    So $h_1 = 2$,
\\    and for $n > 1$: when we add the nth circle, it intersects the existing $n-1$
    circles in $2(n-1)$ points; so the new circle is divided into $2(n-1)$ arcs;
\\    each arc divides a pre-existing region into two. So we get $2(n-1)$ extra
    regions compared to how many we had before, i.e. compared to $h_{n-1}$. So
\mbox{}\\ \mbox{\hspace{2em}}$h_n = h_{n-1} + 2(n-1)$.

    Applying this recursively, we have
\mbox{}\\ \mbox{\hspace{2em}}$h_n = h_{n-1} + 2(n-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= h_{n-2} + 2(n-2) + 2(n-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= h_{n-3} + 2(n-3) + 2(n-2) + 2(n-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= ...$
\mbox{}\\ \mbox{\hspace{4em}}$= h_1 + 2(1) + 2(2) + ... + 2(n-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= 2 + 2(1+2+...+n-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= 2 + 2(n(n-1)/2)$
\mbox{}\\ \mbox{\hspace{4em}}$= 2 + n(n-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= n^2 - n + 2$

    (We can check this satisfies the recurrence relation:
\mbox{}\\ \mbox{\hspace{2em}}$((n-1)^2 - (n-1) + 2) + 2(n-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= n^2 - 2n + 1 - n - 1 + 2 + 2n - 1$
\mbox{}\\ \mbox{\hspace{4em}}$= n^2 - n + 2$,
\\    as required.)

    So $h_{37} = 37^2 - 37 + 2 = 1334$.


\subsection*{Four principles of counting}

We find ways to use descriptions of a finite set to determine its size in
terms of the sizes of the sets appearing in the description.

\subsection*{Addition principle}

Suppose a set $S$ is partitioned by subsets $S_1,...,S_n$;
\\this means that the sets cover $S$,
\\i.e. $S = S_1 \cup ... \cup S_n$,
\\and are disjoint,
\\i.e. $S_i \cap S_j = \emptyset$ if $i \neq  j$.
\\In other words, suppose $S$ is the disjoint union of the $S_i$.
\\Then $|S| = |S_1| + ... + |S_n|$.

\paragraph{Example:}
\mbox{}\\    In a nim game with three piles of sizes 3, 7, and 27,
\\    how many possible first moves are there?
\paragraph{Solution:}
\mbox{}\\    The first move has to be in one of the three piles, so the answer is the
    sum of the numbers of moves available when the move is in each of the
    three piles. The number of possible moves from a pile with $n$ coins is $n$.
\\    So the answer is $3 + 7 + 27 = 37$.

\subsection*{Multiplication principle}

Let $S$ be a set of ordered pairs $(a,b)$ where the first is picked from a set of
size $p$, and where for each such a there are $q$ possible choices for $b$.
\\Then $|S| = pq$

\paragraph{Example:}
\mbox{}\\    How many two-digit numbers have different digits?
\paragraph{Solution:}
\mbox{}\\    Think of a two-digit number as an ordered pair, e.g. $37 \leftrightarrow  (3,7)$.
\\    Then the set of two-digit numbers with different digits corresponds to the
    set of ordered pairs of numbers where the first number is from $\{1,...,9\}$,
\\    and the second number is then one of the 9 digits which is not equal to
    the first digit. So there are $9*9 = 81$ possibilities.

\paragraph{Proof of the multiplication principle:}
\mbox{}\\    Partition $S$ according to the first entry of the pair. Then there are $p$
    sets each of size $q$, so by the addition principle
\mbox{}\\ \mbox{\hspace{2em}}$|S| = q + ... + q$ ($p$ times)
\mbox{}\\ \mbox{\hspace{4em}}$= pq$

Of course it generalises to ordered triples and so on:\

\paragraph{Example:}
\mbox{}\\    How many three digit numbers have no repeated digits?
\paragraph{Solution:}
\mbox{}\\    9 choices for the first digit, then 9 for the second, then 8 for the
    third; so $9*9*8 = 648$ possibilities.

\paragraph{Example:}
\mbox{}\\    How many factors does 12600 have?
\paragraph{Solution:}
\mbox{}\\    Find prime factorisation:
\mbox{}\\ \mbox{\hspace{2em}}$12600 = 2^3 * 3^2 * 5^2 * 7$
\\    So any factor has prime factorisation
\mbox{}\\ \mbox{\hspace{2em}}$2^a * 3^b * 5^c * 7^d$
\\    with $0 \leq  a \leq  3, 0 \leq  b \leq  2, 0 \leq  c \leq  2, 0 \leq  d \leq  1$.

    By uniqueness of prime factorisation, each choice of $(a,b,c,d)$ within
    these bounds yields a unique factor of 12600.

    So the number of factors is the number of such $(a,b,c,d)$, which is
\mbox{}\\ \mbox{\hspace{2em}}$4 * 3 * 3 * 2 = 72$

\paragraph{Example:}
\mbox{}\\    How many two digit numbers have the sum of their digits odd, and don't end
    with 7?
\paragraph{Solution:}
\mbox{}\\    We need one digit even and the other odd; the first digit can't be 0, the
    second can't be 7.

    If we pick the first digit first, the number of possibilities for the
    second digit will depend on the parity of the first digit.

    There are 5 possibilities for an odd first digit. The second then has to
    be even: 5 possibilities.

    With an even first digit, we have 4 possibilities for the first, and 4 for
    the second.

    So the answer is $5*5 + 4*4 = 41$.

\subsection*{Subtraction principle}

If $S$ is a subset of $U$, then
    $|S| = |U| - |U \setminus S|$.

Proof: $S$ and $U \setminus S$ partition $U$, so $|U| = |S| + |U \setminus S|$.

\paragraph{Example:}
\mbox{}\\    If we toss a coin 10 times, we get a sequence of Heads and Tails,
\\    e.g. HTTHHTTHTH.
\\    How many such sequences contain at least two Heads?
\paragraph{Solution:}
\mbox{}\\    The number of such sequences which {\bf don't} contain at least two Heads is
    much easier to work out; it's 11: 1 for TTTTTTTTTT, and 10 for each of the
    ten positions a single Head could be.

    The total number of sequences is, by the multiplication principle,
\mbox{}\\ \mbox{\hspace{2em}}$2 * 2 * ... * 2$ (10 times)
\mbox{}\\ \mbox{\hspace{2em}}$= 2^10 = 1024$.

    So the answer is
\mbox{}\\ \mbox{\hspace{2em}}$1024 - 11 = 1013$.


\subsection*{Division principle}

If $S$ is partitioned into $k$ sets each with $n$ elements, then
    $k = |S|/n$.
\\(Proof: by the multiplication principle, $|S| = k*n$.)

\paragraph{Example:}
\mbox{}\\    740 pigeons are nesting in some pigeonholes. If there are 5 pigeons in
    each pigeonhole, how many pigeonholes are there?
\paragraph{Solution:}
\mbox{}\\    $740/5 = 148$.


\subsection*{Finite probability}

If we toss a coin 10 times, what's the probability of getting at least two
Heads? Answer: there are $2^{10} = 1024$ possible outcomes (sequences of Heads and
Tails), each equally likely. 1013 of them end up with at least two heads, so
the probability is $1013/1024$.

\paragraph{General setup:}
\mbox{}\\    we have a \underline{sample space}, a finite set $S$ of possible outcomes, each
    equally likely to occur. Then an \underline{event} is a subset $E$ of $S$, and we
    define the probability that $E$ occurs to be
\mbox{}\\ \mbox{\hspace{2em}}$\operatorname{Prob}(E) := |E| / |S|$

Example: Suppose someone offers to play the following game with you: three
    6-sided dice will be rolled, and you'll set up a nim game with three
    piles, the sizes of the piles being given by the dice roll. You will play
    first. Assuming you both know the winning strategy for nim, what are your
    chances of winning?
\paragraph{Solution:}
\mbox{}\\    The number of possible dice rolls is $6^3 = 216$.

    So we want to determine the number of triples $(a,b,c)$ of numbers between 1
    and 6 such that the nim sum is zero,
\mbox{}\\ \mbox{\hspace{2em}}$a \oplus b \oplus c = 0$.

    Nim-adding $c$ to both sides, this is equivalent to
\mbox{}\\ \mbox{\hspace{2em}}$a \oplus b = c$.

    So once $a$ and $b$ are determined, there's at most one choice for $c$. So we
    want to see for how many $(a,b)$,
\mbox{}\\ \mbox{\hspace{2em}}$1 \leq  a \oplus b \leq  6$.

    This can only fail if, in binary,
\mbox{}\\ \mbox{\hspace{2em}}$a \oplus b = 000$ or $111$
\\    i.e. if $a = b$ or $a+b=7$.
\\    So this precisely rules out two choices for $b$ for each choice of $a$.

    So there are $6*4 = 24$ bad rolls for us, so the probability we win is
\mbox{}\\ \mbox{\hspace{2em}}$216 - 24 / 216 = 192 / 216 = 8/9$.
\section{Permutations}

A \underline{permutation} of a finite set $S$ is an ordered list of its elements.

An \underline{$r$-permutation} of $S$ is an ordered list of $r$ of its elements.

\paragraph{Warning:}
\mbox{}\\    there is another, related, meaning of 'permutation': an element of the
    group of bijections of $S$. We won't use that meaning in this course.

$P(n,r) :=$ number of $r$-permutations of a set of size $n$.

e.g. $P(26,5) =$ number of strings of 5 distinct letters from the Roman alphabet.

By the multiplication principle,
\\    $P(n,r) = n * (n-1) * ... * (n-(r-1))$
($n$ choices for first, $n-1$ for second...)

$P(n,r) = n! / (n-r)!$

$P(n,n) = n!$

\paragraph{Remark:}
\mbox{}\\    Can interpret "$P(n,r) = n! / (n-r)!$" as follows:
\\	We can obtain an $r$-permutation of $S$ by taking the first $r$ elements of
	a permutation of $S$.
\\	Partition the permutations of $S$ according to the $r$-permutation which
	results from this: we see that the elements of each set of the
	partition correspond to the permutations of the left-over $n-r$
	elements, so we recover the formula by the division principle.

A \underline{circular $r$-permutation} of a set is a way of putting $r$ of its elements
around a circle, with two such considered equal if one can be rotated to the
other.

We can obtain a circular $r$-permutation from an $r$-permutation by "joining the
ends into a circle". Each circular $r$-permutation is obtained from $r$ different
$r$-permutations, so by the division principle:
\\    number of circular $r$-permutations of $n$ elements
\mbox{}\\ \mbox{\hspace{2em}}$= P(n,r) / r$
\mbox{}\\ \mbox{\hspace{2em}}$= n! / r(n-r)!$

\paragraph{Example:}
\mbox{}\\    How many different kinds of necklace can be made from 7 spherical beads
    of different colours? Consider two necklaces to be of the same kind when
    they can be non-destructively manipulated to look the same.
\paragraph{Solution:}
\mbox{}\\    There are $7!/7 = 6!$ circular permutations of the 13 colours. Each kind
    of necklace is obtained from exactly {\bf two} circular permutations, because
    flipping the necklace in space doesn't change the kind. So the answer is
\mbox{}\\ \mbox{\hspace{2em}}$6! / 2 = 360$.

\paragraph{Example:}
\mbox{}\\    How many ways can 13 people be sat around a round table, if Professor Q
    is not to be sat next to his arch-nemesis Inspector P?
\paragraph{Solution:}
\mbox{}\\    Without the restriction, there would be 12! seating arrangements.
\\    Consider seating everyone but P; each such arrangement yields two
    forbidden arrangements of all 13, one by placing P to Q's right and one by
    placing P to Q's left. We count each forbidden arrangement once in this
    way.

    So the answer is $12! - 2*11! = 10*11! = 399168000$


\subsection*{Subsets ("Combinations")}

An \underline{$r$-subset}, or \underline{$r$-combination}, of a set $S$ is a subset of size $r$.

$C(n,r) = {n \choose r} =$ number of $r$-subsets of a set of size $n$.

e.g. ${26 \choose 5} =$ number of unordered selections of 5 letters from the roman
    alphabet

\paragraph{Theorem:}
\mbox{}\\    ${n \choose r} = n! / r!(n-r)!$
\paragraph{Proof:}
\mbox{}\\    The $r$-permutations of a set are precisely the permutations of the
    $r$-subsets. Each $r$-subset has $r!$ permutations, so
\mbox{}\\ \mbox{\hspace{2em}}$P(n,r) = r! * {n \choose r}$.
\\    So
\mbox{}\\ \mbox{\hspace{2em}}${n \choose r} = P(n,r) / r!$
\mbox{}\\ \mbox{\hspace{4em}}$= n! / r!(n-r)!$.

${n \choose r}$ is also called a "binomial coefficient".

\paragraph{Example:}
\mbox{}\\    If we expand out $(x+y)^n$ and collect terms to obtain
\mbox{}\\ \mbox{\hspace{2em}}$a_0 x^n + a_1 x^{n-1}y + ... + a_{n-1} xy^{n-1} + a_n y^n$,
\\    what are the coefficients $a_k$?
\paragraph{Solution:}
\mbox{}\\    $a_k$ is the number of ways of choosing $y$ $k$ times when we have to choose
    either $x$ or $y$ from each factor of the product
\mbox{}\\ \mbox{\hspace{2em}}$(x+y)(x+y)...(x+y)$  ($n$ times),
\\    which is the number of subsets of this set of $n$ factors.

    So $a_k = {n \choose k}$.

\paragraph{Theorem [Pascal's Formula]:}
\mbox{}\\    If $0 < k < n$,
\mbox{}\\ \mbox{\hspace{2em}}${n \choose k} = {n-1 \choose k} + {n-1 \choose k-1}$
\paragraph{Proof:}
\mbox{}\\    $|S| = n$.
\\    Fix $x \in S$; let $S' := S \setminus \{x\}$.
\\    Partition the $k$-subsets of $S$ according to whether they contain $x$.
\\    Those which don't correspond to $k$-subsets of $S'$,
\\    those which do correspond to $(k-1)$-subsets of $S'$.

\paragraph{Theorem:}
\mbox{}\\ \mbox{\hspace{2em}}$\Sigma_{k=0}^n {n \choose k} = 2^n$
\paragraph{Proof:}
\mbox{}\\    $|S| = n$.
\\    $\Sigma_{k=0}^n {n \choose k} =$ number of subsets of $S$.
\\    But to choose a subset of $S$ is to choose for each element of $S$ whether it
    should or should not go in to the subset. That's two choices for each of
    the $n$ elements, so by the multiplication principle there are
    $2*2*...*2 = 2^n$ subsets of $S$.
\section{Multisets}

\paragraph{Example:}
\mbox{}\\    A bag of Scrabble tiles contains 100 tiles: 10 A's, 2 B's, 2 C's, 5 D's
    and so on.

    When you start a game, you take 7 letters from the bag, and put them on a
    rack. How many possible hands can you get, if we say that the order of the
    tiles on the rack matters? How about if it doesn't?

A \underline{multiset} is a "set with multiplicity".

Notation: $\{2*a,3*b,1*c\}$

(Think of a "bag" with 2 $a$'s, 3 $b$'s and a $c$ in it.)

We also allow "infinite multiplicity", denoted $\{\infty*a\}$.

Multiplicities are also called "repetition numbers".

The \underline{size} of a multiset is the sum of the multiplicities (may be $\infty$).

An $r$-permutation of a multiset is an ordered list of $r$ elements from the
multiset;
\\e.g. the 2-permutations of $\{2*a,1*b\}$ are
\mbox{}\\ \mbox{\hspace{2em}}$aa, ab, ba$;
\\the 3-permutations of $\{\infty*a,2*b\}$ are
\mbox{}\\ \mbox{\hspace{2em}}$aaa, aab, aba, baa, abb, bab, bba$.

A \underline{permutation} of a multiset of size $n$ is an $n$-permutation.

Example: how many permutations are there of the unfortunate scrabble hand
    $\{4*U,1*J,2*K\}$?

\paragraph{Theorem:}
\mbox{}\\    Let $S$ be a multiset with $k$ types with finite multiplicities $n_1,...,n_k$.
\\    Let $n = \Sigma_i n_i$ be the size of $S$.
\\    Then the number of permutations of $S$ is
\mbox{}\\ \mbox{\hspace{2em}}$n! / n_1!*n_2!*...*n_k!$.
\paragraph{Proof:}
\mbox{}\\    Label the elements $1,...,n$. Each permutation of $\{1,...,n\}$ yields a
    permutation of $S$, and two yield the same permutation precisely when we can
    get one from another by permuting the labels on elements of the same type.
\\    So there are $n_1!*n_2!*...*n_k$! permutations of $\{1,...,n\}$ per permutation
    of $S$. We conclude by the division principle.

\paragraph{Example:}
\mbox{}\\    We have 4 black rooks and 4 white rooks. How many ways are there of
    putting them on a chess board such that no two are attacking (/defending)
    each other? e.g.

\begin{verbatim}
    .......R  8
    ....r...  5
    ..R.....  3
    ......R.  7
    ...r....  4
    .R......  2
    .....r..  6
    r.......  1
\end{verbatim}
\paragraph{Solution:}
\mbox{}\\    First, just choose the 8 squares for them to occupy.

    By listing off the filled columns row-by-row, a choice corresponds to a
    permutation of the columns, so there are 8!.

    For a given such choice, a choice of colours corresponds to a permutation
    of the multiset $\{4*r,4*R\}$.

    So the answer is
\mbox{}\\ \mbox{\hspace{2em}}$8! * (8! / 4!*4!) = 2822400$


An \defn{$r$-submultiset}, or \defn{$r$-combination}, of a multiset $S$ is a
multiset $S'$ of size $r$ such that for all $x$,
\\    the multiplicity of $x$ in $S'$ is at most the multiplicity of $x$ in $S$.

e.g. the 2-submultisets of $\{3*a,b\}$ are
    $\{2*a\}$, $\{1*a,1*b\}$.

\pagebreak
\paragraph{Theorem:}
\mbox{}\\    The number of $r$-combinations of a multiset with $k$ types each with
    multiplicity at least $r$ is
\mbox{}\\ \mbox{\hspace{2em}}${r+k-1 \choose r}$
\paragraph{Example:}
\mbox{}\\    If we have a bag containing red, green and blue marbles, with many of
    each, and we draw 5 marbles from the bag, how many possible results
    (numbers of each colour drawn) are there?
\\    Answer: ${5+3-1 \choose 5} = {7 \choose 5} = 7! / 2!5! = 21$
\paragraph{Proof:}
\mbox{}\\    We can identify an $r$-submultiset with an arrangement of $k-1$ partitions
    interspersed among $r$ identical objects, by counting the numbers of objects
    between the partitions; e.g. with $k=6$ and $r=8$
    \begin{verbatim}
	oo|ooo||o|oo|
    \end{verbatim}
    corresponds to
\mbox{}\\ \mbox{\hspace{2em}}$\{ 2*a_1, 3*a_2, 0*a_3, 1*a_4, 2*a_5, 0*a_6 \}$.

    These arrangements correspond to choosing $r$ of the $r+k-1$ characters to
    be 'o's, so the number of such arrangements is ${r+k-1 \choose r}$.

\section{Pigeonhole Principles}

\paragraph{Pigeonhole Principle (PP):}
\mbox{}\\    If some pigeons are in some pigeonholes,
\\    and there are fewer pigeonholes than there are pigeons,
\\    then some pigeonhole must contain at least two pigeons.

{\em // The "pigeons" and "pigeonholes" can be abstract!}

\paragraph{Example:}
\mbox{}\\    If there are 367 people in a room,
\\    there must be two who share a common birthday.

\subsection*{Interlude: maps and numbers}

$f : X \rightarrow  Y$ map between finite sets.

For $y\in Y$, $f^{-1}(y) =$ "fibre of $f$ over $y$" $= \{ x \;|\; f(x) = y \}$.

\paragraph{Recall:}
\begin{itemize}\item $f$ is \underline{surjective} aka \underline{onto},
\\    written $f : X \twoheadrightarrow  Y$,
\\    if for all $y \in Y$, $| f^{-1}(y) \;|\; \geq  1$

\item $f$ is \underline{injective} aka \underline{1-1},
\\    written $f : X \ensuremath{\lhook\joinrel\relbar\joinrel\rightarrow} Y$,
\\    if for all $y \in Y$, $| f^{-1}(y) \;|\; \leq  1$

\item $f$ is \underline{bijective} aka \underline{a (1-1) correspondence} aka \underline{invertible},
\\    written $f : X \xrightarrow{\equiv} Y$,
\\    if $f$ is both injective and surjective,
\\    i.e. if for all $y \in Y$, $| f^{-1}(y) \;|\; = 1$

\end{itemize}

\paragraph{Remark:}
\mbox{}\\    If $f$ is
\begin{itemize}\item injective then $|X| \leq  |Y|$ (Pigeonhole principle)
\item surjective then $|X| \geq  |Y|$
\item bijective then $|X| = |Y|$
\end{itemize}

\subsection*{Applications of the Pigeonhole principle}

\paragraph{Example:}
\mbox{}\\    If I take 13 coins,
\\    divide them into 9 piles, placed in a row,
\\    then there will be a group of neighbouring piles within the row
    such that there are exactly 4 coins in the group.
\\(Generally: $n$ coins, $m$ piles; must be $k$ coins in a contiguous group if
\mbox{}\\ \mbox{\hspace{2em}}$n+k < 2*m$
\\    (this isn't sharp))
\paragraph{Proof:}
\mbox{}\\    Let $a_i :=$ number of coins in first $i$ piles, $1 \leq  i \leq  9$.
\\    Consider the 18 numbers
\mbox{}\\ \mbox{\hspace{2em}}$a_1, a_2, ..., a_9, a_1+4, a_2+4, ..., a_9+4$.
\\    Since $1 \leq  a_i \leq  13$, these numbers are all between 1 and 17.
\\    So by the PP, two must be equal.

    Since no two $a_i$ are equal (since the piles are non-empty),
\\    and similarly no two $a_i+4$ are equal,
\\    we must have $a_i = a_j+4$ for some $i$,$j$.

    So $a_i - a_j = 4$,
\\    so 4 is the sum of the sizes of the piles after $i$ and up to $j$,
\\    namely piles $i+1,...,j$.

\paragraph{Example:}
\mbox{}\\    Using as many coins as I want,
\\    I make a row of $k$ piles.
\\    Then there is a group of neighbouring piles
    such that the number of coins in the group is divisible by $k$.
\paragraph{Proof:}
\mbox{}\\    Let $a_1,...,a_k$ be as above.

    Let $r_i$ be the remainder on dividing $a_i$ by $k$.
\\    If any $r_i=0$, we're done.
\\    Else, $0 < r_i < k$,
\\    so by the PP, two remainders are equal,
\\    $r_i = r_j$.
\\    But then $a_i - a_j$ is divisible by 7, and we conclude as in the previous
    example.


\subsection*{Packed Pigeonhole Principle}

\paragraph{Packed Pigeonhole Principle (PPP):}
\mbox{}\\    If there are more than $k*n$ pigeons in $n$ pigeonholes,
\\    then some pigeonhole contains more than $k$ pigeons.
\\(Note: "Packed" is not standard terminology. This principle is commonly
referred to as the pigeonhole principle. Brualdi calls something slightly more
general (but less pleasing) the "strong pigeonhole principle", but I don't
think we need to cover it)

\paragraph{Example:}
\mbox{}\\    If $a_1,...,a_{n^2+1}$ is a sequence of $n^2 + 1$ real numbers,
\\    there is a subsequence of length $n+1$ which is monotonic, i.e. is either
    (nonstrictly) increasing or (nonstrictly) decreasing.
\paragraph{Proof:}
\mbox{}\\    Suppose there is no increasing subsequence of length $n+1$.

    Let $l_i$ be the length of the longest increasing subsequence starting with
    $a_i$. So $1 \leq  l_i \leq  n$.

    So by the PPP, $n+1$ of these $n^2+1$ numbers are equal;
\\    say $l_{i_1} = ... = l_{i_{n+1}}$.

    Now suppose $a_{i_j} < a_{i_{j+1}}$.
\\    Then we can extend the longest increasing subsequence starting with
    $a_{i_{j+1}}$ to a longer one starting with $a_{i_j}$, by prepending $a_{i_j}$.
\\    This contradicts $l_{i_j} = l_{i_{j+1}}$.

    So $(a_{i_j})_j$ is a decreasing sequence of length $n+1$.

\paragraph{Abstract version:}
\mbox{}\\    If $f : X \twoheadrightarrow  Y$ is a surjection,
\\    and if all fibres are of size at most $k$,
\mbox{}\\ \mbox{\hspace{2em}}i.e. $| f^{-1}(y) \;|\; \leq  k$ for all $y$,
\\    then $|X| \leq  k|Y|$.

\paragraph{Remark (Division principle, map form):}
\mbox{}\\    If $f : X \twoheadrightarrow  Y$ is a surjection,
\\    and if all fibres have size exactly $k$,
\mbox{}\\ \mbox{\hspace{2em}}i.e. $| f^{-1}(y) \;|\; = k$ for all $y$,
\\    then $|X| = k|Y|$.

    (Proof: partition $X$ according to the value of $f$, apply division principle)

\paragraph{Averaging principle:}
\mbox{}\\    Given integers $a_1,...,a_n$,
\\    some $a_i$ is at least the average,
\mbox{}\\ \mbox{\hspace{2em}}$a_i \geq  (a_1+...+a_n)/n$
\\    (Note the average might not be an integer!)

\paragraph{Packed Pigeonhole follows from averaging:}
\mbox{}\\    if there are more than $k*n$ pigeons,
\\    then the average number of pigeons per pigeonhole is more than k;
\\    some pigeonhole has at least the average number of pigeons,
\\    so has more than $k$ pigeons.

\paragraph{Example:}
\mbox{}\\    Discs (p.75)
\section{Ramsey Theory}

\paragraph{Example:}
\mbox{}\\    Given 6 people,
\\    either there are 3 who all like each other,
\\    or there are 3 no two of whom like each other.

\paragraph{Abstract version:}
\mbox{}\\    $K_n :=$ "complete graph on $n$ vertices"
\mbox{}\\ \mbox{\hspace{2em}}$= n$ points with an edge between each pair.

    Colour the edges of $K_6$ each either red or blue,
\\    then there's a red copy of $K_3$ or there's a blue copy of $K_3$;
\\    i.e. there is a monochromatic triangle.

    Denote this fact
\mbox{}\\ \mbox{\hspace{2em}}$K_6 \rightarrow  K_3,K_3$

\paragraph{Proof:}
\mbox{}\\    Pick a vertex $v_0$.
\\    Consider the 5 edges from it.
\\    3 of them are red or 3 of them are blue, since $5 > (3-1) + (3-1)$.
\\    Say 3 are red, and consider the 3 other vertices of these red edges.
\\    If the edges between them are all blue, they form a blue triangle and
    we're done.
\\    Else, some edge is red; but then it along with the edges from $v_0$ form a
    red triangle, and we're done.

\paragraph{Ramsey's Theorem for 2-coloured graphs:}
\mbox{}\\    Given $n$ and $m$ positive integers,
\\    there exists $r$ such that for any red-blue colouring of the edges of $K_r$,
\\    there are $n$ vertices all edges between which are red
    or there are $m$ vertices all edges between which are blue.
\paragraph{Notation:}
\mbox{}\\    We write
\mbox{}\\ \mbox{\hspace{2em}}$K_r \rightarrow  K_n,K_m$
\\    to mean that $r$ has this property,
\\    and we let $r(m,n)$ ("the (m,n)th Ramsey number") be the least such $r$.
\paragraph{Remarks:}
\mbox{}\\    We saw that $K_6 \rightarrow  K_3,K_3$;
\\    it's easy to see that $K_5 \not\rightarrow  K_3,K_3$,
\\    so $r(3,3) = 6$.

    It has been shown that
\mbox{}\\ \mbox{\hspace{2em}}$r(3,4) = 9$
\mbox{}\\ \mbox{\hspace{2em}}$r(3,5) = 14$
\mbox{}\\ \mbox{\hspace{2em}}$r(4,4) = 18$

    $r(5,5)$ is unknown! All we know is
\mbox{}\\ \mbox{\hspace{2em}}$43 \leq  r(5,5) \leq  49$.

    Erd\"{o}s:
\\	"Suppose aliens invade the earth and threaten to obliterate it in a
	year's time unless human beings can find the Ramsey number for red
	five and blue five. We could marshal the world's best minds and
	fastest computers, and within a year we could probably calculate the
	value. If the aliens demanded the Ramsey number for red six and blue
	six, however, we would have no choice but to launch a preemptive
	attack."

\paragraph{Proof of Theorem:}
\mbox{}\\    Suppose inductively that
\mbox{}\\ \mbox{\hspace{2em}}$K_b \rightarrow  K_{n-1},K_m$
\\    and
\mbox{}\\ \mbox{\hspace{2em}}$K_c \rightarrow  K_n,K_{m-1}$.

    We show that
\mbox{}\\ \mbox{\hspace{2em}}$K_{b+c} \rightarrow  K_n,K_m$.

    So colour $K_{b+c}$, and suppose there's no red $K_n$ and no blue $K_m$.

    Pick a vertex $v_0$; consider the $b+c-1$ edges from it.
\\    Since $b+c-1 > (b-1)+(c-1)$,
\\    $b$ of the edges are red or $c$ of the edges are blue.

    Say $b$ are red.
\\	Consider the $K_b$ formed by the vertices these edges connect to $v_0$.
\\	By the inductive hypothesis, it contains a red $K_{n-1}$ or a blue $K_m$.
\\	If it contains a red $K_{n-1}$, adjoining $v_0$ yields a red $K_n$;
\\	contradiction.
\\	If it contains a blue $K_m$, then so does our original $K_{b+c}$;
\\	contradiction.

    A symmetrical argument applies in the case that $c$ of the edges from
    $v_0$ are blue.

\paragraph{Remark:}
\mbox{}\\    This proof yields a recursive upper bound on the Ramsey numbers:
\mbox{}\\ \mbox{\hspace{2em}}$r(m,n) \leq  r(n-1,m) + r(n,m-1)$
\\    (but this is far from sharp).
\section{Binomial coefficients}
\providecommand{\ddx}{\frac{d}{dx}}
\providecommand{\ddz}{\frac{d}{dz}}

\subsection*{Miscellaneous Curiosities}

\paragraph{Recall:}
\begin{itemize}\item For $n$ a non-negative integer and $r$ an integer,
\\	${n \choose r} =$ number of subsets of size $r$ of a set of size $n$
\mbox{}\\ \mbox{\hspace{2em}}$= \frac{n!}{n!(n-r)!} \quad$ if $0 \leq  r \leq  n$
\mbox{}\\ \mbox{\hspace{2em}}$= 0 \quad\quad$ else.

\item Pascal's triangle

\item Pascal's Formula: ${n \choose r} = {n-1 \choose r} + {n-1 \choose r-1}$

\item ${n \choose r} = {n \choose n-r}$

\item $\sum_{r=0}^n {n \choose r} = 2^n$

\item $(x+y)^n = \sum_{r=0}^n {n \choose r} x^{n-r}y^r$
\end{itemize}

\paragraph{Remark:}
\mbox{}\\    ${n \choose r} =$ number of paths from root of Pascal's triangle to the
    $(n,r)$ position.

\paragraph{Further identities:}
\begin{itemize}\item $k{n \choose k} = n{n-1 \choose k-1}$
        (immediate from ${n \choose k} = \frac{n(n-1)...(n-k+1)}{k(k-1)...1}$)

\item $(x+1)^n = \sum_{r=0}^n {n \choose r} x^r$

\item $0 = ((-1) + 1)^n = \sum_{r=0}^n {n \choose r} (-1)^r$;
\\        so alternating sum of binomial coefficients is 0;
\\        so sum of even coefficients = sum of odd coefficients $= 2^{n-1}$.
\end{itemize}

\paragraph{Yet further identities:}
\begin{enumerate}[(i)]\item ${n+1 \choose r+1} = \sum_{s=0}^n {s \choose r}$
\item $\sum_{r=0}^n {n \choose r}^2 = {2n \choose n}$
\item $\sum_{r=0}^n r {n \choose r} = n2^{n-1}$
\end{enumerate}
\paragraph{Proofs:}
\begin{enumerate}[(i)]\item Iteratively apply Pascal's formula:
\\        ${n+1 \choose r+1} = {n \choose r+1} + {n \choose r}$
\mbox{}\\ \mbox{\hspace{2em}}$= {n-1 \choose r+1} + {n-1 \choose r}$
\mbox{}\\ \mbox{\hspace{2em}}$= ...$
\mbox{}\\ \mbox{\hspace{2em}}$= {0 \choose r+1} + {0 \choose r} + ... + {n-1 \choose r}$
\mbox{}\\ \mbox{\hspace{2em}}$= {0 \choose r} + ... + {n-1 \choose r}$

        Alternative inductive proof:
\\        Easily holds for $n=r=0$.
\\        Suppose inductively it holds for smaller $n+r$.
\\        Then using Pascal's formula, we have:
\\        ${n+1\choose r+1} = {n \choose r+1} + {n \choose r}$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^{n-1} {k \choose r} + \sum_{k=0}^{n-1} {k \choose r-1}$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^{n-1} ({k \choose r} + {k\choose r-1})$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^{n-1} {k+1 \choose r}$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=1}^{n} {k \choose r}$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^{n} {k \choose r}$

\item Consider diamonds in Pascal's triangle.

	OR: Given a set $S$ of size $2n$, arbitrarily split it into two sets
	$S_1,S_2$ of size $n$.
\\	Then an $n$-subset $S'$ of $S$ corresponds to
	the pair $(S' \cap S_1, S' \cap S_2)$.
\\	The pairs of subsets arising in this way are precisely those of sizes
	summing to $n$,
\\	so
\mbox{}\\ \mbox{\hspace{4em}}${2n \choose n} = \sum_{r=0}^n {n \choose r}{n \choose n-r} = \sum_{r=0}^n {n \choose r}^2$

\item Neat algebraic proof:
\\        $n (x+1)^{n-1} = \ddx(x+1)^n = \ddx \sum_{r=0}^n {n \choose r} x^r$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{r=0}^n r {n \choose r} x^r$.
\\        This holds for all $x$; taking $x=1$ gives the result.

\end{enumerate}
\paragraph{Examples of (i):}
\begin{itemize}\item ${n \choose 1} = \sum_{s=0}^{n-1} {s \choose 0} = \sum_{s=0}^{n-1} 1 = n$
\item${n \choose 2} = \sum_{s=0}^{n-1} {s \choose 1} = \sum_{s=0}^{n-1} s =$ $n$th triangular number
\item${n \choose 3} = \sum_{s=0}^{n-1} {s \choose 2} =$ $n$th pyrimidal number
\end{itemize}


\subsection*{Multinomial theorem}

What is the coefficient $a_{r,s,t}$ of $x^ry^sz^t$ in the expansion of $(x+y+z)^n$?

Clearly $a_{r,s,t} \neq  0$ only if $r+s+t=n$.

$a_{r,s,t}$ is the number of ways of choosing $r$ $x$'s, $s$ $y$'s, and $t$
$z$'s from the $n$ factors $(x+y+z)$;
\\i.e. the number of strings like "xyzzyxyzzy" with this many of each letter;
\\i.e. the number of permutations of the multiset $\{ r*x, s*y, t*z \}$.

So as we saw before,
\mbox{}\\ \mbox{\hspace{2em}}$a_{r,s,t} = \frac{n!}{r!s!t!}$.

Write ${n \choose r\;s\;t}$ for this number.

Generalising to arbitrarily many variables, we have

\paragraph{Theorem:}
\mbox{}\\    $(x_1 + ... + x_t)^n = \sum_{n_i \geq  0,n_1+...+n_t=n}
        {n \choose n_1\;n_2\;...\;n_t} x_1^{n_1}x_2^{n_2}...x_t^{n_t}$

Here, ${n \choose n_1\;n_2\;...\;n_t} = \frac{n!}{n_1!...n_t!}$
are the \underline{multinomial coefficients} (only defined if $n_1+...+n_t=n$).

\paragraph{Note:}
\mbox{}\\    ${n \choose r} = {n \choose r\;n-r}$.

The number of terms in the multinomial expansion of $(x_1+...+x_t)^n$ is the
number of $n$-combinations with $t$ types in unlimited supply, which we saw is
\mbox{}\\ \mbox{\hspace{2em}}${n+t-1 \choose n}$.


\subsection*{Unnatural exponents: $(x+y)^\alpha$}

\paragraph{Theorem [Newton's Binomial Theorem]:}
\mbox{}\\    Let $\alpha$ be a real (or even complex) number.
\\    Suppose $0 \leq  |x| < |y|$.
\\    Then
\mbox{}\\ \mbox{\hspace{2em}}$(x+y)^\alpha = \sum_{k=0}^\infty {\alpha \choose k} x^k y^{\alpha-k}$

    where
\mbox{}\\ \mbox{\hspace{2em}}${\alpha \choose k} = \frac{\alpha(\alpha-1)...(\alpha-k+1)}{k!}$.

Note that for $\alpha$ natural, this agrees with our previous definition.

\paragraph{Proof (not on syllabus):}
\mbox{}\\    Dividing through by $y^\alpha$, sufficient to show that for $|z|<1$,
\mbox{}\\ \mbox{\hspace{2em}}$(1+z)^\alpha = \sum_{k=0}^\infty {\alpha \choose k} z^k$.

    We show this for complex $z$ with $|z|<1$.
\\    $(1+z)^\alpha = \exp(\alpha\log(1+z))$ for any choice of branch.
\\    This is holomorphic on the domain $|z|<1$,
\\    so the Taylor series at 0 converges to the value of the function on this domain.

    Since $\ddz (1+z)^\alpha = \alpha(1+z)^{\alpha-1}$ and $(1+0)^\alpha=1$,
\\    this gives
\mbox{}\\ \mbox{\hspace{2em}}$(1+z)^\alpha = ((1+z)^\alpha |_{z=0})\frac{z^0}{0!} +$
\mbox{}\\ \mbox{\hspace{4em}}$(\alpha(1+z)^{\alpha-1}|_{z=0})\frac{z^1}{1!} +$
\mbox{}\\ \mbox{\hspace{4em}}$(\alpha(\alpha-1)(1+z)^{\alpha-2}|_{z=0})\frac{z^2}{2!} + ...$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^\infty (\alpha(\alpha-1)...(\alpha-k+1))\frac{z^k}{k!}$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^\infty {\alpha \choose k} z^k$
\\    \qed

\paragraph{Examples:}
\begin{itemize}\item $\frac{1}{1+z} = (1+z)^{-1}$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^\infty {-1 \choose k} z^k$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^\infty \frac{(-1)*(-2)*...*(-k)}{k*(k-1)*...*1} z^k$
\mbox{}\\ \mbox{\hspace{2em}}$= \sum_{k=0}^\infty (-1)^k z^k$
\mbox{}\\ \mbox{\hspace{2em}}$= 1 - z + z^2 - z^3 + ...$

\item
\mbox{}\\ \mbox{\hspace{2em}}$\sqrt{37} = \sqrt{6^2+1} = 6\sqrt{1+1/36} = 6(1+1/36)^{1/2}$
\mbox{}\\ \mbox{\hspace{4em}}$= 6(\sum_{k=0}^\infty {1/2 \choose k} (1/36)^k)$

        Now for $k>0$,
\mbox{}\\ \mbox{\hspace{2em}}${1/2\choose k} = \frac{ \frac{1}{2}\frac{1-2}{2}...\frac{1-2(k-1)}{2} }{k!}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{(-1)^{k-1} 1*3*5*...*(2k-3)}{2^k k!}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{(-1)^{k-1} (2k-2)!}{2^k (2*4*...*(2k-2)) k!}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{(-1)^{k-1} (2k-2)!}{2^{2k-1} (k-1)! k!}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{(-1)^{k-1}}{k 2^{2k-1}} {2k-2 \choose k-1}$

        So
\mbox{}\\ \mbox{\hspace{2em}}$\sqrt{1+z} = 1 + \sum_{k=1}^\infty \frac{(-1)^{k-1}}{k 2^{2k-1}} {2k-2 \choose k-1} z^k$
\mbox{}\\ \mbox{\hspace{4em}}$= 1 + \frac{1}{2}z - \frac{1}{8}z^2 + \frac{1}{16}z^3 - \frac{1}{25}z^4 + ...$

        So $\sqrt{37} = 6(1+1/36)^{1/2}$
\mbox{}\\ \mbox{\hspace{2em}}$\approx 6(1 + 1/(2*36) - 1/(8*36^2) + 1/(16*36^3) - 1/(25*36^4))$
\mbox{}\\ \mbox{\hspace{2em}}$= 6.0828$

        (Error is very small: $(6*(1 + 1/(2*36) - 1/(8*36^2) + 1/(16*36^3) - 1/(25*36^4)))^2 = 36.99999992692224$)

\end{itemize}
\section{Partial Orders}

\subsection*{Basics}

A \underline{partial order} on a set $X$ is a binary relation $\leq $ which is
\begin{itemize}\item reflexive ($x=y => x\leq y$)
\item transitive (if $x\leq y$ and $y\leq z$ then $x\leq z$)
\item antisymmetric (if $x\leq y$ and $y\leq x$ then $x=y$).
\end{itemize}

A set $X$ equipped with a partial order $\leq $, denoted $(X;\leq )$, is called a
\underline{partially ordered set} or a \underline{poset}.

"$x\geq y$" means "$y\leq x$".
\\"$x<y$" means "$x\leq y$ and $x\neq y$".

\paragraph{Examples:}
\begin{enumerate}[(i)]\item The usual order $\leq $ on the integers.
\item The relation of divisibility is a partial order on the natural numbers;
\\	$(\mathbb{N};|)$ is the corresponding poset.
\item If $A$ is a set, the set of subsets of $A$ is partially ordered by
	inclusion, $\subseteq $.
\end{enumerate}

\paragraph{Hasse diagrams:}
\mbox{}\\    $x$ \underline{covers} $y$ if $x > y$ and there is no $z$ such that $x > z > y$.

    The \underline{Hasse diagram} of a finite poset $(X,\leq )$ consists of points for the
    elements of $X$ and a line drawn upwards from $y$ to $x$ whenever $x$
    covers $y$.

    (We will see below that every finite poset has a Hasse diagram.)

$x$ is \underline{minimal} if $x>y$ for no $y$.
\\$x$ is \underline{maximal} if $x<y$ for no $y$.

\paragraph{Lemma:}
\mbox{}\\    $<$ is transitive: if $x<y$ and $y<z$ then $x<z$.
\paragraph{Proof:}
\mbox{}\\    $x\leq z$ by transitivity of $\leq $.
\\    Suppose $x=z$.
\\    Then $y \leq  x$ and $x \leq  y$, so $x=y$ by antisymmetry, contradicting $x<y$.
\\    So $x\neq z$. \qed

\paragraph{Lemma:}
\mbox{}\\    Any finite poset has at least one minimal element, and at least one
    maximal element.
\paragraph{Proof:}
\mbox{}\\    Suppose $(X;\leq )$ has no minimal element.
\\    Then there exist arbitrarily long chains $x_1 > x_2 > x_3 > ... > x_n$.
\\    By transitivity of $>$, the $x_i$ are distinct, so we contradict
    finiteness. \qed

A partial order $\leq $ on a set $X$ is \underline{total} (aka \underline{linear}) if
    for all $x$ and $y$ in $X$, either $x\leq y$ or $y\leq x$.

\paragraph{Lemma:}
\mbox{}\\    Any finite total order $(X;\leq )$ can be enumerated as $X=\{x_1,...,x_n\}$
    with $x_i \leq  x_j$ iff $i \leq  j$.

    (i.e. $(X;\leq )$ is isomorphic to $\{1,...,n\}$ with the usual order.)
\paragraph{Proof:}
\mbox{}\\    If $x$ is minimal in a total order, then $x\leq y$ for any $y$.

    Let $x_1$ be minimal in $X$, then let $x_2$ be minimal in $X \setminus \{x_1\}$,
\\    and so on.

    Then $x_i \leq  x_j$ for $i \leq  j$.
\\    By antisymmetry, $x_i \not\leq  x_j$ for $i \not\leq  j$. \qed

\paragraph{Lemma:}
\mbox{}\\    Any finite poset $(X;\leq )$ can be linearised,
\\    i.e. there exists a total order $\leq '$ such that $x\leq y => x\leq 'y$.
\paragraph{Proof:}
\mbox{}\\    Let $x_1,...,x_n$ be the minimal elements of $(X;\leq )$.
\\    Let $X' := X \setminus \{x_1,...,x_n\}$.
\\    By induction, $(X';\leq )$ can be linearised, say to $\leq '$.
\\    Extend $\leq '$ to $X$ by defining
\begin{itemize}\item $x_i \leq ' x_j$ iff $i \leq  j$
\item $x_i \leq ' y$ for any $y \in X'$
\end{itemize}
    This is total. \qed

\paragraph{Consequence:}
\mbox{}\\    Any finite poset has a Hasse diagram:
\\	draw the points with heights ordered according to a linearisation of
	the partial order,
\\	then draw a line whenever $x$ covers $y$, which implies that $x$ is
	above $y$.
\\	(Nudge the points horizontally if there are any overlapping lines). 

\subsection*{Chains and antichains}

\paragraph{Definition:}
\mbox{}\\    Let $(X;\leq )$ be a poset.

    $\emptyset  \neq  C \subseteq  X$ is a \underline{chain} if $(C;\leq )$ is a total order.

    $\emptyset  \neq  A \subseteq  X$ is an \underline{antichain} if $a_1 \leq  a_2 => a_1 = a_2$
    for $a_i \in A$.

    A \underline{chain partition} is a partition $X = C_1 \cup ... \cup C_n$ by disjoint
    chains. 

    An \underline{antichain partition} is a partition $X = A_1 \cup ... \cup A_n$ by
    disjoint antichains. 

\paragraph{Theorem:}
\mbox{}\\    In a finite poset $(X;\leq )$,
\begin{enumerate}[(i)]\item The maximal size of a chain is equal to the minimal size of an
	antichain partition.
\item {[}Dilworth's theorem{]}
	The maximal size of an antichain is equal to the minimal size of a
	chain partition.
\end{enumerate}
\paragraph{Proof:}
\mbox{}\\    First observe that a chain and an antichain can have no more than 1 point
    in common,
\mbox{}\\ \mbox{\hspace{2em}}$| C \cap A \;|\; \leq  1$.

    So given an antichain partition and a chain,
\\    each element of the chain is in precisely one of the antichains,
\\    and no two elements of the chain are in the same antichain,
\\    so
\mbox{}\\ \mbox{\hspace{2em}}size of any chain $\leq $ size of any antichain partition
\\    so
\mbox{}\\ \mbox{\hspace{2em}}maximal size of a chain $\leq $ minimal size of an antichain partition.

    Similarly for (ii): given a chain partition, each element of an antichain
    is in exactly one of the chains in the partition,
\\    and no two elements of the antichain are in the same chain, so
\mbox{}\\ \mbox{\hspace{2em}}maximal size of an antichain $\leq $ minimal size of a chain partition.

    So it remains to see
\begin{enumerate}[(i)]\item There exists an antichain partition of size the maximal size of a chain;
\item There exists a chain partition of size the maximal size of an antichain.
\end{enumerate}

    These require separate arguments.

\begin{enumerate}[(i)]\item Let $C$ be a chain of maximal length.
\\	Say $C = \{c_1,...,c_n\}$ with $c_i \leq  c_j$ iff $i \leq  j$.

	For $i=1,...,n$, recursively define
\mbox{}\\ \mbox{\hspace{2em}}$A_i :=$ the set of minimal elements of $X \setminus (A_1 \cup ... \cup A_{i-1})$.

	Then $A_i$ is an antichain,
\\	and $c_i \in A_i$ so no $A_i$ is empty.

	If $x \in X \setminus \cup_i A_i$ then $x \geq  c_i$ for all $i$,
\\	so we could extend $C$ to a larger chain by adjoining $x$,
\\	contradicting maximality of $C$.
\\	So $X = \cup_i A_i$.

	So the $A_i$ form an antichain partition of size $n=|C|$ as required.

\item By induction on the size of $X$.

	First, suppose some antichain $A$ of maximal size $m$
	is not the set of maximal elements and
	is not the set of minimal elements.

	Let
\mbox{}\\ \mbox{\hspace{2em}}$A^+ := \bigcup_{a\in A} \{x \;|\; x \geq  a\}$
\mbox{}\\ \mbox{\hspace{2em}}$A^- := \bigcup_{a\in A} \{x \;|\; x \leq  a\}$.

	Then $A^+ \cup A^- = X$ by maximality of $A$,
\\	and $A^+ \cap A^- = A$.

	Now $A$ is a maximal-size antichain in $A^+$,
\\	and $A^+ \neq  X$ since $A$ is not the set of maximal elements,
\\	so by induction, $A^+$ has a chain partition of size $|A|$.

	Similarly, we have a chain partition of $A^-$. 

	For each element $a \in A$, $a$ is in one of the chains of $A^+$ and one
	of the chains of $A^-$, and the union of these two chains is a chain
	$C_a$ in $X$.

	Then $\{ C_a \;|\; a \in A\}$ is a chain partition of $X$ of size $m=|A|$.

	For the remaining case,
\\	suppose every maximal-size antichain is either
	the set of maximal elements or
	the set of minimal elements.

	Let $x$ be minimal and $y$ be maximal, with $x\leq y$.

	(To see that such that such $x$ and $y$ exist:
\\	we proved above that maximal and minimal elements always exist,
\\	so we only need to see that some minimal element is comparable with
	(and hence $\leq $) some maximal element.
\\	Otherwise, the minimal elements and the maximal elements
	together form an antichain,
\\	which contradicts our assumption unless the set of minimal elements is
	equal to the set of maximal elements,
\\	in which case we can take $x=y$.)

	Then $X \setminus \{x,y\}$ has no antichains of size $m$ but has an antichain
	of size $m-1$,
\\	so by induction it has a chain partition of size $m-1$.
\\	Adjoining the chain $\{x,y\}$, we obtain a chain partition of $X$ of size $m$.
\end{enumerate}
    \qed

\subsection*{Bonus: Sperner's Theorem}

\newcommand{\flrhn}{\lfloor\frac{n}{2}\rfloor}
\paragraph{Example:}
\mbox{}\\    Inductively define "symmetric" chain partitions $S_n$ of the set of subsets of
    $\{1,...,n\}$:

	Let $S_1$ be the partition with only one chain,
\mbox{}\\ \mbox{\hspace{2em}}$\emptyset  \subsetneq  \{1\}$.

	Given a chain partition $S_n$ of $\{1,...,n\}$,
\\	let $S_{n+1}$ have, for each chain $A_1 \subsetneq  ... \subsetneq  A_k$ of $S_n$,
\begin{itemize}\item the chain $A_1 \subsetneq  ... \subsetneq  A_k \subsetneq  A_k \cup \{n+1\}$,
\item   and, if $k>1$, the chain $A_1\cup\{n+1\} \subsetneq  ... \subsetneq  A_{k-1} \cup \{n+1\}$.
\end{itemize}

    Each chain $A_1 \subsetneq  ... \subsetneq  A_k$ in $S_n$
    has subsequent elements differing in size by one,
\\    and $|A_1| + |A_k| = n$.

    Hence each chain contains a subset of size $\flrhn$,
\\    so $|S_n| = {n \choose \flrhn}$.

    ($\flrhn = n/2$ "rounded down")

    So by Dilworth, we obtain "Sperner's Theorem":
\\    The set of subsets of $\{1,...,n\}$ of size $\flrhn$ is a maximal-size
    antichain.
\section{Inclusion-Exclusion}

Let $A_1$ and $A_2$ be finite subsets of a set $X$.
\\If $A_1$ and $A_2$ are disjoint, the addition principle tells us
$|A_1\cup A_2| = |A_1| + |A_2|$.

If they're not disjoint, this "double-counts" the elements of the
intersection; we can fix this by subtracting the size of the intersection,
\\yielding the general formula
$|A_1\cup A_2| = |A_1| + |A_2| - |A_1\cap A_2|$.

For three finite subsets $A_1$,$A_2$,$A_3$ of some set $X$, similar reasoning
yields
\mbox{}\\ \mbox{\hspace{2em}}$|A_1 \cup A_2 \cup A_3| =$
\mbox{}\\ \mbox{\hspace{4em}}$|A_1| + |A_2| + |A_3| - (|A_1\cap A_2| + |A_2\cap A_3| + |A_1\cap A_3|) + |A_1\cap A_2\cap A_3|$.

\paragraph{Example:}
\mbox{}\\    {\it How many integers in [1,100] are divisible by 2, 5, or 7?}

    Let $D_n := \{ k \in \mathbb{Z} \cap [1,100] : n \;|\; k \}$.

    Note $|D_n| = \lfloor\frac{100}n\rfloor$.

    By inclusion-exclusion,
\mbox{}\\ \mbox{\hspace{2em}}$|D_2 \cup D_5 \cup D_7| = |D_2| + |D_5| + |D_7|$
\mbox{}\\ \mbox{\hspace{4em}}$- (|D_2 \cap D_5| + |D_5 \cap D_7| + |D_2 \cap D_7|)$
\mbox{}\\ \mbox{\hspace{4em}}$+ |D_2 \cap D_5 \cap D_7|$
\mbox{}\\ \mbox{\hspace{2em}}$= |D_2| + |D_5| + |D_7| - (D_{10} + D_{35} + D_{14}) + D_{70}$
\mbox{}\\ \mbox{\hspace{2em}}$= 50 + 20 + 14 - (10 + 2 + 7) + 1$
\mbox{}\\ \mbox{\hspace{2em}}$= 66$.

\paragraph{Theorem [Inclusion-Exclusion Principle]:}
\mbox{}\\    Let $A_1, ..., A_n$ be finite subsets of a set $X$.
\\    Then
\mbox{}\\ \mbox{\hspace{2em}}$|A_1\cup...\cup A_n| =$
\mbox{}\\ \mbox{\hspace{4em}}$\sum_i |A_i|$
\mbox{}\\ \mbox{\hspace{4em}}$- \sum_{i<j} |A_i \cap A_j|$
\mbox{}\\ \mbox{\hspace{4em}}$+ \sum_{i<j<k} |A_i \cap A_j \cap A_k|$
\mbox{}\\ \mbox{\hspace{4em}}$- ...$
\mbox{}\\ \mbox{\hspace{4em}}$+ (-1)^{n-1} |A_1 \cap ... \cap A_n|$
\paragraph{Proof:}
\mbox{}\\    Let $x \in A_1\cup...\cup A_n$.
\\    We show that $x$ "contributes 1" to the right hand side.
\\    Say $x$ is in $m\geq 1$ of the $n$ sets.
\\    Then $x$ contributes 1 to $m={m \choose 1}$ of the $|A_i|$,
\\    to ${m \choose 2}$ of the $|A_i \cap A_j|$,
\\    to ${m \choose 3}$ of the $|A_i \cap A_j \cap A_k|$,
\\    and so on.

    So $x$ contributes
\mbox{}\\ \mbox{\hspace{2em}}$\sum_{k>0} (-1)^{k-1} {m \choose k}$
\mbox{}\\ \mbox{\hspace{4em}}$= - \sum_{k>0} (-1)^k {m \choose k}$
\mbox{}\\ \mbox{\hspace{4em}}$= 1 - \sum_{k\geq 0} (-1)^k {m \choose k}$
\mbox{}\\ \mbox{\hspace{4em}}$= 1 - 0$
\mbox{}\\ \mbox{\hspace{4em}}$= 1$

\paragraph{Remark:}
\mbox{}\\    Neat alternative expression:
\mbox{}\\ \mbox{\hspace{2em}}$\left|{\bigcup_i A_i}\right| = \sum_{ \emptyset  \neq  I \subseteq  \{1,...,n\} } (-1)^{|I| - 1} \left|{\bigcap_{i \in I} A_i}\right|$.

\paragraph{Bonus:}
\mbox{}\\    Version of the proof using this notation:
    \begin{align*} \left|{\bigcup_i A_i}\right| &= \sum_{x \subseteq  \bigcup_i A_i} 1 \\
\\    &= \sum_{x \subseteq  \bigcup_i A_i} \left( - \sum_{k>0} (-1)^k { \# \{i \;|\; x \in A_i\} \choose k} \right)\\
\\    &= \sum_{x \subseteq  \bigcup_i A_i, I\subseteq \{i \;|\; x \in A_i\} } (-1)^{|I| - 1} \\
\\    &= \sum_{\{(x,I) \;|\; x \in \bigcup_i A_i, I \subseteq  \{1,...,n\}, x \subseteq  \bigcap_{i \in I} A_i\}} (-1)^{|I| - 1} \\
\\    &= \sum_{\emptyset  \neq  I \subseteq  \{1,...,n\}} (-1)^{|I| - 1} \left|{\bigcap_{i \in I} A_i}\right|
    \end{align*}

\paragraph{Example:}
\mbox{}\\    {\it How many strings of 8 letters from the Roman alphabet contain 'j', 'q', 'x', 'y' and 'z'?}

    We could do this positively, but it would be fiddly.

    Instead, let's count the number of strings which {\bf don't} contain all of
    these letters, i.e. which omit 'j' or omit 'q' or... .

    Let $O_j$ be the strings which omit 'j', $O_{jq}$ the strings which omit
    'j' {\bf and} 'q', and so on.

    Then by inclusion-exclusion,
\mbox{}\\ \mbox{\hspace{2em}}$|O_j \cup O_q \cup O_x \cup O_y \cup O_z| = |O_j| + |O_q| + ...$
\mbox{}\\ \mbox{\hspace{4em}}$- (|O_{jq}| + |O_{jx}| + ...)$
\mbox{}\\ \mbox{\hspace{4em}}$+ (|O_{jqx}| + |O_{jqy}| + ...)$
\mbox{}\\ \mbox{\hspace{4em}}$- (|O_{jqxy}| + |O_{jqxz}| + ...)$
\mbox{}\\ \mbox{\hspace{4em}}$+ |O_{jqxyz}|$

    Now $|O_j| = |O_q| = ... = 25^8$,
\\    and $|O_{jq}| = |O_{jx}| = ... = 24^8$,
\\    and so on.

    So
\mbox{}\\ \mbox{\hspace{2em}}$|O_j \cup O_q \cup O_x \cup O_y \cup O_z| =$
\mbox{}\\ \mbox{\hspace{4em}}${5 \choose 1} 25^8 - {5\choose 2}24^8 + {5 \choose 3} 23^8 - {5\choose 4}22^8 + {5 \choose 5} 21^8$

    and the answer to the original question is
    $26^8 - |O_j \cup O_q \cup O_x \cup O_y \cup O_z|$, which comes to
    87408720.


\subsection*{Combinations of multisets, revisited}

\paragraph{Recall:}
\mbox{}\\    The number of $r$-combinations of a multiset with at least $r$ of each of
    its $t$ types is ${ r+t-1 \choose t-1}$.

If there are fewer than $r$ of some of the types, we can use
inclusion-exclusion.

This is clearest if we transform the problem.

An $r$-combination of a multiset
\mbox{}\\ \mbox{\hspace{2em}}$\{ c_1 * a_1, ... , c_t * a_t \}$
\\corresponds to a solution in non-negative integers of the equation
\mbox{}\\ \mbox{\hspace{2em}}$x_1 + ... + x_t = r$
\\subject to the constraints
\mbox{}\\ \mbox{\hspace{2em}}$x_1 \leq  c_1, ..., x_t \leq  c_t$.

\paragraph{Concrete example:}
\mbox{}\\    I take 8 marbles from a bag containing 3 red marbles, 2 blue marbles, and
    10 green marbles. How many possibilities are there for the numbers of each
    colour I get? Equivalently, what is
    \[ |\{(r,b,g) \;|\; r+b+g=8, 0\leq r\leq 3, 0\leq b\leq 2\}| ?\]

So we want to count the number of such solutions,
\\and we know that the answer is ${ r+t-1 \choose t-1}$ if there are no
constraints.

By the subtraction principle,
\\the number of solutions in the constrained case is the number in the
unconstrained case minus the number which fail at least one constraint,
\mbox{}\\ \mbox{\hspace{2em}}${r+t-1 \choose t-1} - |F_1 \cup ... \cup F_t|$,
\\where $F_i := \{ (x_1,...,x_t) \;|\; x_1 + ... + x_t = r, x_i > c_i \}$.

So we can use the inclusion-exclusion principle if we can determine the sizes
of the intersections of the $F_i$.

If $c_i \geq  r$, then $F_i=\emptyset $.
\\Otherwise, subtracting $c_i+1$ from $x_i$ puts $F_i$ in correspondence with
$\{ (y_1,...,y_t) \;|\; y_1 + ... + y_t = r-(c_i+1), y_i \geq  0 \}$,
\\so $|F_i| = {r-(c_i+1) + t - 1 \choose t - 1}$.

Similarly, $|F_i \cap F_j| = {r-(c_i+1)-(c_j+1)+t-1 \choose t-1}$, and so on.

So inclusion-exclusion yields
\mbox{}\\ \mbox{\hspace{2em}}$\sum_{I \subseteq  \{1,...,t\}} (-1)^{|I|} { r-(\sum_{i \in I} (c_i+1)) +t-1 \choose t-1 }$.

\paragraph{Marble example:}
\mbox{}\\ \mbox{\hspace{2em}}${8+2 \choose 2} - {8-(3+1)+2 \choose 2} - {8-(2+1)+2 \choose 2} + {8-(3+1)-(2+1)+2\choose 2}$
\mbox{}\\ \mbox{\hspace{4em}}$= {10\choose 2} - {6\choose 2} - {7\choose 2} + {3 \choose 2}$
\mbox{}\\ \mbox{\hspace{4em}}$= 12$

\paragraph{Scrabble example:}
\mbox{}\\    How many 7-tile hands can be drawn from a standard 100-tile bag of
    scrabble tiles?
\\    Using the above formula, my computer calculates it as
    3199724.

    (for the curious, here's the Haskell code I used to calculate this:
\\\begin{verbatim}
 import Math.Combinatorics.Binomial (choose)
 combs :: Int -> [Int] -> Int
 combs r cs =
    let
        t = length cs
        subs = subs' [] cs
        -- subs': returns relevant subsequences of cs, omitting those which
        -- will contribute 0 to the final sum (without this, the algorithm
        -- would have complexity exponential in t)
        subs' sub [] = [sub]
        subs' sub _ | (sum (map (+1) sub) > r) = []
        subs' sub (c:cs) = subs' (c:sub) cs ++ subs' sub cs
  in sum [ (-1)^(length sub) *
         choose (r - sum (map (+1) sub) + t-1) (t-1) | sub <- subs ]
 scrabbleBag :: [Int]
 scrabbleBag = concat [ replicate n c | (n,c) <-
         [(5,1), (10,2), (1,3), (4,4), (3,6), (1,8), (2,9), (1,12)] ]
 main :: IO ()
 main = print $ combs 7 scrabbleBag
\end{verbatim}
)


\subsection*{Derangements}

A \underline{derangement} is a permutation which leaves nothing in its original position.

e.g.
\\\\{\tt (5,3,4,2,1)} is a derangement of 
\\{\tt (1,2,3,4,5)}, and
\\"{\tt endgreatmen}" is a derangement of
\\"{\tt derangement}".

$D_n :=$ the number of derangements of a sequence of length $n$,
\mbox{}\\ \mbox{\hspace{2em}}$=$ number of derangements of $(1,2,...,n)$.

We can use inclusion-exclusion to determine $D_n$.

A derangement of $(1,...,n)$ is a permutation $(a_1,...,a_n)$ which
satisfies the conditions $a_1 \neq  1, ..., a_n \neq  n$.

Let $P_i$ be the set of permutations which fail the $i$th of these conditions,
\\i.e. such that $a_i=i$.

Easily, for $I \subseteq  \{1,...,n\}$,
\mbox{}\\ \mbox{\hspace{2em}}$|\bigcap_{i\in I} P_i| = (n - |I|)!$.

So by inclusion-exclusion,
\mbox{}\\ \mbox{\hspace{2em}}$D_n = n! - |\bigcup_i P_i|$
\mbox{}\\ \mbox{\hspace{4em}}$= n! - \sum_{\emptyset  \neq  I \subseteq  \{1,...,n\}} (-1)^{|I| - 1} |\bigcap_{i\in I} P_i|$
\mbox{}\\ \mbox{\hspace{4em}}$= n! - \sum_{\emptyset  \neq  I \subseteq  \{1,...,n\}} (-1)^{|I| - 1} (n - |I|)!$
\mbox{}\\ \mbox{\hspace{4em}}$= n! - \sum_{i=1}^n (-1)^{i-1} {n\choose i}(n-i)!$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{i=0}^n (-1)^i {n\choose i}(n-i)!$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{i=0}^n (-1)^i \frac{n!}{i!}$
\mbox{}\\ \mbox{\hspace{4em}}$= n!\sum_{i=0}^n \frac{(-1)^i}{i!}$

Note then that the probability that a random $n$-permutation is a derangement
is
\mbox{}\\ \mbox{\hspace{2em}}$Pr_n = \frac{|D_n|}{n!} = \sum_{i=0}^n \frac{(-1)^i}{i!}$,
\\so
\mbox{}\\ \mbox{\hspace{2em}}$\lim_{n \rightarrow  \infty} Pr_n = \sum_{i=0}^\infty \frac{(-1)^i}{i!}$
\mbox{}\\ \mbox{\hspace{4em}}$= e^{-1} \approx 0.368$

The convergence is very fast;
\\e.g. $Pr_n \approx 0.368$ to 3 significant figures for $n\geq 6$.

\paragraph{Example:}
\mbox{}\\    A deranged scientist removes the heads from a large number of different
    animals and re-attaches them at random. What is the probability that every
    resulting creature is a chimera, i.e. that no head is reattached to its
    own body?
\paragraph{Answer:}
\mbox{}\\    About $e^{-1}$.
\section{Number sequences}

A \underline{number sequence} is simply an infinite sequence $h_0,h_1,h_2,...$ of numbers.
\\For us, $h_i$ will typically be an integer.

\paragraph{Examples:}
\mbox{}\\    1,2,3,4,5,...
\\    2,4,8,16,32,...
\\    2,3,5,7,13,...
\\    1,1,2,3,5,8,13,...
\\    1,5,10,10,5,1,0,0,0,0,...

\subsection*{Generating functions}

The \underline{generating function} of a number sequence $h_0,h_1,....$ is the formal
power series $g(x) = \sum_{n=0}^\infty h_n x^n$.

\paragraph{Technical remark:}
\mbox{}\\    Despite the notation and terminology, we do not assume any convergence;
\\    we do not need $g(a)$ to make sense for $a$ a real number,
\\    so $g$ doesn't really have to be a function in the usual sense.
\\    for example, $\sum_{n=0}^\infty n^n x^n$ doesn't converge for $x\neq 0$,
\\    but it's a perfectly good generating function.

    We use the usual algebraic notation for generating functions. We can make
    sense of algebraic operations as follows:\;

    Given formal power series $g(x)=\sum_{n=0}^\infty h_n x^n$ and
    $g'(x)=\sum_{n=0}^\infty h'_n x^n$, and a number $c$, we define
\mbox{}\\ \mbox{\hspace{2em}}$g(x)+g'(x) := \sum_{n=0}^\infty (h_n+h'_n) x^n$
\mbox{}\\ \mbox{\hspace{2em}}$c g(x) := \sum_{n=0}^\infty ch_n x^n$
\mbox{}\\ \mbox{\hspace{2em}}$g(x)g'(x) := \sum_{n=0}^\infty (\sum_{j=0}^n h_jh'_{n-j}) x^n$.

    We also write $c(x) = \frac{a(x)}{b(x)}$ to mean that $a(x) = b(x)c(x)$
    (this is well-defined).

We can often use this algebraic structure to write generating functions compactly.

\paragraph{Example 1:}
\mbox{}\\    Consider the binomial coefficients ${m\choose n}$ for a fixed $m$.
\\    This is a finite number sequence, but we can make it infinite by appending
    0s,
\mbox{}\\ \mbox{\hspace{2em}}${m\choose 0},{m\choose 1},...,{m\choose m},0,0,0,...$.

    So the generating function is
\mbox{}\\ \mbox{\hspace{2em}}${m\choose 0} + {m\choose 1}x + {m \choose 2} x^2 ... + {m \choose m} x^m$
\mbox{}\\ \mbox{\hspace{4em}}$= (x+1)^m$

\paragraph{Example 2:}
\mbox{}\\    The generating function of the number sequence
\mbox{}\\ \mbox{\hspace{2em}}$1,1,1,...$
\\    is $g(x) = \sum_{n=0}^\infty x^n = 1+x+x^2+...$.

    Now, multiplying out,
\mbox{}\\ \mbox{\hspace{2em}}$(1+x+x^2+...)(1-x) = 1 + (-1+1)x + (-1+1) x^2 + ...$,
\\    so $g(x) = 1/(1-x)$.
\\    \qed

Generating functions provide an efficient notation for describing and
manipulating classes of combinatorial problems.

\paragraph{Example 3:}
\mbox{}\\    Given $t$, let $h_n$ be the number of $n$-combinations of a multiset with
    $t$ types and infinite multiplicity for each type.

    We know $h_n = {n+t-1\choose t-1}$, so
    the generating function is
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = \sum_{n=0}^\infty h_n x^n = \sum_{n=0}^\infty {n+t-1\choose t-1} x^n$.

    But note also that 
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = (1+x+x^2+...)^t$,
\\    since when we multiply the right hand side out,
\\    the coefficient of $x^n$ is precisely the number of ways of obtaining
    $x^n$ as $x^{e_1}x^{e_2}...x^{e_t}$,
\\    which is the number of solutions in non-negative integers to
    $e_1+...+e_k=n$,
\\    which (as we've seen before) is $h_n$.

    So as in the previous example,
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = (1+x+x^2+...)^t = \left(\frac{1}{1-x}\right)^t = \frac{1}{(1-x)^t}$.
\\    \qed

Note we found here the power series expansion of $\frac 1{(1-x)^t}$, which
will come in handy later.

\paragraph{Lemma 1:}
\mbox{}\\ \mbox{\hspace{2em}}$\frac{1}{(1-x)^t} = \sum_{n=0}^\infty {n+t-1 \choose t-1} x^n$

If we have restrictions on how many of each type we're allowed to take in a
combination, we can incorporate these into an algebraic expression for the
generating function.

\paragraph{Example 4:}
\mbox{}\\    {\it Find the generating function for the number $h_n$ of bags of $n$ marbles
    consisting of an even number of red marbles, at least 1 green marble, at
    most 36 blue marbles, and an odd number of yellow marbles.}

    Arguing as in the previous example, the generating function is
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = (1+x^2+x^4+...)(x+x^2+x^3+...)(1+x+x^2+...+x^36)(x+x^3+x^5+...)$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac 1{1-x^2}\frac x{1-x}\frac{1-x^{37}}{1-x}\frac x{1-x^2}$.
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{x^2(1-x^{37})}{(1-x^2)^2(1-x)^2}$.

\paragraph{Example 5:}
\mbox{}\\    {\it Find the generating function for the number $h_n$ of bags of $n$ marbles
    consisting of an even number of red marbles, a multiple of 3 of green marbles, at
    most 2 blue marbles, and at most one yellow marble. Hence explicitly
    determine $h_n$.}

\mbox{}\\ \mbox{\hspace{2em}}$g(x) = (1+x^2+x^4+...)(1+x^3+x^6+...)(1+x+x^2)(1+x)$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac 1{1-x^2}\frac 1{1-x^3}\frac{1-x^3}{1-x}(1+x)$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{1+x}{(1-x^2)(1-x)}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{1+x}{(1+x)(1-x)(1-x)}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{1}{(1-x)^2}$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{n=0}^\infty {n+2-1\choose 2-1} x^n$ (by Lemma 1) 
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{n=0}^\infty (n+1) x^n$.

    So there are $n+1$ such bags of $n$ marbles!

    (Exercise: find a direct proof of this, without going via generating
    functions.)

\paragraph{Example 6:}
\mbox{}\\    {\it Find the generating function for the number $h_n$ of ways of making
    $n$ cents out of Canadian coins.}

    The coins in current circulation are worth 5,10,25,100, and 200 cents
    each.

    So $h_n$ is the number of solutions in non-negative integers to
\mbox{}\\ \mbox{\hspace{2em}}$5N+10D+25Q+100L+200T=n$.
\\    Equivalently, $h_n$ is the number of solutions to
\mbox{}\\ \mbox{\hspace{2em}}$e_1+e_2+e_3+e_4+e_5=n$
\\    where $e_1$ is a multiple of 5, $e_2$ is a multiple of 10, etc.

    So as above,
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = (x^5+x^{10}+x^{15}+...)(x^{10}+x^{20}+...)...(x^{200}+x^{400}+...)$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac 1{(1-x^5)(1-x^{10})...(1-x^{200})}$.

\subsection*{Exponential Generating Functions}

The \underline{exponential generating function} of a number sequence $h_0,h_1,...$ is
the formal power series
\mbox{}\\ \mbox{\hspace{2em}}$g^{(e)}(x) = \sum_{n=0}^\infty h_n \frac{x^n}{n!}$.

While ordinary generating functions are useful for counting combinations,
\\exponential generating functions are useful for counting permutations.

\paragraph{Example 7:}
\mbox{}\\    The exponential generating function of
\mbox{}\\ \mbox{\hspace{2em}}$(m,0),P(m,1),...,P(m,m),0,0,0,...$
\\    is
\mbox{}\\ \mbox{\hspace{2em}}$g^{(e)} = \sum_{n=0}^m \frac{m!}{(m-n)!}\frac{x^n}{n!}$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{n=0}^m {m\choose n}x^n$
\mbox{}\\ \mbox{\hspace{4em}}$= (1+x)^m$

\paragraph{Example 8:}
\mbox{}\\    Let $h_n$ be the number of $n$-permutations of a multiset with $k$
    different types, each with infinite multiplicity,
\mbox{}\\ \mbox{\hspace{2em}}$\{\infty\cdot a_1,..., \infty \cdot a_k\}$.


    So $h_n=k^n$.
\\    Then the exponential generating function is
    : $g^{(e)}(x) = \sum_{n=0}^\infty \frac{k^nx^n}{n!} = e^{kx}$.

(remark for anyone who might worry what exactly we mean by this last
equality: we can just \underline{define} $e^{ax}$ to be the formal power series
$\sum_{n=0}^\infty \frac{a^n}{n!}x^n$. This obeys the usual law $e^{ax}e^{bx}
= e^{(a+b)x}$. We could define more, but this will suffice for our purposes.)

\paragraph{Theorem:}
\mbox{}\\    Let $h_n$ be the number of $n$-permutations of the multiset
\mbox{}\\ \mbox{\hspace{2em}}$S := \{n_1\cdot a_1,..., n_k \cdot a_k\}$,
\\    with $n_i \in \mathbb{N}\cup\{\infty\}$.

    Then the exponential generating function is
\mbox{}\\ \mbox{\hspace{2em}}$g^{(e)} = f_{n_1}(x)f_{n_2}(x)...f_{n_k}(x)$
\\    where
\mbox{}\\ \mbox{\hspace{2em}}$f_{n}(x) = \sum_{i=0}^n \frac{x^i}{i!}$
\\    and in particular, $f_\infty(x) = e^x$.

\paragraph{Proof:}
\mbox{}\\ \mbox{\hspace{2em}}$h_n = \sum_{\textrm{$S'$ an $n$-combination of $S$}} (\textrm{number of permutations of $S'$})$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{\{(m_1,...,m_k) \;|\; m_1+...+m_k=n,\; 0\leq m_i\leq n_i \}}$
\mbox{}\\ \mbox{\hspace{4em}}\;\;\;\; $(\textrm{number of permutations of $\{m_1 * a_1,...,m_k*a_k\}$}$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{\{(m_1,...,m_k) \;|\; m_1+...+m_k=n,\; 0\leq m_i\leq n_i \}} \frac{n!}{m_1!...m_k!}$
\mbox{}\\ \mbox{\hspace{4em}}$= n! \sum_{\{(m_1,...,m_k) \;|\; m_1+...+m_k=n,\; 0\leq m_i\leq n_i \}} \frac{1}{m_1!...m_k!}$

    Meanwhile, if we multiply out $f_{n_1}(x)f_{n_2}(x)...f_{n_k}(x)$, we find
    the coefficient of $x^n$ is 
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{\{(m_1,...,m_k) \;|\; m_1+...+m_k=n,\; 0\leq m_i\leq n_i \}} \frac{1}{m_1!...m_k!}$.

    So this is indeed the exponential generating function.
\\    \qed

Just as we saw with ordinary generating functions, if we have restrictions on
how many of each type we are allowed in a permutation, we can incorporate
these restrictions into the factors in the above expression for the
exponential generating function, by only including the appropriate powers of
$x$.

Often, expanding out the resulting power series will give us a solution to the
combinatorial problem, as the following example demonstrates.

\paragraph{Example 9:}
\mbox{}\\    {\it How many $n$-digit numbers can be written using only the digits
    '1','2', and '3', using an even number of '2's and at least 1 '3'?}

    The exponential generating function is
\mbox{}\\ \mbox{\hspace{2em}}$g^{(e)}(x) = (\sum_{n=0}^\infty \frac{x^n}{n!})(\sum_{n=0}^\infty \frac{x^{2n}}{(2n)!})(\sum_{n=1}^\infty \frac{x^n}{n!})$
\mbox{}\\ \mbox{\hspace{4em}}$= (e^x)(\frac{e^x+e^{-x}}2)(e^x-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac 12 (e^{3x}+e^x-e^{2x}-1)$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{n=1}^\infty \frac{3^n + 1 - 2^n}{2n!}$

    So the answer is $\frac{3^n + 1 - 2^n}2$
\section{Recurrence relations}

\subsection*{Warm-up: The Fibonacci sequence}

The {\bf Fibonacci sequence} is the sequence $f_n$ satisfying
\mbox{}\\ \mbox{\hspace{2em}}$f_0 = 0, f_1=1$
\mbox{}\\ \mbox{\hspace{2em}}$f_{n+2} = f_n + f_{n+1}$
\\so
\mbox{}\\ \mbox{\hspace{2em}}0,1,1,2,3,5,8,13,21,34,55,...

Such an expression for a term in a sequence as a function of previous terms 
is called a {\bf recurrence relation}.

\paragraph{Other examples:}
\mbox{}\\ \mbox{\hspace{2em}}$h_0=1$
\mbox{}\\ \mbox{\hspace{2em}}$h_{n+1}=h_n+3$

\mbox{}\\ \mbox{\hspace{2em}}$h_0=1$
\mbox{}\\ \mbox{\hspace{2em}}$h_{n+1}=3h_n$

In these cases, we can easily find an expression for $h_n$ in terms of $n$.

Can we do this for $f_n$?

To do so, we should consider the more general problem where we vary the
initial values $f_0$ and $f_1$, and just consider sequences $f_n$ satisfying
the recurrence relation $f_{n+2}=f_n+f_{n+1}$.

If $f_n$ and $f'_n$ are two such sequences, then so is $c_1f_n+c_2f'_n$ for
any $c_1,c_2$ (i.e. the solutions form a vector space).

So if we can find {\bf some} solutions to the Fibonacci recurrence relation, we
can easily generate more - perhaps including the Fibonacci sequence itself.

(In fact, if you recall your linear algebra, you should be able to see that we
only need to find two linearly independent sequences to generate all of them)

Let's look for solutions of the particularly simple form
\mbox{}\\ \mbox{\hspace{2em}}$f_n = q^n$
\\with $q \neq  0$. Then the recurrence relation becomes
\mbox{}\\ \mbox{\hspace{2em}}$q^{n+2}=q^n+q^{n+1}$
\mbox{}\\ \mbox{\hspace{2em}}$\leftrightarrow  q^n(q^2-q-1)=0$
\mbox{}\\ \mbox{\hspace{2em}}$\leftrightarrow  (q^2-q-1)=0$ (since $q\neq 0$)

This is a quadratic equation, so it has two solutions.

They are
\mbox{}\\ \mbox{\hspace{2em}}$\phi = \frac{1+\sqrt 5}2, \phi' = \frac{1-\sqrt 5}2$.

($\phi$ is known as the Golden Ratio; it is the unique positive real
satisfying $\frac{1+\phi}\phi = \phi$)

So for any $c_1$ and $c_2$,
\mbox{}\\ \mbox{\hspace{2em}}$c_1\phi^n + c_2\phi'^n$
\\satisfies the Fibonacci recurrence relation.

Let's find such a sequence which satisfies the initial conditions of the
Fibonacci sequence, $f_0=f_1=1$; then it must be the Fibonacci sequence.

\mbox{}\\ \mbox{\hspace{2em}}$c_1 + c_2 = 0$
\mbox{}\\ \mbox{\hspace{2em}}$c_1\phi + c_2\phi' = 1$

We can solve this system of simultaneous equations

\[ \begin{pmatrix}
	1 & 1 \\\phi & \phi'
    \end{pmatrix}
    \begin{pmatrix}
	c_1 \\c_2
    \end{pmatrix}
=
    \begin{pmatrix}
	0 \\1
    \end{pmatrix}
\]
\[
    \begin{pmatrix}
	c_1 \\c_2
    \end{pmatrix}
=
    \frac 1{\phi'-\phi}
    \begin{pmatrix}
	\phi' & -1 \\-\phi & 1
    \end{pmatrix}
    \begin{pmatrix}
	0 \\1
    \end{pmatrix}
=
    \frac 1{\sqrt 5}
    \begin{pmatrix}
	-1 \\1
    \end{pmatrix}
\]

So we obtain

\paragraph{Theorem:}
\mbox{}\\    The Fibonacci numbers are
\mbox{}\\ \mbox{\hspace{2em}}$f_n = \frac{\phi^n - \phi'^n}{\sqrt 5}$
\\    where
\mbox{}\\ \mbox{\hspace{2em}}$\phi = \frac{1+\sqrt 5}2, \phi' = \frac{1-\sqrt 5}2$.

Note that $\phi'=1-\phi$, so we could also write this as
\mbox{}\\ \mbox{\hspace{2em}}$f_n = \frac{\phi^n - (1-\phi)^n}{\sqrt 5}$.


\subsection*{Homogeneous Linear Recurrence Relations with Constant Coefficients}

A \underline{homogeneous linear recurrence relation with constant coefficients} is an
equation
\mbox{}\\ \mbox{\hspace{2em}}$h_{n+k} = a_0 h_n + a_1 h_{n+1} + ... + a_{k-1} h_{n+k-1}$,
\\with $a_i$ complex numbers.

$k$ is the \underline{order} of the recurrence relation.
\\A number sequence $h_n$ satisfying the recurrence relation is called a
\underline{solution} to the recurrence relation.

If we add \underline{initial conditions}
\mbox{}\\ \mbox{\hspace{2em}}$h_0= c_0, ..., h_{k-1} = c_{k-1}$,
\\this clearly uniquely determines a solution.

\paragraph{Examples:}
\begin{enumerate}[(i)]\item The Fibonacci sequence.
\item Geometric sequences, $h_{n+1} = a h_n$.
\item The life-cycle of {\em inventioni exemplicus} is as follows:
\\	a new hatchling remains in the larval stage until its first summer,
\\	then spends a year maturing, and in the subsequent summer lays
	a clutch of 7 eggs (which quickly hatch into larvae),
\\	then in the summer after lays a second clutch of 6 eggs, then dies.

	All {\em excemplicus} are female (they reproduce parthenogenetically).

	Suppose no {\em exemplicus} die except at the end of their life cycle.

	If 100 exemplicus hatchlings are introduced one summer, how many
	exemplicus larvae will there be at the end of the $n$th summer
	thereafter?

	Solution:
\\	    At the end of the $n$th summer, there are 7 larvae born from each
	    2-year-old {\em exemplicus}, and 6 from each 3-year-old.

	    So $h_n = 6h_{n-3} + 7h_{n-2}$ for $n\geq 3$,
\\	    i.e. $h_{n+3} = 6h_n + 7h_{n+1}$ for $n\geq 0$.

	    We also have the initial conditions
\mbox{}\\ \mbox{\hspace{2em}}$h_0 = 100, h_1=0, h_2=700$.

	    So we get
\mbox{}\\ \mbox{\hspace{2em}}$h_3=600, h_4=4900, h_5=8400, h_6=37900,...$
\end{enumerate}

We proceed to generalise the solution to the Fibonacci recurrence relation to
solve general homogeneous linear recurrence relation with constant
coefficients.

Given a recurrence relation
\mbox{}\\ \mbox{\hspace{2em}}$h_{n+k} = a_0 h_n + a_1 h_{n+1} + ... + a_{k-1} h_{n+k-1}$,
\mbox{}\\ \mbox{\hspace{2em}}i.e. $h_{n+k} = \sum_{j=0}^{k-1}a_jh_{n+j}$,
\\we again look for solutions $h_n = q^n$.

Clearly $h_n = q^n$ is a solution iff
\mbox{}\\ \mbox{\hspace{2em}}$q^k = a_0 + a_1 q + ... + a_{k-1} q^{k-1}$,
\mbox{}\\ \mbox{\hspace{2em}}i.e. $q^k - a_{k-1}q^{k-1} - ... - a_1 q - a_0 = 0$.

The polynomial $x^k - a_{k-1}x^{k-1} - ... - a_1 x - a_0$ is called the
\underline{characteristic polynomial} of the recurrence relation.
\\It is a degree $k$ polynomial, so has $k$ roots in the complex numbers
(counting multiplicities).

Suppose that it has $k$ {\bf distinct} roots, $q_1,...,q_k$.
\\(See the "Bonus" section for what happens when we have repeated roots.)

\paragraph{Claim:}
\mbox{}\\    the $k$ vectors $((q_1^0,...,q_1^{k-1}),...,(q_k^0,...,q_k^{k-1}))$ are
    linearly independent in $\mathbb{C}^k$,
\paragraph{Proof:}
\mbox{}\\    Otherwise, considering the columns of the $k$-by-$k$ matrix whose rows are
    these vectors,
\\    the $k$ vectors $((q_1^0,...,q_k^0),...,(q_1^{k-1},...,q_k^{k-1}))$ are
    linearly dependent.
\\    i.e. there are $b_0,...,b_{k-1} \in \mathbb{C}$ not all 0, such that the
    polynomial
\mbox{}\\ \mbox{\hspace{2em}}$b_0 + b_1 x + ... + b_{k-1}x^{k-1}$
\\    has roots $q_1,...,q_k$.

    But a degree $k-1$ polynomial can't have $k$ distinct roots;
\\    contradiction.
\\    \qed

So any given initial conditions $h_0=c_0,...,h_{k-1}=c_{k-1}$ can be written
as a linear combination
\mbox{}\\ \mbox{\hspace{2em}}$h_n = b_1q_1^n + ... + b_kq_k^n = \sum_{i=1}^kb_iq_i^n$.

Taking this as a definition of $h_n$ for all $n$,
\\we see that not only does it satisfy the initial conditions by choice of
$b_i$,
\\but it satisfies the recurrence relation; indeed
\mbox{}\\ \mbox{\hspace{2em}}$h_{n+k} = \sum_{i=1}^k b_iq_i^{n+k}$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{i=1}^k b_iq_i^nq_i^k$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{i=1}^k b_iq_i^n(\sum_{j=0}^{k-1}a_jq_i^j)$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{j=0}^{k-1}\sum_{i=1}^k b_iq_i^na_jq_i^j$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{j=0}^{k-1}a_j\sum_{i=1}^k b_iq_i^{n+j}$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{j=0}^{k-1}a_jh_{n+j}$


\paragraph{Example:}
\mbox{}\\    Let's solve the {\em exemplicus} example.

\mbox{}\\ \mbox{\hspace{2em}}$h_{n+3} = 6h_n + 7h_{n+1}$,
\mbox{}\\ \mbox{\hspace{2em}}$h_0 = 100, h_1=0, h_2=700$.

    The characteristic polynomial is
\mbox{}\\ \mbox{\hspace{2em}}$x^3 - 7x - 6 = (x-3)(x+2)(x+1)$
\\    so the solutions are of the form
\mbox{}\\ \mbox{\hspace{2em}}$h_n = b_1 3^n + b_2 (-2)^n + b_3 (-1)^n$.

    Solving
\mbox{}\\ \mbox{\hspace{2em}}$100 = h_0 = b_1 + b_2 + b_3$
\mbox{}\\ \mbox{\hspace{2em}}$0 = h_1 = 3b_1 - 2b_2 - b_3$
\mbox{}\\ \mbox{\hspace{2em}}$700 = h_2 = 9b_1 + 4b_2 + b_3$

    gives
\mbox{}\\ \mbox{\hspace{2em}}$b_1=45, b_2=80, b_3=25$.

    So the solution is
\mbox{}\\ \mbox{\hspace{2em}}$h_n = 45*3^n + 80*(-2)^n - 25*(-1)^n$.
\\    \qed

\subsection*{Bonus: Solving recurrence relations with generating functions}

Generating functions provide a convenient device for solving recurrence
relations (although in theoretical terms, they only provide a different
way to package the same linear algebra).

If $g(x)$ is the generating function for the sequence $h_n$,
\\i.e. the coefficient of $x^n$ in $g(x)$ is $h_n$,
\\then the coefficient of $x^{n+1}$ in $xg(x)$ is $h_n$.

So if $h_n$ satisfy a recurrence relation 
\mbox{}\\ \mbox{\hspace{2em}}$h_{n+k} = a_0 h_n + a_1 h_{n+1} + ... + a_{k-1} h_{n+k-1}$
\\then in
\mbox{}\\ \mbox{\hspace{2em}}$g(x) - a_0 x^kg(x) - a_1x^{k-1}g(x) - ... - a_{k-1}xg(x)$,
\\$x^{n+k}$ has coefficient 0 for $n\geq 0$,
\\i.e. this is a polynomial of order $k-1$.

Using initial conditions, we can find this polynomial, and so express $g(x)$
as a rational function.

For example, consider the Fibonacci relations
$f_{n+2}=f_n+f_{n+1}, f_0=0,f_1=1$.

If $g(x)$ is the generating function, then
\mbox{}\\ \mbox{\hspace{2em}}$g(x) - xg(x) - x^2g(x) = f_0 + f_1x + f_2x^2 + f_3x^3 + ... - f_0x - f_1x^2 - f_2x^3 - ... -
\\f_0 x^2 - f_1 x^3 - ...$
\mbox{}\\ \mbox{\hspace{4em}}$= f_0 + (f_1-f_0)x + (f_2-f_1-f_0)x^2 + (f_3-f_2-f_1)x^3 + ...$
\mbox{}\\ \mbox{\hspace{4em}}$= f_0 + (f_1-f_0)x$
\mbox{}\\ \mbox{\hspace{4em}}$= 0 + (1-0)x$
\mbox{}\\ \mbox{\hspace{4em}}$= x$,
\\so
\mbox{}\\ \mbox{\hspace{2em}}$(1-x-x^2)g(x) = x$
\\so
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = \frac{x}{1-x-x^2}$.

Furthermore, by factoring the denominator and finding partial fractions, we
can expand this as a power series and so solve the recurrence equations.
\\In this case, the solutions to $1-x-x^2=0$ are the reciprocals of the
solutions $\phi,\phi'$ to $x^2-x-1$, so
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = \frac{x}{1-x-x^2}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{x}{(x-\phi^{-1})(x-\phi'^{-1})}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{\phi\phi'x}{(1-\phi x)(1-\phi' x)}$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{a}{1-\phi x} + \frac{b}{1-\phi' x}$
\\where
\mbox{}\\ \mbox{\hspace{2em}}$0 = a+b$
\mbox{}\\ \mbox{\hspace{2em}}$\phi\phi' = -\phi' a - \phi b$
\mbox{}\\ \mbox{\hspace{2em}}$=> b=-a$
\mbox{}\\ \mbox{\hspace{2em}}$=> \phi\phi' = a(\phi-\phi')$
\mbox{}\\ \mbox{\hspace{4em}}$=> a=\frac{1}{\sqrt 5}$ (using the definitions of $\phi,\phi'$)
\mbox{}\\ \mbox{\hspace{2em}}$=> b=\frac{-1}{\sqrt 5}$
\\so
\mbox{}\\ \mbox{\hspace{2em}}$g(x) = \frac{1}{\sqrt 5}((1-\phi x)^{-1} - (1-\phi' x)^{-1})$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{1}{\sqrt 5}((\sum_{n=0}^\infty \phi^n) - (\sum_{n=0}^\infty \phi'^n))$
\mbox{}\\ \mbox{\hspace{4em}}$= \frac{1}{\sqrt 5}\sum_{n=0}^\infty (\phi^n-\phi'^n)$
\\so we reclaim the formula we found before,
\mbox{}\\ \mbox{\hspace{2em}}$f_n = \frac{\phi^n - \phi'^n}{\sqrt 5}$,
\\and we had to do the same algebra to get there.


\subsection*{Bonus: abstract reformulation, and handling repeated roots}

Consider the (infinite dimensional) complex vector space of complex number
sequences $h = h_0,h_1,...$. Let $\sigma$ be the downshift operator, which
from a sequence $h$ obtains the new sequence $(\sigma h)_n = h_{n+1}$. Note
that this is a linear map.

Then a linear homogeneous constant coefficient recurrence relation, which we
can write as $\sum_{i=0}^k a_i h_{n+i}=0$, with $a_k\neq 0$, can be rewritten as
\mbox{}\\ \mbox{\hspace{2em}}$(\sum_{i=0}^k a_i\sigma^i) h = 0$.

The subspace of solutions to this is then the kernel of the linear operator
$\sum_{i=0}^k a_i\sigma^i$. This is a finite dimensional vector space. The
solution method described above is a matter of finding a basis of eigenvectors
of $\sigma$ on this space. Note that eigenvectors are precisely geometric
series $cq^n$.

In general of course, the eigenvectors of $\sigma$ won't span the space. But
its generalised eigenvectors will. One can check inductively that the $k$th
generalised $q$-eigenspace of $\sigma$, i.e. the kernel of $(\sigma-q)^k$,\
\\is the space of sequences $f(n)q^n$ where $f$ is a polynomial of degree at
most $k-1$. So if the characteristic polynomial factors as
\mbox{}\\ \mbox{\hspace{2em}}$\sum_{i=0}^k a_i\sigma^i = \Pi (\sigma-q_i)^{k_i}$,
\\the space of solutions has a basis of eigensequences
\mbox{}\\ \mbox{\hspace{2em}}$n^jq_i^n$ where $j < k_i$,
\\so any solution can be expressed as a linear combination of these.

We might as well note that this abstract formulation also applies to
homogeneous linear differential equations over the constants: replace the
space of sequences with, say, the space of complex analytic functions in one
variable, and replace $\sigma$ with the differentiation operator. The $k$th
generalised $\lambda$-eigenspace consists of $f(x)e^{\lambda x}$ for $f(x)$ a
polynomial of degree at most $k-1$.
\section{Difference sequences, sums of powers, and Stirling numbers}

\subsection*{Difference sequences}
\newcommand{\D}{\Delta}

\paragraph{Notation:}
\mbox{}\\    If $h_0,h_1,...$ is a number sequence, we will sometimes refer to the
    sequence just as $h$.

\paragraph{Definition:}
\mbox{}\\    $\D$ is the operator on number sequences of taking successive differences;
\\    for a number sequence $h$, the number sequence $\D h$ is defined by
\mbox{}\\ \mbox{\hspace{2em}}$\D h_n = h_{n+1}-h_n$.

    $\D^2 h_n = \D\D h_n$, etc.

    We write out a sequence and its iterated differences as an infinite triangle;
\\    e.g. if $h_n = n^2$, the iterated differences are as follows:
\\    \begin{verbatim}
 1   4   9   16  25 ...
   3   5   7   9 ...
     2   2   2 ...
       0   0 ...
         0 ...
    \end{verbatim}

\paragraph{Remark:}
\mbox{}\\    $\D$ is a linear operator, i.e. for sequences $h$ and $h'$ and numbers $c$
    and $c'$,
\mbox{}\\ \mbox{\hspace{2em}}$\D(ch + c'h')_n = c\D h_n + c'\D h'_n$.

    Hence the powers $\D^k$ are also linear.

\paragraph{Lemma:}
\mbox{}\\    Let $f$ be a polynomial of degree at most $d$, and let $h_n = f(n)$ be the
    sequence of its values on natural numbers.

    Then $\D^{d+1}h_n=0$ for all $n$.
\paragraph{Proof:}
\mbox{}\\    By linearity, it suffices to show this for monomials $f(x)=x^d$.

    So let $h_n = n^d$, and suppose inductively that the lemma holds for
    polynomials of degree less than $d$.

    Then
\mbox{}\\ \mbox{\hspace{2em}}$\D h_n = h_{n+1} - h_n = (n+1)^d - n^d$
\mbox{}\\ \mbox{\hspace{4em}}$= n^d + dn^{d-1} + {d\choose 2}n^{d-2} + ... + 1 - n^d$
\mbox{}\\ \mbox{\hspace{4em}}$= {d\choose 1}n^{d-1} + {d\choose 2}n^{d-2} + ... + 1$
\\    which has degree $d-1$.

    So by the inductive hypothesis,
\\    $0 = \D^d \D h_n = \D^{d+1} h_n$. \qed

Now suppose $h_n = f(n)$ with $f$ a polynomial of degree $d$.
\\By the above lemma, the numbers $h_0,\D h_0,...,\D^d h_0$ determine the
whole sequence $h$,
\\since we can generate the whole triangle from the initial diagonal
$h_0,\D h_0,...,\D^d h_0,0,0,...$.

Let's find a formula for $h_n$ in terms of $h_0,\D h_0,...,\D^d h_0$.

Generating the triangle is a linear process,
\\so if we can find a formula for $h_n$ generated from an initial diagonal
$0,0,...,0,1,0,0,...$, with $\D^k h_0 = 1$ and all other $\D^i h_0 = 0$,
\\we can then take a linear combination.

We get a "twisted Pascal's triangle", e.g.:\

    \begin{verbatim}
 0   0   0   0   1   5   15  35  70 ...
   0   0   0   1   4   10  20  35 ...
     0   0   1   3   6   10  15 ...
       0   1   2   3   4   5 ...
         1   1   1   1   1 ...
           0   0   0   0 ...
    \end{verbatim}

and so we see that $h_n = {n \choose k}$.

To prove this: let $f(x) := \frac{x(x-1)(x-2)...(x-(k-1))}{k!}$;
\\then $f(0)=f(1)=...=f(k-1)=0$ and $f(k)=1$,
\\so the difference triangle of $f(n)$ also starts with
    \begin{verbatim}
 0   0   0   0   1 
   0   0   0   1
     0   0   1
       0   1
         1
    \end{verbatim}
, and since by the lemma it also has 0s thereafter,
\\we must have $h_n = f(n)$.

Then we directly calculate that $f(n) = {n \choose k}$.

So, taking linear combinations, we conclude :\

\paragraph{Theorem:}
\mbox{}\\    If the initial diagonal of the difference triangle of $h_n$ is
    $c_0,c_1,...,c_d,0,0,...$
    (i.e. if $\D^k h_0 = c_i$ for $k \leq  d$, and $\D^k h_0 = 0$ for $k>d$),
\\    then
\mbox{}\\ \mbox{\hspace{2em}}$h_n = \sum_{k=0}^d c_k{n \choose k}$.


\subsection*{Sums of powers}

We can use this theorem to give neat formulae for sums of powers
$\sum_{n=0}^k n^d$, generalising the formulae you know and love for $d=1$ and
$d=2$ (and maybe even $d=3$, if you're that generous with your affections),
\\and more generally to give formulae for $\sum_{n=0}^k f(n)$ where $f$ is any polynomial.

First recall the formula (from the section on Binomial Coefficients)
\mbox{}\\ \mbox{\hspace{2em}}${k+1 \choose r+1} = \sum_{n=0}^k {n \choose r}$.

So to find $\sum_{n=0}^k f(n)$, we can first use the above theorem to find an
expression for $f(n)$ in terms of binomial coefficients, then use this formula
to sum them.

\paragraph{Example:}
\mbox{}\\    Let's find a formula for $\sum_{n=0}^k n^4$.

    Drawing the start of the difference triangle,
\\    \begin{verbatim}
 0   1   16  81  256 
   1   15  65  175
     14  50  110
       36  60
         24
    \end{verbatim}
    , and recalling that all further rows are 0 since $n^4$ has degree 4,
\\    we see that the initial diagonal is $0,1,14,36,24,0,0,...$.

    So by the above theorem,
\mbox{}\\ \mbox{\hspace{2em}}$n^4 = {n \choose 1} + 14 {n \choose 2} + 36 {n \choose 3} + 24 {n \choose 4}$.

    So using the formula
\mbox{}\\ \mbox{\hspace{2em}}${k+1 \choose r+1} = \sum_{n=0}^k {n \choose r}$,
\\    we find
\mbox{}\\ \mbox{\hspace{2em}}$\sum_{n=0}^k n^4$
\mbox{}\\ \mbox{\hspace{4em}}$=\sum_{n=0}^k \left({n \choose 1} + 14 {n \choose 2} + 36 {n \choose 3} + 24 {n \choose 4}\right)$
\mbox{}\\ \mbox{\hspace{4em}}$={k+1 \choose 2} + 14 {k+1 \choose 3} + 36 {k+1 \choose 4} + 24 {k+1 \choose 5}$


\paragraph{Exercise:}
\mbox{}\\    Repeat this procedure for $n^1$, $n^2$ and $n^3$, and check that the
    answers you get agree with the standard formulae.

\subsection*{Stirling numbers}

We would like to understand the mysterious numbers which appear in the formula
for $\sum_{n=0}^k n^p$,
\\i.e the numbers $c(p,k)$ defined by
\mbox{}\\ \mbox{\hspace{2em}}$c(p,k) := \D^k h_0$ where $h_n = n^p$.

So as we saw, these are the numbers $c(p,k)$ such that
\mbox{}\\ \mbox{\hspace{2em}}$n^p = \sum_{k=0}^p c(p,k) {n \choose k}$.

We observe (and will eventually prove) that $c(p,k)$ seems to be divisible by
$k!$, so set
\mbox{}\\ \mbox{\hspace{2em}}$S(p,k) := c(p,k)/k!$.

So, introducing the notation $[n]_k := P(n,k) = k!{n \choose k}$,
\mbox{}\\ \mbox{\hspace{2em}}$n^p = \sum_{k=0}^p S(p,k) [n]_k$.

These numbers $S(p,k)$ are the \underline{Stirling numbers of the second kind}.

Here's a table, written in Pascal triangle format with k going across and p
going down, and starting with $S(1,1)=1$:\
\\\begin{verbatim}
           1
         1   1
       1   3   1
     1   7   6   1
   1   15  25  10  1
 1   31  90  65  15  1
\end{verbatim}

This corresponds to the formulae
\mbox{}\\ \mbox{\hspace{2em}}$n^1 = [n]_1$
\mbox{}\\ \mbox{\hspace{2em}}$n^2 = [n]_1 + [n]_2$
\mbox{}\\ \mbox{\hspace{2em}}$n^3 = [n]_1 + 3[n]_2 + [n]_1$
\mbox{}\\ \mbox{\hspace{2em}}...

All values of $S(p,k)$ not shown in the triangle are 0, except $S(0,0)=1$.

\paragraph{Lemma:}
\mbox{}\\    For all $p>0$, and all $k$,
\mbox{}\\ \mbox{\hspace{2em}}$S(p,k) = S(p-1,k-1) + kS(p-1,k)$.
\paragraph{Proof:}
\mbox{}\\    First, note that $S(p,k)=0$ when $k>p$, by considering degrees of polynomials.
\\    Also $S(p,k)=0$ when $k<0$, by definition.

    Now
\mbox{}\\ \mbox{\hspace{2em}}$n^p = nn^{p-1} = n \sum_{k=0}^{p-1} S(p-1,k) [n]_k$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{k=0}^{p-1} S(p-1,k) ((n-k)+k) [n]_k$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{k=0}^{p-1} S(p-1,k) [n]_{k+1} + \sum_{k=0}^{p-1} k S(p-1,k) [n]_k$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{k=1}^p S(p-1,k-1) [n]_k + \sum_{k=0}^{p-1} k S(p-1,k) [n]_k$
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{k=0}^p S(p-1,k-1) [n]_k + \sum_{k=0}^p k S(p-1,k) [n]_k$
\mbox{}\\ \mbox{\hspace{4em}}\hspace{4em} {\it (using $S(p-1,-1)=0=S(p-1,p)$)}
\mbox{}\\ \mbox{\hspace{4em}}$= \sum_{k=0}^p (S(p-1,k-1) + kS(p-1,k)) [n]_k$,

    so we conclude by comparing coefficients with
\mbox{}\\ \mbox{\hspace{2em}}$n^p = \sum_{k=0}^p S(p,k) [n]_k$. \qed

\paragraph{Theorem:}
\mbox{}\\    $S(p,k)$ is the number of partitions of a set of $p$ objects into $k$
    indistinguishable boxes in which no box is empty,
\\    i.e. the number of partitions of a set of size $p$ into a set of
    $k$ non-empty subsets,
\\    i.e. the number of sets of non-empty subsets of $\{1,...,p\}$ which are
    disjoint and have union $\{1,...,p\}$.
\paragraph{Proof:}
\mbox{}\\    Write $S'(p,k)$ for this number.

    Suppose $p \geq  1$ and $1 \leq  k \leq  p$.
\\    Consider a partition of $\{1,...,p\}$ into a set of $k$ non-empty subsets,
\\    and consider removing $p$.
\\    First, suppose the set in the partition which contains $p$ is just $\{p\}$.
\\    Then on removing $p$, we obtain a partition of $\{1,...,p-1\}$ into $k-1$
    subsets.
\\    Otherwise, on removing $p$ we obtain a partition of $\{1,...,p-1\}$ into
    $k$ subsets.
\\    In the first case, the map is bijective, but in the second case there are
    $k$ ways of obtaining the same partition of $\{1,...,p-1\}$, since $p$
    could have been removed from any of the $k$ sets in that partition.

    So
\mbox{}\\ \mbox{\hspace{2em}}$S'(p,k) = S'(p-1,k-1) + kS'(p-1,k)$.
\\    Clearly $S'(p,k)=0$ for $k<0$ or $k>p$ or $p<0$, and $S(0,0)=1$.
\\    So by induction on $p$, 
    $S(p,k)=S'(p,k)$ for all $p$ and $k$.
\\    \qed

So now we know that $S(p,k)$ is an integer.

Moreover, we can now reason combinatorially to find a formula for $S(p,k)$:\

\paragraph{Theorem:}
\mbox{}\\    For $p\geq 0$ and $0\leq k\leq p$,
\\    \[ S(p,k) = \sum_{i=0}^k (-1)^i \frac{(k-i)^p}{i!(k-i)!} \]
\paragraph{Proof:}
\mbox{}\\    Fix $p$ and $k$.

    Let $P$ be the number of partitions of $\{1,...,p\}$ into an
    {\bf ordered sequence} of $k$ non-empty subsets.
\\    So $P = k! S(p,k)$.

    A partition of $\{1,...,p\}$ into an ordered sequence of $k$ subsets,
\\    with no restrictions on the subsets being non-empty,
\\    just corresponds to a $k$-colouring of $\{1,...,p\}$,
\\    i.e. a choice of which of the $k$ sets in the partition each element
    should go in,
\\    so there are $k^p$ such partitions.

    Let $A_i$ be the partitions of $\{1,...,p\}$ into an ordered sequence of
    $k$ subsets, where the $i$th is empty.

    Such a partition corresponds to a partition into $k-1$ possibly empty
    subsets, by ignoring the one which is required to be empty.
\\    So $|A_i| = (k-1)^p$.

    Similarly, $|A_i\cap A_j| = (k-2)^p$ for $i \neq  j$, and generally
    $|\bigcap_{i\in I} A_i| = (k - |I|)^p$.

    So by inclusion-exclusion,

\mbox{}\\ \mbox{\hspace{2em}}$S(p,k) = \frac 1{k!} P$
\mbox{}\\ \mbox{\hspace{4em}}$=\frac 1{k!} (k^p - |\bigcup_i A_i|)$
\mbox{}\\ \mbox{\hspace{4em}}$=\frac 1{k!} (k^p - \sum_{ \emptyset  \neq  I \subseteq  \{1,...,k\} } (-1)^{|I| - 1} |\bigcap_{i\in I} A_i|)$
\mbox{}\\ \mbox{\hspace{4em}}$=\frac 1{k!} (k^p - \sum_{i=1}^k (-1)^{i-1} {k \choose i} (k-i)^p)$
\mbox{}\\ \mbox{\hspace{4em}}$=\frac 1{k!} (\sum_{i=0}^k (-1)^i {k \choose i} (k-i)^p)$
\mbox{}\\ \mbox{\hspace{4em}}$=\sum_{i=0}^k (-1)^i \frac{(k-i)^p}{i!(k-i)!})$ \qed
\section{Heterosexuality, and Hall's Marriage Theorem}

\subsection*{Basic "marriage" problem}

A set $A$ of men;
\\a set $B$ of women;
\\certain pairings $(a,b)$ are "compatible".

A {\bf matching} is a choice of some compatible pairings ("marriages"),
\\such that no man is paired to multiple women and no woman to multiple men.

Can we find a matching in which every woman is paired with some man?


\subsection*{Graphical formulation}

(You can skip this if you don't know what a graph is.)

Bipartite graph: two collections of $A$ and $B$ of vertices; all edges have
one vertex in $A$ and the other in $B$.

A matching is a set of edges such that no vertex is incident to more than one
of the edges.

Can we find a matching such that every $b \in B$ is incident to one of the
edges?


\subsection*{SDR formulation}

Given a family of subsets $(A_i)_{i\in B}$ of a set $A$,
\\can we find elements $a_i \in A_i$
which are distinct, i.e. $a_i \neq  a_j$ when $i \neq  j$?

Such a selection of $a_i \in A_i$ for $i\in B$ is called a
\underline{system of distinct representatives} (SDR).


\subsection*{Other instances}

\paragraph{Example:}
\mbox{}\\    Given an $m\times n$ board with certain squares missing,
\\    can we place a rook on each row such that no row or column contains two
    rooks?

    Here we marry rows and columns, consecrating the marriage with the
    symbolic placing of a rook.

\paragraph{Example:}
\mbox{}\\    Certain jobs are to performed by certain people, one job by each;
\\    not all people are suitable for all jobs.
\\    Can we assign each job to some person?

\paragraph{Example:}
\mbox{}\\    Various classes are to be scheduled at various times in various rooms;
\\    some classes require certain rooms,
\\    and some times are unsuitable for some classes.

    Suppose all classes are to start on the hour.

    Can all classes be scheduled?

    Here, we marry classes and hour-room pairs.

\subsection*{Hall's Marriage Theorem}

\paragraph{Definition:}
\mbox{}\\    A family $(A_i)_{i\in B}$ of sets satisfies the \underline{marriage condition}
    if for any $k$, the union of any $k$ of the sets in the family has size at
    least $k$;
\\    i.e. for every $B' \subseteq  B$,
\\    \[ \left|{\bigcup_{i\in B'} A_i}\right| \geq  |B'| .\]

\paragraph{Theorem [Philip Hall, 1935]:}
\mbox{}\\    A family $(A_i)_{i\in B}$ of finite sets has a system of distinct representatives
    iff it satisfies the marriage condition.
\paragraph{Proof:}
\mbox{}\\    The marriage condition is necessary, since if $a_i \in A_i$ is an SDR and
    $B' \subseteq  B$
\mbox{}\\ \mbox{\hspace{2em}}$\bigcup_{j\in B'} A_j \supseteq  \{a_j \;|\; j \in B'\}$
\\    so, by distinctness,
\mbox{}\\ \mbox{\hspace{2em}}$\left|{\bigcup_{j\in B'} A_j}\right| \geq  |\{a_j \;|\; j \in B'\}| = |B'|$.

    Now suppose the marriage condition holds, and suppose inductively that the
    theorem holds when $B$ is smaller.

    Suppose first that for all $B' \subsetneq  B$,
\\    \[ \left|{\bigcup_{i\in B'} A_i}\right| \geq  |B'| + 1 .\]

    Let $i_0 \in B$,
\\    and let $a_0 \in A_{i_0}$. We proceed by deleting $i_0$ and $a$.
\\    Let $A_i' := A_i \setminus \{a_0\}$;
\\    then $(A_i')_{i\in B \setminus \{i_0\} }$ also satisfies the marriage condition.
\\    Indeed, for any $B' \subseteq  B \setminus \{i_0\}$,
\mbox{}\\ \mbox{\hspace{2em}}$|\bigcup_{j\in B'} A_i'| \geq  |\bigcup_{j\in B'} A_i| - 1$
\mbox{}\\ \mbox{\hspace{4em}}$\geq  (|B'|+1) - 1$
\mbox{}\\ \mbox{\hspace{4em}}$= |B'|$.
\\    So by the inductive hypothesis, this family has an SDR,
\\    adjoining $a_0$ to which yields an SDR for $(A_i)_{i\in B}$.

    Otherwise, say $B^0 \subsetneq  B$ with
    \[ \left|{\bigcup_{i\in B^0} A_i}\right| = |B^0| .\]

    Let $A^0 := \bigcup_{i\in B^0} A_i$.
\\    We proceed by deleting $B^0$ and $A^0$.
\\    Let $A_i' := A_i \setminus A^0$, and
    consider $(A_i')_{i \in (B \setminus B^0)}$.
\\    Then for $B' \subseteq  B \setminus B^0$,
\mbox{}\\ \mbox{\hspace{2em}}$|\bigcup_{j\in B'} A_i'| \geq  |\bigcup_{j\in B'\cup B^0} A_i| - |B^0|$
\mbox{}\\ \mbox{\hspace{4em}}$\geq  |B'\cup B^0| - |B^0|$
\mbox{}\\ \mbox{\hspace{4em}}$= |B'|$
\\    So by the inductive hypothesis, this family has an SDR.

    Now $(A_i)_{i\in B^0}$ also satisfies MC,
\\    and $B^0 \subsetneq  B$,
\\    so by the inductive hypothesis,
\\    this family also has an SDR.

    Since $A_i' \cap A_j = \emptyset $ for $j\in B^0$,
\\    the union of these two SDRs is an SDR for $(A_i)_{i\in B}$.
\\    \qed

\subsection*{Dominoes on a chessboard}

\providecommand{\by}{\times}

Consider an $m\by n$ chequered board,
\\meaning that each square is either white or black, and no two neighbouring
squares are of the same colour,
\\and suppose we delete some squares.

e.g.
\\\begin{verbatim}
      , . ,   ,
    , .   . , .
    .     , .  
    ,     . , .
\end{verbatim}
(here I use dots for white squares, commas for black squares, and spaces for
deleted squares.).

Can we put $2\by 1$ dominoes on the non-deleted squares of the board, such
that the dominoes don't overlap and every non-deleted square is covered?

e.g. here's a solution in the case of the above example, using 8 dominoes
denoted by the corresponding numbers:\
\\\begin{verbatim}
      3 3 4   5
    2 2   4 6 5
    1     7 6  
    1     7 8 8
\end{verbatim}.

We can view this as a marriage problem: we want to marry white squares to
black squares, and only adjacent squares are compatible.

So we can solve the problem iff there are as many white squares as black
squares and the family of sets of (say) black squares adjacent to the white
squares satisfies MC.

\paragraph{Exercise:}
\mbox{}\\    Draw the corresponding bipartite graph for the example board above.

    Find some boards which don't satisfy the marriage condition,
\\    then try (and fail) to cover them with dominoes.

\subsection*{Latin squares}

\paragraph{Definition:}
\mbox{}\\    A \underline{Latin rectangle} is an $m\by c$ array with each entry an element of
    $\{1,...,n\}$,
\\    such that no number appears twice in any row or column.

\paragraph{Theorem:}
\mbox{}\\    Any $m\by n$ Latin rectangle with $m<n$ can be completed by adding rows
    to form an $n\by n$ Latin square.
\paragraph{Proof:}
\mbox{}\\    By induction, it suffices to show that we can add a row to form an
    $m\by n+1$ Latin rectangle.

    Let $A_i$, for $i=1,...,n$, be the set of numbers in $\{1,...,n\}$ {\bf not}
    appearing in the $i$th column.

    Then $(A_i)_{i\in\{1,...,n\}}$ satisfies the marriage condition.
\\    Indeed suppose $I \subseteq  \{1,...,n\}$.
\\    Each number in $\{1,...,n\}$ occurs in each of the $m$ rows,
\\    and so occurs in at most $n-m$ of the $A_i$.
\\    Each $A_i$ has size $n-m$.
\\    So
\mbox{}\\ \mbox{\hspace{2em}}$|I|(n-m) = \sum_{i\in I} |A_i| \leq  (n-m) |\bigcup_{i\in I} A_i|$,
\\    so
\mbox{}\\ \mbox{\hspace{2em}}$|\bigcup_{i\in I} A_i| \geq  |I|$.

    So by Hall's Marriage Theorem, an SDR $(a_i\in A_i)_{i\in\{1,...,n\}}$
    exists;
\\    by distinctness and the definition of $A_i$, adding this as a row
    yields an $m\times n+1$ Latin rectangle. \qed
\\\providecommand{\N}{\mathbb{N}}
\providecommand{\Z}{\mathbb{Z}}
\section{Impartial Games}

An \underline{impartial game} is a two-player game in which players take turns to make
moves, and where the moves available from a given position don't depend on
whose turn it is.

A player \underline{loses} if they can't make a move on their turn
(i.e. a player wins if they move to a position from which no move is
possible).

In games which are not impartial, the two players take on different roles
(e.g. one controls white pieces and the other black), so the moves available
in a given position depend on whose turn it is. Before we treat these more
complicated games, we first consider the special case of impartial games.

Any nim position is an impartial game. Write $*n$ for the nim position with a
single heap of size $n$.

In particular, $*0$ is the "zero game", written 0: the player to move
immediately loses.

We consider a position in a game as a game in itself.
\\We can specify a game by giving the set of its \underline{options} - the games to which
the first player can move.
\\e.g. $0 = \emptyset $, $*1 = \{0\}$, $*n=\{*0,*1,*2,...,*(n-1)\}$.

For impartial games $G$ and $H$, $G+H$ is the game where the two games are
played side-by-side, with a player on their turn getting to decide which of
the two to move in.

So e.g. $*2 + *2 + *3$ is the nim position with two heaps of size 2 and one of
size 3.

If $G = \{G_1,...,G_n\}$ and $H=\{H_1,...,H_n\}$, then
\mbox{}\\ \mbox{\hspace{2em}}$G+H = \{G_1+H,...,G_n+H,G+H_1,...,G+H_n\}$.

\paragraph{Remark:}
\begin{itemize}\item $G+H = H+G$
\item $(G+H)+K = G+(H+K)$
\end{itemize}

We refer to the player who is to move first as "Player 1", and their opponent
as "Player 2". So after a move, Player 1 in the new game was Player 2 in the
original game, and vice versa.

The \underline{finite} (or \underline{short}) games are defined recursively by:\
\begin{itemize}\item 0 is finite
\item  if $G_1,...,G_n$ are finite, then $\{G_1,...,G_n\}$ is finite.
\end{itemize}

\paragraph{Lemma:}
\mbox{}\\    In any finite impartial game $G$, either Player 1 has a winning strategy,
\\    or Player 2 has.

    We say that $G$ is a "first/second-player win" accordingly,
\\    and that the \underline{outcome} of $G$ is a win for Player 1/2.

\paragraph{Proof:}
\mbox{}\\    Suppose inductively that the lemma holds for all options of $G$.

    If some option of $G$ is a win for Player 2, then Player 1 can win in $G$
    by making that move.

    Else, Player 2 can win in $G$, 
    since they have a winning strategy in whatever game Player 1 moves to.

\subsection*{Green Hackenbush}

A green hackenbush game consists of some dots joined by lines, with some dots
"on the ground"; a move comprises deleting a line, and then deleting all lines
which are no longer connected to the ground.

\paragraph{Example:}
\mbox{}\\    \begin{verbatim}
    *           *         *         *         *
    |           |         |         |         |
    |           |         |         |         |
    *           *         *         *         *         *    
   / \           \       /         / \       / \       / \   
  /   \           \     /         /   \     /   \     /   \  
 *     * = { *     * , *     * , *     * , *     * , *     * }
  \   /       \   /     \   /         /     \         \   /  
   \ /         \ /       \ /         /       \         \ /   
____*____   ____*____ ____*____ ____*____ ____*____ ____*____
    \end{verbatim}

    \begin{verbatim}
    *             *                                    
    |             |                                    
    |             |                                    
    *             *           *                             
   / \             \         / \                            
  /   \             \       /   \                           
 *     *  = {        *  ,  *     *  ,        *  ,           }
      /             /           /           /               
     /             /           /           /                
____*____     ____*____   ____*____   ____*____   ____*____ 
    \end{verbatim}

$P+Q =$ [picture where we draw $P$ and $Q$ side by side, with no connections
    between them]


\subsection*{Equivalence of impartial games}

\paragraph{Definition:}
\mbox{}\\    Two games $G$ and $H$ are \underline{equivalent}, $G \equiv  H$,
\\    if for any game $K$,
\\    $G+K$ has the same outcome as $H+K$.

\paragraph{Remark:}
\begin{itemize}\item If $G \equiv  H$ then $G$ has the same outcome as $H$;
\item   $0+G \equiv  G$;
\item   if $G \equiv  H$ then $G+K \equiv  H+K$ for any $K$.
\end{itemize}

\paragraph{Lemma:}
\mbox{}\\    $G \equiv  0$ iff $G$ is a second-player win.
\paragraph{Proof:}
\mbox{}\\    $\Rightarrow $:
\\	If $G \equiv  0$, then $G=G+0$ has the same outcome as $0+0=0$, which is a
	second-player win.

    $\Leftarrow $:
\\	Suppose $G$ is a second player win, and $K$ is any game.
\\	We want to show $G+K$ has the same outcome as $0+K=K$.
\\	The following is a winning strategy in $G+K$ for whichever player
	has the winning strategy in $K$:\

	On our turn, play the next move in the winning strategy for $K$,
\\	unless our opponent just played in $G$ - then play the next move
	in the winning strategy for $G$ as Player 2. \qed

\paragraph{Example:}
\mbox{}\\    $G+G \equiv  0$

    Indeed, the second player wins by mirroring any move made in one copy of $G$
    in the other copy of $G$.

\paragraph{Lemma:}
\mbox{}\\    $G+H \equiv  0$ iff $G \equiv  H$
\paragraph{Proof:}
\mbox{}\\    If $G\equiv H$, then $G+H \equiv  G+G \equiv  0$ by the above example.

    If $G+H \equiv  0$, then
\mbox{}\\ \mbox{\hspace{2em}}$G = G + 0 \equiv  G+H+H \equiv  0 + H = H$. \qed

\paragraph{Exercise:}
\mbox{}\\    Confirm, by finding a winning strategy for Player 2 in the sum, that
    \begin{verbatim}
        *
        |
        |
        *
       / \      ==  *1
      /   \
     *     *
      \   /
       \ /
    ____*____
    \end{verbatim}

\paragraph{Remark:}
\mbox{}\\    One easily checks that games with equivalent options are equivalent,
\\    i.e. if $G_i \equiv  G'_i$, then $\{G_1,...,G_n\} \equiv  \{G'_1,...,G'_n\}$.

From now on, we will just write $=$ in place of $\equiv $ - we are only interested
in games up to equivalence.

Our winning strategy for nim tells us how to add nim heaps:\

\paragraph{Proposition:}
\mbox{}\\    $*n + *m = *( n \oplus m )$
\paragraph{Proof:}
\mbox{}\\    $n \oplus m \oplus ( n \oplus m ) = 0$,
\\    so as we proved when discussing Nim,
\\    $*n + *m + *( n \oplus m )$ is a second-player win. \qed

It follows that every nim game is equivalent to a single heap, with size the
nim sum of the sizes of the heaps in the original game.

\paragraph{Example:}
\mbox{}\\    \begin{verbatim}
        *      *
        |       \
        |        \
        *         *
       / \       /    =  *1 + *3  =  *(1 (+) 3)  =  *2
      /   \     /
     *     *   *
      \   /    |
       \ /     |
    ____*______*_
    \end{verbatim}

\subsection*{Impartial games are secret heaps}

Now we show that not only every Nim game, but every impartial game is
equivalent to a Nim heap.

\paragraph{Theorem [Sprague-Grundy]:}
\mbox{}\\    Any finite impartial game $G$ is equivalent to a nim heap;
\\    $G = *n$ where $n$ is the smallest non-negative integer such that
    $G$ has no option equivalent to $*n$.
\paragraph{Proof:}
\mbox{}\\    Suppose inductively that the theorem holds for all options of $G$.

    We show that we can win in $G + *n$ as the second player.

    If Player 1 moves in $*n$, say to $*m$ with $m<n$,
\\    then by definition of $n$ we can move in $G$ to a game equivalent to $*m$,
\\    yielding a game equivalent to $*m + *m = 0$.
\\    So we win.

    If Player 1 moves in $G$, picking an option $G_i$ of $G$,
\\    then by the inductive hypothesis, $G_i$ is equivalent to some $*m$.

    So we have to win as first player in $*m + *n$,
\\    but by definition of $n$, $m \neq  n$, so $*m + *n = *(m \oplus n) \neq  *0$.
\\    Again, we win. \qed

\newpage
\paragraph{Exercise:}
\mbox{}\\    Confirm by using the Sprague-Grundy theorem that
    \begin{verbatim}
        *
        |
        |
        *
       / \      =  *1
      /   \
     *     *
      \   /
       \ /
    ____*____
    \end{verbatim}

\paragraph{Exercise:}
\mbox{}\\    By using the Sprague-Grundy theorem to find the nim heaps equivalent to
    each component, and then applying the winning strategy from nim, find a
    winning move for the first player in the following Green Hackenbush game.

    \begin{verbatim}
              *
              |
              |
        *     *     *    *
       / \     \   /    / \
      /   \     \ /    /   \
     *     *     *    *-----*
      \   /      |     \   /
       \ /       |      \ /
    ____*________*_______*____
    \end{verbatim}

\newpage
\section{Bonus: Games}

Now, we consider dropping the impartiality condition.

A (combinatorial) game is played by two players, Left and Right, taking turns.
\\A player who can't move loses.

A game is given by the moves available to Left (\underline{Left-options}) and
to Right (\underline{Right-options}).

We write $G=\{L_1,...,L_n \;|\; R_1,...,R_m \}$ for the game with Left-options
$L_i$ and Right-options $R_i$, which are themselves games.

Finiteness is defined as in the impartial case.

A game is impartial iff the set of Right-options is equal to the set of
Left-options, and all options are impartial.


\subsection*{Real-life games}

Chess nearly fits into this framework - but it's possible for the game to end
in a draw, which we haven't allowed in our formalism, and it isn't finite.

Go fits even better, though rare loopy situations make it technically
infinite.

Some other games of variable notoriety which fit, at least roughly, our
definition of a game: draughts/chequers, pente, gess, khet, dots-and-boxes,
\\sprouts, connect 4, gomoku, tic-tac-toe, hex, Y, shogi, xi\`{a}ngq\'{i}, hnefatafl.
\\See 
 {\tt https://en.wikipedia.org/wiki/List\_of\_abstract\_strategy\_games}
for many more.

\subsection*{Red-Blue Hackenbush}

Like Green Hackenbush, but each line is either red or blue. Only Right can cut
Red lines, only Left can cut bLue lines.

\subsection*{Domineering}

Played on an 8x8 (say) chess board; players take turns to place 2x1 dominoes
on free squares. Left places her dominoes vertically, Right places his
horizontally.


\subsection*{Basic theory}

We still have the impartial games $*n$,
\mbox{}\\ \mbox{\hspace{2em}}$*n = \{ *0,*1,...,*(n-1) \;|\; *0,*1,...,*(n-1) \}$.
\\We call these \underline{nimbers}.
\\We write $*$ for $*1 = \{ 0 \;|\; 0 \}$.

$G+H$ is the game where players choose which of $G$ and $H$ to move in;
\\Left plays as Left in both, Right as Right in both.

\paragraph{Outcomes:}
\mbox{}\\    Now there are four possibilities:\
\begin{itemize}\item Right wins (whoever moves first);
\item   Left wins (whoever moves first);
\item   Player 1 (whether that's Left or Right) wins;
\item	 Player 2 (whether that's Left or Right) wins.
\end{itemize}

\paragraph{Exercise:}
\mbox{}\\    Determine the outcomes of these four basic domineering games:
\\    \begin{verbatim}
                                  ***
    *        **        **         *
    *                  *          *
    \end{verbatim}

We define equivalence ("$G=H$") as in the impartial case, but with this
notion of outcome.

By the same arguments as in the impartial case, we have:\

\paragraph{Lemma:}
\mbox{}\\    $G = 0$ iff $G$ is a second-player win.

\paragraph{Definition:}
\mbox{}\\    The \underline{negative} of a game $G=\{L_1,...,L_n \;|\; R_1,...,R_m\}$ is
    the game $-G$ in which Left and Right switch roles, i.e.
\mbox{}\\ \mbox{\hspace{2em}}$-G := \{ -R_1,...,-R_m \;|\; -L_1,...,-L_n \}$.

Note that $G$ is impartial iff $-G=G$.

Write $G-H$ for $G+(-H)$.

$G-G = G+(-G)$ is a second-player win, by mirroring moves made in one component
in the other.

Just as in the impartial case, we have

\paragraph{Lemma:}
\mbox{}\\    $G-H=0$ iff $G=H$
\paragraph{Proof:}
\mbox{}\\    If $G=H$, then $G-H = H-H = 0$.

    If $G-H = 0$, then
\mbox{}\\ \mbox{\hspace{2em}}$G = G + 0 = G+H-H = G-H + H = 0+H = H$. \qed

\paragraph{Definition:}
\mbox{}\\    For games $G$ and $H$, we say
\begin{itemize}\item $G>H$ if $G-H$ is a win for Left;
\item   $G<H$ if $G-H$ is a win for Right;
\item   $G=H$ if $G-H$ is a win for Player 2;
\item   $G\;\|\;H$ ("$G$ is confused with $H$") if $G-H$ is a win for Player 1.
\end{itemize}

\paragraph{Lemma:}
\mbox{}\\    $\leq $ is a partial order, and addition respects it,
\\    i.e. $G \leq  H \Rightarrow  G+K \leq  H+K$.

\subsection*{Numbers}

$1 = \{0 \;|\; \}$,
\\$2 = \{1 \;|\; \}$,
\\$3 = \{2 \;|\; \}$,
\\etc.

So
$-1 = \{ \;|\; 0 \}$,
\\$-2 = \{ \;|\; -1 \}$,
\\$-3 = \{ \;|\; -2 \}$,
\\etc.

$\frac 12 = \{ 0 \;|\; 1 \}$.

\paragraph{Exercise:}
\mbox{}\\    By e.g. considering the following red-blue hackenbush position (L means
    bLue, R means Red), prove
\mbox{}\\ \mbox{\hspace{2em}}$\frac 12 + \frac 12 - 1 = 0$

    \begin{verbatim}
        *       *       
       R|      R|       
        |       |       
        *       *      *
       L|      L|     R|
        |       |      |
    ____*_______*______*____
    \end{verbatim}

$\frac 14 = \{ 0 \;|\; \frac 12 \}$.

\paragraph{Exercise:}
\mbox{}\\    Confirm that
    \begin{verbatim}
        *
       R|
        |
        *       1
       R|   =   -
        |       4
        *
       L|
        |
    ____*___
    \end{verbatim}

Generally, we can define
\mbox{}\\ \mbox{\hspace{2em}}$\frac 1{2^{n+1}} = \{ 0 \;|\; \frac 1{2^n} \}$.
\\and then for $m\in\N$,
\mbox{}\\ \mbox{\hspace{2em}}$\frac m{2^n} = m \frac 1{2^n}$ = [the sum of $m$ copies of $\frac 1{2^n}$]
\mbox{}\\ \mbox{\hspace{2em}}$\frac {-m}{2^n} = -(m \frac 1{2^n})$

\paragraph{Definition:}
\mbox{}\\    A finite game is a \underline{number} if it is equivalent to one of these games
    $\frac m{2^n}$ (with $m\in\Z$, $n\in\N$).

\paragraph{Lemma:}
\mbox{}\\    The usual ordering on numbers agrees with the definition of $<$ for games.

\paragraph{Lemma:}
\mbox{}\\    If $G$ is a finite game,
\\    and every Left-option is less than every Right-option,
\\    then $G$ is the simplest number $\frac m{2^n}$ which is greater than every
    Left-option and less than every Right-option,
\\    where $\frac m{2^n}$ is \underline{simpler} than $\frac {m'}{2^{n'}}$ if $n<n'$, or
    if $n=n'$ and $|m|<|m'|$.
\paragraph{Proof:}
\mbox{}\\    We show $\frac m{2^n}-G=0$.

    Say Left plays first.

    If she plays in $-G$, it is to a number $-k$ with $k>\frac m{2^n}$,
\\    so Right wins the resulting game $\frac m{2^n}-k$.

    If she plays in $\frac m{2^n}$, it follows from the definitions (exercise)
    that it must be to a simpler number $k < \frac m{2^n}$.

    So since $\frac m{2^n}$ is simplest,
\\    $k$ must be less than some Left-option $H$ of $G$.

    So Right can play the Right-option $-H$ of $-G$,
\\    and win the resulting game $k-H$.

\paragraph{Exercise:}
\mbox{}\\    Use this lemma to confirm the following values of domineering games:\
\\    \begin{verbatim}
  **   1         -1   ***   3
  *  = -   *** =  -   * * = -
  *    2     *    2   *     4
    \end{verbatim}

\paragraph{Lemma:}
\mbox{}\\    The usual addition of numbers agrees with addition of games.

\paragraph{Exercise:}
\mbox{}\\    Confirm, both by using addition and by thinking through strategies, that
    \begin{verbatim}
  *** ***            
  * * * * ** ***  = 0
  *   *        *     
    \end{verbatim}


\subsection*{More on Domineering}

\paragraph{Exercise:}
\mbox{}\\    Confirm
    \begin{verbatim}
               *  
  **          *** 
  ** = *      *   = { 1 | 1 } = 1 + *
              *   
    \end{verbatim}

There is however much more to games than numbers and nimbers.

Consider
    \begin{verbatim}
  **  
  **  = { 1 | -1 } =: +/- 1
      
    \end{verbatim}

Whoever moves in this game gets a free move for their efforts.

\paragraph{Exercise:}
\mbox{}\\    Confirm that for a number $x$,
\begin{itemize}\item $\pm 1 > x$ if $x < 1$,
\item   $\pm 1 < x$ if $x > 1$,
\item   $\pm 1\;\|\;x$ if $-1 \leq  x \leq  1$.
\end{itemize}

Games like $\pm 1$, where there is an advantage to moving, are called \underline{hot}
games.

For a positive number $x$, define $\pm x := \{ x \;|\; -x \}$. These games are
called \underline{switches}.

\paragraph{Lemma:}
\mbox{}\\    If $x$ and $y$ are numbers with $x \geq  y$, then
\mbox{}\\ \mbox{\hspace{2em}}$\{ x \;|\; y \} = \frac{x+y}2 \pm \frac{x-y}2$.

\paragraph{Example:}
\mbox{}\\    \begin{verbatim}
     
  *** 
  *** = { 2 | -1/2 } = 3/4 +/- 5/4.
      
    \end{verbatim}

Roughly, it is sound strategy to play in the "hottest" component first.

Certainly this is true of games which are sums of switches, numbers and
nimbers:\
\\on your turn, you should play in the largest switch if there is one,
\\else if there are impartial components you should use the nim strategy to deal
with them,
\\and finally you should (always) only play in a number if there's nothing else
left.

This advice is far from being enough to let you win any domineering game
(it's actually a hard game, tournaments have been played), but it's a start!

See pp114-121 of "On Numbers and Games" for much more on Domineering.

\subsection*{Further reading}
The classic texts are
\begin{itemize}\item E. Berlekamp, J. Conway, and R. Guy, "Winning Ways for your Mathematical
Plays, vol 1";
\item John Conway, "On Numbers and Games".
\end{itemize}

There are also two more recent books,
\begin{itemize}\item M. Albert, R. Nowakowski, D. Wolfe, "Lessons in Play: An Introduction to
Combinatorial Game Theory";
\item Aaron Siegel, "Combinatorial Game Theory".
\end{itemize}

\end{document}
