|
SNAP Library, Developer Reference
2012-10-02 12:56:23
SNAP, a general purpose network analysis and graph mining library
|
#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) | |
| TGHash (TSIn &SIn) | |
| void | Save (TSOut &SOut) const |
| const TDat & | operator[] (const int &KeyId) const |
| TDat & | operator[] (const int &KeyId) |
| const TDat & | operator() (const TGraphKey &Key) const |
| TDat & | operator() (const TGraphKey &Key) |
| TIter | BegI () const |
| TIter | EndI () const |
| TIter | GetI (const int &KeyId) const |
| bool | HashTrees () const |
| void | Gen (const int &Ports) |
| void | Clr (const bool &DoDel=true, const int &NoDelLim=-1) |
| bool | Empty () const |
| int | Len () const |
| int | GetPorts () const |
| bool | IsAutoSize () const |
| int | GetMxKeyIds () const |
| bool | IsKeyIdEqKeyN () const |
| int | AddKey (const PNGraph &Graph) |
| TDat & | AddDat (const PNGraph &Graph) |
| TDat & | AddDat (const PNGraph &Graph, const TDat &Dat) |
| bool | IsKey (const PNGraph &Graph) const |
| int | GetKeyId (const PNGraph &Graph) const |
| const TDat & | GetDat (const PNGraph &Graph) const |
| TDat & | GetDat (const PNGraph &Graph) |
| const TGraphKey & | GetKey (const int &KeyId) const |
| int | GetKeyId (const TGraphKey &Key) const |
| bool | IsKey (const TGraphKey &Key) const |
| bool | IsKey (const TGraphKey &Key, int &KeyId) const |
| bool | IsKeyId (const int &KeyId) const |
| const TDat & | GetDat (const TGraphKey &Key) const |
| TDat & | GetDat (const TGraphKey &Key) |
| const TDat & | GetDatId (const int &KeyId) const |
| TDat & | GetDatId (const int &KeyId) |
| void | GetKeyDat (const int &KeyId, TGraphKey &Key, TDat &Dat) const |
| bool | IsKeyGetDat (const TGraphKey &Key, TDat &Dat) const |
| bool | GetNodeMap (const PNGraph &Graph, TIntPrV &NodeMapV) const |
| bool | GetNodeMap (const PNGraph &Graph, TIntPrV &NodeMapV, int &KeyId) const |
| int | FFirstKeyId () const |
| bool | FNextKeyId (int &KeyId) const |
| void | GetKeyV (TVec< TGraphKey > &KeyV) const |
| void | GetDatV (TVec< TDat > &DatV) const |
| void | GetKeyIdByDat (TIntV &KeyIdV, const bool &Asc=true) const |
| void | GetKeyIdByGSz (TIntV &KeyIdV, const bool &Asc=true) const |
| void | GetKeyDatPrV (TVec< TPair< TGraphKey, TDat > > &KeyDatPrV) const |
| void | GetDatKeyPrV (TVec< TPair< TDat, TGraphKey > > &DatKeyPrV) const |
| void | Defrag () |
| void | Pack () |
| void | DrawGViz (const int &KeyId, const TStr &OutFNmPref, const TStr &OutputType="gif", TStr Desc="") const |
| void | DrawGViz (const TIntV &KeyIdV, const TStr &OutFNmPref, const TStr &OutputType="gif") const |
| void | SaveTxt (const TStr &OutFNm, const TStr &Desc, const TStr &DatColNm, const bool &SortByKeyVal=true) const |
| void | SaveDetailTxt (const TStr &OutFNm, const TStr &Desc, const TStr &DatColNm) const |
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, a hash table where keys are (little) undirected graphs. Class is useful for counting frequencies of small subgraphs or information cascades.
| TGHash< TDat >::TGHash | ( | const bool & | HashTrees, |
| const int & | MaxIsoCheck = 8, |
||
| const int & | MaxSvdGraph = 500 |
||
| ) |
Definition at line 149 of file ghash.h.
References TGHash< TDat >::InitPermutations().
: MxIsoCheck(MaxIsoCheck), MxSvdGraph(MaxSvdGraph), GSzToPermH(), HashOnlyTrees(HashTrees), GraphH() { if (! HashTrees) { InitPermutations(); } }

Definition at line 157 of file ghash.h.
References TGHash< TDat >::HashOnlyTrees, and TGHash< TDat >::InitPermutations().
: MxIsoCheck(SIn), MxSvdGraph(SIn), GSzToPermH(), HashOnlyTrees(SIn), GraphH(SIn) { if (! HashOnlyTrees) { InitPermutations(); } }

Definition at line 89 of file ghash.h.
Referenced by TDGHashGraphCounter::operator()().

Definition at line 172 of file ghash.h.
References Fail, TGraphKey::GetNodes(), TSnap::GetTreeSig(), IAssert, TGraphKey::IsIsomorph(), TSnap::IsTree(), TGraphKey::SetVariant(), TGraphKey::TakeGraph(), and TGraphKey::TakeSig().
Referenced by TGHash< TUInt64 >::AddDat().
{
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) {
KeyId = GraphH.AddKey(GKey);
//printf(" new variant: %d (%d)\n", KeyId, variant);
return KeyId;
}
if (TGraphKey::IsIsomorph(GKey, GraphH.GetKey(KeyId), GSzToPermH.GetDat(Nodes))) {
//printf(" isomorphic to: %d\n", KeyId);
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 |
Definition at line 328 of file ghash.h.
References TStr::CStr(), TStr::Fmt(), TGraphKey::GetEdges(), TGraphKey::GetNodes(), TSnap::TSnapDetail::GVizDoLayout(), gvlDot, IAssert, and TGraphKey::SaveGViz().
{
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 |
Definition at line 337 of file ghash.h.
References TStr::CStr(), TStr::Fmt(), FNm, TGraphKey::GetEdges(), TGraphKey::GetNodes(), TExeTm::GetTmStr(), TSnap::TSnapDetail::GVizDoLayout(), gvlDot, IAssert, TVec< TVal >::Len(), and TGraphKey::SaveGViz().
{
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] |
Definition at line 114 of file ghash.h.
{ return GraphH.FNextKeyId(KeyId); }
Definition at line 94 of file ghash.h.
Referenced by TDGHashGraphCounter::operator()().

| void TGHash< TDat >::GetDatKeyPrV | ( | TVec< TPair< TDat, TGraphKey > > & | DatKeyPrV | ) | const [inline] |
Definition at line 120 of file ghash.h.
{ GraphH.GetDatKeyPrV(DatKeyPrV); }
| void TGHash< TDat >::GetKeyDatPrV | ( | TVec< TPair< TGraphKey, TDat > > & | KeyDatPrV | ) | const [inline] |
Definition at line 119 of file ghash.h.
{ GraphH.GetKeyDatPrV(KeyDatPrV); }
Definition at line 93 of file ghash.h.
Referenced by TGHash< TUInt64 >::GetDat().
{ int k=IsGetKeyId(Graph); IAssert(k!=-1); return k; }

| void TGHash< TDat >::GetKeyIdByDat | ( | TIntV & | KeyIdV, |
| const bool & | Asc = true |
||
| ) | const |
Definition at line 302 of file ghash.h.
References TVec< TVal >::Add(), TVec< TVal >::Gen(), Len(), and TVec< TVal >::Sort().
{
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 |
Definition at line 315 of file ghash.h.
References TVec< TVal >::Add(), TVec< TVal >::Gen(), Len(), and TVec< TVal >::Sort().
{
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] |
Definition at line 85 of file ghash.h.
{ return GraphH.GetMxKeyIds(); }
| bool TGHash< TDat >::GetNodeMap | ( | const PNGraph & | Graph, |
| TIntPrV & | NodeMapV | ||
| ) | const |
Definition at line 255 of file ghash.h.
{
int KeyId;
return GetNodeMap(Graph, NodeMapV, KeyId);
}
| bool TGHash< TDat >::GetNodeMap | ( | const PNGraph & | Graph, |
| TIntPrV & | NodeMapV, | ||
| int & | KeyId | ||
| ) | const |
Definition at line 261 of file ghash.h.
References TVec< TVal >::Add(), TNGraph::BegNI(), TVec< TVal >::Clr(), Fail, TVec< TVal >::GetDat(), TNGraph::TNodeI::GetId(), TNGraph::GetNodes(), TSnap::GetTreeSig(), IAssert, TGraphKey::IsIsomorph(), TSnap::IsTree(), TVec< TVal >::Len(), TGraphKey::SetVariant(), TGraphKey::TakeGraph(), and TGraphKey::TakeSig().
{
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;
}

Definition at line 77 of file ghash.h.
{ return HashOnlyTrees; }
| void TGHash< TDat >::InitPermutations | ( | ) | [private] |
Definition at line 132 of file ghash.h.
References TVec< TVal >::Add(), TVec< TVal >::NextPerm(), and TVec< TVal >::Pack().
Referenced by TGHash< TDat >::TGHash().
{
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] |
Definition at line 84 of file ghash.h.
{ return GraphH.IsAutoSize(); }
| int TGHash< TDat >::IsGetKeyId | ( | const PNGraph & | Graph | ) | const [private] |
Definition at line 218 of file ghash.h.
Referenced by TGHash< TUInt64 >::GetKeyId(), and TGHash< TUInt64 >::IsKey().
{
TGraphKey GKey;
return IsGetKeyId(Graph, GKey);
}

| int TGHash< TDat >::IsGetKeyId | ( | const PNGraph & | Graph, |
| TGraphKey & | GKey | ||
| ) | const [private] |
Definition at line 224 of file ghash.h.
References Fail, TGraphKey::GetNodes(), TSnap::GetTreeSig(), IAssert, TGraphKey::IsIsomorph(), TSnap::IsTree(), TGraphKey::SetVariant(), TGraphKey::TakeGraph(), and TGraphKey::TakeSig().
{
if (HashOnlyTrees) {
int RootNId; IAssert(TSnap::IsTree(Graph, RootNId));
TIntV TreeSig; TSnap::GetTreeSig(Graph, RootNId, TreeSig);
GKey = TGraphKey(TreeSig);
const int KeyId = GraphH.GetKeyId(GKey);
//IAssert(KeyId != -1);
return KeyId;
} else {
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);
//IAssert(KeyId != -1);
if (TGraphKey::IsIsomorph(GKey, GraphH.GetKey(KeyId), GSzToPermH.GetDat(Nodes))) { return KeyId; }
}
return -1;
} else {
const int KeyId = GraphH.GetKeyId(GKey);
//IAssert(KeyId != -1);
return KeyId;
}
}
Fail;
return -1;
}

| int TGHash< TDat >::IsGetKeyId | ( | const PNGraph & | Graph, |
| TGraphKey & | GKey, | ||
| TIntPrV & | NodeMap | ||
| ) | const [private] |
Definition at line 92 of file ghash.h.
Referenced by TDGHashGraphCounter::operator()().
{ int k=IsGetKeyId(Graph); return k!=-1; }

| bool TGHash< TDat >::IsKeyGetDat | ( | const TGraphKey & | Key, |
| TDat & | Dat | ||
| ) | const [inline] |
Definition at line 108 of file ghash.h.
{ return GraphH.IsKeyGetDat(Key, Dat); }
| bool TGHash< TDat >::IsKeyIdEqKeyN | ( | ) | const [inline] |
Definition at line 86 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 164 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 |
Definition at line 371 of file ghash.h.
References TStr::CStr(), F(), GetStr(), and TVec< TVal >::Len().
{
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 |
Definition at line 353 of file ghash.h.
References TStr::CStr(), F(), TGraphKey::GetEdges(), TGraphKey::GetNodes(), and TVec< TVal >::Len().
{
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);
}

Definition at line 58 of file ghash.h.
Referenced by TGHash< TUInt64 >::AddDat(), TGHash< TUInt64 >::BegI(), TGHash< TUInt64 >::Clr(), TGHash< TUInt64 >::Defrag(), TGHash< TUInt64 >::Empty(), TGHash< TUInt64 >::EndI(), TGHash< TUInt64 >::FNextKeyId(), TGHash< TUInt64 >::Gen(), TGHash< TUInt64 >::GetDat(), TGHash< TUInt64 >::GetDatId(), TGHash< TUInt64 >::GetDatKeyPrV(), TGHash< TUInt64 >::GetDatV(), TGHash< TUInt64 >::GetI(), TGHash< TUInt64 >::GetKey(), TGHash< TUInt64 >::GetKeyDat(), TGHash< TUInt64 >::GetKeyDatPrV(), TGHash< TUInt64 >::GetKeyId(), TGHash< TUInt64 >::GetKeyV(), TGHash< TUInt64 >::GetMxKeyIds(), TGHash< TUInt64 >::GetPorts(), TGHash< TUInt64 >::IsAutoSize(), TGHash< TUInt64 >::IsKey(), TGHash< TUInt64 >::IsKeyGetDat(), TGHash< TUInt64 >::IsKeyId(), TGHash< TUInt64 >::IsKeyIdEqKeyN(), TGHash< TUInt64 >::Len(), TGHash< TUInt64 >::operator()(), TGHash< TUInt64 >::operator[](), and TGHash< TUInt64 >::Pack().
TBool TGHash< TDat >::HashOnlyTrees [private] |
Definition at line 57 of file ghash.h.
Referenced by TGHash< TUInt64 >::HashTrees(), and TGHash< TDat >::TGHash().
TInt TGHash< TDat >::MxIsoCheck [private] |
TInt TGHash< TDat >::MxSvdGraph [private] |