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

the union-find of the elements \( [ 0 , n ) \) that threads change at once More...

#include <ADS/concurrent/ConcurrentUnionFind.h>

Public member functions

 ConcurrentUnionFind (std::size_t n)
 the \( n \) singletons \( \{ 0 \} , \ldots , \{ n - 1 \} \)
 ConcurrentUnionFind (const ConcurrentUnionFind &)=delete
 no copy, since the threads share it by its address
ConcurrentUnionFind & operator= (const ConcurrentUnionFind &)=delete
 no copy, since the threads share it by its address
std::size_t size (void) const
 the number \( n \) of the elements
std::int32_t find (std::int32_t x)
 the root of \( x \), the least element of its set when no union is running
std::int32_t parent (std::int32_t x) const
 the parent of \( x \) at a moment of the call (\( x \) if a root)
void compress (std::int32_t x)
 \( x \) pointed to its root, unless a union moved \( x \) first
bool unite (std::int32_t x, std::int32_t y)
 the union of the sets of \( x \) and \( y \); false if they were already one
bool sameSet (std::int32_t x, std::int32_t y)
 whether \( x \) and \( y \) are in the same set

Detailed description

the union-find of the elements \( [ 0 , n ) \) that threads change at once

Constructor and destructor documentation

◆ ConcurrentUnionFind() [1/2]

ADS::ConcurrentUnionFind::ConcurrentUnionFind ( std::size_t n)
inlineexplicit

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

Precondition
\( n < 2^{31} \)

◆ ConcurrentUnionFind() [2/2]

ADS::ConcurrentUnionFind::ConcurrentUnionFind ( const ConcurrentUnionFind & )
delete

no copy, since the threads share it by its address

Member function documentation

◆ compress()

void ADS::ConcurrentUnionFind::compress ( std::int32_t x)
inline

\( x \) pointed to its root, unless a union moved \( x \) first

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{compress}( x ) \cr 2 & \quad u \gets p[ x ] \cr 3 & \quad \textbf{if}\ u \ne x\ \textbf{then} \cr 4 & \qquad r \gets \mathrm{find}( u ) \cr 5 & \qquad \textbf{if}\ r \ne u\ \textbf{then}\ \mathrm{CAS}( p[ x ] , u , r ) \end{array}} \]

Precondition
\( 0 \le x < n \)

◆ find()

std::int32_t ADS::ConcurrentUnionFind::find ( std::int32_t x)
inlinenodiscard

the root of \( x \), the least element of its set when no union is running

In pseudocode (the splitting of Jayanti and Tarjan, beyond the book), \( p[ x ] \) the parent of \( x \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( x ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad u \gets p[ x ];\ g \gets p[ u ]\quad \triangleright \ \text{acquire} \cr 4 & \qquad \textbf{if}\ u = g\ \textbf{then return}\ u \cr 5 & \qquad \mathrm{CAS}( p[ x ] , u , g )\quad \triangleright \ \text{release, its failure harmless} \cr 6 & \qquad x \gets u \end{array}} \]

Precondition
\( 0 \le x < n \)

◆ operator=()

ConcurrentUnionFind & ADS::ConcurrentUnionFind::operator= ( const ConcurrentUnionFind & )
delete

no copy, since the threads share it by its address

◆ parent()

std::int32_t ADS::ConcurrentUnionFind::parent ( std::int32_t x) const
inlinenodiscard

the parent of \( x \) at a moment of the call (\( x \) if a root)

Precondition
\( 0 \le x < n \)

◆ sameSet()

bool ADS::ConcurrentUnionFind::sameSet ( std::int32_t x,
std::int32_t y )
inlinenodiscard

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

In pseudocode (Jayanti and Tarjan, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sameSet}( x , y ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad x \gets \mathrm{find}( x );\ y \gets \mathrm{find}( y ) \cr 4 & \qquad \textbf{if}\ x = y\ \textbf{then return true} \cr 5 & \qquad \textbf{if}\ p[ x ] = x\ \textbf{then return false}\quad \triangleright\ x \text{ still a root after } y \text{ was found} \end{array}} \]

Precondition
\( 0 \le x , y < n \)

◆ size()

std::size_t ADS::ConcurrentUnionFind::size ( void ) const
inlinenodiscard

the number \( n \) of the elements

◆ unite()

bool ADS::ConcurrentUnionFind::unite ( std::int32_t x,
std::int32_t y )
inline

the union of the sets of \( x \) and \( y \); false if they were already one

In pseudocode (the linking by index of Jayanti and Tarjan, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{unite}( x , y ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad x \gets \mathrm{find}( x );\ y \gets \mathrm{find}( y ) \cr 4 & \qquad \textbf{if}\ x = y\ \textbf{then return false} \cr 5 & \qquad \textbf{if}\ x < y\ \textbf{then}\ \text{swap } x \text{ and } y \cr 6 & \qquad \textbf{if}\ \mathrm{CAS}( p[ x ] , x , y ) \ \textbf{then return true}\quad \triangleright\ \text{the larger} \text{ root hooked} \end{array}} \]

Precondition
\( 0 \le x , y < n \)

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