SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
TUnionFind Class Reference

Union Find class (Disjoint-set data structure). More...

#include <gbase.h>

Public Member Functions

TIntParent (const int &Key)
 Returns the parent of element Key. More...
 
TIntRank (const int &Key)
 Returns the rank of element Key. More...
 
 TUnionFind ()
 
 TUnionFind (const int &ExpectKeys)
 Constructor that reserves enough memory for ExpectKeys elements. More...
 
 TUnionFind (const TUnionFind &UnionFind)
 
TUnionFindoperator= (const TUnionFind &UF)
 
int Len () const
 Returns the number of elements in the structure. More...
 
bool IsKey (const int &Key) const
 Returns true if the structure contains element Key. More...
 
int GetKeyI (const int &KeyN) const
 Returns the element KeyN. More...
 
int Find (const int &Key)
 Returns the set that contains element Key. More...
 
int Add (const int &Key)
 Adds an element Key to the structure. More...
 
void Union (const int &Key1, const int &Key2)
 Merges sets with elements Key1 and Key2. More...
 
bool IsSameSet (const int &Key1, const int &Key2)
 Returns true if elements Key1 and Key2 are in the same set. More...
 
void Dump ()
 Prints out the structure to standard output. More...
 

Private Attributes

THash< TInt, TIntPrKIdSetH
 

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 230 of file gbase.h.

Constructor & Destructor Documentation

TUnionFind::TUnionFind ( )
inline

Definition at line 239 of file gbase.h.

239 : KIdSetH() { }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
TUnionFind::TUnionFind ( const int &  ExpectKeys)
inline

Constructor that reserves enough memory for ExpectKeys elements.

Definition at line 241 of file gbase.h.

241 : KIdSetH(ExpectKeys, true) { }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
TUnionFind::TUnionFind ( const TUnionFind UnionFind)
inline

Definition at line 242 of file gbase.h.

242 : KIdSetH(UnionFind.KIdSetH) { }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232

Member Function Documentation

int TUnionFind::Add ( const int &  Key)
inline

Adds an element Key to the structure.

Definition at line 254 of file gbase.h.

254 { KIdSetH.AddDat(Key, TIntPr(-1, 0)); return Key; }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void TUnionFind::Dump ( )

Prints out the structure to standard output.

Definition at line 53 of file gbase.cpp.

53  {
54  printf(" key\tset\n");
55  for (int i = 0; i < KIdSetH.Len(); i++) {
56  printf(" %d\t%d\n", int(KIdSetH.GetKey(i)), Find(KIdSetH.GetKey(i)));
57  }
58  printf("\n");
59 }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
int Find(const int &Key)
Returns the set that contains element Key.
Definition: gbase.cpp:23
int Len() const
Definition: hash.h:228
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
int TUnionFind::Find ( const int &  Key)

Returns the set that contains element Key.

Definition at line 23 of file gbase.cpp.

23  {
24  int SetId = Key, parent = Parent(Key);
25  // find set id
26  while (parent != -1) {
27  SetId = parent;
28  parent = Parent(parent);
29  }
30  // flatten
31  parent = Key;
32  while (parent != -1) {
33  const int tmp = Parent(parent);
34  if (tmp != -1) { Parent(parent) = SetId; }
35  parent = tmp;
36  }
37  return SetId;
38 }
TInt & Parent(const int &Key)
Returns the parent of element Key.
Definition: gbase.h:235
int TUnionFind::GetKeyI ( const int &  KeyN) const
inline

Returns the element KeyN.

Definition at line 250 of file gbase.h.

250 { return KIdSetH.GetKey(KeyN); }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
bool TUnionFind::IsKey ( const int &  Key) const
inline

Returns true if the structure contains element Key.

Definition at line 248 of file gbase.h.

248 { return KIdSetH.IsKey(Key); }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
bool IsKey(const TKey &Key) const
Definition: hash.h:258
bool TUnionFind::IsSameSet ( const int &  Key1,
const int &  Key2 
)
inline

Returns true if elements Key1 and Key2 are in the same set.

Definition at line 258 of file gbase.h.

258  {
259  return Find(Key1) == Find(Key2); }
int Find(const int &Key)
Returns the set that contains element Key.
Definition: gbase.cpp:23
int TUnionFind::Len ( ) const
inline

Returns the number of elements in the structure.

Definition at line 246 of file gbase.h.

246 { return KIdSetH.Len(); }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
int Len() const
Definition: hash.h:228
TUnionFind& TUnionFind::operator= ( const TUnionFind UF)
inline

Definition at line 243 of file gbase.h.

243 { KIdSetH=UF.KIdSetH; return *this; }
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
TInt& TUnionFind::Parent ( const int &  Key)
inline

Returns the parent of element Key.

Definition at line 235 of file gbase.h.

235 { return KIdSetH.GetDat(Key).Val1; }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
TVal1 Val1
Definition: ds.h:34
TInt& TUnionFind::Rank ( const int &  Key)
inline

Returns the rank of element Key.

Definition at line 237 of file gbase.h.

237 { return KIdSetH.GetDat(Key).Val2; }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
THash< TInt, TIntPr > KIdSetH
Definition: gbase.h:232
TVal2 Val2
Definition: ds.h:35
void TUnionFind::Union ( const int &  Key1,
const int &  Key2 
)

Merges sets with elements Key1 and Key2.

Definition at line 40 of file gbase.cpp.

40  {
41  const int root1 = Find(Key1);
42  const int root2 = Find(Key2);
43  TInt& rank1 = Rank(root1);
44  TInt& rank2 = Rank(root2);
45  if (rank1 > rank2) { Parent(root2) = root1; }
46  else if (rank1 < rank2) { Parent(root1) = root2; }
47  else if (root1 != root2) {
48  Parent(root2) = root1;
49  Rank(root1)++;
50  }
51 }
int Find(const int &Key)
Returns the set that contains element Key.
Definition: gbase.cpp:23
TInt & Rank(const int &Key)
Returns the rank of element Key.
Definition: gbase.h:237
Definition: dt.h:1137
TInt & Parent(const int &Key)
Returns the parent of element Key.
Definition: gbase.h:235

Member Data Documentation

THash<TInt, TIntPr> TUnionFind::KIdSetH
private

Definition at line 232 of file gbase.h.


The documentation for this class was generated from the following files: