|
ADS
Algorithms and Data Structures
|
the union of disjoint sets on lists, each element with the head of its list More...
#include <ADS/mfset/ListMFSet.h>
Public member functions | |
| ListMFSet (int n) | |
| the \( n \) singletons \( \{ 0 \} , \ldots , \{ n - 1 \} \) | |
| int | n (void) const |
| the number \( n \) of elements | |
| int | find (int x) const |
| the head of the list of \( x \), in \( O( 1 ) \) time | |
| bool | merge (int x, int y) |
| joins the lists of \( x \) and \( y \), the shorter after the longer, its elements relabeled; true if they were distinct | |
| long | relabels (void) const |
| the elements relabeled so far | |
| const std::vector< int > & | members (int x) const |
| the members of the set of \( x \) | |
the union of disjoint sets on lists, each element with the head of its list
|
explicit |
the \( n \) singletons \( \{ 0 \} , \ldots , \{ n - 1 \} \)
|
nodiscard |
the head of the list of \( x \), in \( O( 1 ) \) time
|
nodiscard |
the members of the set of \( x \)
| bool ADS::ListMFSet::merge | ( | int | x, |
| int | y ) |
joins the lists of \( x \) and \( y \), the shorter after the longer, its elements relabeled; true if they were distinct
In pseudocode (section 10.2.1), \( \mathit{head}[ x ] \) the head of the list of \( x \) and \( L[ a ] \) the list of the head \( a \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{merge}( x , y ) \cr 2 & \quad a \gets \mathit{head}[ x ];\ b \gets \mathit{head}[ y ]; \ \textbf{if}\ a = b\ \textbf{then return false} \cr 3 & \quad \textbf{if}\ |L[ a ]| < |L[ b ]|\ \textbf{then}\ a \leftrightarrow b\quad \triangleright\ \text{the shorter list is } L[ b ] \cr 4 & \quad \textbf{foreach}\ z \in L[ b ]\ \textbf{do}\ \text{append } z \text{ to } L[ a ];\ \mathit{head}[ z ] \gets a \cr 5 & \quad \text{empty } L[ b ];\ \textbf{return true} \end{array}} \]
|
nodiscard |
the number \( n \) of elements
|
nodiscard |
the elements relabeled so far