|
ADS
Algorithms and Data Structures
|
the union of disjoint sets of the elements \( 0 , \ldots , n - 1 \) More...
#include <ADS/mfset/RemUnionFind.h>
Public member functions | |
| RemUnionFind (std::size_t n=0) | |
| the \( n \) sets \( \{ 0 \} , \ldots , \{ n - 1 \} \) | |
| std::size_t | n (void) const |
| the number of the elements | |
| std::size_t | count (void) const |
| the number of the sets | |
| std::uint32_t | find (std::uint32_t x) |
| the representative of the set of \( x \), the root of its tree | |
| bool | merge (std::uint32_t x, std::uint32_t y) |
| joins the sets of \( x \) and \( y \); true if they were distinct | |
| bool | same (std::uint32_t x, std::uint32_t y) |
| whether \( x \) and \( y \) are in the same set | |
| std::uint32_t | parent (std::uint32_t x) const |
| the parent of \( x \), \( x \) itself if a root | |
the union of disjoint sets of the elements \( 0 , \ldots , n - 1 \)
|
explicit |
the \( n \) sets \( \{ 0 \} , \ldots , \{ n - 1 \} \)
|
nodiscard |
the number of the sets
| std::uint32_t ADS::RemUnionFind::find | ( | std::uint32_t | x | ) |
the representative of the set of \( x \), the root of its tree
In pseudocode (the halving of Tarjan and van Leeuwen, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( x ) \cr 2 & \quad \textbf{while}\ p[ x ] \ne x\ \textbf{do}\ p[ x ] \gets p[ p[ x ] ];\ x \gets p[ x ] \cr 3 & \quad \textbf{return}\ x \end{array}} \]
| bool ADS::RemUnionFind::merge | ( | std::uint32_t | x, |
| std::uint32_t | y ) |
joins the sets of \( x \) and \( y \); true if they were distinct
In pseudocode (the algorithm of Rem with splicing, as Dijkstra and Patwary, Blair and Manne describe it, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{merge}( x , y ) \cr 2 & \quad a \gets x;\ b \gets y \cr 3 & \quad \textbf{while}\ p[ a ] \ne p[ b ]\ \textbf{do} \cr 4 & \qquad \textbf{if}\ p[ a ] > p[ b ]\ \textbf{then}\ \text{swap } a \text{ and } b \cr 5 & \qquad \textbf{if}\ p[ a ] = a\ \textbf{then}\ p[ a ] \gets p[ b ];\ \textbf{return true}\quad \triangleright\ \text{a root, linked} \cr 6 & \qquad z \gets p[ a ];\ p[ a ] \gets p[ b ];\ a \gets z\quad \triangleright\ \text{the splicing} \cr 7 & \quad \textbf{return false} \end{array}} \]
|
nodiscard |
the number of the elements
|
nodiscard |
the parent of \( x \), \( x \) itself if a root
| bool ADS::RemUnionFind::same | ( | std::uint32_t | x, |
| std::uint32_t | y ) |
whether \( x \) and \( y \) are in the same set