-
Notifications
You must be signed in to change notification settings - Fork 121
/
chapterCombinatorics.tex
209 lines (174 loc) · 6.85 KB
/
chapterCombinatorics.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
\chapter{Combinatorics}
\section{Basics}
\subsection{Considerations}
\begin{enumerate}
\item Does \textbf{order} matter?
\item Are the objects \textbf{repeatable}?
\item Are the objects partially \textbf{duplicated}?
\end{enumerate}
If order does not matter, you can pre-set the order.
\subsection{Basic formula}
\begin{eqnarray*}
&& {n \choose k} = \frac{n!}{k!(n-k)!} \\
&& {n \choose k} = {n \choose n-k} \\
&& {n\choose k} = {n-1\choose k} + {n-1 \choose k-1}
\end{eqnarray*}
\subsection{N objects, K ceils}
When $N=10, K=3$:
$$
x_1 + x_2 + x_3 = 10
$$
is equivalent to
$$
*****|**|***
$$
, notice that $*$ are non-order, and it is possible to have
$$
*****||*****
$$
\\
then the formula is:
$$
{n+r \choose r}
$$
,where $r=k-1$.
\\
Intuitively, the meaning is to choose $r$ objects from $n+r$ objects to become the $|$.
\runinhead{Unique paths.} Given a $m \times n$ matrix, starting from $(0, 0)$, ending at $(m-1, n-1)$, can only goes down or right. What is the number of unique paths?
There are total
\subsection{N objects, K types} \label{N_objects_K_types}
What is the number of permutation of $N$ objects with $K$ different types:
\begin{align*}
ret &= \frac{A_N^N}{\prod_{k=1}^K{A_{sz(k)}^{sz(k)}}} \\
&= \frac{N!}{\prod_{k} sz[k]!}
\end{align*}
\subsection{Inclusion–Exclusion Principle}
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[width=\linewidth]{500px-Inclusion-exclusion}}
\caption{Inclusion–exclusion principl}
\label{fig:500px-Inclusion-exclusion}
\end{figure}
\begin{eqnarray*}
|A \cup B \cup C| = |A| + |B| + |C| \\ - |A \cap B| - |A \cap C| - |B \cap C| \\ + |A \cap B \cap C|
\end{eqnarray*}
Generally,
$$
\Biggl|\bigcup_{i=1}^n A_i\Biggr| = \sum_{k = 1}^{n} (-1)^{k+1} \left( \sum_{1 \leq i_{1} < \cdots < i_{k} \leq n} \left| A_{i_{1}} \cap \cdots \cap A_{i_{k}} \right| \right)
$$
\section{Combinations with Duplicated Objects}
Determine the number of combinations of 10 letters (order does not matter) that can be formed from 3A, 4B, 5C.
\subsection{Basic Solution}
If there are no restrictions on the number of any of the letter, it is ${10+2 \choose 2}$; then we get the universal set,
$$
|U|={10+2 \choose 2}
$$
Let $P_A$ be the set that a 10-combination has more than 3A. $P_B$...4B. $P_C$...5C.
The result is:
\begin{align*}
|3A \cap 4B \cap 5C| = & |U|\\
& - sum(|P_i|\cdot \forall i) \\
& + sum(|P_i \cap P_j|\cdot \forall i,j)\\
& - sum(|P_i \cap P_j \cap P_k|\cdot \forall i,jk)
\end{align*}
To calculate $|P_i|$, take $|P_A|$ as an example. \textbf{Pre-set} 4A -- if we take any one of these 10-combinations in $P_A$ and remove 4A we are left with a 6-combination with unlimited on the numbers of letters; thus,
$$
|P_A|={6+2 \choose 2}
$$
Similarly, we can get $P_B, P_C$.
To calculate $|P_i \cap P_j|$, take $|P_A \cap P_B|$ as an example. \textbf{Pre-set} 4A and 5B; thus,
$$
|P_A \cap P_B| = {1+2 \choose 2}
$$
Similarly, we can get other $|P_i \cap P_j|$.
Similarly, we can get other $|P_i \cap P_j \cap P_k|$.
\subsection{Algebra Solution}
The number of 10-combinations that can be made from 3A, 4B, 5C is found from the coefficient of $x^{10}$ in the expansion of:
$$
(1+x+x^2+x^3)(1+x+x^2+x^3+x^4)(1+x+x^2+x^3+x^4+x^5)
$$
And we know:
\begin{eqnarray*}
1+x+x^2+x^3 = (1-x^4)/(1-x) \\
1+x+x^2+x^3+x^4 = (1-x^5)/(1-x) \\
1+x+x^2+x^3+x^4+x^5 = (1-x^6)/(1-x) \\
\end{eqnarray*}
We expand the formula, although the naive way of getting the coefficient of $x^{10}$ is tedious.
\section{Permutation}
\subsection{$k$-th permutation}
Given $n$ and $k$, return the $k$-th permutation sequence. $k\in [1, n!]$. $O(nk)$ in time complexity is easy, can you do it in $O(n^2)$ or less?
Reversed Cantor Expansion
Core clues:
\begin{enumerate}
\item \pyinline{A = [1, 2, ..., n]}
Suppose for $n$ element, the $k$-th permutation is:
\pyinline{ret = [a0, a1, a2, ..., an-1]}
\item \rih{Basic case.} Since \pyinline{[a1, a3, ..., an-1]} has $(n-1)!$ permutations,
if $k < (n-1)!, a_0 = A_0$ (first element in array), else $a_0 = A_{k/(n-1)!}$
\item Recursively, (or iteratively), calculate the values at each position. Similar to Radix.
\begin{enumerate}
\item $a_0 = A_{k_0/(n-1)!}$, where $k_0 = k$
\item $a_1 = A_{k_1/(n-2)!}$, where $k_1 = k_0\%(n-1)!$ in the remaining array $A$
\item $a_2 = A_{k_2/(n-3)!}$, where $k_2 = k_1\%(n-2)!$ in the remaining array $A$
\end{enumerate}
\end{enumerate}
\begin{python}
def getPermutation(self, n, k):
k -= 1 # start from 0
A = range(1, n+1)
k %= math.factorial(n)
ret = []
for i in range(n-1, -1, -1):
idx, k = divmod(k, math.factorial(i))
ret.append(A.pop(idx))
return "".join(map(str, ret))
\end{python}
\subsection{Numbers counting}
\runinhead{Count numbers with unique digit.} Given a non-negative integer n, count all numbers with unique digits, $x$, where $0 \leq x < 10^n$.
Digit by digit:
\begin{enumerate}
\item The 1st digit has 10 possibilities. The 2nd digit has 9 possibilities. Therefore it seems to be $A_{10}^n$.
\item Exception: The first digit cannot be 0. Therefore it is $9\times 9\times 8\times ...\times (10-i)$
\end{enumerate}
\section{Catalan Number}\label{section:catalanNumber}
\subsection{Math}
\runinhead{Definition.}
$$
C_n = {2n\choose n} - {2n\choose n+1} = {1\over n+1}{2n\choose n} \quad\text{ for }n\ge 0
$$
\runinhead{Proof.} Proof of Calatan Number $C_n ={2n\choose n} - {2n\choose n+1}$. Objective: count the number of paths in $n\times n$ grid without exceeding the main diagonal.
\begin{itemize}
\begin{figure}[]
\centerline{\includegraphics[height = 1.6in]{catalan_proof}}
\caption{Monotonic Paths}
\label{fig:catalanProof}
\end{figure}
\item monotonic paths - $n$ right, $n$ up
$$
{2n\choose n}
$$
\item flip at the line just above the diagonal line - $n-1$ right, $n+1$ up
$$
{n-1+n+1\choose n-1}
$$
\item thus, the number of path without \textit{exceedance} (i.e. passing the diagonal line) is:
\begin{align*}
C_n &= {2n\choose n} - {2n\choose n-1}\\
&={2n\choose n} - {2n\choose n+1}
\end{align*}
\end{itemize}
\subsection{Applications}
The paths in Figure \ref{fig:catalanProof} can be abstracted to anything that at any time \#right $\geq$ \#up.
\runinhead{\#Parentheses.}Number of different ways of adding parentheses. At any time, \#\pyinline{(} $\geq$ \#\pyinline{)}.
\runinhead{\#BSTs.}Number of different BSTs. Consider it as a set of same binary operators with their operands. Reduce this problem to \#Parentheses.
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[width=\linewidth]{Catalan_number_binary_tree_example}}
\caption{\#BSTs. Circles are operators; crescents are operands.}
\label{fig:NumberOfBSTs}
\end{figure}
\section{Stirling Number}
a Stirling number of the second kind (or Stirling partition number) is the number of ways to partition a set of n objects into k non-empty subsets and is denoted by $S(n,k)$ or $\lbrace{n\atop k}\rbrace$.
$$
\left\{ {n \atop k}\right\} = \frac{1}{k!}\sum_{j=0}^{k} (-1)^{k-j} \binom{k}{j} j^n.
$$