You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/combinatorics/binomial-coefficients.md
+31-12Lines changed: 31 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,21 +34,36 @@ Note that for $n \lt k$ the value of $\binom n k$ is assumed to be zero.
34
34
Binomial coefficients have many different properties. Here are the simplest of them:
35
35
36
36
* Symmetry rule:
37
-
$$ \binom n k = \binom n {n-k} $$
37
+
38
+
\[ \binom n k = \binom n {n-k} \]
39
+
38
40
* Factoring in:
39
-
$$ \binom n k = \frac n k \binom {n-1} {k-1} $$
41
+
42
+
\[ \binom n k = \frac n k \binom {n-1} {k-1} \]
43
+
40
44
* Sum over $k$:
41
-
$$ \sum_{k = 0}^n \binom n k = 2 ^ n $$
45
+
46
+
\[ \sum_{k = 0}^n \binom n k = 2 ^ n \]
47
+
42
48
* Sum over $n$:
43
-
$$ \sum_{m = 0}^n \binom m k = \binom {n + 1} {k + 1} $$
49
+
50
+
\[ \sum_{m = 0}^n \binom m k = \binom {n + 1} {k + 1} \]
51
+
44
52
* Sum over $n$ and $k$:
45
-
$$ \sum_{k = 0}^m \binom {n + k} k = \binom {n + m + 1} m $$
53
+
54
+
\[ \sum_{k = 0}^m \binom {n + k} k = \binom {n + m + 1} m \]
55
+
46
56
* Sum of the squares:
47
-
$$ {\binom n 0}^2 + {\binom n 1}^2 + \cdots + {\binom n n}^2 = \binom {2n} n $$
57
+
58
+
\[ {\binom n 0}^2 + {\binom n 1}^2 + \cdots + {\binom n n}^2 = \binom {2n} n \]
59
+
48
60
* Weighted sum:
49
-
$$ 1 \binom n 1 + 2 \binom n 2 + \cdots + n \binom n n = n 2^{n-1} $$
61
+
62
+
\[ 1 \binom n 1 + 2 \binom n 2 + \cdots + n \binom n n = n 2^{n-1} \]
63
+
50
64
* Connection with the [Fibonacci numbers](../algebra/fibonacci-numbers.md):
51
-
$$ \binom n 0 + \binom {n-1} 1 + \cdots + \binom {n-k} k + \cdots + \binom 0 n = F_{n+1} $$
65
+
66
+
\[ \binom n 0 + \binom {n-1} 1 + \cdots + \binom {n-k} k + \cdots + \binom 0 n = F_{n+1} \]
52
67
53
68
## Calculation
54
69
@@ -100,25 +115,28 @@ for (int n = 1; n <= maxn; ++n) {
100
115
101
116
If the entire table of values is not necessary, storing only two last rows of it is sufficient (current $n$-th row and the previous $n-1$-th).
102
117
103
-
### Calculation in $O(1)$
118
+
### Calculation in $O(1)$ {data-toc-label="Calculation in O(1)"}
104
119
105
120
Finally, in some situations it is beneficial to precompute all the factorials in order to produce any necessary binomial coefficient with only two divisions later. This can be advantageous when using [long arithmetic](../algebra/big-integer.md), when the memory does not allow precomputation of the whole Pascal's triangle.
Quite often you come across the problem of computing binomial coefficients modulo some $m$.
111
126
112
-
### Binomial coefficient for small $n$
127
+
### Binomial coefficient for small $n$ {data-toc-label="Binomial coefficient for small n"}
113
128
114
129
The previously discussed approach of Pascal's triangle can be used to calculate all values of $\binom{n}{k} \bmod m$ for reasonably small $n$, since it requires time complexity $\mathcal{O}(n^2)$. This approach can handle any modulo, since only addition operations are used.
115
130
116
131
117
132
### Binomial coefficient modulo large prime
118
133
119
134
The formula for the binomial coefficients is
135
+
120
136
$$\binom n k = \frac {n!} {k!(n-k)!},$$
137
+
121
138
so if we want to compute it modulo some prime $m > n$ we get
139
+
122
140
$$\binom n k \equiv n! \cdot (k!)^{-1} \cdot ((n-k)!)^{-1} \mod m.$$
123
141
124
142
First we precompute all factorials modulo $m$ up to $\text{MAXN}!$ in $O(\text{MAXN})$ time.
@@ -158,6 +176,7 @@ We compute for each $x!$ the biggest exponent $c$ such that $p^c$ divides $x!$,
158
176
Let $c(x)$ be that number.
159
177
And let $g(x) := \frac{x!}{p^{c(x)}}$.
160
178
Then we can write the binomial coefficient as:
179
+
161
180
$$\binom n k = \frac {g(n) p^{c(n)}} {g(k) p^{c(k)} g(n-k) p^{c(n-k)}} = \frac {g(n)} {g(k) g(n-k)}p^{c(n) - c(k) - c(n-k)}$$
162
181
163
182
The interesting thing is, that $g(x)$ is now free from the prime divisor $p$.
@@ -177,7 +196,7 @@ We can compute the binomial coefficient modulo $p_i^{e_i}$ for every $i$.
177
196
This gives us $h$ different congruences.
178
197
Since all moduli $p_i^{e_i}$ are coprime, we can apply the [Chinese Remainder Theorem](../algebra/chinese-remainder-theorem.md) to compute the binomial coefficient modulo the product of the moduli, which is the desired binomial coefficient modulo $m$.
179
198
180
-
### Binomial coefficient for large $n$ and small modulo
199
+
### Binomial coefficient for large $n$ and small modulo {data-toc-label="Binomial coefficient for large n and small modulo"}
181
200
182
201
When $n$ is too large, the $\mathcal{O}(n)$ algorithms discussed above become impractical. However, if the modulo $m$ is small there are still ways to calculate $\binom{n}{k} \bmod m$.
Copy file name to clipboardExpand all lines: src/combinatorics/bracket_sequences.md
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,9 +45,11 @@ Otherwise it is.
45
45
46
46
The number of balanced bracket sequences with only one bracket type can be calculated using the [Catalan numbers](catalan-numbers.md).
47
47
The number of balanced bracket sequences of length $2n$ ($n$ pairs of brackets) is:
48
+
48
49
$$\frac{1}{n+1} \binom{2n}{n}$$
49
50
50
51
If we allow $k$ types of brackets, then each pair be of any of the $k$ types (independently of the others), thus the number of balanced bracket sequences is:
52
+
51
53
$$\frac{1}{n+1} \binom{2n}{n} k^n$$
52
54
53
55
### Dynamic programming
@@ -59,7 +61,9 @@ And somewhere later is the corresponding closing bracket of the pair.
59
61
It is clear that inside this pair there is a balanced bracket sequence, and similarly after this pair there is a balanced bracket sequence.
60
62
So to compute $d[n]$, we will look at how many balanced sequences of $i$ pairs of brackets are inside this first bracket pair, and how many balanced sequences with $n-1-i$ pairs are after this pair.
61
63
Consequently the formula has the form:
64
+
62
65
$$d[n] = \sum_{i=0}^{n-1} d[i] \cdot d[n-1-i]$$
66
+
63
67
The initial value for this recurrence is $d[0] = 1$.
64
68
65
69
## Finding the lexicographical next balanced sequence
@@ -79,7 +83,7 @@ We change the symbol, compute the number of opening and closing brackets that we
79
83
80
84
If we find do suitable position, then this sequence is already the maximal possible one, and there is no answer.
81
85
82
-
```cpp next_balanced_brackets_sequence
86
+
```{.cppfile=next_balanced_brackets_sequence}
83
87
boolnext_balanced_sequence(string & s) {
84
88
int n = s.size();
85
89
int depth = 0;
@@ -128,7 +132,9 @@ For the start value $i = 0$ the answer is obvious: $d[0][0] = 1$, and $d[0][j] =
128
132
Now let $i > 0$, and we look at the last character in the sequence.
129
133
If the last character was an opening bracket $($, then the state before was $(i-1, j-1)$, if it was a closing bracket $)$, then the previous state was $(i-1, j+1)$.
130
134
Thus we obtain the recursion formula:
135
+
131
136
$$d[i][j] = d[i-1][j-1] + d[i-1][j+1]$$
137
+
132
138
$d[i][j] = 0$ holds obviously for negative $j$.
133
139
Thus we can compute this array in $O(n^2)$.
134
140
@@ -142,14 +148,16 @@ If the current character $s[i]$ is equal to $)$, then we must add $d[2n-i-1][\te
142
148
New let there be $k$ different bracket types.
143
149
144
150
Thus, when we look at the current character $s[i]$ before recomputing $\text{depth}$, we have to go through all bracket types that are smaller than the current character, and try to put this bracket into the current position (obtaining a new balance $\text{ndepth} = \text{depth} \pm 1$), and add the number of ways to finish the sequence (length $2n-i-1$, balance $ndepth$) to the answer:
151
+
145
152
$$d[2n - i - 1][\text{ndepth}] \cdot k^{\frac{2n - i - 1 - ndepth}{2}}$$
153
+
146
154
This formula can be derived as follows:
147
155
First we "forget" that there are multiple bracket types, and just take the answer $d[2n - i - 1][\text{ndepth}]$.
148
156
Now we consider how the answer will change is we have $k$ types of brackets.
149
157
We have $2n - i - 1$ undefined positions, of which $\text{ndepth}$ are already predetermined because of the opening brackets.
150
158
But all the other brackets ($(2n - i - i - \text{ndepth})/2$ pairs) can be of any type, therefore we multiply the number by such a power of $k$.
151
159
152
-
## Finding the $k$-th sequence
160
+
## Finding the $k$-th sequence {data-toc-label="Finding the k-th sequence"}
153
161
154
162
Let $n$ be the number of bracket pairs in the sequence.
155
163
We have to find the $k$-th balanced sequence in lexicographically sorted list of all balanced sequences for a given $k$.
@@ -165,7 +173,7 @@ To have to put an opening bracket character, it $d[2n - i - 1][\text{depth}+1] \
165
173
We increment the counter $\text{depth}$, and move on to the next character.
166
174
Otherwise we decrement $k$ by $d[2n - i - 1][\text{depth}+1]$, put a closing bracket and move on.
Copy file name to clipboardExpand all lines: src/combinatorics/burnside.md
+23-8Lines changed: 23 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,8 +36,10 @@ At the same time we introduce a partition of this set into equivalence classes.
36
36
37
37
For example, suppose $n = 3$, and the tree consists of the root $1$ and its two children $2$ and $3$.
38
38
Then the following functions $f_1$ and $f_2$ are considered equivalent.
39
-
$$\begin{array}{ll}f_1(1) = 0 & f_2(1) = 0\\\\
40
-
f_1(2) = 1 & f_2(2) = 0\\\\
39
+
40
+
$$\begin{array}{ll}
41
+
f_1(1) = 0 & f_2(1) = 0\\
42
+
f_1(2) = 1 & f_2(2) = 0\\
41
43
f_1(3) = 0 & f_2(3) = 1
42
44
\end{array}$$
43
45
@@ -47,6 +49,7 @@ Why do these two function $f_1$ and $f_2$ belong to the same equivalence class?
47
49
Intuitively this is understandable - we can rearrange the children of vertex $1$, the vertices $2$ and $3$, and after such a transformation of the function $f_1$ it will coincide with $f_2$.
48
50
49
51
But formally this means that there exists an **invariant permutation** $\pi$ (i.e. a permutation which does not change the object itself, but only its representation), such that:
52
+
50
53
$$f_2 \pi \equiv f_1$$
51
54
52
55
So starting from the definition of objects, we can find all the invariant permutations, i.e. all permutations which do not change the object when applying the permutation to the representation.
@@ -69,6 +72,7 @@ We denote by $I(\pi)$ the **number of fixed points** for the permutation $\pi$.
69
72
70
73
Then **Burnside's lemma** goes as follows:
71
74
the number of equivalence classes is equal to the sum of the numbers of fixed points with respect to all permutations from the group $G$, divided by the size of this group:
Although Burnside's lemma itself is not so convenient to use in practice (it is unclear how to quickly look for the value $I(\pi)$, it most clearly reveals the mathematical essence on which the idea of calculating equivalence classes is based.
@@ -81,11 +85,13 @@ The proof here is the simplest known, and does not use group theory.
81
85
The proof was published by Kenneth P. Bogart in 1991.
The value on the right side is nothing more than the number of "invariant pairs" $(f, \pi)$, i.e. pairs such that $f \pi \equiv f$.
87
92
It is obvious that we can change the order of summation.
88
93
We let the sum iterate over all elements $f$ and sum over the values $J(f)$ - the number of permutations for which $f$ is a fixed point.
94
+
89
95
$$|\text{Classes}| \cdot |G| = \sum_{f} J(f)$$
90
96
91
97
To prove this formula we will compose a table with columns labeled with all functions $f_i$ and rows labeled with all permutations $\pi_j$.
@@ -107,6 +113,7 @@ On the other hand, all columns within the same equivalence class are the same as
107
113
Therefore within each column of a given equivalence class any element $g$ occurs exactly $J(g)$ times.
108
114
109
115
Thus if we arbitrarily take one column from each equivalence class, and sum the number of elements in them, we obtain on one hand $|\text{Classes}| \cdot |G|$ (simply by multiplying the number of columns by the number of rows), and on the other hand the sum of the quantities $J(f)$ for all $f$ (this follows from all the previous arguments):
116
+
110
117
$$|\text{Classes}| \cdot |G| = \sum_{f} J(f)$$
111
118
112
119
## Pólya enumeration theorem
@@ -120,7 +127,9 @@ The general formula of the theorem will not be discussed.
120
127
121
128
We denote by $C(\pi)$ the number of cycles in the permutation $\pi$.
122
129
Then the following formula (a **special case of the Pólya enumeration theorem**) holds:
$k$ is the number of values that each representation element can take, in the case of the coloring of a binary tree this would be $k = 2$.
125
134
126
135
### Evidence
@@ -134,6 +143,7 @@ During the application of $\pi$, the elements in $f$ move via the cycles in the
134
143
Since the result should obtain $f \equiv f \pi$, the elements touched by one cycle must all be equal.
135
144
At the same time different cycles are independent.
136
145
Thus for each permutation cycle $\pi$ we can choose one value (among $k$ possible) and thus we get the number of fixed points:
146
+
137
147
$$I(\pi) = k^{C(\pi)}$$
138
148
139
149
## Application: Coloring necklaces
@@ -143,34 +153,39 @@ The task is to count the number of different necklaces from $n$ beads, each of w
143
153
When comparing two necklaces, they can be rotated, but not reversed (i.e. a cyclic shift is permitted).
144
154
145
155
In this problem we can immediately find the group of invariant permutations:
156
+
146
157
$$\begin{align}
147
-
\pi_0 &= 1 2 3 \dots n\\\\
148
-
\pi_1 &= 2 3 \dots n 1\\\\
149
-
\pi_2 &= 3 \dots n 12\\\\
150
-
&\dots\\\\
158
+
\pi_0 &= 1 2 3 \dots n\\
159
+
\pi_1 &= 2 3 \dots n 1\\
160
+
\pi_2 &= 3 \dots n 12\\
161
+
&\dots\\
151
162
\pi_{n-1} &= n 1 2 3\dots\end{align}$$
152
163
153
164
Let us find an explicit formula for calculating $C(\pi_i)$.
154
165
First we note, that the permutation $\pi_i$ has at the $j$-th position the value $i + j$ (taken modulo $n$).
155
166
If we check the cycle structure for $\pi_i$.
156
167
We see that $1$ goes to $1 + i$, $1 + i$ goes to $1 + 2i$, which goes to $1 + 3i$, etc., until we come to a number of the form $1 + k n$.
157
168
Similar statements can be mode for the remaining elements.
158
-
Hence we see that all cycles have the same length, namely $\frac{\\text{lcm}(i, n)}{i} = \frac{n}{\gcd(i, n)}$.
169
+
Hence we see that all cycles have the same length, namely $\frac{\text{lcm}(i, n)}{i} = \frac{n}{\gcd(i, n)}$.
159
170
Thus the number of cycles in $\pi_i$ will be equal to $\gcd(i, n)$.
160
171
161
172
Substituting these values into the Pólya enumeration theorem, we obtain the solution:
173
+
162
174
$$\frac{1}{n} \sum_{i=1}^n k^{\gcd(i, n)}$$
163
175
164
176
You can leave this formula in this form, or you can simplify it even more.
165
177
Let transfer the sum so that it iterates over all divisors of $n$.
166
178
In the original sum there will be many equivalent terms: if $i$ is not a divisor of $n$, then such a divisor can be found after computing $\gcd(i, n)$.
167
179
Therefore for each divisor $d ~|~ n$ its term $k^{\gcd(d, n)} = k^d$ will appear in the sum multiple times, i.e. the answer to the problem can be rewritten as
180
+
168
181
$$\frac{1}{n} \sum_{d ~|~ n} C_d k^d,$$
182
+
169
183
where $C_d$ is the number of such numbers $i$ with $\gcd(i, n) = d$.
170
184
We can find an explicit expression for this value.
171
185
Any such number $i$ has the form $i = d j$ with $\gcd(j, n / d) = 1$ (otherwise $\gcd(i, n) > d$).
172
186
So we can count the number of $j$ with this behavior.
173
187
[Euler's phi function](../algebra/phi-function.md) gives us the result $C_d = \phi(n / d)$, and therefore we get the answer:
Copy file name to clipboardExpand all lines: src/combinatorics/catalan-numbers.md
+5-2Lines changed: 5 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,8 +33,10 @@ The Catalan number $C_n$ is the solution for
33
33
There are two formulas for the Catalan numbers: **Recursive and Analytical**. Since, we believe that all the mentioned above problems are equivalent (have the same solution), for the proof of the formulas below we will choose the task which it is easiest to do.
Copy file name to clipboardExpand all lines: src/combinatorics/counting_labeled_graphs.md
+6-1Lines changed: 6 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,7 @@ For each edge $(i, j)$ we can assume that $i < j$ (because the graph is undirect
16
16
Therefore the set of all edges has the cardinality $\binom{n}{2}$, i.e. $\frac{n(n-1)}{2}$.
17
17
18
18
Since any labeled graph is uniquely determined by its edges, the number of labeled graphs with $n$ vertices is equal to:
19
+
19
20
$$G_n = 2^{\frac{n(n-1)}{2}}$$
20
21
21
22
## Connected labeled graphs
@@ -32,11 +33,14 @@ Obviously we have $n$ possibilities to root a graph with $n$ labeled vertices, t
32
33
The root vertex will appear in a connected component of size $1, \dots n-1$.
33
34
There are $k \binom{n}{k} C_k G_{n-k}$ graphs such that the root vertex is in a connected component with $k$ vertices (there are $\binom{n}{k}$ ways to choose $k$ vertices for the component, these are connected in one of $C_k$ ways, the root vertex can be any of the $k$ vertices, and the remainder $n-k$ vertices can be connected/disconnected in any way, which gives a factor of $G_{n-k}$).
34
35
Therefore the number of disconnected graphs with $n$ vertices is:
36
+
35
37
$$\frac{1}{n} \sum_{k=1}^{n-1} k \binom{n}{k} C_k G_{n-k}$$
38
+
36
39
And finally the number of connected graphs is:
40
+
37
41
$$C_n = G_n - \frac{1}{n} \sum_{k=1}^{n-1} k \binom{n}{k} C_k G_{n-k}$$
38
42
39
-
## Labeled graphs with $k$ connected components
43
+
## Labeled graphs with $k$ connected components {data-toc-label="Labeled graphs with k connected components"}
40
44
41
45
Based on the formula from the previous section, we will learn how to count the number of labeled graphs with $n$ vertices and $k$ connected components.
42
46
@@ -48,4 +52,5 @@ We use a common approach, we take the last vertex (index $n$).
48
52
This vertex belongs to some component.
49
53
If the size of this component be $s$, then there are $\binom{n-1}{s-1}$ ways to choose such a set of vertices, and $C_s$ ways to connect them.After removing this component from the graph we have $n-s$ remaining vertices with $k-1$ connected components.
50
54
Therefore we obtain the following recurrence relation:
0 commit comments