17 TGraphKey() : Nodes(-1), EdgeV(), SigV(), VariantId(0) { }
47 void SetVariant(
const int& Variant) { VariantId = Variant; }
65 void TakeSig(
const PNGraph& Graph,
const int& MnSvdGraph,
const int& MxSvdGraph);
102 template <
class TDat>
126 TGHash(
const bool&
HashTrees,
const int& MaxIsoCheck=8,
const int& MaxSvdGraph=500);
131 const TDat&
operator [] (
const int& KeyId)
const {
return GraphH[KeyId]; }
143 TIter
GetI(
const int& KeyId)
const {
return GraphH.
GetI(KeyId); }
149 void Gen(
const int& ExpectVals) { GraphH.
Gen(ExpectVals); }
154 void Clr(
const bool& DoDel=
true,
const int& NoDelLim=-1) { GraphH.
Clr(DoDel, NoDelLim); }
221 const TDat&
GetDatId(
const int& KeyId)
const {
return GraphH[KeyId]; }
225 TDat&
GetDatId(
const int& KeyId) {
return GraphH[KeyId]; }
277 void DrawGViz(
const int& KeyId,
const TStr& OutFNmPref,
const TStr& OutputType =
"gif",
TStr Desc=
"")
const;
281 void SaveTxt(
const TStr& OutFNm,
const TStr& Desc,
const TStr& DatColNm,
const bool& SortByKeyVal=
true)
const;
286 template <
class TDat>
289 for (
int nodes = 2; nodes <= MxIsoCheck; nodes++) {
291 TIntV NodeIdV(nodes, 0);
292 for (
int i = 0; i < nodes; i++) NodeIdV.
Add(i);
294 NodePermutationV.
Add(NodeIdV);
296 NodePermutationV.
Add(NodeIdV);
298 NodePermutationV.
Pack();
299 GSzToPermH.AddDat(nodes, NodePermutationV);
303 template <
class TDat>
305 MxIsoCheck(MaxIsoCheck), MxSvdGraph(MaxSvdGraph), GSzToPermH(), HashOnlyTrees(HashTrees), GraphH() {
311 template <
class TDat>
318 template <
class TDat>
320 MxIsoCheck.Save(SOut);
321 MxSvdGraph.Save(SOut);
322 HashOnlyTrees.Save(SOut);
326 template <
class TDat>
332 const int KeyId = GraphH.GetKeyId(GKey);
335 return GraphH.AddKey(GKey);
340 GKey.
TakeSig(Graph, MxIsoCheck+1, MxSvdGraph);
342 if (Nodes > 2 && Nodes <= MxIsoCheck) {
345 for (
int variant = 1; ; variant++) {
347 int KeyId = GraphH.GetKeyId(GKey);
349 KeyId = GraphH.AddKey(GKey);
357 const int KeyId = GraphH.GetKeyId(GKey);
360 return GraphH.AddKey(GKey);
369 template <
class TDat>
372 return IsGetKeyId(Graph, GKey);
375 template <
class TDat>
382 const int KeyId = GraphH.GetKeyId(GKey);
386 GKey.
TakeSig(Graph, MxIsoCheck+1, MxSvdGraph);
388 if (Nodes > 2 && Nodes <= MxIsoCheck) {
390 for (
int variant = 1; ; variant++) {
392 int KeyId = GraphH.GetKeyId(GKey);
393 if (KeyId == -1) {
return -1; }
398 const int KeyId = GraphH.GetKeyId(GKey);
406 template <
class TDat>
409 return GetNodeMap(Graph, NodeMapV, KeyId);
412 template <
class TDat>
419 KeyId = GraphH.GetKeyId(GKey);
422 const int Nodes = Graph->
GetNodes();
425 if (Nodes == 0) {
return true; }
426 else if (Nodes == 1) {
428 else if (Nodes <= MxIsoCheck) {
430 GKey.
TakeSig(Graph, MxIsoCheck+1, MxSvdGraph);
432 for (
int variant = 1; ; variant++) {
434 KeyId = GraphH.GetKeyId(GKey);
435 if (KeyId == -1) {
return false; }
437 const TIntV& K1K2Perm = GSzToPermH.
GetDat(Nodes)[IsoPermId];
439 for (
int i = 0; i < NodeMapV.
Len(); i++) {
440 NodeMapV[i].Val2 = K1K2Perm[NodeMapV[i].Val2]; }
453 template <
class TDat>
456 for (
int i = FFirstKeyId(); FNextKeyId(i); ) {
460 KeyIdV.
Gen(Len(), 0);
461 for (
int i = 0; i < Len(); i++) {
462 KeyIdV.
Add(DatKeyIdV[i].Val4);
466 template <
class TDat>
469 for (
int i = FFirstKeyId(); FNextKeyId(i); ) {
473 KeyIdV.
Gen(Len(), 0);
474 for (
int i = 0; i < Len(); i++) {
475 KeyIdV.
Add(DatKeyIdV[i].Val4);
479 template <
class TDat>
481 IAssert(OutputType ==
"ps" || OutputType ==
"gif" || OutputType ==
"png");
484 GKey.
SaveGViz(OutFNmPref+
".dot", Desc1);
488 template <
class TDat>
490 IAssert(OutputType ==
"ps" || OutputType ==
"gif" || OutputType ==
"png");
492 printf(
"Plotting %d graphs\n", KeyIdV.
Len());
493 for (
int i = 0; i < KeyIdV.
Len(); i++) {
494 const TStr FNm =
TStr::Fmt(
"%s.%03d.key%d.", OutFNmPref.
CStr(), i+1, KeyIdV[i]());
496 const TGraphKey& GKey = GetKey(KeyIdV[i]);
501 printf(
"done [%s].\n", ExeTm.
GetTmStr());
504 template <
class TDat>
507 if (SortByKeyVal) GetKeyIdByDat(KeyIdV,
false);
508 else GetKeyIdByGSz(KeyIdV,
true);
509 FILE *F = fopen(OutFNm.
CStr(),
"wt");
510 fprintf(F,
"Graph-Hash-Table");
511 fprintf(F,
"%s\n", Desc.
CStr());
512 fprintf(F,
"%d graphs\n", KeyIdV.
Len());
513 fprintf(F,
"Rank\tKeyId\tNodes\tEdges\t%s\n", DatColNm.
CStr());
514 for (
int i = 0; i < KeyIdV.
Len(); i++) {
515 const TGraphKey& Key = GetKey(KeyIdV[i]);
516 fprintf(F,
"%d\t%d\t%d\t%d\t%s\n", i+1, KeyIdV[i](), Key.
GetNodes(), Key.
GetEdges(),
517 GetDatId(KeyIdV[i]).GetStr().CStr());
522 template <
class TDat>
524 TIntV KeyIdV; GetKeyIdByDat(KeyIdV,
false);
525 FILE *F = fopen(OutFNm.
CStr(),
"wt");
526 fprintf(F,
"Graph-Hash-Table\n");
527 fprintf(F,
"%s\n", Desc.
CStr());
528 fprintf(F,
"%d graphs", KeyIdV.
Len());
529 for (
int i = 0; i < KeyIdV.
Len(); i++) {
530 fprintf(F,
"\n\n[%5d]\tRank: %d\n", KeyIdV[i](), i+1);
531 fprintf(F,
"Dat: %s\n", GetDat(KeyIdV[i]).GetStr().CStr());
532 GetDatId(KeyIdV[i]).SaveTxt(F);
573 void RecurBfs1(
const int& NId,
const int& Depth);
int GetEdges() const
Returns the number of edges in the graph.
void TakeSig(const PNGraph &Graph, const int &MnSvdGraph, const int &MxSvdGraph)
Creates a signature for a given directed graph.
int Len() const
Returns the number of keys in the hash table.
TDat & GetDatId(const int &KeyId)
Returns data at a given position index KeyId.
TPair< TInt, TInt > TIntPr
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.
const TGraphKey & GetKey(const int &KeyId) const
Returns the GraphKey with position index KeyId.
Connected Sub-graph Enumeration.
bool IsKeyIdEqKeyN() const
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
TIter EndI() const
Returns iterator to one past the last element of the hash table.
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...
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...
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
void GetDatV(TVec< TDat > &DatV) const
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 GetKeyDat(const int &KeyId, TGraphKey &Key, TDat &Dat) const
Returns Key and Data at a given position index KeyId.
bool operator<(const TSimpleGraph &Graph) const
Simple directed/undirected graph defined by its edges.
TGHash(const bool &HashTrees, const int &MaxIsoCheck=8, const int &MaxSvdGraph=500)
Default contructor.
bool operator==(const TGraphKey &GraphKey) const
THash< TGraphKey, TDat >::TIter TIter
bool IsAutoSize() const
Tests whether the hash table automatically adjusts the number of ports based on the number of keys...
bool IsKeyId(const int &KeyId) const
void GetKeyDat(const int &KeyId, TKey &Key, TDat &Dat) const
void GetKeyIdByGSz(TIntV &KeyIdV, const bool &Asc=true) const
Returns a vector of KeyIds of hash table elements sorted by their graph size.
TSizeTy Len() const
Returns the number of elements in the vector.
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
THash< TInt, TVec< TIntV > > GSzToPermH
void Gen(const int &ExpectVals)
Initializes the hash table for the expected number of keys ExpectVals.
int GetPorts() const
Returns the number of ports in the hash table.
const TDat & GetDat(const PNGraph &Graph) const
Returns the data associated with key Graph.
bool FNextKeyId(int &KeyId) const
Finds next KeyId.
int GetNodes() const
Returns the number of nodes in the graph.
const TDat & GetDat(const TKey &Key) const
bool Join(const TSimpleGraph &G1, const TSimpleGraph &G2)
static bool IsIsomorph(const TGraphKey &Key1, const TGraphKey &Key2, const TIntV &NodeIdMap)
Checks whether directed graph Key1 is isomorphic to the directed graph Key2 under node Id permutation...
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Removes all the elements from the hash table.
void GetDatKeyPrV(TVec< TPair< TDat, TKey > > &DatKeyPrV) const
void EnumSubGraphs(const int &MaxEdges)
void GetTreeSig(const PGraph &Graph, const int &RootNId, TIntV &Sig)
TVec< TSimpleGraph > TSimpleGraphV
void Save(TSOut &SOut) const
void AddEdge(const int &SrcNId, const int &DstNId)
const char * GetTmStr() const
bool IsKeyGetDat(const TGraphKey &Key, TDat &Dat) const
Test whether Key exists and sets its data to Dat.
THash< TIntPr, TIntH > EdgeH
void GVizDoLayout(const TStr &GraphInFNm, TStr OutFNm, const TGVizLayout &Layout)
Runs GraphViz layout engine over a graph saved in the file GraphInFNm with output saved to OutFNm...
bool IsTree(const PGraph &Graph, int &RootNIdX)
TGraphKey & operator=(const TGraphKey &GraphKey)
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
bool IsKeyIdEqKeyN() const
Tests whether there are any unused slots in the hash table.
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
void Gen(const int &ExpectVals)
bool NextPerm()
Generates next permutation of the elements in the vector.
bool IsKey(const PNGraph &Graph) const
Test whether Graph is an existing key in the hash table.
void RecurBfs(const int &MxDepth)
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
const TDat & operator()(const TGraphKey &Key) const
Accesses the data of graph-key Key.
bool FNextKeyId(int &KeyId) const
int GetVariant() const
Returns the graph variant Id.
bool IsKeyGetDat(const TKey &Key, TDat &Dat) const
const TDat & GetDat(const TGraphKey &Key) const
Returns data with a given graph Key.
void Dump(const TStr &Desc=TStr()) const
bool operator==(const TSimpleGraph &Graph) const
TDat & AddDat(const PNGraph &Graph)
Adds a key Graph to the table and returns its data value.
int GetSigLen() const
Returns the length of the signature vector of a graph.
void Save(TSOut &SOut) const
void TakeGraph(const PNGraph &Graph)
Creates a key from a given directed graph.
TIter BegI() const
Returns iterator to the first element of the hash table.
int GetNodes() const
Returns the number of nodes in the graph.
bool IsKey(const TGraphKey &Key, int &KeyId) const
Tests whether a given Key exists in the hash table.
void Pack()
Frees the unused memory by the hash table.
const TDat & GetDatId(const int &KeyId) const
Returns data at a given position index KeyId.
int GetKeyId(const TKey &Key) const
void GetKeyDatPrV(TVec< TPair< TGraphKey, TDat > > &KeyDatPrV) const
Returns a vector of pairs (Key, Data) elements stored in the hash table.
void Defrag()
Removes unused slots from the hash table.
PNGraph GetNGraph() const
Returns the directed graph stored in the GraphKey object.
int FFirstKeyId() const
Finds first KeyId.
void SaveGViz(const TStr &OutFNm, const TStr &Desc=TStr(), const TStr &NodeAttrs="", const int &Size=-1) const
Saves the graph to the .DOT file format used by GraphViz.
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.
int GetId() const
Returns ID of the current node.
bool Empty() const
Tests whether the hash table is empty.
int IsGetKeyId(const PNGraph &Graph) const
void GetKeyV(TVec< TKey > &KeyV) const
void SetEdgeV(const TIntPrV &EdgeIdV)
Returns a vector of directed edges of a graph.
int GetMxKeyIds() const
Returns the maximum key Id of any element in the hash table.
static TStr Fmt(const char *FmtStr,...)
void Pack()
Reduces vector capacity (frees memory) to match its size.
bool IsKeyId(const int &KeyId) const
Tests whether there exists a key at given position index KeyId.
TSubGraphsEnum(PNGraph Graph)
int GetPrimHashCd() const
void RecurBfs1(const int &MxDepth)
void GetDatKeyPrV(TVec< TPair< TDat, TGraphKey > > &DatKeyPrV) const
Returns a vector of pairs (Data, Key) elements stored in the hash table.
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
int GetKeyId(const PNGraph &Graph) const
Returns the KeyId (position index) of key Graph.
THash< TGraphKey, TDat > GraphH
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
bool IsKey(const TGraphKey &Key) const
Tests whether a given Key exists in the hash table.
void SetVariant(const int &Variant)
Sets the Variant Id of a given graph.
void DrawGViz(const TStr &OutFNm, const TStr &Desc=TStr(), const TStr &NodeAttrs="", const int &Size=-1) const
Saves the graph to the .DOT file format and calls GraphViz to draw it.
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
bool HashTrees() const
Returns whether the hash table only hashes trees and not arbitrary directed graphs.
const TDat & operator[](const int &KeyId) const
Accesses the data at hash table position index KeyId.
int AddKey(const PNGraph &Graph)
Adds a key Graph to the table and returns its KeyId.
void SaveTxt(FILE *F) const
Saves the graph as a list of edges.
TIter GetI(const int &KeyId) const
Returns iterator to a key at position index KeyId.
bool IsKey(const TKey &Key) const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
void GetKeyIdByDat(TIntV &KeyIdV, const bool &Asc=true) const
Returns a vector of KeyIds of hash table elements sorted by their data value.
TDat & GetDat(const TGraphKey &Key)
Returns data with a given graph Key.
int GetKeyId(const TGraphKey &Key) const
Returns the KeyId for a given Key.
void GetDatV(TVec< TDat > &DatV) const
Returns a vector of data elements stored in the hash table.
TDat & GetDat(const PNGraph &Graph)
Returns the data associated with key Graph.
const TKey & GetKey(const int &KeyId) const
void GetKeyV(TVec< TGraphKey > &KeyV) const
Returns a vector of keys stored in the hash table.
TIter GetI(const TKey &Key) const
TSimpleGraph(const TIntPrV &GEdgeV)