3 #define Kilo(n) (1000*(n)) 
    4 #define Mega(n) (1000*1000*(n)) 
    5 #define Giga(n) (1000*1000*1000*(n)) 
   38 template <
class TGraph> 
struct IsBipart     { 
enum { 
Val = 0 }; };
 
   41 #define HasGraphFlag(TGraph, Flag) \ 
   42   ((Flag)==gfDirected ? TSnap::IsDirected<TGraph::TNet>::Val : \ 
   43   (Flag)==gfMultiGraph ? TSnap::IsMultiGraph<TGraph::TNet>::Val : \ 
   44   (Flag)==gfNodeDat ? TSnap::IsNodeDat<TGraph::TNet>::Val : \ 
   45   (Flag)==gfEdgeDat ? TSnap::IsEdgeDat<TGraph::TNet>::Val : \ 
   46   (Flag)==gfSources ? TSnap::IsSources<TGraph::TNet>::Val : \ 
   47   (Flag)==gfBipart ? TSnap::IsBipart<TGraph::TNet>::Val : 0) 
   53 template<
class TDerivClass, 
class TBaseClass>
 
   56   struct Yes { 
char a[1]; };
 
   57   struct No { 
char a[10]; };
 
   58   static Yes Test(TBaseClass*);
 
   61   enum { Val = 
sizeof(Test(static_cast<TDerivClass*>(0))) == 
sizeof(Yes) ? 1 : 0 };
 
   73 template <
class PGraph> 
void PrintInfo(
const PGraph& Graph, 
const TStr& Desc=
"", 
const TStr& OutFNm=
"", 
const bool& Fast=
true);
 
   79 template <
class PGraph> 
int64 GetTriads(
const PGraph& Graph, 
int64& ClosedTriads, 
int64& OpenTriads, 
int SampleNodes=-1);
 
   80 template <
class PGraph> 
double GetBfsEffDiam(
const PGraph& Graph, 
const int& NTestNodes, 
const bool& IsDir, 
double& EffDiam, 
int& FullDiam);
 
   81 template <
class PGraph> 
double GetMxWccSz(
const PGraph& Graph);
 
   82 template <
class PGraph> 
double GetMxSccSz(
const PGraph& Graph);
 
   86 template <
class PGraph>
 
   87 void PrintInfo(
const PGraph& Graph, 
const TStr& Desc, 
const TStr& OutFNm, 
const bool& Fast) {
 
   88   int BiDirEdges=0, ZeroNodes=0, ZeroInNodes=0, ZeroOutNodes=0, SelfEdges=0, NonZIODegNodes=0;
 
   91   if (! OutFNm.
Empty()) F = fopen(OutFNm.
CStr(), 
"wt");
 
   92   if (! Desc.
Empty()) { fprintf(F, 
"%s:", Desc.
CStr()); }
 
   93   else { fprintf(F, 
"Graph:"); }
 
   99   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  100     if (NI.GetDeg()==0) ZeroNodes++;
 
  101     if (NI.GetInDeg()==0) ZeroInNodes++;
 
  102     if (NI.GetOutDeg()==0) ZeroOutNodes++;
 
  103     if (NI.GetInDeg()!=0 && NI.GetOutDeg()!=0) NonZIODegNodes++;
 
  104     if (! Fast || Graph->GetNodes() < 1000) {
 
  105       const int NId = NI.GetId();
 
  107         const int DstNId = NI.GetOutNId(
edge);
 
  108         if (Graph->IsEdge(DstNId, NId)) BiDirEdges++;
 
  109         if (NId == DstNId) SelfEdges++;
 
  115   int64 Closed=0, Open=0;
 
  116   double WccSz=0, SccSz=0;
 
  127   fprintf(F, 
"  Nodes:                    %d\n", Graph->GetNodes());
 
  128   fprintf(F, 
"  Edges:                    %d\n", Graph->GetEdges());
 
  129   fprintf(F, 
"  Zero Deg Nodes:           %d\n", ZeroNodes);
 
  130   fprintf(F, 
"  Zero InDeg Nodes:         %d\n", ZeroInNodes);
 
  131   fprintf(F, 
"  Zero OutDeg Nodes:        %d\n", ZeroOutNodes);
 
  132   fprintf(F, 
"  NonZero In-Out Deg Nodes: %d\n", NonZIODegNodes);
 
  134     fprintf(F, 
"  Unique directed edges:    %d\n", UniqDirE.
Len());
 
  135     fprintf(F, 
"  Unique undirected edges:  %d\n", UniqUnDirE.
Len());
 
  136     fprintf(F, 
"  Self Edges:               %d\n", SelfEdges);
 
  137     fprintf(F, 
"  BiDir Edges:              %d\n", BiDirEdges);
 
  140     fprintf(F, 
"  Frac. of closed triads:   %f\n", Closed/
double(Closed+Open));
 
  141     fprintf(F, 
"  Connected component size: %f\n", WccSz);
 
  142     fprintf(F, 
"  Strong conn. comp. size:  %f\n", SccSz);
 
  143     fprintf(F, 
"  Approx. full diameter:    %d\n", FullDiam);
 
  144     fprintf(F, 
"  90%% effective diameter:  %f\n", EffDiam);
 
  150   if (! OutFNm.
Empty()) { fclose(F); }
 
  157 template <
class TVal>
 
  164   TSnapQueue() : MxFirst(1024), First(0), Last(0), ValV(MxFirst, 0) { }
 
  166   TSnapQueue(
const int& MxVals) : MxFirst(1024+MxVals/10), First(0), Last(0), ValV(
TInt::GetMx(MxFirst, MxVals), 0) { }
 
  167   TSnapQueue(
const int& MxVals, 
const int& MaxFirst) : MxFirst(MaxFirst),
 
  168     First(0), Last(0), ValV(
TInt::GetMx(MxFirst, MxVals), 0) { }
 
  169   TSnapQueue(
const TSnapQueue& Queue) : MxFirst(Queue.MxFirst), First(Queue.First), Last(Queue.Last), ValV(Queue.ValV) { }
 
  171   explicit TSnapQueue(
TSIn& SIn): MxFirst(SIn), First(SIn), Last(SIn), ValV(SIn) { }
 
  176     First=Queue.
First; Last=Queue.
Last; ValV=Queue.
ValV; } 
return *
this; }
 
  178   const TVal& 
operator[](
const int& ValN)
 const { 
return ValV[First+ValN]; }
 
  182     const int size = Last - 
First;
 
  185     for (
int i = 0; i < num && i < size; ++i) {
 
  186       loc = Rnd.GetUniDevInt(size - i) + First + i;
 
  188       ValV[loc] = ValV[First + i];
 
  189       ValV[First + i] = temp;
 
  194   void Clr(
const bool& DoDel=
true) { ValV.
Clr(DoDel);  First=Last=0; }
 
  195   void Gen(
const int& MxVals, 
const int& MaxFirst=1024) {
 
  196     MxFirst=MaxFirst; First=Last=0; ValV.
Gen(MxVals, 0); }
 
  212     if (First==Last) { ValV.
Clr(
false); First=Last=0; } }
 
  215     if (First>0 && (First > MxFirst || ValV.
Len() == ValV.
Reserved()) && ! ValV.
Empty()) {
 
  217       memmove(ValV.
BegI(), ValV.
GetI(First), 
sizeof(TVal)*
Len());
 
  222     Last++;  ValV.
Add(Val);
 
  241   TUnionFind(
const int& ExpectKeys) : KIdSetH(ExpectKeys, true) { }
 
  246   int Len()
 const { 
return KIdSetH.
Len(); }
 
  248   bool IsKey(
const int& Key)
 const { 
return KIdSetH.
IsKey(Key); }
 
  252   int Find(
const int& Key);
 
  256   void Union(
const int& Key1, 
const int& Key2);
 
  268 template <
class TVal, 
class TCmp = TLss<TVal> >
 
  274   void PushHeap(
const int& First, 
int HoleIdx, 
const int& Top, TVal Val);
 
  275   void AdjustHeap(
const int& First, 
int HoleIdx, 
const int& 
Len, TVal Val);
 
  279   THeap(
const int& MxVals) : Cmp(), HeapV(MxVals, 0) { }
 
  283   THeap(
const THeap& Heap) : Cmp(Heap.Cmp), HeapV(Heap.HeapV) { }
 
  287   const TVal& 
TopHeap()
 const { 
return HeapV[0]; }
 
  301   void Add(
const TVal& Val) { HeapV.
Add(Val); }
 
  306 template <
class TVal, 
class TCmp>
 
  309   PushHeap(0, HeapV.Len()-1, 0, Val);
 
  312 template <
class TVal, 
class TCmp>
 
  315   const TVal Top = HeapV[0];
 
  316   HeapV[0] = HeapV.Last();
 
  318   if (! HeapV.Empty()) {
 
  319     AdjustHeap(0, 0, HeapV.Len(), HeapV[0]);
 
  324 template <
class TVal, 
class TCmp>
 
  326   int Parent = (HoleIdx-1)/2;
 
  327   while (HoleIdx > Top && 
Cmp(HeapV[First+Parent], Val)) {
 
  328     HeapV[First+HoleIdx] = HeapV[First+Parent];
 
  329     HoleIdx = Parent;  Parent = (HoleIdx-1)/2;
 
  331   HeapV[First+HoleIdx] = Val;
 
  334 template <
class TVal, 
class TCmp>
 
  336   const int Top = HoleIdx;
 
  337   int Right = 2*HoleIdx+2;
 
  338   while (Right < Len) {
 
  339     if (
Cmp(HeapV[First+Right], HeapV[First+Right-1])) { Right--; }
 
  340     HeapV[First+HoleIdx] = HeapV[First+Right];
 
  341     HoleIdx = Right;  Right = 2*(Right+1); }
 
  343     HeapV[First+HoleIdx] = HeapV[First+Right-1];
 
  345   PushHeap(First, HoleIdx, Top, Val);
 
  348 template <
class TVal, 
class TCmp>
 
  350   if (Len < 2) { 
return; }
 
  351   int Parent = (Len-2)/2;
 
  353     AdjustHeap(First, Parent, Len, HeapV[First+Parent]);
 
  354     if (Parent == 0) { 
return; }
 
int GetLast() const 
Returns the location of the last element in the queue. 
 
TPair< TInt, TInt > TIntPr
 
Tests (at compile time) if the graph is a network with data on nodes. 
 
Main namespace for all the Snap global entities. 
 
int64 GetTriads(const PGraph &Graph, int64 &ClosedTriads, int64 &OpenTriads, int SampleNodes=-1)
Computes the number of Closed and Open triads. 
 
TSizeTy Reserved() const 
Returns the size of allocated storage capacity. 
 
void MakeHeap()
Builds a heap from a set of elements. 
 
int GetKCoreNodes(const PGraph &Graph, TIntPrV &CoreIdSzV)
Returns the number of nodes in each core of order K (where K=0, 1, ...) 
 
int Add(const int &Key)
Adds an element Key to the structure. 
 
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes. 
 
TSnapQueue(TSIn &SIn)
Constructor that loads the queue from a (binary) stream SIn. 
 
void Union(const int &Key1, const int &Key2)
Merges sets with elements Key1 and Key2. 
 
bool Empty() const 
Tests whether the heap is empty. 
 
void Add(const TVal &Val)
Adds an element to the data structure. Heap property is not maintained by Add() and thus after all th...
 
void Del(const TSizeTy &ValN)
Removes the element at position ValN. 
 
double GetBfsEffDiam(const PGraph &Graph, const int &NTestNodes, const bool &IsDir=false)
Returns the (approximation of the) Effective Diameter (90-th percentile of the distribution of shorte...
 
const TVal & TopHeap() const 
Returns a reference to the element at the top of the heap (the largest element of the heap)...
 
void Save(TSOut &SOut) const 
 
double GetMxWccSz(const PGraph &Graph)
Returns the fraction of nodes in the largest weakly connected component of a Graph. 
 
static int GetMx(const int &Int1, const int &Int2)
 
TSnapQueue & operator=(const TSnapQueue &Queue)
 
Tests (at compile time) if the graph is directed. 
 
const TVal & operator[](const int &ValN) const 
Returns the value of the ValN element in the queue, but does not remove the element. 
 
TSizeTy Len() const 
Returns the number of elements in the vector. 
 
TUnionFind & operator=(const TUnionFind &UF)
 
void Gen(const int &MxVals, const int &MaxFirst=1024)
 
const TDat & GetDat(const TKey &Key) const 
 
TAttrType_
Types for tables, sparse and dense attributes. 
 
void Save(TSOut &SOut) const 
 
bool Empty() const 
Tests whether the vector is empty. 
 
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet 
 
void Pop()
Removes the first element from the queue. 
 
TStr GetFlagStr(const TGraphFlag &GraphFlag)
Returns a string representation of a flag. 
 
bool Empty() const 
Tests whether the queue is empty (contains no elements). 
 
THash< TInt, TIntPr > KIdSetH
 
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector. 
 
bool IsKey(const int &Key) const 
Returns true if the structure contains element Key. 
 
int Find(const int &Key)
Returns the set that contains element Key. 
 
static int GetMn(const int &Int1, const int &Int2)
 
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
 
Tests (at compile time) if the graph is a network with data on edges. 
 
int GetKeyI(const int &KeyN) const 
Returns the element KeyN. 
 
network with data on edges 
 
double GetMxSccSz(const PGraph &Graph)
Returns the fraction of nodes in the largest strongly connected component of a Graph. 
 
Simple heap data structure. 
 
THeap(const TVec< TVal > &Vec, const TCmp &_Cmp)
 
Tests (at compile time) if the graph is a multigraph with multiple edges between the same nodes...
 
int GetKCoreEdges(const PGraph &Graph, TIntPrV &CoreIdSzV)
Returns the number of edges in each core of order K (where K=0, 1, ...) 
 
TUnionFind(const TUnionFind &UnionFind)
 
const TVec< TVal > & operator()() const 
Returns a reference to the vector containing the elements of the heap. 
 
int Len() const 
Returns the number of elements in the structure. 
 
int Len() const 
Returns the number of elements in the queue. 
 
TInt & Rank(const int &Key)
Returns the rank of element Key. 
 
void Clr(const bool &DoDel=true)
Deletes all elements from the queue. 
 
int GetFirst() const 
Returns the location of the first element in the queue. 
 
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph 
 
TSnapQueue(const int &MxVals)
Constructor that reserves enough memory for a queue with MxVals elements. 
 
int AddKey(const TKey &Key)
 
void Dump()
Prints out the structure to standard output. 
 
void Sample(const int num, TRnd &Rnd=TInt::Rnd)
 
sentinel, last value for iteration 
 
void PushHeap(const int &First, int HoleIdx, const int &Top, TVal Val)
 
TIter BegI() const 
Returns an iterator pointing to the first element in the vector. 
 
enum TGraphFlag_ TGraphFlag
Graph Flags, used for quick testing of graph types. 
 
void Push(const TVal &Val)
Adds an element at the end of the queue. 
 
THeap & operator=(const THeap &H)
 
Fast Queue used by the TBreathFS (uses memcpy to move objects TVal around). 
 
TInt & Parent(const int &Key)
Returns the parent of element Key. 
 
nodes only store out-edges (but not in-edges). See TBigNet 
 
bool IsSameSet(const int &Key1, const int &Key2)
Returns true if elements Key1 and Key2 are in the same set. 
 
TUnionFind(const int &ExpectKeys)
Constructor that reserves enough memory for ExpectKeys elements. 
 
const TVal & Top() const 
Returns the value of the first element in the queue, but does not remove the element. 
 
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements. 
 
TVal PopHeap()
Removes the top element from the heap. 
 
Tests (at compile time) if the nodes store only out-edges, but not in-edges. 
 
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
 
void AdjustHeap(const int &First, int HoleIdx, const int &Len, TVal Val)
 
network with data on nodes 
 
int Len() const 
Returns the number of elements in the heap. 
 
bool IsKey(const TKey &Key) const 
 
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element. 
 
TIter GetI(const TSizeTy &ValN) const 
Returns an iterator an element at position ValN. 
 
TGraphFlag_
Graph Flags, used for quick testing of graph types. 
 
TDat & AddDat(const TKey &Key)
 
Tests (at compile time) if the graph is a bipartite graph type. 
 
TSnapQueue(const TSnapQueue &Queue)
 
Union Find class (Disjoint-set data structure). 
 
const TKey & GetKey(const int &KeyId) const 
 
TVec< TVal > & operator()()
Returns a reference to the vector containing the elements of the heap. 
 
TSnapQueue(const int &MxVals, const int &MaxFirst)
 
void Save(TSOut &SOut) const 
Saves the queue to a (binary) stream SOut. 
 
THeap(const TVec< TVal > &Vec)
 
void PrintInfo(const PGraph &Graph, const TStr &Desc="", const TStr &OutFNm="", const bool &Fast=true)
Prints basic graph statistics.