COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
DisjSets.cpp
Go to the documentation of this file.
1
2
7DisjSets::DisjSets( int numElements ) : s( numElements, -1 )
8{
9}
10
18void DisjSets::unionSets( int root1, int root2 )
19{
20 if( s[ root2 ] < s[ root1 ] ) // root2 is deeper
21 s[ root1 ] = root2; // Make root2 new root
22 else
23 {
24 if( s[ root1 ] == s[ root2 ] )
25 --s[ root1 ]; // Update height if same
26 s[ root2 ] = root1; // Make root1 new root
27 }
28}
29
30
36int DisjSets::find( int x ) const
37{
38 if( s[ x ] < 0 )
39 return x;
40 else
41 return find( s[ x ] );
42}
43
44
50int DisjSets::find( int x )
51{
52 if( s[ x ] < 0 )
53 return x;
54 else
55 return s[ x ] = find( s[ x ] );
56}
57
int find(int x) const
Definition DisjSets.cpp:36
DisjSets(int numElements)
Definition DisjSets.cpp:7
void unionSets(int root1, int root2)
Definition DisjSets.cpp:18