|
ADS
Algorithms and Data Structures
|
the union of disjoint sets of \( [ 0 , n ) \) by rank, with no compression, whose merges can be undone More...
#include <ADS/mfset/RollbackUnionFind.h>
Public member functions | |
| RollbackUnionFind (int n=0) | |
| the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \), no merge done | |
| void | reset (int n) |
| back to the \( n \) components, no merge done | |
| int | n (void) const |
| the number of elements | |
| int | count (void) const |
| the number of components | |
| int | find (int x) const |
| the representative of the component of \( x \), in \( O( \log n ) \) | |
| bool | merge (int x, int y) |
| joins the components of \( x \) and \( y \), the root of lower rank under the other, and records it; true if they were distinct | |
| bool | same (int x, int y) const |
| whether \( x \) and \( y \) are in the same component | |
| int | size (int x) const |
| the number of elements of the component of \( x \) | |
| int | checkpoint (void) const |
| the merges done and not undone, the time to roll back to | |
| void | undo (int k=1) |
| undoes the last \( k \) merges | |
| void | rollback (int t) |
| undoes the merges back to the checkpoint \( t \), i.e., undo( checkpoint() - t ) | |
the union of disjoint sets of \( [ 0 , n ) \) by rank, with no compression, whose merges can be undone
|
explicit |
the \( n \) components \( \{ 0 \} , \ldots , \{ n - 1 \} \), no merge done
|
inlinenodiscard |
the merges done and not undone, the time to roll back to
|
inlinenodiscard |
the number of components
|
nodiscard |
the representative of the component of \( x \), in \( O( \log n ) \)
In pseudocode (section 10.2.2, with no compression):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( x ) \cr 2 & \quad \textbf{while}\ p[ x ] \ne x\ \textbf{do}\ x \gets p[ x ] \cr 3 & \quad \textbf{return}\ x \end{array}} \]
| bool ADS::RollbackUnionFind::merge | ( | int | x, |
| int | y ) |
joins the components of \( x \) and \( y \), the root of lower rank under the other, and records it; true if they were distinct
In pseudocode (section 10.2.3, with the record of the undo, beyond the book):
\[\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 ) \cr 3 & \quad \textbf{if}\ x = y\ \textbf{then}\ \mathrm{push}( R , ( \textbf{nil} , \textbf{nil} , \textbf{false} ) ); \ \textbf{return}\ \textbf{false} \cr 4 & \quad \textbf{if}\ \mathit{rank}[ x ] > \mathit{rank}[ y ] \ \textbf{then}\ \text{swap } x \text{ and } y \cr 5 & \quad p[ x ] \gets y;\ \mathit{size}[ y ] \gets \mathit{size}[ y ] + \mathit{size}[ x ] \cr 6 & \quad g \gets \mathit{rank}[ x ] = \mathit{rank}[ y ]; \ \textbf{if}\ g\ \textbf{then}\ \mathit{rank}[ y ] \gets \mathit{rank}[ y ] + 1 \cr 7 & \quad \mathrm{push}( R , ( x , y , g ) );\ \textbf{return} \ \textbf{true} \end{array}} \]
|
inlinenodiscard |
the number of elements
| void ADS::RollbackUnionFind::reset | ( | int | n | ) |
back to the \( n \) components, no merge done
| void ADS::RollbackUnionFind::rollback | ( | int | t | ) |
undoes the merges back to the checkpoint \( t \), i.e., undo( checkpoint() - t )
|
inlinenodiscard |
whether \( x \) and \( y \) are in the same component
|
inlinenodiscard |
the number of elements of the component of \( x \)
| void ADS::RollbackUnionFind::undo | ( | int | k = 1 | ) |
undoes the last \( k \) merges
In pseudocode (beyond the book; each step restores the forest before the merge that it undoes):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{undo}( k ) \cr 2 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ k\ \textbf{do} \cr 3 & \qquad ( x , y , g ) \gets \mathrm{pop}( R ) \cr 4 & \qquad \textbf{if}\ x \ne \textbf{nil}\ \textbf{then} \ p[ x ] \gets x;\ \mathit{size}[ y ] \gets \mathit{size}[ y ] - \mathit{size}[ x ] \cr 5 & \qquad \quad \textbf{if}\ g\ \textbf{then} \ \mathit{rank}[ y ] \gets \mathit{rank}[ y ] - 1 \end{array}} \]