ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MFSet.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File MFSet.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
71/*--------------------------------------------------------------------------*/
72/*----------------------------- DEFINITIONS --------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#ifndef __ADS_MFSet
76 #define __ADS_MFSet
77/* self-identification: #endif constant */
78
79/*--------------------------------------------------------------------------*/
80/*------------------------------ INCLUDES ----------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83#include "ADS/Contract.h"
84#include "ADS/graph/Graph.h"
85
86#include <stdexcept>
87#include <utility>
88#include <vector>
89
90/*--------------------------------------------------------------------------*/
91/*--------------------------- NAMESPACE ------------------------------------*/
92/*--------------------------------------------------------------------------*/
93
94namespace ADS
95{
96
97/*--------------------------------------------------------------------------*/
98/*------------------------------ CLASS MFSet -------------------------------*/
99/*--------------------------------------------------------------------------*/
100
102class MFSet
103{
104
105/*--------------------------------------------------------------------------*/
106 public:
108 enum class Union {
112 };
113
118 explicit MFSet( int n , Union u = Union::RANK , bool compress = true );
119
121 [[nodiscard]] int n( void ) const;
122
124 [[nodiscard]] int count( void ) const;
125
144 int find( int x );
145
171 bool merge( int x , int y );
172
176 bool same( int x , int y );
177
180 [[nodiscard]] int parent( int x ) const;
181
184 [[nodiscard]] int rank( int x ) const;
185
189 [[nodiscard]] int size( int x ) const;
190
192 [[nodiscard]] long steps( void ) const;
193
194/*--------------------------------------------------------------------------*/
195 private:
197 [[nodiscard]] int root( int x ) const;
198
201 [[nodiscard]] bool forest( void ) const;
202
203 std::vector< int > f_p;
204 std::vector< int > f_rank;
205 std::vector< int > f_size;
206 Union f_union;
207 bool f_compress;
208 int f_count;
209 long f_steps = 0;
210
211 }; // end( class MFSet )
212
213/*--------------------------------------------------------------------------*/
214
232template< Graph G >
233MFSet components( const G & g );
234
235/*--------------------------------------------------------------------------*/
236
237} // end( namespace ADS )
238
239/*--------------------------------------------------------------------------*/
240/*-------------------------- IMPLEMENTATION --------------------------------*/
241/*--------------------------------------------------------------------------*/
242
243#include "ADS/mfset/impl/MFSet.tpp"
244
245/*--------------------------------------------------------------------------*/
246
247#endif /* MFSet.h included */
248
249/*--------------------------------------------------------------------------*/
250/*---------------------------- End File MFSet.h ----------------------------*/
251/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
the union of disjoint sets on a forest
Definition MFSet.h:103
int rank(int x) const
the rank of
int find(int x)
the representative of the component of , the root of its tree
Union
how merge() chooses the root that goes under the other one
Definition MFSet.h:108
@ RANK
the lower tree under the higher one
Definition MFSet.h:110
@ SIZE
the smaller tree under the larger one
Definition MFSet.h:111
@ NONE
the root of under that of
Definition MFSet.h:109
int n(void) const
the number of elements
bool merge(int x, int y)
joins the components of and ; true if they were distinct
bool same(int x, int y)
true if and are in the same component, find( x ) == find( y )
int size(int x) const
the number of nodes of the tree of , if is a root and the union is by size
int parent(int x) const
the parent of , itself if a root
MFSet(int n, Union u=Union::RANK, bool compress=true)
the components (the Mfset( n ) of the book), with the given union and with the compression of the p...
long steps(void) const
the steps up the trees made by find() so far
int count(void) const
the number of components
The GRAPH of chapter 9 and its realizations.
MFSet components(const G &g)
the MFSET of the connected components of an unoriented graph, built by merging the ends of each edge ...
Definition ADT.h:96