Skip to content

Commit 5023769

Browse files
gabrielsimoestcNickolas
authored andcommitted
Fix binomial coefficients notation per issue #83 (#113)
1 parent aab14a1 commit 5023769

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

src/algebra/all-submasks.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ while (s > 0) {
1818

1919
or, using a more compact for statement:
2020

21-
```cpp
21+
```cpp
2222
for (int s=m; s; s=(s-1)&m)
2323
... you can use s ...
2424
```
@@ -49,14 +49,14 @@ for (int m=0; m<(1<<n); ++m)
4949
for (int s=m; s; s=(s-1)&m)
5050
... s and m ...
5151
```
52-
52+
5353
Let's prove that the inner loop will execute a total of $O(3^n)$ iterations.
5454

5555
**First proof**: Consider the i-th bit. There are exactly three options for it: it is not included in the mask $m$ (and therefore not included in submask $s$); it is included in $m$, but not included in $s$, or it's included in both $m$ and $s$. As there are a total of $n$ bits, there will be $3^n$ different combinations.
5656

57-
**Second proof**: Note that if mask $m$ has $k$ enabled bits, then it will have $2^k$ submasks. As we have a total of $C_n^k$ masks with $k$ enabled bits (see "binomial coefficients"), then the total number of combinations for all masks will be:
57+
**Second proof**: Note that if mask $m$ has $k$ enabled bits, then it will have $2^k$ submasks. As we have a total of $\binom{n}{k}$ masks with $k$ enabled bits (see "binomial coefficients"), then the total number of combinations for all masks will be:
5858

59-
$$\sum_{k=0}^n C_n^k 2^k$$
59+
$$\sum_{k=0}^n \binom{n}{k} \cdot 2^k$$
6060

6161
To calculate this number, note that the sum above is equal to the expansion of $(1+2)^n$ using the binomial theorem. Therefore, we have $3^n$ combinations, as we wanted to prove.
6262

src/combinatorics/catalan-numbers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ void init() {
6262
```
6363

6464
###Analytical formula
65-
$$C_n = \frac{1}{n + 1} C_{2n}^{n}$$
65+
$$C_n = \frac{1}{n + 1} {\binom{2n}{n}$$
6666

67-
(here $C_{n}^{k}$ denotes the usual binomial coefficient, i.e. number of ways to select $k$ objects from set of $n$ objects).
67+
(here $\binom{n}{k}$ denotes the usual binomial coefficient, i.e. number of ways to select $k$ objects from set of $n$ objects).
6868

69-
The above formula can be easily concluded from the problem of the monotonic paths in square grid. The total number of monotonic paths in the lattice size of $n \times n$ is given by $C_{2n}^{n}$.
69+
The above formula can be easily concluded from the problem of the monotonic paths in square grid. The total number of monotonic paths in the lattice size of $n \times n$ is given by $\binom{2n}{n}$.
7070

7171
Now we count the number of monotonic paths which cross the main diagonal. Consider such paths crossing the main diagonal and find the first edge in it which is above the diagonal. Reflect the path about the diagonal all the way, going after this edge. The result is always a monotonic path in the grid $(n - 1) \times (n + 1)$. On the other hand, any monotonic path in the lattice $(n - 1) \times (n + 1)$ must intersect the diagonal. Hence, we enumerated all monotonic paths crossing the main diagonal in the lattice $n \times n$.
7272

73-
The number of monotonic paths in the lattice $(n - 1) \times (n + 1)$ are $C_{2n}^{n-1}$ . Let us call such paths as "bad" paths. As a result, to obtain the number of monotonic paths which do not cross the main diagonal, we subtract the above "bad" paths, obtaining the formula:
73+
The number of monotonic paths in the lattice $(n - 1) \times (n + 1)$ are $\binom{2n}{n-1}$ . Let us call such paths as "bad" paths. As a result, to obtain the number of monotonic paths which do not cross the main diagonal, we subtract the above "bad" paths, obtaining the formula:
7474

75-
$$C_n = C_{2n}^{n} - C_{2n}^{n-1} = \frac{1}{n + 1} C_{2n}^{n} , {n} \geq 0$$
75+
$$C_n = \binom{2n}{n} - \binom{2n}{n-1} = \frac{1}{n + 1} \binom{2n}{n} , {n} \geq 0$$
7676

7777
## Practice Problems
7878
- [Codechef - PANSTACK](https://www.codechef.com/APRIL12/problems/PANSTACK/)

src/combinatorics/inclusion-exclusion.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ We want to prove that any element contained in at least one of the sets $A_i$ wi
5757
Consider an element $x$ occurring in $k \geq 1$ sets $A_i$. We will show it is counted only once in the formula. Note that:
5858

5959
* in terms which $|J| = 1$, the item $x$ will be counted **$+\ k$** times;
60-
* in terms which $|J| = 2$, the item $x$ will be counted **$-\ C_k^2$** times - because it will be counted in those terms that include two of the $k$ sets containing $x$;
61-
* in terms which $|J| = 3$, the item $x$ will be counted **$+\ C_k^3$** times;
60+
* in terms which $|J| = 2$, the item $x$ will be counted **$-\ \binom{k}{2}$** times - because it will be counted in those terms that include two of the $k$ sets containing $x$;
61+
* in terms which $|J| = 3$, the item $x$ will be counted **$+\ \binom{k}{3}$** times;
6262
* $\cdots$
63-
* in terms which $|J| = k$, the item $x$ will be counted **$(-1)^{k-1}\cdot C_k^k$** times;
63+
* in terms which $|J| = k$, the item $x$ will be counted **$(-1)^{k-1}\cdot \binom{k}{k}$** times;
6464
* in terms which $|J| \gt k$, the item $x$ will be counted **zero** times;
6565

6666
This leads us to the following sum of [binomial coefficients](./combinatorics/binomial-coefficients.html):
6767

68-
$$ T = C_k^{1} - C_k^{2} + C_k^{3} - \cdots + (-1)^{i-1}\cdot C_k^{i} + \cdots + (-1)^{k-1}\cdot C_k^k $$
68+
$$ T = \binom{k}{1} - \binom{k}{2} + \binom{k}{3} - \cdots + (-1)^{i-1}\cdot \binom{k}{i} + \cdots + (-1)^{k-1}\cdot \binom{k}{k}$$
6969

7070
This expression is very similar to the binomial expansion of $(1 - x)^k$:
7171

72-
$$ (1 - x)^k = C_k^0 - C_k^1\cdot x + C_k^2\cdot x^2 - C_k^3\cdot x^3 + \cdots + (-1)^k\cdot C_k^k\cdot x^k $$
72+
$$ (1 - x)^k = \binom{k}{0} - \binom{k}{1} \cdot x + \binom{k}{2} \cdot x^2 - \binom{k}{3} \cdot x^3 + \cdots + (-1)^k\cdot \binom{k}{k} \cdot x^k $$
7373

74-
When $x = 1$, $(1 - x)^k$ looks a lot like $T$. However, the expression has an additional $C_k^0 = 1$, and it is multiplied by $-1$. That leads us to $(1 - 1)^k = 1 - T$. Therefore $T = 1 - (1 - 1)^k = 1$, what was required to prove. The element is counted only once.
74+
When $x = 1$, $(1 - x)^k$ looks a lot like $T$. However, the expression has an additional $\binom{k}{0} = 1$, and it is multiplied by $-1$. That leads us to $(1 - 1)^k = 1 - T$. Therefore $T = 1 - (1 - 1)^k = 1$, what was required to prove. The element is counted only once.
7575

7676
## Usage when solving problems
7777

@@ -125,23 +125,23 @@ Task: count the number of solutions to the equation.
125125

126126
Forget the restriction on $x_i$ for a moment and just count the number of nonnegative solutions to this equation. This is easily done using [binomial coefficients](./combinatorics/binomial-coefficients.html): we want to break a sequence of $20$ units into $6$ groups, which is the same as distributing $5$ "walls" over $25$ slots:
127127

128-
$$N_0 = C_{25}^5$$
128+
$$N_0 = \binom{25}{5}$$
129129

130130
We will now calculate the number of "bad" solutions with the inclusion-exclusion principle. The "bad" solutions will be those in which one or more $x_i$ are greater than $9$.
131131

132132
Denote by $A_k (k = 1,2\ldots 6)$ the set of solutions where $x_k \ge 9$, and all other $x_i \ge 0 (i \ne k)$ (they may be $\ge 9$ or not). To calculate the size of $A_k$, note that we have essentially the same combinatorial problem that was solved in the two paragraphs above, but now $9$ of the units are excluded from the slots and definitely belong to the first group. Thus:
133133

134-
$$ | A_k | = C_{16}^5 $$
134+
$$ | A_k | = \binom{16}{5} $$
135135

136136
Similarly, the size of the intersection between sets $A_k$ and $A_p$ is equal to:
137137

138-
$$ \left| A_k \cap A_p \right| = C_7^5 $$
138+
$$ \left| A_k \cap A_p \right| = \binom{7}{5}$$
139139

140140
The size of each intersection of three sets is zero, since $20$ units will not be enough for three or more variables greater than or equal to $9$.
141141

142142
Combining all this into the formula of inclusions-exceptions and given that we solved the inverse problem, we finally get the answer:
143143

144-
$$C_{25}^5 - (C_6^1 \cdot C_{16}^5 - C_6^2 \cdot C_7^5) $$
144+
$$\binom{25}{5} - \left(\binom{6}{1} \cdot \binom{16}{5} - \binom{6}{2} \cdot \binom{7}{5}\right) $$
145145

146146
### The number of relatively primes in a given interval
147147

@@ -226,9 +226,9 @@ $$ ans = \sum_{X ~ : ~ |X| = k} ans(X) $$
226226
227227
However, asymptotics of this solution is $O(3^k \cdot k)$. To improve it, notice that different $ans(X)$ computations very often share $Y$ sets.
228228
229-
We will reverse the formula of inclusion-exclusion and sum in terms of $Y$ sets. Now it becomes clear that the same set $Y$ would be taken into account in the computation of $ans(X)$ of $C_{|Y|}^k$ sets with the same sign $(-1)^{|Y| - k}$.
229+
We will reverse the formula of inclusion-exclusion and sum in terms of $Y$ sets. Now it becomes clear that the same set $Y$ would be taken into account in the computation of $ans(X)$ of $\binom{|Y|}{k}$ sets with the same sign $(-1)^{|Y| - k}$.
230230
231-
$$ ans = \sum_{Y ~ : ~ |Y| \ge k} (-1)^{|Y|-k} \cdot C_{|Y|}^k \cdot f(Y) $$
231+
$$ ans = \sum_{Y ~ : ~ |Y| \ge k} (-1)^{|Y|-k} \cdot \binom{|Y|}{k} \cdot f(Y) $$
232232
233233
Now our solution has asymptotics $O(2^k \cdot k)$.
234234
@@ -237,19 +237,19 @@ We will now solve the second version of the problem: find the number of strings
237237
Of course, we can just use the solution to the first version of the problem and add the answers for sets with size greater than $k$. However, you may notice that in this problem, a set |Y| is considered in the formula for all sets with size $\ge k$ which are contained in $Y$. That said, we can write the part of the expression that is being multiplied by $f(Y)$ as:
238238
239239
240-
$$ (-1)^{|Y|-k} \cdot C_{|Y|}^k + (-1)^{|Y|-k-1} \cdot C_{|Y|}^{k+1} + (-1)^{|Y|-k-2} \cdot C_{|Y|}^{k+2} + \cdots + (-1)^{|Y|-|Y|} \cdot C_{|Y|}^{|Y|} $$
240+
$$ (-1)^{|Y|-k} \cdot \binom{|Y|}{k} + (-1)^{|Y|-k-1} \cdot \binom{|Y|}{k+1} + (-1)^{|Y|-k-2} \cdot \binom{|Y|}{k+2} + \cdots + (-1)^{|Y|-|Y|} \cdot \binom{|Y|}{|Y|} $$
241241
242242
Looking at Graham's (Graham, Knuth, Patashnik. "Concrete mathematics" [1998] ), we see a well-known formula for [binomial coefficients](./combinatorics/binomial-coefficients.html):
243243
244-
$$ \sum_{k=0}^m (-1)^k \cdot C_n^k = (-1)^m \cdot C_{n-1}^m $$
244+
$$ \sum_{k=0}^m (-1)^k \cdot \binom{n}{k} = (-1)^m \cdot \binom{n-1}{m} $$
245245
246246
Applying it here, we find that the entire sum of binomial coefficients is minimized:
247247
248-
$$ (-1)^{|Y|-k} \cdot C_{|Y|-1}^{|Y|-k} $$
248+
$$ (-1)^{|Y|-k} \cdot \binom{|Y|-1}{|Y|-k} $$
249249
250250
Thus, for this task, we also obtained a solution with the asymptotics $O(2^k \cdot k)$:
251251
252-
$$ ans = \sum_{Y ~ : ~ |Y| \ge k} (-1)^{|Y|-k} \cdot C_{|Y|-1}^{|Y|-k} \cdot f(Y) $$
252+
$$ ans = \sum_{Y ~ : ~ |Y| \ge k} (-1)^{|Y|-k} \cdot \binom{|Y|-1}{|Y|-k} \cdot f(Y) $$
253253
254254
### The number of ways of going from a cell to another
255255
@@ -261,7 +261,7 @@ For now, sort the obstacles by their coordinate $x$, and in case of equality —
261261
262262
Also just learn how to solve a problem without obstacles: i.e. learn how to count the number of ways to get from one cell to another. In one axis, we need to go through $x$ cells, and on the other, $y$ cells. From simple combinatorics, we get a formula using [binomial coefficients](./combinatorics/binomial-coefficients.html):
263263
264-
$$C_{x+y}^{x}$$
264+
$$\binom{x+y}{x}$$
265265
266266
Now to count the number of ways to get from one cell to another, avoiding all obstacles, you can use inclusion-exclusion to solve the inverse problem: count the number of ways to walk through the board stepping at a subset of obstacles (and subtract it from the total number of ways).
267267
@@ -365,7 +365,7 @@ The asymptotics of our solution is $O(n \log n)$, as for almost every number up
365365

366366
Prove that the number of permutations of length $n$ without fixed points (i.e. no number $i$ is in position $i$ - also called a derangement) is equal to the following number:
367367

368-
$$n! - C_n^1 \cdot (n-1)! + C_n^2 \cdot (n-2)! - C_n^3 \cdot (n-3)! + \cdots \pm C_n^n \cdot (n-n)! $$
368+
$$n! - \binom{n}{1} \cdot (n-1)! + \binom{n}{2} \cdot (n-2)! - \binom{n}{3} \cdot (n-3)! + \cdots \pm \binom{n}{n} \cdot (n-n)! $$
369369

370370
and approximately equal to:
371371

@@ -386,13 +386,13 @@ $$\begin{eqnarray}
386386

387387
because if we know that the number of fixed points is equal $x$, then we know the position of $x$ elements of the permutation, and all other $(n-x)$ elements can be placed anywhere.
388388

389-
Substituting this into the formula of inclusion-exclusion, and given that the number of ways to choose a subset of size $x$ from the set of $n$ elements is equal to $C_n^x$, we obtain a formula for the number of permutations with at least one fixed point:
389+
Substituting this into the formula of inclusion-exclusion, and given that the number of ways to choose a subset of size $x$ from the set of $n$ elements is equal to $\binom{n}{x}$, we obtain a formula for the number of permutations with at least one fixed point:
390390

391-
$$C_n^1 \cdot (n-1)! - C_n^2 \cdot (n-2)! + C_n^3 \cdot (n-3)! - \cdots \pm C_n^n \cdot (n-n)! $$
391+
$$\binom{n}{1} \cdot (n-1)! - \binom{n}{2} \cdot (n-2)! + \binom{n}{3} \cdot (n-3)! - \cdots \pm \binom{n}{n} \cdot (n-n)! $$
392392

393393
Then the number of permutations without fixed points is equal to:
394394

395-
$$n! - C_n^1 \cdot (n-1)! + C_n^2 \cdot (n-2)! - C_n^3 \cdot (n-3)! + \cdots \pm C_n^n \cdot (n-n)! $$
395+
$$n! - \binom{n}{1} \cdot (n-1)! + \binom{n}{2} \cdot (n-2)! - \binom{n}{3} \cdot (n-3)! + \cdots \pm \binom{n}{n} \cdot (n-n)! $$
396396

397397
Simplifying this expression, we obtain **exact and approximate expressions for the number of permutations without fixed points**:
398398

src/contrib.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ And here is the formula in the separate block:
4444

4545
$$E = mc^{2}$$
4646

47+
###Some conventions
48+
49+
* We have agreed as of issue [#83](https://github.com/e-maxx-eng/e-maxx-eng/issues/83) to express binomial coefficients with `\binom{n}{k}` instead of `C_n^k`. The first one renders as $\binom{n}{k}$ and is a more universal convention. The second would render as $C_n^k$.
50+
4751
###Adding images
4852

4953
Small images could be pushed along with texts to the [/img](https://github.com/e-maxx-eng/e-maxx-eng/tree/master/img) subfolder. Let them be in `PNG` format and less than `200kb`. Then you can refer to them inside the article with the tag:

0 commit comments

Comments
 (0)