ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RollbackUnionFind.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File RollbackUnionFind.h ------------------------*/
3/*--------------------------------------------------------------------------*/
62/*--------------------------------------------------------------------------*/
63/*----------------------------- DEFINITIONS --------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#ifndef __ADS_RollbackUnionFind
67 #define __ADS_RollbackUnionFind
68/* self-identification: #endif constant */
69
70/*--------------------------------------------------------------------------*/
71/*------------------------------ INCLUDES ----------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#include "ADS/Contract.h"
75
76#include <cstddef>
77#include <vector>
78
79/*--------------------------------------------------------------------------*/
80/*--------------------------- NAMESPACE ------------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83namespace ADS
84{
85
86/*--------------------------------------------------------------------------*/
87/*------------------------ CLASS RollbackUnionFind -------------------------*/
88/*--------------------------------------------------------------------------*/
89
93{
94
95/*--------------------------------------------------------------------------*/
96 public:
100 explicit RollbackUnionFind( int n = 0 );
101
104 void reset( int n );
105
107 [[nodiscard]] int n( void ) const { return( int( f_p.size() ) ); }
108
110 [[nodiscard]] int count( void ) const { return( f_count ); }
111
124 [[nodiscard]] int find( int x ) const;
125
150 bool merge( int x , int y );
151
154 [[nodiscard]] bool same( int x , int y ) const {
155 return( find( x ) == find( y ) );
156 }
157
160 [[nodiscard]] int size( int x ) const { return( f_size[ find( x ) ] ); }
161
163 [[nodiscard]] int checkpoint( void ) const {
164 return( int( f_stack.size() ) );
165 }
166
184 void undo( int k = 1 );
185
189 void rollback( int t );
190
191/*--------------------------------------------------------------------------*/
192 private:
195 struct Record
196 {
197 int child;
198 int parent;
199 bool grew;
200 };
201
204 [[nodiscard]] bool forest( void ) const;
205
206 std::vector< int > f_p;
207 std::vector< int > f_rank;
208 std::vector< int > f_size;
209 std::vector< Record > f_stack;
210 int f_count = 0;
211
212 }; // end( class RollbackUnionFind )
213
214/*--------------------------------------------------------------------------*/
215/*----------------------- OFFLINE DYNAMIC CONNECTIVITY ---------------------*/
216/*--------------------------------------------------------------------------*/
217
220{
229 int u = 0;
230 int v = 0;
231 };
232
273[[nodiscard]] inline std::vector< int >
274offlineConnectivity( int n , const std::vector< ConnectivityOp > & ops );
275
276/*--------------------------------------------------------------------------*/
277
278} // end( namespace ADS )
279
280/*--------------------------------------------------------------------------*/
281/*-------------------------- IMPLEMENTATION --------------------------------*/
282/*--------------------------------------------------------------------------*/
283
284#include "ADS/mfset/impl/RollbackUnionFind.tpp"
285
286/*--------------------------------------------------------------------------*/
287
288#endif /* RollbackUnionFind.h included */
289
290/*--------------------------------------------------------------------------*/
291/*---------------------- End File RollbackUnionFind.h ----------------------*/
292/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
int find(int x) const
the representative of the component of , in
int size(int x) const
the number of elements of the component of
Definition RollbackUnionFind.h:160
RollbackUnionFind(int n=0)
the components , no merge done
void undo(int k=1)
undoes the last merges
bool merge(int x, int y)
joins the components of and , the root of lower rank under the other, and records it; true if they w...
bool same(int x, int y) const
whether and are in the same component
Definition RollbackUnionFind.h:154
int checkpoint(void) const
the merges done and not undone, the time to roll back to
Definition RollbackUnionFind.h:163
int n(void) const
the number of elements
Definition RollbackUnionFind.h:107
void reset(int n)
back to the components, no merge done
int count(void) const
the number of components
Definition RollbackUnionFind.h:110
void rollback(int t)
undoes the merges back to the checkpoint , i.e., undo( checkpoint() - t )
std::vector< int > offlineConnectivity(int n, const std::vector< ConnectivityOp > &ops)
the answers to the questions of ops on nodes, in their order: 1 or 0 for CONNECTED,...
Definition ADT.h:96
an operation on a graph whose edges come and go
Definition RollbackUnionFind.h:220
int u
the first node
Definition RollbackUnionFind.h:229
Kind
the kinds of operation
Definition RollbackUnionFind.h:222
@ COMPONENTS
how many components there are
Definition RollbackUnionFind.h:226
@ CONNECTED
whether and are connected
Definition RollbackUnionFind.h:225
@ INSERT
the insertion of the edge
Definition RollbackUnionFind.h:223
@ DELETE
the deletion of the edge
Definition RollbackUnionFind.h:224
Kind kind
the kind
Definition RollbackUnionFind.h:228
int v
the second node
Definition RollbackUnionFind.h:230