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

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 )

Detailed description

the union of disjoint sets of \( [ 0 , n ) \) by rank, with no compression, whose merges can be undone

Constructor and destructor documentation

◆ RollbackUnionFind()

ADS::RollbackUnionFind::RollbackUnionFind ( int n = 0)
explicit

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

Precondition
\( n \ge 0 \)

Member function documentation

◆ checkpoint()

int ADS::RollbackUnionFind::checkpoint ( void ) const
inlinenodiscard

the merges done and not undone, the time to roll back to

◆ count()

int ADS::RollbackUnionFind::count ( void ) const
inlinenodiscard

the number of components

◆ find()

int ADS::RollbackUnionFind::find ( int x) const
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}} \]

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

◆ merge()

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

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

◆ n()

int ADS::RollbackUnionFind::n ( void ) const
inlinenodiscard

the number of elements

◆ reset()

void ADS::RollbackUnionFind::reset ( int n)

back to the \( n \) components, no merge done

Precondition
\( n \ge 0 \)

◆ rollback()

void ADS::RollbackUnionFind::rollback ( int t)

undoes the merges back to the checkpoint \( t \), i.e., undo( checkpoint() - t )

Precondition
\( 0 \le t \le \) checkpoint()

◆ same()

bool ADS::RollbackUnionFind::same ( int x,
int y ) const
inlinenodiscard

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

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

◆ size()

int ADS::RollbackUnionFind::size ( int x) const
inlinenodiscard

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

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

◆ undo()

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

Precondition
\( 0 \le k \le \) checkpoint()

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