3 #define Kilo(n) (1000*(n))
4 #define Mega(n) (1000*1000*(n))
5 #define Giga(n) (1000*1000*1000*(n))
35 template <
class TGraph>
struct IsBipart {
enum {
Val = 0 }; };
38 #define HasGraphFlag(TGraph, Flag) \
39 ((Flag)==gfDirected ? TSnap::IsDirected<TGraph::TNet>::Val : \
40 (Flag)==gfMultiGraph ? TSnap::IsMultiGraph<TGraph::TNet>::Val : \
41 (Flag)==gfNodeDat ? TSnap::IsNodeDat<TGraph::TNet>::Val : \
42 (Flag)==gfEdgeDat ? TSnap::IsEdgeDat<TGraph::TNet>::Val : \
43 (Flag)==gfSources ? TSnap::IsSources<TGraph::TNet>::Val : \
44 (Flag)==gfBipart ? TSnap::IsBipart<TGraph::TNet>::Val : 0)
50 template<
class TDerivClass,
class TBaseClass>
53 struct Yes {
char a[1]; };
54 struct No {
char a[10]; };
55 static Yes Test(TBaseClass*);
58 enum { Val =
sizeof(Test(static_cast<TDerivClass*>(0))) ==
sizeof(Yes) ? 1 : 0 };
70 template <
class PGraph>
void PrintInfo(
const PGraph& Graph,
const TStr& Desc=
"",
const TStr& OutFNm=
"",
const bool& Fast=
true);
76 template <
class PGraph>
int64 GetTriads(
const PGraph& Graph,
int64& ClosedTriads,
int64& OpenTriads,
int SampleNodes=-1);
77 template <
class PGraph>
double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir,
double& EffDiam,
int& FullDiam);
78 template <
class PGraph>
double GetMxWccSz(
const PGraph& Graph);
79 template <
class PGraph>
double GetMxSccSz(
const PGraph& Graph);
83 template <
class PGraph>
84 void PrintInfo(
const PGraph& Graph,
const TStr& Desc,
const TStr& OutFNm,
const bool& Fast) {
85 int BiDirEdges=0, ZeroNodes=0, ZeroInNodes=0, ZeroOutNodes=0, SelfEdges=0, NonZIODegNodes=0;
88 if (! OutFNm.
Empty()) F = fopen(OutFNm.
CStr(),
"wt");
89 if (! Desc.
Empty()) { fprintf(F,
"%s:", Desc.
CStr()); }
90 else { fprintf(F,
"Graph:"); }
96 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
97 if (NI.GetDeg()==0) ZeroNodes++;
98 if (NI.GetInDeg()==0) ZeroInNodes++;
99 if (NI.GetOutDeg()==0) ZeroOutNodes++;
100 if (NI.GetInDeg()!=0 && NI.GetOutDeg()!=0) NonZIODegNodes++;
101 if (! Fast || Graph->GetNodes() < 1000) {
102 const int NId = NI.GetId();
103 for (
int edge = 0; edge < NI.GetOutDeg(); edge++) {
104 const int DstNId = NI.GetOutNId(edge);
105 if (Graph->IsEdge(DstNId, NId)) BiDirEdges++;
106 if (NId == DstNId) SelfEdges++;
112 int64 Closed=0, Open=0;
113 double WccSz=0, SccSz=0;
124 fprintf(F,
" Nodes: %d\n", Graph->GetNodes());
125 fprintf(F,
" Edges: %d\n", Graph->GetEdges());
126 fprintf(F,
" Zero Deg Nodes: %d\n", ZeroNodes);
127 fprintf(F,
" Zero InDeg Nodes: %d\n", ZeroInNodes);
128 fprintf(F,
" Zero OutDeg Nodes: %d\n", ZeroOutNodes);
129 fprintf(F,
" NonZero In-Out Deg Nodes: %d\n", NonZIODegNodes);
131 fprintf(F,
" Unique directed edges: %d\n", UniqDirE.
Len());
132 fprintf(F,
" Unique undirected edges: %d\n", UniqUnDirE.
Len());
133 fprintf(F,
" Self Edges: %d\n", SelfEdges);
134 fprintf(F,
" BiDir Edges: %d\n", BiDirEdges);
137 fprintf(F,
" Frac. of closed triads: %f\n", Closed/
double(Closed+Open));
138 fprintf(F,
" Connected component size: %f\n", WccSz);
139 fprintf(F,
" Strong conn. comp. size: %f\n", SccSz);
140 fprintf(F,
" Approx. full diameter: %d\n", FullDiam);
141 fprintf(F,
" 90%% effective diameter: %f\n", EffDiam);
147 if (! OutFNm.
Empty()) { fclose(F); }
154 template <
class TVal>
179 void Gen(
const int& MxVals,
const int& MaxFirst=1024) {
236 int Find(
const int& Key);
240 void Union(
const int& Key1,
const int& Key2);
252 template <
class TVal,
class TCmp = TLss<TVal> >
258 void PushHeap(
const int& First,
int HoleIdx,
const int& Top, TVal Val);
259 void AdjustHeap(
const int& First,
int HoleIdx,
const int&
Len, TVal Val);
290 template <
class TVal,
class TCmp>
293 PushHeap(0, HeapV.Len()-1, 0, Val);
296 template <
class TVal,
class TCmp>
299 const TVal Top = HeapV[0];
300 HeapV[0] = HeapV.Last();
302 if (! HeapV.Empty()) {
303 AdjustHeap(0, 0, HeapV.Len(), HeapV[0]);
308 template <
class TVal,
class TCmp>
310 int Parent = (HoleIdx-1)/2;
311 while (HoleIdx > Top &&
Cmp(HeapV[First+Parent], Val)) {
312 HeapV[First+HoleIdx] = HeapV[First+Parent];
313 HoleIdx = Parent; Parent = (HoleIdx-1)/2;
315 HeapV[First+HoleIdx] = Val;
318 template <
class TVal,
class TCmp>
320 const int Top = HoleIdx;
321 int Right = 2*HoleIdx+2;
322 while (Right < Len) {
323 if (
Cmp(HeapV[First+Right], HeapV[First+Right-1])) { Right--; }
324 HeapV[First+HoleIdx] = HeapV[First+Right];
325 HoleIdx = Right; Right = 2*(Right+1); }
327 HeapV[First+HoleIdx] = HeapV[First+Right-1];
329 PushHeap(First, HoleIdx, Top, Val);
332 template <
class TVal,
class TCmp>
334 if (Len < 2) {
return; }
335 int Parent = (Len-2)/2;
337 AdjustHeap(First, Parent, Len, HeapV[First+Parent]);
338 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.
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.
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
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.
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)
Vector is a sequence TVal objects representing an array that can change in size.
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.