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

the union of disjoint sets on a forest More...

#include <ADS/mfset/MFSet.h>

Public types

enum class  Union { NONE , RANK , SIZE }
 how merge() chooses the root that goes under the other one More...

Public member functions

 MFSet (int n, Union u=Union::RANK, bool compress=true)
 the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \) (the Mfset( n ) of the book), with the given union and with the compression of the paths or not
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 \), the root of its tree
bool merge (int x, int y)
 joins the components of \( x \) and \( y \); true if they were distinct
bool same (int x, int y)
 true if \( x \) and \( y \) are in the same component, find( x ) == find( y )
int parent (int x) const
 the parent of \( x \), \( x \) itself if a root
int rank (int x) const
 the rank of \( x \)
int size (int x) const
 the number of nodes of the tree of \( x \), if \( x \) is a root and the union is by size
long steps (void) const
 the steps up the trees made by find() so far

Detailed description

the union of disjoint sets on a forest

Member enumeration documentation

◆ Union

enum class ADS::MFSet::Union
strong

how merge() chooses the root that goes under the other one

Enumerator
NONE 

the root of \( y \) under that of \( x \)

RANK 

the lower tree under the higher one

SIZE 

the smaller tree under the larger one

Constructor and destructor documentation

◆ MFSet()

ADS::MFSet::MFSet ( int n,
Union u = Union::RANK,
bool compress = true )
explicit

the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \) (the Mfset( n ) of the book), with the given union and with the compression of the paths or not

Precondition
\( n \ge 0 \)

Member function documentation

◆ count()

int ADS::MFSet::count ( void ) const
nodiscard

the number of components

◆ find()

int ADS::MFSet::find ( int x)

the representative of the component of \( x \), the root of its tree

In pseudocode (sections 10.2.2 and 10.2.4, the iterative version of exercise 10.8), the root first and then the compression:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( x ) \cr 2 & \quad r \gets \text{the root above } x \text{, by following } p \text{ until } p[ r ] = r \cr 3 & \quad \textbf{if}\ \mathit{compress}\ \textbf{then}\quad \triangleright\ \text{a second pass, which hangs the path on } r \cr 4 & \qquad \textbf{while}\ x \ne r\ \textbf{do}\ y \gets p[ x ];\ p[ x ] \gets r;\ x \gets y \cr 5 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( x \in [ 0 , n ) \)
Postcondition
the result is a root

◆ merge()

bool ADS::MFSet::merge ( int x,
int y )

joins the components of \( x \) and \( y \); true if they were distinct

In pseudocode (sections 10.2.2 and 10.2.3), with the union by rank:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{merge}( x , y ) \cr 2 & \quad r_x \gets \mathrm{find}( x );\ r_y \gets \mathrm{find}( y ); \ \textbf{if}\ r_x = r_y\ \textbf{then return false} \cr 3 & \quad \textbf{if}\ \mathit{rank}[ r_x ] < \mathit{rank}[ r_y ] \ \textbf{then}\ p[ r_x ] \gets r_y\quad \triangleright \ \text{the lower tree goes under} \cr 4 & \quad \textbf{else if}\ \mathit{rank}[ r_y ] < \mathit{rank}[ r_x ] \ \textbf{then}\ p[ r_y ] \gets r_x \cr 5 & \quad \textbf{else}\ p[ r_x ] \gets r_y;\ \mathit{rank}[ r_y ] \gets \mathit{rank}[ r_y ] + 1\quad \triangleright \ \text{a tie: one level more} \cr 6 & \quad \textbf{return true} \end{array}} \]

and with the union by size the root of the smaller tree goes under the other one, whose size grows by that of the smaller tree (the root of \( y \) under that of \( x \) on a tie).

Precondition
\( x , y \in [ 0 , n ) \)
Postcondition
\( x \) and \( y \) are in the same component

◆ n()

int ADS::MFSet::n ( void ) const
nodiscard

the number \( n \) of elements

◆ parent()

int ADS::MFSet::parent ( int x) const
nodiscard

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

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

◆ rank()

int ADS::MFSet::rank ( int x) const
nodiscard

the rank of \( x \)

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

◆ same()

bool ADS::MFSet::same ( int x,
int y )

true if \( x \) and \( y \) are in the same component, find( x ) == find( y )

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

◆ size()

int ADS::MFSet::size ( int x) const
nodiscard

the number of nodes of the tree of \( x \), if \( x \) is a root and the union is by size

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

◆ steps()

long ADS::MFSet::steps ( void ) const
nodiscard

the steps up the trees made by find() so far


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