SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
TSnap Namespace Reference

Main namespace for all the Snap global entities. More...

Namespaces

 TSnapDetail
 

Classes

struct  IsBipart
 Tests (at compile time) if the graph is a bipartite graph type. More...
 
struct  IsBipart< TBPGraph >
 
struct  IsDirected
 Tests (at compile time) if the graph is directed. More...
 
struct  IsDirected< TBigNet< TNodeData, IsDir > >
 
struct  IsDirected< TBigNet< TNodeData, true > >
 
struct  IsDirected< TDirNet >
 
struct  IsDirected< TModeNet >
 
struct  IsDirected< TNEANet >
 
struct  IsDirected< TNEANetMP >
 
struct  IsDirected< TNEGraph >
 
struct  IsDirected< TNGraph >
 
struct  IsDirected< TNGraphMP >
 
struct  IsDirected< TNodeEDatNet< TNodeData, TEdgeData > >
 
struct  IsDirected< TNodeEdgeNet< TNodeData, TEdgeData > >
 
struct  IsDirected< TNodeNet< TNodeData > >
 
struct  IsDirected< TTimeNENet >
 
struct  IsDirected< TTimeNet >
 
struct  IsEdgeDat
 Tests (at compile time) if the graph is a network with data on edges. More...
 
struct  IsEdgeDat< TNodeEDatNet< TNodeData, TEdgeData > >
 
struct  IsEdgeDat< TNodeEdgeNet< TNodeData, TEdgeData > >
 
struct  IsEdgeDat< TTimeNENet >
 
struct  IsMultiGraph
 Tests (at compile time) if the graph is a multigraph with multiple edges between the same nodes. More...
 
struct  IsMultiGraph< TModeNet >
 
struct  IsMultiGraph< TNEANet >
 
struct  IsMultiGraph< TNEANetMP >
 
struct  IsMultiGraph< TNEGraph >
 
struct  IsMultiGraph< TNodeEdgeNet< TNodeData, TEdgeData > >
 
struct  IsMultiGraph< TTimeNENet >
 
struct  IsNodeDat
 Tests (at compile time) if the graph is a network with data on nodes. More...
 
struct  IsNodeDat< TBigNet< TNodeData, IsDir > >
 
struct  IsNodeDat< TNodeEDatNet< TNodeData, TEdgeData > >
 
struct  IsNodeDat< TNodeEdgeNet< TNodeData, TEdgeData > >
 
struct  IsNodeDat< TNodeNet< TNodeData > >
 
struct  IsNodeDat< TTimeNENet >
 
struct  IsNodeDat< TTimeNet >
 
struct  IsSources
 Tests (at compile time) if the nodes store only out-edges, but not in-edges. More...
 
class  TPRManager
 Push relabel attr manager. More...
 

Functions

template<class PGraph >
int CntInDegNodes (const PGraph &Graph, const int &NodeInDeg)
 Returns the number of nodes with in-degree NodeInDeg. More...
 
template<class PGraph >
int CntOutDegNodes (const PGraph &Graph, const int &NodeOutDeg)
 Returns the number of nodes with out-degree NodeOutDeg. More...
 
template<class PGraph >
int CntDegNodes (const PGraph &Graph, const int &NodeDeg)
 Returns the number of nodes with degree NodeDeg. More...
 
template<class PGraph >
int CntNonZNodes (const PGraph &Graph)
 Returns the number of nodes with degree greater than 0. More...
 
template<class PGraph >
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. More...
 
template<class PGraph >
int GetMxDegNId (const PGraph &Graph)
 Returns a randomly chosen node from all the nodes with the maximum degree. More...
 
template<class PGraph >
int GetMxInDegNId (const PGraph &Graph)
 Returns a randomly chosen node from all the nodes with the maximum in-degree. More...
 
template<class PGraph >
int GetMxOutDegNId (const PGraph &Graph)
 Returns a randomly chosen node from all the nodes with the maximum out-degree. More...
 
template<class PGraph >
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) More...
 
template<class PGraph >
void GetInDegCnt (const PGraph &Graph, TFltPrV &DegToCntV)
 Returns an in-degree histogram: a set of pairs (in-degree, number of nodes of such in-degree) More...
 
template<class PGraph >
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) More...
 
template<class PGraph >
void GetOutDegCnt (const PGraph &Graph, TFltPrV &DegToCntV)
 Returns an out-degree histogram: a set of pairs (out-degree, number of nodes of such out-degree) More...
 
template<class PGraph >
void GetDegCnt (const PGraph &Graph, TIntPrV &DegToCntV)
 Returns a degree histogram: a set of pairs (degree, number of nodes of such degree) More...
 
template<class PGraph >
void GetDegCnt (const PGraph &Graph, TFltPrV &DegToCntV)
 Returns a degree histogram: a set of pairs (degree, number of nodes of such degree) More...
 
template<class PGraph >
void GetDegSeqV (const PGraph &Graph, TIntV &DegV)
 Returns a degree sequence vector. More...
 
template<class PGraph >
void GetDegSeqV (const PGraph &Graph, TIntV &InDegV, TIntV &OutDegV)
 Returns an in- and out-degree sequence vectors. More...
 
template<class PGraph >
void GetNodeInDegV (const PGraph &Graph, TIntPrV &NIdInDegV)
 Returns a vector of pairs (node id, node in-degree) More...
 
template<class PGraph >
void GetNodeOutDegV (const PGraph &Graph, TIntPrV &NIdOutDegV)
 Returns a vector of pairs (node id, node out-degree) More...
 
template<class PGraph >
int CntUniqUndirEdges (const PGraph &Graph)
 Counts unique undirected edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via an undirected edge if there exists an edge in either direction (u,v) or (v,u). More...
 
template<class PGraph >
int CntUniqDirEdges (const PGraph &Graph)
 Counts unique directed edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via a directed edge if there exists a directed edge from node u to node v. More...
 
template<class PGraph >
int CntUniqBiDirEdges (const PGraph &Graph)
 Counts unique bidirectional edges in the graph Graph. Edge is bidirectional if there exist directed edges in both directions: (u,v) and (v,u) More...
 
template<class PGraph >
int CntSelfEdges (const PGraph &Graph)
 Counts the number of self-edges in a graph. Edge (u,u) is a self-edge. More...
 
template<class PGraph >
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 not yet exist). More...
 
template<class PGraph >
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). More...
 
template<class PGraph >
void AddSelfEdges (const PGraph &Graph)
 Adds a self-edge to every node in the graph. More...
 
template<class PGraph >
void DelSelfEdges (const PGraph &Graph)
 Removes all the self-edges from the graph. More...
 
template<class PGraph >
void DelNodes (PGraph &Graph, const TIntV &NIdV)
 Removes nodes with ids stored in NIdV from the graph. More...
 
template<class PGraph >
void DelZeroDegNodes (PGraph &Graph)
 Removes all the zero-degree nodes, that isolated nodes, from the graph. More...
 
template<class PGraph >
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. More...
 
template<class PGraph >
bool IsTree (const PGraph &Graph, int &RootNIdX)
 
template<class PGraph >
int GetTreeRootNId (const PGraph &Graph)
 
template<class PGraph >
void GetTreeSig (const PGraph &Graph, const int &RootNId, TIntV &Sig)
 
template<class PGraph >
void GetTreeSig (const PGraph &Graph, const int &RootNId, TIntV &Sig, TIntPrV &NodeMap)
 
template<class PGraph >
void GetAnf (const PGraph &Graph, const int &SrcNId, TIntFltKdV &DistNbrsV, const int &MxDist, const bool &IsDir, const int &NApprox=32)
 
template<class PGraph >
void GetAnf (const PGraph &Graph, TIntFltKdV &DistNbrsV, const int &MxDist, const bool &IsDir, const int &NApprox=32)
 
template<class PGraph >
double GetAnfEffDiam (const PGraph &Graph, const bool &IsDir, const double &Percentile, const int &NApprox)
 
template<class PGraph >
double GetAnfEffDiam (const PGraph &Graph, const int NRuns=1, int NApprox=-1)
 
template<class PGraph >
void TestAnf ()
 
template<class PGraph >
PNGraph GetBfsTree (const PGraph &Graph, const int &StartNId, const bool &FollowOut, const bool &FollowIn)
 Returns a directed Breadth-First-Search tree rooted at StartNId. More...
 
template<class PGraph >
int GetSubTreeSz (const PGraph &Graph, const int &StartNId, const bool &FollowOut, const bool &FollowIn, int &TreeSzX, int &TreeDepthX)
 Returns the BFS tree size (number of nodes) and depth (number of levels) by following in-links (parameter FollowIn = true) and/or out-links (parameter FollowOut = true) of node StartNId. More...
 
template<class PGraph >
int GetNodesAtHop (const PGraph &Graph, const int &StartNId, const int &Hop, TIntV &NIdV, const bool &IsDir=false)
 Finds IDs of all nodes that are at distance Hop from node StartNId. More...
 
template<class PGraph >
int GetNodesAtHops (const PGraph &Graph, const int &StartNId, TIntPrV &HopCntV, const bool &IsDir=false)
 Returns the number of nodes at each hop distance from the starting node StartNId. More...
 
template<class PGraph >
int GetShortPath (const PGraph &Graph, const int &SrcNId, const int &DstNId, const bool &IsDir=false)
 Returns the length of the shortest path from node SrcNId to node DstNId. More...
 
template<class PGraph >
int GetShortPath (const PGraph &Graph, const int &SrcNId, TIntH &NIdToDistH, const bool &IsDir=false, const int &MaxDist=TInt::Mx)
 Returns the length of the shortest path from node SrcNId to all other nodes in the network. More...
 
template<class PGraph >
int GetBfsFullDiam (const PGraph &Graph, const int &NTestNodes, const bool &IsDir=false)
 Returns the (approximation of the) Diameter (maximum shortest path length) of a graph (by performing BFS from NTestNodes random starting nodes). More...
 
template<class PGraph >
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 shortest path lengths) of a graph (by performing BFS from NTestNodes random starting nodes). More...
 
template<class PGraph >
double GetBfsEffDiam (const PGraph &Graph, const int &NTestNodes, const bool &IsDir, double &EffDiamX, int &FullDiamX)
 Returns the (approximation of the) Effective Diameter and the Diameter of a graph (by performing BFS from NTestNodes random starting nodes). More...
 
template<class PGraph >
double GetBfsEffDiam (const PGraph &Graph, const int &NTestNodes, const bool &IsDir, double &EffDiamX, int &FullDiamX, double &AvgSPLX)
 Returns the (approximation of the) Effective Diameter, the Diameter and the Average Shortest Path length in a graph (by performing BFS from NTestNodes random starting nodes). More...
 
template<class PGraph >
double GetBfsEffDiamAll (const PGraph &Graph, const int &NTestNodes, const bool &IsDir, double &EffDiamX, int &FullDiamX, double &AvgSPLX)
 Returns the (approximation of the) Effective Diameter, the Diameter and the Average Shortest Path length in a graph (by performing BFS from NTestNodes random starting nodes). More...
 
template<class PGraph >
double GetBfsEffDiam (const PGraph &Graph, const int &NTestNodes, const TIntV &SubGraphNIdV, const bool &IsDir, double &EffDiamX, int &FullDiamX)
 Use the whole graph (all edges) to measure the shortest path lengths but only report the path lengths between nodes in the SubGraphNIdV. More...
 
template<class PGraph >
int GetShortestDistances (const PGraph &Graph, const int &StartNId, const bool &FollowOut, const bool &FollowIn, TIntV &ShortestDists)
 
template<class PGraph >
int GetShortestDistancesMP2 (const PGraph &Graph, const int &StartNId, const bool &FollowOut, const bool &FollowIn, TIntV &ShortestDists)
 
PNGraph CascGraphSource (PTable P, const TStr C1, const TStr C2, const TStr C3, const TStr C4, const TInt W)
 Takes as input the column names of the PTable P as C1, C2,C3 and C4 and returns a directed graph of W-adjacent events. For graph generation events are sorted by C1. More...
 
PNGraph CascGraphTime (PTable P, const TStr C1, const TStr C2, const TStr C3, const TStr C4, const TInt W)
 Takes as input the column names of the PTable P as C1, C2,C3 and C4 and returns a directed graph of W-adjacent events. For graph generation events are sorted by C3. More...
 
PNGraph CascGraph (PTable P, const TStr C1, const TStr C2, const TStr C3, const TStr C4, const TInt W, bool SortParam=true)
 Takes as input the column names of the PTable P as C1, C2, C3 and C4 and returns a directed graph of W-adjacent events. By default calls CascGraphSource. Toggle SortParam to use CascGraphTime. More...
 
void CascFind (PNGraph Graph, PTable P, const TStr C1, const TStr C2, const TStr C3, const TStr C4, TVec< TIntV > &TopCascVV, bool Print=false)
 Takes as input a directed graph and returns all the top cascades in TopCascVV. More...
 
void CascFindMP (PNGraph Graph, PTable P, const TStr C1, const TStr C2, const TStr C3, const TStr C4, TVec< TIntV > &TopCascVV)
 Parallel implementaion of CascFind takes as input a directed graph and returns all the top cascades in TopCascVV. More...
 
double GetDegreeCentr (const PUNGraph &Graph, const int &NId)
 
void GetEigenVectorCentr (const PUNGraph &Graph, TIntFltH &NIdEigenH, const double &Eps, const int &MaxIter)
 
double GetGroupDegreeCentr (const PUNGraph &Graph, const PUNGraph &Group)
 
double GetGroupDegreeCentr0 (const PUNGraph &Graph, const TIntH &GroupNodes)
 
double GetGroupDegreeCentr (const PUNGraph &Graph, const TIntH &GroupNodes)
 
double GetGroupFarnessCentr (const PUNGraph &Graph, const TIntH &GroupNodes)
 
PUNGraphAllGraphsWithNNodes (int n)
 
TIntHAllCombinationsMN (int m, int n)
 
double GetGroupClosenessCentr (const PUNGraph &Graph, const TIntH &GroupNodes)
 
TIntH MaxCPGreedyBetter (const PUNGraph &Graph, const int k)
 Returns centrality Maximum k group. More...
 
TIntH MaxCPGreedyBetter1 (const PUNGraph &Graph, const int k)
 Returns centrality Maximum k group. More...
 
TIntH MaxCPGreedyBetter2 (const PUNGraph &Graph, const int k)
 Returns centrality Maximum k group. More...
 
TIntH MaxCPGreedyBetter3 (const PUNGraph &Graph, const int k)
 Returns centrality Maximum k group. More...
 
int GetWeightedPageRank (const PNEANet Graph, TIntFltH &PRankH, const TStr &Attr, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
 Weighted PageRank (TODO: Use template) More...
 
int GetWeightedPageRankMP (const PNEANet Graph, TIntFltH &PRankH, const TStr &Attr, const double &C, const double &Eps, const int &MaxIter)
 
TIntFltH EventImportance (const PNGraph &Graph, const int k)
 Event importance. More...
 
TIntFltH EventImportance1 (const PNGraph &Graph, const int k)
 
int Intersect (TUNGraph::TNodeI Node, TIntH NNodes)
 Intersect. More...
 
int Intersect (TUNGraph::TNodeI Node, TStr NNodes)
 Intersect. More...
 
int Intersect (TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
 Intersect. More...
 
int Intersect1 (TUNGraph::TNodeI Node, TStr NNodes)
 
TIntH LoadNodeList (TStr InFNmNodes)
 
int findMinimum (TIntV &Frontier, TIntFltH &NIdDistH)
 
int GetWeightedShortestPath (const PNEANet Graph, const int &SrcNId, TIntFltH &NIdDistH, const TFltV &Attr)
 
double GetWeightedFarnessCentr (const PNEANet Graph, const int &NId, const TFltV &Attr, const bool &Normalized, const bool &IsDir)
 
double GetWeightedClosenessCentr (const PNEANet Graph, const int &NId, const TFltV &Attr, const bool &Normalized, const bool &IsDir)
 
void GetWeightedBetweennessCentr (const PNEANet Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent, const TFltV &Attr, const bool &IsDir)
 Computes (approximate) weighted Beetweenness Centrality of all nodes and all edges of the network. More...
 
void GetWeightedBetweennessCentr (const PNEANet Graph, TIntFltH &NodeBtwH, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const double &NodeFrac, const bool &IsDir)
 
void GetWeightedBetweennessCentr (const PNEANet Graph, TIntFltH &NodeBtwH, const TFltV &Attr, const double &NodeFrac, const bool &IsDir)
 
void GetWeightedBetweennessCentr (const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const double &NodeFrac, const bool &IsDir)
 
TTableIterator GetMapPageRank (const TVec< PNEANet > &GraphSeq, TTableContext *Context, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
 Gets sequence of PageRank tables from given GraphSeq. More...
 
TTableIterator GetMapHitsIterator (const TVec< PNEANet > &GraphSeq, TTableContext *Context, const int &MaxIter=20)
 Gets sequence of Hits tables from given GraphSeq. More...
 
template<class PGraph >
double GetFarnessCentr (const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
 
template<class PGraph >
double GetFarnessCentrMP (const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
 
template<class PGraph >
double GetClosenessCentr (const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
 
template<class PGraph >
double GetClosenessCentrMP (const PGraph &Graph, const int &NId, const bool &Normalized=true, const bool &IsDir=false)
 
template<class PGraph >
int GetNodeEcc (const PGraph &Graph, const int &NId, const bool &IsDir=false)
 
template<class PGraph >
void GetBetweennessCentr (const PGraph &Graph, TIntFltH &NIdBtwH, const double &NodeFrac=1.0, const bool &IsDir=false)
 
template<class PGraph >
void GetBetweennessCentr (const PGraph &Graph, TIntPrFltH &EdgeBtwH, const double &NodeFrac=1.0, const bool &IsDir=false)
 
template<class PGraph >
void GetBetweennessCentr (const PGraph &Graph, TIntFltH &NIdBtwH, TIntPrFltH &EdgeBtwH, const double &NodeFrac=1.0, const bool &IsDir=false)
 
template<class PGraph >
void GetBetweennessCentr (const PGraph &Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent, const bool &IsDir)
 
template<class PGraph >
void GetPageRank (const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
 
template<class PGraph >
void GetPageRank_v1 (const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
 
template<class PGraph >
void GetPageRankMP (const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
 
template<class PGraph >
void GetHits (const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
 
template<class PGraph >
void GetHitsMP (const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
 
template<class PGraph >
void MapPageRank (const TVec< PGraph > &GraphSeq, TVec< PTable > &TableSeq, TTableContext *Context, const double &C, const double &Eps, const int &MaxIter)
 Gets sequence of PageRank tables from given GraphSeq into TableSeq. More...
 
template<class PGraph >
void MapHits (const TVec< PGraph > &GraphSeq, TVec< PTable > &TableSeq, TTableContext *Context, const int &MaxIter)
 Gets sequence of Hits tables from given GraphSeq into TableSeq. More...
 
double CommunityGirvanNewman (PUNGraph &Graph, TCnComV &CmtyV)
 
double Infomap (PUNGraph &Graph, TCnComV &CmtyV)
 
double InfomapOnline (PUNGraph &Graph, int n1, int n2, TIntFltH &PAlpha, double &SumPAlphaLogPAlpha, TIntFltH &Qi, TIntH &Module, int &Br, TCnComV &CmtyV)
 
void CmtyEvolutionFileBatchV (TStr InFNm, TIntIntVH &sizesContV, TIntIntVH &cContV, TIntIntVH &edges, double alpha, double beta, int CmtyAlg)
 
void CmtyEvolutionFileBatch (TStr InFNm, TIntIntHH &sizesCont, TIntIntHH &cCont, TIntIntVH &edges, double alpha, double beta, int CmtyAlg)
 
void CmtyEvolutionJson (TStr &Json, TIntIntVH &sizesContV, TIntIntVH &cContV, TIntIntVH &edges)
 
TStr CmtyTest (TStr InFNm, int CmtyAlg)
 
void ReebSimplify (PNGraph &Graph, TIntH &t, int e, PNGraph &gFinal, TIntH &tFinal, bool collapse)
 
void ReebRefine (PNGraph &Graph, TIntH &t, int e, PNGraph &gFinal, TIntH &tFinal, bool collapse)
 
double CommunityCNM (const PUNGraph &Graph, TCnComV &CmtyV)
 
template<typename PGraph >
double GetModularity (const PGraph &G, const TIntV &NIdV, int GEdges=-1)
 
template<typename PGraph >
double GetModularity (const PGraph &G, const TCnComV &CmtyV, int GEdges=-1)
 
template<typename PGraph >
void GetEdgesInOut (const PGraph &Graph, const TIntV &NIdV, int &EdgesInX, int &EdgesOutX)
 
void GetBiConSzCnt (const PUNGraph &Graph, TIntPrV &SzCntV)
 Returns a distribution of bi-connected component sizes. More...
 
void GetBiCon (const PUNGraph &Graph, TCnComV &BiCnComV)
 Returns all bi-connected components of a Graph. More...
 
void GetArtPoints (const PUNGraph &Graph, TIntV &ArtNIdV)
 Returns articulation points of a Graph. More...
 
void GetEdgeBridges (const PUNGraph &Graph, TIntPrV &EdgeV)
 Returns bridge edges of a Graph. More...
 
void Get1CnComSzCnt (const PUNGraph &Graph, TIntPrV &SzCntV)
 Distribution of sizes of 1-components, maximal number of components that can be disconnected from the Graph by removing a single edge. More...
 
void Get1CnCom (const PUNGraph &Graph, TCnComV &Cn1ComV)
 Returns 1-components: maximal connected components of that can be disconnected from the Graph by removing a single edge. More...
 
PUNGraph GetMxBiCon (const PUNGraph &Graph, const bool &RenumberNodes=false)
 Returns a graph representing the largest bi-connected component on an undirected Graph. More...
 
template<class PGraph >
void GetNodeWcc (const PGraph &Graph, const int &NId, TIntV &CnCom)
 Returns (via output parameter CnCom) all nodes that are in the same connected component as node NId. More...
 
template<class PGraph >
bool IsConnected (const PGraph &Graph)
 Tests whether the Graph is (weakly) connected. More...
 
template<class PGraph >
bool IsWeaklyConn (const PGraph &Graph)
 Tests whether the Graph is weakly connected. More...
 
template<class PGraph >
void GetWccSzCnt (const PGraph &Graph, TIntPrV &WccSzCnt)
 Returns a distribution of weakly connected component sizes. More...
 
template<class PGraph >
void GetWccs (const PGraph &Graph, TCnComV &CnComV)
 Returns all weakly connected components in a Graph. More...
 
template<class PGraph >
void GetSccSzCnt (const PGraph &Graph, TIntPrV &SccSzCnt)
 Returns a distribution of strongly connected component sizes. More...
 
template<class PGraph >
void GetSccs (const PGraph &Graph, TCnComV &CnComV)
 Returns all strongly connected components in a Graph. More...
 
template<class PGraph >
double GetMxWccSz (const PGraph &Graph)
 Returns the fraction of nodes in the largest weakly connected component of a Graph. More...
 
template<class PGraph >
double GetMxSccSz (const PGraph &Graph)
 Returns the fraction of nodes in the largest strongly connected component of a Graph. More...
 
template<class PGraph >
PGraph GetMxWcc (const PGraph &Graph)
 Returns a graph representing the largest weakly connected component on an input Graph. More...
 
template<class PGraph >
PGraph GetMxScc (const PGraph &Graph)
 Returns a graph representing the largest strongly connected component on an input Graph. More...
 
template<class PGraph >
PGraph GetMxBiCon (const PGraph &Graph)
 Returns a graph representing the largest bi-connected component on an input Graph. More...
 
int LoadModeNetToNet (PMMNet Graph, const TStr &Name, PTable Table, const TStr &NCol, TStrV &NodeAttrV)
 Loads a mode, with name Name, into the PMMNet from the TTable. NCol specifies the node id column and NodeAttrV the node attributes. More...
 
int LoadMode (TModeNet &Graph, PTable Table, const TStr &NCol, TStrV &NodeAttrV)
 Loads the nodes specified in column NCol from the TTable with the attributes specified in NodeAttrV. More...
 
int LoadCrossNetToNet (PMMNet Graph, const TStr &Mode1, const TStr &Mode2, const TStr &CrossName, PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV)
 Loads a crossnet from Mode1 to Mode2, with name CrossName, from the provided TTable. EdgeAttrV specifies edge attributes. More...
 
int LoadCrossNet (TCrossNet &Graph, PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV)
 Loads the edges from the TTable and EdgeAttrV specifies columns containing edge attributes. More...
 
template<class PGraph >
PGraph ToGraph (PTable Table, const TStr &SrcCol, const TStr &DstCol, TAttrAggr AggrPolicy)
 Sequentially converts the table into a graph with links from nodes in SrcCol to those in DstCol. More...
 
template<class PGraph >
PGraph ToNetwork (PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &SrcAttrV, TStrV &DstAttrV, TStrV &EdgeAttrV, TAttrAggr AggrPolicy)
 Converts the Table into a graph with edges from SrcCol to DstCol, and attribute vector defined by the arguments. More...
 
template<class PGraph >
PGraph ToNetwork (PTable Table, const TStr &SrcCol, const TStr &DstCol, TAttrAggr AggrPolicy)
 Calls ToNetwork with an empty attribute vector. Convenience wrapper. More...
 
template<class PGraphMP >
PGraphMP ToGraphMP (PTable Table, const TStr &SrcCol, const TStr &DstCol)
 Performs table to graph conversion in parallel using the sort-first algorithm. This is the recommended method to use. More...
 
template<class PGraphMP >
PGraphMP ToGraphMP3 (PTable Table, const TStr &SrcCol, const TStr &DstCol)
 Performs table to graph conversion in parallel. Uses the hash-first method, which is less optimal, use ToGraphMP instead. More...
 
template<class PGraphMP >
PGraphMP ToNetworkMP (PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &SrcAttrV, TStrV &DstAttrV, TStrV &EdgeAttrV, TAttrAggr AggrPolicy)
 Does Table to Network conversion in parallel using the sort-first algorithm. This is the recommended method to use. More...
 
template<class PGraphMP >
PGraphMP ToNetworkMP (PTable Table, const TStr &SrcCol, const TStr &DstCol, TAttrAggr AggrPolicy)
 Calls ToNetworkMP with empty attribute vector. Convenience wrapper. More...
 
template<class PGraphMP >
PGraphMP ToNetworkMP2 (PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &SrcAttrV, TStrV &DstAttrV, TStrV &EdgeAttrV, TAttrAggr AggrPolicy)
 Implements table to network conversion in parallel. Not the recommended algorithm, using ToNetworkMP instead. More...
 
template<class PGraphMP >
PGraphMP ToNetworkMP2 (PTable Table, const TStr &SrcCol, const TStr &DstCol, TAttrAggr AggrPolicy)
 Calls ToNetworkMP2 with an empty attribute vector. Convenience wrapper. More...
 
template<class PGraph >
PGraph ToNetwork (PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV, TAttrAggr AggrPolicy)
 Converts table to a network sequentially. Use if network has only edge attributes. More...
 
template<class PGraphMP >
PGraphMP ToNetworkMP (PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV, TAttrAggr AggrPolicy)
 Converts table to network in parallel. Use if network has only edge attributes. More...
 
template<class PGraph >
PGraph ToNetwork (PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV, PTable NodeTable, const TStr &NodeCol, TStrV &NodeAttrV, TAttrAggr AggrPolicy)
 Converts table to network sequentially. Takes edges from Table and nodes explicitly from NodeCol in NodeTable, with attribute vectors passed as columns in corresponding tables. More...
 
template<class PGraphMP >
PGraphMP ToNetworkMP (PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV, PTable NodeTable, const TStr &NodeCol, TStrV &NodeAttrV, TAttrAggr AggrPolicy)
 Converts table to network in parallel. Takes edges from Table and nodes explicitly from NodeCol in NodeTable, with attribute vectors passed as columns in corresponding tables. More...
 
int FastCorePeriphery (PUNGraph &Graph, TIntIntH &out)
 
int FastCorePeripheryGC (PUNGraph &Graph, TIntIntH &out)
 
double BorgattiEverettMeasure (PUNGraph &Graph, TIntIntH &out, double coresize, int type)
 
double PearsonCorrelation (PUNGraph &Graph, TIntIntH &out, int coresize)
 
int IntFlowBiDBFS (const PNEANet &Net, const int &CapIndex, TIntV &Flow, TIntQ &FwdNodeQ, TIntH &PredEdgeH, TIntQ &BwdNodeQ, TIntH &SuccEdgeH, const int &SrcNId, const int &SnkNId)
 
int FindAugV (const PNEANet &Net, const int &CapIndex, TIntV &Flow, TIntQ &FwdNodeQ, TIntH &PredEdgeH, TIntQ &BwdNodeQ, TIntH &SuccEdgeH, TIntV &MidToSrcAugV, TIntV &MidToSnkAugV, const int &SrcNId, const int &SnkNId)
 Returns the amount the flow can be augmented over the paths, 0 if no path can be found. More...
 
int GetMaxFlowIntEK (PNEANet &Net, const int &SrcNId, const int &SnkNId)
 Returns the maximum integer valued flow in the network Net from source SrcNId to sink SnkNId. More...
 
void PushToOutNbr (TPRManager &PRM, const int &NId, const int &OutNId, const int &EId)
 Pushes flow from a node NId to a neighbor OutNId over edge EId. More...
 
void PushToInNbr (TPRManager &PRM, const int &NId, const int &InNId, const int &EId)
 Returns flow from a node NId to a neighbor InNId over edge EId. More...
 
void Relabel (TPRManager &PRM, const int &NId, const TNEANet::TNodeI &NI)
 Increases the label of a node NId to allow valid pushes to some neighbor. More...
 
int PushRelabel (TPRManager &PRM, const int &NId, const TNEANet::TNodeI &NI)
 Returns the ID of the neighbor that NId pushes to, -1 if no push was made. More...
 
void GlobalRelabel (PNEANet &Net, TPRManager &PRM, const int &SrcNId, const int &SnkNId)
 Implements the Global Relabeling heuristic. More...
 
int GetMaxFlowIntPR (PNEANet &Net, const int &SrcNId, const int &SnkNId)
 Returns the maximum integer valued flow in the network Net from source SrcNId to sink SnkNId. More...
 
TStr GetFlagStr (const TGraphFlag &GraphFlag)
 Returns a string representation of a flag. More...
 
template<class PGraph >
void PrintInfo (const PGraph &Graph, const TStr &Desc="", const TStr &OutFNm="", const bool &Fast=true)
 Prints basic graph statistics. More...
 
template<class PGraph >
int64 GetTriads (const PGraph &Graph, int64 &ClosedTriadsX, int64 &OpenTriadsX, int SampleNodes)
 Computes the number of Closed and Open triads. More...
 
template<class PGraph >
int GetKCoreNodes (const PGraph &Graph, TIntPrV &CoreIdSzV)
 Returns the number of nodes in each core of order K (where K=0, 1, ...) More...
 
template<class PGraph >
int GetKCoreEdges (const PGraph &Graph, TIntPrV &CoreIdSzV)
 Returns the number of edges in each core of order K (where K=0, 1, ...) More...
 
PBPGraph GenRndBipart (const int &LeftNodes, const int &RightNodes, const int &Edges, TRnd &Rnd=TInt::Rnd)
 Generates a random bipartite graph. More...
 
PUNGraph GenRndDegK (const int &Nodes, const int &NodeDeg, const int &NSwitch=100, TRnd &Rnd=TInt::Rnd)
 Generates a random graph where each node has degree exactly NodeDeg. More...
 
PUNGraph GenRndPowerLaw (const int &Nodes, const double &PowerExp, const bool &ConfModel=true, TRnd &Rnd=TInt::Rnd)
 Generates a random scale-free graph with power-law degree distribution. More...
 
PUNGraph GenDegSeq (const TIntV &DegSeqV, TRnd &Rnd=TInt::Rnd)
 Generates a random graph with exact degree sequence. More...
 
PUNGraph GenConfModel (const TIntV &DegSeqV, TRnd &Rnd=TInt::Rnd)
 Generates a random undirect graph with a given degree sequence. More...
 
PUNGraph GenRewire (const PUNGraph &Graph, const int &NSwitch=100, TRnd &Rnd=TInt::Rnd)
 Rewire a random undirected graph. Keeps node degrees the same, but randomly rewires the edges. More...
 
PNGraph GenRewire (const PNGraph &Graph, const int &NSwitch=100, TRnd &Rnd=TInt::Rnd)
 Rewire a random directed graph. Keeps node degrees the same, but randomly rewires the edges. More...
 
PBPGraph GenRewire (const PBPGraph &Graph, const int &NSwitch=100, TRnd &Rnd=TInt::Rnd)
 Rewire a random bipartite graph. Keeps node degrees the same, but randomly rewires the edges. More...
 
PUNGraph GenPrefAttach (const int &Nodes, const int &NodeOutDeg, TRnd &Rnd=TInt::Rnd)
 Generates a power-law degree distribution using Barabasi-Albert model of scale-free graphs. More...
 
PUNGraph GenConfModel (const PUNGraph &G)
 Generate a random graph using (approximately) the same node degrees as in G using the configuration model. More...
 
PUNGraph GenGeoPrefAttach (const int &Nodes, const int &OutDeg, const double &Beta, TRnd &Rnd=TInt::Rnd)
 Generates a random scale-free graph using the Geometric Preferential model. More...
 
PUNGraph GenSmallWorld (const int &Nodes, const int &NodeOutDeg, const double &RewireProb, TRnd &Rnd=TInt::Rnd)
 Generates a randomly small-world graph using the Watts-Strogatz model. More...
 
PNGraph GenForestFire (const int &Nodes, const double &FwdProb, const double &BckProb)
 Generates a random Forest Fire, directed graph with given probabilities. More...
 
PNGraph GenCopyModel (const int &Nodes, const double &Beta, TRnd &Rnd=TInt::Rnd)
 Generates a random scale-free network using the Copying Model. More...
 
PNGraph GenRMat (const int &Nodes, const int &Edges, const double &A, const double &B, const double &C, TRnd &Rnd=TInt::Rnd)
 Generates a R-MAT graph using recursive descent into a 2x2 matrix [A,B; C, 1-(A+B+C)]. More...
 
PNGraph GenRMatEpinions ()
 Generates a R-Mat graph, with a synthetic copy of the Epinions social network. More...
 
template<class PGraph >
PGraph GenGrid (const int &Rows, const int &Cols, const bool &IsDir=true)
 Generates a 2D-grid graph of Rows rows and Cols columns. More...
 
template<class PGraph >
PGraph GenStar (const int &Nodes, const bool &IsDir=true)
 Generates a graph with star topology. Node id 0 is in the center and then links to all other nodes. More...
 
template<class PGraph >
PGraph GenCircle (const int &Nodes, const int &NodeOutDeg=1, const bool &IsDir=true)
 Generates a circle graph where every node creates out-links to NodeOutDeg forward nodes. More...
 
template<class PGraph >
PGraph GenFull (const int &Nodes)
 Generates a complete graph on Nodes nodes. Graph has no self-loops. More...
 
template<class PGraph >
PGraph GenTree (const int &Fanout, const int &Levels, const bool &IsDir=true, const bool &ChildPointsToParent=true)
 Generates a tree graph of Levels levels with every parent having Fanout children. More...
 
template<class PGraph >
PGraph GenBaraHierar (const int &Levels, const bool &IsDir=true)
 Generates a Ravasz-Barabasi deterministic scale-free graph. More...
 
template<class PGraph >
PGraph GenRndGnm (const int &Nodes, const int &Edges, const bool &IsDir=true, TRnd &Rnd=TInt::Rnd)
 Generates an Erdos-Renyi random graph. More...
 
int ReadEdgeSchemaFromFile (TSsParser &Ss, const char &Separator, int &SrcColId, int &DstColId, TStrIntH &IntAttrEVals, TStrIntH &FltAttrEVals, TStrIntH &StrAttrEVals)
 
bool ReadEdgesFromFile (TSsParser &Ss, const char &Separator, PNEANet &Graph, int &SrcColId, int &DstColId, TStrIntH &IntAttrEVals, TStrIntH &FltAttrEVals, TStrIntH &StrAttrEVals)
 
int ReadNodeSchemaFromFile (TSsParser &Ss, const char &Separator, int &NId, TStrIntH &IntAttrNVals, TStrIntH &FltAttrNVals, TStrIntH &StrAttrNVals)
 
bool ReadNodesFromFile (TSsParser &Ss, const char &Separator, PNEANet &Graph, int &NColId, TStrIntH &IntAttrNVals, TStrIntH &FltAttrNVals, TStrIntH &StrAttrNVals)
 
PNEANet LoadEdgeListNet (const TStr &InFNm, const char &Separator)
 Loads a network from the text file InFNm with 1 node/edge per line ('Separator' separated columns, integer node id(s) + node/edge attributes). More...
 
void WriteNodeSchemaToFile (FILE *F, TStrV &IntAttrNNames, TStrV &FltAttrNNames, TStrV &StrAttrNNames)
 
void WriteNodesToFile (FILE *F, const PNEANet &Graph, TStrV &IntAttrNNames, TStrV &FltAttrNNames, TStrV &StrAttrNNames)
 
void WriteEdgeSchemaToFile (FILE *F, TStrV &IntAttrENames, TStrV &FltAttrENames, TStrV &StrAttrENames)
 
void WriteEdgesToFile (FILE *F, const PNEANet &Graph, TStrV &IntAttrENames, TStrV &FltAttrENames, TStrV &StrAttrENames)
 
void SaveEdgeListNet (const PNEANet &Graph, const TStr &OutFNm, const TStr &Desc)
 Saves a network into a text file. Each line encodes either an edge or a node, along with its attributes. More...
 
PNGraph LoadDyNet (const TStr &FNm)
 For more info see ORA Network Analysis Data (http://www.casos.cs.cmu.edu/computational_tools/data2.php) More...
 
TVec< PNGraphLoadDyNetGraphV (const TStr &FNm)
 For more info see ORA Network Analysis Data (http://www.casos.cs.cmu.edu/computational_tools/data2.php) More...
 
template<class PGraph >
PGraph LoadEdgeList (const TStr &InFNm, const int &SrcColId=0, const int &DstColId=1)
 Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line (whitespace separated columns, integer node ids). More...
 
template<class PGraph >
PGraph LoadEdgeList (const TStr &InFNm, const int &SrcColId, const int &DstColId, const char &Separator)
 Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line ('Separator' separated columns, integer node ids). More...
 
template<class PGraph >
PGraph LoadEdgeListStr (const TStr &InFNm, const int &SrcColId=0, const int &DstColId=1)
 Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line (whitespace separated columns, arbitrary string node ids). More...
 
template<class PGraph >
PGraph LoadEdgeListStr (const TStr &InFNm, const int &SrcColId, const int &DstColId, TStrHash< TInt > &StrToNIdH)
 Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line (whitespace separated columns, arbitrary string node ids). More...
 
template<class PGraph >
PGraph LoadConnList (const TStr &InFNm)
 Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in a single line. More...
 
template<class PGraph >
PGraph LoadConnListStr (const TStr &InFNm, TStrHash< TInt > &StrToNIdH)
 Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in a single line. More...
 
template<class PGraph >
PGraph LoadPajek (const TStr &InFNm)
 Loads a (directed, undirected or multi) graph from Pajek .PAJ format file. More...
 
template<class PGraph >
void SaveEdgeList (const PGraph &Graph, const TStr &OutFNm, const TStr &Desc=TStr())
 Saves a graph into a text file. Each line contains two columns and encodes a single edge: <source node="" id>=""><tab><destination node="" id>=""> More...
 
template<class PGraph >
void SavePajek (const PGraph &Graph, const TStr &OutFNm)
 Saves a graph in a Pajek .NET format. More...
 
template<class PGraph >
void SavePajek (const PGraph &Graph, const TStr &OutFNm, const TIntStrH &NIdColorH)
 Saves a graph in a Pajek .NET format. More...
 
template<class PGraph >
void SavePajek (const PGraph &Graph, const TStr &OutFNm, const TIntStrH &NIdColorH, const TIntStrH &NIdLabelH)
 Saves a graph in a Pajek .NET format. More...
 
template<class PGraph >
void SavePajek (const PGraph &Graph, const TStr &OutFNm, const TIntStrH &NIdColorH, const TIntStrH &NIdLabelH, const TIntStrH &EIdColorH)
 Saves a graph in a Pajek .NET format. More...
 
template<class PGraph >
void SaveMatlabSparseMtx (const PGraph &Graph, const TStr &OutFNm)
 Saves a graph in a MATLAB sparse matrix format. More...
 
template<class PGraph >
void SaveGViz (const PGraph &Graph, const TStr &OutFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
 Save a graph in GraphVizp .DOT format. More...
 
template<class PGraph >
void SaveGViz (const PGraph &Graph, const TStr &OutFNm, const TStr &Desc, const TIntStrH &NIdLabelH)
 Save a graph in GraphVizp .DOT format. More...
 
void SetAllInvertSign (TFltV &ValV, const double &Val)
 
bool IsAllValVNeg (TFltV &ValV, const bool &InvertSign)
 
void GetSngVals (const PNGraph &Graph, const int &SngVals, TFltV &SngValV)
 Computes largest SngVals singular values of the adjacency matrix representing a directed Graph. More...
 
void GetSngVec (const PNGraph &Graph, TFltV &LeftSV, TFltV &RightSV)
 Computes the leading left and right singular vector of the adjacency matrix representing a directed Graph. More...
 
void GetSngVec (const PNGraph &Graph, const int &SngVecs, TFltV &SngValV, TFltVFltV &LeftSV, TFltVFltV &RightSV)
 
void GetEigVals (const PUNGraph &Graph, const int &EigVals, TFltV &EigValV)
 Computes top EigVals eigenvalues of the adjacency matrix representing a given undirected Graph. More...
 
void GetEigVec (const PUNGraph &Graph, TFltV &EigVecV)
 Computes the leading eigenvector of the adjacency matrix representing a given undirected Graph. More...
 
void GetEigVec (const PUNGraph &Graph, const int &EigVecs, TFltV &EigValV, TFltVFltV &EigVecV)
 Computes top EigVecs eigenvalues and eigenvectors of the adjacency matrix representing a given undirected Graph. More...
 
void GetInvParticipRat (const PUNGraph &Graph, int MaxEigVecs, int TimeLimit, TFltPrV &EigValIprV)
 
template<class PGraph >
void DrawGViz (const PGraph &Graph, const TGVizLayout &Layout, const TStr &PltFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
 Draws a given Graph using a selected GraphViz Layout engine with nodes colored. More...
 
template<class PGraph >
void DrawGViz (const PGraph &Graph, const TGVizLayout &Layout, const TStr &PltFNm, const TStr &Desc, const TIntStrH &NodeLabelH)
 Draws a given Graph using a selected GraphViz Layout engine with nodes labeled. More...
 
template<class PGraph >
PGraph GetKCore (const PGraph &Graph, const int &K)
 
void TIntVToNumpy (TIntV &IntV, int *IntNumpyVecOut, int n)
 Converts TIntV to Numpy array. More...
 
void TFltVToNumpy (TFltV &FltV, float *FltNumpyVecOut, int n)
 Converts TFltV to Numpy array. More...
 
void NumpyToTIntV (TIntV &IntV, int *IntNumpyVecIn, int n)
 Converts NumpyArray to TIntV. More...
 
void NumpyToTFltV (TFltV &FltV, float *FltNumpyVecIn, int n)
 Converts NumpyArray to TFltV. More...
 
template<class PGraph >
int SamplePersonalizedPageRank (const PGraph &Graph, double JumpProb, const TIntV &StartNIdV, TRnd &Rnd)
 
template<class PGraph >
double GetPersonalizedPageRankBidirectional (const PGraph &Graph, double JumpProb, const TIntV &StartNIdV, int TargetNId, double MinProbability=-1.0, double RelativeError=0.1, bool provableRelativeError=false, bool PrintTimeForTuning=false)
 
template<class PGraph >
double GetRndWalkRestartBidirectional (const PGraph &Graph, double JumpProb, int StartNId, int TargetNId, double minProbability=-1.0, double relativeError=0.1, bool proveRelativeError=false, bool PrintTimeForTuning=false)
 
void PlotEigValRank (const PUNGraph &Graph, const int &EigVals, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the eigen-value rank distribution of the Graph adjacency matrix. Plots first EigVals eigenvalues. More...
 
void PlotEigValDistr (const PUNGraph &Graph, const int &EigVals, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the distribution of components of the leading eigen-vector of the Graph adjacency matrix. Plots first EigVals values. More...
 
void PlotInvParticipRat (const PUNGraph &Graph, const int &MaxEigVecs, const int &TimeLimit, const TStr &FNmPref, TStr DescStr)
 
void PlotSngValRank (const PNGraph &Graph, const int &SngVals, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the rank distribution of singular values of the Graph adjacency matrix. Plots first SngVals values. More...
 
void PlotSngValDistr (const PNGraph &Graph, const int &SngVals, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the rank distribution of singular values of the Graph adjacency matrix. Plots first SngVals values. More...
 
void PlotSngVec (const PNGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the distribution of the values of the leading left singular vector of the Graph adjacency matrix. Plots first SngVals values. More...
 
template<class PGraph >
void PlotInDegDistr (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr(), const bool &PlotCCdf=false, const bool &PowerFit=false)
 
template<class PGraph >
void PlotOutDegDistr (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr(), const bool &PlotCCdf=false, const bool &PowerFit=false)
 
template<class PGraph >
void PlotWccDistr (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the distribution of sizes of weakly connected components of a Graph. More...
 
template<class PGraph >
void PlotSccDistr (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the distribution of sizes of strongly connected components of a Graph. More...
 
template<class PGraph >
void PlotClustCf (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the distribution of clustering coefficient of a Graph. More...
 
template<class PGraph >
void PlotHops (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr(), const bool &IsDir=false, const int &NApprox=32)
 
template<class PGraph >
void PlotShortPathDistr (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr(), int TestNodes=TInt::Mx)
 Plots the distribution of the shortest path lengths of a Graph. Implementation is based on BFS. More...
 
template<class PGraph >
void PlotKCoreNodes (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the k-Core node-size distribution: Core k vs. number of nodes in k-core. More...
 
template<class PGraph >
void PlotKCoreEdges (const PGraph &Graph, const TStr &FNmPref, TStr DescStr=TStr())
 Plots the k-Core edge-size distribution: Core k vs. number of edges in k-core. More...
 
PUNGraph GetSubGraph (const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes=false)
 Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumbering. More...
 
PNGraph GetSubGraph (const PNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
 
PUNGraph GetEgonet (const PUNGraph &Graph, const int CtrNId, int &ArndEdgesX)
 Returns the egonet of node CtrNId as center in undirected graph Graph. And returns number of edges around the egonet. More...
 
PNGraph GetEgonet (const PNGraph &Graph, const int CtrNId, int &InEgoEdgesX, int &OutEgoEdgesX)
 Returns the egonet of node CtrNId as center in directed graph Graph. And returns number of edges go in and out the egonet. More...
 
void AddNodeWithAttributes (const PNEANet &Graph1, PNEANet &Graph2, const int NId)
 
void AddEdgeWithAttributes (const PNEANet &Graph1, PNEANet &Graph2, const int EId)
 
void AddEdgeWithAttributes (const PNEANet &Graph1, PNEANet &Graph2, const int NId, const int NbrId)
 
PNEANet GetEgonetAttr (const PNEANet &Graph, const int CtrNId, const int Radius)
 Returns the complete egonet of at given radius and copies node and edge attributes. More...
 
PNEANet GetInEgonetAttr (const PNEANet &Graph, const int CtrNId, const int Radius)
 Returns the in-egonet of at given radius and copies node and edge attributes. More...
 
PNEANet GetOutEgonetAttr (const PNEANet &Graph, const int CtrNId, const int Radius)
 Returns the out-egonet of at given radius and copies node and edge attributes. More...
 
PNEANet GetInEgonetSubAttr (const PNEANet &Graph, const int CtrNId, const int Radius, const int MaxNum, const float percent)
 Returns the randomly sampled egonet with nodes sampled based on percentage or raw number, copying attributes. More...
 
PNEANet GetGraphUnionAttr (PNEANet &DstGraph, const PNEANet &SrcGraph)
 
template<class PGraph >
PGraph GetSubGraph (const PGraph &Graph, const TIntV &NIdV)
 Returns an induced subgraph of graph Graph with NIdV nodes. More...
 
template<class PGraph >
PGraph GetSubGraphRenumber (const PGraph &Graph, const TIntV &NIdV)
 Returns an induced subgraph of graph Graph with NIdV nodes with an node renumbering. More...
 
template<class PGraph >
PGraph GetESubGraph (const PGraph &Graph, const TIntV &EIdV)
 Returns a subgraph of graph Graph with EIdV edges. More...
 
template<class PGraph >
PGraph GetESubGraph (const PGraph &Graph, const TIntPrV &EdgeV)
 
template<class PGraph , class TEdgeDat >
PGraph GetEDatSubGraph (const PGraph &Graph, const TEdgeDat &EDat, const int &Cmp)
 Returns a subgraph of graph Graph with edges where edge data matches the parameters. More...
 
template<class PGraph , class TEdgeDat >
PGraph GetEDatSubGraph (const PGraph &Graph, const TIntV &NIdV, const TEdgeDat &EDat, const int &Cmp)
 Returns a subgraph of graph Graph with NIdV nodes and edges where edge data matches the parameters. More...
 
template<class POutGraph , class PInGraph >
POutGraph ConvertGraph (const PInGraph &InGraph, const bool &RenumberNodes=false)
 Performs conversion of graph InGraph with an optional node renumbering. More...
 
template<class POutGraph , class PInGraph >
POutGraph ConvertSubGraph (const PInGraph &InGraph, const TIntV &NIdV, const bool &RenumberNodes=false)
 Returns an induced subgraph of graph InGraph with NIdV nodes with an optional node renumbering. More...
 
template<class POutGraph , class PInGraph >
POutGraph ConvertESubGraph (const PInGraph &InGraph, const TIntV &EIdV, const bool &RenumberNodes=false)
 Returns a subgraph of graph InGraph with EIdV edges with an optional node renumbering. More...
 
template<class PGraph >
PGraph GetRndSubGraph (const PGraph &Graph, const int &NNodes)
 Returns an induced random subgraph of graph Graph with NNodes nodes. More...
 
template<class PGraph >
PGraph GetRndESubGraph (const PGraph &Graph, const int &NEdges)
 Returns a random subgraph of graph Graph with NEdges edges. More...
 
template<class PGraph >
PGraph GetEgonetHop (const PGraph &Graph, const int CtrNId, const int Radius)
 Returns the egonet of node CtrNId as center for a Graph for a given radius. More...
 
template<class PGraph >
PGraph GetInEgonetHop (const PGraph &Graph, const int CtrNId, const int Radius)
 Returns the in-egonet of node CtrNId as center in directed graph Graph for a given radius. More...
 
template<class PGraph >
PGraph GetOutEgonetHop (const PGraph &Graph, const int CtrNId, const int Radius)
 Returns the out-egonet of node CtrNId as center in directed graph Graph for a given radius. More...
 
template<class PGraph >
PGraph GetInEgonetSub (const PGraph &Graph, const int CtrNId, const int Radius, const int MaxNum=2, const float percent=-1.0)
 Returns the randomly sampled in-egonet with nodes sampled based on percentage, if percent != -1.0, or MaxNum nodes otherwise. More...
 
template<class PGraph >
PGraph GetGraphUnion (PGraph &DstGraph, const PGraph &SrcGraph)
 
int GetCommon (TIntV &A, TIntV &B)
 Returns the number of common elements in two sorted TInt vectors. More...
 
template<class PGraph >
double GetClustCf (const PGraph &Graph, int SampleNodes=-1)
 Computes the average clustering coefficient as defined in Watts and Strogatz, Collective dynamics of 'small-world' networks. More...
 
template<class PGraph >
double GetClustCf (const PGraph &Graph, TFltPrV &DegToCCfV, int SampleNodes=-1)
 Computes the distribution of average clustering coefficient. More...
 
template<class PGraph >
double GetClustCf (const PGraph &Graph, TFltPrV &DegToCCfV, int64 &ClosedTriads, int64 &OpenTriads, int SampleNodes=-1)
 Computes the distribution of average clustering coefficient as well as the number of open and closed triads in the graph. More...
 
template<class PGraph >
double GetClustCfAll (const PGraph &Graph, TFltPrV &DegToCCfV, int64 &ClosedTriadsX, int64 &OpenTriadsX, int SampleNodes=-1)
 Computes the distribution of average clustering coefficient as well as the number of open and closed triads in the graph. More...
 
template<class PGraph >
double GetNodeClustCf (const PGraph &Graph, const int &NId)
 Returns clustering coefficient of a particular node. More...
 
template<class PGraph >
void GetNodeClustCf (const PGraph &Graph, TIntFltH &NIdCCfH)
 Computes clustering coefficient of each node of the Graph. More...
 
template<class PGraph >
int64 GetTriads (const PGraph &Graph, int SampleNodes=-1)
 Returns the number of triangles in a graph. More...
 
template<class PGraph >
int64 GetTriadsAll (const PGraph &Graph, int64 &ClosedTriadsX, int64 &OpenTriadsX, int SampleNodes=-1)
 Computes the number of Closed and Open triads. More...
 
template<class PGraph >
void GetTriads (const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
 Computes the number of open and close triads for every node of the network. More...
 
template<class PGraph >
int GetTriadEdges (const PGraph &Graph, int SampleEdges=-1)
 Counts the number of edges that participate in at least one triad. More...
 
template<class PGraph >
int GetNodeTriads (const PGraph &Graph, const int &NId)
 Returns the number of undirected triads a node NId participates in. More...
 
template<class PGraph >
int GetNodeTriads (const PGraph &Graph, const int &NId, int &ClosedNTriadsX, int &OpenNTriadsX)
 Returns number of Open and Closed triads a node NId participates in. More...
 
template<class PGraph >
int GetNodeTriadsAll (const PGraph &Graph, const int &NId, int &ClosedNTriadsX, int &OpenNTriadsX)
 Returns number of Open and Closed triads a node NId participates in. More...
 
template<class PGraph >
int GetNodeTriads (const PGraph &Graph, const int &NId, const TIntSet &GroupSet, int &InGroupEdgesX, int &InOutGroupEdgesX, int &OutGroupEdgesX)
 Returns the number of triads between a node NId and a subset of its neighbors GroupSet. More...
 
template<class PGraph >
void GetTriadParticip (const PGraph &Graph, TIntPrV &TriadCntV)
 Triangle Participation Ratio: For each node counts how many triangles it participates in and then returns a set of pairs (number of triangles, number of such nodes). More...
 
template<class PGraph >
int GetCmnNbrs (const PGraph &Graph, const int &NId1, const int &NId2)
 Returns a number of shared neighbors between a pair of nodes NId1 and NId2. More...
 
template<class PGraph >
int GetCmnNbrs (const PGraph &Graph, const int &NId1, const int &NId2, TIntV &NbrV)
 Returns the shared neighbors between a pair of nodes NId1 and NId2. More...
 
template<class PGraph >
int GetLen2Paths (const PGraph &Graph, const int &NId1, const int &NId2)
 Returns the number of length 2 directed paths between a pair of nodes NId1, NId2 (NId1 –> U –> NId2). More...
 
template<class PGraph >
int GetLen2Paths (const PGraph &Graph, const int &NId1, const int &NId2, TIntV &NbrV)
 Returns the 2 directed paths between a pair of nodes NId1, NId2 (NId1 –> U –> NId2). More...
 
template<class PGraph >
int64 GetTriangleCnt (const PGraph &Graph)
 Returns the number of triangles in graph Graph. More...
 
template<class PGraph >
void MergeNbrs (TIntV &NeighbourV, const typename PGraph::TObj::TNodeI &NI)
 Merges neighbors by removing duplicates and produces one sorted vector of neighbors. More...
 
template<class PGraph >
void GetUniqueNbrV (const PGraph &Graph, const int &NId, TIntV &NbrV)
 Returns sorted vector NbrV containing unique in or out neighbors of node NId in graph Graph. More...
 
template<class PGraph >
void GetTriads_v0 (const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes)
 
template<>
int GetCmnNbrs< PUNGraph > (const PUNGraph &Graph, const int &NId1, const int &NId2, TIntV &NbrV)
 

Variables

const TStr CapAttrName = "capacity"
 
const TStr EDGES_START = ("#EDGES")
 
const TStr NODES_START = ("#NODES")
 
const TStr END_SENTINEL = ("#END")
 
const TStr SRC_ID_NAME = ("SrcNId")
 
const TStr DST_ID_NAME = ("DstNId")
 
const TStr NID_NAME = ("NId")
 
const TStr INT_TYPE_PREFIX = ("Int")
 
const TStr FLT_TYPE_PREFIX = ("Flt")
 
const TStr STR_TYPE_PREFIX = ("Str")
 
const TStr NULL_VAL = ("__null__")
 

Detailed Description

Main namespace for all the Snap global entities.

The name of the friend is not found by simple name lookup until a matching declaration is provided in that namespace scope (either before or after the class declaration granting friendship).

Function Documentation

void TSnap::AddEdgeWithAttributes ( const PNEANet Graph1,
PNEANet Graph2,
const int  EId 
)

Definition at line 179 of file subgraph.cpp.

References TVec< TVal, TSizeTy >::Len().

Referenced by GetEgonetAttr(), GetGraphUnionAttr(), GetInEgonetAttr(), GetInEgonetSubAttr(), and GetOutEgonetAttr().

179  {
180  const TNEANet::TEdgeI EI = Graph1->GetEI(EId);
181  Graph2->AddEdge(EI);
182  //const int EId = Graph2->GetEId(NId, NbrId);
183  // Copy Int Attributes
184  TStrV IntAttrNames;
185  TIntV IntAttrValues;
186  Graph1->IntAttrNameEI(EId, IntAttrNames);
187  Graph1->IntAttrValueEI(EId, IntAttrValues);
188  for (int i = 0; i < IntAttrNames.Len(); i++) {
189  Graph2->AddIntAttrDatE(EId, IntAttrValues[i], IntAttrNames[i]);
190  }
191  // Copy Float Attributes
192  TStrV FltAttrNames;
193  TFltV FltAttrValues;
194  Graph1->FltAttrNameEI(EId, FltAttrNames);
195  Graph1->FltAttrValueEI(EId, FltAttrValues);
196  for (int i = 0; i < FltAttrNames.Len(); i++) {
197  Graph2->AddFltAttrDatE(EId, FltAttrValues[i], FltAttrNames[i]);
198  }
199  // Copy Str Attributes
200  TStrV StrAttrNames;
201  TStrV StrAttrValues;
202  Graph1->StrAttrNameEI(EId, StrAttrNames);
203  Graph1->StrAttrValueEI(EId, StrAttrValues);
204  for (int i = 0; i < StrAttrNames.Len(); i++) {
205  Graph2->AddStrAttrDatE(EId, StrAttrValues[i], StrAttrNames[i]);
206  }
207  // Copy IntV Attributes
208  TStrV IntVAttrNames;
209  TVec<TIntV> IntVAttrValues;
210  Graph1->IntVAttrNameEI(EId, IntVAttrNames);
211  Graph1->IntVAttrValueEI(EId, IntVAttrValues);
212  for (int i = 0; i < IntVAttrNames.Len(); i++) {
213  Graph2->AddIntVAttrDatE(EId, IntVAttrValues[i], IntVAttrNames[i]);
214  }
215 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1867

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::AddEdgeWithAttributes ( const PNEANet Graph1,
PNEANet Graph2,
const int  NId,
const int  NbrId 
)

Definition at line 217 of file subgraph.cpp.

References TVec< TVal, TSizeTy >::Len().

217  {
218  Graph2->AddEdge(NId, NbrId);
219  const int EId = Graph2->GetEId(NId, NbrId);
220  // Copy Int Attributes
221  TStrV IntAttrNames;
222  TIntV IntAttrValues;
223  Graph1->IntAttrNameEI(EId, IntAttrNames);
224  Graph1->IntAttrValueEI(EId, IntAttrValues);
225  for (int i = 0; i < IntAttrNames.Len(); i++) {
226  Graph2->AddIntAttrDatE(EId, IntAttrValues[i], IntAttrNames[i]);
227  }
228  // Copy Float Attributes
229  TStrV FltAttrNames;
230  TFltV FltAttrValues;
231  Graph1->FltAttrNameEI(EId, FltAttrNames);
232  Graph1->FltAttrValueEI(EId, FltAttrValues);
233  for (int i = 0; i < FltAttrNames.Len(); i++) {
234  Graph2->AddFltAttrDatE(EId, FltAttrValues[i], FltAttrNames[i]);
235  }
236  // Copy Str Attributes
237  TStrV StrAttrNames;
238  TStrV StrAttrValues;
239  Graph1->StrAttrNameEI(EId, StrAttrNames);
240  Graph1->StrAttrValueEI(EId, StrAttrValues);
241  for (int i = 0; i < StrAttrNames.Len(); i++) {
242  Graph2->AddStrAttrDatE(EId, StrAttrValues[i], StrAttrNames[i]);
243  }
244  // Copy IntV Attributes
245  TStrV IntVAttrNames;
246  TVec<TIntV> IntVAttrValues;
247  Graph1->IntVAttrNameEI(EId, IntVAttrNames);
248  Graph1->IntVAttrValueEI(EId, IntVAttrValues);
249  for (int i = 0; i < IntVAttrNames.Len(); i++) {
250  Graph2->AddIntVAttrDatE(EId, IntVAttrValues[i], IntVAttrNames[i]);
251  }
252 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575

Here is the call graph for this function:

void TSnap::AddNodeWithAttributes ( const PNEANet Graph1,
PNEANet Graph2,
const int  NId 
)

Definition at line 143 of file subgraph.cpp.

References TVec< TVal, TSizeTy >::Len().

Referenced by GetEgonetAttr(), GetGraphUnionAttr(), GetInEgonetAttr(), GetInEgonetSubAttr(), and GetOutEgonetAttr().

143  {
144  Graph2->AddNode(NId);
145  // Copy Int Attributes
146  TStrV IntAttrNames;
147  TIntV IntAttrValues;
148  Graph1->IntAttrNameNI(NId, IntAttrNames);
149  Graph1->IntAttrValueNI(NId, IntAttrValues);
150  for (int i = 0; i < IntAttrNames.Len(); i++) {
151  Graph2->AddIntAttrDatN(NId, IntAttrValues[i], IntAttrNames[i]);
152  }
153  // Copy Float Attributes
154  TStrV FltAttrNames;
155  TFltV FltAttrValues;
156  Graph1->FltAttrNameNI(NId, FltAttrNames);
157  Graph1->FltAttrValueNI(NId, FltAttrValues);
158  for (int i = 0; i < FltAttrNames.Len(); i++) {
159  Graph2->AddFltAttrDatN(NId, FltAttrValues[i], FltAttrNames[i]);
160  }
161  // Copy Str Attributes
162  TStrV StrAttrNames;
163  TStrV StrAttrValues;
164  Graph1->StrAttrNameNI(NId, StrAttrNames);
165  Graph1->StrAttrValueNI(NId, StrAttrValues);
166  for (int i = 0; i < StrAttrNames.Len(); i++) {
167  Graph2->AddStrAttrDatN(NId, StrAttrValues[i], StrAttrNames[i]);
168  }
169  // Copy IntV Attributes
170  TStrV IntVAttrNames;
171  TVec<TIntV> IntVAttrValues;
172  Graph1->IntVAttrNameNI(NId, IntVAttrNames);
173  Graph1->IntVAttrValueNI(NId, IntVAttrValues);
174  for (int i = 0; i < IntVAttrNames.Len(); i++) {
175  Graph2->AddIntVAttrDatN(NId, IntVAttrValues[i], IntVAttrNames[i]);
176  }
177 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
void TSnap::AddSelfEdges ( const PGraph &  Graph)

Adds a self-edge to every node in the graph.

Definition at line 369 of file alg.h.

References TVec< TVal, TSizeTy >::Add(), and TVec< TVal, TSizeTy >::Len().

369  {
370  TIntV EdgeV;
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)) {
374  EdgeV.Add(NId);
375  }
376  }
377  for (int i = 0; i < EdgeV.Len(); i++) {
378  Graph->AddEdge(EdgeV[i], EdgeV[i]);
379  }
380 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

TIntH* TSnap::AllCombinationsMN ( int  m,
int  n 
)

Definition at line 157 of file centr.cpp.

157  {
158  float N = 1;
159  for(int i=n; i>0; i--){
160  N *= (float)m/(float)n;
161  m--;
162  n--;
163  }
164 
165  TIntH* C = new TIntH[(int)N];
166  return C;
167 }
PUNGraph* TSnap::AllGraphsWithNNodes ( int  n)

Definition at line 138 of file centr.cpp.

References TUNGraph::AddEdge(), and TUNGraph::AddNode().

138  {
139  PUNGraph* g = new PUNGraph[(((n*n)-n)/2)+1];
140  PUNGraph g0;
141  for(int i=0; i<n; i++)
142  g0->AddNode(i);
143 
144  g[0] = g0;
145  int br=1;
146 
147  for(int i=0; i<n; i++)
148  for(int j=i; j<n; j++){
149  g0->AddEdge(i,j);
150  g[br] = g0;
151  br++;
152  }
153 
154  return g;
155 }
Definition: bd.h:196

Here is the call graph for this function:

double TSnap::BorgattiEverettMeasure ( PUNGraph Graph,
TIntIntH out,
double  coresize,
int  type 
)

Definition at line 186 of file coreper.cpp.

References TUNGraph::BegEI(), TUNGraph::EndEI(), and THash< TKey, TDat, THashFunc >::GetDat().

186  {
187 
188  double sum = 0.0;
189  for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++){ // Calculate and store the degrees of each node.
190  int i = EI.GetSrcNId();
191  int j = EI.GetDstNId();
192  if (type == 1) {
193  if (out.GetDat(i) == 1 || out.GetDat(j) == 1)
194  sum += 1;
195  }
196  else {
197  if (out.GetDat(i) == 1 && out.GetDat(j) == 1)
198  sum += 1;
199  }
200  }
201 
202  return sum/(((coresize*coresize)-coresize)/2);
203  }
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:121
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262

Here is the call graph for this function:

void TSnap::CascFind ( PNGraph  Graph,
PTable  P,
const TStr  C1,
const TStr  C2,
const TStr  C3,
const TStr  C4,
TVec< TIntV > &  TopCascVV,
bool  Print 
)

Takes as input a directed graph and returns all the top cascades in TopCascVV.

Definition at line 135 of file casc.cpp.

References TVec< TVal, TSizeTy >::Add(), THashSet< TKey, THashFunc >::AddKey(), TVec< TVal, TSizeTy >::BegI(), TSnapQueue< TVal >::Empty(), TVec< TVal, TSizeTy >::EndI(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), TVec< TVal, TSizeTy >::GetVal(), THashSet< TKey, THashFunc >::IsKey(), TVec< TVal, TSizeTy >::Len(), THashSet< TKey, THashFunc >::Len(), TSnapQueue< TVal >::Pop(), TSnapQueue< TVal >::Push(), TVec< TVal, TSizeTy >::Sort(), TSnapQueue< TVal >::Top(), and TInt::Val.

135  {
136  // Attribute to Int mapping
137  TInt SIdx = P->GetColIdx(C1);
138  TInt DIdx = P->GetColIdx(C2);
139  TInt StIdx = P->GetColIdx(C3);
140  TInt DuIdx = P->GetColIdx(C4);
141  TIntV MapV, PhyV;
142  TStrV SortBy;
143  SortBy.Add(C3);
144  P->Order(SortBy);
145  int count = 0;
146  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
147  MapV.Add(RI.GetRowIdx());
148  PhyV.Add(count++);
149  }
150  // After sort attach with each row a rank helpful for sorting
151  P->StoreIntCol("Physical",PhyV);
152  TInt PIdx = P->GetColIdx("Physical");
153  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
154  // Check for top cascades
155  if (NI.GetInDeg() != 0) { continue;}
156  TIntV CurCasc;
157  TSnapQueue<TInt> EventQ;
158  THashSet<TInt> VisitedH;
159  TInt NId = NI.GetId();
160  EventQ.Push(NId);
161  VisitedH.AddKey(NId);
162  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,NId));
163  while (! EventQ.Empty()) {
164  TNGraph::TNodeI CNI = Graph->GetNI(EventQ.Top().Val); //Get Current Node
165  EventQ.Pop();
166  // Go over the outdegree nodes of the currernt node
167  for (int e = 0; e < CNI.GetOutDeg(); e++) {
168  TInt CId = CNI.GetOutNId(e);
169  if ( !VisitedH.IsKey(CId)) {
170  EventQ.Push(CId);
171  VisitedH.AddKey(CId);
172  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,CId));
173  }
174  }
175  }
176  CurCasc.Sort();
177  TIntV ToAddV;
178  if (Print && VisitedH.Len() > 1) {
179  printf("__casacade__\t%d\n",VisitedH.Len());
180  }
181  for (TIntV::TIter VI = CurCasc.BegI(); VI < CurCasc.EndI(); VI++) {
182  ToAddV.Add(MapV.GetVal(VI->Val));
183  if (Print && VisitedH.Len() > 1) {
184  int PIdx = MapV.GetVal(VI->Val).Val;
185  int PSource = P->GetIntValAtRowIdx(SIdx,PIdx).Val;
186  int PDest = P->GetIntValAtRowIdx(DIdx,PIdx).Val;
187  int PStart = P->GetIntValAtRowIdx(StIdx,PIdx).Val;
188  int PDur = P->GetIntValAtRowIdx(DuIdx,PIdx).Val;
189  printf("%d\t%d\t%d\t%d\t%d\n",PIdx,PSource,PDest,PStart,PDur);
190  }
191  }
192  if (ToAddV.Len() > 1) {
193  TopCascVV.Add(ToAddV);
194  }
195  }
196  return;
197 }
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:595
int Val
Definition: dt.h:1139
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool IsKey(const TKey &Key) const
Definition: shash.h:1148
void Pop()
Removes the first element from the queue.
Definition: gbase.h:211
bool Empty() const
Tests whether the queue is empty (contains no elements).
Definition: gbase.h:199
Iterator class for TTable rows.
Definition: table.h:330
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
int AddKey(const TKey &Key)
Definition: shash.h:1254
Definition: dt.h:1137
int Len() const
Definition: shash.h:1121
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:406
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
Definition: ds.h:593
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
Fast Queue used by the TBreathFS (uses memcpy to move objects TVal around).
Definition: gbase.h:158
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:416

Here is the call graph for this function:

void TSnap::CascFindMP ( PNGraph  Graph,
PTable  P,
const TStr  C1,
const TStr  C2,
const TStr  C3,
const TStr  C4,
TVec< TIntV > &  TopCascVV 
)

Parallel implementaion of CascFind takes as input a directed graph and returns all the top cascades in TopCascVV.

Definition at line 200 of file casc.cpp.

References TVec< TVal, TSizeTy >::Add(), THashSet< TKey, THashFunc >::AddKey(), TVec< TVal, TSizeTy >::BegI(), TSnapQueue< TVal >::Empty(), TVec< TVal, TSizeTy >::EndI(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), TVec< TVal, TSizeTy >::GetVal(), THashSet< TKey, THashFunc >::IsKey(), TVec< TVal, TSizeTy >::Len(), TSnapQueue< TVal >::Pop(), TSnapQueue< TVal >::Push(), TVec< TVal, TSizeTy >::Sort(), TSnapQueue< TVal >::Top(), and TInt::Val.

200  {
201  // Attribute to Int mapping
202  TInt SIdx = P->GetColIdx(C1);
203  TInt DIdx = P->GetColIdx(C2);
204  TInt StIdx = P->GetColIdx(C3);
205  TInt DuIdx = P->GetColIdx(C4);
206  TIntV MapV, PhyV;
207  TStrV SortBy;
208  SortBy.Add(C3);
209  P->Order(SortBy);
210  int count = 0;
211  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
212  MapV.Add(RI.GetRowIdx());
213  PhyV.Add(count++);
214  }
215  P->StoreIntCol("Physical",PhyV);
216  TInt PIdx = P->GetColIdx("Physical");
217  TIntV GNodeV;
218  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
219  if (NI.GetInDeg() == 0) { GNodeV.Add(NI.GetId()); }
220  }
221  TVec<TIntV> ThTopCascVV; // for each thread
222  #pragma omp parallel private(ThTopCascVV) num_threads(10)
223  {
224  #pragma omp for schedule(dynamic,1000)
225  for (int i = 0; i < GNodeV.Len(); i++) {
226  TIntV CurCasc;
227  TSnapQueue<TInt> EventQ;
228  THashSet<TInt> VisitedH;
229  TInt NId = GNodeV[i];
230  EventQ.Push(NId);
231  VisitedH.AddKey(NId);
232  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,NId));
233  while (! EventQ.Empty()) {
234  TNGraph::TNodeI CNI = Graph->GetNI(EventQ.Top().Val); //Get Current Node
235  EventQ.Pop();
236  // Go over the outdegree nodes of the currernt node
237  for (int e = 0; e < CNI.GetOutDeg(); e++) {
238  TInt CId = CNI.GetOutNId(e);
239  if ( !VisitedH.IsKey(CId)) {
240  EventQ.Push(CId);
241  VisitedH.AddKey(CId);
242  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,CId));
243  }
244  }
245  }
246  CurCasc.Sort();
247  TIntV ToAddV;
248  for (TIntV::TIter VI = CurCasc.BegI(); VI < CurCasc.EndI(); VI++) {
249  ToAddV.Add(MapV.GetVal(VI->Val));
250  }
251  if (ToAddV.Len() > 1) { ThTopCascVV.Add(ToAddV);}
252  }
253  #pragma omp critical
254  {
255  for (int j = 0; j < ThTopCascVV.Len(); j++) {
256  TopCascVV.Add(ThTopCascVV[j]);
257  }
258  }
259  }
260  return;
261 }
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:595
int Val
Definition: dt.h:1139
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool IsKey(const TKey &Key) const
Definition: shash.h:1148
void Pop()
Removes the first element from the queue.
Definition: gbase.h:211
bool Empty() const
Tests whether the queue is empty (contains no elements).
Definition: gbase.h:199
Iterator class for TTable rows.
Definition: table.h:330
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
int AddKey(const TKey &Key)
Definition: shash.h:1254
Definition: dt.h:1137
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:406
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
Definition: ds.h:593
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
Fast Queue used by the TBreathFS (uses memcpy to move objects TVal around).
Definition: gbase.h:158
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:416

Here is the call graph for this function:

PNGraph TSnap::CascGraph ( PTable  P,
const TStr  C1,
const TStr  C2,
const TStr  C3,
const TStr  C4,
const TInt  W,
bool  SortParam 
)

Takes as input the column names of the PTable P as C1, C2, C3 and C4 and returns a directed graph of W-adjacent events. By default calls CascGraphSource. Toggle SortParam to use CascGraphTime.

Definition at line 126 of file casc.cpp.

References CascGraphSource(), and CascGraphTime().

126  {
127  if (SortParam) {
128  return CascGraphSource(P, C1, C2, C3, C4, W);
129  }
130  else {
131  return CascGraphTime(P, C1, C2, C3, C4, W);
132  }
133 }
PNGraph CascGraphSource(PTable P, const TStr C1, const TStr C2, const TStr C3, const TStr C4, const TInt W)
Takes as input the column names of the PTable P as C1, C2,C3 and C4 and returns a directed graph of W...
Definition: casc.cpp:3
PNGraph CascGraphTime(PTable P, const TStr C1, const TStr C2, const TStr C3, const TStr C4, const TInt W)
Takes as input the column names of the PTable P as C1, C2,C3 and C4 and returns a directed graph of W...
Definition: casc.cpp:59

Here is the call graph for this function:

PNGraph TSnap::CascGraphSource ( PTable  P,
const TStr  C1,
const TStr  C2,
const TStr  C3,
const TStr  C4,
const TInt  W 
)

Takes as input the column names of the PTable P as C1, C2,C3 and C4 and returns a directed graph of W-adjacent events. For graph generation events are sorted by C1.

Definition at line 3 of file casc.cpp.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::GetVal(), TVec< TVal, TSizeTy >::Len(), TNGraph::New(), and TInt::Val.

Referenced by CascGraph().

3  {
4  // Attribute to Int mapping
5  TInt SIdx = P->GetColIdx(C1); //Source
6  TInt DIdx = P->GetColIdx(C2); //Dest
7  TInt StIdx = P->GetColIdx(C3); //Start
8  TInt DuIdx = P->GetColIdx(C4); //Duration
9  TIntV MapV;
10  TStrV SortBy;
11  SortBy.Add(C1);
12  P->Order(SortBy);
13  TIntV Source;
14  P->ReadIntCol(C1,Source);
15  PNGraph Graph = TNGraph::New();
16  //Add Nodes
17  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
18  Graph->AddNode(RI.GetRowIdx().Val);
19  MapV.Add(RI.GetRowIdx());
20  }
21  //Add Edges
22  for (TRowIterator OI = P->BegRI(); OI < P->EndRI(); OI++) {
23  int OIdx = OI.GetRowIdx().Val;
24  int ODest = P->GetIntValAtRowIdx(DIdx,OIdx).Val;
25  int OStart = P->GetIntValAtRowIdx(StIdx,OIdx).Val;
26  int ODur = P->GetIntValAtRowIdx(DuIdx,OIdx).Val;
27  // Inline binary Search
28  int val = ODest;
29  int lo = 0;
30  int hi = Source.Len() - 1;
31  int index = -1;
32  while (hi >= lo) {
33  int mid = lo + (hi - lo)/2;
34  if (Source.GetVal(mid) > val) { hi = mid - 1;}
35  else if (Source.GetVal(mid) < val) { lo = mid + 1;}
36  else { index = mid; hi = mid - 1;}
37  }
38  // End of binary Search
39  if (index < 0) {
40  continue;
41  }
42  int BIdx = index;
43  for(int i = BIdx; i < Source.Len(); i++) {
44  int InIdx = MapV.GetVal(i).Val;
45  if (InIdx == OIdx) {continue;}
46  int InSource = P->GetIntValAtRowIdx(SIdx,InIdx).Val;
47  int InStart = P->GetIntValAtRowIdx(StIdx,InIdx).Val;
48  if (InSource != ODest) { break;}
49  if (InStart >= (ODur + OStart) && InStart - (ODur + OStart) <= W.Val) {
50  if (!Graph->IsEdge(OIdx,InIdx)) {
51  Graph->AddEdge(OIdx,InIdx);
52  }
53  }
54  }
55  }
56  return Graph;
57 }
int Val
Definition: dt.h:1139
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Iterator class for TTable rows.
Definition: table.h:330
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649
Definition: dt.h:1137
Definition: bd.h:196
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

Here is the caller graph for this function:

PNGraph TSnap::CascGraphTime ( PTable  P,
const TStr  C1,
const TStr  C2,
const TStr  C3,
const TStr  C4,
const TInt  W 
)

Takes as input the column names of the PTable P as C1, C2,C3 and C4 and returns a directed graph of W-adjacent events. For graph generation events are sorted by C3.

Definition at line 59 of file casc.cpp.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::GetVal(), TVec< TVal, TSizeTy >::Len(), TNGraph::New(), and TInt::Val.

Referenced by CascGraph().

59  {
60  // Attribute to Int mapping
61  TInt SIdx = P->GetColIdx(C1); //Source
62  TInt DIdx = P->GetColIdx(C2); //Dest
63  TInt StIdx = P->GetColIdx(C3); //Start
64  TInt DuIdx = P->GetColIdx(C4); //Duration
65  TIntV MapV;
66  TStrV SortBy;
67  SortBy.Add(C3);
68  P->Order(SortBy);
69  TIntV Start;
70  P->ReadIntCol(C3,Start);
71  PNGraph Graph = TNGraph::New();
72  //Add Nodes
73  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
74  Graph->AddNode(RI.GetRowIdx().Val);
75  MapV.Add(RI.GetRowIdx());
76  }
77  //Add Edges
78  for (TRowIterator OI = P->BegRI(); OI < P->EndRI(); OI++) {
79  int OIdx = OI.GetRowIdx().Val;
80  int ODest = P->GetIntValAtRowIdx(DIdx,OIdx).Val;
81  int OStart = P->GetIntValAtRowIdx(StIdx,OIdx).Val;
82  int ODur = P->GetIntValAtRowIdx(DuIdx,OIdx).Val;
83  // Inline binary Search
84  int val = OStart + ODur;
85  int lo = 0;
86  int hi = Start.Len() - 1;
87  int index = -1;
88  if (val >= Start.GetVal(hi)) { val = Start.GetVal(hi);}
89  while (hi >= lo) {
90  int mid = lo + (hi - lo)/2;
91  if (Start.GetVal(mid) > val) {
92  if ((mid-1) >= lo && Start.GetVal(mid - 1) < val) {
93  index = mid - 1;break;
94  }
95  hi = mid - 1;
96  }
97  else if (Start.GetVal(mid) < val) {
98  if (mid + 1 <= hi && Start.GetVal(mid + 1) > val) {
99  index = mid;break;
100  }
101  lo = mid + 1;
102  }
103  else { index = mid; hi = mid - 1;}
104  }
105  // End of binary Search
106  if (index < 0) {
107  continue;
108  }
109  int BIdx = index;
110  for(int i = BIdx; i < Start.Len(); i++) {
111  int InIdx = MapV.GetVal(i).Val;
112  if (InIdx == OIdx) {continue;}
113  int InSource = P->GetIntValAtRowIdx(SIdx,InIdx).Val;
114  int InStart = P->GetIntValAtRowIdx(StIdx,InIdx).Val;
115  if (InStart - (ODur + OStart) > W.Val) { break;}
116  if (InSource == ODest && InStart >= (ODur + OStart)) {
117  if (!Graph->IsEdge(OIdx,InIdx)) {
118  Graph->AddEdge(OIdx,InIdx);
119  }
120  }
121  }
122  }
123  return Graph;
124 }
int Val
Definition: dt.h:1139
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Iterator class for TTable rows.
Definition: table.h:330
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649
Definition: dt.h:1137
Definition: bd.h:196
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::CmtyEvolutionFileBatch ( TStr  InFNm,
TIntIntHH sizesCont,
TIntIntHH cCont,
TIntIntVH edges,
double  alpha,
double  beta,
int  CmtyAlg 
)

Definition at line 490 of file cmty.cpp.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TUNGraph::AddEdge(), TUNGraph::AddNode(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::Clr(), DelSelfEdges(), edge, TSsParser::Eof(), Fail, TStr::GetCh(), THash< TKey, TDat, THashFunc >::GetDat(), TVec< TVal, TSizeTy >::GetDat(), TSsParser::GetInt(), THash< TKey, TDat, THashFunc >::GetKey(), TSsParser::GetLnStr(), TUNGraph::GetNodes(), THashKeyDatI< TKey, TDat >::IsEnd(), THash< TKey, TDat, THashFunc >::IsKey(), TUNGraph::IsNode(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TUNGraph::New(), TSsParser::Next(), and ssfWhiteSep.

Referenced by CmtyEvolutionFileBatchV().

490  {
491 
492 
493  // reading folder with networks and calculating core/periphery
494  int br = 0;
495  TIntIntH prev;
496  TIntH prev_sizes;
497 
498  TSsParser Ss(InFNm, ssfWhiteSep, true, false, true);
499  Ss.Next();
500  //int internal_year_counter = 0;
501  // variable for delimiter between networks
502  TStr Marker;
503  // defining variables for node ids and starting year
504  int SrcNId, DstNId; // , t = 1970;
505 
506  // temporal container for edges
507  TIntIntVH edges_;
508 
509  while (!Ss.Eof()) {
510 
511  //printf("%i\n", t);
512  Marker = Ss.GetLnStr();
513  // get the year from the network seperator
514  //t = Marker.GetSubStr(1, 4).GetInt();
515 
516  if (Marker.GetCh(0) == '#'){
517 
518  Ss.Next();
519  PUNGraph Graph = PUNGraph::TObj::New();
520  do{
521  if (!Ss.GetInt(0, SrcNId) || !Ss.GetInt(1, DstNId)) {
522  if (!Ss.Eof()){
523  Ss.Next();
524  if (!Ss.Eof())
525  Marker = Ss.GetLnStr();
526  }
527  continue;
528  }
529  if (!Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
530  if (!Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
531  Graph->AddEdge(SrcNId, DstNId);
532  Ss.Next();
533  if (!Ss.Eof())
534  Marker = Ss.GetLnStr();
535  } while (Marker.GetCh(0) != '#' && !Ss.Eof());
536 
537 
538  if (Graph->GetNodes()>0) {
539  // WORK
540 
541  TSnap::DelSelfEdges(Graph);
542  TCnComV CmtyV;
543  //double Q = 0.0;
544  TStr CmtyAlgStr;
545  if (CmtyAlg == 1) {
546  CmtyAlgStr = "Girvan-Newman";
547  //Q = TSnap::CommunityGirvanNewman(Graph, CmtyV);
548  }
549  else if (CmtyAlg == 2) {
550  CmtyAlgStr = "Clauset-Newman-Moore";
551  //Q = TSnap::CommunityCNM(Graph, CmtyV);
552  }
553  else if (CmtyAlg == 3) {
554  CmtyAlgStr = "Infomap";
555  //Q = TSnap::Infomap(Graph, CmtyV);
556  }
557  else { Fail; }
558 
559  TIntIntHH distCont;
560 
561  if (br == 0) {
562  prev.Clr();
563  //int size = 0;
564  for (int c = 0; c < CmtyV.Len(); c++) {
565  for (int i = 0; i < CmtyV[c].Len(); i++){
566  prev.AddDat(CmtyV[c][i].Val, c);
567  }
568  //int s = CmtyV[c].Len();
569  prev_sizes.AddDat(c, CmtyV[c].Len());
570  }
571  }
572  else {
573 
574  // containers for statistics
575 
576  //TIntFltHH stat1;
577  //TIntIntHH stat2;
578  TIntH dist;
579  TIntH map;
580 
581  int first_new_c_id = -1;
582 
583  // getting first free id for a new community
584  for (THashKeyDatI<TInt, TInt> it = prev_sizes.BegI(); !it.IsEnd(); it++)
585  if (it.GetKey() > first_new_c_id)
586  first_new_c_id = it.GetKey();
587  if (CmtyV.Len() - 1>first_new_c_id)
588  first_new_c_id = CmtyV.Len() - 1;
589  first_new_c_id++;
590 
591  for (int c = 0; c < CmtyV.Len(); c++) {
592 
593  TIntV stat;
594  TIntFltH statH1;
595  TIntFltH statH2;
596 
597  // initialize distributions to 0
598  for (THashKeyDatI<TInt, TInt> it = prev_sizes.BegI(); !it.IsEnd(); it++)
599  dist.AddDat(it.GetKey(), 0);
600  //for new nodes
601  dist.AddDat(-1, 0);
602 
603  for (int i = 0; i < CmtyV[c].Len(); i++) {
604  int id = CmtyV[c][i].Val;
605  int prev_comm = -1;
606  if (prev.IsKey(id))
607  prev_comm = prev.GetDat(CmtyV[c][i].Val);
608  stat.Add(prev_comm);
609  int pre_val = dist.GetDat(prev_comm);
610  dist.AddDat(prev_comm, pre_val + 1);
611  }
612 
613  double sumstat2 = 0;
614  for (THashKeyDatI<TInt, TInt> it = dist.BegI(); !it.IsEnd(); it++) {
615 
616  int k = it.GetKey();
617  int d = it.GetDat();
618  if (d > 0){
619  if (prev_sizes.IsKey(it.GetKey())){
620 
621  double stat1_ = (double)d / (double)prev_sizes.GetDat(k);
622  statH1.AddDat(k, stat1_);
623  }
624  double stat2_ = (double)d / (double)CmtyV[c].Len();
625  statH2.AddDat(k, stat2_);
626  sumstat2 += stat2_;
627 
628  TIntV edge;
629  edge.Add(k);
630  edge.Add(c);
631  edge.Add(d);
632  edge.Add(br - 1);
633  edge.Add(br);
634  edges_.AddDat(edges_.Len() + 1, edge);
635  }
636 
637  // adding edges between two communities in two neighbouring time points;
638 
639 
640  if (sumstat2 > 0.98) break;
641  }
642 
643  int n_of_c_greater_than_half = 0;
644  int id_of_c_greater_than_half = -1;
645  TIntV ids_of_c_greater_than_half;
646 
647  for (THashKeyDatI<TInt, TFlt> it = statH1.BegI(); !it.IsEnd(); it++){
648  if (it.GetDat()>alpha){
649  id_of_c_greater_than_half = it.GetKey();
650  ids_of_c_greater_than_half.Add(it.GetKey());
651  n_of_c_greater_than_half++;
652  }
653  }
654 
655  // if this community is build of majority of one previous community and the other parts of the community are fractions of other communities smaller than half, the new community gets its label
656  if (n_of_c_greater_than_half == 1){
657  map.AddDat(c, id_of_c_greater_than_half);
658  }
659  else{
660  int h2part_id = -2;
661  for (int i = 0; i<ids_of_c_greater_than_half.Len(); i++){
662  double H2 = statH2.GetDat(ids_of_c_greater_than_half[i]);
663  if (H2>beta){
664  h2part_id = ids_of_c_greater_than_half[i];
665  }
666  }
667  if (h2part_id != -2)
668  map.AddDat(c, h2part_id);
669  else{
670  map.AddDat(c, first_new_c_id);
671  first_new_c_id++;
672  }
673  }
674 
675  distCont.AddDat(c, dist);
676 
677  //stat1.AddDat(c,statH1);
678  //stat2.AddDat(c,statH2);
679 
680  }
681 
682 
683  prev.Clr();
684  prev_sizes.Clr();
685  for (int c = 0; c < CmtyV.Len(); c++){
686  for (int i = 0; i < CmtyV[c].Len(); i++){
687  prev.AddDat(CmtyV[c][i].Val, map.GetDat(c));
688  }
689  //int s = CmtyV[c].Len();
690  prev_sizes.AddDat(map.GetDat(c), CmtyV[c].Len());
691  }
692 
693  // filing the edges container - the key thing is the map(c)
694  for (THashKeyDatI<TInt, TIntV> it = edges_.BegI(); !it.IsEnd(); it++){
695  TIntV edgesV;
696  int a = it.GetDat()[0];
697  int b = it.GetDat()[1];
698  int v = it.GetDat()[2];
699  int d = it.GetDat()[3];
700  int e = it.GetDat()[4];
701  edgesV.Add(map.GetDat(b));
702  edgesV.Add(a);
703  edgesV.Add(v);
704  edgesV.Add(d);
705  edgesV.Add(e);
706  if (a != -1)
707  edges.AddDat(edges.Len(), edgesV);
708  }
709  edges_.Clr();
710 
711 
712  }
713 
714  sizesCont.AddDat(br, prev_sizes);
715  cCont.AddDat(br, prev);
716  br++;
717  // WORK - END
718  }
719  }
720  else Ss.Next();
721  }
722 
723 }
#define Fail
Definition: bd.h:238
TIter BegI() const
Definition: hash.h:213
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: ss.h:72
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:78
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:838
Whitespace (space or tab) separated.
Definition: ss.h:11
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:172
char GetCh(const int &ChN) const
Definition: dt.h:486
Definition: dt.h:412
Definition: hash.h:97
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
Definition: bd.h:196
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
void DelSelfEdges(const PGraph &Graph)
Removes all the self-edges from the graph.
Definition: alg.h:419
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::CmtyEvolutionFileBatchV ( TStr  InFNm,
TIntIntVH sizesContV,
TIntIntVH cContV,
TIntIntVH edges,
double  alpha,
double  beta,
int  CmtyAlg 
)

Definition at line 441 of file cmty.cpp.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), CmtyEvolutionFileBatch(), THash< TKey, TDat, THashFunc >::GetDat(), TVec< TVal, TSizeTy >::IsIn(), THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Len().

Referenced by CmtyTest().

441  {
442  TIntIntHH sizesCont;
443  TIntIntHH cCont;
444  CmtyEvolutionFileBatch(InFNm, sizesCont, cCont, edges, alpha, beta, CmtyAlg);
445 
446  TIntV uniqueId;
447  for (int i = 0; i < cCont.Len(); i++){
448  for (THashKeyDatI<TInt, TInt> it = cCont[i].BegI(); !it.IsEnd(); it++){
449  if (!uniqueId.IsIn(it.GetKey()))
450  uniqueId.Add(it.GetKey());
451  }
452  }
453 
454  for (int j = 0; j<uniqueId.Len(); j++)
455  {
456  TIntV cV;
457  for (int i = 0; i<cCont.Len(); i++)
458  {
459  if (cCont[i].IsKey(uniqueId[j]))
460  cV.Add(cCont[i].GetDat(uniqueId[j]));
461  else
462  cV.Add(-1);
463  }
464  cContV.AddDat(uniqueId[j], cV);
465  }
466 
467  TIntV uniqueC;
468  for (int i = 0; i < sizesCont.Len(); i++){
469  for (THashKeyDatI<TInt, TInt> it = sizesCont[i].BegI(); !it.IsEnd(); it++){
470  if (!uniqueC.IsIn(it.GetKey()))
471  uniqueC.Add(it.GetKey());
472  }
473  }
474 
475  for (int j = 0; j<uniqueC.Len(); j++)
476  {
477  TIntV cV;
478  for (int i = 0; i<sizesCont.Len(); i++)
479  {
480  if (sizesCont[i].IsKey(uniqueC[j]))
481  cV.Add(sizesCont[i].GetDat(uniqueC[j]));
482  else
483  cV.Add(0);
484  }
485  sizesContV.AddDat(uniqueC[j], cV);
486  }
487 
488 }
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:828
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
void CmtyEvolutionFileBatch(TStr InFNm, TIntIntHH &sizesCont, TIntIntHH &cCont, TIntIntVH &edges, double alpha, double beta, int CmtyAlg)
Definition: cmty.cpp:490
Definition: hash.h:97
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::CmtyEvolutionJson ( TStr Json,
TIntIntVH sizesContV,
TIntIntVH cContV,
TIntIntVH edges 
)

Definition at line 725 of file cmty.cpp.

References THash< TKey, TDat, THashFunc >::BegI(), TInt::GetStr(), TStr::InsStr(), THashKeyDatI< TKey, TDat >::IsEnd(), THash< TKey, TDat, THashFunc >::Len(), and TStr::Len().

Referenced by CmtyTest().

725  {
727  // This function creates a JSON string with communities and edges for community evolution visualization using D3.js
729 
730  // writing json label for edges
731  Json.InsStr(Json.Len(), "{\n\"edges\":[\n");
732 
733  TInt br = 0;
734  // iterating hash of vector of edges and writing into string
735  for (THashKeyDatI<TInt, TIntV> it = edges.BegI(); !it.IsEnd(); it++)
736  {
737  // first node
738  TInt n1 = it.GetDat()[1];
739  // second node
740  TInt n2 = it.GetDat()[0];
741  // edge weight
742  TInt w = it.GetDat()[2];
743  // start time point
744  TInt t0 = it.GetDat()[3];
745  // end time point
746  TInt t1 = it.GetDat()[4];
747 
748  if (br>0)
749  Json.InsStr(Json.Len(), ",");
750 
751  // writing to string
752  Json.InsStr(Json.Len(), "{\"n1\":"); Json.InsStr(Json.Len(), n1.GetStr());
753  Json.InsStr(Json.Len(), ", \"n2\":"); Json.InsStr(Json.Len(), n2.GetStr());
754  Json.InsStr(Json.Len(), ", \"w\":"); Json.InsStr(Json.Len(), w.GetStr());
755  Json.InsStr(Json.Len(), ", \"t0\":"); Json.InsStr(Json.Len(), t0.GetStr());
756  Json.InsStr(Json.Len(), ", \"t1\":"); Json.InsStr(Json.Len(), t1.GetStr());
757  Json.InsStr(Json.Len(), " }\n");
758  br++;
759  }
760 
761  // json label for communities
762  Json.InsStr(Json.Len(), "],\n\"communities\":[\n");
763 
764  br = 0;
765  // printing communities into json file
766  for (int i = 0; i < sizesContV[0].Len(); i++)
767  {
768  for (THashKeyDatI<TInt, TIntV> it = sizesContV.BegI(); !it.IsEnd(); it++)
769  {
770  // id of community
771  TInt id = it.GetKey();
772  // community size
773  TInt size = it.GetDat()[i];
774  // time
775  TInt j = i;
776 
777  // if the community has size greater than 0, output it to json string
778  if (size > 0) {
779  if (br>0)
780  Json.InsStr(Json.Len(), ",");
781 
782  TInt size = it.GetDat()[i];
783  Json.InsStr(Json.Len(), "{\"id\":"); Json.InsStr(Json.Len(), id.GetStr());
784  Json.InsStr(Json.Len(), ", \"size\":"); Json.InsStr(Json.Len(), size.GetStr());
785  Json.InsStr(Json.Len(), ", \"t\":"); Json.InsStr(Json.Len(), j.GetStr());
786  Json.InsStr(Json.Len(), " }\n");
787 
788  br++;
789  }
790  }
791  }
792 
793  // printing communities into json file - alternative ordering
794  /*
795  for (THashKeyDatI<TInt, TIntV> it = sizesContV.BegI(); !it.IsEnd(); it++)
796  {
797  TInt id = it.GetKey();
798  int len = it.GetDat().Len();
799  for (int i=0; i < it.GetDat().Len(); i++)
800  {
801  TInt size = it.GetDat()[i];
802  TInt j = i;
803  if (size > 0) {
804 
805  if(br>0)
806  Json.InsStr(Json.Len(),",");
807 
808  TInt size = it.GetDat()[i];
809 
810  Json.InsStr(Json.Len(),"{\"id\":"); Json.InsStr(Json.Len(),id.GetStr());
811  Json.InsStr(Json.Len(),", \"size\":"); Json.InsStr(Json.Len(),size.GetStr());
812  Json.InsStr(Json.Len(),", \"t\":"); Json.InsStr(Json.Len(),j.GetStr());
813  Json.InsStr(Json.Len()," }\n");
814 
815  br++;
816 
817  }
818 
819  }
820  }
821  */
822 
823  Json.InsStr(Json.Len(), "]\n}");
824 
825 }
TStr GetStr() const
Definition: dt.h:1200
int Len() const
Definition: dt.h:490
TIter BegI() const
Definition: hash.h:213
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:78
Definition: dt.h:1137
int Len() const
Definition: hash.h:228
void InsStr(const int &BChN, const TStr &Str)
Definition: dt.cpp:825

Here is the call graph for this function:

Here is the caller graph for this function:

TStr TSnap::CmtyTest ( TStr  InFNm,
int  CmtyAlg 
)

Definition at line 827 of file cmty.cpp.

References CmtyEvolutionFileBatchV(), and CmtyEvolutionJson().

827  {
828 
829  TIntIntVH sizesContV;
830  TIntIntVH cContV;
831  TIntIntVH edges;
832  double alpha = 0.5;
833  double beta = 0.75;
834  CmtyEvolutionFileBatchV(InFNm, sizesContV, cContV, edges, alpha, beta, CmtyAlg);
835  TStr out;
836  //int a = sizesContV.Len();
837  //int b = cContV.Len();
838  //int c = edges.Len();
839  CmtyEvolutionJson(out, sizesContV, cContV, edges);
840 
841  return out;
842 }
void CmtyEvolutionFileBatchV(TStr InFNm, TIntIntVH &sizesContV, TIntIntVH &cContV, TIntIntVH &edges, double alpha, double beta, int CmtyAlg)
Definition: cmty.cpp:441
void CmtyEvolutionJson(TStr &Json, TIntIntVH &sizesContV, TIntIntVH &cContV, TIntIntVH &edges)
Definition: cmty.cpp:725
Definition: dt.h:412

Here is the call graph for this function:

template<class PGraph >
int TSnap::CntDegNodes ( const PGraph &  Graph,
const int &  NodeDeg 
)

Returns the number of nodes with degree NodeDeg.

Definition at line 105 of file alg.h.

Referenced by TTimeNet::LoadArxiv(), TTimeNet::LoadPatents(), and TGStat::TakeBasicStat().

105  {
106  int Cnt = 0;
107  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
108  if (NI.GetDeg() == NodeDeg) Cnt++;
109  }
110  return Cnt;
111 }

Here is the caller graph for this function:

template<class PGraph >
int TSnap::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.

Definition at line 123 of file alg.h.

References THashSet< TKey, THashFunc >::AddKey(), gfDirected, and THashSet< TKey, THashFunc >::IsKey().

123  {
124  if (! Graph->IsNode(NId)) { return 0; }
125  const bool IsDir = Graph->HasFlag(gfDirected);
126  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
127  if (! IsDir) {
128  int EdgesToSet = 0;
129  for (int e = 0; e < NI.GetOutDeg(); e++) {
130  if (NodeSet.IsKey(NI.GetOutNId(e))) { EdgesToSet++; } }
131  return EdgesToSet;
132  } else {
133  TIntSet Set(NI.GetDeg());
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)); } }
138  return Set.Len();
139  }
140 }
bool IsKey(const TKey &Key) const
Definition: shash.h:1148
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13

Here is the call graph for this function:

template<class PGraph >
int TSnap::CntInDegNodes ( const PGraph &  Graph,
const int &  NodeInDeg 
)

Returns the number of nodes with in-degree NodeInDeg.

Definition at line 87 of file alg.h.

Referenced by TTimeNet::LoadBipartite(), and TGStat::TakeBasicStat().

87  {
88  int Cnt = 0;
89  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
90  if (NI.GetInDeg() == NodeInDeg) Cnt++;
91  }
92  return Cnt;
93 }

Here is the caller graph for this function:

template<class PGraph >
int TSnap::CntNonZNodes ( const PGraph &  Graph)

Returns the number of nodes with degree greater than 0.

Definition at line 114 of file alg.h.

Referenced by TTimeNet::PlotMedianDegOverTm().

114  {
115  int Cnt = 0;
116  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
117  if (NI.GetDeg() > 0) Cnt++;
118  }
119  return Cnt;
120 }

Here is the caller graph for this function:

template<class PGraph >
int TSnap::CntOutDegNodes ( const PGraph &  Graph,
const int &  NodeOutDeg 
)

Returns the number of nodes with out-degree NodeOutDeg.

Definition at line 96 of file alg.h.

Referenced by TTimeNet::LoadBipartite(), and TGStat::TakeBasicStat().

96  {
97  int Cnt = 0;
98  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
99  if (NI.GetOutDeg() == NodeOutDeg) Cnt++;
100  }
101  return Cnt;
102 }

Here is the caller graph for this function:

template<class PGraph >
int TSnap::CntSelfEdges ( const PGraph &  Graph)

Counts the number of self-edges in a graph. Edge (u,u) is a self-edge.

Definition at line 334 of file alg.h.

334  {
335  int Cnt = 0;
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++; }
339  }
340  }
341  return Cnt;
342 }
template<class PGraph >
int TSnap::CntUniqBiDirEdges ( const PGraph &  Graph)

Counts unique bidirectional edges in the graph Graph. Edge is bidirectional if there exist directed edges in both directions: (u,v) and (v,u)

Definition at line 316 of file alg.h.

References CntUniqUndirEdges(), and gfDirected.

Referenced by TGStat::TakeBasicStat().

316  {
317  if (! Graph->HasFlag(gfDirected)) { // graph is undirected
318  return CntUniqUndirEdges(Graph); // then every edge is bi-directional
319  }
320  TIntSet NbrSet;
321  int Cnt = 0;
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; } // count each un-dir edge only once
327  if (Graph->IsEdge(DstId, SrcId)) { Cnt++; }
328  }
329  }
330  return Cnt;
331 }
int CntUniqUndirEdges(const PGraph &Graph)
Counts unique undirected edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via an undi...
Definition: alg.h:279
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
int TSnap::CntUniqDirEdges ( const PGraph &  Graph)

Counts unique directed edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via a directed edge if there exists a directed edge from node u to node v.

Definition at line 301 of file alg.h.

References THashSet< TKey, THashFunc >::AddKey(), THashSet< TKey, THashFunc >::Clr(), and THashSet< TKey, THashFunc >::Len().

Referenced by TGStat::TakeBasicStat().

301  {
302  TIntSet NbrSet;
303  int Cnt = 0;
304  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
305  NbrSet.Clr(false);
306  for (int e = 0; e < NI.GetOutDeg(); e++) { // unique out-neighbors of a node
307  if (NI.GetOutNId(e) != NI.GetId()) { // skip self-edges
308  NbrSet.AddKey(NI.GetOutNId(e)); }
309  }
310  Cnt += NbrSet.Len();
311  }
312  return Cnt;
313 }
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Definition: shash.h:1243
int AddKey(const TKey &Key)
Definition: shash.h:1254
int Len() const
Definition: shash.h:1121

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
int TSnap::CntUniqUndirEdges ( const PGraph &  Graph)

Counts unique undirected edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via an undirected edge if there exists an edge in either direction (u,v) or (v,u).

Definition at line 279 of file alg.h.

References THashSet< TKey, THashFunc >::AddKey(), THashSet< TKey, THashFunc >::Clr(), and THashSet< TKey, THashFunc >::Len().

Referenced by CntUniqBiDirEdges().

279  {
280  TIntSet NbrSet;
281  TIntSet SelfNbrSet;
282  int Cnt = 0;
283  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
284  NbrSet.Clr(false);
285  for (int e = 0; e < NI.GetDeg(); e++) { // unique neighbors of a node
286  const int NbrId = NI.GetNbrNId(e);
287  if (NbrId == NI.GetId()) { // remember self-edges
288  SelfNbrSet.AddKey(NbrId);
289  } else {
290  NbrSet.AddKey(NbrId);
291  }
292  }
293  Cnt += NbrSet.Len();
294  }
295  // OP RS 2014/06/11 self-edges are currently not used
296  //return Cnt / 2 + SelfNbrSet.Len();
297  return Cnt / 2;
298 }
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Definition: shash.h:1243
int AddKey(const TKey &Key)
Definition: shash.h:1254
int Len() const
Definition: shash.h:1121

Here is the call graph for this function:

Here is the caller graph for this function:

double TSnap::CommunityCNM ( const PUNGraph Graph,
TCnComV CmtyV 
)

Clauset-Newman-Moore community detection method for large networks. At every step of the algorithm two communities that contribute maximum positive value to global modularity are merged. See: Finding community structure in very large networks, A. Clauset, M.E.J. Newman, C. Moore, 2004

Definition at line 1449 of file cmty.cpp.

References TSnap::TSnapDetail::TCNMQMatrix::CmtyCMN().

1449  {
1450  return TSnapDetail::TCNMQMatrix::CmtyCMN(Graph, CmtyV);
1451 }

Here is the call graph for this function:

double TSnap::CommunityGirvanNewman ( PUNGraph Graph,
TCnComV CmtyV 
)

Girvan-Newman community detection algorithm based on Betweenness centrality. See: Girvan M. and Newman M. E. J., Community structure in social and biological networks, Proc. Natl. Acad. Sci. USA 99, 7821-7826 (2002)

Definition at line 312 of file cmty.cpp.

References TSnap::TSnapDetail::_GirvanNewmanGetModularity(), TUNGraph::BegNI(), TVec< TVal, TSizeTy >::Clr(), TSnap::TSnapDetail::CmtyGirvanNewmanStep(), TUNGraph::EndNI(), TUNGraph::GetEdges(), TVec< TVal, TSizeTy >::Len(), and TVec< TVal, TSizeTy >::Swap().

312  {
313  PUNGraph LocalGraph = TSnap::ConvertGraph<PUNGraph>(Graph, false);
314 
315  TIntH OutDegH;
316  const int NEdges = LocalGraph->GetEdges();
317  for (TUNGraph::TNodeI NI = LocalGraph->BegNI(); NI < LocalGraph->EndNI(); NI++) {
318  OutDegH.AddDat(NI.GetId(), NI.GetOutDeg());
319  }
320  double BestQ = -1; // modularity
321  TCnComV CurCmtyV;
322  CmtyV.Clr();
323  TIntV Cmty1, Cmty2;
324  while (true) {
325  TSnapDetail::CmtyGirvanNewmanStep(LocalGraph, Cmty1, Cmty2);
326  const double Q = TSnapDetail::_GirvanNewmanGetModularity(LocalGraph, OutDegH, NEdges, CurCmtyV);
327  //printf("current modularity: %f\n", Q);
328  if (Q > BestQ) {
329  BestQ = Q;
330  CmtyV.Swap(CurCmtyV);
331  }
332  if (Cmty1.Len() == 0 || Cmty2.Len() == 0) { break; }
333  }
334  return BestQ;
335 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1101
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
Definition: bd.h:196
double _GirvanNewmanGetModularity(const PUNGraph &G, const TIntH &OutDegH, const int &OrigEdges, TCnComV &CnComV)
Definition: cmty.cpp:37
void CmtyGirvanNewmanStep(PUNGraph &Graph, TIntV &Cmty1, TIntV &Cmty2)
A single step of Girvan-Newman clustering procedure.
Definition: cmty.cpp:15

Here is the call graph for this function:

template<class POutGraph , class PInGraph >
POutGraph TSnap::ConvertESubGraph ( const PInGraph &  InGraph,
const TIntV EIdV,
const bool &  RenumberNodes = false 
)

Returns a subgraph of graph InGraph with EIdV edges with an optional node renumbering.

Creates a subgraph of the input graph InGraph on EIdV edges and returns an output graph. Input and output graphs can have different types. Node and edge data is not copied, but it is shared by input and output graphs.

Parameter RenumberNodes determines, whether the node IDs are preserved or not. If RenumberNodes is false, then nodes in the resulting graph have the same node IDs as nodes in InGraph. If RenumberNodes is true, then nodes in the resulting graph are renumbered sequentially from 0 to N-1. By default, the nodes are not renumbered.

Definition at line 441 of file subgraph.h.

References CAssert, edge, gfMultiGraph, HasGraphFlag, IAssert, and TVec< TVal, TSizeTy >::Len().

441  {
442  CAssert(HasGraphFlag(typename PInGraph::TObj, gfMultiGraph)); // needs to have explicit edges
443  POutGraph NewGraphPt = POutGraph::TObj::New();
444  typename POutGraph::TObj& NewGraph = *NewGraphPt;
445  NewGraph.Reserve(-1, EIdV.Len());
446  if (! RenumberNodes) {
447  for (int edge = 0; edge < EIdV.Len(); edge++) {
448  const int EId = EIdV[edge];
449  IAssert(InGraph->IsEdge(EId));
450  const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
451  const int SrcNId = EI.GetSrcNId();
452  const int DstNId = EI.GetDstNId();
453  if (! NewGraph.IsNode(SrcNId)) {
454  NewGraph.AddNode(SrcNId); }
455  if (! NewGraph.IsNode(DstNId)) {
456  NewGraph.AddNode(DstNId); }
457  NewGraph.AddEdge(SrcNId, DstNId);
458  }
459  } else {
460  // renumber nodes so that node ids are 0...N-1
461  TIntSet NIdSet(InGraph->GetNodes());
462  for (int edge = 0; edge < EIdV.Len(); edge++) {
463  const int EId = EIdV[edge];
464  IAssert(InGraph->IsEdge(EId));
465  const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
466  const int SrcNId = NIdSet.AddKey(EI.GetSrcNId()); // map node ids
467  const int DstNId = NIdSet.AddKey(EI.GetDstNId());
468  if (! NewGraph.IsNode(SrcNId)) {
469  NewGraph.AddNode(SrcNId); }
470  if (! NewGraph.IsNode(DstNId)) {
471  NewGraph.AddNode(DstNId); }
472  NewGraph.AddEdge(SrcNId, DstNId);
473  }
474  }
475  return NewGraphPt;
476 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
Definition: gbase.h:14
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
#define CAssert(Cond)
Definition: bd.h:302

Here is the call graph for this function:

template<class POutGraph , class PInGraph >
POutGraph TSnap::ConvertGraph ( const PInGraph &  InGraph,
const bool &  RenumberNodes = false 
)

Performs conversion of graph InGraph with an optional node renumbering.

Takes an input graph InGraph and returns an output graph. Input and output graphs can have different types. Node and edge data is not copied, but it is shared by input and output graphs.

Parameter RenumberNodes determines, whether the node IDs are preserved or not. If RenumberNodes is false, then nodes in the resulting graph have the same node IDs as nodes in InGraph. If RenumberNodes is true, then nodes in the resulting graph are renumbered sequentially from 0 to N-1. By default, the nodes are not renumbered.

Definition at line 326 of file subgraph.h.

References THashSet< TKey, THashFunc >::AddKey(), gfDirected, and HasGraphFlag.

326  {
327  POutGraph OutGraphPt = POutGraph::TObj::New();
328  typename POutGraph::TObj& OutGraph = *OutGraphPt;
329  OutGraph.Reserve(InGraph->GetNodes(), InGraph->GetEdges());
330  if (! RenumberNodes) {
331  for (typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
332  OutGraph.AddNode(NI.GetId());
333  }
334  for (typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
335  OutGraph.AddEdge(EI.GetSrcNId(), EI.GetDstNId());
336  if (! HasGraphFlag(typename PInGraph::TObj, gfDirected) && HasGraphFlag(typename POutGraph::TObj, gfDirected)) { // add edge in the other direction
337  OutGraph.AddEdge(EI.GetDstNId(), EI.GetSrcNId()); }
338  }
339  } else { // renumber nodes so that node ids are 0...N-1
340  TIntSet NIdSet(InGraph->GetNodes());
341  for (typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
342  const int nid = NIdSet.AddKey(NI.GetId());
343  OutGraph.AddNode(nid);
344  }
345  for (typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
346  const int SrcNId = NIdSet.GetKeyId(EI.GetSrcNId());
347  const int DstNId = NIdSet.GetKeyId(EI.GetDstNId());
348  OutGraph.AddEdge(SrcNId, DstNId);
349  if (! HasGraphFlag(typename PInGraph::TObj, gfDirected) && HasGraphFlag(typename POutGraph::TObj, gfDirected)) {
350  OutGraph.AddEdge(DstNId, SrcNId); }
351  }
352  }
353  //OutGraph.Defrag();
354  return OutGraphPt;
355 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13

Here is the call graph for this function:

template<class POutGraph , class PInGraph >
POutGraph TSnap::ConvertSubGraph ( const PInGraph &  InGraph,
const TIntV NIdV,
const bool &  RenumberNodes = false 
)

Returns an induced subgraph of graph InGraph with NIdV nodes with an optional node renumbering.

Creates a subgraph of the input graph InGraph on NIdV nodes and returns an output graph. Input and output graphs can have different types. Node and edge data is not copied, but it is shared by input and output graphs.

Parameter RenumberNodes determines, whether the node IDs are preserved or not. If RenumberNodes is false, then nodes in the resulting graph have the same node IDs as nodes in InGraph. If RenumberNodes is true, then nodes in the resulting graph are renumbered sequentially from 0 to N-1. By default, the nodes are not renumbered.

Definition at line 436 of file subgraph.h.

436  {
437  return TSnapDetail::TConvertSubGraph<POutGraph, PInGraph, HasGraphFlag(typename PInGraph::TObj, gfMultiGraph)>::Do(InGraph, NIdV, RenumberNodes);
438 }
template<class PGraph >
void TSnap::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.

Definition at line 445 of file alg.h.

References TVec< TVal, TSizeTy >::Add(), and TVec< TVal, TSizeTy >::Len().

445  {
446  TIntV DelNIdV;
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());
450  }
451  }
452  for (int i = 0; i < DelNIdV.Len(); i++) {
453  Graph->DelNode(DelNIdV[i]);
454  }
455 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

template<class PGraph >
void TSnap::DelNodes ( PGraph &  Graph,
const TIntV NIdV 
)

Removes nodes with ids stored in NIdV from the graph.

Definition at line 425 of file alg.h.

References TVec< TVal, TSizeTy >::Len().

425  {
426  for (int n = 0; n < NIdV.Len(); n++) {
427  Graph->DelNode(NIdV[n]);
428  }
429 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575

Here is the call graph for this function:

template<class PGraph >
void TSnap::DelSelfEdges ( const PGraph &  Graph)

Removes all the self-edges from the graph.

Definition at line 419 of file alg.h.

Referenced by CmtyEvolutionFileBatch(), ChibaNishizekiWeighter::Initialize(), TAGMFit::InitNodeData(), TAGMFast::SetGraph(), TCoda::SetGraph(), TCesna::SetGraph(), and TKronMomentsFit::Test().

419  {
420  TSnapDetail::TDelSelfEdges<PGraph, HasGraphFlag(typename PGraph::TObj, gfMultiGraph)>
421  ::Do(Graph);
422 }

Here is the caller graph for this function:

template<class PGraph >
void TSnap::DelZeroDegNodes ( PGraph &  Graph)

Removes all the zero-degree nodes, that isolated nodes, from the graph.

Definition at line 432 of file alg.h.

References TVec< TVal, TSizeTy >::Add(), and TVec< TVal, TSizeTy >::Len().

Referenced by TKronMtx::PlotCmpGraphs(), and TMAGFitBern::PlotProperties().

432  {
433  TIntV DelNIdV;
434  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
435  if (NI.GetDeg() == 0) {
436  DelNIdV.Add(NI.GetId());
437  }
438  }
439  for (int i = 0; i < DelNIdV.Len(); i++) {
440  Graph->DelNode(DelNIdV[i]);
441  }
442 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
void TSnap::DrawGViz ( const PGraph &  Graph,
const TGVizLayout Layout,
const TStr PltFNm,
const TStr Desc = TStr(),
const bool &  NodeLabels = false,
const TIntStrH NIdColorH = TIntStrH() 
)

Draws a given Graph using a selected GraphViz Layout engine with nodes colored.

Useful for drawing small (<100 node) graphs.

Parameters
PltFNmOutput filename (extension .ps, .png, .gif) determines the output format.
NIdColorHMaps node ids to node colors (see GraphViz documentation for more details).

Definition at line 34 of file gviz.h.

References TStr::GetFExt(), TStr::GetSubStr(), TSnap::TSnapDetail::GVizDoLayout(), TStr::Len(), and SaveGViz().

Referenced by TLocClust::DrawWhiskers(), and PlotRoles().

34  {
35  const TStr Ext = PltFNm.GetFExt();
36  const TStr GraphFNm = PltFNm.GetSubStr(0, PltFNm.Len()-Ext.Len()) + "dot";
37  SaveGViz(Graph, GraphFNm, Desc, NodeLabels, NIdColorH);
38  TSnap::TSnapDetail::GVizDoLayout(GraphFNm, PltFNm, Layout);
39 }
int Len() const
Definition: dt.h:490
void SaveGViz(const PGraph &Graph, const TStr &OutFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
Save a graph in GraphVizp .DOT format.
Definition: gio.h:387
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
TStr GetFExt() const
Definition: dt.cpp:1421
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...
Definition: gviz.cpp:5
Definition: dt.h:412

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
void TSnap::DrawGViz ( const PGraph &  Graph,
const TGVizLayout Layout,
const TStr PltFNm,
const TStr Desc,
const TIntStrH NodeLabelH 
)

Draws a given Graph using a selected GraphViz Layout engine with nodes labeled.

Useful for drawing small (<100 node) graphs.

Parameters
PltFNmOutput filename (extension .ps, .png, .gif) determines the output format.
NIdColorHMaps node ids to node colors (see GraphViz documentation for more details).

Definition at line 42 of file gviz.h.

References TStr::GetFExt(), TStr::GetSubStr(), TSnap::TSnapDetail::GVizDoLayout(), TStr::Len(), and SaveGViz().

42  {
43  const TStr Ext = PltFNm.GetFExt();
44  const TStr GraphFNm = PltFNm.GetSubStr(0, PltFNm.Len()-Ext.Len()) + "dot";
45  SaveGViz(Graph, GraphFNm, Desc, NodeLabelH);
46  TSnap::TSnapDetail::GVizDoLayout(GraphFNm, PltFNm, Layout);
47 }
int Len() const
Definition: dt.h:490
void SaveGViz(const PGraph &Graph, const TStr &OutFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
Save a graph in GraphVizp .DOT format.
Definition: gio.h:387
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
TStr GetFExt() const
Definition: dt.cpp:1421
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...
Definition: gviz.cpp:5
Definition: dt.h:412

Here is the call graph for this function:

TIntFltH TSnap::EventImportance ( const PNGraph Graph,
const int  k 
)

Event importance.

Definition at line 527 of file centr.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::EndI(), and THash< TKey, TDat, THashFunc >::GetDat().

527  {
528  TIntFltH NodeList; // values for nodese
529 
530  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
531  NodeList.AddDat(NI.GetId(),NI.GetOutDeg());
532  }
533 
534 
535  for (THashKeyDatI<TInt,TFlt> NI = NodeList.BegI(); NI < NodeList.EndI(); NI++){
536  int outdeg = Graph->GetNI(NI.GetKey()).GetOutDeg();
537  int indeg = Graph->GetNI(NI.GetKey()).GetInDeg();
538 
539  if (outdeg>1 && indeg>0){
540  double val = (1-(1/(double)outdeg))/(double)indeg;
541  for(int i=0; i<(outdeg+indeg);i++){
542  int NId = Graph->GetNI(NI.GetKey()).GetNbrNId(i);
543  if (Graph->GetNI(NI.GetKey()).IsInNId(NId) == true){
544  NodeList.AddDat(NId,NodeList.GetDat(NId)+val);
545  }
546 
547  }
548  }
549 
550  }
551 
552  return NodeList;
553 }
TIter BegI() const
Definition: hash.h:213
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TIter EndI() const
Definition: hash.h:218
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

TIntFltH TSnap::EventImportance1 ( const PNGraph Graph,
const int  k 
)

Definition at line 556 of file centr.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::EndI(), and THash< TKey, TDat, THashFunc >::GetDat().

556  {
557  TIntFltH NodeList; // values for nodese
558 
559  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
560  NodeList.AddDat(NI.GetId(),NI.GetOutDeg());
561  }
562 
563 
564  for (THashKeyDatI<TInt,TFlt> NI = NodeList.BegI(); NI < NodeList.EndI(); NI++){
565  int outdeg = Graph->GetNI(NI.GetKey()).GetOutDeg();
566  int indeg = Graph->GetNI(NI.GetKey()).GetInDeg();
567 
568  if (outdeg>1 && indeg>0){
569  double val = (1-(1/(double)outdeg))/(double)indeg;
570  for(int i=0; i<(outdeg+indeg);i++){
571  int NId = Graph->GetNI(NI.GetKey()).GetNbrNId(i);
572  if (Graph->GetNI(NI.GetKey()).IsInNId(NId) == true){
573  NodeList.AddDat(NId,NodeList.GetDat(NId)+val);
574  }
575 
576  }
577  }
578 
579  }
580 
581  return NodeList;
582 }
TIter BegI() const
Definition: hash.h:213
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TIter EndI() const
Definition: hash.h:218
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

int TSnap::FastCorePeriphery ( PUNGraph Graph,
TIntIntH out 
)

Girvan-Newman community detection algorithm based on Betweenness centrality. See: Girvan M. and Newman M. E. J., Community structure in social and biological networks, Proc. Natl. Acad. Sci. USA 99, 7821-7826 (2002)

Definition at line 12 of file coreper.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::BegI(), TUNGraph::BegNI(), TUNGraph::EndNI(), THashKeyDatI< TKey, TDat >::IsEnd(), THash< TKey, TDat, THashFunc >::Len(), and THash< TKey, TDat, THashFunc >::SortByDat().

12  {
13 
14  TIntIntH nodes;
15  double Z=0;
16 
17  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){ // Calculate and store the degrees of each node.
18  int deg = NI.GetDeg();
19  int id = NI.GetId();
20  Z += deg;
21  nodes.AddDat(id,deg);
22  }
23 
24  Z = Z/2;
25 
26  nodes.SortByDat(false); // Then sort the nodes in descending order of degree, to get a list of nodes {v1, v2, . . . , vn}.
27 
28  double Zbest = 99999900000000000;
29  int kbest = 0;
30 
31  int br=0;
32  for (int k=0; k<nodes.Len(); k++){
33  br++;
34  Z = Z + br - 1 - nodes[k];
35  if (Z < Zbest){ // or <=
36  Zbest = Z;
37  kbest = br;
38  }
39  }
40 
41  int cp = 0;
42  br = 0;
43  for (THashKeyDatI<TInt, TInt> it = nodes.BegI(); !it.IsEnd(); it++) {
44  if (br < kbest)
45  cp = 1;
46  else
47  cp = 0;
48  out.AddDat(it.GetKey(), cp);
49  br++;
50  }
51 
52  return kbest;
53  }
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:78
Definition: hash.h:97
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

int TSnap::FastCorePeripheryGC ( PUNGraph Graph,
TIntIntH out 
)

Definition at line 56 of file coreper.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::BegI(), TUNGraph::BegNI(), THash< TKey, TDat, THashFunc >::EndI(), TUNGraph::EndNI(), TUNGraph::GetNI(), TUNGraph::GetNodes(), Intersect(), THashKeyDatI< TKey, TDat >::IsEnd(), THash< TKey, TDat, THashFunc >::IsKey(), THash< TKey, TDat, THashFunc >::Len(), and THash< TKey, TDat, THashFunc >::SortByDat().

56  {
57  TIntH GroupNodes; // buildup cpntainer of group nodes
58  int *NNodes = new int[Graph->GetNodes()]; // container of neighbouring nodes
59  int NNodes_br = 0;
60 
61  TIntIntH nodes;
62  TIntIntH nodesIds;
63  double Z=0;
64 
65  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){ // Calculate and store the degrees of each node.
66  int deg = NI.GetDeg();
67  int id = NI.GetId();
68  Z += deg;
69  nodes.AddDat(id,deg);
70 
71  }
72 
73  Z = Z/2;
74 
75  nodes.SortByDat(false); // Then sort the nodes in descending order of degree, to get a list of nodes {v1, v2, . . . , vn}.
76 
77  int br1=0;
78  for (THashKeyDatI<TInt,TInt> NI = nodes.BegI(); NI < nodes.EndI(); NI++){
79  nodesIds.AddDat(NI.GetKey(),NI.GetKey());
80  br1++;
81  }
82 
83  double Zbest = 99999900000000000;
84  //int kbest;
85  //int olddeg;
86  int br=0;
87  for (int k=0; k<nodes.Len(); k++){
88  if (k<nodes.Len()-1){
89  if (nodes[k]==nodes[k+1]){ // go into same deg mode
90  int kmin=-2; int knew=-1;
91  while (kmin < 999999 && kmin !=-1 ){
92  int kind=-1;
93  knew=k;
94  kmin=999999;
95  while(nodes[k]==nodes[knew] && knew < nodes.Len()-1){
96  int inter = Intersect(Graph->GetNI(nodesIds[knew]),NNodes,NNodes_br);
97  int deg = nodes[knew];
98  //if (((((nodes.Len()-NNodes_br)*(nodes.Len()-NNodes_br)))-(nodes.Len()-NNodes_br))/2<(((br*br)-br)/2))
99  if ((deg-inter)<kmin && !GroupNodes.IsKey(nodesIds[knew]))
100  {
101  kmin = deg-inter; kind = knew;
102  }
103 
104  knew++;
105  }
106 
107  if (kind!=-1){
108  br++;
109  Z = Z + br - 1 - nodes[kind];
110  if (Z < (Zbest)){ // or <=
111  //if (olddeg>nodes[kind])
112 
113  //olddeg = nodes[kind];
114  Zbest = Z;
115  //kbest = br;
116  int w = nodes[kind];
117  int id = nodesIds[kind];
118  GroupNodes.AddDat(id,w);
119  NNodes[NNodes_br] = id;
120  NNodes_br++;
121  }
122  else{
123 
124  break;
125  }
126  }
127  }
128  k=knew-1;
129  }
130  else{
131  br++;
132  Z = Z + br - 1 - nodes[k];
133  if (Z < (Zbest)){ // or <=
134  //if (olddeg>nodes[k])
135 
136  //olddeg = nodes[k];
137  Zbest = Z;
138  //kbest = br;
139  int w = nodes[k];
140  int id = nodesIds[k];
141  GroupNodes.AddDat(id,w);
142  NNodes[NNodes_br] = id;
143  NNodes_br++;
144  }
145  }
146  }
147 
148  else{
149  br++;
150  Z = Z + br - 1 - nodes[k];
151  if (Z < Zbest){ // or <=
152  //if (olddeg>nodes[k])
153 
154  //olddeg = nodes[k];
155  Zbest = Z;
156  //kbest = br;
157  int w = nodes[k];
158  int id = nodesIds[k];
159  GroupNodes.AddDat(id,w);
160  NNodes[NNodes_br] = id;
161  NNodes_br++;
162  }
163  }
164  }
165 
166  int cp = 0;
167  br = 0;
168  for (THashKeyDatI<TInt, TInt> it = nodes.BegI(); !it.IsEnd(); it++) {
169  if (GroupNodes.IsKey(it.GetKey()))
170  cp = 1;
171  else
172  cp = 0;
173  out.AddDat(it.GetKey(), cp);
174  br++;
175  }
176 
177  /*for (THashKeyDatI<TInt, TInt> it = GroupNodes.BegI(); it < GroupNodes.EndI(); it++) {
178  out.AddDat(it.GetKey(), 1);
179  br++;
180  }*/
181 
182  //return kbest;
183  return GroupNodes.Len();
184  }
int Intersect(TUNGraph::TNodeI Node, TIntH NNodes)
Intersect.
Definition: centr.cpp:584
TIter BegI() const
Definition: hash.h:213
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
TIter EndI() const
Definition: hash.h:218
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:78
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

int TSnap::FindAugV ( const PNEANet Net,
const int &  CapIndex,
TIntV Flow,
TIntQ FwdNodeQ,
TIntH PredEdgeH,
TIntQ BwdNodeQ,
TIntH SuccEdgeH,
TIntV MidToSrcAugV,
TIntV MidToSnkAugV,
const int &  SrcNId,
const int &  SnkNId 
)

Returns the amount the flow can be augmented over the paths, 0 if no path can be found.

Find the augmenting path. Calls bidirectional BFS to find the path, and then builds the two path vectors.

Parameters
MidToSrcAugVContains the path vector from the midpoint node where the bi-d search met back to the source node.
MidToSnkAugVContains the path vector from the midpoint node where the bi-d search met back to the sink node.

Definition at line 71 of file flow.cpp.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::GetDat(), TNEANet::TEdgeI::GetDstNId(), TNEANet::TEdgeI::GetSrcNId(), IntFlowBiDBFS(), and TInt::Mx.

Referenced by GetMaxFlowIntEK().

71  {
72  int MidPtNId = IntFlowBiDBFS(Net, CapIndex, Flow, FwdNodeQ, PredEdgeH, BwdNodeQ, SuccEdgeH, SrcNId, SnkNId);
73  if (MidPtNId == -1) { return 0; }
74  int MinAug = TInt::Mx, NId = MidPtNId, AugFlow = 0;
75  // Build the path from the midpoint back to the source by tracing through the PredEdgeH
76  for (int EId = PredEdgeH.GetDat(NId); NId != SrcNId; EId = PredEdgeH.GetDat(NId)) {
77  MidToSrcAugV.Add(EId);
78  const TNEANet::TEdgeI &EI = Net->GetEI(EId);
79  if (EI.GetSrcNId() == NId) {
80  NId = EI.GetDstNId();
81  AugFlow = Flow[EId];
82  } else {
83  NId = EI.GetSrcNId();
84  AugFlow = Net->GetIntAttrIndDatE(EId, CapIndex) - Flow[EId];
85  }
86  if (AugFlow < MinAug) { MinAug = AugFlow; }
87  }
88  NId = MidPtNId;
89  // Build the path from the midpoint back to the sink by tracing through the SuccEdgeH
90  for (int EId = SuccEdgeH.GetDat(NId); NId != SnkNId; EId = SuccEdgeH.GetDat(NId)) {
91  MidToSnkAugV.Add(EId);
92  const TNEANet::TEdgeI &EI = Net->GetEI(EId);
93  if (EI.GetDstNId() == NId) {
94  NId = EI.GetSrcNId();
95  AugFlow = Flow[EId];
96  } else {
97  NId = EI.GetDstNId();
98  AugFlow = Net->GetIntAttrIndDatE(EId, CapIndex) - Flow[EId];
99  }
100  if (AugFlow < MinAug) { MinAug = AugFlow; }
101  }
102  return MinAug;
103 }
static const int Mx
Definition: dt.h:1142
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
int GetDstNId() const
Returns the destination of the edge.
Definition: network.h:1886
int GetSrcNId() const
Returns the source of the edge.
Definition: network.h:1884
int IntFlowBiDBFS(const PNEANet &Net, const int &CapIndex, TIntV &Flow, TIntQ &FwdNodeQ, TIntH &PredEdgeH, TIntQ &BwdNodeQ, TIntH &SuccEdgeH, const int &SrcNId, const int &SnkNId)
Definition: flow.cpp:4
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1867
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

Here is the caller graph for this function:

int TSnap::findMinimum ( TIntV Frontier,
TIntFltH NIdDistH 
)

Definition at line 685 of file centr.cpp.

References TVec< TVal, TSizeTy >::Del(), THash< TKey, TDat, THashFunc >::GetDat(), TVec< TVal, TSizeTy >::GetVal(), TVec< TVal, TSizeTy >::Len(), and TInt::Mx.

Referenced by GetWeightedShortestPath().

685  {
686  TFlt minimum = TInt::Mx;
687  int min_index = 0;
688  for (int i = 0; i < Frontier.Len(); i++) {
689  int NId = Frontier.GetVal(i);
690  if (NIdDistH.GetDat(NId) < minimum) {
691  minimum = NIdDistH.GetDat(NId);
692  min_index = i;
693  }
694  }
695  const int NId = Frontier.GetVal(min_index);
696  Frontier.Del(min_index);
697  return NId;
698 }
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1189
static const int Mx
Definition: dt.h:1142
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
Definition: dt.h:1386
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
PGraph TSnap::GenBaraHierar ( const int &  Levels,
const bool &  IsDir 
)

Generates a Ravasz-Barabasi deterministic scale-free graph.

Corners of the graph are recursively expanded with miniature copies of the base graph (below). The graph has power-law degree distribution with the exponent 1+ln(5)/ln(4) and clustering coefficient with power-law decay exponent -1. Base graph:

///   o---o
///   |\ /|
///   | o |
///   |/ \|
///   o---o
/// 

See: Hierarchical organization in complex networks. Ravasz and Barabasi. URL: http://arxiv.org/abs/cond-mat/0206130

Definition at line 174 of file ggen.h.

References TMath::Power(), and TMath::Round().

174  {
175  const int Nodes = (int) TMath::Round(TMath::Power(5, Levels));
176  PGraph GraphPt = PGraph::New();
177  typename PGraph::TObj& Graph = *GraphPt;
178  Graph.Reserve(Nodes, -1);
179  // base graph
180  for (int i = 0; i < 5; i++) { Graph.AddNode(i); }
181  Graph.AddEdge(1,2); Graph.AddEdge(2,3);
182  Graph.AddEdge(3,4); Graph.AddEdge(4,1);
183  Graph.AddEdge(1,0); Graph.AddEdge(3,0);
184  Graph.AddEdge(2,0); Graph.AddEdge(4,0);
185  // expansion
186  const int CenterId = 0;
187  for (int lev = 1; lev < Levels+1; lev++) {
188  const int MxNId = Graph.GetNodes();
189  // make 4 duplicate copies
190  for (int d = 0; d < 4; d++) {
191  for (int n = 0; n < MxNId; n++) { Graph.AddNode(); }
192  for (int n = 0; n < MxNId; n++) {
193  typename PGraph::TObj::TNodeI NI = Graph.GetNI(n);
194  const int SrcId = n+MxNId*(d+1);
195  for (int e = 0; e < NI.GetOutDeg(); e++) {
196  Graph.AddEdge(SrcId, NI.GetOutNId(e)+MxNId*(d+1));
197  }
198  }
199  }
200  // add edges to the center
201  //const int LevPow = (int)TMath::Round(TMath::Power(5,lev-1));
202  for (int n = MxNId; n < Graph.GetNodes(); n++) {
203  //typename PGraph::TObj::TNodeI NI = Graph.GetNI(n);
204  const int SrcId = n;
205  int Pow = 1; bool Skip = false;
206  for (int p = 1; p <= lev; p++) {
207  if (SrcId % (5*Pow) < Pow) { Skip=true; break; }
208  Pow *= 5;
209  }
210  if (Skip) { continue; }
211  Graph.AddEdge(SrcId, CenterId);
212  }
213  }
214  return GraphPt;
215 }
static double Round(const double &Val)
Definition: xmath.h:16
static double Power(const double &Base, const double &Exponent)
Definition: xmath.h:25

Here is the call graph for this function:

template<class PGraph >
PGraph TSnap::GenCircle ( const int &  Nodes,
const int &  NodeOutDeg = 1,
const bool &  IsDir = true 
)

Generates a circle graph where every node creates out-links to NodeOutDeg forward nodes.

Definition at line 104 of file ggen.h.

References gfDirected.

104  {
105  PGraph Graph = PGraph::TObj::New();
106  Graph->Reserve(Nodes, Nodes*NodeOutDeg);
107  for (int n = 0; n < Nodes; n++) {
108  Graph->AddNode(n); }
109  for (int n = 0; n < Nodes; n++) {
110  for (int x = 0; x < NodeOutDeg; x++) {
111  Graph->AddEdge(n, (n+x+1) % Nodes);
112  if (Graph->HasFlag(gfDirected) && ! IsDir) { Graph->AddEdge((n+x+1) % Nodes, n); }
113  }
114  }
115  return Graph;
116 }
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
PUNGraph TSnap::GenConfModel ( const TIntV DegSeqV,
TRnd Rnd 
)

Generates a random undirect graph with a given degree sequence.

Generates a random undirect graph with a given degree sequence DegSeqV. Configuration model operates as follows. For each node N, of degree DeqSeqV[N] we create DeqSeqV[N] spokes (half-edges). We then pick two spokes at random, and connect the spokes endpoints. We continue this process until no spokes are left. Generally this generates a multigraph (i.e., spokes out of same nodes can be chosen multiple times).We ignore (discard) self-loops and multiple edges. Thus, the generated graph will only approximate follow the given degree sequence. The method is very fast!

Definition at line 122 of file ggen.cpp.

References TUNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TUNGraph::AddNode(), TRnd::GetUniDevInt(), THashSet< TKey, THashFunc >::IsKey(), TVec< TVal, TSizeTy >::Len(), TMath::Mn(), TUNGraph::New(), TUNGraph::Reserve(), and Swap().

Referenced by GenConfModel(), GenRndPowerLaw(), and TTimeNet::PlotEffDiam().

122  {
123  const int Nodes = DegSeqV.Len();
124  PUNGraph GraphPt = TUNGraph::New();
125  TUNGraph& Graph = *GraphPt;
126  Graph.Reserve(Nodes, -1);
127  TIntV NIdDegV(DegSeqV.Len(), 0);
128  int DegSum=0, edges=0;
129  for (int node = 0; node < Nodes; node++) {
130  Graph.AddNode(node);
131  for (int d = 0; d < DegSeqV[node]; d++) { NIdDegV.Add(node); }
132  DegSum += DegSeqV[node];
133  }
134  NIdDegV.Shuffle(Rnd);
135  TIntPrSet EdgeH(DegSum/2); // set of all edges, is faster than graph edge lookup
136  if (DegSum % 2 != 0) {
137  printf("Seg seq is odd [%d]: ", DegSeqV.Len());
138  for (int d = 0; d < TMath::Mn(100, DegSeqV.Len()); d++) { printf(" %d", (int)DegSeqV[d]); }
139  printf("\n");
140  }
141  int u=0, v=0;
142  for (int c = 0; NIdDegV.Len() > 1; c++) {
143  u = Rnd.GetUniDevInt(NIdDegV.Len());
144  while ((v = Rnd.GetUniDevInt(NIdDegV.Len())) == u) { }
145  if (u > v) { Swap(u, v); }
146  const int E1 = NIdDegV[u];
147  const int E2 = NIdDegV[v];
148  if (v == NIdDegV.Len()-1) { NIdDegV.DelLast(); }
149  else { NIdDegV[v] = NIdDegV.Last(); NIdDegV.DelLast(); }
150  if (u == NIdDegV.Len()-1) { NIdDegV.DelLast(); }
151  else { NIdDegV[u] = NIdDegV.Last(); NIdDegV.DelLast(); }
152  if (E1 == E2 || EdgeH.IsKey(TIntPr(E1, E2))) { continue; }
153  EdgeH.AddKey(TIntPr(E1, E2));
154  Graph.AddEdge(E1, E2);
155  edges++;
156  if (c % (DegSum/100+1) == 0) { printf("\r configuration model: iter %d: edges: %d, left: %d", c, edges, NIdDegV.Len()/2); }
157  }
158  printf("\n");
159  return GraphPt;
160 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Undirected graph.
Definition: graph.h:32
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:302
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:172
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92
Definition: ds.h:32
Definition: bd.h:196
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568

Here is the call graph for this function:

Here is the caller graph for this function:

PUNGraph TSnap::GenConfModel ( const PUNGraph G)

Generate a random graph using (approximately) the same node degrees as in G using the configuration model.

Definition at line 338 of file ggen.cpp.

References GenConfModel(), GetDegSeqV(), and TUNGraph::GetNodes().

338  {
339  TIntV DegSeqV(G->GetNodes(), 0);
340  TSnap::GetDegSeqV(G, DegSeqV);
341  return TSnap::GenConfModel(DegSeqV);
342 }
void GetDegSeqV(const PGraph &Graph, TIntV &DegV)
Returns a degree sequence vector.
Definition: alg.h:245
PUNGraph GenConfModel(const TIntV &DegSeqV, TRnd &Rnd)
Generates a random undirect graph with a given degree sequence.
Definition: ggen.cpp:122

Here is the call graph for this function:

PNGraph TSnap::GenCopyModel ( const int &  Nodes,
const double &  Beta,
TRnd Rnd 
)

Generates a random scale-free network using the Copying Model.

Generates a random scale-free network using the Copying Model. The generating process operates as follows: Node u is added to a graph, it selects a random node v, and with prob Beta it links to v, with 1-Beta links u links to neighbor of v. The power-law degree exponent is -1/(1-Beta). See: Stochastic models for the web graph. Kumar, Raghavan, Rajagopalan, Sivakumar, Tomkins, Upfal. URL: http://snap.stanford.edu/class/cs224w-readings/kumar00stochastic.pdf

Definition at line 456 of file ggen.cpp.

References TNGraph::AddEdge(), TNGraph::AddNode(), TNGraph::GetNI(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), TNGraph::GetRndNId(), TRnd::GetUniDev(), TRnd::GetUniDevInt(), TNGraph::New(), and TNGraph::Reserve().

456  {
457  PNGraph GraphPt = TNGraph::New();
458  TNGraph& Graph = *GraphPt;
459  Graph.Reserve(Nodes, Nodes);
460  const int startNId = Graph.AddNode();
461  Graph.AddEdge(startNId, startNId);
462  for (int n = 1; n < Nodes; n++) {
463  const int rnd = Graph.GetRndNId();
464  const int NId = Graph.AddNode();
465  if (Rnd.GetUniDev() < Beta) {
466  Graph.AddEdge(NId, rnd); }
467  else {
468  const TNGraph::TNodeI NI = Graph.GetNI(rnd);
469  const int rnd2 = Rnd.GetUniDevInt(NI.GetOutDeg());
470  Graph.AddEdge(NId, NI.GetOutNId(rnd2));
471  }
472  }
473  return GraphPt;
474 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:552
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:603
Directed graph.
Definition: graph.h:346
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:406
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
double GetUniDev()
Definition: dt.h:30
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:614
Definition: bd.h:196
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:416

Here is the call graph for this function:

PUNGraph TSnap::GenDegSeq ( const TIntV DegSeqV,
TRnd Rnd 
)

Generates a random graph with exact degree sequence.

Generates a random graph with exact degree sequence DegSeqV. DegSeqV must be sorted in descending order. The generated graph has no self loops. The graph generation process simulates the Configuration Model, but if a duplicate edge occurs, we find a random edge, break it and reconnect it with the duplicate.

Definition at line 61 of file ggen.cpp.

References TUNGraph::AddEdge(), TUNGraph::AddNode(), TUNGraph::DelEdge(), edge, TSnap::TSnapDetail::GetRndEdgeNonAdjNode(), IAssert, IAssertR, TUNGraph::IsEdge(), TVec< TVal, TSizeTy >::IsSorted(), TVec< TVal, TSizeTy >::Len(), TUNGraph::New(), TUNGraph::Reserve(), TPair< TVal1, TVal2 >::Val1, and TPair< TVal1, TVal2 >::Val2.

Referenced by GenRndDegK(), and GenRndPowerLaw().

61  {
62  const int Nodes = DegSeqV.Len();
63  PUNGraph GraphPt = TUNGraph::New();
64  TUNGraph& Graph = *GraphPt;
65  Graph.Reserve(Nodes, -1);
66  TIntH DegH(DegSeqV.Len(), true);
67 
68  IAssertR(DegSeqV.IsSorted(false), "DegSeqV must be sorted in descending order.");
69  int DegSum=0, edge=0;
70  for (int node = 0; node < Nodes; node++) {
71  IAssert(Graph.AddNode(node) == node);
72  DegH.AddDat(node, DegSeqV[node]);
73  DegSum += DegSeqV[node];
74  }
75  IAssert(DegSum % 2 == 0);
76  while (! DegH.Empty()) {
77  // pick random nodes and connect
78  const int NId1 = DegH.GetKey(DegH.GetRndKeyId(Rnd, 0.5));
79  const int NId2 = DegH.GetKey(DegH.GetRndKeyId(Rnd, 0.5));
80  IAssert(DegH.IsKey(NId1) && DegH.IsKey(NId2));
81  if (NId1 == NId2) {
82  if (DegH.GetDat(NId1) == 1) { continue; }
83  // find rnd edge, break it, and connect the endpoints to the nodes
84  const TIntPr Edge = TSnapDetail::GetRndEdgeNonAdjNode(GraphPt, NId1, -1);
85  if (Edge.Val1==-1) { continue; }
86  Graph.DelEdge(Edge.Val1, Edge.Val2);
87  Graph.AddEdge(Edge.Val1, NId1);
88  Graph.AddEdge(NId1, Edge.Val2);
89  if (DegH.GetDat(NId1) == 2) { DegH.DelKey(NId1); }
90  else { DegH.GetDat(NId1) -= 2; }
91  } else {
92  if (! Graph.IsEdge(NId1, NId2)) {
93  Graph.AddEdge(NId1, NId2); } // good edge
94  else {
95  // find rnd edge, break and cross-connect
96  const TIntPr Edge = TSnapDetail::GetRndEdgeNonAdjNode(GraphPt, NId1, NId2);
97  if (Edge.Val1==-1) { continue; }
98  Graph.DelEdge(Edge.Val1, Edge.Val2);
99  Graph.AddEdge(NId1, Edge.Val1);
100  Graph.AddEdge(NId2, Edge.Val2);
101  }
102  if (DegH.GetDat(NId1)==1) { DegH.DelKey(NId1); }
103  else { DegH.GetDat(NId1) -= 1; }
104  if (DegH.GetDat(NId2)==1) { DegH.DelKey(NId2); }
105  else { DegH.GetDat(NId2) -= 1; }
106  }
107  if (++edge % 1000 == 0) {
108  printf("\r %dk / %dk", edge/1000, DegSum/2000); }
109  }
110  return GraphPt;
111 }
#define IAssert(Cond)
Definition: bd.h:262
#define IAssertR(Cond, Reason)
Definition: bd.h:265
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TIntPr GetRndEdgeNonAdjNode(const PGraph &Graph, int NId1, int NId2)
Returns a random edge in a graph Graph where the edge does not touch nodes NId1 and NId2...
Definition: ggen.h:240
Undirected graph.
Definition: graph.h:32
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:302
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:172
void DelEdge(const int &SrcNId, const int &DstNId)
Deletes an edge between node IDs SrcNId and DstNId from the graph.
Definition: graph.cpp:124
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92
Definition: ds.h:32
bool IsSorted(const bool &Asc=true) const
Checks whether the vector is sorted in ascending (if Asc=true) or descending (if Asc=false) order...
Definition: ds.h:1323
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
Definition: graph.cpp:137

Here is the call graph for this function:

Here is the caller graph for this function:

PNGraph TSnap::GenForestFire ( const int &  Nodes,
const double &  FwdProb,
const double &  BckProb 
)

Generates a random Forest Fire, directed graph with given probabilities.

Definition at line 445 of file ggen.cpp.

References TForestFire::GenGraph().

445  {
446  return TForestFire::GenGraph(Nodes, FwdProb, BckProb);
447 }
static PNGraph GenGraph(const int &Nodes, const double &FwdProb, const double &BckProb)
Definition: ff.cpp:250

Here is the call graph for this function:

template<class PGraph >
PGraph TSnap::GenFull ( const int &  Nodes)

Generates a complete graph on Nodes nodes. Graph has no self-loops.

Definition at line 119 of file ggen.h.

119  {
120  PGraph Graph = PGraph::TObj::New();
121  Graph->Reserve(Nodes, Nodes*Nodes);
122  for (int n = 0; n < Nodes; n++) {
123  Graph->AddNode(n); }
124  for (int n1 = 0; n1 < Nodes; n1++) {
125  for (int n2 = 0; n2 < Nodes; n2++) {
126  if (n1 != n2) { Graph->AddEdge(n1, n2); }
127  }
128  }
129  return Graph;
130 }
PUNGraph TSnap::GenGeoPrefAttach ( const int &  Nodes,
const int &  OutDeg,
const double &  Beta,
TRnd Rnd 
)

Generates a random scale-free graph using the Geometric Preferential model.

Generates a random scale-free graph using the Geometric Preferential Attachment model by Flexman, Frieze and Vera. See: A geometric preferential attachment model of networks by Flexman, Frieze and Vera. WAW 2004. URL: http://math.cmu.edu/~af1p/Texfiles/GeoWeb.pdf

Definition at line 364 of file ggen.cpp.

References TVec< TVal, TSizeTy >::Add(), TUNGraph::AddEdge(), TUNGraph::AddNode(), TVec< TVal, TSizeTy >::Clr(), TVec< TVal, TSizeTy >::Del(), TUNGraph::TNodeI::GetDeg(), TUNGraph::GetNI(), TSnap::TSnapDetail::GetSphereDev(), TRnd::GetUniDevInt(), TUNGraph::IsNode(), TVec< TVal, TSizeTy >::Last(), TVec< TVal, TSizeTy >::Len(), TUNGraph::New(), TMath::Pi, TMath::Sqr(), TTriple< TVal1, TVal2, TVal3 >::Val1, TTriple< TVal1, TVal2, TVal3 >::Val2, and TTriple< TVal1, TVal2, TVal3 >::Val3.

364  {
365  PUNGraph G = TUNGraph::New(Nodes, Nodes*OutDeg);
366  TFltTrV PointV(Nodes, 0);
367  TFltV ValV;
368  // points on a sphere of radius 1/(2*pi)
369  const double Rad = 0.5 * TMath::Pi;
370  for (int i = 0; i < Nodes; i++) {
371  TSnapDetail::GetSphereDev(3, Rnd, ValV);
372  PointV.Add(TFltTr(Rad*ValV[0], Rad*ValV[1], Rad*ValV[2]));
373  }
374  const double R2 = TMath::Sqr(log((double) Nodes) / (pow((double) Nodes, 0.5-Beta)));
375  TIntV DegV, NIdV;
376  int SumDeg;
377  for (int t = 0; t < Nodes; t++) {
378  const int pid = t;
379  const TFltTr& P1 = PointV[pid];
380  // add node
381  if (! G->IsNode(pid)) { G->AddNode(pid); }
382  // find neighborhood
383  DegV.Clr(false); NIdV.Clr(false); SumDeg=0;
384  for (int p = 0; p < t; p++) {
385  const TFltTr& P2 = PointV[p];
386  if (TMath::Sqr(P1.Val1-P2.Val1)+TMath::Sqr(P1.Val2-P2.Val2)+TMath::Sqr(P1.Val3-P2.Val3) < R2) {
387  NIdV.Add(p);
388  DegV.Add(G->GetNI(p).GetDeg()+1);
389  SumDeg += DegV.Last();
390  }
391  }
392  // add edges
393  for (int m = 0; m < OutDeg; m++) {
394  const int rnd = Rnd.GetUniDevInt(SumDeg);
395  int sum = 0, dst = -1;
396  for (int s = 0; s < DegV.Len(); s++) {
397  sum += DegV[s];
398  if (rnd < sum) { dst=s; break; }
399  }
400  if (dst != -1) {
401  G->AddEdge(pid, NIdV[dst]);
402  SumDeg -= DegV[dst];
403  NIdV.Del(dst); DegV.Del(dst);
404  }
405  }
406  }
407  return G;
408 }
Definition: ds.h:130
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1189
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TVal1 Val1
Definition: ds.h:132
static double Sqr(const double &x)
Definition: xmath.h:12
void GetSphereDev(const int &Dim, TRnd &Rnd, TFltV &ValV)
Sample random point from the surface of a Dim-dimensional unit sphere.
Definition: ggen.cpp:346
TVal2 Val2
Definition: ds.h:133
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:172
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
TTriple< TFlt, TFlt, TFlt > TFltTr
Definition: ds.h:181
static double Pi
Definition: xmath.h:8
Definition: bd.h:196
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TVal3 Val3
Definition: ds.h:134
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Here is the call graph for this function:

template<class PGraph >
PGraph TSnap::GenGrid ( const int &  Rows,
const int &  Cols,
const bool &  IsDir = true 
)

Generates a 2D-grid graph of Rows rows and Cols columns.

Definition at line 65 of file ggen.h.

References gfDirected.

65  {
66  PGraph GraphPt = PGraph::New();
67  typename PGraph::TObj& Graph = *GraphPt;
68  Graph.Reserve(Rows*Cols, 4*Rows*Cols);
69  int node, r, c;
70  for (node = 0; node < Rows * Cols; node++) {
71  Graph.AddNode(node); }
72  for (r = 0; r < Rows; r++) {
73  for (c = 0; c < Cols; c++) {
74  const int nodeId = Cols*r + c;
75  if (r < Rows-1) { // bottom node
76  Graph.AddEdge(nodeId, nodeId+Cols);
77  if (Graph.HasFlag(gfDirected) && ! IsDir) {
78  Graph.AddEdge(nodeId+Cols, nodeId); }
79  }
80  if (c < Cols-1) { // right node
81  Graph.AddEdge(nodeId, nodeId+1);
82  if (Graph.HasFlag(gfDirected) && ! IsDir) {
83  Graph.AddEdge(nodeId+1, nodeId); }
84  }
85  }
86  }
87  return GraphPt;
88 }
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
PUNGraph TSnap::GenPrefAttach ( const int &  Nodes,
const int &  NodeOutDeg,
TRnd Rnd 
)

Generates a power-law degree distribution using Barabasi-Albert model of scale-free graphs.

Barabasi-Albert model of scale-free graphs. The graph has power-law degree distribution. See: Emergence of scaling in random networks by Barabasi and Albert. URL: http://arxiv.org/abs/cond-mat/9910332

Definition at line 313 of file ggen.cpp.

References TVec< TVal, TSizeTy >::Add(), TUNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TUNGraph::AddNode(), THashSet< TKey, THashFunc >::Clr(), TRnd::GetUniDevInt(), TVec< TVal, TSizeTy >::Len(), THashSet< TKey, THashFunc >::Len(), TPt< TRec >::New(), and TUNGraph::Reserve().

313  {
314  PUNGraph GraphPt = PUNGraph::New();
315  TUNGraph& Graph = *GraphPt;
316  Graph.Reserve(Nodes, NodeOutDeg*Nodes);
317  TIntV NIdV(NodeOutDeg*Nodes, 0);
318  // first edge
319  Graph.AddNode(0); Graph.AddNode(1);
320  NIdV.Add(0); NIdV.Add(1);
321  Graph.AddEdge(0, 1);
322  TIntSet NodeSet;
323  for (int node = 2; node < Nodes; node++) {
324  NodeSet.Clr(false);
325  while (NodeSet.Len() < NodeOutDeg && NodeSet.Len() < node) {
326  NodeSet.AddKey(NIdV[Rnd.GetUniDevInt(NIdV.Len())]);
327  }
328  const int N = Graph.AddNode();
329  for (int i = 0; i < NodeSet.Len(); i++) {
330  Graph.AddEdge(N, NodeSet[i]);
331  NIdV.Add(N);
332  NIdV.Add(NodeSet[i]);
333  }
334  }
335  return GraphPt;
336 }
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Definition: shash.h:1243
static TPt New()
Definition: bd.h:479
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
Undirected graph.
Definition: graph.h:32
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:302
int AddKey(const TKey &Key)
Definition: shash.h:1254
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92
int Len() const
Definition: shash.h:1121
Definition: bd.h:196
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39

Here is the call graph for this function:

PUNGraph TSnap::GenRewire ( const PUNGraph OrigGraph,
const int &  NSwitch,
TRnd Rnd 
)

Rewire a random undirected graph. Keeps node degrees the same, but randomly rewires the edges.

Rewire the network. Keeps node degrees as is but randomly rewires the edges. Use this function to generate a random graph with the same degree sequence as the OrigGraph. See: On the uniform generation of random graphs with prescribed degree sequences by R. Milo, N. Kashtan, S. Itzkovitz, M. E. J. Newman, U. Alon URL: http://arxiv.org/abs/cond-mat/0312028

Definition at line 168 of file ggen.cpp.

References TUNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TUNGraph::AddNode(), TUNGraph::BegNI(), THashSet< TKey, THashFunc >::DelKeyId(), TUNGraph::EndNI(), TUNGraph::GetEdges(), TUNGraph::GetNodes(), THashSet< TKey, THashFunc >::GetRndKeyId(), TExeTm::GetSecs(), TExeTm::GetStr(), THashSet< TKey, THashFunc >::IsKey(), THashSet< TKey, THashFunc >::Len(), TUNGraph::New(), TUNGraph::Reserve(), Swap(), TPair< TVal1, TVal2 >::Val1, and TPair< TVal1, TVal2 >::Val2.

Referenced by GenRndDegK(), GenRndPowerLaw(), and TLocClust::PlotNCP().

168  {
169  const int Nodes = OrigGraph->GetNodes();
170  const int Edges = OrigGraph->GetEdges();
171  PUNGraph GraphPt = TUNGraph::New();
172  TUNGraph& Graph = *GraphPt;
173  Graph.Reserve(Nodes, -1);
174  TExeTm ExeTm;
175  // generate a graph that satisfies the constraints
176  printf("Randomizing edges (%d, %d)...\n", Nodes, Edges);
177  TIntPrSet EdgeSet(Edges);
178  for (TUNGraph::TNodeI NI = OrigGraph->BegNI(); NI < OrigGraph->EndNI(); NI++) {
179  const int NId = NI.GetId();
180  for (int e = 0; e < NI.GetOutDeg(); e++) {
181  if (NId <= NI.GetOutNId(e)) { continue; }
182  EdgeSet.AddKey(TIntPr(NId, NI.GetOutNId(e)));
183  }
184  Graph.AddNode(NI.GetId());
185  }
186  // edge switching
187  uint skip=0;
188  for (uint swps = 0; swps < 2*uint(Edges)*uint(NSwitch); swps++) {
189  const int keyId1 = EdgeSet.GetRndKeyId(Rnd);
190  const int keyId2 = EdgeSet.GetRndKeyId(Rnd);
191  if (keyId1 == keyId2) { skip++; continue; }
192  const TIntPr& E1 = EdgeSet[keyId1];
193  const TIntPr& E2 = EdgeSet[keyId2];
194  TIntPr NewE1(E1.Val1, E2.Val1), NewE2(E1.Val2, E2.Val2);
195  if (NewE1.Val1 > NewE1.Val2) { Swap(NewE1.Val1, NewE1.Val2); }
196  if (NewE2.Val1 > NewE2.Val2) { Swap(NewE2.Val1, NewE2.Val2); }
197  if (NewE1!=NewE2 && NewE1.Val1!=NewE1.Val2 && NewE2.Val1!=NewE2.Val2 && ! EdgeSet.IsKey(NewE1) && ! EdgeSet.IsKey(NewE2)) {
198  EdgeSet.DelKeyId(keyId1); EdgeSet.DelKeyId(keyId2);
199  EdgeSet.AddKey(TIntPr(NewE1));
200  EdgeSet.AddKey(TIntPr(NewE2));
201  } else { skip++; }
202  if (swps % Edges == 0) {
203  printf("\r %uk/%uk: %uk skip [%s]", swps/1000u, 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
204  if (ExeTm.GetSecs() > 2*3600) { printf(" *** Time limit!\n"); break; } // time limit 2 hours
205  }
206  }
207  printf("\r total %uk switchings attempted, %uk skiped [%s]\n", 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
208  for (int e = 0; e < EdgeSet.Len(); e++) {
209  Graph.AddEdge(EdgeSet[e].Val1, EdgeSet[e].Val2); }
210  return GraphPt;
211 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
Definition: tm.h:355
unsigned int uint
Definition: bd.h:11
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
Undirected graph.
Definition: graph.h:32
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:302
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:172
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92
Definition: ds.h:32
double GetSecs() const
Definition: tm.h:366
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
const char * GetStr() const
Definition: tm.h:368
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568

Here is the call graph for this function:

Here is the caller graph for this function:

PNGraph TSnap::GenRewire ( const PNGraph OrigGraph,
const int &  NSwitch,
TRnd Rnd 
)

Rewire a random directed graph. Keeps node degrees the same, but randomly rewires the edges.

Rewire the network. Keeps node degrees as is but randomly rewires the edges. Use this function to generate a random graph with the same degree sequence as the OrigGraph. See: On the uniform generation of random graphs with prescribed degree sequences by R. Milo, N. Kashtan, S. Itzkovitz, M. E. J. Newman, U. Alon. URL: http://arxiv.org/abs/cond-mat/0312028

Definition at line 219 of file ggen.cpp.

References TNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TNGraph::AddNode(), THashSet< TKey, THashFunc >::DelKeyId(), THashSet< TKey, THashFunc >::GetRndKeyId(), TExeTm::GetSecs(), TExeTm::GetStr(), THashSet< TKey, THashFunc >::IsKey(), THashSet< TKey, THashFunc >::Len(), TNGraph::New(), TNGraph::Reserve(), TPair< TVal1, TVal2 >::Val1, and TPair< TVal1, TVal2 >::Val2.

219  {
220  const int Nodes = OrigGraph->GetNodes();
221  const int Edges = OrigGraph->GetEdges();
222  PNGraph GraphPt = TNGraph::New();
223  TNGraph& Graph = *GraphPt;
224  Graph.Reserve(Nodes, -1);
225  TExeTm ExeTm;
226  // generate a graph that satisfies the constraints
227  printf("Randomizing edges (%d, %d)...\n", Nodes, Edges);
228  TIntPrSet EdgeSet(Edges);
229  for (TNGraph::TNodeI NI = OrigGraph->BegNI(); NI < OrigGraph->EndNI(); NI++) {
230  const int NId = NI.GetId();
231  for (int e = 0; e < NI.GetOutDeg(); e++) {
232  EdgeSet.AddKey(TIntPr(NId, NI.GetOutNId(e))); }
233  Graph.AddNode(NI);
234  }
235  // edge switching
236  uint skip=0;
237  for (uint swps = 0; swps < 2*uint(Edges)*uint(NSwitch); swps++) {
238  const int keyId1 = EdgeSet.GetRndKeyId(Rnd);
239  const int keyId2 = EdgeSet.GetRndKeyId(Rnd);
240  if (keyId1 == keyId2) { skip++; continue; }
241  const TIntPr& E1 = EdgeSet[keyId1];
242  const TIntPr& E2 = EdgeSet[keyId2];
243  TIntPr NewE1(E1.Val1, E2.Val2), NewE2(E2.Val1, E1.Val2);
244  if (NewE1.Val1!=NewE1.Val2 && NewE2.Val1!=NewE2.Val2 && NewE1.Val1!=NewE2.Val1 && NewE1.Val2!=NewE2.Val2 && ! EdgeSet.IsKey(NewE1) && ! EdgeSet.IsKey(NewE2)) {
245  EdgeSet.DelKeyId(keyId1); EdgeSet.DelKeyId(keyId2);
246  EdgeSet.AddKey(TIntPr(NewE1));
247  EdgeSet.AddKey(TIntPr(NewE2));
248  } else { skip++; }
249  if (swps % Edges == 0) {
250  printf("\r %uk/%uk: %uk skip [%s]", swps/1000u, 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
251  if (ExeTm.GetSecs() > 2*3600) { printf(" *** Time limit!\n"); break; } // time limit 2 hours
252  }
253  }
254  printf("\r total %uk switchings attempted, %uk skiped [%s]\n", 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
255  for (int e = 0; e < EdgeSet.Len(); e++) {
256  Graph.AddEdge(EdgeSet[e].Val1, EdgeSet[e].Val2); }
257  return GraphPt;
258 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
Definition: tm.h:355
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
unsigned int uint
Definition: bd.h:11
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
Directed graph.
Definition: graph.h:346
Definition: ds.h:32
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
double GetSecs() const
Definition: tm.h:366
TVal1 Val1
Definition: ds.h:34
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:614
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
const char * GetStr() const
Definition: tm.h:368

Here is the call graph for this function:

PBPGraph TSnap::GenRewire ( const PBPGraph OrigGraph,
const int &  NSwitch,
TRnd Rnd 
)

Rewire a random bipartite graph. Keeps node degrees the same, but randomly rewires the edges.

Rewire a bipartite graph. Keeps node degrees as is but randomly rewires the edges. Use this function to generate a random graph with the same degree sequence as the OrigGraph. See: On the uniform generation of random graphs with prescribed degree sequences by R. Milo, N. Kashtan, S. Itzkovitz, M. E. J. Newman, U. Alon URL: http://arxiv.org/abs/cond-mat/0312028

Definition at line 266 of file ggen.cpp.

References TBPGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TBPGraph::AddNode(), THashSet< TKey, THashFunc >::DelKeyId(), THashSet< TKey, THashFunc >::GetRndKeyId(), TExeTm::GetSecs(), TExeTm::GetStr(), IAssert, THashSet< TKey, THashFunc >::IsKey(), THashSet< TKey, THashFunc >::Len(), TBPGraph::New(), TBPGraph::Reserve(), TPair< TVal1, TVal2 >::Val1, and TPair< TVal1, TVal2 >::Val2.

266  {
267  const int Nodes = OrigGraph->GetNodes();
268  const int Edges = OrigGraph->GetEdges();
269  PBPGraph GraphPt = TBPGraph::New();
270  TBPGraph& Graph = *GraphPt;
271  Graph.Reserve(Nodes, -1);
272  TExeTm ExeTm;
273  // generate a graph that satisfies the constraints
274  printf("Randomizing edges (%d, %d)...\n", Nodes, Edges);
275  TIntPrSet EdgeSet(Edges);
276  for (TBPGraph::TNodeI NI = OrigGraph->BegLNI(); NI < OrigGraph->EndLNI(); NI++) {
277  const int NId = NI.GetId();
278  for (int e = 0; e < NI.GetOutDeg(); e++) {
279  EdgeSet.AddKey(TIntPr(NId, NI.GetOutNId(e))); } // edges left-->right
280  Graph.AddNode(NI.GetId(), true); } // left nodes
281  for (TBPGraph::TNodeI NI = OrigGraph->BegRNI(); NI < OrigGraph->EndRNI(); NI++) {
282  Graph.AddNode(NI.GetId(), false); } // right nodes
283  IAssert(EdgeSet.Len() == Edges);
284  // edge switching
285  uint skip=0;
286  for (uint swps = 0; swps < 2*uint(Edges)*uint(NSwitch); swps++) {
287  const int keyId1 = EdgeSet.GetRndKeyId(Rnd);
288  const int keyId2 = EdgeSet.GetRndKeyId(Rnd);
289  if (keyId1 == keyId2) { skip++; continue; }
290  const TIntPr& E1 = EdgeSet[keyId1];
291  const TIntPr& E2 = EdgeSet[keyId2];
292  TIntPr NewE1(E1.Val1, E2.Val2), NewE2(E2.Val1, E1.Val2);
293  if (NewE1!=NewE2 && NewE1.Val1!=NewE1.Val2 && NewE2.Val1!=NewE2.Val2 && ! EdgeSet.IsKey(NewE1) && ! EdgeSet.IsKey(NewE2)) {
294  EdgeSet.DelKeyId(keyId1); EdgeSet.DelKeyId(keyId2);
295  EdgeSet.AddKey(TIntPr(NewE1));
296  EdgeSet.AddKey(TIntPr(NewE2));
297  } else { skip++; }
298  if (swps % Edges == 0) {
299  printf("\r %uk/%uk: %uk skip [%s]", swps/1000u, 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
300  if (ExeTm.GetSecs() > 2*3600) { printf(" *** Time limit!\n"); break; } // time limit 2 hours
301  }
302  }
303  printf("\r total %uk switchings attempted, %uk skiped [%s]\n", 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
304  for (int e = 0; e < EdgeSet.Len(); e++) {
305  Graph.AddEdge(EdgeSet[e].Val1, EdgeSet[e].Val2); }
306  return GraphPt;
307 }
#define IAssert(Cond)
Definition: bd.h:262
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a biparite graph of Nodes nodes and Edges edges.
Definition: graph.cpp:790
Definition: tm.h:355
unsigned int uint
Definition: bd.h:11
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:968
int AddNode(int NId=-1, const bool &LeftNode=true)
Adds a node of ID NId to the graph.
Definition: graph.cpp:671
int AddEdge(const int &LeftNId, const int &RightNId)
Adds an edge between a node LeftNId on the left and a node RightNId on the right side of the bipartit...
Definition: graph.cpp:705
Definition: ds.h:32
Bipartite graph.
Definition: graph.h:936
double GetSecs() const
Definition: tm.h:366
TVal1 Val1
Definition: ds.h:34
static PBPGraph New()
Static constructor that returns a pointer to the graph. Call: PBPGraph BPGraph = TBPGraph::New();.
Definition: graph.h:1062
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
const char * GetStr() const
Definition: tm.h:368

Here is the call graph for this function:

PNGraph TSnap::GenRMat ( const int &  Nodes,
const int &  Edges,
const double &  A,
const double &  B,
const double &  C,
TRnd Rnd 
)

Generates a R-MAT graph using recursive descent into a 2x2 matrix [A,B; C, 1-(A+B+C)].

R-MAT Generator. The modes is based on the recursive descent into a 2x2 matrix [A,B; C, 1-(A+B+C)]. See: R-MAT Generator: A Recursive Model for Graph Mining. D. Chakrabarti, Y. Zhan and C. Faloutsos, in SIAM Data Mining 2004. URL: http://www.cs.cmu.edu/~deepay/mywww/papers/siam04.pdf

Definition at line 481 of file ggen.cpp.

References TVec< TVal, TSizeTy >::Add(), TNGraph::AddEdge(), TNGraph::AddNode(), TNGraph::Defrag(), edge, Fail, TRnd::GetUniDev(), IAssert, TNGraph::IsEdge(), TNGraph::New(), and TNGraph::Reserve().

Referenced by GenRMatEpinions().

481  {
482  PNGraph GraphPt = TNGraph::New();
483  TNGraph& Graph = *GraphPt;
484  Graph.Reserve(Nodes, Edges);
485  IAssert(A+B+C < 1.0);
486  int rngX, rngY, offX, offY;
487  int Depth=0, Collisions=0, Cnt=0, PctDone=0;
488  const int EdgeGap = Edges / 100 + 1;
489  // sum of parameters (probabilities)
490  TVec<double> sumA(128, 0), sumAB(128, 0), sumAC(128, 0), sumABC(128, 0); // up to 2^128 vertices ~ 3.4e38
491  for (int i = 0; i < 128; i++) {
492  const double a = A * (Rnd.GetUniDev() + 0.5);
493  const double b = B * (Rnd.GetUniDev() + 0.5);
494  const double c = C * (Rnd.GetUniDev() + 0.5);
495  const double d = (1.0 - (A+B+C)) * (Rnd.GetUniDev() + 0.5);
496  const double abcd = a+b+c+d;
497  sumA.Add(a / abcd);
498  sumAB.Add((a+b) / abcd);
499  sumAC.Add((a+c) / abcd);
500  sumABC.Add((a+b+c) / abcd);
501  }
502  // nodes
503  for (int node = 0; node < Nodes; node++) {
504  IAssert(Graph.AddNode(-1) == node);
505  }
506  // edges
507  for (int edge = 0; edge < Edges; ) {
508  rngX = Nodes; rngY = Nodes; offX = 0; offY = 0;
509  Depth = 0;
510  // recurse the matrix
511  while (rngX > 1 || rngY > 1) {
512  const double RndProb = Rnd.GetUniDev();
513  if (rngX>1 && rngY>1) {
514  if (RndProb < sumA[Depth]) { rngX/=2; rngY/=2; }
515  else if (RndProb < sumAB[Depth]) { offX+=rngX/2; rngX-=rngX/2; rngY/=2; }
516  else if (RndProb < sumABC[Depth]) { offY+=rngY/2; rngX/=2; rngY-=rngY/2; }
517  else { offX+=rngX/2; offY+=rngY/2; rngX-=rngX/2; rngY-=rngY/2; }
518  } else
519  if (rngX>1) { // row vector
520  if (RndProb < sumAC[Depth]) { rngX/=2; rngY/=2; }
521  else { offX+=rngX/2; rngX-=rngX/2; rngY/=2; }
522  } else
523  if (rngY>1) { // column vector
524  if (RndProb < sumAB[Depth]) { rngX/=2; rngY/=2; }
525  else { offY+=rngY/2; rngX/=2; rngY-=rngY/2; }
526  } else { Fail; }
527  Depth++;
528  }
529  // add edge
530  const int NId1 = offX;