ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::DisjointSets< L, C, Policy > Class Template Reference

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

Detailed description

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
class ADS::DisjointSets< L, C, Policy >

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

Member typedef documentation

◆ Linked

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
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

◆ modifications

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
using ADS::DisjointSets< L, C, Policy >::modifications = mod::list< Linked , mod::Rebuilt >

the events: a root linked under another, the forest made anew

◆ Subject

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
using ADS::DisjointSets< L, C, Policy >::Subject
Initial value:
typename Policy::template bind< modifications ,
mod::list< Inserted, Erased, Assigned, mod::Cleared, mod::Rebuilt > modifications
the events: a key inserted or erased with its value, its value changed, the map emptied or rebuilt
Definition DenseHashMap.h:188
DisjointSets(int n=0)
the components

the subject

Constructor and destructor documentation

◆ DisjointSets() [1/2]

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
ADS::DisjointSets< L, C, Policy >::DisjointSets ( int n = 0)
explicit

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

Precondition
\( n \ge 0 \)

◆ DisjointSets() [2/2]

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
template<class... O>
requires ( sizeof...( O ) > 0 )
ADS::DisjointSets< L, C, Policy >::DisjointSets ( int n,
O &... o )

the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \), the static observers o...

Precondition
\( n \ge 0 \)

Member function documentation

◆ attach()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
template<class O>
requires ( Subject::dynamic )
int ADS::DisjointSets< L, C, Policy >::attach ( O & o)
inline

attaches the observer \( o \) and gives its number

Precondition
no batch is open

◆ count()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
int ADS::DisjointSets< L, C, Policy >::count ( void ) const
inlinenodiscard

the number of components

◆ detach()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
void ADS::DisjointSets< L, C, Policy >::detach ( int id)
inline

detaches the observer number \( \mathit{id} \)

Precondition
it is attached, no batch is open

◆ find()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
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().

Precondition
\( x \in [ 0 , n ) \)

◆ merge()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
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.

Precondition
\( x , y \in [ 0 , n ) \)

◆ n()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
int ADS::DisjointSets< L, C, Policy >::n ( void ) const
inlinenodiscard

the number \( n \) of elements

◆ reset()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
void ADS::DisjointSets< L, C, Policy >::reset ( int n)

back to the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \)

Precondition
\( n \ge 0 \)

◆ same()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
bool ADS::DisjointSets< L, C, Policy >::same ( int x,
int y )

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

Precondition
\( x , y \in [ 0 , n ) \)

◆ size()

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
int ADS::DisjointSets< L, C, Policy >::size ( int x)

the number of elements of the component of \( x \)

Precondition
\( x \in [ 0 , n ) \), the union by size

◆ subject() [1/2]

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
Subject & ADS::DisjointSets< L, C, Policy >::subject ( void )
inlinenodiscard

the subject, for ADS::batch() and ADS::quiet()

◆ subject() [2/2]

template<Union L = Union::SIZE, Compression C = Compression::HALVING, class Policy = NoObservers>
const Subject & ADS::DisjointSets< L, C, Policy >::subject ( void ) const
inlinenodiscard

the subject


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