ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::RemUnionFind Class Reference

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

Detailed description

the union of disjoint sets of the elements \( 0 , \ldots , n - 1 \)

Constructor and destructor documentation

◆ RemUnionFind()

ADS::RemUnionFind::RemUnionFind ( std::size_t n = 0)
explicit

the \( n \) sets \( \{ 0 \} , \ldots , \{ n - 1 \} \)

Precondition
\( 0 \le n < 2^{32} \)

Member function documentation

◆ count()

std::size_t ADS::RemUnionFind::count ( void ) const
nodiscard

the number of the sets

◆ find()

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}} \]

Precondition
\( x < n \)

◆ merge()

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}} \]

Precondition
\( x < n \) and \( y < n \)

◆ n()

std::size_t ADS::RemUnionFind::n ( void ) const
nodiscard

the number of the elements

◆ parent()

std::uint32_t ADS::RemUnionFind::parent ( std::uint32_t x) const
nodiscard

the parent of \( x \), \( x \) itself if a root

◆ same()

bool ADS::RemUnionFind::same ( std::uint32_t x,
std::uint32_t y )

whether \( x \) and \( y \) are in the same set

Precondition
\( x < n \) and \( y < n \)

The documentation for this class was generated from the following file: