Union Find class (Disjoint-set data structure).
More...
#include <gbase.h>
List of all members.
Public Member Functions |
TInt & | Parent (const int &Key) |
| Returns the parent of element Key.
|
TInt & | Rank (const int &Key) |
| Returns the rank of element Key.
|
| TUnionFind () |
| TUnionFind (const int &ExpectKeys) |
| Constructor that reserves enough memory for ExpectKeys elements.
|
| TUnionFind (const TUnionFind &UnionFind) |
TUnionFind & | operator= (const TUnionFind &UF) |
int | Len () const |
| Returns the number of elements in the structure.
|
bool | IsKey (const int &Key) const |
| Returns true if the structure contains element Key.
|
int | GetKeyI (const int &KeyN) const |
| Returns the element KeyN.
|
int | Find (const int &Key) |
| Returns the set that contains element Key.
|
int | Add (const int &Key) |
| Adds an element Key to the structure.
|
void | Union (const int &Key1, const int &Key2) |
| Merges sets with elements Key1 and Key2.
|
bool | IsSameSet (const int &Key1, const int &Key2) |
| Returns true if elements Key1 and Key2 are in the same set.
|
void | Dump () |
| Prints out the structure to standard output.
|
Private Attributes |
THash< TInt, TIntPr > | KIdSetH |
Detailed Description
Union Find class (Disjoint-set data structure).
For more info see: http://en.wikipedia.org/wiki/Disjoint-set_data_structure).
Definition at line 214 of file gbase.h.
Constructor & Destructor Documentation
Constructor that reserves enough memory for ExpectKeys elements.
Definition at line 225 of file gbase.h.
Member Function Documentation
Adds an element Key to the structure.
Definition at line 238 of file gbase.h.
Prints out the structure to standard output.
Definition at line 53 of file gbase.cpp.
Returns the set that contains element Key.
Definition at line 23 of file gbase.cpp.
{
int SetId = Key, parent = Parent(Key);
while (parent != -1) {
SetId = parent;
parent = Parent(parent);
}
parent = Key;
while (parent != -1) {
const int tmp = Parent(parent);
if (tmp != -1) { Parent(parent) = SetId; }
parent = tmp;
}
return SetId;
}
Returns the element KeyN.
Definition at line 234 of file gbase.h.
Returns true if the structure contains element Key.
Definition at line 232 of file gbase.h.
Returns true if elements Key1 and Key2 are in the same set.
Definition at line 242 of file gbase.h.
Returns the number of elements in the structure.
Definition at line 230 of file gbase.h.
Returns the parent of element Key.
Definition at line 219 of file gbase.h.
Returns the rank of element Key.
Definition at line 221 of file gbase.h.
Merges sets with elements Key1 and Key2.
Definition at line 40 of file gbase.cpp.
{
const int root1 = Find(Key1);
const int root2 = Find(Key2);
TInt& rank1 = Rank(root1);
TInt& rank2 = Rank(root2);
if (rank1 > rank2) { Parent(root2) = root1; }
else if (rank1 < rank2) { Parent(root1) = root2; }
else if (root1 != root2) {
Parent(root2) = root1;
Rank(root1)++;
}
}
Member Data Documentation
The documentation for this class was generated from the following files: