|
ADS
Algorithms and Data Structures
|
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 | |
the union of disjoint sets on a forest
|
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 |
|
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
|
nodiscard |
the number of components
| 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}} \]
| 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).
|
nodiscard |
the number \( n \) of elements
|
nodiscard |
the parent of \( x \), \( x \) itself if a root
|
nodiscard |
the rank of \( x \)
| bool ADS::MFSet::same | ( | int | x, |
| int | y ) |
true if \( x \) and \( y \) are in the same component, find( x ) == find( y )
|
nodiscard |
the number of nodes of the tree of \( x \), if \( x \) is a root and the union is by size
|
nodiscard |
the steps up the trees made by find() so far