cphb/chapter22.tex

875 lines
25 KiB
TeX
Raw Normal View History

2016-12-28 23:54:51 +01:00
\chapter{Combinatorics}
2017-01-13 17:44:18 +01:00
\index{combinatorics}
\key{Combinatorics} studies methods for counting
combinations of objects.
Usually, the goal is to find a way to
count the combinations efficiently
without generating each combination separately.
2017-02-09 23:10:07 +01:00
As an example, let us consider the problem
of counting the number of ways to
represent an integer $n$ as a sum of positive integers.
2017-01-13 17:44:18 +01:00
For example, there are 8 representations
for the number $4$:
2016-12-28 23:54:51 +01:00
\begin{multicols}{2}
\begin{itemize}
\item $1+1+1+1$
\item $1+1+2$
\item $1+2+1$
\item $2+1+1$
\item $2+2$
\item $3+1$
\item $1+3$
\item $4$
\end{itemize}
\end{multicols}
2017-01-13 17:44:18 +01:00
A combinatorial problem can often be solved
using a recursive function.
2017-02-09 23:10:07 +01:00
In this problem, we can define a function $f(n)$
2017-02-18 15:22:23 +01:00
that gives the number of representations for $n$.
2017-01-13 17:44:18 +01:00
For example, $f(4)=8$ according to the above example.
2017-02-09 23:10:07 +01:00
The values of the function
can be recursively calculated as follows:
2016-12-28 23:54:51 +01:00
\begin{equation*}
f(n) = \begin{cases}
1 & n = 1\\
f(1)+f(2)+\ldots+f(n-1)+1 & n > 1\\
\end{cases}
\end{equation*}
2017-01-13 17:44:18 +01:00
The base case is $f(1)=1$,
because there is only one way to represent the number 1.
2017-02-09 23:10:07 +01:00
When $n>1$, we go through all ways to
2017-02-18 15:22:23 +01:00
choose the last number in the sum.
2017-01-13 17:44:18 +01:00
For example, in when $n=4$, the sum can end
with $+1$, $+2$ or $+3$.
In addition, we also count the representation
that only contains $n$.
The first values for the function are:
2016-12-28 23:54:51 +01:00
\[
\begin{array}{lcl}
f(1) & = & 1 \\
f(2) & = & 2 \\
f(3) & = & 4 \\
f(4) & = & 8 \\
f(5) & = & 16 \\
\end{array}
\]
2017-01-13 17:44:18 +01:00
It turns out that the function also has a closed-form formula
2016-12-28 23:54:51 +01:00
\[
f(n)=2^{n-1},
\]
2017-01-13 17:44:18 +01:00
which is based on the fact that there are $n-1$
2017-02-18 15:22:23 +01:00
possible positions for +-signs in the sum
2017-01-13 17:44:18 +01:00
and we can choose any subset of them.
2016-12-28 23:54:51 +01:00
2017-01-13 19:06:09 +01:00
\section{Binomial coefficients}
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
\index{binomial coefficient}
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
The \key{binomial coefficient} ${n \choose k}$
equals the number of ways we can choose a subset
2017-01-13 17:44:18 +01:00
of $k$ elements from a set of $n$ elements.
For example, ${5 \choose 3}=10$,
because the set $\{1,2,3,4,5\}$
has 10 subsets of 3 elements:
2016-12-28 23:54:51 +01:00
\[ \{1,2,3\}, \{1,2,4\}, \{1,2,5\}, \{1,3,4\}, \{1,3,5\},
\{1,4,5\}, \{2,3,4\}, \{2,3,5\}, \{2,4,5\}, \{3,4,5\} \]
2017-01-13 17:44:18 +01:00
\subsubsection{Formula 1}
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
Binomial coefficients can be
recursively calculated as follows:
2016-12-28 23:54:51 +01:00
\[
{n \choose k} = {n-1 \choose k-1} + {n-1 \choose k}
\]
2017-02-09 23:10:07 +01:00
The idea is to fix an element $x$ in the set.
2017-01-13 17:44:18 +01:00
If $x$ is included in the subset,
2017-02-09 23:10:07 +01:00
we have to choose $k-1$
2017-01-13 17:44:18 +01:00
elements from $n-1$ elements,
2017-02-09 23:10:07 +01:00
and if $x$ is not included in the subset,
we have to choose $k$ elements from $n-1$ elements.
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
The base cases for the recursion are
2016-12-28 23:54:51 +01:00
\[
2017-02-09 23:10:07 +01:00
{n \choose 0} = {n \choose n} = 1,
2016-12-28 23:54:51 +01:00
\]
2017-02-09 23:10:07 +01:00
because there is always exactly
one way to construct an empty subset
and a subset that contains all the elements.
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
\subsubsection{Formula 2}
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
Another way to calculate binomial coefficients is as follows:
2016-12-28 23:54:51 +01:00
\[
{n \choose k} = \frac{n!}{k!(n-k)!}.
\]
2017-02-09 23:10:07 +01:00
There are $n!$ permutations of $n$ elements.
We go through all permutations and always
2017-01-13 17:44:18 +01:00
select the first $k$ elements of the permutation
to the subset.
Since the order of the elements in the subset
2017-02-09 23:10:07 +01:00
and outside the subset does not matter,
2017-01-13 17:44:18 +01:00
the result is divided by $k!$ and $(n-k)!$
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
\subsubsection{Properties}
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
For binomial coefficients,
2016-12-28 23:54:51 +01:00
\[
{n \choose k} = {n \choose n-k},
\]
2017-01-13 17:44:18 +01:00
because we can either select $k$
2017-02-09 23:10:07 +01:00
elements that belong to the subset
or $n-k$ elements that
do not belong to the subset.
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
The sum of binomial coefficients is
2016-12-28 23:54:51 +01:00
\[
{n \choose 0}+{n \choose 1}+{n \choose 2}+\ldots+{n \choose n}=2^n.
\]
2017-01-13 17:44:18 +01:00
The reason for the name ''binomial coefficient''
is that
2016-12-28 23:54:51 +01:00
\[ (a+b)^n =
{n \choose 0} a^n b^0 +
{n \choose 1} a^{n-1} b^1 +
\ldots +
{n \choose n-1} a^1 b^{n-1} +
{n \choose n} a^0 b^n. \]
2017-01-13 17:44:18 +01:00
\index{Pascal's triangle}
2016-12-28 23:54:51 +01:00
2017-01-13 17:44:18 +01:00
Binomial coefficients also appear in
\key{Pascal's triangle}
2017-02-09 23:10:07 +01:00
where each value equals the sum of two
2017-01-13 17:44:18 +01:00
above values:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}{0.9}
\node at (0,0) {1};
\node at (-0.5,-0.5) {1};
\node at (0.5,-0.5) {1};
\node at (-1,-1) {1};
\node at (0,-1) {2};
\node at (1,-1) {1};
\node at (-1.5,-1.5) {1};
\node at (-0.5,-1.5) {3};
\node at (0.5,-1.5) {3};
\node at (1.5,-1.5) {1};
\node at (-2,-2) {1};
\node at (-1,-2) {4};
\node at (0,-2) {6};
\node at (1,-2) {4};
\node at (2,-2) {1};
\node at (-2,-2.5) {$\ldots$};
\node at (-1,-2.5) {$\ldots$};
\node at (0,-2.5) {$\ldots$};
\node at (1,-2.5) {$\ldots$};
\node at (2,-2.5) {$\ldots$};
\end{tikzpicture}
\end{center}
2017-01-13 18:20:18 +01:00
\subsubsection{Boxes and balls}
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
''Boxes and balls'' is a useful model,
2017-01-13 18:20:18 +01:00
where we count the ways to
place $k$ balls in $n$ boxes.
2017-02-09 23:10:07 +01:00
Let us consider three scenarios:
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
\textit{Scenario 1}: Each box can contain
2017-01-13 18:20:18 +01:00
at most one ball.
For example, when $n=5$ and $k=2$,
there are 10 solutions:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.5]
\newcommand\lax[3]{
\path[draw,thick,-] (#1-0.5,#2+0.5) -- (#1-0.5,#2-0.5) --
(#1+0.5,#2-0.5) -- (#1+0.5,#2+0.5);
\ifthenelse{\equal{#3}{1}}{\draw[fill=black] (#1,#2-0.3) circle (0.15);}{}
\ifthenelse{\equal{#3}{2}}{\draw[fill=black] (#1-0.2,#2-0.3) circle (0.15);}{}
\ifthenelse{\equal{#3}{2}}{\draw[fill=black] (#1+0.2,#2-0.3) circle (0.15);}{}
}
\newcommand\laa[7]{
\lax{#1}{#2}{#3}
\lax{#1+1.2}{#2}{#4}
\lax{#1+2.4}{#2}{#5}
\lax{#1+3.6}{#2}{#6}
\lax{#1+4.8}{#2}{#7}
}
\laa{0}{0}{1}{1}{0}{0}{0}
\laa{0}{-2}{1}{0}{1}{0}{0}
\laa{0}{-4}{1}{0}{0}{1}{0}
\laa{0}{-6}{1}{0}{0}{0}{1}
\laa{8}{0}{0}{1}{1}{0}{0}
\laa{8}{-2}{0}{1}{0}{1}{0}
\laa{8}{-4}{0}{1}{0}{0}{1}
\laa{16}{0}{0}{0}{1}{1}{0}
\laa{16}{-2}{0}{0}{1}{0}{1}
\laa{16}{-4}{0}{0}{0}{1}{1}
\end{tikzpicture}
\end{center}
2017-02-09 23:10:07 +01:00
In this scenario, the answer is directly the
2017-01-13 18:20:18 +01:00
binomial coefficient ${n \choose k}$.
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
\textit{Scenario 2}: A box can contain multiple balls.
2017-01-13 18:20:18 +01:00
For example, when $n=5$ and $k=2$,
there are 15 solutions:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.5]
\newcommand\lax[3]{
\path[draw,thick,-] (#1-0.5,#2+0.5) -- (#1-0.5,#2-0.5) --
(#1+0.5,#2-0.5) -- (#1+0.5,#2+0.5);
\ifthenelse{\equal{#3}{1}}{\draw[fill=black] (#1,#2-0.3) circle (0.15);}{}
\ifthenelse{\equal{#3}{2}}{\draw[fill=black] (#1-0.2,#2-0.3) circle (0.15);}{}
\ifthenelse{\equal{#3}{2}}{\draw[fill=black] (#1+0.2,#2-0.3) circle (0.15);}{}
}
\newcommand\laa[7]{
\lax{#1}{#2}{#3}
\lax{#1+1.2}{#2}{#4}
\lax{#1+2.4}{#2}{#5}
\lax{#1+3.6}{#2}{#6}
\lax{#1+4.8}{#2}{#7}
}
\laa{0}{0}{2}{0}{0}{0}{0}
\laa{0}{-2}{1}{1}{0}{0}{0}
\laa{0}{-4}{1}{0}{1}{0}{0}
\laa{0}{-6}{1}{0}{0}{1}{0}
\laa{0}{-8}{1}{0}{0}{0}{1}
\laa{8}{0}{0}{2}{0}{0}{0}
\laa{8}{-2}{0}{1}{1}{0}{0}
\laa{8}{-4}{0}{1}{0}{1}{0}
\laa{8}{-6}{0}{1}{0}{0}{1}
\laa{8}{-8}{0}{0}{2}{0}{0}
\laa{16}{0}{0}{0}{1}{1}{0}
\laa{16}{-2}{0}{0}{1}{0}{1}
\laa{16}{-4}{0}{0}{0}{2}{0}
\laa{16}{-6}{0}{0}{0}{1}{1}
\laa{16}{-8}{0}{0}{0}{0}{2}
\end{tikzpicture}
\end{center}
2017-02-09 23:10:07 +01:00
The process of placing the balls in the boxes
can be represented as a string
2017-01-13 18:20:18 +01:00
that consists of symbols
''o'' and ''$\rightarrow$''.
2017-02-09 23:10:07 +01:00
Initially, assume that we are standing at the leftmost box.
The symbol ''o'' means that we place a ball
2017-01-13 18:20:18 +01:00
in the current box, and the symbol
''$\rightarrow$'' means that we move to
2017-02-09 23:10:07 +01:00
the next box to the right.
2017-01-13 18:20:18 +01:00
Using this notation, each solution is a string
2017-02-09 23:10:07 +01:00
that contains $k$ times the symbol ''o'' and
2017-01-13 18:20:18 +01:00
$n-1$ times the symbol ''$\rightarrow$''.
For example, the upper-right solution
2017-02-09 23:10:07 +01:00
in the above picture corresponds to the string
2017-01-13 18:20:18 +01:00
''$\rightarrow$ $\rightarrow$ o $\rightarrow$ o $\rightarrow$''.
Thus, the number of solutions is
${k+n-1 \choose k}$.
2017-02-09 23:10:07 +01:00
\textit{Scenario 3}: Each box may contain at most one ball,
2017-01-13 18:20:18 +01:00
and in addition, no two adjacent boxes may both contain a ball.
For example, when $n=5$ and $k=2$,
there are 6 solutions:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.5]
\newcommand\lax[3]{
\path[draw,thick,-] (#1-0.5,#2+0.5) -- (#1-0.5,#2-0.5) --
(#1+0.5,#2-0.5) -- (#1+0.5,#2+0.5);
\ifthenelse{\equal{#3}{1}}{\draw[fill=black] (#1,#2-0.3) circle (0.15);}{}
\ifthenelse{\equal{#3}{2}}{\draw[fill=black] (#1-0.2,#2-0.3) circle (0.15);}{}
\ifthenelse{\equal{#3}{2}}{\draw[fill=black] (#1+0.2,#2-0.3) circle (0.15);}{}
}
\newcommand\laa[7]{
\lax{#1}{#2}{#3}
\lax{#1+1.2}{#2}{#4}
\lax{#1+2.4}{#2}{#5}
\lax{#1+3.6}{#2}{#6}
\lax{#1+4.8}{#2}{#7}
}
\laa{0}{0}{1}{0}{1}{0}{0}
\laa{0}{-2}{1}{0}{0}{1}{0}
\laa{8}{0}{1}{0}{0}{0}{1}
\laa{8}{-2}{0}{1}{0}{1}{0}
\laa{16}{0}{0}{1}{0}{0}{1}
\laa{16}{-2}{0}{0}{1}{0}{1}
\end{tikzpicture}
\end{center}
2017-02-09 23:10:07 +01:00
In this scenario, we can assume that
$k$ balls are initially placed in boxes
and there is an empty box between each
two such boxes.
2017-01-13 18:20:18 +01:00
The remaining task is to choose the
positions for
$n-k-(k-1)=n-2k+1$ empty boxes.
2017-02-09 23:10:07 +01:00
There are $k+1$ positions,
so the number of solutions is
2017-01-13 18:20:18 +01:00
${n-2k+1+k+1-1 \choose n-2k+1} = {n-k+1 \choose n-2k+1}$.
2016-12-28 23:54:51 +01:00
2017-02-20 22:23:10 +01:00
\subsubsection{Multinomial coefficients}
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
\index{multinomial coefficient}
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
The \key{multinomial coefficient}
2016-12-28 23:54:51 +01:00
\[ {n \choose k_1,k_2,\ldots,k_m} = \frac{n!}{k_1! k_2! \cdots k_m!}, \]
2017-02-09 23:10:07 +01:00
equals the number of ways
2017-01-13 18:20:18 +01:00
we can divide $n$ elements into subsets
2017-02-09 23:10:07 +01:00
of sizes $k_1,k_2,\ldots,k_m$,
where $k_1+k_2+\cdots+k_m=n$.
Multinomial coefficients can be seen as a
generalization of binomial cofficients;
if $m=2$, the above formula
2017-01-13 18:20:18 +01:00
corresponds to the binomial coefficient formula.
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
\section{Catalan numbers}
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
\index{Catalan number}
2016-12-28 23:54:51 +01:00
2017-02-26 12:10:29 +01:00
The \key{Catalan number}
%\footnote{E. C. Catalan (1814--1894) was a Belgian mathematician.}
$C_n$ equals the
2017-01-13 18:20:18 +01:00
number of valid
parenthesis expressions that consist of
$n$ left parentheses and $n$ right parentheses.
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
For example, $C_3=5$, because using three
left parentheses and three right parentheses,
we can construct the following parenthesis
expressions:
2016-12-28 23:54:51 +01:00
\begin{itemize}[noitemsep]
\item \texttt{()()()}
\item \texttt{(())()}
\item \texttt{()(())}
\item \texttt{((()))}
\item \texttt{(()())}
\end{itemize}
2017-01-13 18:20:18 +01:00
\subsubsection{Parenthesis expressions}
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
\index{parenthesis expression}
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
What is exactly a \emph{valid parenthesis expression}?
The following rules precisely define all
valid parenthesis expressions:
2016-12-28 23:54:51 +01:00
\begin{itemize}
2017-02-18 15:22:23 +01:00
\item The empty expression is valid.
\item If an expression $A$ is valid,
2017-01-13 18:20:18 +01:00
then also the expression
\texttt{(}$A$\texttt{)} is valid.
\item If expressions $A$ and $B$ are valid,
then also the expression $AB$ is valid.
2016-12-28 23:54:51 +01:00
\end{itemize}
2017-01-13 18:20:18 +01:00
Another way to characterize valid
2017-02-09 23:10:07 +01:00
parenthesis expressions is that if
we choose any prefix of such an expression,
2017-01-13 18:20:18 +01:00
it has to contain at least as many left
parentheses as right parentheses.
In addition, the complete expression has to
contain an equal number of left and right
parentheses.
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
\subsubsection{Formula 1}
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
Catalan numbers can be calculated using the formula
2016-12-28 23:54:51 +01:00
\[ C_n = \sum_{i=0}^{n-1} C_{i} C_{n-i-1}.\]
2017-01-13 18:20:18 +01:00
The sum goes through the ways to divide the
expression into two parts
such that both parts are valid
expressions and the first part is as short as possible
but not empty.
For any $i$, the first part contains $i+1$ pairs
2017-02-18 15:22:23 +01:00
of parentheses and the number of expressions
2017-01-13 18:20:18 +01:00
is the product of the following values:
2016-12-28 23:54:51 +01:00
\begin{itemize}
2017-01-13 18:20:18 +01:00
\item $C_{i}$: number of ways to construct an expression
using the parentheses in the first part,
not counting the outermost parentheses
\item $C_{n-i-1}$: number of ways to construct an
expression using the parentheses in the second part
2016-12-28 23:54:51 +01:00
\end{itemize}
2017-01-13 18:20:18 +01:00
In addition, the base case is $C_0=1$,
because we can construct an empty parenthesis
expression using zero pairs of parentheses.
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
\subsubsection{Formula 2}
2016-12-28 23:54:51 +01:00
2017-01-13 18:20:18 +01:00
Catalan numbers can also be calculated
using binomial coefficients:
2016-12-28 23:54:51 +01:00
\[ C_n = \frac{1}{n+1} {2n \choose n}\]
2017-01-13 18:20:18 +01:00
The formula can be explained as follows:
There are a total of ${2n \choose n}$ ways
to construct a (not necessarily valid)
parenthesis expression that contains $n$ left
parentheses and $n$ right parentheses.
2017-02-09 23:10:07 +01:00
Let us calculate the number of such
2017-01-13 18:20:18 +01:00
expressions that are \emph{not} valid.
If a parenthesis expression is not valid,
it has to contain a prefix where the
number of right parentheses exceeds the
number of left parentheses.
The idea is to reverse each parenthesis
that belongs to such a prefix.
For example, the expression
\texttt{())()(} contains a prefix \texttt{())},
and after reversing the prefix,
the expression becomes \texttt{)((()(}.
The resulting expression consists of $n+1$
left parentheses and $n-1$ right parentheses.
The number of such expressions is ${2n \choose n+1}$
that equals the number of non-valid
parenthesis expressions.
Thus the number of valid parenthesis
expressions can be calculated using the formula
2016-12-28 23:54:51 +01:00
\[{2n \choose n}-{2n \choose n+1} = {2n \choose n} - \frac{n}{n+1} {2n \choose n} = \frac{1}{n+1} {2n \choose n}.\]
2017-01-13 18:20:18 +01:00
\subsubsection{Counting trees}
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
Catalan numbers are also related to trees:
2016-12-28 23:54:51 +01:00
\begin{itemize}
2017-01-13 18:20:18 +01:00
\item there are $C_n$ binary trees of $n$ nodes
\item there are $C_{n-1}$ rooted trees of $n$ nodes
2016-12-28 23:54:51 +01:00
\end{itemize}
\noindent
2017-01-13 18:20:18 +01:00
For example, for $C_3=5$, the binary trees are
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\path[draw,thick,-] (0,0) -- (-1,-1);
\path[draw,thick,-] (0,0) -- (1,-1);
\draw[fill=white] (0,0) circle (0.3);
\draw[fill=white] (-1,-1) circle (0.3);
\draw[fill=white] (1,-1) circle (0.3);
\path[draw,thick,-] (4,0) -- (4-0.75,-1) -- (4-1.5,-2);
\draw[fill=white] (4,0) circle (0.3);
\draw[fill=white] (4-0.75,-1) circle (0.3);
\draw[fill=white] (4-1.5,-2) circle (0.3);
\path[draw,thick,-] (6.5,0) -- (6.5-0.75,-1) -- (6.5-0,-2);
\draw[fill=white] (6.5,0) circle (0.3);
\draw[fill=white] (6.5-0.75,-1) circle (0.3);
\draw[fill=white] (6.5-0,-2) circle (0.3);
\path[draw,thick,-] (9,0) -- (9+0.75,-1) -- (9-0,-2);
\draw[fill=white] (9,0) circle (0.3);
\draw[fill=white] (9+0.75,-1) circle (0.3);
\draw[fill=white] (9-0,-2) circle (0.3);
\path[draw,thick,-] (11.5,0) -- (11.5+0.75,-1) -- (11.5+1.5,-2);
\draw[fill=white] (11.5,0) circle (0.3);
\draw[fill=white] (11.5+0.75,-1) circle (0.3);
\draw[fill=white] (11.5+1.5,-2) circle (0.3);
\end{tikzpicture}
\end{center}
2017-01-13 18:20:18 +01:00
and the rooted trees are
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\path[draw,thick,-] (0,0) -- (-1,-1);
\path[draw,thick,-] (0,0) -- (0,-1);
\path[draw,thick,-] (0,0) -- (1,-1);
\draw[fill=white] (0,0) circle (0.3);
\draw[fill=white] (-1,-1) circle (0.3);
\draw[fill=white] (0,-1) circle (0.3);
\draw[fill=white] (1,-1) circle (0.3);
\path[draw,thick,-] (3,0) -- (3,-1) -- (3,-2) -- (3,-3);
\draw[fill=white] (3,0) circle (0.3);
\draw[fill=white] (3,-1) circle (0.3);
\draw[fill=white] (3,-2) circle (0.3);
\draw[fill=white] (3,-3) circle (0.3);
\path[draw,thick,-] (6+0,0) -- (6-1,-1);
\path[draw,thick,-] (6+0,0) -- (6+1,-1) -- (6+1,-2);
\draw[fill=white] (6+0,0) circle (0.3);
\draw[fill=white] (6-1,-1) circle (0.3);
\draw[fill=white] (6+1,-1) circle (0.3);
\draw[fill=white] (6+1,-2) circle (0.3);
\path[draw,thick,-] (9+0,0) -- (9+1,-1);
\path[draw,thick,-] (9+0,0) -- (9-1,-1) -- (9-1,-2);
\draw[fill=white] (9+0,0) circle (0.3);
\draw[fill=white] (9+1,-1) circle (0.3);
\draw[fill=white] (9-1,-1) circle (0.3);
\draw[fill=white] (9-1,-2) circle (0.3);
\path[draw,thick,-] (12+0,0) -- (12+0,-1) -- (12-1,-2);
\path[draw,thick,-] (12+0,0) -- (12+0,-1) -- (12+1,-2);
\draw[fill=white] (12+0,0) circle (0.3);
\draw[fill=white] (12+0,-1) circle (0.3);
\draw[fill=white] (12-1,-2) circle (0.3);
\draw[fill=white] (12+1,-2) circle (0.3);
\end{tikzpicture}
\end{center}
2017-01-13 18:42:00 +01:00
\section{Inclusion-exclusion}
2016-12-28 23:54:51 +01:00
2017-01-13 18:42:00 +01:00
\index{inclusion-exclusion}
2016-12-28 23:54:51 +01:00
2017-01-13 18:42:00 +01:00
\key{Inclusion-exclusion} is a technique
that can be used for counting the size
of a union of sets when the sizes of
the intersections are known, and vice versa.
A simple example of the technique is the formula
2016-12-28 23:54:51 +01:00
\[ |A \cup B| = |A| + |B| - |A \cap B|,\]
2017-01-13 18:42:00 +01:00
where $A$ and $B$ are sets and $|X|$
is the size of a set $X$.
The formula can be illustrated as follows:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.8]
\draw (0,0) circle (1.5);
\draw (1.5,0) circle (1.5);
\node at (-0.75,0) {\small $A$};
\node at (2.25,0) {\small $B$};
\node at (0.75,0) {\small $A \cap B$};
\end{tikzpicture}
\end{center}
2017-02-09 23:10:07 +01:00
Our goal is to calculate
2017-01-13 18:42:00 +01:00
the size of the union $A \cup B$
that corresponds to the area of the region
2017-02-09 23:10:07 +01:00
that belongs to at least one circle.
2017-01-13 18:42:00 +01:00
The picture shows that we can calculate
the area of $A \cup B$ by first summing the
2017-02-18 15:22:23 +01:00
areas of $A$ and $B$ and then subtracting
2017-01-13 18:42:00 +01:00
the area of $A \cap B$.
2017-02-09 23:10:07 +01:00
The same idea can be applied when the number
2017-01-13 18:42:00 +01:00
of sets is larger.
2017-02-18 15:22:23 +01:00
When there are three sets, the inclusion-exclusion formula is
2016-12-28 23:54:51 +01:00
\[ |A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C| \]
2017-01-13 18:42:00 +01:00
and the corresponding picture is
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.8]
\draw (0,0) circle (1.75);
\draw (2,0) circle (1.75);
\draw (1,1.5) circle (1.75);
\node at (-0.75,-0.25) {\small $A$};
\node at (2.75,-0.25) {\small $B$};
\node at (1,2.5) {\small $C$};
\node at (1,-0.5) {\small $A \cap B$};
\node at (0,1.25) {\small $A \cap C$};
\node at (2,1.25) {\small $B \cap C$};
\node at (1,0.5) {\scriptsize $A \cap B \cap C$};
\end{tikzpicture}
\end{center}
2017-01-13 18:42:00 +01:00
In the general case, the size of the
union $X_1 \cup X_2 \cup \cdots \cup X_n$
2017-02-09 23:10:07 +01:00
can be calculated by going through all possible
intersections that contain some of the sets $X_1,X_2,\ldots,X_n$.
2017-01-13 18:42:00 +01:00
If the intersection contains an odd number of sets,
2017-02-09 23:10:07 +01:00
its size is added to the answer,
and otherwise its size is subtracted from the answer.
2017-01-13 18:42:00 +01:00
2017-02-18 15:22:23 +01:00
Note that there are similar formulas
2017-02-09 23:10:07 +01:00
for calculating
2017-01-13 18:42:00 +01:00
the size of an intersection from the sizes of
unions. For example,
2016-12-28 23:54:51 +01:00
\[ |A \cap B| = |A| + |B| - |A \cup B|\]
2017-01-13 18:42:00 +01:00
and
2016-12-28 23:54:51 +01:00
\[ |A \cap B \cap C| = |A| + |B| + |C| - |A \cup B| - |A \cup C| - |B \cup C| + |A \cup B \cup C| .\]
2017-01-13 18:42:00 +01:00
\subsubsection{Derangements}
2016-12-28 23:54:51 +01:00
2017-01-13 18:42:00 +01:00
\index{derangement}
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
As an example, let us count the number of \key{derangements}
of elements $\{1,2,\ldots,n\}$, i.e., permutations
2017-01-13 18:42:00 +01:00
where no element remains in its original place.
For example, when $n=3$, there are
2017-02-09 23:10:07 +01:00
two possible derangements: $(2,3,1)$ and $(3,1,2)$.
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
One approach for solving the problem is to use
2017-01-13 18:42:00 +01:00
inclusion-exclusion.
Let $X_k$ be the set of permutations
2017-02-09 23:10:07 +01:00
that contain the element $k$ at position $k$.
2017-01-13 18:42:00 +01:00
For example, when $n=3$, the sets are as follows:
2016-12-28 23:54:51 +01:00
\[
\begin{array}{lcl}
X_1 & = & \{(1,2,3),(1,3,2)\} \\
X_2 & = & \{(1,2,3),(3,2,1)\} \\
X_3 & = & \{(1,2,3),(2,1,3)\} \\
\end{array}
\]
2017-02-09 23:10:07 +01:00
Using these sets, the number of derangements equals
2016-12-28 23:54:51 +01:00
\[ n! - |X_1 \cup X_2 \cup \cdots \cup X_n|, \]
2017-01-13 18:42:00 +01:00
so it suffices to calculate the size of the union.
Using inclusion-exclusion, this reduces to
calculating sizes of intersections which can be
done efficiently.
For example, when $n=3$, the size of
$|X_1 \cup X_2 \cup X_3|$ is
2016-12-28 23:54:51 +01:00
\[
\begin{array}{lcl}
& & |X_1| + |X_2| + |X_3| - |X_1 \cap X_2| - |X_1 \cap X_3| - |X_2 \cap X_3| + |X_1 \cap X_2 \cap X_3| \\
& = & 2+2+2-1-1-1+1 \\
& = & 4, \\
\end{array}
\]
2017-01-13 18:42:00 +01:00
so the number of solutions is $3!-4=2$.
2016-12-28 23:54:51 +01:00
2017-02-09 23:10:07 +01:00
It turns out that the problem can also be solved
without using inclusion-exclusion.
2017-01-13 18:42:00 +01:00
Let $f(n)$ denote the number of derangements
for $\{1,2,\ldots,n\}$. We can use the following
recursive formula:
2016-12-28 23:54:51 +01:00
\begin{equation*}
f(n) = \begin{cases}
0 & n = 1\\
1 & n = 2\\
(n-1)(f(n-2) + f(n-1)) & n>2 \\
\end{cases}
\end{equation*}
2017-01-13 18:42:00 +01:00
The formula can be derived by going through
2017-02-09 23:10:07 +01:00
the possibilities how the element 1 changes
2017-01-13 18:42:00 +01:00
in the derangement.
2017-02-09 23:10:07 +01:00
There are $n-1$ ways to choose an element $x$
that replaces the element 1.
2017-01-13 18:42:00 +01:00
In each such choice, there are two options:
2017-02-09 23:10:07 +01:00
\textit{Option 1:} We also replace the element $x$
with the element 1.
2017-01-13 18:42:00 +01:00
After this, the remaining task is to construct
2017-02-09 23:10:07 +01:00
a derangement of $n-2$ elements.
2017-01-13 18:42:00 +01:00
2017-02-09 23:10:07 +01:00
\textit{Option 2:} We replace the element $x$
with some other element than 1.
Now we have to construct a derangement
of $n-1$ element, because we cannot replace
the element $x$ with the element $1$, and all other
elements should be changed.
2016-12-28 23:54:51 +01:00
2017-01-13 19:02:46 +01:00
\section{Burnside's lemma}
2016-12-28 23:54:51 +01:00
2017-01-13 19:02:46 +01:00
\index{Burnside's lemma}
2016-12-28 23:54:51 +01:00
2017-02-26 12:10:29 +01:00
\key{Burnside's lemma}
%\footnote{Actually, Burnside did not discover this lemma; he only mentioned it in his book \cite{bur97}.}
can be used to count
2017-02-18 15:22:23 +01:00
the number of combinations so that
2017-02-09 23:10:07 +01:00
only one representative is counted
2017-02-18 15:22:23 +01:00
for each group of symmetric combinations.
2017-01-13 19:02:46 +01:00
Burnside's lemma states that the number of
combinations is
2016-12-28 23:54:51 +01:00
\[\sum_{k=1}^n \frac{c(k)}{n},\]
2017-01-13 19:02:46 +01:00
where there are $n$ ways to change the
position of a combination,
and there are $c(k)$ combinations that
remain unchanged when the $k$th way is applied.
2017-02-09 23:10:07 +01:00
As an example, let us calculate the number of
2017-01-13 19:02:46 +01:00
necklaces of $n$ pearls,
where the color of each pearl is
one of $1,2,\ldots,m$.
Two necklaces are symmetric if they are
similar after rotating them.
For example, the necklace
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw[fill=white] (0,0) circle (1);
\draw[fill=red] (0,1) circle (0.3);
\draw[fill=blue] (1,0) circle (0.3);
\draw[fill=red] (0,-1) circle (0.3);
\draw[fill=green] (-1,0) circle (0.3);
\end{tikzpicture}
\end{center}
2017-01-13 19:02:46 +01:00
has the following symmetric necklaces:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw[fill=white] (0,0) circle (1);
\draw[fill=red] (0,1) circle (0.3);
\draw[fill=blue] (1,0) circle (0.3);
\draw[fill=red] (0,-1) circle (0.3);
\draw[fill=green] (-1,0) circle (0.3);
\draw[fill=white] (4,0) circle (1);
\draw[fill=green] (4+0,1) circle (0.3);
\draw[fill=red] (4+1,0) circle (0.3);
\draw[fill=blue] (4+0,-1) circle (0.3);
\draw[fill=red] (4+-1,0) circle (0.3);
\draw[fill=white] (8,0) circle (1);
\draw[fill=red] (8+0,1) circle (0.3);
\draw[fill=green] (8+1,0) circle (0.3);
\draw[fill=red] (8+0,-1) circle (0.3);
\draw[fill=blue] (8+-1,0) circle (0.3);
\draw[fill=white] (12,0) circle (1);
\draw[fill=blue] (12+0,1) circle (0.3);
\draw[fill=red] (12+1,0) circle (0.3);
\draw[fill=green] (12+0,-1) circle (0.3);
\draw[fill=red] (12+-1,0) circle (0.3);
\end{tikzpicture}
\end{center}
2017-01-13 19:02:46 +01:00
There are $n$ ways to change the position
of a necklace,
because we can rotate it
$0,1,\ldots,n-1$ steps clockwise.
If the number of steps is 0,
all $m^n$ necklaces remain the same,
and if the number of steps is 1,
only the $m$ necklaces where each
pearl has the same color remain the same.
More generally, when the number of steps is $k$,
a total of
\[m^{\textrm{gcd}(k,n)},\]
necklaces remain the same,
where $\textrm{gcd}(k,n)$ is the greatest common
divisor of $k$ and $n$.
2017-02-09 23:10:07 +01:00
The reason for this is that blocks
of pearls of size $\textrm{gcd}(k,n)$
2017-01-13 19:02:46 +01:00
will replace each other.
Thus, according to Burnside's lemma,
the number of necklaces is
2017-02-09 23:10:07 +01:00
\[\sum_{i=0}^{n-1} \frac{m^{\textrm{gcd}(i,n)}}{n}. \]
2017-01-13 19:02:46 +01:00
For example, the number of necklaces of length 4
with 3 colors is
2016-12-28 23:54:51 +01:00
\[\frac{3^4+3+3^2+3}{4} = 24. \]
2017-01-13 19:02:46 +01:00
\section{Cayley's formula}
2016-12-28 23:54:51 +01:00
2017-01-13 19:02:46 +01:00
\index{Cayley's formula}
2016-12-28 23:54:51 +01:00
2017-02-26 12:10:29 +01:00
\key{Cayley's formula}
% \footnote{While the formula is named after A. Cayley,
% who studied it in 1889, it was discovered earlier by C. W. Borchardt in 1860.}
states that
2017-01-13 19:02:46 +01:00
there are $n^{n-2}$ labeled trees
that contain $n$ nodes.
The nodes are labeled $1,2,\ldots,n$,
and two trees are different
2017-02-09 23:10:07 +01:00
if either their structure or
2017-01-13 19:02:46 +01:00
labeling is different.
2016-12-28 23:54:51 +01:00
\begin{samepage}
2017-01-13 19:02:46 +01:00
For example, when $n=4$, the number of labeled
trees is $4^{4-2}=16$:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.8]
\footnotesize
\newcommand\puua[6]{
\path[draw,thick,-] (#1,#2) -- (#1-1.25,#2-1.5);
\path[draw,thick,-] (#1,#2) -- (#1,#2-1.5);
\path[draw,thick,-] (#1,#2) -- (#1+1.25,#2-1.5);
\node[draw, circle, fill=white] at (#1,#2) {#3};
\node[draw, circle, fill=white] at (#1-1.25,#2-1.5) {#4};
\node[draw, circle, fill=white] at (#1,#2-1.5) {#5};
\node[draw, circle, fill=white] at (#1+1.25,#2-1.5) {#6};
}
\newcommand\puub[6]{
\path[draw,thick,-] (#1,#2) -- (#1+1,#2);
\path[draw,thick,-] (#1+1,#2) -- (#1+2,#2);
\path[draw,thick,-] (#1+2,#2) -- (#1+3,#2);
\node[draw, circle, fill=white] at (#1,#2) {#3};
\node[draw, circle, fill=white] at (#1+1,#2) {#4};
\node[draw, circle, fill=white] at (#1+2,#2) {#5};
\node[draw, circle, fill=white] at (#1+3,#2) {#6};
}
\puua{0}{0}{1}{2}{3}{4}
\puua{4}{0}{2}{1}{3}{4}
\puua{8}{0}{3}{1}{2}{4}
\puua{12}{0}{4}{1}{2}{3}
\puub{0}{-3}{1}{2}{3}{4}
\puub{4.5}{-3}{1}{2}{4}{3}
\puub{9}{-3}{1}{3}{2}{4}
\puub{0}{-4.5}{1}{3}{4}{2}
\puub{4.5}{-4.5}{1}{4}{2}{3}
\puub{9}{-4.5}{1}{4}{3}{2}
\puub{0}{-6}{2}{1}{3}{4}
\puub{4.5}{-6}{2}{1}{4}{3}
\puub{9}{-6}{2}{3}{1}{4}
\puub{0}{-7.5}{2}{4}{1}{3}
\puub{4.5}{-7.5}{3}{1}{2}{4}
\puub{9}{-7.5}{3}{2}{1}{4}
\end{tikzpicture}
\end{center}
\end{samepage}
2017-01-13 19:02:46 +01:00
Next we will see how Cayley's formula can
be derived using Prüfer codes.
2016-12-28 23:54:51 +01:00
2017-01-13 19:02:46 +01:00
\subsubsection{Prüfer code}
2016-12-28 23:54:51 +01:00
2017-01-13 19:02:46 +01:00
\index{Prüfer code}
2016-12-28 23:54:51 +01:00
2017-02-26 12:10:29 +01:00
A \key{Prüfer code}
%\footnote{In 1918, H. Prüfer proved Cayley's theorem using Prüfer codes \cite{pru18}.}
is a sequence of
2017-01-13 19:02:46 +01:00
$n-2$ numbers that describes a labeled tree.
2017-02-09 23:10:07 +01:00
The code is constructed by following a process
that removes $n-2$ leaves from the tree.
At each step, the leaf with the smallest label is removed,
and the label of its only neighbor is added to the code.
2016-12-28 23:54:51 +01:00
2017-01-13 19:02:46 +01:00
For example, the Prüfer code for
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1) at (2,3) {$1$};
\node[draw, circle] (2) at (4,3) {$2$};
\node[draw, circle] (3) at (2,1) {$3$};
\node[draw, circle] (4) at (4,1) {$4$};
\node[draw, circle] (5) at (5.5,2) {$5$};
%\path[draw,thick,-] (1) -- (2);
%\path[draw,thick,-] (1) -- (3);
\path[draw,thick,-] (1) -- (4);
\path[draw,thick,-] (3) -- (4);
\path[draw,thick,-] (2) -- (4);
\path[draw,thick,-] (2) -- (5);
%\path[draw,thick,-] (4) -- (5);
\end{tikzpicture}
\end{center}
2017-01-13 19:02:46 +01:00
is $[4,4,2]$, because we first remove
node 1, then node 3 and finally node 5.
2017-02-09 23:10:07 +01:00
We can construct a Prüfer code for any tree,
2017-01-13 19:02:46 +01:00
and more importantly,
2017-02-09 23:10:07 +01:00
the original tree can be reconstructed
from a Prüfer code.
Hence, the number of labeled trees
of $n$ nodes equals
$n^{n-2}$, the number of Prüfer codes
of size $n$.