Skip to content

Commit 7fb42d2

Browse files
1 parent efaa75f commit 7fb42d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+409
-270
lines changed

main/algebra/euclid-algorithm.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6561,6 +6561,12 @@
65616561

65626562

65636563

6564+
6565+
6566+
6567+
6568+
6569+
65646570

65656571

65666572

main/algebra/factorization.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6919,7 +6919,8 @@ <h2 id="pollards-rho-algorithm">Pollard's rho algorithm<a class="headerlink" hre
69196919
If <span class="arithmatex">$p$</span> is smaller than <span class="arithmatex">$\sqrt{n}$</span>, the repetition will likely start in <span class="arithmatex">$O(\sqrt[4]{n})$</span>.</p>
69206920
<p>Here is a visualization of such a sequence <span class="arithmatex">$\{x_i \bmod p\}$</span> with <span class="arithmatex">$n = 2206637$</span>, <span class="arithmatex">$p = 317$</span>, <span class="arithmatex">$x_0 = 2$</span> and <span class="arithmatex">$f(x) = x^2 + 1$</span>.
69216921
From the form of the sequence you can see very clearly why the algorithm is called Pollard's <span class="arithmatex">$\rho$</span> algorithm.</p>
6922-
<p><center><img alt="Pollard's rho visualization" src="pollard_rho.png" /></center></p>
6922+
<center>![Pollard's rho visualization](pollard_rho.png)</center>
6923+
69236924
<p>Yet, there is still an open question.
69246925
How can we exploit the properties of the sequence <span class="arithmatex">$\{x_i \bmod p\}$</span> to our advantage without even knowing the number <span class="arithmatex">$p$</span> itself?</p>
69256926
<p>It's actually quite easy.

main/algebra/fft.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6660,6 +6660,12 @@
66606660

66616661

66626662

6663+
6664+
6665+
6666+
6667+
6668+
66636669

66646670

66656671

main/algebra/linear-diophantine-equation.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6570,6 +6570,12 @@
65706570

65716571

65726572

6573+
6574+
6575+
6576+
6577+
6578+
65736579

65746580

65756581

main/algebra/sieve-of-eratosthenes.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6816,7 +6816,8 @@ <h1 id="sieve-of-eratosthenes">Sieve of Eratosthenes<a class="headerlink" href="
68166816
The next unmarked number is 5, which is the next prime number, and we mark all proper multiples of it.
68176817
And we continue this procedure until we have processed all numbers in the row.</p>
68186818
<p>In the following image you can see a visualization of the algorithm for computing all prime numbers in the range <span class="arithmatex">$[1; 16]$</span>. It can be seen, that quite often we mark numbers as composite multiple times.</p>
6819-
<p><center><img alt="Sieve of Eratosthenes" src="sieve_eratosthenes.png" /></center></p>
6819+
<center>![Sieve of Eratosthenes](sieve_eratosthenes.png)</center>
6820+
68206821
<p>The idea behind is this:
68216822
A number is prime, if none of the smaller prime numbers divides it.
68226823
Since we iterate over the prime numbers in order, we already marked all numbers, which are divisible by at least one of the prime numbers, as divisible.

main/combinatorics/binomial-coefficients.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6648,6 +6648,12 @@
66486648

66496649

66506650

6651+
6652+
6653+
6654+
6655+
6656+
66516657

66526658

66536659

main/combinatorics/inclusion-exclusion.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6684,6 +6684,12 @@
66846684

66856685

66866686

6687+
6688+
6689+
6690+
6691+
6692+
66876693

66886694

66896695

main/data_structures/fenwick.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7029,7 +7029,8 @@ <h3 id="definition-of-gi">Definition of <span class="arithmatex">$g(i)$</span><a
70297029
<p>where <span class="arithmatex">$|$</span> is the bitwise OR operator.</p>
70307030
<p>The following image shows a possible interpretation of the Fenwick tree as tree.
70317031
The nodes of the tree show the ranges they cover.</p>
7032-
<p><center><img alt="Binary Indexed Tree" src="binary_indexed_tree.png" /></center></p>
7032+
<center>![Binary Indexed Tree](binary_indexed_tree.png)</center>
7033+
70337034
<h2 id="implementation">Implementation<a class="headerlink" href="#implementation" title="Permanent link">&para;</a></h2>
70347035
<h3 id="finding-sum-in-one-dimensional-array">Finding sum in one-dimensional array<a class="headerlink" href="#finding-sum-in-one-dimensional-array" title="Permanent link">&para;</a></h3>
70357036
<p>Here we present an implementation of the Fenwick tree for sum queries and single updates.</p>

main/data_structures/treap.html

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6799,9 +6799,10 @@ <h1 id="treap-cartesian-tree">Treap (Cartesian tree)<a class="headerlink" href="
67996799
<p>More specifically, treap is a data structure that stores pairs <span class="arithmatex">$(X, Y)$</span> in a binary tree in such a way that it is a binary search tree by <span class="arithmatex">$X$</span> and a binary heap by <span class="arithmatex">$Y$</span>.
68006800
If some node of the tree contains values <span class="arithmatex">$(X_0, Y_0)$</span>, all nodes in the left subtree have <span class="arithmatex">$X \leq X_0$</span>, all nodes in the right subtree have <span class="arithmatex">$X_0 \leq X$</span>, and all nodes in both left and right subtrees have <span class="arithmatex">$Y \leq Y_0$</span>.</p>
68016801
<p>A treap is also often referred to as a "cartesian tree", as it is easy to embed it in a Cartesian plane:</p>
6802-
<p><center>
6802+
<center>
68036803
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e4/Treap.svg" width="350px"/>
6804-
</center></p>
6804+
</center>
6805+
68056806
<p>Treaps have been proposed by Raimund Siedel and Cecilia Aragon in 1989.</p>
68066807
<h2 id="advantages-of-such-data-organisation">Advantages of such data organisation<a class="headerlink" href="#advantages-of-such-data-organisation" title="Permanent link">&para;</a></h2>
68076808
<p>In such implementation, <span class="arithmatex">$X$</span> values are the keys (and at same time the values stored in the treap), and <span class="arithmatex">$Y$</span> values are called <strong>priorities</strong>. Without priorities, the treap would be a regular binary search tree by <span class="arithmatex">$X$</span>, and one set of <span class="arithmatex">$X$</span> values could correspond to a lot of different trees, some of them degenerate (for example, in the form of a linked list), and therefore extremely slow (the main operations would have <span class="arithmatex">$O(N)$</span> complexity).</p>
@@ -6827,9 +6828,10 @@ <h2 id="implementation-description">Implementation Description<a class="headerli
68276828
<p>In terms of implementation, each node contains <span class="arithmatex">$X$</span>, <span class="arithmatex">$Y$</span> and pointers to the left (<span class="arithmatex">$L$</span>) and right (<span class="arithmatex">$R$</span>) children.</p>
68286829
<p>We will implement all the required operations using just two auxiliary operations: Split and Merge.</p>
68296830
<h3 id="split">Split<a class="headerlink" href="#split" title="Permanent link">&para;</a></h3>
6830-
<p><center>
6831+
<center>
68316832
<img src="https://upload.wikimedia.org/wikipedia/commons/6/69/Treap_split.svg" width="450px"/>
6832-
</center></p>
6833+
</center>
6834+
68336835
<p><strong>Split (<span class="arithmatex">$T$</span>, <span class="arithmatex">$X$</span>)</strong> separates tree <span class="arithmatex">$T$</span> in 2 subtrees <span class="arithmatex">$L$</span> and <span class="arithmatex">$R$</span> trees (which are the return values of split) so that <span class="arithmatex">$L$</span> contains all elements with key <span class="arithmatex">$X_L \le X$</span>, and <span class="arithmatex">$R$</span> contains all elements with key <span class="arithmatex">$X_R &gt; X$</span>. This operation has <span class="arithmatex">$O (\log N)$</span> complexity and is implemented using a clean recursion:</p>
68346836
<ol>
68356837
<li>If the value of the root node (R) is <span class="arithmatex">$\le X$</span>, then <code>L</code> would at least consist of <code>R-&gt;L</code> and <code>R</code>. We then call split on <code>R-&gt;R</code>, and note its split result as <code>L'</code> and <code>R'</code>. Finally, <code>L</code> would also contain <code>L'</code>, whereas <code>R = R'</code>.</li>
@@ -6842,20 +6844,23 @@ <h3 id="split">Split<a class="headerlink" href="#split" title="Permanent link">&
68426844
<li>create the final result by reusing the recursive split call.</li>
68436845
</ol>
68446846
<h3 id="merge">Merge<a class="headerlink" href="#merge" title="Permanent link">&para;</a></h3>
6845-
<p><center>
6847+
<center>
68466848
<img src="https://upload.wikimedia.org/wikipedia/commons/a/a8/Treap_merge.svg" width="500px"/>
6847-
</center></p>
6849+
</center>
6850+
68486851
<p><strong>Merge (<span class="arithmatex">$T_1$</span>, <span class="arithmatex">$T_2$</span>)</strong> combines two subtrees <span class="arithmatex">$T_1$</span> and <span class="arithmatex">$T_2$</span> and returns the new tree. This operation also has <span class="arithmatex">$O (\log N)$</span> complexity. It works under the assumption that <span class="arithmatex">$T_1$</span> and <span class="arithmatex">$T_2$</span> are ordered (all keys <span class="arithmatex">$X$</span> in <span class="arithmatex">$T_1$</span> are smaller than keys in <span class="arithmatex">$T_2$</span>). Thus, we need to combine these trees without violating the order of priorities <span class="arithmatex">$Y$</span>. To do this, we choose as the root the tree which has higher priority <span class="arithmatex">$Y$</span> in the root node, and recursively call Merge for the other tree and the corresponding subtree of the selected root node.</p>
68496852
<h3 id="insert">Insert<a class="headerlink" href="#insert" title="Permanent link">&para;</a></h3>
6850-
<p><center>
6853+
<center>
68516854
<img src="https://upload.wikimedia.org/wikipedia/commons/3/35/Treap_insert.svg" width="500px"/>
6852-
</center></p>
6855+
</center>
6856+
68536857
<p>Now implementation of <strong>Insert (<span class="arithmatex">$X$</span>, <span class="arithmatex">$Y$</span>)</strong> becomes obvious. First we descend in the tree (as in a regular binary search tree by X), and stop at the first node in which the priority value is less than <span class="arithmatex">$Y$</span>. We have found the place where we will insert the new element. Next, we call <strong>Split (T, X)</strong> on the subtree starting at the found node, and use returned subtrees <span class="arithmatex">$L$</span> and <span class="arithmatex">$R$</span> as left and right children of the new node.</p>
68546858
<p>Alternatively, insert can be done by splitting the initial treap on <span class="arithmatex">$X$</span> and doing <span class="arithmatex">$2$</span> merges with the new node (see the picture).</p>
68556859
<h3 id="erase">Erase<a class="headerlink" href="#erase" title="Permanent link">&para;</a></h3>
6856-
<p><center>
6860+
<center>
68576861
<img src="https://upload.wikimedia.org/wikipedia/commons/6/62/Treap_erase.svg" width="500px"/>
6858-
</center></p>
6862+
</center>
6863+
68596864
<p>Implementation of <strong>Erase (<span class="arithmatex">$X$</span>)</strong> is also clear. First we descend in the tree (as in a regular binary search tree by <span class="arithmatex">$X$</span>), looking for the element we want to delete. Once the node is found, we call <strong>Merge</strong> on it children and put the return value of the operation in the place of the element we're deleting.</p>
68606865
<p>Alternatively, we can factor out the subtree holding <span class="arithmatex">$X$</span> with <span class="arithmatex">$2$</span> split operations and merge the remaining treaps (see the picture).</p>
68616866
<h3 id="build">Build<a class="headerlink" href="#build" title="Permanent link">&para;</a></h3>

0 commit comments

Comments
 (0)