|
ADS
Algorithms and Data Structures
|
the union of disjoint sets of \( [ 0 , n ) \), the roots joined by L, the paths shortened by C, the links of the roots told through Policy More...
#include <ADS/mfset/UnionFind.h>
Public types | |
| using | Linked = mod::Changed< mod::Parent , int , int > |
| the link of a root under another root: the parent of the handle from \( -1 \) to the other root | |
| using | modifications = mod::list< Linked , mod::Rebuilt > |
| the events: a root linked under another, the forest made anew | |
| using | Subject |
| the subject | |
Public member functions | |
| DisjointSets (int n=0) | |
| the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \) | |
| template<class... O> requires ( sizeof...( O ) > 0 ) | |
| DisjointSets (int n, O &... o) | |
| the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \), the static observers o... | |
| void | reset (int n) |
| back to the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \) | |
| int | n (void) const |
| the number \( n \) of elements | |
| int | count (void) const |
| the number of components | |
| int | find (int x) |
| the representative of the component of \( x \) | |
| bool | merge (int x, int y) |
| joins the components of \( x \) and \( y \), the smaller (of lower rank) under the larger, under the root of \( y \) if they tie; true if they were distinct | |
| bool | same (int x, int y) |
| whether \( x \) and \( y \) are in the same component | |
| int | size (int x) |
| the number of elements of the component of \( x \) | |
| Subject & | subject (void) |
| the subject, for ADS::batch() and ADS::quiet() | |
| const Subject & | subject (void) const |
| the subject | |
| template<class O> requires ( Subject::dynamic ) | |
| int | attach (O &o) |
| attaches the observer \( o \) and gives its number | |
| void | detach (int id) |
| detaches the observer number \( \mathit{id} \) | |
the union of disjoint sets of \( [ 0 , n ) \), the roots joined by L, the paths shortened by C, the links of the roots told through Policy
| using ADS::DisjointSets< L, C, Policy >::Linked = mod::Changed< mod::Parent , int , int > |
the link of a root under another root: the parent of the handle from \( -1 \) to the other root
| using ADS::DisjointSets< L, C, Policy >::modifications = mod::list< Linked , mod::Rebuilt > |
the events: a root linked under another, the forest made anew
| using ADS::DisjointSets< L, C, Policy >::Subject |
the subject
|
explicit |
the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \)
| ADS::DisjointSets< L, C, Policy >::DisjointSets | ( | int | n, |
| O &... | o ) |
the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \), the static observers o...
|
inline |
attaches the observer \( o \) and gives its number
|
inlinenodiscard |
the number of components
|
inline |
detaches the observer number \( \mathit{id} \)
| int ADS::DisjointSets< L, C, Policy >::find | ( | int | x | ) |
the representative of the component of \( x \)
In pseudocode (the halving of Tarjan and van Leeuwen, beyond the book), \( p[ x ] < 0 \) at a root:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( x ) \cr 2 & \quad \textbf{while}\ p[ x ] \ge 0\ \textbf{do} \cr 3 & \qquad y \gets p[ x ];\ \textbf{if}\ p[ y ] < 0\ \textbf{then return}\ y \cr 4 & \qquad p[ x ] \gets p[ y ];\ x \gets p[ y ]\quad \triangleright \ x \text{ under its grandparent} \cr 5 & \quad \textbf{return}\ x \end{array}} \]
and with FULL the two passes of MFSet::find().
| bool ADS::DisjointSets< L, C, Policy >::merge | ( | int | x, |
| int | y ) |
joins the components of \( x \) and \( y \), the smaller (of lower rank) under the larger, under the root of \( y \) if they tie; true if they were distinct
In pseudocode (section 10.2.3, the union by size), a root \( r \) holding \( p[ r ] = -\mathit{size}[ r ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{merge}( x , y ) \cr 2 & \quad x \gets \mathrm{find}( x );\ y \gets \mathrm{find}( y ); \ \textbf{if}\ x = y\ \textbf{then return false} \cr 3 & \quad \textbf{if}\ p[ y ] > p[ x ]\ \textbf{then}\ x \leftrightarrow y\quad \triangleright\ \text{now } y \text{ is the larger tree} \cr 4 & \quad p[ y ] \gets p[ y ] + p[ x ];\ p[ x ] \gets y; \ \textbf{return true} \end{array}} \]
and with RANK a root holds \( -1 - \mathit{rank}[ r ] \), the rank growing by 1 on a tie.
|
inlinenodiscard |
the number \( n \) of elements
| void ADS::DisjointSets< L, C, Policy >::reset | ( | int | n | ) |
back to the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \)
| bool ADS::DisjointSets< L, C, Policy >::same | ( | int | x, |
| int | y ) |
whether \( x \) and \( y \) are in the same component
| int ADS::DisjointSets< L, C, Policy >::size | ( | int | x | ) |
the number of elements of the component of \( x \)
|
inlinenodiscard |
the subject, for ADS::batch() and ADS::quiet()
|
inlinenodiscard |
the subject