SNAP Library 2.1, User Reference
2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
Graph Hash Table. More...
#include <ghash.h>
Public Types | |
typedef THash< TGraphKey, TDat > ::TIter | TIter |
Public Member Functions | |
TGHash (const bool &HashTrees, const int &MaxIsoCheck=8, const int &MaxSvdGraph=500) | |
Default contructor. | |
TGHash (TSIn &SIn) | |
void | Save (TSOut &SOut) const |
const TDat & | operator[] (const int &KeyId) const |
Accesses the data at hash table position index KeyId. | |
TDat & | operator[] (const int &KeyId) |
Accesses the data at hash table position index KeyId. | |
const TDat & | operator() (const TGraphKey &Key) const |
Accesses the data of graph-key Key. | |
TDat & | operator() (const TGraphKey &Key) |
Accesses the data of graph-key Key. | |
TIter | BegI () const |
Returns iterator to the first element of the hash table. | |
TIter | EndI () const |
Returns iterator to one past the last element of the hash table. | |
TIter | GetI (const int &KeyId) const |
Returns iterator to a key at position index KeyId. | |
bool | HashTrees () const |
Returns whether the hash table only hashes trees and not arbitrary directed graphs. | |
void | Gen (const int &ExpectVals) |
Initializes the hash table for the expected number of keys ExpectVals. | |
void | Clr (const bool &DoDel=true, const int &NoDelLim=-1) |
Removes all the elements from the hash table. | |
bool | Empty () const |
Tests whether the hash table is empty. | |
int | Len () const |
Returns the number of keys in the hash table. | |
int | GetPorts () const |
Returns the number of ports in the hash table. | |
bool | IsAutoSize () const |
Tests whether the hash table automatically adjusts the number of ports based on the number of keys. | |
int | GetMxKeyIds () const |
Returns the maximum key Id of any element in the hash table. | |
bool | IsKeyIdEqKeyN () const |
Tests whether there are any unused slots in the hash table. | |
int | AddKey (const PNGraph &Graph) |
Adds a key Graph to the table and returns its KeyId. | |
TDat & | AddDat (const PNGraph &Graph) |
Adds a key Graph to the table and returns its data value. | |
TDat & | AddDat (const PNGraph &Graph, const TDat &Dat) |
Adds a key Graph to the table, sets its data value to value of Dat and returns Dat. | |
bool | IsKey (const PNGraph &Graph) const |
Test whether Graph is an existing key in the hash table. | |
int | GetKeyId (const PNGraph &Graph) const |
Returns the KeyId (position index) of key Graph. | |
const TDat & | GetDat (const PNGraph &Graph) const |
Returns the data associated with key Graph. | |
TDat & | GetDat (const PNGraph &Graph) |
Returns the data associated with key Graph. | |
const TGraphKey & | GetKey (const int &KeyId) const |
Returns the GraphKey with position index KeyId. | |
int | GetKeyId (const TGraphKey &Key) const |
Returns the KeyId for a given Key. | |
bool | IsKey (const TGraphKey &Key) const |
Tests whether a given Key exists in the hash table. | |
bool | IsKey (const TGraphKey &Key, int &KeyId) const |
Tests whether a given Key exists in the hash table. | |
bool | IsKeyId (const int &KeyId) const |
Tests whether there exists a key at given position index KeyId. | |
const TDat & | GetDat (const TGraphKey &Key) const |
Returns data with a given graph Key. | |
TDat & | GetDat (const TGraphKey &Key) |
Returns data with a given graph Key. | |
const TDat & | GetDatId (const int &KeyId) const |
Returns data at a given position index KeyId. | |
TDat & | GetDatId (const int &KeyId) |
Returns data at a given position index KeyId. | |
void | GetKeyDat (const int &KeyId, TGraphKey &Key, TDat &Dat) const |
Returns Key and Data at a given position index KeyId. | |
bool | IsKeyGetDat (const TGraphKey &Key, TDat &Dat) const |
Test whether Key exists and sets its data to Dat. | |
bool | GetNodeMap (const PNGraph &Graph, TIntPrV &NodeMapV) const |
Returns the mapping of node Ids of the Graph to those of the graph-key in the hash table. | |
bool | GetNodeMap (const PNGraph &Graph, TIntPrV &NodeMapV, int &KeyId) const |
Returns the mapping of node Ids of the Graph to those of the graph-key in the hash table and the Graph KeyId. | |
int | FFirstKeyId () const |
Finds first KeyId. | |
bool | FNextKeyId (int &KeyId) const |
Finds next KeyId. | |
void | GetKeyV (TVec< TGraphKey > &KeyV) const |
Returns a vector of keys stored in the hash table. | |
void | GetDatV (TVec< TDat > &DatV) const |
Returns a vector of data elements stored in the hash table. | |
void | GetKeyIdByDat (TIntV &KeyIdV, const bool &Asc=true) const |
Returns a vector of KeyIds of hash table elements sorted by their data value. | |
void | GetKeyIdByGSz (TIntV &KeyIdV, const bool &Asc=true) const |
Returns a vector of KeyIds of hash table elements sorted by their graph size. | |
void | GetKeyDatPrV (TVec< TPair< TGraphKey, TDat > > &KeyDatPrV) const |
Returns a vector of pairs (Key, Data) elements stored in the hash table. | |
void | GetDatKeyPrV (TVec< TPair< TDat, TGraphKey > > &DatKeyPrV) const |
Returns a vector of pairs (Data, Key) elements stored in the hash table. | |
void | Defrag () |
Removes unused slots from the hash table. | |
void | Pack () |
Frees the unused memory by the hash table. | |
void | DrawGViz (const int &KeyId, const TStr &OutFNmPref, const TStr &OutputType="gif", TStr Desc="") const |
Saves a given graph with key Id KeyId in DOT format and calls the GraphViz to draw it. | |
void | DrawGViz (const TIntV &KeyIdV, const TStr &OutFNmPref, const TStr &OutputType="gif") const |
Saves a set of graphs with key Ids KeyIdV in DOT format and calls the GraphViz to draw them. | |
void | SaveTxt (const TStr &OutFNm, const TStr &Desc, const TStr &DatColNm, const bool &SortByKeyVal=true) const |
Saves all graphs stored in the hash table into a text file. | |
void | SaveDetailTxt (const TStr &OutFNm, const TStr &Desc, const TStr &DatColNm) const |
Saves all graphs stored in the hash table into a text file and include additional information. | |
Private Member Functions | |
void | InitPermutations () |
int | IsGetKeyId (const PNGraph &Graph) const |
int | IsGetKeyId (const PNGraph &Graph, TGraphKey &GKey) const |
int | IsGetKeyId (const PNGraph &Graph, TGraphKey &GKey, TIntPrV &NodeMap) const |
Private Attributes | |
TInt | MxIsoCheck |
TInt | MxSvdGraph |
THash< TInt, TVec< TIntV > > | GSzToPermH |
TBool | HashOnlyTrees |
THash< TGraphKey, TDat > | GraphH |
Graph Hash Table.
Keys in this hash table are (little) directed graphs. The class is useful for counting frequencies of small subgraphs or information cascades. For small graphs with less than MxIsoCheck nodes the class performs exact isomorphism checking. For graphs with less than MxSvdGraph nodes the class performs approximate isomorphism checking by comparing a numeric SVD-based signatures of two graphs. For graphs with more than MxSvdGraph nodes the class performs approximate isorphism checking by comparing only the signature based on simple graph statistics. For hashing trees (tree is encoded as a directed graph where children point to the parent) the class always performs exact isomorphism testing.
TGHash< TDat >::TGHash | ( | const bool & | HashTrees, |
const int & | MaxIsoCheck = 8 , |
||
const int & | MaxSvdGraph = 500 |
||
) |
Default contructor.
For hashing trees and not general graphs, set HashTrees=true. A tree is a directed graph where children point to the parent. In this case exact isomorphism checking will be performed. For hashing general graphs, set HashTrees=false. MaxIsoCheck is the maximum number of nodes for which we perform brute force exact isomorphism check. For larger graph the isomorphism test is only approximate.
Definition at line 304 of file ghash.h.
: MxIsoCheck(MaxIsoCheck), MxSvdGraph(MaxSvdGraph), GSzToPermH(), HashOnlyTrees(HashTrees), GraphH() { if (! HashTrees) { InitPermutations(); } }
Definition at line 312 of file ghash.h.
: MxIsoCheck(SIn), MxSvdGraph(SIn), GSzToPermH(), HashOnlyTrees(SIn), GraphH(SIn) { if (! HashOnlyTrees) { InitPermutations(); } }
Adds a key Graph to the table and returns its KeyId.
KeyId is position index in the hash table. If the key already exists the function returns KeyId of that existing key.
Definition at line 327 of file ghash.h.
{ if (HashOnlyTrees) { int RootNId; IAssert(TSnap::IsTree(Graph, RootNId)); TIntV TreeSig; TSnap::GetTreeSig(Graph, RootNId, TreeSig); TGraphKey GKey(TreeSig); const int KeyId = GraphH.GetKeyId(GKey); if (KeyId == -1) { GKey.TakeGraph(Graph); return GraphH.AddKey(GKey); } return KeyId; } else { TGraphKey GKey; GKey.TakeSig(Graph, MxIsoCheck+1, MxSvdGraph); // get signature const int Nodes = GKey.GetNodes(); if (Nodes > 2 && Nodes <= MxIsoCheck) { GKey.TakeGraph(Graph); // Check all variants with same signature for (int variant = 1; ; variant++) { GKey.SetVariant(variant); int KeyId = GraphH.GetKeyId(GKey); if (KeyId == -1) { // Key of such signature and variant does not exist yet. KeyId = GraphH.AddKey(GKey); return KeyId; } if (TGraphKey::IsIsomorph(GKey, GraphH.GetKey(KeyId), GSzToPermH.GetDat(Nodes))) { // Graph isomorphism test return KeyId; // Found isomorphic graph. } } } else { const int KeyId = GraphH.GetKeyId(GKey); if (KeyId == -1) { GKey.TakeGraph(Graph); return GraphH.AddKey(GKey); } return KeyId; } } Fail; return -1; }
void TGHash< TDat >::DrawGViz | ( | const int & | KeyId, |
const TStr & | OutFNmPref, | ||
const TStr & | OutputType = "gif" , |
||
TStr | Desc = "" |
||
) | const |
Saves a given graph with key Id KeyId in DOT format and calls the GraphViz to draw it.
Definition at line 480 of file ghash.h.
{ IAssert(OutputType == "ps" || OutputType == "gif" || OutputType == "png"); const TGraphKey& GKey = GetKey(KeyId); const TStr Desc1 = TStr::Fmt("%s (%d, %d)", Desc.CStr(), GKey.GetNodes(), GKey.GetEdges()); GKey.SaveGViz(OutFNmPref+".dot", Desc1); TSnap::TSnapDetail::GVizDoLayout(OutFNmPref+".dot", OutFNmPref+"."+OutputType, gvlDot); }
void TGHash< TDat >::DrawGViz | ( | const TIntV & | KeyIdV, |
const TStr & | OutFNmPref, | ||
const TStr & | OutputType = "gif" |
||
) | const |
Saves a set of graphs with key Ids KeyIdV in DOT format and calls the GraphViz to draw them.
Definition at line 489 of file ghash.h.
{ IAssert(OutputType == "ps" || OutputType == "gif" || OutputType == "png"); TExeTm ExeTm; printf("Plotting %d graphs\n", KeyIdV.Len()); for (int i = 0; i < KeyIdV.Len(); i++) { const TStr FNm = TStr::Fmt("%s.%03d.key%d.", OutFNmPref.CStr(), i+1, KeyIdV[i]()); const TStr Desc = TStr::Fmt("KeyId:%d", KeyIdV[i]()); const TGraphKey& GKey = GetKey(KeyIdV[i]); printf("\r %d g(%d, %d) ", i, GKey.GetNodes(), GKey.GetEdges()); GKey.SaveGViz(FNm+"dot", Desc); TSnap::TSnapDetail::GVizDoLayout(FNm+"dot", FNm+OutputType, gvlDot); } printf("done [%s].\n", ExeTm.GetTmStr()); }
int TGHash< TDat >::FFirstKeyId | ( | ) | const [inline] |
bool TGHash< TDat >::FNextKeyId | ( | int & | KeyId | ) | const [inline] |
Finds next KeyId.
Used for traversing the elements of the hash table: for (int KeyId = GHash.FFirstKeyId(); GHash.FNextKeyId(KeyId); ) { }
Definition at line 250 of file ghash.h.
{ return GraphH.FNextKeyId(KeyId); }
void TGHash< TDat >::GetDatKeyPrV | ( | TVec< TPair< TDat, TGraphKey > > & | DatKeyPrV | ) | const [inline] |
Returns a vector of pairs (Data, Key) elements stored in the hash table.
Definition at line 267 of file ghash.h.
{ GraphH.GetDatKeyPrV(DatKeyPrV); }
void TGHash< TDat >::GetKeyDatPrV | ( | TVec< TPair< TGraphKey, TDat > > & | KeyDatPrV | ) | const [inline] |
Returns a vector of pairs (Key, Data) elements stored in the hash table.
Definition at line 265 of file ghash.h.
{ GraphH.GetKeyDatPrV(KeyDatPrV); }
Returns the KeyId (position index) of key Graph.
If the key does not exist, the function returns -1.
Definition at line 188 of file ghash.h.
{ return IsGetKeyId(Graph); }
void TGHash< TDat >::GetKeyIdByDat | ( | TIntV & | KeyIdV, |
const bool & | Asc = true |
||
) | const |
Returns a vector of KeyIds of hash table elements sorted by their data value.
Asc=true: sort in ascending order, otherwise in descending order.
Definition at line 454 of file ghash.h.
{ TVec<TQuad<TDat, TInt,TInt, TInt> > DatKeyIdV(Len(), 0); // <TDat,Nodes,Edges,KeyId> for (int i = FFirstKeyId(); FNextKeyId(i); ) { DatKeyIdV.Add(TQuad<TDat, TInt,TInt, TInt>(GetDatId(i), GetKey(i).GetNodes(), GetKey(i).GetEdges(), i)); } DatKeyIdV.Sort(Asc); KeyIdV.Gen(Len(), 0); for (int i = 0; i < Len(); i++) { KeyIdV.Add(DatKeyIdV[i].Val4); } }
void TGHash< TDat >::GetKeyIdByGSz | ( | TIntV & | KeyIdV, |
const bool & | Asc = true |
||
) | const |
Returns a vector of KeyIds of hash table elements sorted by their graph size.
Asc=true: sort in ascending order, otherwise in descending order. Graph size is the number of nodes and edges.
Definition at line 467 of file ghash.h.
{ TVec<TQuad<TInt,TInt, TDat, TInt> > DatKeyIdV(Len(), 0); // <Nodes,Edges,TDat,KeyId> for (int i = FFirstKeyId(); FNextKeyId(i); ) { DatKeyIdV.Add(TQuad< TInt,TInt, TDat, TInt>(GetKey(i).GetNodes(), GetKey(i).GetEdges(), GetDatId(i), i)); } DatKeyIdV.Sort(Asc); KeyIdV.Gen(Len(), 0); for (int i = 0; i < Len(); i++) { KeyIdV.Add(DatKeyIdV[i].Val4); } }
int TGHash< TDat >::GetMxKeyIds | ( | ) | const [inline] |
Returns the maximum key Id of any element in the hash table.
Definition at line 164 of file ghash.h.
{ return GraphH.GetMxKeyIds(); }
bool TGHash< TDat >::GetNodeMap | ( | const PNGraph & | Graph, |
TIntPrV & | NodeMapV | ||
) | const |
Returns the mapping of node Ids of the Graph to those of the graph-key in the hash table.
The function assumes the Graph exists as a key in the table. Otherwise it returns false.
Definition at line 407 of file ghash.h.
{ int KeyId; return GetNodeMap(Graph, NodeMapV, KeyId); }
bool TGHash< TDat >::GetNodeMap | ( | const PNGraph & | Graph, |
TIntPrV & | NodeMapV, | ||
int & | KeyId | ||
) | const |
Returns the mapping of node Ids of the Graph to those of the graph-key in the hash table and the Graph KeyId.
The function assumes the Graph exists as a key in the table. Otherwise it returns false.
Definition at line 413 of file ghash.h.
{ NodeMapV.Clr(false); if (HashOnlyTrees) { int RootNId; IAssert(TSnap::IsTree(Graph, RootNId)); TIntV TreeSig; TSnap::GetTreeSig(Graph, RootNId, TreeSig, NodeMapV); TGraphKey GKey(TreeSig); KeyId = GraphH.GetKeyId(GKey); return KeyId != -1; } else { const int Nodes = Graph->GetNodes(); int IsoPermId = -1; NodeMapV.Clr(false); if (Nodes == 0) { return true; } else if (Nodes == 1) { NodeMapV.Add(TIntPr(Graph->BegNI().GetId(), 0)); return true; } else if (Nodes <= MxIsoCheck) { TGraphKey GKey; GKey.TakeSig(Graph, MxIsoCheck+1, MxSvdGraph); GKey.TakeGraph(Graph, NodeMapV); for (int variant = 1; ; variant++) { GKey.SetVariant(variant); KeyId = GraphH.GetKeyId(GKey); if (KeyId == -1) { return false; } if (TGraphKey::IsIsomorph(GKey, GraphH.GetKey(KeyId), GSzToPermH.GetDat(Nodes), IsoPermId)) { const TIntV& K1K2Perm = GSzToPermH.GetDat(Nodes)[IsoPermId]; // map from graph to key1 to key2 for (int i = 0; i < NodeMapV.Len(); i++) { NodeMapV[i].Val2 = K1K2Perm[NodeMapV[i].Val2]; } return true; } } return false; } else { return false; // graph too big to find the mapping } } Fail; return false; }
Returns whether the hash table only hashes trees and not arbitrary directed graphs.
Definition at line 146 of file ghash.h.
{ return HashOnlyTrees; }
void TGHash< TDat >::InitPermutations | ( | ) | [private] |
Definition at line 287 of file ghash.h.
{ GSzToPermH.Clr(); for (int nodes = 2; nodes <= MxIsoCheck; nodes++) { TVec<TIntV> NodePermutationV; TIntV NodeIdV(nodes, 0); for (int i = 0; i < nodes; i++) NodeIdV.Add(i); NodeIdV.Pack(); NodePermutationV.Add(NodeIdV); while (NodeIdV.NextPerm()) { NodePermutationV.Add(NodeIdV); } NodePermutationV.Pack(); GSzToPermH.AddDat(nodes, NodePermutationV); } }
bool TGHash< TDat >::IsAutoSize | ( | ) | const [inline] |
Tests whether the hash table automatically adjusts the number of ports based on the number of keys.
Definition at line 162 of file ghash.h.
{ return GraphH.IsAutoSize(); }
int TGHash< TDat >::IsGetKeyId | ( | const PNGraph & | Graph | ) | const [private] |
Definition at line 370 of file ghash.h.
{ TGraphKey GKey; return IsGetKeyId(Graph, GKey); }
int TGHash< TDat >::IsGetKeyId | ( | const PNGraph & | Graph, |
TGraphKey & | GKey | ||
) | const [private] |
Definition at line 376 of file ghash.h.
{ if (HashOnlyTrees) { // For trees we perform exact isomorshism test based on graph signatures int RootNId; IAssert(TSnap::IsTree(Graph, RootNId)); TIntV TreeSig; TSnap::GetTreeSig(Graph, RootNId, TreeSig); GKey = TGraphKey(TreeSig); const int KeyId = GraphH.GetKeyId(GKey); return KeyId; } else { // For small graphs of less than MxIsoCheck nodes we perform brute force isomorphism checking GKey.TakeSig(Graph, MxIsoCheck+1, MxSvdGraph); const int Nodes = GKey.GetNodes(); if (Nodes > 2 && Nodes <= MxIsoCheck) { GKey.TakeGraph(Graph); for (int variant = 1; ; variant++) { GKey.SetVariant(variant); int KeyId = GraphH.GetKeyId(GKey); // Is there a graph of the same signature and same VariantId if (KeyId == -1) { return -1; } if (TGraphKey::IsIsomorph(GKey, GraphH.GetKey(KeyId), GSzToPermH.GetDat(Nodes))) { return KeyId; } // perform brute force isomorphism check } } else { // For all other graphs we perform approximate graph isomorphism checking const int KeyId = GraphH.GetKeyId(GKey); return KeyId; } } Fail; return -1; }
int TGHash< TDat >::IsGetKeyId | ( | const PNGraph & | Graph, |
TGraphKey & | GKey, | ||
TIntPrV & | NodeMap | ||
) | const [private] |
Test whether Graph is an existing key in the hash table.
Definition at line 184 of file ghash.h.
{ int k=IsGetKeyId(Graph); return k!=-1; }
bool TGHash< TDat >::IsKeyGetDat | ( | const TGraphKey & | Key, |
TDat & | Dat | ||
) | const [inline] |
Test whether Key exists and sets its data to Dat.
Definition at line 230 of file ghash.h.
{ return GraphH.IsKeyGetDat(Key, Dat); }
bool TGHash< TDat >::IsKeyIdEqKeyN | ( | ) | const [inline] |
Tests whether there are any unused slots in the hash table.
Slots get freed after removing keys from the table.
Definition at line 168 of file ghash.h.
{ return GraphH.IsKeyIdEqKeyN(); }
const TDat& TGHash< TDat >::operator[] | ( | const int & | KeyId | ) | const [inline] |
TDat& TGHash< TDat >::operator[] | ( | const int & | KeyId | ) | [inline] |
Definition at line 319 of file ghash.h.
{ MxIsoCheck.Save(SOut); MxSvdGraph.Save(SOut); HashOnlyTrees.Save(SOut); GraphH.Save(SOut); }
void TGHash< TDat >::SaveDetailTxt | ( | const TStr & | OutFNm, |
const TStr & | Desc, | ||
const TStr & | DatColNm | ||
) | const |
Saves all graphs stored in the hash table into a text file and include additional information.
Definition at line 523 of file ghash.h.
{ TIntV KeyIdV; GetKeyIdByDat(KeyIdV, false); FILE *F = fopen(OutFNm.CStr(), "wt"); fprintf(F, "Graph-Hash-Table\n"); fprintf(F, "%s\n", Desc.CStr()); fprintf(F, "%d graphs", KeyIdV.Len()); for (int i = 0; i < KeyIdV.Len(); i++) { fprintf(F, "\n\n[%5d]\tRank: %d\n", KeyIdV[i](), i+1); fprintf(F, "Dat: %s\n", GetDat(KeyIdV[i]).GetStr().CStr()); GetDatId(KeyIdV[i]).SaveTxt(F); } fclose(F); }
void TGHash< TDat >::SaveTxt | ( | const TStr & | OutFNm, |
const TStr & | Desc, | ||
const TStr & | DatColNm, | ||
const bool & | SortByKeyVal = true |
||
) | const |
Saves all graphs stored in the hash table into a text file.
Definition at line 505 of file ghash.h.
{ TIntV KeyIdV; if (SortByKeyVal) GetKeyIdByDat(KeyIdV, false); else GetKeyIdByGSz(KeyIdV, true); FILE *F = fopen(OutFNm.CStr(), "wt"); fprintf(F, "Graph-Hash-Table"); fprintf(F, "%s\n", Desc.CStr()); fprintf(F, "%d graphs\n", KeyIdV.Len()); fprintf(F, "Rank\tKeyId\tNodes\tEdges\t%s\n", DatColNm.CStr()); for (int i = 0; i < KeyIdV.Len(); i++) { const TGraphKey& Key = GetKey(KeyIdV[i]); fprintf(F, "%d\t%d\t%d\t%d\t%s\n", i+1, KeyIdV[i](), Key.GetNodes(), Key.GetEdges(), GetDatId(KeyIdV[i]).GetStr().CStr()); } fclose(F); }
TBool TGHash< TDat >::HashOnlyTrees [private] |
TInt TGHash< TDat >::MxIsoCheck [private] |
TInt TGHash< TDat >::MxSvdGraph [private] |