7 template <
class PGraph> 
int CntInDegNodes(
const PGraph& Graph, 
const int& NodeInDeg);
 
    9 template <
class PGraph> 
int CntOutDegNodes(
const PGraph& Graph, 
const int& NodeOutDeg);
 
   11 template <
class PGraph> 
int CntDegNodes(
const PGraph& Graph, 
const int& NodeDeg);
 
   13 template <
class PGraph> 
int CntNonZNodes(
const PGraph& Graph);
 
   15 template <
class PGraph> 
int CntEdgesToSet(
const PGraph& Graph, 
const int& NId, 
const TIntSet& NodeSet);
 
   18 template <
class PGraph> 
int GetMxDegNId(
const PGraph& Graph);
 
   20 template <
class PGraph> 
int GetMxInDegNId(
const PGraph& Graph);
 
   34 template <
class PGraph> 
void GetDegCnt(
const PGraph& Graph, 
TIntPrV& DegToCntV);
 
   36 template <
class PGraph> 
void GetDegCnt(
const PGraph& Graph, 
TFltPrV& DegToCntV);
 
   38 template <
class PGraph> 
void GetDegSeqV(
const PGraph& Graph, 
TIntV& DegV);
 
   54 template <
class PGraph> 
int CntSelfEdges(
const PGraph& Graph);
 
   59 template <
class PGraph> PGraph 
GetUnDir(
const PGraph& Graph);
 
   61 template <
class PGraph> 
void MakeUnDir(
const PGraph& Graph);
 
   63 template <
class PGraph> 
void AddSelfEdges(
const PGraph& Graph);
 
   65 template <
class PGraph> 
void DelSelfEdges(
const PGraph& Graph);
 
   71 template <
class PGraph> 
void DelNodes(PGraph& Graph, 
const TIntV& NIdV);
 
   75 template <
class PGraph> 
void DelDegKNodes(PGraph& Graph, 
const int& OutDegK, 
const int& InDegK);
 
   79 template <
class PGraph> 
bool IsTree(
const PGraph& Graph, 
int& RootNIdX);
 
   80 template <
class PGraph> 
int GetTreeRootNId(
const PGraph& Graph) { 
int RootNId; 
bool Tree; Tree = 
IsTree(Graph, RootNId);  
Assert(Tree);  
return RootNId; }
 
   81 template <
class PGraph> 
void GetTreeSig(
const PGraph& Graph, 
const int& RootNId, 
TIntV& Sig);
 
   82 template <
class PGraph> 
void GetTreeSig(
const PGraph& Graph, 
const int& RootNId, 
TIntV& Sig, 
TIntPrV& NodeMap);
 
   86 template <
class PGraph>
 
   89   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
   90     if (NI.GetInDeg() == NodeInDeg) Cnt++;
 
   95 template <
class PGraph>
 
   98   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
   99     if (NI.GetOutDeg() == NodeOutDeg) Cnt++;
 
  104 template <
class PGraph>
 
  107   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  108     if (NI.GetDeg() == NodeDeg) Cnt++;
 
  113 template <
class PGraph>
 
  116   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  117     if (NI.GetDeg() > 0) Cnt++;
 
  122 template <
class PGraph>
 
  124   if (! Graph->IsNode(NId)) { 
return 0; }
 
  125   const bool IsDir = Graph->HasFlag(
gfDirected);
 
  126   const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
 
  129     for (
int e = 0; e < NI.GetOutDeg(); e++) {
 
  130       if (NodeSet.
IsKey(NI.GetOutNId(e))) { EdgesToSet++; } }
 
  134     for (
int e = 0; e < NI.GetOutDeg(); e++) {
 
  135       if (NodeSet.
IsKey(NI.GetOutNId(e))) { Set.
AddKey(NI.GetOutNId(e)); } }
 
  136     for (
int e = 0; e < NI.GetInDeg(); e++) {
 
  137       if (NodeSet.
IsKey(NI.GetInNId(e))) { Set.AddKey(NI.GetInNId(e)); } }
 
  142 template <
class PGraph>
 
  146   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  147     if (MxDeg < NI.GetDeg()) { MxDegV.
Clr(); MxDeg = NI.GetDeg(); }
 
  148     if (MxDeg == NI.GetDeg()) { MxDegV.
Add(NI.GetId()); }
 
  154 template <
class PGraph>
 
  158   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  159     if (MxDeg < NI.GetInDeg()) { MxDegV.
Clr(); MxDeg = NI.GetInDeg(); }
 
  160     if (MxDeg == NI.GetInDeg()) { MxDegV.
Add(NI.GetId()); }
 
  166 template <
class PGraph>
 
  170   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  171     if (MxDeg < NI.GetOutDeg()) { MxDegV.
Clr(); MxDeg = NI.GetOutDeg(); }
 
  172     if (MxDeg == NI.GetOutDeg()) { MxDegV.
Add(NI.GetId()); }
 
  178 template <
class PGraph>
 
  181   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  182     DegToCntH.
AddDat(NI.GetInDeg())++; }
 
  183   DegToCntV.
Gen(DegToCntH.
Len(), 0);
 
  184   for (
int i = 0; i < DegToCntH.
Len(); i++) {
 
  189 template <
class PGraph>
 
  192   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  193     DegToCntH.
AddDat(NI.GetInDeg())++; }
 
  194   DegToCntV.
Gen(DegToCntH.
Len(), 0);
 
  195   for (
int i = 0; i < DegToCntH.
Len(); i++) {
 
  200 template <
class PGraph>
 
  203   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  204     DegToCntH.
AddDat(NI.GetOutDeg())++; }
 
  205   DegToCntV.
Gen(DegToCntH.
Len(), 0);
 
  206   for (
int i = 0; i < DegToCntH.
Len(); i++) {
 
  211 template <
class PGraph>
 
  214   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  215     DegToCntH.
AddDat(NI.GetOutDeg())++; }
 
  216   DegToCntV.
Gen(DegToCntH.
Len(), 0);
 
  217   for (
int i = 0; i < DegToCntH.
Len(); i++) {
 
  222 template <
class PGraph>
 
  225   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  226     DegToCntH.
AddDat(NI.GetDeg())++; }
 
  227   DegToCntV.
Gen(DegToCntH.
Len(), 0);
 
  228   for (
int i = 0; i < DegToCntH.
Len(); i++) {
 
  233 template <
class PGraph>
 
  236   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  237     DegToCntH.
AddDat(NI.GetDeg())++; }
 
  238   DegToCntV.
Gen(DegToCntH.
Len(), 0);
 
  239   for (
int i = 0; i < DegToCntH.
Len(); i++) {
 
  244 template <
class PGraph>
 
  246   DegV.
Gen(Graph->GetNodes(), 0);
 
  247   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  248     DegV.
Add(NI.GetDeg());
 
  252 template <
class PGraph>
 
  254   InDegV.
Gen(Graph->GetNodes(), 0);
 
  255   OutDegV.
Gen(Graph->GetNodes(), 0);
 
  256   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  257     InDegV.
Add(NI.GetInDeg());
 
  258     OutDegV.
Add(NI.GetOutDeg());
 
  262 template <
class PGraph>
 
  264   NIdInDegV.
Reserve(Graph->GetNodes(), 0);
 
  265   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  266     NIdInDegV.
Add(
TIntPr(NI.GetId(), NI.GetInDeg()));
 
  270 template <
class PGraph>
 
  272   NIdOutDegV.
Reserve(Graph->GetNodes(), 0);
 
  273   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  274     NIdOutDegV.
Add(
TIntPr(NI.GetId(), NI.GetOutDeg()));
 
  278 template <
class PGraph>
 
  283   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  285     for (
int e = 0; e < NI.GetDeg(); e++) { 
 
  286       const int NbrId = NI.GetNbrNId(e);
 
  287       if (NbrId == NI.GetId()) { 
 
  300 template <
class PGraph>
 
  304   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  306     for (
int e = 0; e < NI.GetOutDeg(); e++) { 
 
  307       if (NI.GetOutNId(e) != NI.GetId()) { 
 
  308         NbrSet.
AddKey(NI.GetOutNId(e)); }
 
  315 template <
class PGraph>
 
  322   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  323     const int SrcId = NI.GetId();
 
  324     for (
int e = 0; e < NI.GetOutDeg(); e++) {
 
  325       const int DstId = NI.GetOutNId(e);
 
  326       if (DstId <= SrcId) { 
continue; } 
 
  327       if (Graph->IsEdge(DstId, SrcId)) { Cnt++; }
 
  333 template <
class PGraph>
 
  336   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  337     for (
int e = 0; e < NI.GetOutDeg(); e++) {
 
  338       if (NI.GetId() == NI.GetOutNId(e)) { Cnt++; }
 
  344 template <
class PGraph>
 
  346   PGraph NewGraphPt = PGraph::New();
 
  347   *NewGraphPt = *Graph;
 
  352 template <
class PGraph>
 
  356   for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
 
  357     const int SrcNId = EI.GetSrcNId();
 
  358     const int DstNId = EI.GetDstNId();
 
  359     if (! Graph->IsEdge(DstNId, SrcNId)) {
 
  363   for (
int i = 0; i < EdgeV.
Len(); i++) {
 
  364     Graph->AddEdge(EdgeV[i].Val1, EdgeV[i].Val2);
 
  368 template <
class PGraph>
 
  371   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  372     const int NId = NI.GetId();
 
  373     if (! Graph->IsEdge(NId, NId)) {
 
  377   for (
int i = 0; i < EdgeV.
Len(); i++) {
 
  378     Graph->AddEdge(EdgeV[i], EdgeV[i]);
 
  382 namespace TSnapDetail {
 
  384 template <
class PGraph, 
bool IsMultiGraph>
 
  386   static void Do(
const PGraph& Graph) {
 
  389     for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
 
  390       if (EI.GetSrcNId() == EI.GetDstNId()) {
 
  391         EdgeV.
Add(EI.GetSrcNId());
 
  394     for (
int i = 0; i < EdgeV.
Len(); i++) {
 
  395       Graph->DelEdge(EdgeV[i], EdgeV[i]);
 
  400 template <
class PGraph>
 
  402   static void Do(
const PGraph& Graph) {
 
  404     for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
 
  405       if (EI.GetSrcNId() == EI.GetDstNId()) {
 
  407         EdgeV.
Add(EI.GetId());
 
  410     for (
int i = 0; i < EdgeV.
Len(); i++) { 
 
  411       Graph->DelEdge(EdgeV[i]);
 
  418 template <
class PGraph>
 
  424 template <
class PGraph>
 
  426   for (
int n = 0; n < NIdV.
Len(); n++) {
 
  427     Graph->DelNode(NIdV[n]);
 
  431 template <
class PGraph>
 
  434   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  435     if (NI.GetDeg() == 0) {
 
  436       DelNIdV.
Add(NI.GetId());
 
  439   for (
int i = 0; i < DelNIdV.
Len(); i++) {
 
  440     Graph->DelNode(DelNIdV[i]);
 
  444 template <
class PGraph>
 
  445 void DelDegKNodes(PGraph& Graph, 
const int& OutDegK, 
const int& InDegK) {
 
  447   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  448     if (NI.GetOutDeg() == OutDegK || NI.GetInDeg() == InDegK) {
 
  449       DelNIdV.
Add(NI.GetId());
 
  452   for (
int i = 0; i < DelNIdV.
Len(); i++) {
 
  453     Graph->DelNode(DelNIdV[i]);
 
  459 template <
class PGraph>
 
  460 bool IsTree(
const PGraph& Graph, 
int& RootNId) {
 
  461   if (Graph->GetNodes() == 1 && Graph->GetEdges() == 0) {
 
  462     RootNId = Graph->BegNI().GetId();
 
  466   if (Graph->GetNodes() != Graph->GetEdges()+1) { 
return false; }
 
  468   int ZeroOutDegN = -1;
 
  469   for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
 
  470     if (NI.GetOutDeg() == 0) {
 
  471       ZeroOutDegN = NI.GetId();  NZeroOutDeg++;
 
  473     if (NI.GetDeg() == 0) { 
return false; } 
 
  475   if (NZeroOutDeg==1) {
 
  477     RootNId = ZeroOutDegN;  
return true;
 
  483 template <
class PGraph>
 
  486   Sig.
Gen(Graph->GetNodes(), 0);
 
  489   int LastPos = 0, NodeCnt = 1;
 
  490   while (! NIdQ.Empty()) {
 
  491     const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top());  NIdQ.Pop();
 
  492     IAssert(Node.GetInDeg()==0 || Node.GetOutDeg()==0); 
 
  493     if (Node.GetInDeg() != 0) {
 
  494       for (
int e = 0; e < Node.GetInDeg(); e++) {
 
  495         NIdQ.Push(Node.GetInNId(e)); }
 
  496     } 
else if (Node.GetOutDeg() != 0) {
 
  497       for (
int e = 0; e < Node.GetOutDeg(); e++) {
 
  498         NIdQ.Push(Node.GetOutNId(e)); }
 
  500     Sig.
Add(Node.GetInDeg());
 
  501     if (--NodeCnt == 0) {
 
  502       for (
int i = LastPos; i < Sig.
Len(); i++) NodeCnt += Sig[i];
 
  503       Sig.
QSort(LastPos, Sig.
Len()-1, 
false);
 
  510 template <
class PGraph>
 
  513   NodeMap.
Gen(Graph->GetNodes(), 0);
 
  514   Sig.
Gen(Graph->GetNodes(), 0);
 
  517   int LastPos = 0, NodeCnt = 1;
 
  518   while (! NIdQ.Empty()) {
 
  519     const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top());  NIdQ.Pop();
 
  520     IAssert(Node.GetInDeg()==0 || Node.GetOutDeg()==0); 
 
  521     if (Node.GetInDeg() != 0) {
 
  522       for (
int e = 0; e < Node.GetInDeg(); e++) {
 
  523         NIdQ.Push(Node.GetInNId(e)); }
 
  524       NodeMap.
Add(
TIntPr(Node.GetInDeg(), Node.GetId()));
 
  525     } 
else if (Node.GetOutDeg() != 0) {
 
  526       for (
int e = 0; e < Node.GetOutDeg(); e++) {
 
  527         NIdQ.Push(Node.GetOutNId(e)); }
 
  528       NodeMap.
Add(
TIntPr(Node.GetOutDeg(), Node.GetId()));
 
  530     if (--NodeCnt == 0) {
 
  531       for (
int i = LastPos; i < NodeMap.
Len(); i++) {
 
  532         NodeCnt += NodeMap[i].Val1; }
 
  533       NodeMap.
QSort(LastPos, NodeMap.
Len()-1, 
false);
 
  534       LastPos = NodeMap.
Len();
 
  537   for (
int i = 0; i < NodeMap.
Len(); i++) {
 
  538     Sig.
Add(NodeMap[i].Val1); 
 
  539     NodeMap[i].Val1 = NodeMap[i].Val2;
 
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
TPair< TInt, TInt > TIntPr
void GetOutDegCnt(const PGraph &Graph, TIntPrV &DegToCntV)
Returns an out-degree histogram: a set of pairs (out-degree, number of nodes of such out-degree) ...
int CntUniqUndirEdges(const PGraph &Graph)
Counts unique undirected edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via an undi...
void GetNodeOutDegV(const PGraph &Graph, TIntPrV &NIdOutDegV)
Returns a vector of pairs (node id, node out-degree) 
void GetDegCnt(const PGraph &Graph, TIntPrV &DegToCntV)
Returns a degree histogram: a set of pairs (degree, number of nodes of such degree) ...
void QSort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Quick sorts the values between positions MnLValN...MxLValN. 
TSizeTy Len() const 
Returns the number of elements in the vector. 
void GetNodeInDegV(const PGraph &Graph, TIntPrV &NIdInDegV)
Returns a vector of pairs (node id, node in-degree) 
int CntUniqDirEdges(const PGraph &Graph)
Counts unique directed edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via a directe...
bool IsKey(const TKey &Key) const 
void MakeUnDir(const PGraph &Graph)
Makes the graph undirected. For every edge (u,v) an edge (v,u) is added (if it does not yet exist)...
void GetTreeSig(const PGraph &Graph, const int &RootNId, TIntV &Sig)
bool Empty() const 
Tests whether the vector is empty. 
bool IsTree(const PGraph &Graph, int &RootNIdX)
void DelZeroDegNodes(PGraph &Graph)
Removes all the zero-degree nodes, that isolated nodes, from the graph. 
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector. 
void GetDegSeqV(const PGraph &Graph, TIntV &DegV)
Returns a degree sequence vector. 
void GetInDegCnt(const PGraph &Graph, TIntPrV &DegToCntV)
Returns an in-degree histogram: a set of pairs (in-degree, number of nodes of such in-degree) ...
int CntSelfEdges(const PGraph &Graph)
Counts the number of self-edges in a graph. Edge (u,u) is a self-edge. 
int GetTreeRootNId(const PGraph &Graph)
void Sort(const bool &Asc=true)
Sorts the elements of the vector. 
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
int CntNonZNodes(const PGraph &Graph)
Returns the number of nodes with degree greater than 0. 
int GetMxOutDegNId(const PGraph &Graph)
Returns a randomly chosen node from all the nodes with the maximum out-degree. 
int AddKey(const TKey &Key)
static void Do(const PGraph &Graph)
int CntEdgesToSet(const PGraph &Graph, const int &NId, const TIntSet &NodeSet)
Returns the number of nodes in NodeSet that have an edge to the node NId. 
int CntUniqBiDirEdges(const PGraph &Graph)
Counts unique bidirectional edges in the graph Graph. Edge is bidirectional if there exist directed e...
TPair< TFlt, TFlt > TFltPr
int CntInDegNodes(const PGraph &Graph, const int &NodeInDeg)
Returns the number of nodes with in-degree NodeInDeg. 
void AddSelfEdges(const PGraph &Graph)
Adds a self-edge to every node in the graph. 
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph 
int GetMxDegNId(const PGraph &Graph)
Returns a randomly chosen node from all the nodes with the maximum degree. 
bool IsConnected(const PGraph &Graph)
Tests whether the Graph is (weakly) connected. 
void Push(const TVal &Val)
Adds an element at the end of the queue. 
void DelNodes(PGraph &Graph, const TIntV &NIdV)
Removes nodes with ids stored in NIdV from the graph. 
#define EAssertR(Cond, MsgStr)
static void Do(const PGraph &Graph)
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements. 
int GetUniDevInt(const int &Range=0)
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements. 
void DelDegKNodes(PGraph &Graph, const int &OutDegK, const int &InDegK)
Removes all the node of out-degree OutDegK and all the nodes of in-degree InDegK from the graph...
int CntDegNodes(const PGraph &Graph, const int &NodeDeg)
Returns the number of nodes with degree NodeDeg. 
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element. 
void DelSelfEdges(const PGraph &Graph)
Removes all the self-edges from the graph. 
TDat & AddDat(const TKey &Key)
int GetMxInDegNId(const PGraph &Graph)
Returns a randomly chosen node from all the nodes with the maximum in-degree. 
PGraph GetUnDir(const PGraph &Graph)
Returs an undirected version of the graph. For every edge (u,v) an edge (v,u) is added (if it does no...
const TKey & GetKey(const int &KeyId) const 
int CntOutDegNodes(const PGraph &Graph, const int &NodeOutDeg)
Returns the number of nodes with out-degree NodeOutDeg.