Copyright © 2014 - 2024 by cp-algorithms contributors +edit_uri: edit/main/src/ +copyright: Text is available under the Creative Commons Attribution Share Alike 4.0 International License
Copyright © 2014 - 2025 by cp-algorithms contributors extra_javascript: - javascript/config.js - https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6 @@ -57,12 +56,13 @@ markdown_extensions: permalink: true plugins: + - toggle-sidebar: + toggle_button: all - mkdocs-simple-hooks: hooks: on_env: "hooks:on_env" - search - - tags: - tags_file: tags.md + - tags - literate-nav: nav_file: navigation.md - git-revision-date-localized: @@ -74,6 +74,7 @@ plugins: docs_path: src/ token: !ENV MKDOCS_GIT_COMMITTERS_APIKEY enabled: !ENV [MKDOCS_ENABLE_GIT_COMMITTERS, False] + branch: !ENV [MKDOCS_GIT_COMMITERS_BRANCH, main] - macros - rss diff --git a/scripts/install-mkdocs.sh b/scripts/install-mkdocs.sh index 4202c9a97..9c4e73c3f 100755 --- a/scripts/install-mkdocs.sh +++ b/scripts/install-mkdocs.sh @@ -2,6 +2,7 @@ pip install \ "mkdocs-material>=9.0.2" \ + mkdocs-toggle-sidebar-plugin \ mkdocs-macros-plugin \ mkdocs-literate-nav \ mkdocs-git-authors-plugin \ diff --git a/src/algebra/big-integer.md b/src/algebra/big-integer.md index 7f8dd1816..72d137983 100644 --- a/src/algebra/big-integer.md +++ b/src/algebra/big-integer.md @@ -30,7 +30,7 @@ To improve performance we'll use $10^9$ as the base, so that each "digit" of the const int base = 1000*1000*1000; ``` -Digits will be stored in order from least to most significant. All operations will be implemented so that after each of them the result doesn't have any leading zeros, as long as operands didn't have any leading zeros either. All operations which might result in a number with leading zeros should be followed by code which removes them. Note that in this representation there are two valid notations for number zero: and empty vector, and a vector with a single zero digit. +Digits will be stored in order from least to most significant. All operations will be implemented so that after each of them the result doesn't have any leading zeros, as long as operands didn't have any leading zeros either. All operations which might result in a number with leading zeros should be followed by code which removes them. Note that in this representation there are two valid notations for number zero: an empty vector, and a vector with a single zero digit. ### Output diff --git a/src/algebra/bit-manipulation.md b/src/algebra/bit-manipulation.md index 39eda1b46..f29810323 100644 --- a/src/algebra/bit-manipulation.md +++ b/src/algebra/bit-manipulation.md @@ -207,7 +207,7 @@ We can see that the all the columns except the leftmost have $4$ (i.e. $2^2$) se With the new knowledge in hand we can come up with the following algorithm: - Find the highest power of $2$ that is lesser than or equal to the given number. Let this number be $x$. -- Calculate the number of set bits from $1$ to $2^x - 1$ by using the formua $x \cdot 2^{x-1}$. +- Calculate the number of set bits from $1$ to $2^x - 1$ by using the formula $x \cdot 2^{x-1}$. - Count the no. of set bits in the most significant bit from $2^x$ to $n$ and add it. - Subtract $2^x$ from $n$ and repeat the above steps using the new $n$. diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index 665ca2b07..2de931871 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -15,7 +15,7 @@ $$\gcd(a, b) = \max \{k > 0 : (k \mid a) \text{ and } (k \mid b) \}$$ When one of the numbers is zero, while the other is non-zero, their greatest common divisor, by definition, is the second number. When both numbers are zero, their greatest common divisor is undefined (it can be any arbitrarily large number), but it is convenient to define it as zero as well to preserve the associativity of $\gcd$. Which gives us a simple rule: if one of the numbers is zero, the greatest common divisor is the other number. -The Euclidean algorithm, discussed below, allows to find the greatest common divisor of two numbers $a$ and $b$ in $O(\log \min(a, b))$. +The Euclidean algorithm, discussed below, allows to find the greatest common divisor of two numbers $a$ and $b$ in $O(\log \min(a, b))$. Since the function is **associative**, to find the GCD of **more than two numbers**, we can do $\gcd(a, b, c) = \gcd(a, \gcd(b, c))$ and so forth. The algorithm was first described in Euclid's "Elements" (circa 300 BC), but it is possible that the algorithm has even earlier origins. @@ -70,7 +70,7 @@ Moreover, it is possible to show that the upper bound of this theorem is optimal Given that Fibonacci numbers grow exponentially, we get that the Euclidean algorithm works in $O(\log \min(a, b))$. -Another way to estimate the complexity is to notice that $a \bmod b$ for the case $a \geq b$ is at least $2$ times smaller than $a$, so the larger number is reduced at least in half on each iteration of the algorithm. +Another way to estimate the complexity is to notice that $a \bmod b$ for the case $a \geq b$ is at least $2$ times smaller than $a$, so the larger number is reduced at least in half on each iteration of the algorithm. Applying this reasoning to the case when we compute the GCD of the set of numbers $a_1,\dots,a_n \leq C$, this also allows us to estimate the total runtime as $O(n + \log C)$, rather than $O(n \log C)$, since every non-trivial iteration of the algorithm reduces the current GCD candidate by at least a factor of $2$. ## Least common multiple diff --git a/src/algebra/extended-euclid-algorithm.md b/src/algebra/extended-euclid-algorithm.md index 4e96f3729..3e845e728 100644 --- a/src/algebra/extended-euclid-algorithm.md +++ b/src/algebra/extended-euclid-algorithm.md @@ -95,9 +95,32 @@ int gcd(int a, int b, int& x, int& y) { If you look closely at the variables `a1` and `b1`, you can notice that they take exactly the same values as in the iterative version of the normal [Euclidean algorithm](euclid-algorithm.md). So the algorithm will at least compute the correct GCD. -To see why the algorithm also computes the correct coefficients, you can check that the following invariants will hold at any time (before the while loop, and at the end of each iteration): $x \cdot a + y \cdot b = a_1$ and $x_1 \cdot a + y_1 \cdot b = b_1$. -It's trivial to see, that these two equations are satisfied at the beginning. -And you can check that the update in the loop iteration will still keep those equalities valid. +To see why the algorithm computes the correct coefficients, consider that the following invariants hold at any given time (before the while loop begins and at the end of each iteration): + +$$x \cdot a + y \cdot b = a_1$$ + +$$x_1 \cdot a + y_1 \cdot b = b_1$$ + +Let the values at the end of an iteration be denoted by a prime ($'$), and assume $q = \frac{a_1}{b_1}$. From the [Euclidean algorithm](euclid-algorithm.md), we have: + +$$a_1' = b_1$$ + +$$b_1' = a_1 - q \cdot b_1$$ + +For the first invariant to hold, the following should be true: + +$$x' \cdot a + y' \cdot b = a_1' = b_1$$ + +$$x' \cdot a + y' \cdot b = x_1 \cdot a + y_1 \cdot b$$ + +Similarly for the second invariant, the following should hold: + +$$x_1' \cdot a + y_1' \cdot b = a_1 - q \cdot b_1$$ + +$$x_1' \cdot a + y_1' \cdot b = (x - q \cdot x_1) \cdot a + (y - q \cdot y_1) \cdot b$$ + +By comparing the coefficients of $a$ and $b$, the update equations for each variable can be derived, ensuring that the invariants are maintained throughout the algorithm. + At the end we know that $a_1$ contains the GCD, so $x \cdot a + y \cdot b = g$. Which means that we have found the required coefficients. diff --git a/src/algebra/factorial-modulo.md b/src/algebra/factorial-modulo.md index 73ff0145b..86961ba33 100644 --- a/src/algebra/factorial-modulo.md +++ b/src/algebra/factorial-modulo.md @@ -14,7 +14,7 @@ Otherwise $p!$ and subsequent terms will reduce to zero. But in fractions the factors of $p$ can cancel, and the resulting expression will be non-zero modulo $p$. Thus, formally the task is: You want to calculate $n! \bmod p$, without taking all the multiple factors of $p$ into account that appear in the factorial. -Imaging you write down the prime factorization of $n!$, remove all factors $p$, and compute the product modulo $p$. +Imagine you write down the prime factorization of $n!$, remove all factors $p$, and compute the product modulo $p$. We will denote this *modified* factorial with $n!_{\%p}$. For instance $7!_{\%p} \equiv 1 \cdot 2 \cdot \underbrace{1}_{3} \cdot 4 \cdot 5 \underbrace{2}_{6} \cdot 7 \equiv 2 \bmod 3$. diff --git a/src/algebra/factorization.md b/src/algebra/factorization.md index 58a43b961..14715605f 100644 --- a/src/algebra/factorization.md +++ b/src/algebra/factorization.md @@ -159,11 +159,10 @@ By looking at the squares $a^2$ modulo a fixed small number, it can be observed ## Pollard's $p - 1$ method { data-toc-label="Pollard's method" } -It is very likely that at least one factor of a number is $B$**-powersmooth** for small $B$. -$B$-powersmooth means that every prime power $d^k$ that divides $p-1$ is at most $B$. +It is very likely that a number $n$ has at least one prime factor $p$ such that $p - 1$ is $\mathrm{B}$**-powersmooth** for small $\mathrm{B}$. An integer $m$ is said to be $\mathrm{B}$-powersmooth if every prime power dividing $m$ is at most $\mathrm{B}$. Formally, let $\mathrm{B} \geqslant 1$ and let $m$ be any positive integer. Suppose the prime factorization of $m$ is $m = \prod {q_i}^{e_i}$, where each $q_i$ is a prime and $e_i \geqslant 1$. Then $m$ is $\mathrm{B}$-powersmooth if, for all $i$, ${q_i}^{e_i} \leqslant \mathrm{B}$. E.g. the prime factorization of $4817191$ is $1303 \cdot 3697$. -And the factors are $31$-powersmooth and $16$-powersmooth respectably, because $1303 - 1 = 2 \cdot 3 \cdot 7 \cdot 31$ and $3697 - 1 = 2^4 \cdot 3 \cdot 7 \cdot 11$. -In 1974 John Pollard invented a method to extracts $B$-powersmooth factors from a composite number. +And the values, $1303 - 1$ and $3697 - 1$, are $31$-powersmooth and $16$-powersmooth respectively, because $1303 - 1 = 2 \cdot 3 \cdot 7 \cdot 31$ and $3697 - 1 = 2^4 \cdot 3 \cdot 7 \cdot 11$. +In 1974 John Pollard invented a method to extract factors $p$, s.t. $p-1$ is $\mathrm{B}$-powersmooth, from a composite number. The idea comes from [Fermat's little theorem](phi-function.md#application). Let a factorization of $n$ be $n = p \cdot q$. @@ -180,7 +179,7 @@ This means that $a^M - 1 = p \cdot r$, and because of that also $p ~|~ \gcd(a^M Therefore, if $p - 1$ for a factor $p$ of $n$ divides $M$, we can extract a factor using [Euclid's algorithm](euclid-algorithm.md). -It is clear, that the smallest $M$ that is a multiple of every $B$-powersmooth number is $\text{lcm}(1,~2~,3~,4~,~\dots,~B)$. +It is clear, that the smallest $M$ that is a multiple of every $\mathrm{B}$-powersmooth number is $\text{lcm}(1,~2~,3~,4~,~\dots,~B)$. Or alternatively: $$M = \prod_{\text{prime } q \le B} q^{\lfloor \log_q B \rfloor}$$ @@ -189,11 +188,11 @@ Notice, if $p-1$ divides $M$ for all prime factors $p$ of $n$, then $\gcd(a^M - In this case we don't receive a factor. Therefore, we will try to perform the $\gcd$ multiple times, while we compute $M$. -Some composite numbers don't have $B$-powersmooth factors for small $B$. -For example, the factors of the composite number $100~000~000~000~000~493 = 763~013 \cdot 131~059~365~961$ are $190~753$-powersmooth and $1~092~161~383$-powersmooth. -We will have to choose $B >= 190~753$ to factorize the number. +Some composite numbers don't have factors $p$ s.t. $p-1$ is $\mathrm{B}$-powersmooth for small $\mathrm{B}$. +For example, for the composite number $100~000~000~000~000~493 = 763~013 \cdot 131~059~365~961$, values $p-1$ are $190~753$-powersmooth and $1~092~161~383$-powersmooth correspondingly. +We will have to choose $B \geq 190~753$ to factorize the number. -In the following implementation we start with $B = 10$ and increase $B$ after each each iteration. +In the following implementation we start with $\mathrm{B} = 10$ and increase $\mathrm{B}$ after each each iteration. ```{.cpp file=factorization_p_minus_1} long long pollards_p_minus_1(long long n) { @@ -246,7 +245,9 @@ If $p$ is smaller than $\sqrt{n}$, the repetition will likely start in $O(\sqrt[ Here is a visualization of such a sequence $\{x_i \bmod p\}$ with $n = 2206637$, $p = 317$, $x_0 = 2$ and $f(x) = x^2 + 1$. From the form of the sequence you can see very clearly why the algorithm is called Pollard's $\rho$ algorithm. -






























**Note**: This item uses the term "_walk_" rather than a "_path_" for a reason, as the vertices may potentially repeat in the found walk in order to make its length even. The problem of finding the shortest _path_ of even length is NP-Complete in directed graphs, and [solvable in linear time](https://onlinelibrary.wiley.com/doi/abs/10.1002/net.3230140403) in undirected graphs, but with a much more involved approach. ## Practice Problems diff --git a/src/graph/dinic.md b/src/graph/dinic.md index d1945d893..2947e05b2 100644 --- a/src/graph/dinic.md +++ b/src/graph/dinic.md @@ -106,7 +106,7 @@ struct Dinic { int v = q.front(); q.pop(); for (int id : adj[v]) { - if (edges[id].cap - edges[id].flow < 1) + if (edges[id].cap == edges[id].flow) continue; if (level[edges[id].u] != -1) continue; @@ -125,7 +125,7 @@ struct Dinic { for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) { int id = adj[v][cid]; int u = edges[id].u; - if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1) + if (level[v] + 1 != level[u]) continue; long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow)); if (tr == 0) @@ -154,3 +154,7 @@ struct Dinic { } }; ``` + +## Practice Problems + +* [SPOJ: FASTFLOW](https://www.spoj.com/problems/FASTFLOW/) \ No newline at end of file diff --git a/src/graph/edge_vertex_connectivity.md b/src/graph/edge_vertex_connectivity.md index 085e50470..2a875cb02 100644 --- a/src/graph/edge_vertex_connectivity.md +++ b/src/graph/edge_vertex_connectivity.md @@ -39,7 +39,7 @@ It is clear, that the vertex connectivity of a graph is equal to the minimal siz ### The Whitney inequalities -The **Whitney inequalities** (1932) gives a relation between the edge connectivity $\lambda$, the vertex connectivity $\kappa$ and the smallest degree of the vertices $\delta$: +The **Whitney inequalities** (1932) gives a relation between the edge connectivity $\lambda$, the vertex connectivity $\kappa$, and the minimum degree of any vertex in the graph $\delta$: $$\kappa \le \lambda \le \delta$$ @@ -77,7 +77,7 @@ Especially the algorithm will run pretty fast for random graphs. ### Special algorithm for edge connectivity -The task of finding the edge connectivity if equal to the task of finding the **global minimum cut**. +The task of finding the edge connectivity is equal to the task of finding the **global minimum cut**. Special algorithms have been developed for this task. One of them is the Stoer-Wagner algorithm, which works in $O(V^3)$ or $O(V E)$ time. diff --git a/src/graph/edmonds_karp.md b/src/graph/edmonds_karp.md index fa4ec25bf..bab54772f 100644 --- a/src/graph/edmonds_karp.md +++ b/src/graph/edmonds_karp.md @@ -43,7 +43,9 @@ The source $s$ is origin of all the water, and the water can only drain in the s The following image shows a flow network. The first value of each edge represents the flow, which is initially 0, and the second value represents the capacity. -



















+


*In the image left is the MST and right is the second best MST.* - +




+A visual representation of simulated annealing, searching for the maxima of this function with multiple local maxima. +
+




Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: