|
SNAP Library 2.1, Developer Reference
2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
Union Find class (Disjoint-set data structure). More...
#include <gbase.h>

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 |
Union Find class (Disjoint-set data structure).
For more info see: http://en.wikipedia.org/wiki/Disjoint-set_data_structure).
| TUnionFind::TUnionFind | ( | ) | [inline] |
| TUnionFind::TUnionFind | ( | const int & | ExpectKeys | ) | [inline] |
| TUnionFind::TUnionFind | ( | const TUnionFind & | UnionFind | ) | [inline] |
| int TUnionFind::Add | ( | const int & | Key | ) | [inline] |
Adds an element Key to the structure.
Definition at line 238 of file gbase.h.
References THash< TKey, TDat, THashFunc >::AddDat(), and KIdSetH.
Referenced by TSnap::TSnapDetail::TCNMQMatrix::Init().


| void TUnionFind::Dump | ( | ) |
Prints out the structure to standard output.
Definition at line 53 of file gbase.cpp.
References Find(), THash< TKey, TDat, THashFunc >::GetKey(), KIdSetH, and THash< TKey, TDat, THashFunc >::Len().
{
printf(" key\tset\n");
for (int i = 0; i < KIdSetH.Len(); i++) {
printf(" %d\t%d\n", int(KIdSetH.GetKey(i)), Find(KIdSetH.GetKey(i)));
}
printf("\n");
}

| int TUnionFind::Find | ( | const int & | Key | ) |
Returns the set that contains element Key.
Definition at line 23 of file gbase.cpp.
References Parent().
Referenced by TSnap::TSnapDetail::TCNMQMatrix::CmtyCMN(), Dump(), IsSameSet(), and Union().
{
int SetId = Key, parent = Parent(Key);
// find set id
while (parent != -1) {
SetId = parent;
parent = Parent(parent);
}
// flatten
parent = Key;
while (parent != -1) {
const int tmp = Parent(parent);
if (tmp != -1) { Parent(parent) = SetId; }
parent = tmp;
}
return SetId;
}


| int TUnionFind::GetKeyI | ( | const int & | KeyN | ) | const [inline] |
| bool TUnionFind::IsKey | ( | const int & | Key | ) | const [inline] |
| bool TUnionFind::IsSameSet | ( | const int & | Key1, |
| const int & | Key2 | ||
| ) | [inline] |
| int TUnionFind::Len | ( | ) | const [inline] |
| TUnionFind& TUnionFind::operator= | ( | const TUnionFind & | UF | ) | [inline] |
| TInt& TUnionFind::Parent | ( | const int & | Key | ) | [inline] |
Returns the parent of element Key.
Definition at line 219 of file gbase.h.
References THash< TKey, TDat, THashFunc >::GetDat(), KIdSetH, and TPair< TVal1, TVal2 >::Val1.
Referenced by Find(), and Union().


| TInt& TUnionFind::Rank | ( | const int & | Key | ) | [inline] |
Returns the rank of element Key.
Definition at line 221 of file gbase.h.
References THash< TKey, TDat, THashFunc >::GetDat(), KIdSetH, and TPair< TVal1, TVal2 >::Val2.
Referenced by Union().


| void TUnionFind::Union | ( | const int & | Key1, |
| const int & | Key2 | ||
| ) |
Merges sets with elements Key1 and Key2.
Definition at line 40 of file gbase.cpp.
References Find(), Parent(), and Rank().
Referenced by TSnap::TSnapDetail::TCNMQMatrix::MergeBestQ().
{
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)++;
}
}


THash<TInt, TIntPr> TUnionFind::KIdSetH [private] |