\chapter{Combinatorics} \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. As an example, consider the problem of counting the number of ways to represent an integer $n$ as a sum of positive integers. For example, there are 8 representations for the number $4$: \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} A combinatorial problem can often be solved using a recursive function. In this problem, we can define a function $f(n)$ that gives the number of representations for $n$. For example, $f(4)=8$ according to the above example. The values of the function can be recursively calculated as follows: \begin{equation*} f(n) = \begin{cases} 1 & n = 0\\ f(0)+f(1)+\cdots+f(n-1) & n > 0\\ \end{cases} \end{equation*} The base case is $f(0)=1$, because the empty sum represents the number 0. Then, if $n>0$, we consider all ways to choose the first number of the sum. If the first number is $k$, there are $f(n-k)$ representations for the remaining part of the sum. Thus, we calculate the sum of all values of the form $f(n-k)$ where $k2 \\ \end{cases} \end{equation*} The formula can be derived by considering the possibilities how the element 1 changes in the derangement. There are $n-1$ ways to choose an element $x$ that replaces the element 1. In each such choice, there are two options: \textit{Option 1:} We also replace the element $x$ with the element 1. After this, the remaining task is to construct a derangement of $n-2$ elements. \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. \section{Burnside's lemma} \index{Burnside's lemma} \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 the number of combinations so that only one representative is counted for each group of symmetric combinations. Burnside's lemma states that the number of combinations is \[\sum_{k=1}^n \frac{c(k)}{n},\] 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. As an example, let us calculate the number of necklaces of $n$ pearls, where each pearl has $m$ possible colors. Two necklaces are symmetric if they are similar after rotating them. For example, the necklace \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} has the following symmetric necklaces: \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} 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$. The reason for this is that blocks of pearls of size $\textrm{gcd}(k,n)$ will replace each other. Thus, according to Burnside's lemma, the number of necklaces is \[\sum_{i=0}^{n-1} \frac{m^{\textrm{gcd}(i,n)}}{n}. \] For example, the number of necklaces of length 4 with 3 colors is \[\frac{3^4+3+3^2+3}{4} = 24. \] \section{Cayley's formula} \index{Cayley's formula} \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 there are $n^{n-2}$ labeled trees that contain $n$ nodes. The nodes are labeled $1,2,\ldots,n$, and two trees are different if either their structure or labeling is different. \begin{samepage} For example, when $n=4$, the number of labeled trees is $4^{4-2}=16$: \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} Next we will see how Cayley's formula can be derived using Prüfer codes. \subsubsection{Prüfer code} \index{Prüfer code} 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 $n-2$ numbers that describes a labeled tree. 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. For example, let us calculate the Prüfer code of the following graph: \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) -- (4); \path[draw,thick,-] (3) -- (4); \path[draw,thick,-] (2) -- (4); \path[draw,thick,-] (2) -- (5); \end{tikzpicture} \end{center} First we remove node 1 and add node 4 to the code: \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) -- (4); \path[draw,thick,-] (3) -- (4); \path[draw,thick,-] (2) -- (4); \path[draw,thick,-] (2) -- (5); \end{tikzpicture} \end{center} Then we remove node 3 and add node 4 to the code: \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) -- (4); %\path[draw,thick,-] (3) -- (4); \path[draw,thick,-] (2) -- (4); \path[draw,thick,-] (2) -- (5); \end{tikzpicture} \end{center} Finally we remove node 4 and add node 2 to the code: \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) -- (4); %\path[draw,thick,-] (3) -- (4); %\path[draw,thick,-] (2) -- (4); \path[draw,thick,-] (2) -- (5); \end{tikzpicture} \end{center} Thus, the Prüfer code of the graph is $[4,4,2]$. We can construct a Prüfer code for any tree, and more importantly, 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$.