SNAP Library 6.0, User 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.

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
void TSnap::AddEdgeWithAttributes ( const PNEANet Graph1,
PNEANet Graph2,
const int  NId,
const int  NbrId 
)

Definition at line 217 of file subgraph.cpp.

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
void TSnap::AddNodeWithAttributes ( const PNEANet Graph1,
PNEANet Graph2,
const int  NId 
)

Definition at line 143 of file subgraph.cpp.

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
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.

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
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.

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
double TSnap::BorgattiEverettMeasure ( PUNGraph Graph,
TIntIntH out,
double  coresize,
int  type 
)

Definition at line 186 of file coreper.cpp.

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
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.

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
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.

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
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.

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
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.

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
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.

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
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.

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
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.

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
void TSnap::CmtyEvolutionJson ( TStr Json,
TIntIntVH sizesContV,
TIntIntVH cContV,
TIntIntVH edges 
)

Definition at line 725 of file cmty.cpp.

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
TStr TSnap::CmtyTest ( TStr  InFNm,
int  CmtyAlg 
)

Definition at line 827 of file cmty.cpp.

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
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.

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 }
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.

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
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.

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 }
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.

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 }
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.

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 }
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.

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
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.

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
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.

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
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.

1449  {
1450  return TSnapDetail::TCNMQMatrix::CmtyCMN(Graph, CmtyV);
1451 }
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.

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
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.

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
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.

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
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.

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
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.

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
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.

419  {
420  TSnapDetail::TDelSelfEdges<PGraph, HasGraphFlag(typename PGraph::TObj, gfMultiGraph)>
421  ::Do(Graph);
422 }
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.

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
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.

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
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.

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
TIntFltH TSnap::EventImportance ( const PNGraph Graph,
const int  k 
)

Event importance.

Definition at line 527 of file centr.cpp.

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
TIntFltH TSnap::EventImportance1 ( const PNGraph Graph,
const int  k 
)

Definition at line 556 of file centr.cpp.

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
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.

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
int TSnap::FastCorePeripheryGC ( PUNGraph Graph,
TIntIntH out 
)

Definition at line 56 of file coreper.cpp.

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
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.

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
int TSnap::findMinimum ( TIntV Frontier,
TIntFltH NIdDistH 
)

Definition at line 685 of file centr.cpp.

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
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.

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
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.

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.

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
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.

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
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.

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
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.

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
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.

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
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.

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
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.

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.

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
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.

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
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.

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
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.

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
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.

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;
531  const int NId2 = offY;
532  if (NId1 != NId2 && ! Graph.IsEdge(NId1, NId2)) {
533  Graph.AddEdge(NId1, NId2);
534  if (++Cnt > EdgeGap) {
535  Cnt=0; printf("\r %d%% edges", ++PctDone); }
536  edge++;
537  } else {
538  Collisions++; }
539  }
540  printf("\r RMat: nodes:%d, edges:%d, Iterations:%d, Collisions:%d (%.1f%%).\n", Nodes, Edges,
541  Edges+Collisions, Collisions, 100*Collisions/double(Edges+Collisions));
542  Graph.Defrag();
543  return GraphPt;
544 }
#define IAssert(Cond)
Definition: bd.h:262
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
#define Fail
Definition: bd.h:238
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
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:382
Directed graph.
Definition: graph.h:346
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
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
PNGraph TSnap::GenRMatEpinions ( )

Generates a R-Mat graph, with a synthetic copy of the Epinions social network.

R-Mat generator with parameters set so that it generates a synthetic copy of the Epinions social network. The original Epinions social network can be downloaded at http://snap.stanford.edu/data/soc-Epinions1.html

Definition at line 550 of file ggen.cpp.

550  {
551  return GenRMat(75888, 508837, 0.550, 0.228, 0.212);
552 }
PNGraph 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)].
Definition: ggen.cpp:481
PBPGraph TSnap::GenRndBipart ( const int &  LeftNodes,
const int &  RightNodes,
const int &  Edges,
TRnd Rnd 
)

Generates a random bipartite graph.

Definition at line 5 of file ggen.cpp.

5  {
7  for (int i = 0; i < LeftNodes; i++) { G->AddNode(i, true); }
8  for (int i = 0; i < RightNodes; i++) { G->AddNode(LeftNodes+i, false); }
9  IAssertR(Edges <= LeftNodes*RightNodes, "Too many edges in the bipartite graph!");
10  for (int edges = 0; edges < Edges; ) {
11  const int LNId = Rnd.GetUniDevInt(LeftNodes);
12  const int RNId = LeftNodes + Rnd.GetUniDevInt(RightNodes);
13  if (G->AddEdge(LNId, RNId) != -2) { edges++; } // is new edge
14  }
15  return G;
16 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static PBPGraph New()
Static constructor that returns a pointer to the graph. Call: PBPGraph BPGraph = TBPGraph::New();.
Definition: graph.h:1062
Definition: bd.h:196
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
PUNGraph TSnap::GenRndDegK ( const int &  Nodes,
const int &  NodeDeg,
const int &  NSwitch,
TRnd Rnd 
)

Generates a random graph where each node has degree exactly NodeDeg.

Definition at line 18 of file ggen.cpp.

18  {
19  // create degree sequence
20  TIntV DegV(Nodes, 0);
21  int DegSum=0;
22  for (int i = 0; i < Nodes; i++) {
23  DegV.Add(NodeDeg);
24  DegSum += NodeDeg;
25  }
26  IAssert(DegSum % 2 == 0);
27  PUNGraph G = GenDegSeq(DegV, Rnd); // get some graph that obeys the degree sequnce
28  return GenRewire(G, NSwitch, Rnd); // make it random
29 }
#define IAssert(Cond)
Definition: bd.h:262
PUNGraph GenDegSeq(const TIntV &DegSeqV, TRnd &Rnd)
Generates a random graph with exact degree sequence.
Definition: ggen.cpp:61
PBPGraph GenRewire(const PBPGraph &OrigGraph, const int &NSwitch, TRnd &Rnd)
Rewire a random bipartite graph. Keeps node degrees the same, but randomly rewires the edges...
Definition: ggen.cpp:266
Definition: bd.h:196
template<class PGraph >
PGraph TSnap::GenRndGnm ( const int &  Nodes,
const int &  Edges,
const bool &  IsDir = true,
TRnd Rnd = TInt::Rnd 
)

Generates an Erdos-Renyi random graph.

Definition at line 218 of file ggen.h.

218  {
219  PGraph GraphPt = PGraph::New();
220  typename PGraph::TObj& Graph = *GraphPt;
221  Graph.Reserve(Nodes, Edges);
222  IAssertR((1.0 * (Nodes-1) / 2 * (IsDir ? 2 : 1)) >= (1.0 * Edges / Nodes), TStr::Fmt("Not enough nodes (%d), for edges (%d).", Nodes, Edges));
223  for (int node = 0; node < Nodes; node++) {
224  IAssert(Graph.AddNode(node) == node);
225  }
226  for (int edge = 0; edge < Edges; ) {
227  const int SrcNId = Rnd.GetUniDevInt(Nodes);
228  const int DstNId = Rnd.GetUniDevInt(Nodes);
229  if (SrcNId != DstNId && Graph.AddEdge(SrcNId, DstNId) != -2) { // is new edge
230  if (! IsDir) { Graph.AddEdge(DstNId, SrcNId); }
231  edge++;
232  }
233  }
234  return GraphPt;
235 }
#define IAssert(Cond)
Definition: bd.h:262
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
PUNGraph TSnap::GenRndPowerLaw ( const int &  Nodes,
const double &  PowerExp,
const bool &  ConfModel,
TRnd Rnd 
)

Generates a random scale-free graph with power-law degree distribution.

Generates a random scale-free graph with power-law degree distribution with exponent PowerExp. The method uses either the Configuration model (fast but the result is approximate) or the Edge Rewiring method (slow but exact).

Definition at line 34 of file ggen.cpp.

34  {
35  TIntV DegSeqV;
36  uint DegSum=0;
37  for (int n = 0; n < Nodes; n++) {
38  const int Val = (int) TMath::Round(Rnd.GetPowerDev(PowerExp));
39  if (! (Val >= 1 && Val < Nodes/2)) { n--; continue; } // skip nodes with too large degree
40  DegSeqV.Add(Val);
41  DegSum += Val;
42  }
43  printf("%d nodes, %u edges\n", Nodes, DegSum);
44  if (DegSum % 2 == 1) { DegSeqV[0] += 1; }
45  if (ConfModel) {
46  // use configuration model -- fast but does not exactly obey the degree sequence
47  return GenConfModel(DegSeqV, Rnd);
48  } else {
49  DegSeqV.Sort();
50  DegSeqV.Reverse();
51  PUNGraph G = TSnap::GenDegSeq(DegSeqV, Rnd);
52  return TSnap::GenRewire(G, 10, Rnd);
53  }
54 }
PUNGraph GenRewire(const PUNGraph &OrigGraph, const int &NSwitch, TRnd &Rnd)
Rewire a random undirected graph. Keeps node degrees the same, but randomly rewires the edges...
Definition: ggen.cpp:168
unsigned int uint
Definition: bd.h:11
PUNGraph GenDegSeq(const TIntV &DegSeqV, TRnd &Rnd)
Generates a random graph with exact degree sequence.
Definition: ggen.cpp:61
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
static double Round(const double &Val)
Definition: xmath.h:16
PUNGraph GenConfModel(const PUNGraph &G)
Generate a random graph using (approximately) the same node degrees as in G using the configuration m...
Definition: ggen.cpp:338
Definition: bd.h:196
void Reverse()
Reverses the order of the elements in the vector.
Definition: ds.h:1350
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
double GetPowerDev(const double &AlphaSlope)
Definition: dt.h:47
PUNGraph TSnap::GenSmallWorld ( const int &  Nodes,
const int &  NodeOutDeg,
const double &  RewireProb,
TRnd Rnd 
)

Generates a randomly small-world graph using the Watts-Strogatz model.

Generates a small-world graph using the Watts-Strogatz model. We assume a circle where each node creates links to NodeOutDeg other nodes. This way at the end each node is connected to 2*NodeOutDeg other nodes. See: Collective dynamics of 'small-world' networks. Watts and Strogatz. URL: http://research.yahoo.com/files/w_s_NATURE_0.pdf

Definition at line 415 of file ggen.cpp.

415  {
416  THashSet<TIntPr> EdgeSet(Nodes*NodeOutDeg);
417 
418  IAssertR(Nodes > NodeOutDeg, TStr::Fmt("Insufficient nodes for out degree, %d!", NodeOutDeg));
419  for (int node = 0; node < Nodes; node++) {
420  const int src = node;
421  for (int edge = 1; edge <= NodeOutDeg; edge++) {
422  int dst = (node+edge) % Nodes; // edge to next neighbor
423  if (Rnd.GetUniDev() < RewireProb) { // random edge
424  dst = Rnd.GetUniDevInt(Nodes);
425  while (dst == src || EdgeSet.IsKey(TIntPr(src, dst))) {
426  dst = Rnd.GetUniDevInt(Nodes); }
427  }
428  EdgeSet.AddKey(TIntPr(src, dst));
429  }
430  }
431  PUNGraph GraphPt = TUNGraph::New();
432  TUNGraph& Graph = *GraphPt;
433  Graph.Reserve(Nodes, EdgeSet.Len());
434  int node;
435  for (node = 0; node < Nodes; node++) {
436  IAssert(Graph.AddNode(node) == node);
437  }
438  for (int edge = 0; edge < EdgeSet.Len(); edge++) {
439  Graph.AddEdge(EdgeSet[edge].Val1, EdgeSet[edge].Val2);
440  }
441  Graph.Defrag();
442  return GraphPt;
443 }
#define IAssert(Cond)
Definition: bd.h:262
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
#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
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
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:160
double GetUniDev()
Definition: dt.h:30
Definition: bd.h:196
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
template<class PGraph >
PGraph TSnap::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.

Definition at line 91 of file ggen.h.

91  {
92  PGraph Graph = PGraph::TObj::New();
93  Graph->Reserve(Nodes, Nodes);
94  Graph->AddNode(0);
95  for (int n = 1; n < Nodes; n++) {
96  Graph->AddNode(n);
97  Graph->AddEdge(0, n);
98  if (Graph->HasFlag(gfDirected) && ! IsDir) { Graph->AddEdge(n, 0); }
99  }
100  return Graph;
101 }
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
template<class PGraph >
PGraph TSnap::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.

Definition at line 133 of file ggen.h.

133  {
134  const int Nodes = (int) (pow(double(Fanout), double(Levels+1)) - 1) / (Fanout - 1);
135  const int Edges = Nodes - 1;
136  PGraph GraphPt = PGraph::New();
137  typename PGraph::TObj& Graph = *GraphPt;
138  Graph.Reserve(Nodes, Edges);
139  int node;
140  for (node = 0; node < Nodes; node++) {
141  Graph.AddNode(node); }
142  // non-leaf nodes
143  for (node = 0; node < (int) Nodes - (int) pow(double(Fanout), double(Levels)); node++) {
144  for (int edge = 1; edge <= Fanout; edge++) {
145  if (IsDir) {
146  if (ChildPointsToParent) { Graph.AddEdge(Fanout*node+edge, node); }
147  else { Graph.AddEdge(node, Fanout*node+edge); }
148  } else {
149  Graph.AddEdge(node, Fanout*node+edge); // link children
150  Graph.AddEdge(Fanout*node+edge, node);
151  }
152  }
153  }
154  return GraphPt;
155 }
void TSnap::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.

We find such components as follows: Find all bridge edges, remove them from the Graph, find largest component K and add back all bridges that do not touch K. Now, find the connected components of this graph.

Definition at line 98 of file cncom.cpp.

98  {
99  //TCnCom::GetWccCnt(Graph, SzCntV); IAssertR(SzCntV.Len() == 1, "Graph is not connected.");
100  TIntPrV EdgeV;
101  GetEdgeBridges(Graph, EdgeV);
102  if (EdgeV.Empty()) { Cn1ComV.Clr(false); return; }
103  PUNGraph TmpG = TUNGraph::New();
104  *TmpG = *Graph;
105  for (int e = 0; e < EdgeV.Len(); e++) {
106  TmpG->DelEdge(EdgeV[e].Val1, EdgeV[e].Val2); }
107  TCnComV CnComV; GetWccs(TmpG, CnComV);
108  IAssert(CnComV.Len() >= 2);
109  const TIntV& MxWcc = CnComV[0].NIdV;
110  TIntSet MxCcSet(MxWcc.Len());
111  for (int i = 0; i < MxWcc.Len(); i++) {
112  MxCcSet.AddKey(MxWcc[i]); }
113  // create new graph: bridges not touching MxCc of G with no bridges
114  for (int e = 0; e < EdgeV.Len(); e++) {
115  if (! MxCcSet.IsKey(EdgeV[e].Val1) && ! MxCcSet.IsKey(EdgeV[e].Val2)) {
116  TmpG->AddEdge(EdgeV[e].Val1, EdgeV[e].Val2); }
117  }
118  GetWccs(TmpG, Cn1ComV);
119  // remove the largest component of G
120  for (int c = 0; c < Cn1ComV.Len(); c++) {
121  if (MxCcSet.IsKey(Cn1ComV[c].NIdV[0])) {
122  Cn1ComV.Del(c); break; }
123  }
124 }
#define IAssert(Cond)
Definition: bd.h:262
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
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
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
int AddKey(const TKey &Key)
Definition: shash.h:1254
void GetEdgeBridges(const PUNGraph &Graph, TIntPrV &EdgeV)
Returns bridge edges of a Graph.
Definition: cncom.cpp:55
Definition: bd.h:196
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
Definition: cncom.h:376
void TSnap::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.

We find such components as follows: Find all bridge edges, remove them from the Graph, find largest component K and add back all bridges that do not touch K. Now, find the connected components of this graph.

Definition at line 70 of file cncom.cpp.

70  {
71  //TCnCom::GetWccCnt(Graph, SzCntV); IAssertR(SzCntV.Len() == 1, "Graph is not connected.");
72  TIntPrV EdgeV;
73  GetEdgeBridges(Graph, EdgeV);
74  if (EdgeV.Empty()) { SzCntV.Clr(false); return; }
75  PUNGraph TmpG = TUNGraph::New();
76  *TmpG = *Graph;
77  for (int e = 0; e < EdgeV.Len(); e++) {
78  TmpG->DelEdge(EdgeV[e].Val1, EdgeV[e].Val2); }
79  TCnComV CnComV; GetWccs(TmpG, CnComV);
80  IAssert(CnComV.Len() >= 2);
81  const TIntV& MxWcc = CnComV[0].NIdV;
82  TIntSet MxCcSet(MxWcc.Len());
83  for (int i = 0; i < MxWcc.Len(); i++) {
84  MxCcSet.AddKey(MxWcc[i]); }
85  // create new graph: bridges not touching MxCc of G with no bridges
86  for (int e = 0; e < EdgeV.Len(); e++) {
87  if (! MxCcSet.IsKey(EdgeV[e].Val1) && ! MxCcSet.IsKey(EdgeV[e].Val2)) {
88  TmpG->AddEdge(EdgeV[e].Val1, EdgeV[e].Val2); }
89  }
90  GetWccSzCnt(TmpG, SzCntV);
91  for (int c = 0; c < SzCntV.Len(); c++) {
92  if (SzCntV[c].Val1 == MxCcSet.Len()) {
93  SzCntV.Del(c); break; }
94  }
95 }
#define IAssert(Cond)
Definition: bd.h:262
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
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
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
int AddKey(const TKey &Key)
Definition: shash.h:1254
void GetEdgeBridges(const PUNGraph &Graph, TIntPrV &EdgeV)
Returns bridge edges of a Graph.
Definition: cncom.cpp:55
Definition: bd.h:196
void GetWccSzCnt(const PGraph &Graph, TIntPrV &WccSzCnt)
Returns a distribution of weakly connected component sizes.
Definition: cncom.h:337
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
Definition: cncom.h:376
template<class PGraph >
void TSnap::GetAnf ( const PGraph &  Graph,
const int &  SrcNId,
TIntFltKdV DistNbrsV,
const int &  MxDist,
const bool &  IsDir,
const int &  NApprox = 32 
)

Approximate Neighborhood Function of a node: Returns the (approximate) number of nodes reachable from SrcNId in less than H hops.

Parameters
SrcNIdStarting node.
DistNbrsVMaps between the distance H (in hops) and the number of nodes reachable in <=H hops.
MxDistMaximum number of hops the algorithm spreads from SrcNId.
IsDirfalse: consider links as undirected (drop link directions).
NApproxQuality of approximation. See the ANF paper.

Definition at line 204 of file anf.h.

204  {
205  TGraphAnf<PGraph> Anf(Graph, NApprox, 5, 0);
206  Anf.GetNodeAnf(SrcNId, DistNbrsV, MxDist, IsDir);
207 }
Definition: anf.h:33
template<class PGraph >
void TSnap::GetAnf ( const PGraph &  Graph,
TIntFltKdV DistNbrsV,
const int &  MxDist,
const bool &  IsDir,
const int &  NApprox = 32 
)

Approximate Neighborhood Function of a Graph: Returns the number of pairs of nodes reachable in less than H hops. For example, DistNbrsV.GetDat(0) is the number of nodes in the graph, DistNbrsV.GetDat(1) is the number of nodes+edges and so on.

Parameters
DistNbrsVMaps between the distance H (in hops) and the number of nodes reachable in <=H hops.
MxDistMaximum number of hops the algorithm spreads from SrcNId.
IsDirfalse: consider links as undirected (drop link directions).
NApproxQuality of approximation. See the ANF paper.

Definition at line 210 of file anf.h.

210  {
211  TGraphAnf<PGraph> Anf(Graph, NApprox, 5, 0);
212  Anf.GetGraphAnf(DistNbrsV, MxDist, IsDir);
213 }
Definition: anf.h:33
template<class PGraph >
double TSnap::GetAnfEffDiam ( const PGraph &  Graph,
const bool &  IsDir,
const double &  Percentile,
const int &  NApprox 
)

Returns a given Percentile of the shortest path length distribution of a Graph (based on a single run of ANF of approximation quality NApprox).

Parameters
IsDirfalse: consider links as undirected (drop link directions).

Definition at line 216 of file anf.h.

216  {
217  TIntFltKdV DistNbrsV;
218  TGraphAnf<PGraph> Anf(Graph, NApprox, 5, 0);
219  Anf.GetGraphAnf(DistNbrsV, -1, IsDir);
220  return TSnap::TSnapDetail::CalcEffDiam(DistNbrsV, Percentile);
221 }
double CalcEffDiam(const TIntFltKdV &DistNbrsCdfV, const double &Percentile)
Helper function for computing a given Percentile of a (unnormalized) cumulative distribution function...
Definition: anf.cpp:7
Definition: anf.h:33
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
template<class PGraph >
double TSnap::GetAnfEffDiam ( const PGraph &  Graph,
const int  NRuns = 1,
int  NApprox = -1 
)

Returns a 90-th percentile of the shortest path length distribution of a Graph (based on a NRuns runs of ANF of approximation quality NApprox).

Parameters
IsDirfalse: consider links as undirected (drop link directions).

Definition at line 224 of file anf.h.

224  {
225  //return TSnap::GetEffDiam(Graph, IsDir, 0.9, 32);
226  TMom Mom;
227  if (NApprox == -1) {
228  if (Graph->GetNodes() < 100000) { NApprox = 64; }
229  else if (Graph->GetNodes() < 1000000) { NApprox = 32; }
230  else { NApprox = 16; }
231  }
232  const bool IsDir = false;
233  for (int r = 0; r < NRuns; r++) {
234  Mom.Add(TSnap::GetAnfEffDiam(Graph, IsDir, 0.9, NApprox));
235  }
236  Mom.Def();
237  return Mom.GetMean();
238 }
double GetAnfEffDiam(const PGraph &Graph, const bool &IsDir, const double &Percentile, const int &NApprox)
Definition: anf.h:216
Definition: xmath.h:129
void Add(const TFlt &Val, const TFlt &Wgt=1)
Definition: xmath.h:217
double GetMean() const
Definition: xmath.h:240
void Def()
Definition: xmath.cpp:339
void TSnap::GetArtPoints ( const PUNGraph Graph,
TIntV ArtNIdV 
)

Returns articulation points of a Graph.

Articulation point (or a cut vertex) is any node that when removed increases the number of connected components.

Definition at line 48 of file cncom.cpp.

48  {
49  TArtPointVisitor Visitor(Graph->GetNodes());
50  TCnCom::GetDfsVisitor(Graph, Visitor);
51  Visitor.ArtSet.GetKeyV(ArtNIdV);
52 }
static void GetDfsVisitor(const PGraph &Graph, TVisitor &Visitor)
Definition: cncom.h:124
Articulation point Depth-First-Search visitor class.
Definition: cncom.h:169
template<class PGraph >
void TSnap::GetBetweennessCentr ( const PGraph &  Graph,
TIntFltH NIdBtwH,
const double &  NodeFrac = 1.0,
const bool &  IsDir = false 
)

Computes (approximate) Node Beetweenness Centrality based on a sample of NodeFrac nodes.

Parameters
NIdBtwHhash table mapping node ids to their corresponding betweenness centrality values.
NodeFracquality of approximation. NodeFrac=1.0 gives exact betweenness values.

Definition at line 489 of file centr.h.

489  {
490  TIntPrFltH EdgeBtwH;
491  TIntV NIdV; Graph->GetNIdV(NIdV);
492  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
493  NIdV.Shuffle(TInt::Rnd);
494  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
495  NIdV.DelLast(); }
496  }
497  GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, true, EdgeBtwH, false, IsDir);
498 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static TRnd Rnd
Definition: dt.h:1146
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
void DelLast()
Removes the last element of the vector.
Definition: ds.h:665
template<class PGraph >
void TSnap::GetBetweennessCentr ( const PGraph &  Graph,
TIntPrFltH EdgeBtwH,
const double &  NodeFrac = 1.0,
const bool &  IsDir = false 
)

Computes (approximate) Edge Beetweenness Centrality based on a sample of NodeFrac nodes.

Parameters
EdgeBtwHhash table mapping edges (pairs of node ids) to their corresponding betweenness centrality values.
NodeFracquality of approximation. NodeFrac=1.0 gives exact betweenness values.

Definition at line 501 of file centr.h.

501  {
502  TIntFltH NodeBtwH;
503  TIntV NIdV; Graph->GetNIdV(NIdV);
504  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
505  NIdV.Shuffle(TInt::Rnd);
506  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
507  NIdV.DelLast(); }
508  }
509  GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, false, EdgeBtwH, true, IsDir);
510 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static TRnd Rnd
Definition: dt.h:1146
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
void DelLast()
Removes the last element of the vector.
Definition: ds.h:665
template<class PGraph >
void TSnap::GetBetweennessCentr ( const PGraph &  Graph,
TIntFltH NIdBtwH,
TIntPrFltH EdgeBtwH,
const double &  NodeFrac = 1.0,
const bool &  IsDir = false 
)

Computes (approximate) Node and Edge Beetweenness Centrality based on a sample of NodeFrac nodes.

Parameters
NIdBtwHhash table mapping node ids to their corresponding betweenness centrality values.
EdgeBtwHhash table mapping edges (pairs of node ids) to their corresponding betweenness centrality values.
NodeFracquality of approximation. NodeFrac=1.0 gives exact betweenness values.

Definition at line 513 of file centr.h.

513  {
514  TIntV NIdV; Graph->GetNIdV(NIdV);
515  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
516  NIdV.Shuffle(TInt::Rnd);
517  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
518  NIdV.DelLast(); }
519  }
520  GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, true, EdgeBtwH, true, IsDir);
521 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static TRnd Rnd
Definition: dt.h:1146
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
void DelLast()
Removes the last element of the vector.
Definition: ds.h:665
template<class PGraph >
void TSnap::GetBetweennessCentr ( const PGraph &  Graph,
const TIntV BtwNIdV,
TIntFltH NodeBtwH,
const bool &  DoNodeCent,
TIntPrFltH EdgeBtwH,
const bool &  DoEdgeCent,
const bool &  IsDir 
)

Computes (approximate) Beetweenness Centrality of all nodes and all edges of the network. To obtain exact betweenness values one needs to solve single-source shortest-path problem for every node. To speed up the algorithm we solve the shortest-path problem for the BtwNIdV subset of nodes. This gives centrality values that are about Graph->GetNodes()/BtwNIdV.Len() times lower than the exact betweenness centrality valus. See "A Faster Algorithm for Beetweenness Centrality", Ulrik Brandes, Journal of Mathematical Sociology, 2001, and "Centrality Estimation in Large Networks", Urlik Brandes and Christian Pich, 2006 for more details.

Definition at line 374 of file centr.h.

374  {
375  if (DoNodeCent) { NodeBtwH.Clr(); }
376  if (DoEdgeCent) { EdgeBtwH.Clr(); }
377  const int nodes = Graph->GetNodes();
378  TIntS S(nodes);
379  TIntQ Q(nodes);
380  TIntIntVH P(nodes); // one vector for every node
381  TIntFltH delta(nodes);
382  TIntH sigma(nodes), d(nodes);
383  // init
384  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
385  if (DoNodeCent) {
386  NodeBtwH.AddDat(NI.GetId(), 0); }
387  if (DoEdgeCent) {
388  for (int e = 0; e < NI.GetOutDeg(); e++) {
389  if (Graph->HasFlag(gfDirected) && IsDir) {
390  // add all outgoing edges for directed graphs
391  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
392  } else {
393  // add each edge only once in undirected graphs
394  if (NI.GetId() < NI.GetOutNId(e)) {
395  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
396  }
397  }
398  }
399  // add incoming edges in directed graphs that were not added yet
400  if (Graph->HasFlag(gfDirected) && !IsDir) {
401  for (int e = 0; e < NI.GetInDeg(); e++) {
402  if (NI.GetId() < NI.GetInNId(e) &&
403  !Graph->IsEdge(NI.GetId(), NI.GetInNId(e))) {
404  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetInNId(e)), 0);
405  }
406  }
407  }
408  }
409  sigma.AddDat(NI.GetId(), 0);
410  d.AddDat(NI.GetId(), -1);
411  P.AddDat(NI.GetId(), TIntV());
412  delta.AddDat(NI.GetId(), 0);
413  }
414  // calc betweeness
415  for (int k=0; k < BtwNIdV.Len(); k++) {
416  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
417  // reset
418  for (int i = 0; i < sigma.Len(); i++) {
419  sigma[i]=0; d[i]=-1; delta[i]=0; P[i].Clr(false);
420  }
421  S.Clr(false);
422  Q.Clr(false);
423  sigma.AddDat(NI.GetId(), 1);
424  d.AddDat(NI.GetId(), 0);
425  Q.Push(NI.GetId());
426  while (! Q.Empty()) {
427  const int v = Q.Top(); Q.Pop();
428  const typename PGraph::TObj::TNodeI NI2 = Graph->GetNI(v);
429  S.Push(v);
430  const int VDat = d.GetDat(v);
431  // iterate over all outgoing edges
432  for (int e = 0; e < NI2.GetOutDeg(); e++) {
433  const int w = NI2.GetOutNId(e);
434  if (d.GetDat(w) < 0) { // find w for the first time
435  Q.Push(w);
436  d.AddDat(w, VDat+1);
437  }
438  //shortest path to w via v ?
439  if (d.GetDat(w) == VDat+1) {
440  sigma.AddDat(w) += sigma.GetDat(v);
441  P.GetDat(w).Add(v);
442  }
443  }
444  // if ignoring direction in directed networks, iterate over incoming edges
445  if (Graph->HasFlag(gfDirected) && !IsDir) {
446  for (int e = 0; e < NI2.GetInDeg(); e++) {
447  const int w = NI2.GetInNId(e);
448  // skip neighbors that are also outgoing
449  if (Graph->IsEdge(NI2.GetId(), w)) {
450  continue;
451  }
452  if (d.GetDat(w) < 0) { // find w for the first time
453  Q.Push(w);
454  d.AddDat(w, VDat+1);
455  }
456  //shortest path to w via v ?
457  if (d.GetDat(w) == VDat+1) {
458  sigma.AddDat(w) += sigma.GetDat(v);
459  P.GetDat(w).Add(v);
460  }
461  }
462  }
463  }
464  while (! S.Empty()) {
465  const int w = S.Top();
466  const double SigmaW = sigma.GetDat(w);
467  const double DeltaW = delta.GetDat(w);
468  const TIntV NIdV = P.GetDat(w);
469  S.Pop();
470  for (int i = 0; i < NIdV.Len(); i++) {
471  const int NId = NIdV[i];
472  const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
473  delta.AddDat(NId) += c;
474  if (DoEdgeCent) {
475  if (Graph->HasFlag(gfDirected) && IsDir) {
476  EdgeBtwH.AddDat(TIntPr(NId, w)) += c;
477  } else {
478  EdgeBtwH.AddDat(TIntPr(TMath::Mn(NId, w), TMath::Mx(NId, w))) += c;
479  }
480  }
481  }
482  if (DoNodeCent && w != NI.GetId()) {
483  NodeBtwH.AddDat(w) += delta.GetDat(w)/2.0; }
484  }
485  }
486 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:838
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
TVec< TInt > TIntV
Definition: ds.h:1594
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
double TSnap::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).

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 424 of file bfsdfs.h.

424  {
425  int FullDiam;
426  double EffDiam;
427  GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam);
428  return EffDiam;
429 }
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...
Definition: bfsdfs.h:472
template<class PGraph >
double TSnap::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).

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 432 of file bfsdfs.h.

432  {
433  double AvgDiam;
434  EffDiam = -1; FullDiam = -1;
435  return GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam, AvgDiam);
436 }
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...
Definition: bfsdfs.h:472
template<class PGraph >
double TSnap::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).

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 439 of file bfsdfs.h.

439  {
440  EffDiam = -1; FullDiam = -1; AvgSPL = -1;
441  TIntFltH DistToCntH;
442  TBreathFS<PGraph> BFS(Graph);
443  // shotest paths
444  TIntV NodeIdV;
445  Graph->GetNIdV(NodeIdV); NodeIdV.Shuffle(TInt::Rnd);
446  for (int tries = 0; tries < TMath::Mn(NTestNodes, Graph->GetNodes()); tries++) {
447  const int NId = NodeIdV[tries];
448  BFS.DoBfs(NId, true, ! IsDir, -1, TInt::Mx);
449  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
450  DistToCntH.AddDat(BFS.NIdDistH[i]) += 1; }
451  }
452  TIntFltKdV DistNbrsPdfV;
453  double SumPathL=0, PathCnt=0;
454  for (int i = 0; i < DistToCntH.Len(); i++) {
455  DistNbrsPdfV.Add(TIntFltKd(DistToCntH.GetKey(i), DistToCntH[i]));
456  SumPathL += DistToCntH.GetKey(i) * DistToCntH[i];
457  PathCnt += DistToCntH[i];
458  }
459  DistNbrsPdfV.Sort();
460  EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9); // effective diameter (90-th percentile)
461  FullDiam = DistNbrsPdfV.Last().Key; // approximate full diameter (max shortest path length over the sampled nodes)
462  AvgSPL = SumPathL/PathCnt; // average shortest path length
463  return EffDiam;
464 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static const int Mx
Definition: dt.h:1142
TKeyDat< TInt, TFlt > TIntFltKd
Definition: ds.h:381
static TRnd Rnd
Definition: dt.h:1146
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
double CalcEffDiamPdf(const TIntFltKdV &DistNbrsPdfV, const double &Percentile)
Helper function for computing a given Percentile of a (unnormalized) probability distribution functio...
Definition: anf.cpp:29
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
template<class PGraph >
double TSnap::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.

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 472 of file bfsdfs.h.

472  {
473  EffDiam = -1;
474  FullDiam = -1;
475 
476  TIntFltH DistToCntH;
477  TBreathFS<PGraph> BFS(Graph);
478  // shotest paths
479  TIntV NodeIdV(SubGraphNIdV); NodeIdV.Shuffle(TInt::Rnd);
480  TInt Dist;
481  for (int tries = 0; tries < TMath::Mn(NTestNodes, SubGraphNIdV.Len()); tries++) {
482  const int NId = NodeIdV[tries];
483  BFS.DoBfs(NId, true, ! IsDir, -1, TInt::Mx);
484  for (int i = 0; i < SubGraphNIdV.Len(); i++) {
485  if (BFS.NIdDistH.IsKeyGetDat(SubGraphNIdV[i], Dist)) {
486  DistToCntH.AddDat(Dist) += 1;
487  }
488  }
489  }
490  TIntFltKdV DistNbrsPdfV;
491  for (int i = 0; i < DistToCntH.Len(); i++) {
492  DistNbrsPdfV.Add(TIntFltKd(DistToCntH.GetKey(i), DistToCntH[i]));
493  }
494  DistNbrsPdfV.Sort();
495  EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9); // effective diameter (90-th percentile)
496  FullDiam = DistNbrsPdfV.Last().Key; // approximate full diameter (max shortest path length over the sampled nodes)
497  return EffDiam; // average shortest path length
498 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static const int Mx
Definition: dt.h:1142
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TKeyDat< TInt, TFlt > TIntFltKd
Definition: ds.h:381
static TRnd Rnd
Definition: dt.h:1146
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
double CalcEffDiamPdf(const TIntFltKdV &DistNbrsPdfV, const double &Percentile)
Helper function for computing a given Percentile of a (unnormalized) probability distribution functio...
Definition: anf.cpp:29
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
Definition: dt.h:1137
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
template<class PGraph >
double TSnap::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).

This function is a duplicate. It is required by Snap.py.

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 467 of file bfsdfs.h.

467  {
468  return GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam, AvgSPL);
469 }
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...
Definition: bfsdfs.h:472
template<class PGraph >
int TSnap::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).

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 416 of file bfsdfs.h.

416  {
417  int FullDiam;
418  double EffDiam;
419  GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam);
420  return FullDiam;
421 }
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...
Definition: bfsdfs.h:472
template<class PGraph >
PNGraph TSnap::GetBfsTree ( const PGraph &  Graph,
const int &  StartNId,
const bool &  FollowOut,
const bool &  FollowIn 
)

Returns a directed Breadth-First-Search tree rooted at StartNId.

Returns a directed graph where a parent points to its child node. Tree is created by following in-links (parameter FollowIn = true) and/or out-links (parameter FollowOut = true).

Definition at line 332 of file bfsdfs.h.

332  {
333  TBreathFS<PGraph> BFS(Graph);
334  BFS.DoBfs(StartNId, FollowOut, FollowIn, -1, TInt::Mx);
335  PNGraph Tree = TNGraph::New();
336  BFS.NIdDistH.SortByDat();
337  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
338  const int NId = BFS.NIdDistH.GetKey(i);
339  const int Dist = BFS.NIdDistH[i];
340  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
341  if (!Tree->IsNode(NId)) {
342  Tree->AddNode(NId);
343  }
344  if (FollowOut) {
345  for (int e = 0; e < NI.GetInDeg(); e++) {
346  const int Prev = NI.GetInNId(e);
347  if (Tree->IsNode(Prev) && BFS.NIdDistH.GetDat(Prev)==Dist-1) {
348  Tree->AddEdge(Prev, NId); }
349  }
350  }
351  if (FollowIn) {
352  for (int e = 0; e < NI.GetOutDeg(); e++) {
353  const int Prev = NI.GetOutNId(e);
354  if (Tree->IsNode(Prev) && BFS.NIdDistH.GetDat(Prev)==Dist-1) {
355  Tree->AddEdge(Prev, NId); }
356  }
357  }
358  }
359  return Tree;
360 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
static const int Mx
Definition: dt.h:1142
Definition: bd.h:196
void TSnap::GetBiCon ( const PUNGraph Graph,
TCnComV BiCnComV 
)

Returns all bi-connected components of a Graph.

Parameters
BiCnComVis a vector of bi-connected components. Each component is defined by the IDs of its member nodes.

Definition at line 42 of file cncom.cpp.

42  {
43  TBiConVisitor Visitor(Graph->GetNodes());
44  TCnCom::GetDfsVisitor(Graph, Visitor);
45  BiCnComV = Visitor.CnComV;
46 }
static void GetDfsVisitor(const PGraph &Graph, TVisitor &Visitor)
Definition: cncom.h:124
Biconnected componetns Depth-First-Search visitor class.
Definition: cncom.h:195
void TSnap::GetBiConSzCnt ( const PUNGraph Graph,
TIntPrV SzCntV 
)

Returns a distribution of bi-connected component sizes.

Parameters
SzCntVreturns a set of pairs (number of nodes in the bi-component, number of such components)

Definition at line 31 of file cncom.cpp.

31  {
32  TCnComV BiCnComV;
33  GetBiCon(Graph, BiCnComV);
34  TIntH SzCntH;
35  for (int c =0; c < BiCnComV.Len(); c++) {
36  SzCntH.AddDat(BiCnComV[c].Len()) += 1;
37  }
38  SzCntH.GetKeyDatPrV(SzCntV);
39  SzCntV.Sort();
40 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void GetBiCon(const PUNGraph &Graph, TCnComV &BiCnComV)
Returns all bi-connected components of a Graph.
Definition: cncom.cpp:42
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:500
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
double TSnap::GetClosenessCentr ( const PGraph &  Graph,
const int &  NId,
const bool &  Normalized = true,
const bool &  IsDir = false 
)

Returns Closeness centrality of a given node NId. Closeness centrality of a node is defined as 1/FarnessCentrality.

Definition at line 161 of file centr.h.

161  {
162  const double Farness = GetFarnessCentr<PGraph> (Graph, NId, Normalized, IsDir);
163  if (Farness != 0.0) { return 1.0/Farness; }
164  else { return 0.0; }
165  return 0.0;
166 }
template<class PGraph >
double TSnap::GetClosenessCentrMP ( const PGraph &  Graph,
const int &  NId,
const bool &  Normalized = true,
const bool &  IsDir = false 
)

Definition at line 169 of file centr.h.

169  {
170  const double Farness = GetFarnessCentrMP<PGraph> (Graph, NId, Normalized, IsDir);
171  if (Farness != 0.0) { return 1.0/Farness; }
172  else { return 0.0; }
173  return 0.0;
174 }
template<class PGraph >
double TSnap::GetClustCf ( const PGraph &  Graph,
int  SampleNodes = -1 
)

Computes the average clustering coefficient as defined in Watts and Strogatz, Collective dynamics of 'small-world' networks.

Considers the graph as undirected.

Definition at line 137 of file triad.h.

137  {
138  TIntTrV NIdCOTriadV;
139  GetTriads(Graph, NIdCOTriadV, SampleNodes);
140  if (NIdCOTriadV.Empty()) { return 0.0; }
141  double SumCcf = 0.0;
142  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
143  const double OpenCnt = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
144  if (OpenCnt > 0) {
145  SumCcf += NIdCOTriadV[i].Val2() / OpenCnt; }
146  }
147  IAssert(SumCcf>=0);
148  return SumCcf / double(NIdCOTriadV.Len());
149 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void GetTriads(const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
Computes the number of open and close triads for every node of the network.
Definition: triad.h:318
template<class PGraph >
double TSnap::GetClustCf ( const PGraph &  Graph,
TFltPrV DegToCCfV,
int  SampleNodes = -1 
)

Computes the distribution of average clustering coefficient.

Considers the graph as undirected.

Parameters
DegToCCfVVector of pairs (degree, avg. clustering coefficient of nodes of that degree).
SampleNodesIf !=-1 then compute clustering coefficient only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 151 of file triad.h.

151  {
152  TIntTrV NIdCOTriadV;
153  GetTriads(Graph, NIdCOTriadV, SampleNodes);
154  if (NIdCOTriadV.Empty()) {
155  DegToCCfV.Clr(false);
156  return 0.0;
157  }
158  THash<TInt, TFltPr> DegSumCnt;
159  double SumCcf = 0.0;
160  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
161  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
162  const double Ccf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
163  TFltPr& SumCnt = DegSumCnt.AddDat(Graph->GetNI(NIdCOTriadV[i].Val1).GetDeg());
164  SumCnt.Val1 += Ccf;
165  SumCnt.Val2 += 1;
166  SumCcf += Ccf;
167  }
168  // get average clustering coefficient for each degree
169  DegToCCfV.Gen(DegSumCnt.Len(), 0);
170  for (int d = 0; d < DegSumCnt.Len(); d++) {
171  DegToCCfV.Add(TFltPr(DegSumCnt.GetKey(d).Val, double(DegSumCnt[d].Val1()/DegSumCnt[d].Val2())));
172  }
173  DegToCCfV.Sort();
174  return SumCcf / double(NIdCOTriadV.Len());
175 }
int Val
Definition: dt.h:1139
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void GetTriads(const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
Computes the number of open and close triads for every node of the network.
Definition: triad.h:318
Definition: ds.h:32
Definition: hash.h:97
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
template<class PGraph >
double TSnap::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.

Considers the graph as undirected.

Parameters
DegToCCfVVector of pairs (degree, avg. clustering coefficient of nodes of that degree).
ClosedTriadsOn return contains the number of closed triads.
OpenTriadsOn return contains the number of open triads.
SampleNodesIf !=-1 then compute clustering coefficient only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 178 of file triad.h.

178  {
179  TIntTrV NIdCOTriadV;
180  GetTriads(Graph, NIdCOTriadV, SampleNodes);
181  if (NIdCOTriadV.Empty()) {
182  DegToCCfV.Clr(false);
183  ClosedTriads = 0;
184  OpenTriads = 0;
185  return 0.0;
186  }
187  THash<TInt, TFltPr> DegSumCnt;
188  double SumCcf = 0.0;
189  int64 closedTriads = 0;
190  int64 openTriads = 0;
191  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
192  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
193  const double Ccf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
194  closedTriads += NIdCOTriadV[i].Val2;
195  openTriads += NIdCOTriadV[i].Val3;
196  TFltPr& SumCnt = DegSumCnt.AddDat(Graph->GetNI(NIdCOTriadV[i].Val1).GetDeg());
197  SumCnt.Val1 += Ccf;
198  SumCnt.Val2 += 1;
199  SumCcf += Ccf;
200  }
201  // get average clustering coefficient for each degree
202  DegToCCfV.Gen(DegSumCnt.Len(), 0);
203  for (int d = 0; d < DegSumCnt.Len(); d++) {
204  DegToCCfV.Add(TFltPr(DegSumCnt.GetKey(d).Val, DegSumCnt[d].Val1()/DegSumCnt[d].Val2()));
205  }
206  //if(closedTriads/3 > (uint64) TInt::Mx) { WarnNotify(TStr::Fmt("[%s line %d] %g closed triads.\n", __FILE__, __LINE__, float(closedTriads/3)).CStr()); }
207  //if(openTriads > (uint64) TInt::Mx) { WarnNotify(TStr::Fmt("[%s line %d] %g open triads.\n", __FILE__, __LINE__, float(openTriads/3)).CStr()); }
208  ClosedTriads = closedTriads/int64(3); // each triad is counted 3 times
209  OpenTriads = openTriads;
210  DegToCCfV.Sort();
211  return SumCcf / double(NIdCOTriadV.Len());
212 }
int Val
Definition: dt.h:1139
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void GetTriads(const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
Computes the number of open and close triads for every node of the network.
Definition: triad.h:318
Definition: ds.h:32
long long int64
Definition: bd.h:27
Definition: hash.h:97
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
template<class PGraph >
double TSnap::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.

Considers the graph as undirected. This function is a duplicate. It is required by Snap.py.

Parameters
DegToCCfVVector of pairs (degree, avg. clustering coefficient of nodes of that degree).
ClosedTriadsOn return contains the number of closed triads.
OpenTriadsOn return contains the number of open triads.
SampleNodesIf !=-1 then compute clustering coefficient only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 215 of file triad.h.

215  {
216  return GetClustCf(Graph, DegToCCfV, ClosedTriads, OpenTriads, SampleNodes);
217 }
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 ...
Definition: triad.h:178
template<class PGraph >
int TSnap::GetCmnNbrs ( const PGraph &  Graph,
const int &  NId1,
const int &  NId2 
)

Returns a number of shared neighbors between a pair of nodes NId1 and NId2.

Definition at line 708 of file triad.h.

708  {
709  TIntV NbrV;
710  return GetCmnNbrs(Graph, NId1, NId2, NbrV);
711 }
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.
Definition: triad.h:715
template<class PGraph >
int TSnap::GetCmnNbrs ( const PGraph &  Graph,
const int &  NId1,
const int &  NId2,
TIntV NbrV 
)

Returns the shared neighbors between a pair of nodes NId1 and NId2.

Definition at line 715 of file triad.h.

715  {
716  if (! Graph->IsNode(NId1) || ! Graph->IsNode(NId2)) { NbrV.Clr(false); return 0; }
717  typename PGraph::TObj::TNodeI NI1 = Graph->GetNI(NId1);
718  typename PGraph::TObj::TNodeI NI2 = Graph->GetNI(NId2);
719  NbrV.Clr(false);
720  NbrV.Reserve(TMath::Mn(NI1.GetDeg(), NI2.GetDeg()));
721  TIntSet NSet1(NI1.GetDeg()), NSet2(NI2.GetDeg());
722  for (int i = 0; i < NI1.GetDeg(); i++) {
723  const int nid = NI1.GetNbrNId(i);
724  if (nid!=NId1 && nid!=NId2) {
725  NSet1.AddKey(nid); }
726  }
727  for (int i = 0; i < NI2.GetDeg(); i++) {
728  const int nid = NI2.GetNbrNId(i);
729  if (NSet1.IsKey(nid)) {
730  NSet2.AddKey(nid);
731  }
732  }
733  NSet2.GetKeyV(NbrV);
734  return NbrV.Len();
735 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
int AddKey(const TKey &Key)
Definition: shash.h:1254
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
template<>
int TSnap::GetCmnNbrs< PUNGraph > ( const PUNGraph Graph,
const int &  NId1,
const int &  NId2,
TIntV NbrV 
)
inline

Definition at line 738 of file triad.h.

738  {
739  if (! Graph->IsNode(NId1) || ! Graph->IsNode(NId2)) { NbrV.Clr(false); return 0; }
740  const TUNGraph::TNodeI NI1 = Graph->GetNI(NId1);
741  const TUNGraph::TNodeI NI2 = Graph->GetNI(NId2);
742  int i=0, j=0;
743  NbrV.Clr(false);
744  NbrV.Reserve(TMath::Mn(NI1.GetDeg(), NI2.GetDeg()));
745  while (i < NI1.GetDeg() && j < NI2.GetDeg()) {
746  const int nid = NI1.GetNbrNId(i);
747  while (j < NI2.GetDeg() && NI2.GetNbrNId(j) < nid) { j++; }
748  if (j < NI2.GetDeg() && nid==NI2.GetNbrNId(j) && nid!=NId1 && nid!=NId2) {
749  IAssert(NbrV.Empty() || NbrV.Last() < nid);
750  NbrV.Add(nid);
751  j++;
752  }
753  i++;
754  }
755  return NbrV.Len();
756 }
#define IAssert(Cond)
Definition: bd.h:262
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
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
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int TSnap::GetCommon ( TIntV A,
TIntV B 
)

Returns the number of common elements in two sorted TInt vectors.

Definition at line 59 of file triad.cpp.

59  {
60  int i, j;
61  int ret = 0;
62  int alen, blen;
63  int d;
64  TInt ai;
65 
66  alen = A.Len();
67  blen = B.Len();
68  i = 0;
69  j = 0;
70  if (i >= alen || j >= blen) {
71  return ret;
72  }
73 
74  while (1) {
75  d = A[i] - B[j];
76  if (d < 0) {
77  i++;
78  if (i >= alen) {
79  break;
80  }
81  } else if (d > 0) {
82  j++;
83  if (j >= blen) {
84  break;
85  }
86  } else {
87  ret++;
88  i++;
89  if (i >= alen) {
90  break;
91  }
92  j++;
93  if (j >= blen) {
94  break;
95  }
96  }
97  }
98  return ret;
99 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: dt.h:1137
template<class PGraph >
void TSnap::GetDegCnt ( const PGraph &  Graph,
TIntPrV DegToCntV 
)

Returns a degree histogram: a set of pairs (degree, number of nodes of such degree)

Definition at line 223 of file alg.h.

223  {
224  TIntH DegToCntH;
225  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
226  DegToCntH.AddDat(NI.GetDeg())++; }
227  DegToCntV.Gen(DegToCntH.Len(), 0);
228  for (int i = 0; i < DegToCntH.Len(); i++) {
229  DegToCntV.Add(TIntPr(DegToCntH.GetKey(i), DegToCntH[i])); }
230  DegToCntV.Sort();
231 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
template<class PGraph >
void TSnap::GetDegCnt ( const PGraph &  Graph,
TFltPrV DegToCntV 
)

Returns a degree histogram: a set of pairs (degree, number of nodes of such degree)

Definition at line 234 of file alg.h.

234  {
235  TIntH DegToCntH;
236  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
237  DegToCntH.AddDat(NI.GetDeg())++; }
238  DegToCntV.Gen(DegToCntH.Len(), 0);
239  for (int i = 0; i < DegToCntH.Len(); i++) {
240  DegToCntV.Add(TFltPr(DegToCntH.GetKey(i).Val, DegToCntH[i].Val)); }
241  DegToCntV.Sort();
242 }
int Val
Definition: dt.h:1139
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
double TSnap::GetDegreeCentr ( const PUNGraph Graph,
const int &  NId 
)

Returns Degree centrality of a given node NId. Degree centrality if a node is defined as its degree/(N-1), where N is the number of nodes in the network.

Definition at line 5 of file centr.cpp.

5  {
6  if (Graph->GetNodes() > 1) {
7  return double(Graph->GetNI(NId).GetDeg())/double(Graph->GetNodes()-1); }
8  else { return 0.0; }
9 }
template<class PGraph >
void TSnap::GetDegSeqV ( const PGraph &  Graph,
TIntV DegV 
)

Returns a degree sequence vector.

Definition at line 245 of file alg.h.

245  {
246  DegV.Gen(Graph->GetNodes(), 0);
247  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
248  DegV.Add(NI.GetDeg());
249  }
250 }
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::GetDegSeqV ( const PGraph &  Graph,
TIntV InDegV,
TIntV OutDegV 
)

Returns an in- and out-degree sequence vectors.

Definition at line 253 of file alg.h.

253  {
254  InDegV.Gen(Graph->GetNodes(), 0);
255  OutDegV.Gen(Graph->GetNodes(), 0);
256  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
257  InDegV.Add(NI.GetInDeg());
258  OutDegV.Add(NI.GetOutDeg());
259  }
260 }
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph , class TEdgeDat >
PGraph TSnap::GetEDatSubGraph ( const PGraph &  Graph,
const TEdgeDat &  EDat,
const int &  Cmp 
)

Returns a subgraph of graph Graph with edges where edge data matches the parameters.

EDat provides the value for edge data matching. Cmp determines the comparison function. Edges whose edge data matches EDat are included in the resulting subgraph as well as all the nodes which connect to at least one edge in the subgraph. Node IDs are preserved. Nodes in the resulting subgraph have the same node IDs as nodes in Graph.

Values of Cmp can be -1, 0, or +1. If Cmp is -1, edges with edge data less than EDat are included in the resulting subgraph. If Cmp equals 0, the values of edge data and EDat have to match. If Cmp is +1, edge data has to be greater than EDat.

Definition at line 286 of file subgraph.h.

286  {
287  CAssert(HasGraphFlag(typename PGraph::TObj, gfEdgeDat));
288  PGraph NewGraphPt = PGraph::TObj::New();
289  typename PGraph::TObj& NewGraph = *NewGraphPt;
290  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
291  if ((Cmp==1 && EI()>EDat) || (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat)) {
292  if (! NewGraph.IsNode(EI.GetSrcNId())) {
293  NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
294  }
295  if (! NewGraph.IsNode(EI.GetDstNId())) {
296  NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
297  }
298  NewGraph.AddEdge(EI);
299  }
300  }
301  return NewGraphPt;
302 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
network with data on edges
Definition: gbase.h:16
#define CAssert(Cond)
Definition: bd.h:302
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
Definition: bd.h:426
template<class PGraph , class TEdgeDat >
PGraph TSnap::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.

The resulting subgraph contains all the nodes from Graph, which have node IDs in the NIdV vector and edges with both nodes in NIdV and whose edge data matches the parameters. Node IDs are preserved. Nodes in the resulting subgraph have the same node IDs as nodes in Graph.

EDat provides the value for edge data matching. Cmp determines the comparison function. Values of Cmp can be -1, 0, or +1. If Cmp is -1, edges with edge data less than EDat are included in the resulting subgraph. If Cmp equals 0, the values of edge data and EDat have to match. If Cmp is +1, edge data has to be greater than EDat.

Definition at line 306 of file subgraph.h.

306  {
307  CAssert(HasGraphFlag(typename PGraph::TObj, gfEdgeDat));
308  PGraph NewGraphPt = PGraph::TObj::New();
309  typename PGraph::TObj& NewGraph = *NewGraphPt;
310  NewGraph.Reserve(NIdV.Len(), -1);
311  for (int n = 0; n < NIdV.Len(); n++) {
312  NewGraph.AddNode(Graph->GetNI(NIdV[n]));
313  }
314  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
315  if (NewGraph.IsNode(EI.GetSrcNId()) && NewGraph.IsNode(EI.GetDstNId()) &&
316  ((Cmp==1 && EI()>EDat)|| (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat))) {
317  NewGraph.AddEdge(EI); }
318  }
319  NewGraph.Defrag();
320  return NewGraphPt;
321 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
network with data on edges
Definition: gbase.h:16
#define CAssert(Cond)
Definition: bd.h:302
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
Definition: bd.h:426
void TSnap::GetEdgeBridges ( const PUNGraph Graph,
TIntPrV EdgeV 
)

Returns bridge edges of a Graph.

Edge is a bridge if, when removed, increases the number of connected components. See http://en.wikipedia.org/wiki/Bridge_(graph_theory)

Definition at line 55 of file cncom.cpp.

55  {
56  TCnComV BiCnComV;
57  GetBiCon(Graph, BiCnComV);
58  TIntPrSet EdgeSet;
59  for (int c = 0; c < BiCnComV.Len(); c++) {
60  const TIntV& NIdV = BiCnComV[c].NIdV;
61  if (NIdV.Len() == 2) {
62  EdgeSet.AddKey(TIntPr(TMath::Mn(NIdV[0], NIdV[1]), TMath::Mx(NIdV[0], NIdV[1])));
63  }
64  }
65  EdgeSet.GetKeyV(EdgeV);
66 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetKeyV(TVec< TKey > &KeyV) const
Definition: shash.h:1347
int AddKey(const TKey &Key)
Definition: shash.h:1254
void GetBiCon(const PUNGraph &Graph, TCnComV &BiCnComV)
Returns all bi-connected components of a Graph.
Definition: cncom.cpp:42
template<typename PGraph >
void TSnap::GetEdgesInOut ( const PGraph &  Graph,
const TIntV NIdV,
int &  EdgesInX,
int &  EdgesOutX 
)

Returns the number of edges between the nodes NIdV and the edges pointing outside the set NIdV.

Parameters
EdgesInXNumber of edges between the nodes NIdV.
EdgesOutXNumber of edges between the nodes in NIdV and the rest of the graph.

Definition at line 76 of file cmty.h.

76  {
77  EdgesIn = 0;
78  EdgesOut = 0;
79  TIntSet NIdSet(NIdV.Len());
80  for (int e = 0; e < NIdV.Len(); e++) {
81  NIdSet.AddKey(NIdV[e]);
82  }
83  for (int e = 0; e < NIdV.Len(); e++) {
84  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[e]);
85  for (int i = 0; i < NI.GetOutDeg(); i++) {
86  if (NIdSet.IsKey(NI.GetOutNId(i))) { EdgesIn += 1; }
87  else { EdgesOut += 1; }
88  }
89  }
90  EdgesIn /= 2;
91 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int AddKey(const TKey &Key)
Definition: shash.h:1254
PUNGraph TSnap::GetEgonet ( const PUNGraph Graph,
const int  CtrNId,
int &  ArndEdges 
)

Returns the egonet of node CtrNId as center in undirected graph Graph. And returns number of edges around the egonet.

Definition at line 82 of file subgraph.cpp.

82  {
83  PUNGraph NewGraphPt = TUNGraph::New();
84  TUNGraph& NewGraph = *NewGraphPt;
85  NewGraph.AddNode(CtrNId);
86  const TUNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
87  for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
88  NewGraph.AddNode(CtrNode.GetInNId(i));
89  }
90  ArndEdges = 0;
91  for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
92  int NbrNId = CtrNode.GetInNId(i);
93  const TUNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
94  for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
95  int NbrNbrNId = NbrNode.GetInNId(j);
96  if (NewGraph.IsNode(NbrNbrNId)) {
97  if (!NewGraph.IsEdge(NbrNId, NbrNbrNId)) {
98  NewGraph.AddEdge(NbrNId, NbrNbrNId);
99  }
100  } else {
101  ArndEdges++;
102  }
103  }
104  }
105  return NewGraphPt;
106 }
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
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
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:92
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:235
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
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:101
PNGraph TSnap::GetEgonet ( const PNGraph Graph,
const int  CtrNId,
int &  InEdges,
int &  OutEdges 
)

Returns the egonet of node CtrNId as center in directed graph Graph. And returns number of edges go in and out the egonet.

Definition at line 108 of file subgraph.cpp.

108  {
109  PNGraph NewGraphPt = TNGraph::New();
110  TNGraph& NewGraph = *NewGraphPt;
111  NewGraph.AddNode(CtrNId);
112  const TNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
113  for (int i = 0; i < CtrNode.GetDeg(); ++i) {
114  if (!NewGraph.IsNode(CtrNode.GetNbrNId(i))) {
115  NewGraph.AddNode(CtrNode.GetNbrNId(i));
116  }
117  }
118  InEdges = 0;
119  OutEdges = 0;
120  for (int i = 0; i < CtrNode.GetDeg(); ++i) {
121  int NbrNId = CtrNode.GetNbrNId(i);
122  const TNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
123  for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
124  int NbrNbrNId = NbrNode.GetInNId(j);
125  if (NewGraph.IsNode(NbrNbrNId)) {
126  NewGraph.AddEdge(NbrNbrNId, NbrNId);
127  } else {
128  InEdges++;
129  }
130  }
131  for (int j = 0; j < NbrNode.GetOutDeg(); ++j) {
132  int NbrNbrNId = NbrNode.GetOutNId(j);
133  if (NewGraph.IsNode(NbrNbrNId)) {
134  NewGraph.AddEdge(NbrNId, NbrNbrNId);
135  } else {
136  OutEdges++;
137  }
138  }
139  }
140  return NewGraphPt;
141 }
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:420
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
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
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:546
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:402
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
Definition: bd.h:196
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:404
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:412
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:416
PNEANet TSnap::GetEgonetAttr ( const PNEANet Graph,
const int  CtrNId,
const int  Radius 
)

Returns the complete egonet of at given radius and copies node and edge attributes.

Definition at line 254 of file subgraph.cpp.

254  {
255  PNEANet NewGraphPt = PNEANet::New();
256  TNEANet &NewGraph = *NewGraphPt;
257  TSnapQueue<int> Queue1;
258  TSnapQueue<int> Queue2;
259  TSnapQueue<int> tempSwapQueue;
260  AddNodeWithAttributes(Graph, NewGraphPt, CtrNId);
261  Queue1.Clr(false);
262  Queue1.Push(CtrNId);
263  for (int r = 0; r < Radius; ++r) {
264  while (!Queue1.Empty()) {
265  const int NId = Queue1.Top();
266  Queue1.Pop();
267  const TNEANet::TNodeI &Node = Graph->GetNI(NId);
268  for (int i = 0; i < Node.GetInDeg(); ++i) {
269  const int InNId = Node.GetInNId(i);
270  if (!NewGraph.IsNode(InNId)) {
271  AddNodeWithAttributes(Graph, NewGraphPt, InNId);
272  Queue2.Push(InNId);
273  }
274  const int InEId = Node.GetInEId(i);
275  if (!NewGraph.IsEdge(InEId)) {
276  AddEdgeWithAttributes(Graph, NewGraphPt, InEId);
277  }
278  }
279  for (int i = 0; i < Node.GetOutDeg(); ++i) {
280  const int OutNId = Node.GetOutNId(i);
281  if (!NewGraph.IsNode(OutNId)) {
282  AddNodeWithAttributes(Graph, NewGraphPt, OutNId);
283  Queue2.Push(OutNId);
284  }
285  const int OutEId = Node.GetOutEId(i);
286  if (!NewGraph.IsEdge(OutEId)) {
287  AddEdgeWithAttributes(Graph, NewGraphPt, OutEId);
288  }
289  }
290  for (int i = 0; i < Node.GetInDeg(); ++i) {
291  int InNId = Node.GetInNId(i);
292  const TNEANet::TNodeI &InNode = Graph->GetNI(InNId);
293  for (int j = 0; j < InNode.GetInDeg(); ++j) {
294  int NbrInNId = InNode.GetInNId(j);
295  if (NewGraph.IsNode(NbrInNId)) {
296  const int NbrInEId = InNode.GetInEId(j);
297  if (!NewGraph.IsEdge(NbrInEId)) {
298  AddEdgeWithAttributes(Graph, NewGraphPt, NbrInEId);
299  }
300  }
301  }
302  for (int j = 0; j < InNode.GetOutDeg(); ++j) {
303  int NbrOutNId = InNode.GetOutNId(j);
304  if (NewGraph.IsNode(NbrOutNId)) {
305  const int NbrOutEId = InNode.GetOutEId(j);
306  if (!NewGraph.IsEdge(NbrOutEId)) {
307  AddEdgeWithAttributes(Graph, NewGraphPt, NbrOutEId);
308  }
309  }
310  }
311  }
312  for (int i = 0; i < Node.GetOutDeg(); ++i) {
313  int OutNId = Node.GetOutNId(i);
314  const TNEANet::TNodeI &OutNode = Graph->GetNI(OutNId);
315  for (int j = 0; j < OutNode.GetInDeg(); ++j) {
316  int NbrInNId = OutNode.GetInNId(j);
317  if (NewGraph.IsNode(NbrInNId)) {
318  const int NbrInEId = OutNode.GetInEId(j);
319  if (!NewGraph.IsEdge(NbrInEId)) {
320  AddEdgeWithAttributes(Graph, NewGraphPt, NbrInEId);
321  }
322  }
323  }
324  for (int j = 0; j < OutNode.GetOutDeg(); ++j) {
325  int NbrOutNId = OutNode.GetOutNId(j);
326  if (NewGraph.IsNode(NbrOutNId)) {
327  const int NbrOutEId = OutNode.GetOutEId(j);
328  if (!NewGraph.IsEdge(NbrOutEId)) {
329  AddEdgeWithAttributes(Graph, NewGraphPt, NbrOutEId);
330  }
331  }
332  }
333  }
334  }
335  tempSwapQueue = Queue1;
336  Queue1 = Queue2;
337  Queue2 = tempSwapQueue;
338  }
339  return NewGraphPt;
340 }
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
static TPt New()
Definition: bd.h:479
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: network.h:2804
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: network.h:1833
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
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
void AddEdgeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId, const int NbrId)
Definition: subgraph.cpp:217
Directed multigraph with node edge attributes.
Definition: network.h:1741
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
void AddNodeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId)
Definition: subgraph.cpp:143
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
Definition: bd.h:196
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2385
template<class PGraph >
PGraph TSnap::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.

Definition at line 506 of file subgraph.h.

506  {
507  PGraph NewGraphPt = PGraph::TObj::New();
508  typename PGraph::TObj& NewGraph = *NewGraphPt;
509  TSnapQueue<int> Queue1;
510  TSnapQueue<int> Queue2;
511  TSnapQueue<int> tempSwapQueue;
512  NewGraph.AddNode(CtrNId);
513  Queue1.Clr(false);
514  Queue1.Push(CtrNId);
515  for (int r = 0; r < Radius; ++r) {
516  while (!Queue1.Empty()) {
517  const int NId = Queue1.Top();
518  Queue1.Pop();
519  const typename PGraph::TObj::TNodeI &Node = Graph->GetNI(NId);
520  for (int i = 0; i < Node.GetInDeg(); ++i) {
521  const int InNId = Node.GetInNId(i);
522  if (!NewGraph.IsNode(InNId)) {
523  NewGraph.AddNode(InNId);
524  Queue2.Push(InNId);
525  }
526  if (!NewGraph.IsEdge(InNId, NId)) {
527  NewGraph.AddEdge(InNId, NId);
528  }
529  }
530  for (int i = 0; i < Node.GetOutDeg(); ++i) {
531  const int OutNId = Node.GetOutNId(i);
532  if (!NewGraph.IsNode(OutNId)) {
533  NewGraph.AddNode(OutNId);
534  Queue2.Push(OutNId);
535  }
536  if (!NewGraph.IsEdge(NId, OutNId)) {
537  NewGraph.AddEdge(NId, OutNId);
538  }
539  }
540  for (int i = 0; i < Node.GetInDeg(); ++i) {
541  int InNId = Node.GetInNId(i);
542  const typename PGraph::TObj::TNodeI &InNode = Graph->GetNI(InNId);
543  for (int j = 0; j < InNode.GetInDeg(); ++j) {
544  int NbrInNId = InNode.GetInNId(j);
545  if (NewGraph.IsNode(NbrInNId)) {
546  if (!NewGraph.IsEdge(NbrInNId, InNId)) {
547  NewGraph.AddEdge(NbrInNId, InNId);
548  }
549  }
550  }
551  for (int j = 0; j < InNode.GetOutDeg(); ++j) {
552  int NbrOutNId = InNode.GetOutNId(j);
553  if (NewGraph.IsNode(NbrOutNId)) {
554  if (!NewGraph.IsEdge(InNId, NbrOutNId)) {
555  NewGraph.AddEdge(InNId, NbrOutNId);
556  }
557  }
558  }
559  }
560  for (int i = 0; i < Node.GetOutDeg(); ++i) {
561  int OutNId = Node.GetOutNId(i);
562  const typename PGraph::TObj::TNodeI &OutNode = Graph->GetNI(OutNId);
563  for (int j = 0; j < OutNode.GetInDeg(); ++j) {
564  int NbrInNId = OutNode.GetInNId(j);
565  if (NewGraph.IsNode(NbrInNId)) {
566  if (!NewGraph.IsEdge(NbrInNId, OutNId)) {
567  NewGraph.AddEdge(NbrInNId, OutNId);
568  }
569  }
570  }
571  for (int j = 0; j < OutNode.GetOutDeg(); ++j) {
572  int NbrOutNId = OutNode.GetOutNId(j);
573  if (NewGraph.IsNode(NbrOutNId)) {
574  if (!NewGraph.IsEdge(OutNId, NbrOutNId)) {
575  NewGraph.AddEdge(OutNId, NbrOutNId);
576  }
577  }
578  }
579  }
580  }
581  tempSwapQueue = Queue1;
582  Queue1 = Queue2;
583  Queue2 = tempSwapQueue;
584  }
585  return NewGraphPt;
586 }
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
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
void TSnap::GetEigenVectorCentr ( const PUNGraph Graph,
TIntFltH NIdEigenH,
const double &  Eps = 1e-4,
const int &  MaxIter = 100 
)

Computes Eigenvector Centrality of all nodes in the network Eigenvector Centrality of a node N is defined recursively as the average of centrality values of N's neighbors in the network.

Definition at line 11 of file centr.cpp.

11  {
12  const int NNodes = Graph->GetNodes();
13  NIdEigenH.Gen(NNodes);
14  // initialize vector values
15  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
16  NIdEigenH.AddDat(NI.GetId(), 1.0/NNodes);
17  IAssert(NI.GetId() == NIdEigenH.GetKey(NIdEigenH.Len()-1));
18  }
19  TFltV TmpV(NNodes);
20  for (int iter = 0; iter < MaxIter; iter++) {
21  int j = 0;
22  // add neighbor values
23  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
24  TmpV[j] = 0;
25  for (int e = 0; e < NI.GetOutDeg(); e++) {
26  TmpV[j] += NIdEigenH.GetDat(NI.GetOutNId(e)); }
27  }
28 
29  // normalize
30  double sum = 0;
31  for (int i = 0; i < TmpV.Len(); i++) {
32  sum += (TmpV[i]*TmpV[i]);
33  }
34  sum = sqrt(sum);
35  for (int i = 0; i < TmpV.Len(); i++) {
36  TmpV[i] /= sum;
37  }
38 
39  // compute difference
40  double diff = 0.0;
41  j = 0;
42  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
43  diff += fabs(NIdEigenH.GetDat(NI.GetId())-TmpV[j]);
44  }
45 
46  // set new values
47  j = 0;
48  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
49  NIdEigenH.AddDat(NI.GetId(), TmpV[j]);
50  }
51 
52  if (diff < Eps) {
53  break;
54  }
55  }
56 }
#define IAssert(Cond)
Definition: bd.h:262
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
void Gen(const int &ExpectVals)
Definition: hash.h:222
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
void TSnap::GetEigVals ( const PUNGraph Graph,
const int &  EigVals,
TFltV EigValV 
)

Computes top EigVals eigenvalues of the adjacency matrix representing a given undirected Graph.

Definition at line 308 of file gsvd.cpp.

308  {
309  // Lanczos
310  TUNGraphMtx GraphMtx(Graph);
311  //const int Nodes = Graph->GetNodes();
312  //int CalcVals = int(2*EigVals);
313  //if (CalcVals > Nodes) { CalcVals = Nodes; }
314  //while (EigValV.Len() < EigVals && CalcVals < 3*EigVals) {
315  try {
316  if (EigVals > 4) {
317  TSparseSVD::SimpleLanczos(GraphMtx, 2*EigVals, EigValV, false); }
318  else { TFltVV EigVecVV; // this is much more precise, but also much slower
319  TSparseSVD::Lanczos(GraphMtx, EigVals, 3*EigVals, ssotFull, EigValV, EigVecVV, false); }
320  }
321  catch(...) {
322  printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*EigVals, EigValV.Len()); }
323  if (EigValV.Len() < EigVals) {
324  printf(" ***TRIED %d GOT %d values** \n", 2*EigVals, EigValV.Len()); }
325  // CalcVals += EigVals;
326  //}
327  EigValV.Sort(false);
328  /*if (EigValV.Len() > EigVals) {
329  EigValV.Del(EigVals, EigValV.Len()-1); }
330  else {
331  while (EigValV.Len() < EigVals) EigValV.Add(1e-6);
332  }
333  IAssert(EigValV.Len() == EigVals);*/
334 }
static void Lanczos(const TMatrix &Matrix, int NumEig, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
Definition: linalg.cpp:1134
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
static void SimpleLanczos(const TMatrix &Matrix, const int &NumEig, TFltV &EigValV, const bool &DoLocalReortoP=false, const bool &SvdMatrixProductP=false)
Definition: linalg.cpp:1053
void TSnap::GetEigVec ( const PUNGraph Graph,
TFltV EigVecV 
)

Computes the leading eigenvector of the adjacency matrix representing a given undirected Graph.

Definition at line 336 of file gsvd.cpp.

336  {
337  TUNGraphMtx GraphMtx(Graph);
338  TFltV EigValV;
339  TFltVV EigVecVV;
340  TSparseSVD::Lanczos(GraphMtx, 1, 8, ssotFull, EigValV, EigVecVV, false);
341  EigVecVV.GetCol(0, EigVecV); // vector components are not sorted!!!
342  IsAllValVNeg(EigVecV, true);
343 }
static void Lanczos(const TMatrix &Matrix, int NumEig, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
Definition: linalg.cpp:1134
bool IsAllValVNeg(TFltV &ValV, const bool &InvertSign)
Definition: gsvd.cpp:163
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2390
void TSnap::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.

Definition at line 347 of file gsvd.cpp.

347  {
348  const int Nodes = Graph->GetNodes();
349  // Lanczos
350  TUNGraphMtx GraphMtx(Graph);
351  int CalcVals = int(2*EigVecs);
352  if (CalcVals > Nodes) { CalcVals = Nodes; }
353  TFltVV EigVecVV;
354  //while (EigValV.Len() < EigVecs && CalcVals < 10*EigVecs) {
355  try {
356  TSparseSVD::Lanczos(GraphMtx, EigVecs, 2*EigVecs, ssotFull, EigValV, EigVecVV, false); }
357  catch(...) {
358  printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); }
359  if (EigValV.Len() < EigVecs) {
360  printf(" ***TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); }
361  // CalcVals += EigVecs;
362  //}
363  TFltIntPrV EigValIdV;
364  for (int i = 0; i < EigValV.Len(); i++) {
365  EigValIdV.Add(TFltIntPr(EigValV[i], i));
366  }
367  EigValIdV.Sort(false);
368  EigValV.Sort(false);
369  for (int v = 0; v < EigValIdV.Len(); v++) { // vector components are not sorted!!!
370  EigVecV.Add();
371  EigVecVV.GetCol(EigValIdV[v].Val2, EigVecV.Last());
372  }
373  IsAllValVNeg(EigVecV[0], true);
374 }
TPair< TFlt, TInt > TFltIntPr
Definition: ds.h:97
static void Lanczos(const TMatrix &Matrix, int NumEig, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
Definition: linalg.cpp:1134
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool IsAllValVNeg(TFltV &ValV, const bool &InvertSign)
Definition: gsvd.cpp:163
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2390
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
template<class PGraph >
PGraph TSnap::GetESubGraph ( const PGraph &  Graph,
const TIntV EIdV 
)

Returns a subgraph of graph Graph with EIdV edges.

The resulting subgraph contains all the edges from Graph, which have edge IDs in the EIdV vector and all the nodes which connect to at least one edge in EIdV. Node and edge IDs are preserved. Nodes and edges in the resulting subgraph have the same IDs as in Graph.

Use this function for multi-graphs, where the edges have edge IDs.

Definition at line 243 of file subgraph.h.

243  {
244  CAssert(HasGraphFlag(typename PGraph::TObj, gfMultiGraph));
245  PGraph NewGraphPt = PGraph::TObj::New();
246  typename PGraph::TObj& NewGraph = *NewGraphPt;
247  NewGraph.Reserve(-1, EIdV.Len());
248  for (int edge = 0; edge < EIdV.Len(); edge++) {
249  const int EId = EIdV[edge];
250  IAssert(Graph->IsEdge(EId));
251  const typename PGraph::TObj::TEdgeI EI = Graph->GetEI(EId);
252  if (! NewGraph.IsNode(EI.GetSrcNId())) {
253  NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
254  }
255  if (! NewGraph.IsNode(EI.GetDstNId())) {
256  NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
257  }
258  NewGraph.AddEdge(EI);
259  }
260  return NewGraphPt;
261 }
#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
template<class PGraph >
PGraph TSnap::GetESubGraph ( const PGraph &  Graph,
const TIntPrV EdgeV 
)

Definition at line 264 of file subgraph.h.

264  {
265  PGraph NewGraphPt = PGraph::TObj::New();
266  typename PGraph::TObj& NewGraph = *NewGraphPt;
267  NewGraph.Reserve(-1, EdgeV.Len());
268 
269  for (int edge = 0; edge < EdgeV.Len(); edge++) {
270  const int SrcNId = EdgeV[edge].Val1;
271  const int DstNId = EdgeV[edge].Val2;
272  if (! NewGraph.IsNode(SrcNId)) {
273  NewGraph.AddNode(Graph->GetNI(SrcNId));
274  }
275  if (! NewGraph.IsNode(DstNId)) {
276  NewGraph.AddNode(Graph->GetNI(DstNId));
277  }
278 
279  NewGraph.AddEdge(SrcNId, DstNId);
280  }
281  return NewGraphPt;
282 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
template<class PGraph >
double TSnap::GetFarnessCentr ( const PGraph &  Graph,
const int &  NId,
const bool &  Normalized = true,
const bool &  IsDir = false 
)

Returns Farness centrality of a given node NId. Farness centrality of a node is the average shortest path length to all other nodes that reside is the same connected component as the given node.

Definition at line 123 of file centr.h.

123  {
124  TIntH NDistH(Graph->GetNodes());
125  TSnap::GetShortPath<PGraph>(Graph, NId, NDistH, IsDir, TInt::Mx);
126 
127  double sum = 0;
128  for (TIntH::TIter I = NDistH.BegI(); I < NDistH.EndI(); I++) {
129  sum += I->Dat();
130  }
131  if (NDistH.Len() > 1) {
132  double centr = sum/double(NDistH.Len()-1);
133  if (Normalized) {
134  centr *= (Graph->GetNodes() - 1)/double(NDistH.Len()-1);
135  }
136  return centr;
137  }
138  else { return 0.0; }
139 }
static const int Mx
Definition: dt.h:1142
THKeyDat * EndI
Definition: hash.h:54
template<class PGraph >
double TSnap::GetFarnessCentrMP ( const PGraph &  Graph,
const int &  NId,
const bool &  Normalized = true,
const bool &  IsDir = false 
)

Definition at line 142 of file centr.h.

142  {
143  TIntH NDistH(Graph->GetNodes());
144  TSnap::GetShortPath<PGraph>(Graph, NId, NDistH, IsDir, TInt::Mx);
145 
146  double sum = 0;
147  for (TIntH::TIter I = NDistH.BegI(); I < NDistH.EndI(); I++) {
148  sum += I->Dat();
149  }
150  if (NDistH.Len() > 1) {
151  double centr = sum/double(NDistH.Len()-1);
152  if (Normalized) {
153  centr *= (Graph->GetNodes() - 1)/double(NDistH.Len()-1);
154  }
155  return centr;
156  }
157  else { return 0.0; }
158 }
static const int Mx
Definition: dt.h:1142
THKeyDat * EndI
Definition: hash.h:54
TStr TSnap::GetFlagStr ( const TGraphFlag GraphFlag)

Returns a string representation of a flag.

Definition at line 5 of file gbase.cpp.

5  {
6  switch (GraphFlag) {
7  case gfUndef : return "Undef";
8  case gfDirected : return "Directed";
9  case gfMultiGraph : return "Multigraph";
10  case gfNodeDat : return "NodeDat";
11  case gfEdgeDat : return "EdgeDat";
12  case gfSources : return "Sources";
13  case gfBipart : return "Bipartite";
14  default: FailR("Unknown graph type");
15  };
16  return TStr();
17 }
default value, no flags
Definition: gbase.h:12
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
Definition: gbase.h:14
network with data on edges
Definition: gbase.h:16
#define FailR(Reason)
Definition: bd.h:240
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
Definition: dt.h:412
nodes only store out-edges (but not in-edges). See TBigNet
Definition: gbase.h:17
network with data on nodes
Definition: gbase.h:15
bipartite graph
Definition: gbase.h:18
template<class PGraph >
PGraph TSnap::GetGraphUnion ( PGraph &  DstGraph,
const PGraph &  SrcGraph 
)

Definition at line 764 of file subgraph.h.

764  {
765  for (typename PGraph::TObj::TNodeI NI = SrcGraph->BegNI(); NI < SrcGraph->EndNI(); NI++) {
766  if (! DstGraph->IsNode(NI.GetId())){
767  DstGraph->AddNode(NI.GetId());
768  }
769  }
770  for (typename PGraph::TObj::TEdgeI EI = SrcGraph->BegEI(); EI < SrcGraph->EndEI(); EI++) {
771  if (! HasGraphFlag(typename PGraph::TObj, gfMultiGraph)){
772  if (! DstGraph->IsEdge(EI.GetSrcNId(), EI.GetDstNId())){
773  DstGraph->AddEdge(EI.GetSrcNId(), EI.GetDstNId());
774  }
775  }
776  else{
777  if (! DstGraph->IsEdge(EI.GetSrcNId(), EI.GetDstNId()) || ! DstGraph->IsEdge(EI.GetId())){
778  if (! DstGraph->IsEdge(EI.GetId())){
779  DstGraph->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), EI.GetId());
780  }else{
781  DstGraph->AddEdge(EI.GetSrcNId(), EI.GetDstNId());
782  }
783  }
784  }
785  }
786  return DstGraph;
787 }
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
PNEANet TSnap::GetGraphUnionAttr ( PNEANet DstGraph,
const PNEANet SrcGraph 
)

Definition at line 521 of file subgraph.cpp.

521  {
522  for (PNEANet::TObj::TNodeI NI = SrcGraph->BegNI(); NI < SrcGraph->EndNI(); NI++) {
523  if (! DstGraph->IsNode(NI.GetId())){
524  AddNodeWithAttributes(SrcGraph, DstGraph, NI.GetId());
525  }
526  }
527  for (PNEANet::TObj::TEdgeI EI = SrcGraph->BegEI(); EI < SrcGraph->EndEI(); EI++) {
528  if (! DstGraph->IsEdge(EI.GetSrcNId(), EI.GetDstNId()) || ! DstGraph->IsEdge(EI.GetId())){
529  if (! DstGraph->IsEdge(EI.GetId())){
530  AddEdgeWithAttributes(SrcGraph, DstGraph, EI.GetId());
531  }else{
532  AddEdgeWithAttributes(SrcGraph, DstGraph, EI.GetSrcNId(), EI.GetDstNId());
533  }
534  }
535  }
536  return DstGraph;
537 }
void AddEdgeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId, const int NbrId)
Definition: subgraph.cpp:217
void AddNodeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId)
Definition: subgraph.cpp:143
double TSnap::GetGroupClosenessCentr ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Returns Group Degree centrality of a given group NId. Degree centrality if a node is defined as its degree/(N-1), where N is the number of nodes in the network.

Definition at line 169 of file centr.cpp.

169  {
170  const double Farness = GetGroupFarnessCentr(Graph, GroupNodes);
171  if (Farness != 0.0) { return 1.0/Farness; }
172  else { return 0.0; }
173 }
double GetGroupFarnessCentr(const PUNGraph &Graph, const TIntH &GroupNodes)
Definition: centr.cpp:105
double TSnap::GetGroupDegreeCentr ( const PUNGraph Graph,
const PUNGraph Group 
)

Definition at line 59 of file centr.cpp.

59  {
60  int deg;
61  TIntH NN;
62  for (TUNGraph::TNodeI NI = Group->BegNI(); NI < Group->EndNI(); NI++) {
63  deg = Graph->GetNI(NI.GetId()).GetDeg();
64  for (int i=0; i<deg; i++) {
65  if (Group->IsNode(Graph->GetNI(NI.GetId()).GetNbrNId(i))==0)
66  NN.AddDat(Graph->GetNI(NI.GetId()).GetNbrNId(i),NI.GetId());
67  }
68  }
69  return (double)NN.Len();
70 }
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
double TSnap::GetGroupDegreeCentr ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Returns Group Degree centrality of a given group NId. Degree centrality if a node is defined as its degree/(N-1), where N is the number of nodes in the network.

Definition at line 85 of file centr.cpp.

85  {
86  int deg;
87  TIntH NN;
88  TIntH GroupNodes1;
89 
90  for (THashKeyDatI<TInt,TInt> NI = GroupNodes.BegI(); NI < GroupNodes.EndI(); NI++)
91  GroupNodes1.AddDat(NI.GetDat(),NI.GetDat());
92 
93  for (THashKeyDatI<TInt,TInt> NI = GroupNodes1.BegI(); NI < GroupNodes1.EndI(); NI++){
94  TUNGraph::TNodeI node = Graph->GetNI(NI.GetKey());
95  deg = node.GetDeg();
96  for (int j = 0; j < deg; j++){
97  if (GroupNodes1.IsKey(node.GetNbrNId(j))==0 && NN.IsKey(node.GetNbrNId(j))==0)
98  NN.AddDat(node.GetNbrNId(j),NI.GetKey());
99  }
100  }
101 
102  return (double)NN.Len();
103 }
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
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
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
double TSnap::GetGroupDegreeCentr0 ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Definition at line 72 of file centr.cpp.

72  {
73  int deg;
74  TIntH NN;
75  for (int i = 0; i<GroupNodes.Len(); i++) {
76  deg = Graph->GetNI(GroupNodes.GetDat(i)).GetDeg();
77  for (int j = 0; j < deg; j++) {
78  if (GroupNodes.IsKey(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j))==0)
79  NN.AddDat(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j),GroupNodes.GetDat(i));
80  }
81  }
82  return (double)NN.Len();
83 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
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
double TSnap::GetGroupFarnessCentr ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Definition at line 105 of file centr.cpp.

105  {
106  TIntH* NDistH = new TIntH[GroupNodes.Len()];
107 
108  for (int i=0; i<GroupNodes.Len(); i++){
109  NDistH[i](Graph->GetNodes());
110  TSnap::GetShortPath<PUNGraph>(Graph, GroupNodes.GetDat(i), NDistH[i], true, TInt::Mx);
111  }
112 
113  int min, dist, sum=0, len=0;
114  for (PUNGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
115  if(NDistH[0].IsKey(NI.GetId()))
116  min = NDistH[0].GetDat(NI.GetId());
117  else
118  min = -1;
119  for (int j=1; j<GroupNodes.Len(); j++){
120  if (NDistH[j].IsKey(NI.GetId()))
121  dist = NDistH[j].GetDat(NI.GetId());
122  else
123  dist = -1;
124  if ((dist < min && dist != -1) || (dist > min && min == -1))
125  min = dist;
126  }
127  if (min>0){
128  sum += min;
129  len++;
130  }
131 
132  }
133 
134  if (len > 0) { return sum/double(len); }
135  else { return 0.0; }
136 }
static const int Mx
Definition: dt.h:1142
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
int Len() const
Definition: hash.h:228
template<class PGraph >
void TSnap::GetHits ( const PGraph &  Graph,
TIntFltH NIdHubH,
TIntFltH NIdAuthH,
const int &  MaxIter = 20 
)

HITS: Hubs and Authorities For more info see: http://en.wikipedia.org/wiki/HITS_algorithm)

Definition at line 524 of file centr.h.

524  {
525  const int NNodes = Graph->GetNodes();
526  NIdHubH.Gen(NNodes);
527  NIdAuthH.Gen(NNodes);
528  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
529  NIdHubH.AddDat(NI.GetId(), 1.0);
530  NIdAuthH.AddDat(NI.GetId(), 1.0);
531  }
532  double Norm=0;
533  for (int iter = 0; iter < MaxIter; iter++) {
534  // update authority scores
535  Norm = 0;
536  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
537  double& Auth = NIdAuthH.GetDat(NI.GetId()).Val;
538  Auth = 0;
539  for (int e = 0; e < NI.GetInDeg(); e++) {
540  Auth += NIdHubH.GetDat(NI.GetInNId(e)); }
541  Norm += Auth*Auth;
542  }
543  Norm = sqrt(Norm);
544  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
545  // update hub scores
546  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
547  double& Hub = NIdHubH.GetDat(NI.GetId()).Val;
548  Hub = 0;
549  for (int e = 0; e < NI.GetOutDeg(); e++) {
550  Hub += NIdAuthH.GetDat(NI.GetOutNId(e)); }
551  Norm += Hub*Hub;
552  }
553  Norm = sqrt(Norm);
554  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
555  }
556  // make sure Hub and Authority scores normalize to L2 norm 1
557  Norm = 0.0;
558  for (int i = 0; i < NIdHubH.Len(); i++) { Norm += TMath::Sqr(NIdHubH[i]); }
559  Norm = sqrt(Norm);
560  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
561  Norm = 0.0;
562  for (int i = 0; i < NIdAuthH.Len(); i++) { Norm += TMath::Sqr(NIdAuthH[i]); }
563  Norm = sqrt(Norm);
564  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
565 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
static double Sqr(const double &x)
Definition: xmath.h:12
void Gen(const int &ExpectVals)
Definition: hash.h:222
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
void TSnap::GetHitsMP ( const PGraph &  Graph,
TIntFltH NIdHubH,
TIntFltH NIdAuthH,
const int &  MaxIter = 20 
)

Definition at line 569 of file centr.h.

569  {
570  const int NNodes = Graph->GetNodes();
571  TIntV NV;
572  NIdHubH.Gen(NNodes);
573  NIdAuthH.Gen(NNodes);
574  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
575  NV.Add(NI.GetId());
576  NIdHubH.AddDat(NI.GetId(), 1.0);
577  NIdAuthH.AddDat(NI.GetId(), 1.0);
578  }
579  double Norm=0;
580  for (int iter = 0; iter < MaxIter; iter++) {
581  // update authority scores
582  Norm = 0;
583  #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
584  for (int i = 0; i < NNodes; i++) {
585  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
586  double& Auth = NIdAuthH.GetDat(NI.GetId()).Val;
587  Auth = 0;
588  for (int e = 0; e < NI.GetInDeg(); e++) {
589  Auth += NIdHubH.GetDat(NI.GetInNId(e)); }
590  Norm = Norm + Auth*Auth;
591  }
592  Norm = sqrt(Norm);
593  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
594  // update hub scores
595  #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
596  for (int i = 0; i < NNodes; i++) {
597  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
598  double& Hub = NIdHubH.GetDat(NI.GetId()).Val;
599  Hub = 0;
600  for (int e = 0; e < NI.GetOutDeg(); e++) {
601  Hub += NIdAuthH.GetDat(NI.GetOutNId(e)); }
602  Norm = Norm + Hub*Hub;
603  }
604  Norm = sqrt(Norm);
605  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
606  }
607  // make sure Hub and Authority scores normalize to L2 norm 1
608  Norm = 0.0;
609  for (int i = 0; i < NIdHubH.Len(); i++) { Norm += TMath::Sqr(NIdHubH[i]); }
610  Norm = sqrt(Norm);
611  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
612  Norm = 0.0;
613  for (int i = 0; i < NIdAuthH.Len(); i++) { Norm += TMath::Sqr(NIdAuthH[i]); }
614  Norm = sqrt(Norm);
615  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
616 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
static double Sqr(const double &x)
Definition: xmath.h:12
void Gen(const int &ExpectVals)
Definition: hash.h:222
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
template<class PGraph >
void TSnap::GetInDegCnt ( const PGraph &  Graph,
TIntPrV DegToCntV 
)

Returns an in-degree histogram: a set of pairs (in-degree, number of nodes of such in-degree)

Definition at line 179 of file alg.h.

179  {
180  TIntH DegToCntH;
181  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
182  DegToCntH.AddDat(NI.GetInDeg())++; }
183  DegToCntV.Gen(DegToCntH.Len(), 0);
184  for (int i = 0; i < DegToCntH.Len(); i++) {
185  DegToCntV.Add(TIntPr(DegToCntH.GetKey(i), DegToCntH[i])); }
186  DegToCntV.Sort();
187 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
template<class PGraph >
void TSnap::GetInDegCnt ( const PGraph &  Graph,
TFltPrV DegToCntV 
)

Returns an in-degree histogram: a set of pairs (in-degree, number of nodes of such in-degree)

Definition at line 190 of file alg.h.

190  {
191  TIntH DegToCntH;
192  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
193  DegToCntH.AddDat(NI.GetInDeg())++; }
194  DegToCntV.Gen(DegToCntH.Len(), 0);
195  for (int i = 0; i < DegToCntH.Len(); i++) {
196  DegToCntV.Add(TFltPr(DegToCntH.GetKey(i).Val, DegToCntH[i].Val)); }
197  DegToCntV.Sort();
198 }
int Val
Definition: dt.h:1139
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
PNEANet TSnap::GetInEgonetAttr ( const PNEANet Graph,
const int  CtrNId,
const int  Radius 
)

Returns the in-egonet of at given radius and copies node and edge attributes.

Definition at line 342 of file subgraph.cpp.

342  {
343  PNEANet NewGraphPt = PNEANet::New();
344  TNEANet &NewGraph = *NewGraphPt;
345  TSnapQueue<int> Queue1;
346  TSnapQueue<int> Queue2;
347  TSnapQueue<int> tempSwapQueue;
348  AddNodeWithAttributes(Graph, NewGraphPt, CtrNId);
349  Queue1.Clr(false);
350  Queue1.Push(CtrNId);
351  for (int r = 0; r < Radius; ++r) {
352  while (!Queue1.Empty()) {
353  const int NId = Queue1.Top();
354  Queue1.Pop();
355  const TNEANet::TNodeI &Node = Graph->GetNI(NId);
356  for (int i = 0; i < Node.GetInDeg(); ++i) {
357  const int InNId = Node.GetInNId(i);
358  if (!NewGraph.IsNode(InNId)) {
359  AddNodeWithAttributes(Graph, NewGraphPt, InNId);
360  Queue2.Push(InNId);
361  }
362  const int InEId = Node.GetInEId(i);
363  if (!NewGraph.IsEdge(InEId)) {
364  AddEdgeWithAttributes(Graph, NewGraphPt, InEId);
365  }
366  }
367  for (int i = 0; i < Node.GetInDeg(); ++i) {
368  int InNId = Node.GetInNId(i);
369  const TNEANet::TNodeI &InNode = Graph->GetNI(InNId);
370  for (int j = 0; j < InNode.GetInDeg(); ++j) {
371  int NbrInNId = InNode.GetInNId(j);
372  if (NewGraph.IsNode(NbrInNId)) {
373  const int NbrInEId = InNode.GetInEId(j);
374  if (!NewGraph.IsEdge(NbrInEId)) {
375  AddEdgeWithAttributes(Graph, NewGraphPt, NbrInEId);
376  }
377  }
378  }
379  for (int j = 0; j < InNode.GetOutDeg(); ++j) {
380  int NbrOutNId = InNode.GetOutNId(j);
381  if (NewGraph.IsNode(NbrOutNId)) {
382  const int NbrOutEId = InNode.GetOutEId(j);
383  if (!NewGraph.IsEdge(NbrOutEId)) {
384  AddEdgeWithAttributes(Graph, NewGraphPt, NbrOutEId);
385  }
386  }
387  }
388  }
389  }
390  tempSwapQueue = Queue1;
391  Queue1 = Queue2;
392  Queue2 = tempSwapQueue;
393  }
394  return NewGraphPt;
395 }
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
static TPt New()
Definition: bd.h:479
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: network.h:2804
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: network.h:1833
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
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
void AddEdgeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId, const int NbrId)
Definition: subgraph.cpp:217
Directed multigraph with node edge attributes.
Definition: network.h:1741
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
void AddNodeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId)
Definition: subgraph.cpp:143
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
Definition: bd.h:196
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2385
template<class PGraph >
PGraph TSnap::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.

Definition at line 589 of file subgraph.h.

589  {
590  PGraph NewGraphPt = PGraph::TObj::New();
591  typename PGraph::TObj& NewGraph = *NewGraphPt;
592  TSnapQueue<int> Queue1;
593  TSnapQueue<int> Queue2;
594  TSnapQueue<int> tempSwapQueue;
595  NewGraph.AddNode(CtrNId);
596  Queue1.Clr(false);
597  Queue1.Push(CtrNId);
598  for (int r = 0; r < Radius; ++r) {
599  while (!Queue1.Empty()) {
600  const int NId = Queue1.Top();
601  Queue1.Pop();
602  const typename PGraph::TObj::TNodeI &Node = Graph->GetNI(NId);
603  for (int i = 0; i < Node.GetInDeg(); ++i) {
604  const int InNId = Node.GetInNId(i);
605  if (!NewGraph.IsNode(InNId)) {
606  NewGraph.AddNode(InNId);
607  Queue2.Push(InNId);
608  }
609  if (!NewGraph.IsEdge(InNId, NId)) {
610  NewGraph.AddEdge(InNId, NId);
611  }
612  }
613  for (int i = 0; i < Node.GetInDeg(); ++i) {
614  int InNId = Node.GetInNId(i);
615  const typename PGraph::TObj::TNodeI &InNode = Graph->GetNI(InNId);
616  for (int j = 0; j < InNode.GetInDeg(); ++j) {
617  int NbrInNId = InNode.GetInNId(j);
618  if (NewGraph.IsNode(NbrInNId)) {
619  if (!NewGraph.IsEdge(NbrInNId, InNId)) {
620  NewGraph.AddEdge(NbrInNId, InNId);
621  }
622  }
623  }
624  for (int j = 0; j < InNode.GetOutDeg(); ++j) {
625  int NbrOutNId = InNode.GetOutNId(j);
626  if (NewGraph.IsNode(NbrOutNId)) {
627  if (!NewGraph.IsEdge(InNId, NbrOutNId)) {
628  NewGraph.AddEdge(InNId, NbrOutNId);
629  }
630  }
631  }
632  }
633  }
634  tempSwapQueue = Queue1;
635  Queue1 = Queue2;
636  Queue2 = tempSwapQueue;
637  }
638  return NewGraphPt;
639 }
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
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
template<class PGraph >
PGraph TSnap::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.

Definition at line 695 of file subgraph.h.

695  {
696  PGraph NewGraphPt = PGraph::TObj::New();
697  typename PGraph::TObj& NewGraph = *NewGraphPt;
698  TSnapQueue<int> Queue1;
699  TSnapQueue<int> Queue2;
700  TSnapQueue<int> tempSwapQueue;
701  TSnapQueue<int> sampleQueue;
702  NewGraph.AddNode(CtrNId);
703  Queue1.Clr(false);
704  Queue1.Push(CtrNId);
705  bool usePercent = (percent != -1.0);
706  int numSamples = MaxNum;
707  for (int r = 0; r < Radius; ++r) {
708  while (!Queue1.Empty()) {
709  const int NId = Queue1.Top();
710  Queue1.Pop();
711  const typename PGraph::TObj::TNodeI &Node = Graph->GetNI(NId);
712  sampleQueue.Clr(true);
713  for (int i = 0; i < Node.GetInDeg(); ++i) {
714  const int InNId = Node.GetInNId(i);
715  if (!NewGraph.IsNode(InNId)) {
716  sampleQueue.Push(InNId);
717  }
718  }
719  if (usePercent) {
720  numSamples = (int) (percent * sampleQueue.Len());
721  }
722  sampleQueue.Sample(numSamples);
723  for (int i = 0; i < numSamples && !sampleQueue.Empty(); ++i) {
724  const int InNId = sampleQueue.Top();
725  sampleQueue.Pop();
726  if (!NewGraph.IsNode(InNId)) {
727  NewGraph.AddNode(InNId);
728  Queue2.Push(InNId);
729  }
730  if (!NewGraph.IsEdge(InNId, NId)) {
731  NewGraph.AddEdge(InNId, NId);
732  }
733  }
734  for (int i = 0; i < Node.GetInDeg(); ++i) {
735  int InNId = Node.GetInNId(i);
736  if (!NewGraph.IsNode(InNId)) { continue; }
737  const typename PGraph::TObj::TNodeI &InNode = Graph->GetNI(InNId);
738  for (int j = 0; j < InNode.GetInDeg(); ++j) {
739  int NbrInNId = InNode.GetInNId(j);
740  if (NewGraph.IsNode(NbrInNId)) {
741  if (!NewGraph.IsEdge(NbrInNId, InNId)) {
742  NewGraph.AddEdge(NbrInNId, InNId);
743  }
744  }
745  }
746  for (int j = 0; j < InNode.GetOutDeg(); ++j) {
747  int NbrOutNId = InNode.GetOutNId(j);
748  if (NewGraph.IsNode(NbrOutNId)) {
749  if (!NewGraph.IsEdge(InNId, NbrOutNId)) {
750  NewGraph.AddEdge(InNId, NbrOutNId);
751  }
752  }
753  }
754  }
755  }
756  tempSwapQueue = Queue1;
757  Queue1 = Queue2;
758  Queue2 = tempSwapQueue;
759  }
760  return NewGraphPt;
761 }
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
int Len() const
Returns the number of elements in the queue.
Definition: gbase.h:201
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Sample(const int num, TRnd &Rnd=TInt::Rnd)
Definition: gbase.h:181
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
PNEANet TSnap::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.

Definition at line 453 of file subgraph.cpp.

453  {
454  PNEANet NewGraphPt = TNEANet::New();
455  TNEANet& NewGraph = *NewGraphPt;
456  TSnapQueue<int> Queue1;
457  TSnapQueue<int> Queue2;
458  TSnapQueue<int> tempSwapQueue;
459  TSnapQueue<int> sampleQueue;
460  AddNodeWithAttributes(Graph, NewGraphPt, CtrNId);
461  Queue1.Clr(false);
462  Queue1.Push(CtrNId);
463  bool usePercent = (percent != -1.0);
464  int numSamples = MaxNum;
465  for (int r = 0; r < Radius; ++r) {
466  while (!Queue1.Empty()) {
467  const int NId = Queue1.Top();
468  Queue1.Pop();
469  const TNEANet::TNodeI &Node = Graph->GetNI(NId);
470  sampleQueue.Clr(true);
471  for (int i = 0; i < Node.GetInDeg(); ++i) {
472  const int InNId = Node.GetInNId(i);
473  if (!NewGraph.IsNode(InNId)) {
474  sampleQueue.Push(InNId);
475  }
476  }
477  if (usePercent) {
478  numSamples = (int) (percent * sampleQueue.Len());
479  }
480  sampleQueue.Sample(numSamples);
481  for (int i = 0; i < numSamples && !sampleQueue.Empty(); ++i) {
482  const int InNId = sampleQueue.Top();
483  sampleQueue.Pop();
484  if (!NewGraph.IsNode(InNId)) {
485  AddNodeWithAttributes(Graph, NewGraphPt, InNId);
486  Queue2.Push(InNId);
487  }
488  if (!NewGraph.IsEdge(InNId, NId)) {
489  AddEdgeWithAttributes(Graph, NewGraphPt, InNId, NId);
490  }
491  }
492  for (int i = 0; i < Node.GetInDeg(); ++i) {
493  int InNId = Node.GetInNId(i);
494  if (!NewGraph.IsNode(InNId)) { continue; }
495  const TNEANet::TNodeI &InNode = Graph->GetNI(InNId);
496  for (int j = 0; j < InNode.GetInDeg(); ++j) {
497  int NbrInNId = InNode.GetInNId(j);
498  if (NewGraph.IsNode(NbrInNId)) {
499  if (!NewGraph.IsEdge(NbrInNId, InNId)) {
500  AddEdgeWithAttributes(Graph, NewGraphPt, NbrInNId, InNId);
501  }
502  }
503  }
504  for (int j = 0; j < InNode.GetOutDeg(); ++j) {
505  int NbrOutNId = InNode.GetOutNId(j);
506  if (NewGraph.IsNode(NbrOutNId)) {
507  if (!NewGraph.IsEdge(InNId, NbrOutNId)) {
508  AddEdgeWithAttributes(Graph, NewGraphPt, InNId, NbrOutNId);
509  }
510  }
511  }
512  }
513  }
514  tempSwapQueue = Queue1;
515  Queue1 = Queue2;
516  Queue2 = tempSwapQueue;
517  }
518  return NewGraphPt;
519 }
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: network.h:2804
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
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
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
void AddEdgeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId, const int NbrId)
Definition: subgraph.cpp:217
int Len() const
Returns the number of elements in the queue.
Definition: gbase.h:201
Directed multigraph with node edge attributes.
Definition: network.h:1741
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Sample(const int num, TRnd &Rnd=TInt::Rnd)
Definition: gbase.h:181
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
void AddNodeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId)
Definition: subgraph.cpp:143
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
Definition: bd.h:196
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2385
static PNEANet New()
Static cons returns pointer to graph. Ex: PNEANet Graph=TNEANet::New().
Definition: network.h:2226
void TSnap::GetInvParticipRat ( const PUNGraph Graph,
int  MaxEigVecs,
int  TimeLimit,
TFltPrV EigValIprV 
)

Computes Inverse participation ratio of a given graph. See Spectra of "real-world" graphs: Beyond the semicircle law by Farkas, Derenyi, Barabasi and Vicsek

Definition at line 378 of file gsvd.cpp.

378  {
379  TUNGraphMtx GraphMtx(Graph);
380  TFltVV EigVecVV;
381  TFltV EigValV;
382  TExeTm ExeTm;
383  if (MaxEigVecs<=1) { MaxEigVecs=1000; }
384  int EigVecs = TMath::Mn(Graph->GetNodes(), MaxEigVecs);
385  printf("start %d vecs...", EigVecs);
386  try {
387  TSparseSVD::Lanczos2(GraphMtx, EigVecs, TimeLimit, ssotFull, EigValV, EigVecVV, false);
388  } catch(...) {
389  printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", EigVecs, EigValV.Len()); }
390  printf(" ***TRIED %d GOT %d values in %s\n", EigVecs, EigValV.Len(), ExeTm.GetStr());
391  TFltV EigVec;
392  EigValIprV.Clr();
393  if (EigValV.Empty()) { return; }
394  for (int v = 0; v < EigVecVV.GetCols(); v++) {
395  EigVecVV.GetCol(v, EigVec);
396  EigValIprV.Add(TFltPr(EigValV[v], TSnapDetail::GetInvParticipRatEig(EigVec)));
397  }
398  EigValIprV.Sort();
399 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
double GetInvParticipRatEig(const TFltV &EigVec)
Definition: gsvd.cpp:402
Definition: tm.h:355
static void Lanczos2(const TMatrix &Matrix, int MaxNumEig, int MaxSecs, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
Definition: linalg.cpp:1290
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2390
const char * GetStr() const
Definition: tm.h:368
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TSizeTy GetCols() const
Definition: ds.h:2253
template<class PGraph >
PGraph TSnap::GetKCore ( const PGraph &  Graph,
const int &  K 
)

Returns the K-core of a graph. If the core of order K does not exist the function returns an empty graph.

Definition at line 106 of file kcore.h.

106  {
107  TKCore<PGraph> KCore(Graph);
108  KCore.GetCoreK(K);
109  return TSnap::GetSubGraph(Graph, KCore.GetNIdV());
110 }
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
Definition: kcore.h:11
template<class PGraph >
int TSnap::GetKCoreEdges ( const PGraph &  Graph,
TIntPrV CoreIdSzV 
)

Returns the number of edges in each core of order K (where K=0, 1, ...)

Definition at line 126 of file kcore.h.

126  {
127  TKCore<PGraph> KCore(Graph);
128  CoreIdSzV.Clr();
129  CoreIdSzV.Add(TIntPr(0, Graph->GetEdges()));
130  for (int i = 1; KCore.GetNextCore() > 0; i++) {
131  CoreIdSzV.Add(TIntPr(i, KCore.GetCoreEdges()));
132  }
133  return KCore.GetCurK();
134 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
Definition: kcore.h:11
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int TSnap::GetKCoreNodes ( const PGraph &  Graph,
TIntPrV CoreIdSzV 
)

Returns the number of nodes in each core of order K (where K=0, 1, ...)

Definition at line 114 of file kcore.h.

114  {
115  TKCore<PGraph> KCore(Graph);
116  CoreIdSzV.Clr();
117  CoreIdSzV.Add(TIntPr(0, Graph->GetNodes()));
118  for (int i = 1; KCore.GetNextCore() > 0; i++) {
119  CoreIdSzV.Add(TIntPr(i, KCore.GetCoreNodes()));
120  }
121  return KCore.GetCurK();
122 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
Definition: kcore.h:11
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int TSnap::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).

Definition at line 761 of file triad.h.

761  {
762  TIntV NbrV;
763  return GetLen2Paths(Graph, NId1, NId2, NbrV);
764 }
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).
Definition: triad.h:769
template<class PGraph >
int TSnap::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).

NbrV intermediary stores nodes U.

Definition at line 769 of file triad.h.

769  {
770  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId1);
771  NbrV.Clr(false);
772  NbrV.Reserve(NI.GetOutDeg());
773  for (int e = 0; e < NI.GetOutDeg(); e++) {
774  const typename PGraph::TObj::TNodeI MidNI = Graph->GetNI(NI.GetOutNId(e));
775  if (MidNI.IsOutNId(NId2)) {
776  NbrV.Add(MidNI.GetId());
777  }
778  }
779  return NbrV.Len();
780 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TTableIterator TSnap::GetMapHitsIterator ( const TVec< PNEANet > &  GraphSeq,
TTableContext Context,
const int &  MaxIter = 20 
)

Gets sequence of Hits tables from given GraphSeq.

Definition at line 917 of file centr.cpp.

920  {
921  TVec<PTable> TableSeq(GraphSeq.Len());
922  TSnap::MapHits(GraphSeq, TableSeq, Context, MaxIter);
923  return TTableIterator(TableSeq);
924 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Iterator over a vector of tables.
Definition: table.h:423
void MapHits(const TVec< PGraph > &GraphSeq, TVec< PTable > &TableSeq, TTableContext *Context, const int &MaxIter)
Gets sequence of Hits tables from given GraphSeq into TableSeq.
Definition: centr.h:636
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
TTableIterator TSnap::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.

Definition at line 907 of file centr.cpp.

910  {
911  TVec<PTable> TableSeq(GraphSeq.Len());
912  TSnap::MapPageRank(GraphSeq, TableSeq, Context, C, Eps, MaxIter);
913  return TTableIterator(TableSeq);
914 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Iterator over a vector of tables.
Definition: table.h:423
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.
Definition: centr.h:621
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
int TSnap::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.

Implements max flow using the Edmonds-Karp algorithm. http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm Although the asymptotic run time of Edmonds-Karp is worse than that of Push Relabel, in practice Edmonds Karp works very well, especially if the network is sparse. Unless the degree of each node is on the order of the number of nodes, it is best to use Edmonds Karp over Push Relabel.

Definition at line 105 of file flow.cpp.

105  {
106  IAssert(Net->IsNode(SrcNId));
107  IAssert(Net->IsNode(SnkNId));
108  if (SrcNId == SnkNId) { return 0; }
109  int CapIndex = Net->GetIntAttrIndE(CapAttrName);
110  TIntV Flow(Net->GetMxEId());
111  // Initialize flow values to 0, and make sure capacities are nonnegative
112  for (TNEANet::TEdgeI EI = Net->BegEI(); EI != Net->EndEI(); EI++) {
113  IAssert(Net->GetIntAttrIndDatE(EI, CapIndex) >= 0);
114  Flow[EI.GetId()] = 0;
115  }
116  // Return 0 if user attempts to flow from a node to itself.
117  if (SrcNId == SnkNId) { return 0; }
118  int MaxFlow = 0, MinAug, CurNId;
119  while (true) {
120  TIntV MidToSrcAugV; TIntV MidToSnkAugV;
121  TIntQ FwdNodeQ; TIntQ BwdNodeQ;
122  TIntH PredEdgeH; TIntH SuccEdgeH;
123  MinAug = FindAugV(Net, CapIndex, Flow, FwdNodeQ, PredEdgeH, BwdNodeQ, SuccEdgeH, MidToSrcAugV, MidToSnkAugV, SrcNId, SnkNId);
124  if (MinAug == 0) { break; }
125  MaxFlow += MinAug;
126  CurNId = SrcNId;
127  for (int i = MidToSrcAugV.Len() - 1; i >= 0; i--) {
128  int NextEId = MidToSrcAugV[i];
129  const TNEANet::TEdgeI &EI = Net->GetEI(NextEId);
130  if (EI.GetSrcNId() == CurNId) {
131  Flow[NextEId] += MinAug;
132  CurNId = EI.GetDstNId();
133  } else {
134  Flow[NextEId] -= MinAug;
135  CurNId = EI.GetSrcNId();
136  }
137  }
138  for (int i = 0; i < MidToSnkAugV.Len(); i++) {
139  int NextEId = MidToSnkAugV[i];
140  const TNEANet::TEdgeI &EI = Net->GetEI(NextEId);
141  if (EI.GetSrcNId() == CurNId) {
142  Flow[NextEId] += MinAug;
143  CurNId = EI.GetDstNId();
144  } else {
145  Flow[NextEId] -= MinAug;
146  CurNId = EI.GetSrcNId();
147  }
148  }
149  }
150  return MaxFlow;
151 }
#define IAssert(Cond)
Definition: bd.h:262
const TStr CapAttrName
Definition: flow.h:4
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
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.
Definition: flow.cpp:71
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
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1867
int TSnap::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.

Implements max flow using the Edmonds-Karp algorithm. http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm Although the asymptotic run time of Edmonds-Karp is worse than that of Push Relabel, in practice Edmonds Karp works very well, especially if the network is sparse. Unless the degree of each node is on the order of the number of nodes, it is best to use Edmonds Karp over Push Relabel.

Definition at line 410 of file flow.cpp.

410  {
411  IAssert(Net->IsNode(SrcNId));
412  IAssert(Net->IsNode(SnkNId));
413  if (SrcNId == SnkNId) { return 0; }
414 
415  TPRManager PRM(Net);
416  int MaxLabel = PRM.GetMaxLabel();
417 
418  TNEANet::TNodeI SrcNI = Net->GetNI(SrcNId);
419  for (int EdgeN = 0; EdgeN < SrcNI.GetOutDeg(); EdgeN++) {
420  int EId = SrcNI.GetOutEId(EdgeN);
421  int OutNId = SrcNI.GetOutNId(EdgeN);
422  if (OutNId != SrcNId) {
423  int Capacity = PRM.Capacity(EId);
424  PRM.Flow(EId) = Capacity;
425  PRM.Excess(OutNId) = Capacity;
426  }
427  }
428  GlobalRelabel(Net, PRM, SrcNId, SnkNId);
429  PRM.SetLabel(SrcNId, MaxLabel);
430  int RelabelCount = 1;
431  int GRRate = Net->GetNodes();
432  while (PRM.HasActive()) {
433  int NId = PRM.PopActive();
434  const TNEANet::TNodeI &NI = Net->GetNI(NId);
435  int PrevLabel = MaxLabel;
436  while (PRM.Excess(NId) > 0 && PRM.Label(NId) <= PrevLabel) {
437  PrevLabel = PRM.Label(NId);
438  int NbrNId = PushRelabel(PRM, NId, NI);
439  if (NbrNId != -1 && NbrNId != SnkNId && PRM.Excess(NbrNId) > 0 && !PRM.IsActive(NbrNId)) {
440  PRM.PushActive(NbrNId);
441  }
442  }
443  if (PRM.Excess(NId) > 0 && PRM.Label(NId) < MaxLabel) {
444  PRM.PushActive(NId);
445  }
446  if (RelabelCount % GRRate == 0) { GlobalRelabel(Net, PRM, SrcNId, SnkNId); }
447  }
448  return PRM.Excess(SnkNId);
449 }
#define IAssert(Cond)
Definition: bd.h:262
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.
Definition: flow.cpp:328
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
void GlobalRelabel(PNEANet &Net, TPRManager &PRM, const int &SrcNId, const int &SnkNId)
Implements the Global Relabeling heuristic.
Definition: flow.cpp:363
template<typename PGraph >
double TSnap::GetModularity ( const PGraph &  G,
const TIntV NIdV,
int  GEdges = -1 
)

Computes Modularity score of a set of nodes NIdV in a graph G. The function runs much faster if the number of edges in graph G is given (GEdges parameter).

Definition at line 46 of file cmty.h.

46  {
47  if (GEdges == -1) { GEdges = Graph->GetEdges(); }
48  double EdgesIn = 0.0, EEdgesIn = 0.0; // EdgesIn=2*number of edges inside the cluster, EEdgesIn=expected edges inside
49  TIntSet NIdSet(NIdV.Len());
50  for (int e = 0; e < NIdV.Len(); e++) { // edges inside
51  NIdSet.AddKey(NIdV[e]);
52  }
53  for (int e1 = 0; e1 < NIdV.Len(); e1++) {
54  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[e1]);
55  EEdgesIn += NI.GetOutDeg();
56  for (int i = 0; i < NI.GetOutDeg(); i++) {
57  if (NIdSet.IsKey(NI.GetOutNId(i))) { EdgesIn += 1; }
58  }
59  }
60  EEdgesIn = EEdgesIn*EEdgesIn / (2.0*GEdges);
61  if ((EdgesIn - EEdgesIn) == 0) { return 0; }
62  else { return (EdgesIn - EEdgesIn) / (2.0*GEdges); } // modularity
63 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int AddKey(const TKey &Key)
Definition: shash.h:1254
template<typename PGraph >
double TSnap::GetModularity ( const PGraph &  G,
const TCnComV CmtyV,
int  GEdges = -1 
)

Computes Modularity score of a set of communities (each community is defined by its member nodes) in a graph G. The function runs much faster if the number of edges in graph G is given (GEdges parameter).

Definition at line 66 of file cmty.h.

66  {
67  if (GEdges == -1) { GEdges = G->GetEdges(); }
68  double Modularity = 0;
69  for (int c = 0; c < CmtyV.Len(); c++) {
70  Modularity += GetModularity(G, CmtyV[c](), GEdges);
71  }
72  return Modularity;
73 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
double GetModularity(const PGraph &G, const TCnComV &CmtyV, int GEdges=-1)
Definition: cmty.h:66
template<class PGraph >
PGraph TSnap::GetMxBiCon ( const PGraph &  Graph)

Returns a graph representing the largest bi-connected component on an input Graph.

An undirected graph is bi-connected if by removing any single node does not disconnect the graph. http://en.wikipedia.org/wiki/Biconnected_component

Definition at line 486 of file cncom.h.

486  {
487  TCnComV CnComV;
488  GetBiCon(TSnap::ConvertGraph<PUNGraph, PGraph>(Graph), CnComV);
489  if (CnComV.Empty()) { return PGraph::TObj::New(); }
490  int CcId = 0, MxSz = 0;
491  for (int i = 0; i < CnComV.Len(); i++) {
492  if (MxSz < CnComV[i].Len()) {
493  MxSz=CnComV[i].Len(); CcId=i; }
494  }
495  if (CnComV[CcId].Len()==Graph->GetNodes()) {
496  return Graph; }
497  else {
498  return TSnap::GetSubGraph(Graph, CnComV[CcId]());
499  }
500 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
void GetBiCon(const PUNGraph &Graph, TCnComV &BiCnComV)
Returns all bi-connected components of a Graph.
Definition: cncom.cpp:42
PUNGraph TSnap::GetMxBiCon ( const PUNGraph Graph,
const bool &  RenumberNodes = false 
)

Returns a graph representing the largest bi-connected component on an undirected Graph.

An undirected graph is bi-connected if by removing any single node does not disconnect the graph. http://en.wikipedia.org/wiki/Biconnected_component

Definition at line 126 of file cncom.cpp.

126  {
127  TCnComV CnComV;
128  GetBiCon(Graph, CnComV);
129  if (CnComV.Empty()) {
130  return PUNGraph();
131  }
132  int CcId = 0, MxSz = 0;
133  for (int i = 0; i < CnComV.Len(); i++) {
134  if (MxSz < CnComV[i].Len()) {
135  MxSz = CnComV[i].Len();
136  CcId=i;
137  }
138  }
139  return TSnap::GetSubGraph(Graph, CnComV[CcId](), RenumberNodes);
140 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
void GetBiCon(const PUNGraph &Graph, TCnComV &BiCnComV)
Returns all bi-connected components of a Graph.
Definition: cncom.cpp:42
TPt< TUNGraph > PUNGraph
Pointer to an undirected graph (TUNGraph)
Definition: graph.h:5
template<class PGraph >
int TSnap::GetMxDegNId ( const PGraph &  Graph)

Returns a randomly chosen node from all the nodes with the maximum degree.

Definition at line 143 of file alg.h.

143  {
144  TIntV MxDegV;
145  int MxDeg=-1;
146  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
147  if (MxDeg < NI.GetDeg()) { MxDegV.Clr(); MxDeg = NI.GetDeg(); }
148  if (MxDeg == NI.GetDeg()) { MxDegV.Add(NI.GetId()); }
149  }
150  EAssertR(! MxDegV.Empty(), "Input graph is empty!");
151  return MxDegV[TInt::Rnd.GetUniDevInt(MxDegV.Len())];
152 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static TRnd Rnd
Definition: dt.h:1146
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
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
template<class PGraph >
int TSnap::GetMxInDegNId ( const PGraph &  Graph)

Returns a randomly chosen node from all the nodes with the maximum in-degree.

Definition at line 155 of file alg.h.

155  {
156  TIntV MxDegV;
157  int MxDeg=-1;
158  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
159  if (MxDeg < NI.GetInDeg()) { MxDegV.Clr(); MxDeg = NI.GetInDeg(); }
160  if (MxDeg == NI.GetInDeg()) { MxDegV.Add(NI.GetId()); }
161  }
162  EAssertR(! MxDegV.Empty(), "Input graph is empty!");
163  return MxDegV[TInt::Rnd.GetUniDevInt(MxDegV.Len())];
164 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static TRnd Rnd
Definition: dt.h:1146
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
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
template<class PGraph >
int TSnap::GetMxOutDegNId ( const PGraph &  Graph)

Returns a randomly chosen node from all the nodes with the maximum out-degree.

Definition at line 167 of file alg.h.

167  {
168  TIntV MxDegV;
169  int MxDeg=-1;
170  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
171  if (MxDeg < NI.GetOutDeg()) { MxDegV.Clr(); MxDeg = NI.GetOutDeg(); }
172  if (MxDeg == NI.GetOutDeg()) { MxDegV.Add(NI.GetId()); }
173  }
174  EAssertR(! MxDegV.Empty(), "Input graph is empty!");
175  return MxDegV[TInt::Rnd.GetUniDevInt(MxDegV.Len())];
176 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static TRnd Rnd
Definition: dt.h:1146
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
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
template<class PGraph >
PGraph TSnap::GetMxScc ( const PGraph &  Graph)

Returns a graph representing the largest strongly connected component on an input Graph.

A directed graph is strongly connected if there exists a directed path from any vertex to any other vertex in the graph. See http://en.wikipedia.org/wiki/Strongly_connected_component

Definition at line 469 of file cncom.h.

469  {
470  TCnComV CnComV;
471  GetSccs(Graph, CnComV);
472  if (CnComV.Empty()) { return PGraph::TObj::New(); }
473  int CcId = 0, MxSz = 0;
474  for (int i = 0; i < CnComV.Len(); i++) {
475  if (MxSz < CnComV[i].Len()) {
476  MxSz=CnComV[i].Len(); CcId=i; }
477  }
478  if (CnComV[CcId].Len()==Graph->GetNodes()) {
479  return Graph; }
480  else {
481  return TSnap::GetSubGraph(Graph, CnComV[CcId]());
482  }
483 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetSccs(const PGraph &Graph, TCnComV &CnComV)
Returns all strongly connected components in a Graph.
Definition: cncom.h:428
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
template<class PGraph >
double TSnap::GetMxSccSz ( const PGraph &  Graph)

Returns the fraction of nodes in the largest strongly connected component of a Graph.

Definition at line 444 of file cncom.h.

444  {
445  TCnComV CnComV;
446  GetSccs(Graph, CnComV);
447  if (Graph->GetNodes() == 0) { return 0; }
448  else { return CnComV[0].Len() / double(Graph->GetNodes()); }
449 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetSccs(const PGraph &Graph, TCnComV &CnComV)
Returns all strongly connected components in a Graph.
Definition: cncom.h:428
template<class PGraph >
PGraph TSnap::GetMxWcc ( const PGraph &  Graph)

Returns a graph representing the largest weakly connected component on an input Graph.

A directed/undirected graph is connected if there exist an undirected path between any pair of nodes. See http://en.wikipedia.org/wiki/Connected_component_(graph_theory)

Definition at line 452 of file cncom.h.

452  {
453  TCnComV CnComV;
454  GetWccs(Graph, CnComV);
455  if (CnComV.Empty()) { return PGraph::TObj::New(); }
456  int CcId = 0, MxSz = 0;
457  for (int i = 0; i < CnComV.Len(); i++) {
458  if (MxSz < CnComV[i].Len()) {
459  MxSz=CnComV[i].Len(); CcId=i; }
460  }
461  if (CnComV[CcId].Len()==Graph->GetNodes()) {
462  return Graph; }
463  else {
464  return TSnap::GetSubGraph(Graph, CnComV[CcId]());
465  }
466 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
Definition: cncom.h:376
template<class PGraph >
double TSnap::GetMxWccSz ( const PGraph &  Graph)

Returns the fraction of nodes in the largest weakly connected component of a Graph.

Definition at line 436 of file cncom.h.

436  {
437  TCnComV CnComV;
438  GetWccs(Graph, CnComV);
439  if (Graph->GetNodes() == 0) { return 0; }
440  else { return CnComV[0].Len() / double(Graph->GetNodes()); }
441 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
Definition: cncom.h:376
template<class PGraph >
double TSnap::GetNodeClustCf ( const PGraph &  Graph,
const int &  NId 
)

Returns clustering coefficient of a particular node.

Considers the graph as undirected.

Definition at line 220 of file triad.h.

220  {
221  int Open, Closed;
222  GetNodeTriads(Graph, NId, Open, Closed);
223  //const double Deg = Graph->GetNI(NId).GetDeg();
224  return (Open+Closed)==0 ? 0 : double(Open)/double(Open+Closed);
225 }
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.
Definition: triad.h:660
template<class PGraph >
void TSnap::GetNodeClustCf ( const PGraph &  Graph,
TIntFltH NIdCCfH 
)

Computes clustering coefficient of each node of the Graph.

Considers the graph as undirected.

Definition at line 228 of file triad.h.

228  {
229  TIntTrV NIdCOTriadV;
230  GetTriads(Graph, NIdCOTriadV);
231  NIdCCfH.Clr(false);
232  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
233  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
234  const double CCf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
235  NIdCCfH.AddDat(NIdCOTriadV[i].Val1, CCf);
236  }
237 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetTriads(const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
Computes the number of open and close triads for every node of the network.
Definition: triad.h:318
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
int TSnap::GetNodeEcc ( const PGraph &  Graph,
const int &  NId,
const bool &  IsDir = false 
)

Returns node Eccentricity, the largest shortest-path distance from the node NId to any other node in the Graph.

Parameters
IsDirfalse: ignore edge directions and consider edges as undirected (in case they are directed).

Definition at line 177 of file centr.h.

177  {
178  int NodeEcc;
179  int Dist;
180  TBreathFS<PGraph> BFS(Graph);
181  // get shortest paths to all the nodes
182  BFS.DoBfs(NId, true, ! IsDir, -1, TInt::Mx);
183 
184  NodeEcc = 0;
185  // find the largest value
186  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
187  Dist = BFS.NIdDistH[i];
188  if (Dist > NodeEcc) {
189  NodeEcc = Dist;
190  }
191  }
192  return NodeEcc;
193 }
static const int Mx
Definition: dt.h:1142
template<class PGraph >
void TSnap::GetNodeInDegV ( const PGraph &  Graph,
TIntPrV NIdInDegV 
)

Returns a vector of pairs (node id, node in-degree)

Definition at line 263 of file alg.h.

263  {
264  NIdInDegV.Reserve(Graph->GetNodes(), 0);
265  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
266  NIdInDegV.Add(TIntPr(NI.GetId(), NI.GetInDeg()));
267  }
268 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::GetNodeOutDegV ( const PGraph &  Graph,
TIntPrV NIdOutDegV 
)

Returns a vector of pairs (node id, node out-degree)

Definition at line 271 of file alg.h.

271  {
272  NIdOutDegV.Reserve(Graph->GetNodes(), 0);
273  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
274  NIdOutDegV.Add(TIntPr(NI.GetId(), NI.GetOutDeg()));
275  }
276 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int TSnap::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.

false: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 375 of file bfsdfs.h.

375  {
376  TBreathFS<PGraph> BFS(Graph);
377  BFS.DoBfs(StartNId, true, !IsDir, -1, Hop);
378  NIdV.Clr(false);
379  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
380  if (BFS.NIdDistH[i] == Hop) {
381  NIdV.Add(BFS.NIdDistH.GetKey(i)); }
382  }
383  return NIdV.Len();
384 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int TSnap::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.

false: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 387 of file bfsdfs.h.

387  {
388  TBreathFS<PGraph> BFS(Graph);
389  BFS.DoBfs(StartNId, true, !IsDir, -1, TInt::Mx);
390  TIntH HopCntH;
391  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
392  HopCntH.AddDat(BFS.NIdDistH[i]) += 1;
393  }
394  HopCntH.GetKeyDatPrV(HopCntV);
395  HopCntV.Sort();
396  return HopCntV.Len();
397 }
static const int Mx
Definition: dt.h:1142
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:500
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
int TSnap::GetNodeTriads ( const PGraph &  Graph,
const int &  NId 
)

Returns the number of undirected triads a node NId participates in.

Considers the graph as undirected.

Parameters
GraphInput graph
NIdInput node

Definition at line 615 of file triad.h.

615  {
616  int ClosedTriads=0, OpenTriads=0;
617  return GetNodeTriads(Graph, NId, ClosedTriads, OpenTriads);
618 }
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.
Definition: triad.h:660
template<class PGraph >
int TSnap::GetNodeTriads ( const PGraph &  Graph,
const int &  NId,
int &  ClosedNTriadsX,
int &  OpenNTriadsX 
)

Returns number of Open and Closed triads a node NId participates in.

Considers the graph as undirected.

Parameters
GraphInput graph
NIdInput node
ClosedNTriadsXOn return contains the number of closed triads.
OpenNTriadsXOn return contains the number of open triads.

Definition at line 622 of file triad.h.

622  {
623  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
624  ClosedTriads=0; OpenTriads=0;
625  if (NI.GetDeg() < 2) { return 0; }
626  // find neighborhood
627  TIntSet NbrSet(NI.GetDeg());
628  for (int e = 0; e < NI.GetOutDeg(); e++) {
629  if (NI.GetOutNId(e) != NI.GetId()) { // exclude self edges
630  NbrSet.AddKey(NI.GetOutNId(e)); }
631  }
632  if (Graph->HasFlag(gfDirected)) {
633  for (int e = 0; e < NI.GetInDeg(); e++) {
634  if (NI.GetInNId(e) != NI.GetId()) { // exclude self edges
635  NbrSet.AddKey(NI.GetInNId(e)); }
636  }
637  }
638  // count connected neighbors
639  for (int srcNbr = 0; srcNbr < NbrSet.Len(); srcNbr++) {
640  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrSet.GetKey(srcNbr));
641  for (int dstNbr = srcNbr+1; dstNbr < NbrSet.Len(); dstNbr++) {
642  const int dstNId = NbrSet.GetKey(dstNbr);
643  if (SrcNode.IsNbrNId(dstNId)) { ClosedTriads++; }
644  else { OpenTriads++; }
645  }
646  }
647  return ClosedTriads;
648 }
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
template<class PGraph >
int TSnap::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.

Considers the graph as undirected.

Parameters
GraphInput graph
NIdInput node
GroupSetInput set with node neighbors
InGroupEdgesXOn return contains the number of triads (NId, G1, G2), where G1 and G2 are in GroupSet.
InOutGroupEdgesXOn return contains the number of triads (NId, G1, O1), where G1 is in GroupSet and O1 not in GroupSet.
OutGroupEdgesXOn return contains the number of triads (NId, O1, O2), where O1 and O2 are not in GroupSet.

Definition at line 660 of file triad.h.

660  {
661  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
662  const bool IsDir = Graph->HasFlag(gfDirected);
663  InGroupEdges=0; InOutGroupEdges=0; OutGroupEdges=0;
664  if (NI.GetDeg() < 2) { return 0; }
665  // find neighborhood
666  TIntSet NbrSet(NI.GetDeg());
667  for (int e = 0; e < NI.GetOutDeg(); e++) {
668  if (NI.GetOutNId(e) != NI.GetId()) { // exclude self edges
669  NbrSet.AddKey(NI.GetOutNId(e)); }
670  }
671  if (IsDir) {
672  for (int e = 0; e < NI.GetInDeg(); e++) {
673  if (NI.GetInNId(e) != NI.GetId()) {
674  NbrSet.AddKey(NI.GetInNId(e)); }
675  }
676  }
677  // count connected neighbors
678  for (int srcNbr = 0; srcNbr < NbrSet.Len(); srcNbr++) {
679  const int NbrId = NbrSet.GetKey(srcNbr);
680  const bool NbrIn = GroupSet.IsKey(NbrId);
681  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrId);
682  for (int dstNbr = srcNbr+1; dstNbr < NbrSet.Len(); dstNbr++) {
683  const int DstNId = NbrSet.GetKey(dstNbr);
684  if (SrcNode.IsNbrNId(DstNId)) { // triad (NId, NbrId, DstNid)
685  bool DstIn = GroupSet.IsKey(DstNId);
686  if (NbrIn && DstIn) { InGroupEdges++; }
687  else if (NbrIn || DstIn) { InOutGroupEdges++; }
688  else { OutGroupEdges++; }
689  }
690  }
691  }
692  return InGroupEdges;
693 }
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
template<class PGraph >
int TSnap::GetNodeTriadsAll ( const PGraph &  Graph,
const int &  NId,
int &  ClosedNTriadsX,
int &  OpenNTriadsX 
)

Returns number of Open and Closed triads a node NId participates in.

Considers the graph as undirected. This function is a duplicate. It is required by Snap.py.

Parameters
GraphInput graph
NIdInput node
ClosedNTriadsXOn return contains the number of closed triads.
OpenNTriadsXOn return contains the number of open triads.

Definition at line 651 of file triad.h.

651  {
652  return GetNodeTriads(Graph, NId, ClosedTriads, OpenTriads);
653 }
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.
Definition: triad.h:660
template<class PGraph >
void TSnap::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.

Definition at line 277 of file cncom.h.

277  {
278  typename PGraph::TObj::TNodeI NI;
279  THashSet<TInt> VisitedNId(Graph->GetNodes()+1);
280  TSnapQueue<int> NIdQ(Graph->GetNodes()+1);
281  VisitedNId.AddKey(NId);
282  NIdQ.Push(NId);
283  while (! NIdQ.Empty()) {
284  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
285  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
286  for (int e = 0; e < Node.GetInDeg(); e++) {
287  const int InNId = Node.GetInNId(e);
288  if (! VisitedNId.IsKey(InNId)) {
289  NIdQ.Push(InNId); VisitedNId.AddKey(InNId); }
290  }
291  }
292  for (int e = 0; e < Node.GetOutDeg(); e++) {
293  const int OutNId = Node.GetOutNId(e);
294  if (! VisitedNId.IsKey(OutNId)) {
295  NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); }
296  }
297  }
298  CnCom.Gen(VisitedNId.Len(), 0);
299  for (int i = 0; i < VisitedNId.Len(); i++) {
300  CnCom.Add(VisitedNId.GetKey(i));
301  }
302 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::GetOutDegCnt ( const PGraph &  Graph,
TIntPrV DegToCntV 
)

Returns an out-degree histogram: a set of pairs (out-degree, number of nodes of such out-degree)

Definition at line 201 of file alg.h.

201  {
202  TIntH DegToCntH;
203  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
204  DegToCntH.AddDat(NI.GetOutDeg())++; }
205  DegToCntV.Gen(DegToCntH.Len(), 0);
206  for (int i = 0; i < DegToCntH.Len(); i++) {
207  DegToCntV.Add(TIntPr(DegToCntH.GetKey(i), DegToCntH[i])); }
208  DegToCntV.Sort();
209 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
template<class PGraph >
void TSnap::GetOutDegCnt ( const PGraph &  Graph,
TFltPrV DegToCntV 
)

Returns an out-degree histogram: a set of pairs (out-degree, number of nodes of such out-degree)

Definition at line 212 of file alg.h.

212  {
213  TIntH DegToCntH;
214  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
215  DegToCntH.AddDat(NI.GetOutDeg())++; }
216  DegToCntV.Gen(DegToCntH.Len(), 0);
217  for (int i = 0; i < DegToCntH.Len(); i++) {
218  DegToCntV.Add(TFltPr(DegToCntH.GetKey(i).Val, DegToCntH[i].Val)); }
219  DegToCntV.Sort();
220 }
int Val
Definition: dt.h:1139
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
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
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
PNEANet TSnap::GetOutEgonetAttr ( const PNEANet Graph,
const int  CtrNId,
const int  Radius 
)

Returns the out-egonet of at given radius and copies node and edge attributes.

Definition at line 397 of file subgraph.cpp.

397  {
398  PNEANet NewGraphPt = PNEANet::New();
399  TNEANet &NewGraph = *NewGraphPt;
400  TSnapQueue<int> Queue1;
401  TSnapQueue<int> Queue2;
402  TSnapQueue<int> tempSwapQueue;
403  AddNodeWithAttributes(Graph, NewGraphPt, CtrNId);
404  Queue1.Clr(false);
405  Queue1.Push(CtrNId);
406  for (int r = 0; r < Radius; ++r) {
407  while (!Queue1.Empty()) {
408  const int NId = Queue1.Top();
409  Queue1.Pop();
410  const TNEANet::TNodeI &Node = Graph->GetNI(NId);
411  for (int i = 0; i < Node.GetOutDeg(); ++i) {
412  const int OutNId = Node.GetOutNId(i);
413  if (!NewGraph.IsNode(OutNId)) {
414  AddNodeWithAttributes(Graph, NewGraphPt, OutNId);
415  Queue2.Push(OutNId);
416  }
417  const int OutEId = Node.GetOutEId(i);
418  if (!NewGraph.IsEdge(OutEId)) {
419  AddEdgeWithAttributes(Graph, NewGraphPt, OutEId);
420  }
421  }
422  for (int i = 0; i < Node.GetOutDeg(); ++i) {
423  int OutNId = Node.GetOutNId(i);
424  const TNEANet::TNodeI &OutNode = Graph->GetNI(OutNId);
425  for (int j = 0; j < OutNode.GetInDeg(); ++j) {
426  int NbrInNId = OutNode.GetInNId(j);
427  if (NewGraph.IsNode(NbrInNId)) {
428  const int InEId = OutNode.GetInEId(j);
429  if (!NewGraph.IsEdge(InEId)) {
430  AddEdgeWithAttributes(Graph, NewGraphPt, InEId);
431  }
432  }
433  }
434  for (int j = 0; j < OutNode.GetOutDeg(); ++j) {
435  int NbrOutNId = OutNode.GetOutNId(j);
436  if (NewGraph.IsNode(NbrOutNId)) {
437  const int OutEId = OutNode.GetOutEId(j);
438  if (!NewGraph.IsEdge(OutEId)) {
439  AddEdgeWithAttributes(Graph, NewGraphPt, OutEId);
440  }
441  }
442  }
443  }
444  }
445  tempSwapQueue = Queue1;
446  Queue1 = Queue2;
447  Queue2 = tempSwapQueue;
448  }
449  return NewGraphPt;
450 }
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
static TPt New()
Definition: bd.h:479
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: network.h:2804
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: network.h:1833
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
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
void AddEdgeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId, const int NbrId)
Definition: subgraph.cpp:217
Directed multigraph with node edge attributes.
Definition: network.h:1741
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
void AddNodeWithAttributes(const PNEANet &Graph1, PNEANet &Graph2, const int NId)
Definition: subgraph.cpp:143
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
Definition: bd.h:196
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2385
template<class PGraph >
PGraph TSnap::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.

Definition at line 642 of file subgraph.h.

642  {
643  PGraph NewGraphPt = PGraph::TObj::New();
644  typename PGraph::TObj& NewGraph = *NewGraphPt;
645  TSnapQueue<int> Queue1;
646  TSnapQueue<int> Queue2;
647  TSnapQueue<int> tempSwapQueue;
648  NewGraph.AddNode(CtrNId);
649  Queue1.Clr(false);
650  Queue1.Push(CtrNId);
651  for (int r = 0; r < Radius; ++r) {
652  while (!Queue1.Empty()) {
653  const int NId = Queue1.Top();
654  Queue1.Pop();
655  const typename PGraph::TObj::TNodeI &Node = Graph->GetNI(NId);
656  for (int i = 0; i < Node.GetOutDeg(); ++i) {
657  const int OutNId = Node.GetOutNId(i);
658  if (!NewGraph.IsNode(OutNId)) {
659  NewGraph.AddNode(OutNId);
660  Queue2.Push(OutNId);
661  }
662  if (!NewGraph.IsEdge(NId, OutNId)) {
663  NewGraph.AddEdge(NId, OutNId);
664  }
665  }
666  for (int i = 0; i < Node.GetOutDeg(); ++i) {
667  int OutNId = Node.GetOutNId(i);
668  const typename PGraph::TObj::TNodeI &OutNode = Graph->GetNI(OutNId);
669  for (int j = 0; j < OutNode.GetInDeg(); ++j) {
670  int NbrInNId = OutNode.GetInNId(j);
671  if (NewGraph.IsNode(NbrInNId)) {
672  if (!NewGraph.IsEdge(NbrInNId, OutNId)) {
673  NewGraph.AddEdge(NbrInNId, OutNId);
674  }
675  }
676  }
677  for (int j = 0; j < OutNode.GetOutDeg(); ++j) {
678  int NbrOutNId = OutNode.GetOutNId(j);
679  if (NewGraph.IsNode(NbrOutNId)) {
680  if (!NewGraph.IsEdge(OutNId, NbrOutNId)) {
681  NewGraph.AddEdge(OutNId, NbrOutNId);
682  }
683  }
684  }
685  }
686  }
687  tempSwapQueue = Queue1;
688  Queue1 = Queue2;
689  Queue2 = tempSwapQueue;
690  }
691  return NewGraphPt;
692 }
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
void Clr(const bool &DoDel=true)
Deletes all elements from the queue.
Definition: gbase.h:194
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
const TVal & Top() const
Returns the value of the first element in the queue, but does not remove the element.
Definition: gbase.h:209
template<class PGraph >
void TSnap::GetPageRank ( const PGraph &  Graph,
TIntFltH PRankH,
const double &  C = 0.85,
const double &  Eps = 1e-4,
const int &  MaxIter = 100 
)

PageRank For more info see: http://en.wikipedia.org/wiki/PageRank

Definition at line 240 of file centr.h.

240  {
241  const int NNodes = Graph->GetNodes();
243  PRankH.Gen(NNodes);
244  int MxId = -1;
245  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
246  NV.Add(NI);
247  PRankH.AddDat(NI.GetId(), 1.0/NNodes);
248  int Id = NI.GetId();
249  if (Id > MxId) {
250  MxId = Id;
251  }
252  }
253 
254  TFltV PRankV(MxId+1);
255  TIntV OutDegV(MxId+1);
256 
257  for (int j = 0; j < NNodes; j++) {
258  typename PGraph::TObj::TNodeI NI = NV[j];
259  int Id = NI.GetId();
260  PRankV[Id] = 1.0/NNodes;
261  OutDegV[Id] = NI.GetOutDeg();
262  }
263 
264  TFltV TmpV(NNodes);
265 
266  for (int iter = 0; iter < MaxIter; iter++) {
267  for (int j = 0; j < NNodes; j++) {
268  typename PGraph::TObj::TNodeI NI = NV[j];
269  TFlt Tmp = 0;
270  for (int e = 0; e < NI.GetInDeg(); e++) {
271  const int InNId = NI.GetInNId(e);
272  const int OutDeg = OutDegV[InNId];
273  if (OutDeg > 0) {
274  Tmp += PRankV[InNId] / OutDeg;
275  }
276  }
277  TmpV[j] = C*Tmp; // Berkhin (the correct way of doing it)
278  }
279  double sum = 0;
280  for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
281  const double Leaked = (1.0-sum) / double(NNodes);
282 
283  double diff = 0;
284  for (int i = 0; i < NNodes; i++) {
285  typename PGraph::TObj::TNodeI NI = NV[i];
286  double NewVal = TmpV[i] + Leaked; // Berkhin
287  int Id = NI.GetId();
288  diff += fabs(NewVal-PRankV[Id]);
289  PRankV[Id] = NewVal;
290  }
291  if (diff < Eps) { break; }
292  }
293 
294  for (int i = 0; i < NNodes; i++) {
295  typename PGraph::TObj::TNodeI NI = NV[i];
296  PRankH[i] = PRankV[NI.GetId()];
297  }
298 }
Definition: dt.h:1386
void Gen(const int &ExpectVals)
Definition: hash.h:222
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
template<class PGraph >
void TSnap::GetPageRank_v1 ( const PGraph &  Graph,
TIntFltH PRankH,
const double &  C = 0.85,
const double &  Eps = 1e-4,
const int &  MaxIter = 100 
)

Definition at line 200 of file centr.h.

200  {
201  const int NNodes = Graph->GetNodes();
202  //const double OneOver = 1.0/double(NNodes);
203  PRankH.Gen(NNodes);
204  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
205  PRankH.AddDat(NI.GetId(), 1.0/NNodes);
206  //IAssert(NI.GetId() == PRankH.GetKey(PRankH.Len()-1));
207  }
208  TFltV TmpV(NNodes);
209  for (int iter = 0; iter < MaxIter; iter++) {
210  int j = 0;
211  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
212  TmpV[j] = 0;
213  for (int e = 0; e < NI.GetInDeg(); e++) {
214  const int InNId = NI.GetInNId(e);
215  const int OutDeg = Graph->GetNI(InNId).GetOutDeg();
216  if (OutDeg > 0) {
217  TmpV[j] += PRankH.GetDat(InNId) / OutDeg; }
218  }
219  TmpV[j] = C*TmpV[j]; // Berkhin (the correct way of doing it)
220  //TmpV[j] = C*TmpV[j] + (1.0-C)*OneOver; // iGraph
221  }
222  double diff=0, sum=0, NewVal;
223  for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
224  const double Leaked = (1.0-sum) / double(NNodes);
225  for (int i = 0; i < PRankH.Len(); i++) { // re-instert leaked PageRank
226  NewVal = TmpV[i] + Leaked; // Berkhin
227  //NewVal = TmpV[i] / sum; // iGraph
228  diff += fabs(NewVal-PRankH[i]);
229  PRankH[i] = NewVal;
230  }
231  if (diff < Eps) { break; }
232  }
233 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
void Gen(const int &ExpectVals)
Definition: hash.h:222
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
void TSnap::GetPageRankMP ( const PGraph &  Graph,
TIntFltH PRankH,
const double &  C = 0.85,
const double &  Eps = 1e-4,
const int &  MaxIter = 100 
)

Definition at line 306 of file centr.h.

306  {
307  const int NNodes = Graph->GetNodes();
309  PRankH.Gen(NNodes);
310 
311  int MxId = -1;
312  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
313  NV.Add(NI);
314  PRankH.AddDat(NI.GetId(), 1.0/NNodes);
315  int Id = NI.GetId();
316  if (Id > MxId) {
317  MxId = Id;
318  }
319  }
320 
321  TFltV PRankV(MxId+1);
322  TIntV OutDegV(MxId+1);
323 
324  #pragma omp parallel for schedule(dynamic,10000)
325  for (int j = 0; j < NNodes; j++) {
326  typename PGraph::TObj::TNodeI NI = NV[j];
327  int Id = NI.GetId();
328  PRankV[Id] = 1.0/NNodes;
329  OutDegV[Id] = NI.GetOutDeg();
330  }
331 
332  TFltV TmpV(NNodes);
333  for (int iter = 0; iter < MaxIter; iter++) {
334  #pragma omp parallel for schedule(dynamic,10000)
335  for (int j = 0; j < NNodes; j++) {
336  typename PGraph::TObj::TNodeI NI = NV[j];
337  TFlt Tmp = 0;
338  for (int e = 0; e < NI.GetInDeg(); e++) {
339  const int InNId = NI.GetInNId(e);
340  const int OutDeg = OutDegV[InNId];
341  if (OutDeg > 0) {
342  Tmp += PRankV[InNId] / OutDeg;
343  }
344  }
345  TmpV[j] = C*Tmp; // Berkhin (the correct way of doing it)
346  }
347 
348  double sum = 0;
349  #pragma omp parallel for reduction(+:sum) schedule(dynamic,10000)
350  for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
351  const double Leaked = (1.0-sum) / double(NNodes);
352 
353  double diff = 0;
354  #pragma omp parallel for reduction(+:diff) schedule(dynamic,10000)
355  for (int i = 0; i < NNodes; i++) {
356  double NewVal = TmpV[i] + Leaked; // Berkhin
357  int Id = NV[i].GetId();
358  diff += fabs(NewVal-PRankV[Id]);
359  PRankV[Id] = NewVal;
360  }
361  if (diff < Eps) { break; }
362  }
363 
364  #pragma omp parallel for schedule(dynamic,10000)
365  for (int i = 0; i < NNodes; i++) {
366  typename PGraph::TObj::TNodeI NI = NV[i];
367  PRankH[i] = PRankV[NI.GetId()];
368  }
369 }
Definition: dt.h:1386
void Gen(const int &ExpectVals)
Definition: hash.h:222
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
template<class PGraph >
double TSnap::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 
)

Definition at line 88 of file randwalk.h.

95  {
96  if (MinProbability <= 0.0) { // Check if minProbability not set.
97  MinProbability = 1.0 / Graph->GetNodes();
98  }
99  // In experiments, when relativeError = 0.1, a chernoff constant of 0.07 gave mean relative error less than 0.1 on several realistic graphs.
100  float kChernoffConstant = provableRelativeError ? 12 * exp((double) 1) * log(2 / 1.0e-9) : 0.07;
101  float kSecondsPerWalk = 4.0e-7; // The time required to generate a random walk. Can be tuned so that forward and reverse running times are equal, to improve running time
102  float WalkCountRMaxRatio = kChernoffConstant / (RelativeError * RelativeError) / MinProbability;
103  float ForwardSecondsRMaxRatio = kSecondsPerWalk * WalkCountRMaxRatio;
104 
105 
106  double startTime = WallClockTime();
107  // Results from ApproxContributionsBalanced are set by reference:
108  TIntFltH Estimates, Residuals;
109  float MaxResidual;
110  ApproxContributionsBalanced(Graph, JumpProb, TargetNId, ForwardSecondsRMaxRatio, Estimates, Residuals, MaxResidual);
111 
112  double reverseTime = WallClockTime() - startTime;
113  startTime = WallClockTime();
114 
115  double Estimate = 0.0;
116  // First incorporate the average Estimates value for starting nodes
117  for (int i = 0; i < StartNIdV.Len(); i++) {
118  Estimate += Estimates.GetDatWithDefault(StartNIdV[i], 0.0) / StartNIdV.Len();
119  }
120 
121  int RandomWalkCount = static_cast<int>(WalkCountRMaxRatio * MaxResidual);
122  TRnd Rnd(0); // 0 means seed from clock. We use an explicit Rnd for thread safety.
123  for (int i = 0; i < RandomWalkCount; i++) {
124  int vId = SamplePersonalizedPageRank(Graph, JumpProb, StartNIdV, Rnd);
125  Estimate += Residuals.GetDatWithDefault(vId, 0.0) / RandomWalkCount;
126  }
127  double forwardTime = WallClockTime() - startTime;
128  if (PrintTimeForTuning) printf("forwardTime reverseTime %g %g\n", forwardTime, reverseTime);
129 
130  return Estimate;
131 }
void ApproxContributionsBalanced(const PGraph &Graph, double JumpProb, int TargetNId, float ForwardSecondsRMaxRatio, TIntFltH &ResultEstimates, TIntFltH &ResultResiduals, float &ResultMaxResidual)
Definition: randwalk.h:32
Definition: dt.h:11
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TDat GetDatWithDefault(const TKey &Key, TDat DefaultValue)
Definition: hash.h:264
int SamplePersonalizedPageRank(const PGraph &Graph, double JumpProb, const TIntV &StartNIdV, TRnd &Rnd)
Definition: randwalk.h:67
template<class PGraph >
PGraph TSnap::GetRndESubGraph ( const PGraph &  Graph,
const int &  NEdges 
)

Returns a random subgraph of graph Graph with NEdges edges.

Randomly selects NEdges edges from the input graph and returns a subgraph on those edges.

Definition at line 490 of file subgraph.h.

490  {
491  CAssert(! HasGraphFlag(typename PGraph::TObj, gfMultiGraph));
492  TIntPrV EdgeV(Graph->GetEdges(), 0);
493  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
494  EdgeV.Add(TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
495  }
496  EdgeV.Shuffle(TInt::Rnd);
497  EdgeV.Del(NEdges, EdgeV.Len()-1);
498  IAssert(EdgeV.Len() == NEdges);
499  return GetESubGraph(Graph, EdgeV);
500 }
#define IAssert(Cond)
Definition: bd.h:262
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
PGraph GetESubGraph(const PGraph &Graph, const TIntPrV &EdgeV)
Definition: subgraph.h:264
static TRnd Rnd
Definition: dt.h:1146
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
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
PGraph TSnap::GetRndSubGraph ( const PGraph &  Graph,
const int &  NNodes 
)

Returns an induced random subgraph of graph Graph with NNodes nodes.

Randomly selects NNodes nodes from the input graph and returns an induced graph on those nodes.

Definition at line 479 of file subgraph.h.

479  {
480  IAssert(NNodes <= Graph->GetNodes());
481  TIntV NIdV;
482  Graph->GetNIdV(NIdV);
483  NIdV.Shuffle(TInt::Rnd);
484  NIdV.Del(NNodes, NIdV.Len()-1);
485  IAssert(NIdV.Len() == NNodes);
486  return GetSubGraph(Graph, NIdV);
487 }
#define IAssert(Cond)
Definition: bd.h:262
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
static TRnd Rnd
Definition: dt.h:1146
PGraph GetSubGraph(const PGraph &Graph, const TIntV &NIdV)
Returns an induced subgraph of graph Graph with NIdV nodes.
Definition: subgraph.h:232
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
template<class PGraph >
double TSnap::GetRndWalkRestartBidirectional ( const PGraph &  Graph,
double  JumpProb,
int  StartNId,
int  TargetNId,
double  minProbability = -1.0,
double  relativeError = 0.1,
bool  proveRelativeError = false,
bool  PrintTimeForTuning = false 
)

Definition at line 135 of file randwalk.h.

142  {
143  return GetPersonalizedPageRankBidirectional(Graph, JumpProb, TIntV::GetV(StartNId), TargetNId,
144  minProbability, relativeError, proveRelativeError, PrintTimeForTuning);
145  }
static TVec< TInt, int > GetV(const TInt &Val1)
Returns a vector on element Val1.
Definition: ds.h:848
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)
Definition: randwalk.h:88
template<class PGraph >
void TSnap::GetSccs ( const PGraph &  Graph,
TCnComV CnComV 
)

Returns all strongly connected components in a Graph.

Parameters
CnComVis a vector of connected components. Each component is defined by the IDs of its member nodes.

Definition at line 428 of file cncom.h.

428  {
429  TSccVisitor<PGraph, false> Visitor(Graph);
430  TCnCom::GetDfsVisitor(Graph, Visitor);
431  CnComV = Visitor.CnComV;
432  CnComV.Sort(false);
433 }
static void GetDfsVisitor(const PGraph &Graph, TVisitor &Visitor)
Definition: cncom.h:124
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
Strongly connected componetns Depht-First-Search visitor class.
Definition: cncom.h:234
template<class PGraph >
void TSnap::GetSccSzCnt ( const PGraph &  Graph,
TIntPrV SccSzCnt 
)

Returns a distribution of strongly connected component sizes.

Parameters
SccSzCntreturns a set of pairs (number of nodes in the component, number of such components)

Definition at line 420 of file cncom.h.

420  {
421  TSccVisitor<PGraph, true> Visitor(Graph);
422  TCnCom::GetDfsVisitor(Graph, Visitor);
423  Visitor.SccCntH.GetKeyDatPrV(SccSzCnt);
424  SccSzCnt.Sort(true);
425 }
static void GetDfsVisitor(const PGraph &Graph, TVisitor &Visitor)
Definition: cncom.h:124
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
Strongly connected componetns Depht-First-Search visitor class.
Definition: cncom.h:234
template<class PGraph >
int TSnap::GetShortestDistances ( const PGraph &  Graph,
const int &  StartNId,
const bool &  FollowOut,
const bool &  FollowIn,
TIntV ShortestDists 
)

Definition at line 501 of file bfsdfs.h.

501  {
502  PSOut StdOut = TStdOut::New();
503  int MxNId = Graph->GetMxNId();
504  int NonNodeDepth = 2147483647; // INT_MAX
505  int InfDepth = 2147483646; // INT_MAX - 1
506  ShortestDists.Gen(MxNId);
507  for (int NId = 0; NId < MxNId; NId++) {
508  if (Graph->IsNode(NId)) { ShortestDists[NId] = InfDepth; }
509  else { ShortestDists[NId] = NonNodeDepth; }
510  }
511 
512  TIntV Vec1(MxNId, 0); // ensure enough capacity
513  TIntV Vec2(MxNId, 0); // ensure enough capacity
514 
515  ShortestDists[StartNId] = 0;
516  TIntV* PCurV = &Vec1;
517  PCurV->Add(StartNId);
518  TIntV* PNextV = &Vec2;
519  int Depth = 0; // current depth
520  while (!PCurV->Empty()) {
521  Depth++; // increase depth
522  for (int i = 0; i < PCurV->Len(); i++) {
523  int NId = PCurV->GetVal(i);
524  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
525  for (int e = 0; e < NI.GetOutDeg(); e++) {
526  const int OutNId = NI.GetOutNId(e);
527  if (ShortestDists[OutNId].Val == InfDepth) {
528  ShortestDists[OutNId] = Depth;
529  PNextV->Add(OutNId);
530  }
531  }
532  }
533  // swap pointer, no copying
534  TIntV* Tmp = PCurV;
535  PCurV = PNextV;
536  PNextV = Tmp;
537  // clear next
538  PNextV->Reduce(0); // reduce length, does not initialize new array
539  }
540  return Depth-1;
541 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static TPt< TSOut > New()
Definition: fl.h:266
void Reduce(const TSizeTy &_Vals=-1)
Reduces the vector's length to _Vals elements, which must be less than the current length...
Definition: ds.h:556
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649
Definition: bd.h:196
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int TSnap::GetShortestDistancesMP2 ( const PGraph &  Graph,
const int &  StartNId,
const bool &  FollowOut,
const bool &  FollowIn,
TIntV ShortestDists 
)

Definition at line 545 of file bfsdfs.h.

545  {
546  int MxNId = Graph->GetMxNId();
547  int NonNodeDepth = 2147483647; // INT_MAX
548  int InfDepth = 2147483646; // INT_MAX - 1
549  ShortestDists.Gen(MxNId);
550  #pragma omp parallel for schedule(dynamic,10000)
551  for (int NId = 0; NId < MxNId; NId++) {
552  if (Graph->IsNode(NId)) { ShortestDists[NId] = InfDepth; }
553  else { ShortestDists[NId] = NonNodeDepth; }
554  }
555 
556  TIntV Vec1(MxNId, 0); // ensure enough capacity
557  TIntV Vec2(MxNId, 0); // ensure enough capacity
558 
559  ShortestDists[StartNId] = 0;
560  TIntV* PCurV = &Vec1;
561  PCurV->Add(StartNId);
562  TIntV* PNextV = &Vec2;
563  int Depth = 0; // current depth
564 
565  while (!PCurV->Empty()) {
566  Depth++; // increase depth
567  #pragma omp parallel for schedule(dynamic,10000)
568  for (int i = 0; i < PCurV->Len(); i++) {
569  int NId = PCurV->GetVal(i);
570  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
571  for (int e = 0; e < NI.GetOutDeg(); e++) {
572  const int OutNId = NI.GetOutNId(e);
573  if (__sync_bool_compare_and_swap(&(ShortestDists[OutNId].Val), InfDepth, Depth)) {
574  PNextV->AddMP(OutNId);
575  }
576  }
577  }
578 // #pragma omp parallel for schedule(dynamic,10000)
579 // for (int NId = 0; NId < MxNId; NId++) {
580 // if (ShortestDists[NId] == InfDepth) {
581 // typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
582 // for (int e = 0; e < NI.GetInDeg(); e++) {
583 // const int InNId = NI.GetInNId(e);
584 // if (ShortestDists[InNId] < Depth) {
585 // ShortestDists[NId] = Depth;
586 // PNextV->AddMP(NId);
587 // break;
588 // }
589 // }
590 // }
591 // }
592  // swap pointer, no copying
593  TIntV* Tmp = PCurV;
594  PCurV = PNextV;
595  PNextV = Tmp;
596  // clear next
597  PNextV->Reduce(0); // reduce length, does not initialize new array
598  }
599  return Depth-1;
600 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSizeTy AddMP(const TVal &Val)
Adds element Val at the end of the vector in a thread safe manner, returns the element index in the v...
Definition: ds.h:617
void Reduce(const TSizeTy &_Vals=-1)
Reduces the vector's length to _Vals elements, which must be less than the current length...
Definition: ds.h:556
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int TSnap::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.

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).

Definition at line 409 of file bfsdfs.h.

409  {
410  TBreathFS<PGraph> BFS(Graph);
411  BFS.DoBfs(SrcNId, true, ! IsDir, DstNId, TInt::Mx);
412  return BFS.GetHops(SrcNId, DstNId);
413 }
static const int Mx
Definition: dt.h:1142
template<class PGraph >
int TSnap::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.

Parameters
IsDirfalse: ignore edge directions and consider edges/paths as undirected (in case they are directed).
MaxDistMaximum number of hops that BFS expands to. This is helpful for speeding-up the code if one in interested only in nodes less than MaxDist away from SrcNId.
NIdToDistHMaps node ID to shortest path distance. NIdToDistH contains only nodes that are reachable from SrcNId.

Definition at line 400 of file bfsdfs.h.

400  {
401  TBreathFS<PGraph> BFS(Graph);
402  BFS.DoBfs(SrcNId, true, ! IsDir, -1, MaxDist);
403  NIdToDistH.Clr();
404  NIdToDistH.Swap(BFS.NIdDistH);
405  return NIdToDistH[NIdToDistH.Len()-1];
406 }
void Swap(THash &Hash)
Definition: hash.h:544
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
int Len() const
Definition: hash.h:228
void TSnap::GetSngVals ( const PNGraph Graph,
const int &  SngVals,
TFltV SngValV 
)

Computes largest SngVals singular values of the adjacency matrix representing a directed Graph.

Definition at line 175 of file gsvd.cpp.

175  {
176  const int Nodes = Graph->GetNodes();
177  IAssert(SngVals > 0);
178  if (Nodes < 100) {
179  // perform full SVD
180  TFltVV AdjMtx(Nodes+1, Nodes+1);
181  TFltVV LSingV, RSingV;
182  TIntH NodeIdH;
183  // create adjecency matrix
184  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
185  NodeIdH.AddKey(NodeI.GetId()); }
186  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
187  const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1;
188  for (int e = 0; e < NodeI.GetOutDeg(); e++) {
189  const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges
190  if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
191  }
192  }
193  try { // can fail to converge but results seem to be good
194  TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); }
195  catch(...) {
196  printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); }
197  } else {
198  // Lanczos
199  TNGraphMtx GraphMtx(Graph);
200  int CalcVals = int(2*SngVals);
201  //if (CalcVals > Nodes) { CalcVals = int(2*Nodes); }
202  //if (CalcVals > Nodes) { CalcVals = Nodes; }
203  //while (SngValV.Len() < SngVals && CalcVals < 10*SngVals) {
204  try {
205  if (SngVals > 4) {
206  TSparseSVD::SimpleLanczosSVD(GraphMtx, 2*SngVals, SngValV, false); }
207  else { TFltVV LSingV, RSingV; // this is much more precise, but also much slower
208  TSparseSVD::LanczosSVD(GraphMtx, SngVals, 3*SngVals, ssotFull, SngValV, LSingV, RSingV); }
209  }
210  catch(...) {
211  printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*SngVals, SngValV.Len()); }
212  if (SngValV.Len() < SngVals) {
213  printf(" ***TRIED %d GOT %d values** \n", CalcVals, SngValV.Len()); }
214  // CalcVals += SngVals;
215  //}
216  }
217  SngValV.Sort(false);
218  //if (SngValV.Len() > SngVals) {
219  // SngValV.Del(SngVals, SngValV.Len()-1); }
220  //else {
221  // while (SngValV.Len() < SngVals) SngValV.Add(1e-6); }
222  //IAssert(SngValV.Len() == SngVals);
223 }
#define IAssert(Cond)
Definition: bd.h:262
static void SimpleLanczosSVD(const TMatrix &Matrix, const int &CalcSV, TFltV &SngValV, const bool &DoLocalReortoP=false)
Definition: linalg.cpp:1440
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static void LanczosSVD(const TMatrix &Matrix, int NumSV, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &SgnValV, TFltVV &LeftSgnVecVV, TFltVV &RightSgnVecVV)
Definition: linalg.cpp:1454
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
Definition: gsvd.h:5
static void Svd1Based(const TFltVV &InMtx1, TFltVV &LSingV, TFltV &SingValV, TFltVV &RSingV)
Definition: xmath.cpp:1252
int GetKeyId(const TKey &Key) const
Definition: hash.h:466
int AddKey(const TKey &Key)
Definition: hash.h:373
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
void TSnap::GetSngVec ( const PNGraph Graph,
TFltV LeftSV,
TFltV RightSV 
)

Computes the leading left and right singular vector of the adjacency matrix representing a directed Graph.

Definition at line 225 of file gsvd.cpp.

225  {
226  const int Nodes = Graph->GetNodes();
227  TFltVV LSingV, RSingV;
228  TFltV SngValV;
229  if (Nodes < 500) {
230  // perform full SVD
231  TFltVV AdjMtx(Nodes+1, Nodes+1);
232  TIntH NodeIdH;
233  // create adjecency matrix
234  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
235  NodeIdH.AddKey(NodeI.GetId()); }
236  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
237  const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1;
238  for (int e = 0; e < NodeI.GetOutDeg(); e++) {
239  const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges
240  if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
241  }
242  }
243  try { // can fail to converge but results seem to be good
244  TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); }
245  catch(...) {
246  printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); }
247  } else { // Lanczos
248  TNGraphMtx GraphMtx(Graph);
249  TSparseSVD::LanczosSVD(GraphMtx, 1, 8, ssotFull, SngValV, LSingV, RSingV);
250  }
251  TFlt MxSngVal = TFlt::Mn;
252  int ValN = 0;
253  for (int i = 0; i < SngValV.Len(); i++) {
254  if (MxSngVal < SngValV[i]) { MxSngVal = SngValV[i]; ValN = i; } }
255  LSingV.GetCol(ValN, LeftSV);
256  RSingV.GetCol(ValN, RightSV);
257  IsAllValVNeg(LeftSV, true);
258  IsAllValVNeg(RightSV, true);
259 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool IsAllValVNeg(TFltV &ValV, const bool &InvertSign)
Definition: gsvd.cpp:163
static void LanczosSVD(const TMatrix &Matrix, int NumSV, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &SgnValV, TFltVV &LeftSgnVecVV, TFltVV &RightSgnVecVV)
Definition: linalg.cpp:1454
Definition: dt.h:1386
Definition: gsvd.h:5
static void Svd1Based(const TFltVV &InMtx1, TFltVV &LSingV, TFltV &SingValV, TFltVV &RSingV)
Definition: xmath.cpp:1252
int GetKeyId(const TKey &Key) const
Definition: hash.h:466
int AddKey(const TKey &Key)
Definition: hash.h:373
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2390
static const double Mn
Definition: dt.h:1390
void TSnap::GetSngVec ( const PNGraph Graph,
const int &  SngVecs,
TFltV SngValV,
TFltVFltV LeftSV,
TFltVFltV RightSV 
)

Computes the singular values and left and right singular vectors of the adjacency matrix representing a directed Graph.

Parameters
SngVecsNumber of singular values/vectors to compute.

Definition at line 261 of file gsvd.cpp.

261  {
262  const int Nodes = Graph->GetNodes();
263  SngValV.Clr();
264  LeftSV.Clr();
265  RightSV.Clr();
266  TFltVV LSingV, RSingV;
267  if (Nodes < 100) {
268  // perform full SVD
269  TFltVV AdjMtx(Nodes+1, Nodes+1);
270  TIntH NodeIdH;
271  // create adjecency matrix (1-based)
272  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
273  NodeIdH.AddKey(NodeI.GetId()); }
274  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
275  const int NodeId = NodeIdH.GetKeyId(NodeI.GetId())+1;
276  for (int e = 0; e < NodeI.GetOutDeg(); e++) {
277  const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e))+1; // no self edges
278  if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
279  }
280  }
281  try { // can fail to converge but results seem to be good
282  TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV);
283  } catch(...) {
284  printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges());
285  }
286  } else { // Lanczos
287  TNGraphMtx GraphMtx(Graph);
288  TSparseSVD::LanczosSVD(GraphMtx, SngVecs, 2*SngVecs, ssotFull, SngValV, LSingV, RSingV);
289  //TGAlg::SaveFullMtx(Graph, "adj_mtx.txt");
290  //TLAMisc::DumpTFltVVMjrSubMtrx(LSingV, LSingV.GetRows(), LSingV.GetCols(), "LSingV2.txt"); // save MTX
291  }
292  TFltIntPrV SngValIdV;
293  for (int i = 0; i < SngValV.Len(); i++) {
294  SngValIdV.Add(TFltIntPr(SngValV[i], i));
295  }
296  SngValIdV.Sort(false);
297  SngValV.Sort(false);
298  for (int v = 0; v < SngValIdV.Len(); v++) {
299  LeftSV.Add();
300  LSingV.GetCol(SngValIdV[v].Val2, LeftSV.Last());
301  RightSV.Add();
302  RSingV.GetCol(SngValIdV[v].Val2, RightSV.Last());
303  }
304  IsAllValVNeg(LeftSV[0], true);
305  IsAllValVNeg(RightSV[0], true);
306 }
TPair< TFlt, TInt > TFltIntPr
Definition: ds.h:97
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool IsAllValVNeg(TFltV &ValV, const bool &InvertSign)
Definition: gsvd.cpp:163
static void LanczosSVD(const TMatrix &Matrix, int NumSV, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &SgnValV, TFltVV &LeftSgnVecVV, TFltVV &RightSgnVecVV)
Definition: linalg.cpp:1454
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
Definition: gsvd.h:5
static void Svd1Based(const TFltVV &InMtx1, TFltVV &LSingV, TFltV &SingValV, TFltVV &RSingV)
Definition: xmath.cpp:1252
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
int GetKeyId(const TKey &Key) const
Definition: hash.h:466
int AddKey(const TKey &Key)
Definition: hash.h:373
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2390
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
PUNGraph TSnap::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.

The resulting subgraph contains all the nodes from Graph, which have node IDs in the NIdV vector and all the edges with both nodes in NIdV. Parameter RenumberNodes determines, whether the node IDs are preserved or not. If RenumberNodes is false, then nodes in the resulting subgraph have the same node IDs as nodes in Graph. If RenumberNodes is true, then nodes in the resulting subgraph are renumbered sequentially from 0 to N-1. By default, the nodes are not renumbered.

Definition at line 7 of file subgraph.cpp.

7  {
8  //if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
9  PUNGraph NewGraphPt = TUNGraph::New();
10  TUNGraph& NewGraph = *NewGraphPt;
11  NewGraph.Reserve(NIdV.Len(), -1);
12  TIntSet NIdSet(NIdV.Len());
13  for (int n = 0; n < NIdV.Len(); n++) {
14  if (Graph->IsNode(NIdV[n])) {
15  NIdSet.AddKey(NIdV[n]);
16  if (! RenumberNodes) { NewGraph.AddNode(NIdV[n]); }
17  else { NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n])); }
18  }
19  }
20  if (! RenumberNodes) {
21  for (int n = 0; n < NIdSet.Len(); n++) {
22  const int SrcNId = NIdSet[n];
23  const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
24  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
25  const int OutNId = NI.GetOutNId(edge);
26  if (NIdSet.IsKey(OutNId)) {
27  NewGraph.AddEdge(SrcNId, OutNId); }
28  }
29  }
30  } else {
31  for (int n = 0; n < NIdSet.Len(); n++) {
32  const int SrcNId = NIdSet[n];
33  const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
34  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
35  const int OutNId = NI.GetOutNId(edge);
36  if (NIdSet.IsKey(OutNId)) {
37  NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId)); }
38  }
39  }
40  }
41  return NewGraphPt;
42 }
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
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:94
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 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 GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:106
Definition: bd.h:196
template<class PGraph >
PGraph TSnap::GetSubGraph ( const PGraph &  Graph,
const TIntV NIdV 
)

Returns an induced subgraph of graph Graph with NIdV nodes.

The resulting subgraph contains all the nodes from Graph, which have node IDs in the NIdV vector and all the edges with both nodes in NIdV. Node IDs are preserved. Nodes in the resulting subgraph have the same node IDs as nodes in Graph.

Definition at line 232 of file subgraph.h.

232  {
233  return TSnapDetail::TGetSubGraph<PGraph, HasGraphFlag(typename PGraph::TObj, gfMultiGraph)>
234  ::Do(Graph, NIdV);
235 }
PNGraph TSnap::GetSubGraph ( const PNGraph Graph,
const TIntV NIdV,
const bool &  RenumberNodes 
)

Definition at line 45 of file subgraph.cpp.

45  {
46  //if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
47  PNGraph NewGraphPt = TNGraph::New();
48  TNGraph& NewGraph = *NewGraphPt;
49  NewGraph.Reserve(NIdV.Len(), -1);
50  TIntSet NIdSet(NIdV.Len());
51  for (int n = 0; n < NIdV.Len(); n++) {
52  if (Graph->IsNode(NIdV[n])) {
53  NIdSet.AddKey(NIdV[n]);
54  if (! RenumberNodes) { NewGraph.AddNode(NIdV[n]); }
55  else { NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n])); }
56  }
57  }
58  if (! RenumberNodes) {
59  for (int n = 0; n < NIdSet.Len(); n++) {
60  const int SrcNId = NIdSet[n];
61  const TNGraph::TNodeI NI = Graph->GetNI(SrcNId);
62  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
63  const int OutNId = NI.GetOutNId(edge);
64  if (NIdSet.IsKey(OutNId)) {
65  NewGraph.AddEdge(SrcNId, OutNId); }
66  }
67  }
68  } else {
69  for (int n = 0; n < NIdSet.Len(); n++) {
70  const int SrcNId = NIdSet[n];
71  const TNGraph::TNodeI NI = Graph->GetNI(SrcNId);
72  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
73  const int OutNId = NI.GetOutNId(edge);
74  if (NIdSet.IsKey(OutNId)) {
75  NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId)); }
76  }
77  }
78  }
79  return NewGraphPt;
80 }
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
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 AddKey(const TKey &Key)
Definition: shash.h:1254
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
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 GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:416
template<class PGraph >
PGraph TSnap::GetSubGraphRenumber ( const PGraph &  Graph,
const TIntV NIdV 
)

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

The resulting subgraph contains all the nodes from Graph, which have node IDs in the NIdV vector and all the edges with both nodes in NIdV. Node IDs are preserved. Nodes in the resulting subgraph have the same node IDs as nodes in Graph.

Definition at line 238 of file subgraph.h.

238  {
239  return GetSubGraph(Graph, NIdV, true);
240 }
PGraph GetSubGraph(const PGraph &Graph, const TIntV &NIdV)
Returns an induced subgraph of graph Graph with NIdV nodes.
Definition: subgraph.h:232
template<class PGraph >
int TSnap::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.

Definition at line 363 of file bfsdfs.h.

363  {
364  TBreathFS<PGraph> BFS(Graph);
365  BFS.DoBfs(StartNId, FollowOut, FollowIn, -1, TInt::Mx);
366  TreeSz = BFS.NIdDistH.Len();
367  TreeDepth = 0;
368  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
369  TreeDepth = TMath::Mx(TreeDepth, BFS.NIdDistH[i].Val);
370  }
371  return TreeSz;
372 }
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
static const int Mx
Definition: dt.h:1142
template<class PGraph >
int TSnap::GetTreeRootNId ( const PGraph &  Graph)

Definition at line 80 of file alg.h.

80 { int RootNId; bool Tree; Tree = IsTree(Graph, RootNId); Assert(Tree); return RootNId; }
bool IsTree(const PGraph &Graph, int &RootNIdX)
Definition: alg.h:460
#define Assert(Cond)
Definition: bd.h:251
template<class PGraph >
void TSnap::GetTreeSig ( const PGraph &  Graph,
const int &  RootNId,
TIntV Sig 
)

Definition at line 484 of file alg.h.

484  {
485  CAssert(HasGraphFlag(typename PGraph::TObj, gfDirected));
486  Sig.Gen(Graph->GetNodes(), 0);
487  TSnapQueue<int> NIdQ(Graph->GetNodes());
488  NIdQ.Push(RootNId);
489  int LastPos = 0, NodeCnt = 1;
490  while (! NIdQ.Empty()) {
491  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
492  IAssert(Node.GetInDeg()==0 || Node.GetOutDeg()==0); // child points or is-pointed to by the parent
493  if (Node.GetInDeg() != 0) {
494  for (int e = 0; e < Node.GetInDeg(); e++) {
495  NIdQ.Push(Node.GetInNId(e)); }
496  } else if (Node.GetOutDeg() != 0) {
497  for (int e = 0; e < Node.GetOutDeg(); e++) {
498  NIdQ.Push(Node.GetOutNId(e)); }
499  }
500  Sig.Add(Node.GetInDeg());
501  if (--NodeCnt == 0) {
502  for (int i = LastPos; i < Sig.Len(); i++) NodeCnt += Sig[i];
503  Sig.QSort(LastPos, Sig.Len()-1, false);
504  LastPos = Sig.Len();
505  }
506  }
507 }
#define IAssert(Cond)
Definition: bd.h:262
void QSort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Quick sorts the values between positions MnLValN...MxLValN.
Definition: ds.h:1305
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
#define CAssert(Cond)
Definition: bd.h:302
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::GetTreeSig ( const PGraph &  Graph,
const int &  RootNId,
TIntV Sig,
TIntPrV NodeMap 
)

Definition at line 511 of file alg.h.

511  {
512  CAssert(HasGraphFlag(typename PGraph::TObj, gfDirected));
513  NodeMap.Gen(Graph->GetNodes(), 0);
514  Sig.Gen(Graph->GetNodes(), 0);
515  TSnapQueue<int> NIdQ(Graph->GetNodes());
516  NIdQ.Push(RootNId);
517  int LastPos = 0, NodeCnt = 1;
518  while (! NIdQ.Empty()) {
519  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
520  IAssert(Node.GetInDeg()==0 || Node.GetOutDeg()==0); // child points or is-pointed to by the parent
521  if (Node.GetInDeg() != 0) {
522  for (int e = 0; e < Node.GetInDeg(); e++) {
523  NIdQ.Push(Node.GetInNId(e)); }
524  NodeMap.Add(TIntPr(Node.GetInDeg(), Node.GetId()));
525  } else if (Node.GetOutDeg() != 0) {
526  for (int e = 0; e < Node.GetOutDeg(); e++) {
527  NIdQ.Push(Node.GetOutNId(e)); }
528  NodeMap.Add(TIntPr(Node.GetOutDeg(), Node.GetId()));
529  }
530  if (--NodeCnt == 0) {
531  for (int i = LastPos; i < NodeMap.Len(); i++) {
532  NodeCnt += NodeMap[i].Val1; }
533  NodeMap.QSort(LastPos, NodeMap.Len()-1, false);
534  LastPos = NodeMap.Len();
535  }
536  }
537  for (int i = 0; i < NodeMap.Len(); i++) {
538  Sig.Add(NodeMap[i].Val1); // degree dignature
539  NodeMap[i].Val1 = NodeMap[i].Val2;
540  NodeMap[i].Val2 = i;
541  }
542 }
#define IAssert(Cond)
Definition: bd.h:262
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void QSort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Quick sorts the values between positions MnLValN...MxLValN.
Definition: ds.h:1305
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
#define CAssert(Cond)
Definition: bd.h:302
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int TSnap::GetTriadEdges ( const PGraph &  Graph,
int  SampleEdges = -1 
)

Counts the number of edges that participate in at least one triad.

Considers the graph as undirected.

Parameters
SampleNodesIf !=-1 then compute triads only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 579 of file triad.h.

579  {
580  const bool IsDir = Graph->HasFlag(gfDirected);
581  TIntSet NbrH;
582  int TriadEdges = 0;
583  for(typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
584  NbrH.Clr(false);
585  for (int e = 0; e < NI.GetOutDeg(); e++) {
586  if (NI.GetOutNId(e) != NI.GetId()) {
587  NbrH.AddKey(NI.GetOutNId(e)); }
588  }
589  if (IsDir) {
590  for (int e = 0; e < NI.GetInDeg(); e++) {
591  if (NI.GetInNId(e) != NI.GetId()) {
592  NbrH.AddKey(NI.GetInNId(e)); }
593  }
594  }
595  for (int e = 0; e < NI.GetOutDeg(); e++) {
596  if (!IsDir && NI.GetId()<NI.GetOutNId(e)) { continue; } // for undirected graphs count each edge only once
597  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NI.GetOutNId(e));
598  bool Triad=false;
599  for (int e1 = 0; e1 < SrcNode.GetOutDeg(); e1++) {
600  if (NbrH.IsKey(SrcNode.GetOutNId(e1))) { Triad=true; break; }
601  }
602  if (IsDir && ! Triad) {
603  for (int e1 = 0; e1 < SrcNode.GetInDeg(); e1++) {
604  if (NbrH.IsKey(SrcNode.GetInNId(e1))) { Triad=true; break; }
605  }
606  }
607  if (Triad) { TriadEdges++; }
608  }
609  }
610  return TriadEdges;
611 }
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Definition: shash.h:1243
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
template<class PGraph >
void TSnap::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).

Considers the graph as undirected.

Definition at line 697 of file triad.h.

697  {
698  TIntH TriadCntH;
699  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
700  const int Triads = GetNodeTriads(Graph, NI.GetId());
701  TriadCntH.AddDat(Triads) += 1;
702  }
703  TriadCntH.GetKeyDatPrV(TriadCntV);
704  TriadCntV.Sort();
705 }
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.
Definition: triad.h:660
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:500
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
int64 TSnap::GetTriads ( const PGraph &  Graph,
int  SampleNodes = -1 
)

Returns the number of triangles in a graph.

The function returns the number of unique triples of connected nodes (regardless of the number of edges between each pair of nodes). In other words, the function consideres the Graph as a simple undirected graph.

Parameters
SampleNodesIf !=-1 then compute triads only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 240 of file triad.h.

240  {
241  int64 OpenTriads, ClosedTriads;
242  return GetTriads(Graph, ClosedTriads, OpenTriads, SampleNodes);
243 }
void GetTriads(const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
Computes the number of open and close triads for every node of the network.
Definition: triad.h:318
long long int64
Definition: bd.h:27
template<class PGraph >
void TSnap::GetTriads ( const PGraph &  Graph,
TIntTrV NIdCOTriadV,
int  SampleNodes = -1 
)

Computes the number of open and close triads for every node of the network.

Considers the graph as undirected.

Parameters
NIdCOTriadVTriple (node id, open triads: number of pairs of node's neighbors that are not connected, closed triads: number of pairs of node's neighbors that are connected between themselves).
SampleNodesIf !=-1 then compute triads only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 318 of file triad.h.

318  {
319  const bool IsDir = Graph->HasFlag(gfDirected);
320  TIntSet NbrH;
321  TIntV NIdV;
322  //TRnd Rnd(0);
323  TRnd Rnd(1);
324  int NNodes;
325  TIntV Nbrs;
326  int NId;
327 
328  int64 hcount;
329 
330  hcount = 0;
331 
332  NNodes = Graph->GetNodes();
333  Graph->GetNIdV(NIdV);
334  NIdV.Shuffle(Rnd);
335  if (SampleNodes == -1) {
336  SampleNodes = NNodes;
337  }
338 
339  int MxId = -1;
340  for (int i = 0; i < NNodes; i++) {
341  if (NIdV[i] > MxId) {
342  MxId = NIdV[i];
343  }
344  }
345 
346  TVec<TIntV> NbrV(MxId + 1);
347 
348  if (IsDir) {
349  // get in and out neighbors
350  for (int node = 0; node < NNodes; node++) {
351  int NId = NIdV[node];
352  NbrV[NId] = TIntV();
353  GetUniqueNbrV(Graph, NId, NbrV[NId]);
354  }
355  } else {
356  // get only out neighbors
357  for (int node = 0; node < NNodes; node++) {
358  int NId = NIdV[node];
359  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
360  NbrV[NId] = TIntV();
361  NbrV[NId].Reserve(NI.GetOutDeg());
362  NbrV[NId].Reduce(0);
363  for (int i = 0; i < NI.GetOutDeg(); i++) {
364  NbrV[NId].Add(NI.GetOutNId(i));
365  }
366  }
367  }
368 
369  NIdCOTriadV.Clr(false);
370  NIdCOTriadV.Reserve(SampleNodes);
371  for (int node = 0; node < SampleNodes; node++) {
372  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[node]);
373  int NLen;
374 
375  NId = NI.GetId();
376  hcount++;
377  if (NI.GetDeg() < 2) {
378  NIdCOTriadV.Add(TIntTr(NId, 0, 0)); // zero triangles
379  continue;
380  }
381 
382  Nbrs = NbrV[NId];
383  NLen = Nbrs.Len();
384 
385  // count connected neighbors
386  int OpenCnt1 = 0, CloseCnt1 = 0;
387  for (int srcNbr = 0; srcNbr < NLen; srcNbr++) {
388  int Count = GetCommon(NbrV[NbrV[NId][srcNbr]],Nbrs);
389  CloseCnt1 += Count;
390  }
391  CloseCnt1 /= 2;
392  OpenCnt1 = (NLen*(NLen-1))/2 - CloseCnt1;
393  NIdCOTriadV.Add(TIntTr(NId, CloseCnt1, OpenCnt1));
394  }
395 }
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...
Definition: triad.h:783
Definition: dt.h:11
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
long long int64
Definition: bd.h:27
int GetCommon(TIntV &A, TIntV &B)
Returns the number of common elements in two sorted TInt vectors.
Definition: triad.cpp:59
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
TVec< TInt > TIntV
Definition: ds.h:1594
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:171
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int64 TSnap::GetTriads ( const PGraph &  Graph,
int64 ClosedTriadsX,
int64 OpenTriadsX,
int  SampleNodes = -1 
)

Computes the number of Closed and Open triads.

Considers the graph as undirected.

Parameters
SampleNodesIf !=-1 then compute triads only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 246 of file triad.h.

246  {
247  TIntTrV NIdCOTriadV;
248  GetTriads(Graph, NIdCOTriadV, SampleNodes);
249  uint64 closedTriads = 0;
250  uint64 openTriads = 0;
251  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
252  closedTriads += NIdCOTriadV[i].Val2;
253  openTriads += NIdCOTriadV[i].Val3;
254  }
255  //IAssert(closedTriads/3 < (uint64) TInt::Mx);
256  //IAssert(openTriads < (uint64) TInt::Mx);
257  ClosedTriads = int64(closedTriads/3); // each triad is counted 3 times
258  OpenTriads = int64(openTriads);
259  return ClosedTriads;
260 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
unsigned long long uint64
Definition: bd.h:38
void GetTriads(const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
Computes the number of open and close triads for every node of the network.
Definition: triad.h:318
long long int64
Definition: bd.h:27
template<class PGraph >
void TSnap::GetTriads_v0 ( const PGraph &  Graph,
TIntTrV NIdCOTriadV,
int  SampleNodes 
)

Definition at line 270 of file triad.h.

270  {
271  const bool IsDir = Graph->HasFlag(gfDirected);
272  TIntSet NbrH;
273  TIntV NIdV;
274  TRnd Rnd(0);
275 
276  Graph->GetNIdV(NIdV);
277  NIdV.Shuffle(Rnd);
278  if (SampleNodes == -1) {
279  SampleNodes = Graph->GetNodes(); }
280  NIdCOTriadV.Clr(false);
281  NIdCOTriadV.Reserve(SampleNodes);
282  for (int node = 0; node < SampleNodes; node++) {
283  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[node]);
284  if (NI.GetDeg() < 2) {
285  NIdCOTriadV.Add(TIntTr(NI.GetId(), 0, 0)); // zero triangles
286  continue;
287  }
288  // find neighborhood
289  NbrH.Clr(false);
290  for (int e = 0; e < NI.GetOutDeg(); e++) {
291  if (NI.GetOutNId(e) != NI.GetId()) {
292  NbrH.AddKey(NI.GetOutNId(e)); }
293  }
294  if (IsDir) {
295  for (int e = 0; e < NI.GetInDeg(); e++) {
296  if (NI.GetInNId(e) != NI.GetId()) {
297  NbrH.AddKey(NI.GetInNId(e)); }
298  }
299  }
300  // count connected neighbors
301  int OpenCnt=0, CloseCnt=0;
302  for (int srcNbr = 0; srcNbr < NbrH.Len(); srcNbr++) {
303  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrH.GetKey(srcNbr));
304  for (int dstNbr = srcNbr+1; dstNbr < NbrH.Len(); dstNbr++) {
305  const int dstNId = NbrH.GetKey(dstNbr);
306  if (SrcNode.IsNbrNId(dstNId)) { CloseCnt++; } // is edge
307  else { OpenCnt++; }
308  }
309  }
310  IAssert(2*(OpenCnt+CloseCnt) == NbrH.Len()*(NbrH.Len()-1));
311  NIdCOTriadV.Add(TIntTr(NI.GetId(), CloseCnt, OpenCnt));
312  }
313 }
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Definition: shash.h:1243
#define IAssert(Cond)
Definition: bd.h:262
Definition: dt.h:11
const TKey & GetKey(const int &KeyId) const
Definition: shash.h:1141
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
int Len() const
Definition: shash.h:1121
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:171
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
int64 TSnap::GetTriadsAll ( const PGraph &  Graph,
int64 ClosedTriadsX,
int64 OpenTriadsX,
int  SampleNodes = -1 
)

Computes the number of Closed and Open triads.

Considers the graph as undirected. This function is a duplicate. It is required by Snap.py.

Parameters
SampleNodesIf !=-1 then compute triads only for a random sample of SampleNodes nodes. Useful for approximate but quick computations.

Definition at line 263 of file triad.h.

263  {
264  return GetTriads(Graph, ClosedTriads, OpenTriads, SampleNodes);
265 }
void GetTriads(const PGraph &Graph, TIntTrV &NIdCOTriadV, int SampleNodes=-1)
Computes the number of open and close triads for every node of the network.
Definition: triad.h:318
template<class PGraph >
int64 TSnap::GetTriangleCnt ( const PGraph &  Graph)

Returns the number of triangles in graph Graph.

Definition at line 454 of file triad.h.

454  {
455  const int NNodes = Graph->GetNodes();
456 
457  TIntV MapV(NNodes);
459  NV.Reduce(0);
460 
461  int MxId = -1;
462  int ind = 0;
463  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
464  NV.Add(NI);
465  int Id = NI.GetId();
466  if (Id > MxId) {
467  MxId = Id;
468  }
469  MapV[ind] = Id;
470  ind++;
471  }
472 
473  TIntV IndV(MxId+1);
474 
475  for (int j = 0; j < NNodes; j++) {
476  IndV[MapV[j]] = j;
477  }
478 
479  ind = MapV.Len();
480 
481  TVec<TIntV> HigherDegNbrV(ind);
482 
483  for (int i = 0; i < ind; i++) {
484  HigherDegNbrV[i] = TVec<TInt>();
485  HigherDegNbrV[i].Reserve(NV[i].GetDeg());
486  HigherDegNbrV[i].Reduce(0);
487  }
488 
489 #ifdef USE_OPENMP
490 #pragma omp parallel for schedule(dynamic)
491 #endif
492  for (int i = 0; i < ind; i++) {
493  typename PGraph::TObj::TNodeI NI = NV[i];
494  MergeNbrs<PGraph>(HigherDegNbrV[i], NI);
495 
496  int k = 0;
497  for (int j = 0; j < HigherDegNbrV[i].Len(); j++) {
498  TInt Vert = HigherDegNbrV[i][j];
499  TInt Deg = NV[IndV[Vert]].GetDeg();
500  if (Deg > NI.GetDeg() ||
501  (Deg == NI.GetDeg() && Vert > NI.GetId())) {
502  HigherDegNbrV[i][k] = Vert;
503  k++;
504  }
505  }
506  HigherDegNbrV[i].Reduce(k);
507  }
508 
509  int64 cnt = 0;
510 #ifdef USE_OPENMP
511 #pragma omp parallel for schedule(dynamic) reduction(+:cnt)
512 #endif
513  for (int i = 0; i < HigherDegNbrV.Len(); i++) {
514  for (int j = 0; j < HigherDegNbrV[i].Len(); j++) {
515  TInt NbrInd = IndV[HigherDegNbrV[i][j]];
516 
517  int64 num = GetCommon(HigherDegNbrV[i], HigherDegNbrV[NbrInd]);
518  cnt += num;
519  }
520  }
521 
522  return cnt;
523 }
Definition: dt.h:1137
long long int64
Definition: bd.h:27
int GetCommon(TIntV &A, TIntV &B)
Returns the number of common elements in two sorted TInt vectors.
Definition: triad.cpp:59
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
template<class PGraph >
PGraph TSnap::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).

Definition at line 345 of file alg.h.

345  {
346  PGraph NewGraphPt = PGraph::New();
347  *NewGraphPt = *Graph;
348  MakeUnDir(NewGraphPt);
349  return NewGraphPt;
350 }
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)...
Definition: alg.h:353
template<class PGraph >
void TSnap::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.

Definition at line 783 of file triad.h.

783  {
784  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
785  NbrV.Reserve(NI.GetDeg());
786  NbrV.Reduce(0);
787 
788  int j = 0;
789  int k = 0;
790  int Prev = -1;
791  int InDeg = NI.GetInDeg();
792  int OutDeg = NI.GetOutDeg();
793  if (InDeg > 0 && OutDeg > 0) {
794  int v1 = NI.GetInNId(j);
795  int v2 = NI.GetOutNId(k);
796  while (1) {
797  if (v1 <= v2) {
798  if (Prev != v1) {
799  if (v1 != NId) {
800  NbrV.Add(v1);
801  Prev = v1;
802  }
803  }
804  j += 1;
805  if (j >= InDeg) {
806  break;
807  }
808  v1 = NI.GetInNId(j);
809  } else {
810  if (Prev != v2) {
811  if (v2 != NId) {
812  NbrV.Add(v2);
813  }
814  Prev = v2;
815  }
816  k += 1;
817  if (k >= OutDeg) {
818  break;
819  }
820  v2 = NI.GetOutNId(k);
821  }
822  }
823  }
824  while (j < InDeg) {
825  int v = NI.GetInNId(j);
826  if (Prev != v) {
827  if (v != NId) {
828  NbrV.Add(v);
829  }
830  Prev = v;
831  }
832  j += 1;
833  }
834  while (k < OutDeg) {
835  int v = NI.GetOutNId(k);
836  if (Prev != v) {
837  if (v != NId) {
838  NbrV.Add(v);
839  }
840  Prev = v;
841  }
842  k += 1;
843  }
844 }
void Reduce(const TSizeTy &_Vals=-1)
Reduces the vector's length to _Vals elements, which must be less than the current length...
Definition: ds.h:556
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::GetWccs ( const PGraph &  Graph,
TCnComV CnComV 
)

Returns all weakly connected components in a Graph.

Parameters
CnComVis a vector of connected components. Each component is defined by the IDs of its member nodes.

Definition at line 376 of file cncom.h.

376  {
377  typename PGraph::TObj::TNodeI NI;
378  THashSet<TInt> VisitedNId(Graph->GetNodes()+1);
379  TSnapQueue<int> NIdQ(Graph->GetNodes()+1);
380  TIntV CcNIdV;
381  CnComV.Clr(); CcNIdV.Gen(1);
382  // zero degree nodes
383  for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
384  if (NI.GetDeg() == 0) {
385  const int NId = NI.GetId();
386  VisitedNId.AddKey(NId);
387  CcNIdV[0] = NId; CnComV.Add(CcNIdV);
388  }
389  }
390  // the rest of the nodes
391  for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
392  const int NId = NI.GetId();
393  if (! VisitedNId.IsKey(NId)) {
394  VisitedNId.AddKey(NId);
395  NIdQ.Clr(false); NIdQ.Push(NId);
396  CcNIdV.Clr(); CcNIdV.Add(NId);
397  while (! NIdQ.Empty()) {
398  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
399  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
400  for (int e = 0; e < Node.GetInDeg(); e++) {
401  const int InNId = Node.GetInNId(e);
402  if (! VisitedNId.IsKey(InNId)) {
403  NIdQ.Push(InNId); VisitedNId.AddKey(InNId); CcNIdV.Add(InNId); }
404  }
405  }
406  for (int e = 0; e < Node.GetOutDeg(); e++) {
407  const int OutNId = Node.GetOutNId(e);
408  if (! VisitedNId.IsKey(OutNId)) {
409  NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); CcNIdV.Add(OutNId); }
410  }
411  }
412  CcNIdV.Sort(true);
413  CnComV.Add(TCnCom(CcNIdV)); // add wcc comoponent
414  }
415  }
416  CnComV.Sort(false);
417 }
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Definition: shash.h:1243
void Gen(const int &ExpectVals)
Definition: shash.h:1115
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
Definition: cncom.h:88
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
#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
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::GetWccSzCnt ( const PGraph &  Graph,
TIntPrV WccSzCnt 
)

Returns a distribution of weakly connected component sizes.

Parameters
WccSzCntreturns a set of pairs (number of nodes in the component, number of such components)

Definition at line 337 of file cncom.h.

337  {
338  THashSet<TInt> VisitedNId(Graph->GetNodes());
339  TIntH SzToCntH;
340  TSnapQueue<int> NIdQ(Graph->GetNodes()+1);
341  typename PGraph::TObj::TNodeI NI;
342  int Cnt = 0;
343  // zero degree nodes
344  for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
345  if (NI.GetDeg() == 0) { Cnt++; VisitedNId.AddKey(NI.GetId()); }
346  }
347  if (Cnt > 0) SzToCntH.AddDat(1, Cnt);
348  // the rest of the nodes
349  for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
350  if (! VisitedNId.IsKey(NI.GetId())) {
351  VisitedNId.AddKey(NI.GetId());
352  NIdQ.Clr(false); NIdQ.Push(NI.GetId());
353  Cnt = 0;
354  while (! NIdQ.Empty()) {
355  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
356  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
357  for (int e = 0; e < Node.GetInDeg(); e++) {
358  const int InNId = Node.GetInNId(e);
359  if (! VisitedNId.IsKey(InNId)) { NIdQ.Push(InNId); VisitedNId.AddKey(InNId); }
360  }
361  }
362  for (int e = 0; e < Node.GetOutDeg(); e++) {
363  const int OutNId = Node.GetOutNId(e);
364  if (! VisitedNId.IsKey(OutNId)) { NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); }
365  }
366  Cnt++;
367  }
368  SzToCntH.AddDat(Cnt) += 1;
369  }
370  }
371  SzToCntH.GetKeyDatPrV(WccSzCnt);
372  WccSzCnt.Sort(true);
373 }
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
#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
void TSnap::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.

Definition at line 752 of file centr.cpp.

752  {
753  if (DoNodeCent) { NodeBtwH.Clr(); }
754  if (DoEdgeCent) { EdgeBtwH.Clr(); }
755  const int nodes = Graph->GetNodes();
756  TIntS S(nodes);
757  TIntQ Q(nodes);
758  TIntIntVH P(nodes); // one vector for every node
759  TIntFltH delta(nodes);
760  TIntFltH sigma(nodes), d(nodes);
761  // init
762  for (PNEANet::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
763  if (DoNodeCent) {
764  NodeBtwH.AddDat(NI.GetId(), 0); }
765  if (DoEdgeCent) {
766  for (int e = 0; e < NI.GetOutDeg(); e++) {
767  if (Graph->HasFlag(gfDirected) && IsDir) {
768  // add all outgoing edges for directed graphs
769  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
770  } else {
771  // add each edge only once in undirected graphs
772  if (NI.GetId() < NI.GetOutNId(e)) {
773  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
774  }
775  }
776  }
777  // add incoming edges in directed graphs that were not added yet
778  if (Graph->HasFlag(gfDirected) && !IsDir) {
779  for (int e = 0; e < NI.GetInDeg(); e++) {
780  if (NI.GetId() < NI.GetInNId(e) &&
781  !Graph->IsEdge(NI.GetId(), NI.GetInNId(e))) {
782  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetInNId(e)), 0);
783  }
784  }
785  }
786  }
787  sigma.AddDat(NI.GetId(), 0);
788  d.AddDat(NI.GetId(), -1);
789  P.AddDat(NI.GetId(), TIntV());
790  delta.AddDat(NI.GetId(), 0);
791  }
792  // calc betweeness
793  for (int k=0; k < BtwNIdV.Len(); k++) {
794  const PNEANet::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
795  // reset
796  for (int i = 0; i < sigma.Len(); i++) {
797  sigma[i]=0; d[i]=-1; delta[i]=0; P[i].Clr(false);
798  }
799  S.Clr(false);
800  Q.Clr(false);
801  sigma.AddDat(NI.GetId(), 1);
802  d.AddDat(NI.GetId(), 0);
803  Q.Push(NI.GetId());
804  while (! Q.Empty()) {
805  const int v = Q.Top(); Q.Pop();
806  const PNEANet::TObj::TNodeI NI2 = Graph->GetNI(v);
807  S.Push(v);
808  const double VDat = d.GetDat(v);
809  // iterate over all outgoing edges
810  for (int e = 0; e < NI2.GetOutDeg(); e++) {
811  const int w = NI2.GetOutNId(e);
812  const int eid = NI2.GetOutEId(e);
813 
814  if (d.GetDat(w) < 0) { // find w for the first time
815  Q.Push(w);
816  d.AddDat(w, VDat+Attr[eid]);
817  }
818  //shortest path to w via v ?
819  if (d.GetDat(w) == VDat+Attr[eid]) {
820  sigma.AddDat(w) += sigma.GetDat(v);
821  P.GetDat(w).Add(v);
822  }
823  }
824  // if ignoring direction in directed networks, iterate over incoming edges
825  if (Graph->HasFlag(gfDirected) && !IsDir) {
826  for (int e = 0; e < NI2.GetInDeg(); e++) {
827  const int w = NI2.GetInNId(e);
828  // skip neighbors that are also outgoing
829  if (Graph->IsEdge(NI2.GetId(), w)) {
830  continue;
831  }
832  const int eid = NI2.GetInEId(e);
833 
834  if (d.GetDat(w) < 0) { // find w for the first time
835  Q.Push(w);
836  d.AddDat(w, VDat+Attr[eid]);
837  }
838  //shortest path to w via v ?
839  if (d.GetDat(w) == VDat+Attr[eid]) {
840  sigma.AddDat(w) += sigma.GetDat(v);
841  P.GetDat(w).Add(v);
842  }
843  }
844  }
845  }
846 
847  while (! S.Empty()) {
848  const int w = S.Top();
849  const double SigmaW = sigma.GetDat(w);
850  const double DeltaW = delta.GetDat(w);
851  const TIntV NIdV = P.GetDat(w);
852  S.Pop();
853  for (int i = 0; i < NIdV.Len(); i++) {
854  const int NId = NIdV[i];
855  const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
856  delta.AddDat(NId) += c;
857  if (DoEdgeCent) {
858  if (Graph->HasFlag(gfDirected) && IsDir) {
859  EdgeBtwH.AddDat(TIntPr(NId, w)) += c;
860  } else {
861  EdgeBtwH.AddDat(TIntPr(TMath::Mn(NId, w), TMath::Mx(NId, w))) += c;
862  }
863  }
864  }
865  if (DoNodeCent && w != NI.GetId()) {
866  NodeBtwH.AddDat(w) += delta.GetDat(w)/2.0; }
867  }
868  }
869 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Clr()
Definition: bd.h:502
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:838
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
TVec< TInt > TIntV
Definition: ds.h:1594
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void TSnap::GetWeightedBetweennessCentr ( const PNEANet  Graph,
TIntFltH NIdBtwH,
TIntPrFltH EdgeBtwH,
const TFltV Attr,
const double &  NodeFrac = 1.0,
const bool &  IsDir = false 
)

Computes (approximate) weighted Node and Edge Beetweenness Centrality based on a sample of NodeFrac nodes.

Parameters
NIdBtwHhash table mapping node ids to their corresponding betweenness centrality values.
EdgeBtwHhash table mapping edges (pairs of node ids) to their corresponding betweenness centrality values.
NodeFracquality of approximation. NodeFrac=1.0 gives exact betweenness values.

Definition at line 871 of file centr.cpp.

871  {
872  TIntV NIdV; Graph->GetNIdV(NIdV);
873  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
874  NIdV.Shuffle(TInt::Rnd);
875  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
876  NIdV.DelLast(); }
877  }
878  GetWeightedBetweennessCentr(Graph, NIdV, NodeBtwH, true, EdgeBtwH, true,
879  Attr, IsDir);
880 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetWeightedBetweennessCentr(const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const double &NodeFrac, const bool &IsDir)
Definition: centr.cpp:894
static TRnd Rnd
Definition: dt.h:1146
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
void DelLast()
Removes the last element of the vector.
Definition: ds.h:665
void TSnap::GetWeightedBetweennessCentr ( const PNEANet  Graph,
TIntFltH NIdBtwH,
const TFltV Attr,
const double &  NodeFrac = 1.0,
const bool &  IsDir = false 
)

Computes (approximate) weighted Node Beetweenness Centrality based on a sample of NodeFrac nodes.

Parameters
NIdBtwHhash table mapping node ids to their corresponding betweenness centrality values.
NodeFracquality of approximation. NodeFrac=1.0 gives exact betweenness values.

Definition at line 882 of file centr.cpp.

882  {
883  TIntPrFltH EdgeBtwH;
884  TIntV NIdV; Graph->GetNIdV(NIdV);
885  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
886  NIdV.Shuffle(TInt::Rnd);
887  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
888  NIdV.DelLast(); }
889  }
890  GetWeightedBetweennessCentr(Graph, NIdV, NodeBtwH, true, EdgeBtwH, false,
891  Attr, IsDir);
892 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetWeightedBetweennessCentr(const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const double &NodeFrac, const bool &IsDir)
Definition: centr.cpp:894
static TRnd Rnd
Definition: dt.h:1146
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
void DelLast()
Removes the last element of the vector.
Definition: ds.h:665
void TSnap::GetWeightedBetweennessCentr ( const PNEANet  Graph,
TIntPrFltH EdgeBtwH,
const TFltV Attr,
const double &  NodeFrac = 1.0,
const bool &  IsDir = false 
)

Computes (approximate) weighted Edge Beetweenness Centrality based on a sample of NodeFrac nodes.

Parameters
EdgeBtwHhash table mapping edges (pairs of node ids) to their corresponding betweenness centrality values.
NodeFracquality of approximation. NodeFrac=1.0 gives exact betweenness values.

Definition at line 894 of file centr.cpp.

894  {
895  TIntFltH NodeBtwH;
896  TIntV NIdV; Graph->GetNIdV(NIdV);
897  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
898  NIdV.Shuffle(TInt::Rnd);
899  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
900  NIdV.DelLast(); }
901  }
902  GetWeightedBetweennessCentr(Graph, NIdV, NodeBtwH, false, EdgeBtwH, true,
903  Attr, IsDir);
904 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void GetWeightedBetweennessCentr(const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const double &NodeFrac, const bool &IsDir)
Definition: centr.cpp:894
static TRnd Rnd
Definition: dt.h:1146
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
void DelLast()
Removes the last element of the vector.
Definition: ds.h:665
double TSnap::GetWeightedClosenessCentr ( const PNEANet  Graph,
const int &  NId,
const TFltV Attr,
const bool &  Normalized = true,
const bool &  IsDir = false 
)

Returns Closeness centrality of a given node NId. Closeness centrality of a node is defined as 1/FarnessCentrality.

Definition at line 745 of file centr.cpp.

745  {
746  const double Farness = GetWeightedFarnessCentr(Graph, NId, Attr, Normalized, IsDir);
747  if (Farness != 0.0) { return 1.0/Farness; }
748  else { return 0.0; }
749  return 0.0;
750 }
double GetWeightedFarnessCentr(const PNEANet Graph, const int &NId, const TFltV &Attr, const bool &Normalized, const bool &IsDir)
Definition: centr.cpp:726
double TSnap::GetWeightedFarnessCentr ( const PNEANet  Graph,
const int &  NId,
const TFltV Attr,
const bool &  Normalized = true,
const bool &  IsDir = false 
)

Returns weighted Farness centrality of a given node NId. Farness centrality of a node is the average shortest path length to all other nodes that reside is the same connected component as the given node.

Definition at line 726 of file centr.cpp.

726  {
727  TIntFltH NDistH(Graph->GetNodes());
728 
729  GetWeightedShortestPath(Graph, NId, NDistH, Attr);
730 
731  double sum = 0;
732  for (TIntFltH::TIter I = NDistH.BegI(); I < NDistH.EndI(); I++) {
733  sum += I->Dat();
734  }
735  if (NDistH.Len() > 1) {
736  double centr = sum/double(NDistH.Len()-1);
737  if (Normalized) {
738  centr *= (Graph->GetNodes() - 1)/double(NDistH.Len()-1);
739  }
740  return centr;
741  }
742  else { return 0.0; }
743 }
int GetWeightedShortestPath(const PNEANet Graph, const int &SrcNId, TIntFltH &NIdDistH, const TFltV &Attr)
Definition: centr.cpp:700
THKeyDat * EndI
Definition: hash.h:54
int TSnap::GetWeightedPageRank ( const PNEANet  Graph,
TIntFltH PRankH,
const TStr Attr,
const double &  C,
const double &  Eps,
const int &  MaxIter 
)

Weighted PageRank (TODO: Use template)

Definition at line 396 of file centr.cpp.

396  {
397  if (!Graph->IsFltAttrE(Attr)) return -1;
398 
399  TFltV Weights = Graph->GetFltAttrVecE(Attr);
400 
401  int mxid = Graph->GetMxNId();
402  TFltV OutWeights(mxid);
403  Graph->GetWeightOutEdgesV(OutWeights, Weights);
404 
405  const int NNodes = Graph->GetNodes();
406  //const double OneOver = 1.0/double(NNodes);
407  PRankH.Gen(NNodes);
408  for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
409  PRankH.AddDat(NI.GetId(), 1.0/NNodes);
410  //IAssert(NI.GetId() == PRankH.GetKey(PRankH.Len()-1));
411  }
412  TFltV TmpV(NNodes);
413  for (int iter = 0; iter < MaxIter; iter++) {
414  int j = 0;
415  for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
416  TmpV[j] = 0;
417  for (int e = 0; e < NI.GetInDeg(); e++) {
418  const int InNId = NI.GetInNId(e);
419  const TFlt OutWeight = OutWeights[InNId];
420  int EId = Graph->GetEId(InNId, NI.GetId());
421  const TFlt Weight = Weights[Graph->GetFltKeyIdE(EId)];
422  if (OutWeight > 0) {
423  TmpV[j] += PRankH.GetDat(InNId) * Weight / OutWeight; }
424  }
425  TmpV[j] = C*TmpV[j]; // Berkhin (the correct way of doing it)
426  //TmpV[j] = C*TmpV[j] + (1.0-C)*OneOver; // iGraph
427  }
428  double diff=0, sum=0, NewVal;
429  for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
430  const double Leaked = (1.0-sum) / double(NNodes);
431  for (int i = 0; i < PRankH.Len(); i++) { // re-instert leaked PageRank
432  NewVal = TmpV[i] + Leaked; // Berkhin
433  //NewVal = TmpV[i] / sum; // iGraph
434  diff += fabs(NewVal-PRankH[i]);
435  PRankH[i] = NewVal;
436  }
437  if (diff < Eps) { break; }
438  }
439  return 0;
440 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
Definition: dt.h:1386
void Gen(const int &ExpectVals)
Definition: hash.h:222
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
int TSnap::GetWeightedPageRankMP ( const PNEANet  Graph,
TIntFltH PRankH,
const TStr Attr,
const double &  C,
const double &  Eps,
const int &  MaxIter 
)

Definition at line 443 of file centr.cpp.

443  {
444  if (!Graph->IsFltAttrE(Attr)) return -1;
445  const int NNodes = Graph->GetNodes();
447 
448  //const double OneOver = 1.0/double(NNodes);
449  PRankH.Gen(NNodes);
450  int MxId = 0;
451 
452  for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
453  NV.Add(NI);
454  PRankH.AddDat(NI.GetId(), 1.0/NNodes);
455  int Id = NI.GetId();
456  if (Id > MxId) {
457  MxId = Id;
458  }
459  }
460 
461  TFltV PRankV(MxId+1);
462  TFltV OutWeights(MxId+1);
463 
464  TFltV Weights = Graph->GetFltAttrVecE(Attr);
465 
466  #pragma omp parallel for schedule(dynamic,10000)
467  for (int j = 0; j < NNodes; j++) {
468  TNEANet::TNodeI NI = NV[j];
469  int Id = NI.GetId();
470  OutWeights[Id] = Graph->GetWeightOutEdges(NI, Attr);
471  PRankV[Id] = 1/NNodes;
472  }
473 
474  TFltV TmpV(NNodes);
475  for (int iter = 0; iter < MaxIter; iter++) {
476 
477  #pragma omp parallel for schedule(dynamic,10000)
478  for (int j = 0; j < NNodes; j++) {
479  TNEANet::TNodeI NI = NV[j];
480  TFlt Tmp = 0;
481  for (int e = 0; e < NI.GetInDeg(); e++) {
482  const int InNId = NI.GetInNId(e);
483 
484  const TFlt OutWeight = OutWeights[InNId];
485 
486  int EId = Graph->GetEId(InNId, NI.GetId());
487  const TFlt Weight = Weights[Graph->GetFltKeyIdE(EId)];
488 
489  if (OutWeight > 0) {
490  Tmp += PRankH.GetDat(InNId) * Weight / OutWeight;
491  }
492  }
493  TmpV[j] = C*Tmp; // Berkhin (the correct way of doing it)
494  //TmpV[j] = C*TmpV[j] + (1.0-C)*OneOver; // iGraph
495  }
496 
497  double sum = 0;
498  #pragma omp parallel for reduction(+:sum) schedule(dynamic,10000)
499  for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
500  const double Leaked = (1.0-sum) / double(NNodes);
501 
502  double diff = 0;
503  #pragma omp parallel for reduction(+:diff) schedule(dynamic,10000)
504  for (int i = 0; i < NNodes; i++) {
505  TNEANet::TNodeI NI = NV[i];
506  double NewVal = TmpV[i] + Leaked; // Berkhin
507  //NewVal = TmpV[i] / sum; // iGraph
508  int Id = NI.GetId();
509  diff += fabs(NewVal-PRankV[Id]);
510  PRankV[Id] = NewVal;
511  }
512  if (diff < Eps) { break; }
513  }
514 
515  #pragma omp parallel for schedule(dynamic,10000)
516  for (int i = 0; i < NNodes; i++) {
517  TNEANet::TNodeI NI = NV[i];
518  PRankH[i] = PRankV[NI.GetId()];
519  }
520 
521  return 0;
522 }
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
Definition: dt.h:1386
int GetId() const
Returns ID of the current node.
Definition: network.h:1807
void Gen(const int &ExpectVals)
Definition: hash.h:222
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
int TSnap::GetWeightedShortestPath ( const PNEANet  Graph,
const int &  SrcNId,
TIntFltH NIdDistH,
const TFltV Attr 
)

Dijkstra Algorithm For more info see: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

Definition at line 700 of file centr.cpp.

701  {
702  TIntV frontier;
703 
704  NIdDistH.Clr(false); NIdDistH.AddDat(SrcNId, 0);
705  frontier.Add(SrcNId);
706  while (! frontier.Empty()) {
707  const int NId = findMinimum(frontier, NIdDistH);
708  const PNEANet::TObj::TNodeI NodeI = Graph->GetNI(NId);
709  for (int v = 0; v < NodeI.GetOutDeg(); v++) {
710  int DstNId = NodeI.GetOutNId(v);
711  int EId = NodeI.GetOutEId(v);
712 
713  if (! NIdDistH.IsKey(DstNId)) {
714  NIdDistH.AddDat(DstNId, NIdDistH.GetDat(NId) + Attr[EId]);
715  frontier.Add(DstNId);
716  } else {
717  if (NIdDistH.GetDat(DstNId) > NIdDistH.GetDat(NId) + Attr[EId]) {
718  NIdDistH.GetDat(DstNId) = NIdDistH.GetDat(NId) + Attr[EId];
719  }
720  }
721  }
722  }
723  return 0;
724 }
int findMinimum(TIntV &Frontier, TIntFltH &NIdDistH)
Definition: centr.cpp:685
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
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
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void TSnap::GlobalRelabel ( PNEANet Net,
TPRManager PRM,
const int &  SrcNId,
const int &  SnkNId 
)

Implements the Global Relabeling heuristic.

Since labels reflect an estimate of the distance from a node to the sink node, every now and then the Global Relabel heuristic will be run. This BFS over the residual network starting from the sink and updates each nodes label as the distance to the sink. Unreacheable nodes from the sink have their labels set to N, where N is the number of nodes.

Definition at line 363 of file flow.cpp.

363  {
364  TIntQ NodeQ;
365  int size = Net->GetMxNId();
366  TIntV NodeV(size);
367  for (int i = 0; i < size; i++) { NodeV[i] = 0; }
368  NodeQ.Push(SnkNId);
369  NodeV[SnkNId] = 1;
370  int MaxLabel = PRM.GetMaxLabel();
371  while (!NodeQ.Empty()) {
372  // Backward search
373  int NId = NodeQ.Top(); NodeQ.Pop();
374  const TNEANet::TNodeI &NI = Net->GetNI(NId);
375  // Check all edges that point out of the current node for those over which flow can be returned.
376  for (int EdgeN = 0; EdgeN < NI.GetOutDeg(); EdgeN++) {
377  int OutNId = NI.GetOutNId(EdgeN);
378  int EId = NI.GetOutEId(EdgeN);
379  if (!NodeV[OutNId] && PRM.Flow(EId) > 0) {
380  NodeV[OutNId] = 1;
381  NodeQ.Push(OutNId);
382  PRM.SetLabel(OutNId, PRM.Label(NId) + 1);
383  }
384  }
385  // Check all edges that point into the current node for those over which flow can be added.
386  for (int EdgeN = 0; EdgeN < NI.GetInDeg(); EdgeN++) {
387  int InNId = NI.GetInNId(EdgeN);
388  int EId = NI.GetInEId(EdgeN);
389  if (!NodeV[InNId] && PRM.Capacity(EId) > PRM.Flow(EId)) {
390  NodeV[InNId] = 1;
391  NodeQ.Push(InNId);
392  PRM.SetLabel(InNId, PRM.Label(NId) + 1);
393  }
394  }
395  }
396 
397  for (TNEANet::TNodeI NI = Net->BegNI(); NI != Net->EndNI(); NI++) {
398  int NId = NI.GetId();
399  if (NodeV[NId]) {
400  if (PRM.Excess(NId) > 0 && PRM.Label(NId) < MaxLabel && NId != SnkNId) {
401  if (!PRM.IsActive(NId)) { PRM.PushActive(NId); }
402  }
403  } else {
404  if (PRM.IsActive(NId)) { PRM.RemoveActive(NId); }
405  PRM.SetLabel(NId, MaxLabel);
406  }
407  }
408 }
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
bool Empty() const
Definition: ds.h:2646
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: network.h:1833
void Pop()
Definition: ds.h:2650
int GetId() const
Returns ID of the current node.
Definition: network.h:1807
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
const TVal & Top() const
Definition: ds.h:2648
void Push(const TVal &Val)
Definition: ds.h:2653
double TSnap::Infomap ( PUNGraph Graph,
TCnComV CmtyV 
)

Rosvall-Bergstrom community detection algorithm based on information theoretic approach. See: Rosvall M., Bergstrom C. T., Maps of random walks on complex networks reveal community structure, Proc. Natl. Acad. Sci. USA 105, 1118-1123 (2008)

Definition at line 339 of file cmty.cpp.

339  {
340 
341  TIntFltH PAlpha; // probability of visiting node alpha
342  TIntH Module; // module of each node
343  TIntFltH Qi; // probability of leaving each module
344 
345  double SumPAlphaLogPAlpha = 0.0;
346  int Br = 0;
347  const int e = Graph->GetEdges();
348 
349  // initial values
350  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
351  int nodeId = NI.GetId();
352  int nodeDeg = NI.GetDeg();
353  float d = ((float)nodeDeg / (float)(2 * e));
354  PAlpha.AddDat(nodeId, d);
355  SumPAlphaLogPAlpha += d*log(d);
356  Module.AddDat(nodeId, Br);
357  Qi.AddDat(Br, 1.0);
358  Br += 1;
359  }
360 
361  double MinCodeLength = TSnapDetail::Equation(PAlpha, SumPAlphaLogPAlpha, Qi);
362  double NewCodeLength, PrevIterationCodeLength = 0.0;
363  int OldModule, NewModule;
364 
365  TIntV nodes;
366  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
367  nodes.Add(NI.GetId());
368 
369  do {
370  PrevIterationCodeLength = MinCodeLength;
371  TRnd rnd;
372  rnd.Randomize();
373  nodes.Shuffle(rnd);
374  for (int ndcounter = 0; ndcounter<nodes.Len(); ndcounter++) {
375  MinCodeLength = TSnapDetail::Equation(PAlpha, SumPAlphaLogPAlpha, Qi);
376  int nodeId = nodes[ndcounter];
377  TUNGraph::TNodeI NI = Graph->GetNI(nodeId);
378  for (int i = 0; i<NI.GetDeg(); i++) {
379 
380  OldModule = Module.GetDat(nodeId);
381  NewModule = Module.GetDat(NI.GetNbrNId(i));
382 
383  if (OldModule != NewModule){
384 
385  Module.AddDat(nodeId, NewModule);
386 
387  TSnapDetail::MapEquationNew2Modules(Graph, Module, Qi, OldModule, NewModule);
388  NewCodeLength = TSnapDetail::Equation(PAlpha, SumPAlphaLogPAlpha, Qi);
389  if (NewCodeLength<MinCodeLength) {
390  MinCodeLength = NewCodeLength;
391  OldModule = NewModule;
392  }
393  else {
394  Module.AddDat(nodeId, OldModule);
395  }
396  }
397  }
398  }
399  } while (MinCodeLength<PrevIterationCodeLength);
400 
401  Module.SortByDat(true);
402 
403  int Mod = -1;
404  for (int i = 0; i<Module.Len(); i++) {
405  if (Module[i]>Mod){
406  Mod = Module[i];
407  TCnCom t;
408  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
409  if (Module.GetDat(NI.GetId()) == Mod)
410  t.Add(NI.GetId());
411  }
412  CmtyV.Add(t);
413  }
414  }
415 
416  return MinCodeLength;
417 }
void Randomize()
Definition: dt.h:60
void Add(const int &NodeId)
Definition: cncom.h:104
Definition: dt.h:11
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
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
Definition: cncom.h:88
void MapEquationNew2Modules(PUNGraph &Graph, TIntH &Module, TIntFltH &Qi, int a, int b)
Definition: cmty.cpp:54
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
int GetId() const
Returns ID of the current node.
Definition: graph.h:88
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
double Equation(TIntFltH &PAlpha, double &SumPAlphaLogPAlpha, TIntFltH &Qi)
Definition: cmty.cpp:109
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
double TSnap::InfomapOnline ( PUNGraph Graph,
int  n1,
int  n2,
TIntFltH PAlpha,
double &  SumPAlphaLogPAlpha,
TIntFltH Qi,
TIntH Module,
int &  Br,
TCnComV CmtyV 
)

Definition at line 419 of file cmty.cpp.

419  {
420 
421  double MinCodeLength = TSnapDetail::InfomapOnlineIncrement(Graph, n1, n2, PAlpha, SumPAlphaLogPAlpha, Qi, Module, Br);
422 
423  Module.SortByDat(true);
424 
425  int Mod = -1;
426  for (int i = 0; i<Module.Len(); i++) {
427  if (Module[i]>Mod){
428  Mod = Module[i];
429  TCnCom t;
430  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
431  if (Module.GetDat(NI.GetId()) == Mod)
432  t.Add(NI.GetId());
433  }
434  CmtyV.Add(t);
435  }
436  }
437 
438  return MinCodeLength;
439 }
void Add(const int &NodeId)
Definition: cncom.h:104
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
Definition: cncom.h:88
double InfomapOnlineIncrement(PUNGraph &Graph, int n1, int n2, TIntFltH &PAlpha, double &SumPAlphaLogPAlpha, TIntFltH &Qi, TIntH &Module, int &Br)
Definition: cmty.cpp:214
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
void SortByDat(const bool &Asc=true)
Definition: hash.h:292
int TSnap::Intersect ( TUNGraph::TNodeI  Node,
TIntH  NNodes 
)

Intersect.

Definition at line 584 of file centr.cpp.

584  {
585  int br=0;
586  for (int i=0; i<Node.GetDeg(); i++)
587  {
588  if (NNodes.IsKey(Node.GetNbrNId(i)))
589  br++;
590  }
591  if (NNodes.IsKey(Node.GetId()))
592  br++;
593 
594  return br;
595 }
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
int GetId() const
Returns ID of the current node.
Definition: graph.h:88
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int TSnap::Intersect ( TUNGraph::TNodeI  Node,
TStr  NNodes 
)

Intersect.

Definition at line 597 of file centr.cpp.

597  {
598  int br=0;
599 
600  TInt digi = -1;
601  TStr buf = "";
602 
603  for (int i=0; i<Node.GetDeg(); i++)
604  {
605  digi = Node.GetNbrNId(i);
606  TStr buf = digi.GetStr();
607 
608  if (NNodes.IsStrIn(buf.CStr()))
609  br++;
610  }
611 
612  digi = Node.GetId();
613  buf = digi.GetStr();
614 
615  if (NNodes.IsStrIn(buf.CStr()))
616  br++;
617 
618  return br;
619 }
TStr GetStr() const
Definition: dt.h:1200
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
Definition: dt.h:1137
Definition: dt.h:412
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
char * CStr()
Definition: dt.h:479
int GetId() const
Returns ID of the current node.
Definition: graph.h:88
bool IsStrIn(const TStr &Str) const
Definition: dt.h:557
int TSnap::Intersect ( TUNGraph::TNodeI  Node,
int *  NNodes,
int  NNodes_br 
)

Intersect.

Definition at line 621 of file centr.cpp.

621  {
622  int br = 0;
623  int neig;
624  for (int i=0; i<Node.GetDeg(); i++)
625  {
626  neig = Node.GetNbrNId(i);
627  for (int j=0; j<NNodes_br; j++)
628  {
629  if (neig == NNodes[j])
630  {
631  br++;
632  j = NNodes_br;
633  }
634  }
635  }
636 
637  neig = Node.GetId();
638  for (int j=0; j<NNodes_br; j++)
639  {
640  if (neig == NNodes[j])
641  {
642  br++;
643  j = NNodes_br;
644  }
645  }
646 
647  return br;
648 }
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
int GetId() const
Returns ID of the current node.
Definition: graph.h:88
int TSnap::Intersect1 ( TUNGraph::TNodeI  Node,
TStr  NNodes 
)

Definition at line 650 of file centr.cpp.

650  {
651  int br=0;
652  for (int i=0; i<Node.GetDeg(); i++)
653  {
654  TInt digi = Node.GetNbrNId(i);
655  TStr buf = "";
656  buf = digi.GetStr();
657 
658  if (NNodes.SearchStr(buf.CStr())!=-1)
659  br++;
660  }
661 
662  TInt digi = Node.GetId();
663  TStr buf = digi.GetStr();
664 
665  if (NNodes.SearchStr(buf.CStr())!=-1)
666  br++;
667 
668  return br;
669 }
TStr GetStr() const
Definition: dt.h:1200
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
int SearchStr(const TStr &Str, const int &BChN=0) const
Definition: dt.cpp:1065
Definition: dt.h:1137
Definition: dt.h:412
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
char * CStr()
Definition: dt.h:479
int GetId() const
Returns ID of the current node.
Definition: graph.h:88
int TSnap::IntFlowBiDBFS ( const PNEANet Net,
const int &  CapIndex,
TIntV Flow,
TIntQ FwdNodeQ,
TIntH PredEdgeH,
TIntQ BwdNodeQ,
TIntH SuccEdgeH,
const int &  SrcNId,
const int &  SnkNId 
)

Definition at line 4 of file flow.cpp.

4  {
5  FwdNodeQ.Push(SrcNId);
6  PredEdgeH.AddDat(SrcNId, -1);
7  BwdNodeQ.Push(SnkNId);
8  SuccEdgeH.AddDat(SnkNId, -1);
9  while (!FwdNodeQ.Empty() && !BwdNodeQ.Empty()) {
10  // Forward search
11  const TNEANet::TNodeI &FwdNI = Net->GetNI(FwdNodeQ.Top()); FwdNodeQ.Pop();
12  // Check all edges that point into the current node for those over which flow can be returned.
13  for (int EdgeN = 0; EdgeN < FwdNI.GetInDeg(); EdgeN++) {
14  int NextNId = FwdNI.GetInNId(EdgeN);
15  int NextEId = FwdNI.GetInEId(EdgeN);
16  if (!PredEdgeH.IsKey(NextNId) && Flow[NextEId] > 0) {
17  PredEdgeH.AddDat(NextNId, NextEId);
18  if (SuccEdgeH.IsKey(NextNId)) {
19  return NextNId;
20  }
21  FwdNodeQ.Push(NextNId);
22  }
23  }
24  // Check all edges that point out of the current node for those over which flow can be added.
25  for (int EdgeN = 0; EdgeN < FwdNI.GetOutDeg(); EdgeN++) {
26  int NextNId = FwdNI.GetOutNId(EdgeN);
27  int NextEId = FwdNI.GetOutEId(EdgeN);
28  if (!PredEdgeH.IsKey(NextNId) && Net->GetIntAttrIndDatE(NextEId, CapIndex) > Flow[NextEId]) {
29  PredEdgeH.AddDat(NextNId, NextEId);
30  if (SuccEdgeH.IsKey(NextNId)) {
31  return NextNId;
32  }
33  FwdNodeQ.Push(NextNId);
34  }
35  }
36  // Backward search
37  const TNEANet::TNodeI &BwdNI = Net->GetNI(BwdNodeQ.Top()); BwdNodeQ.Pop();
38  // Check all edges that point out of the current node for those over which flow can be returned.
39  for (int EdgeN = 0; EdgeN < BwdNI.GetOutDeg(); EdgeN++) {
40  int PrevNId = BwdNI.GetOutNId(EdgeN);
41  int PrevEId = BwdNI.GetOutEId(EdgeN);
42  if (!SuccEdgeH.IsKey(PrevNId) && Flow[PrevEId] > 0) {
43  SuccEdgeH.AddDat(PrevNId, PrevEId);
44  if (PredEdgeH.IsKey(PrevNId)) {
45  return PrevNId;
46  }
47  BwdNodeQ.Push(PrevNId);
48  }
49  }
50  // Check all edges that point into the current node for those over which flow can be added.
51  for (int EdgeN = 0; EdgeN < BwdNI.GetInDeg(); EdgeN++) {
52  int PrevNId = BwdNI.GetInNId(EdgeN);
53  int PrevEId = BwdNI.GetInEId(EdgeN);
54  if (!SuccEdgeH.IsKey(PrevNId) && Net->GetIntAttrIndDatE(PrevEId, CapIndex) > Flow[PrevEId]) {
55  SuccEdgeH.AddDat(PrevNId, PrevEId);
56  if (PredEdgeH.IsKey(PrevNId)) {
57  return PrevNId;
58  }
59  BwdNodeQ.Push(PrevNId);
60  }
61  }
62  }
63  return -1;
64 }
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
bool Empty() const
Definition: ds.h:2646
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: network.h:1833
void Pop()
Definition: ds.h:2650
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
const TVal & Top() const
Definition: ds.h:2648
void Push(const TVal &Val)
Definition: ds.h:2653
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
bool TSnap::IsAllValVNeg ( TFltV ValV,
const bool &  InvertSign 
)

Definition at line 163 of file gsvd.cpp.

163  {
164  bool IsAllNeg=true;
165  for (int i = 0; i < ValV.Len(); i++) {
166  if (ValV[i]>0.0) { IsAllNeg=false; break; }
167  }
168  if (IsAllNeg && InvertSign) {
169  for (int i = 0; i < ValV.Len(); i++) {
170  ValV[i] = -ValV[i]; }
171  }
172  return IsAllNeg;
173 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
template<class PGraph >
bool TSnap::IsConnected ( const PGraph &  Graph)

Tests whether the Graph is (weakly) connected.

Definition at line 305 of file cncom.h.

305  {
306  return IsWeaklyConn(Graph);
307 }
bool IsWeaklyConn(const PGraph &Graph)
Tests whether the Graph is weakly connected.
Definition: cncom.h:310
template<class PGraph >
bool TSnap::IsTree ( const PGraph &  Graph,
int &  RootNIdX 
)

Definition at line 460 of file alg.h.

460  {
461  if (Graph->GetNodes() == 1 && Graph->GetEdges() == 0) {
462  RootNId = Graph->BegNI().GetId();
463  return true;
464  }
465  RootNId = -1;
466  if (Graph->GetNodes() != Graph->GetEdges()+1) { return false; }
467  int NZeroOutDeg = 0;
468  int ZeroOutDegN = -1;
469  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
470  if (NI.GetOutDeg() == 0) {
471  ZeroOutDegN = NI.GetId(); NZeroOutDeg++;
472  }
473  if (NI.GetDeg() == 0) { return false; } // isolated nodes
474  }
475  if (NZeroOutDeg==1) {
476  if (! TSnap::IsConnected(Graph)) { return false; }
477  RootNId = ZeroOutDegN; return true;
478  }
479  return false;
480 }
bool IsConnected(const PGraph &Graph)
Tests whether the Graph is (weakly) connected.
Definition: cncom.h:305
template<class PGraph >
bool TSnap::IsWeaklyConn ( const PGraph &  Graph)

Tests whether the Graph is weakly connected.

Definition at line 310 of file cncom.h.

310  {
311  if (Graph->Empty()) {
312  return true;
313  }
314  THashSet<TInt> VisitedNId(Graph->GetNodes());
315  TSnapQueue<int> NIdQ(Graph->GetNodes()+1);
316  typename PGraph::TObj::TNodeI NI;
317  // the rest of the nodes
318  NIdQ.Push(Graph->BegNI().GetId());
319  while (! NIdQ.Empty()) {
320  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
321  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
322  for (int e = 0; e < Node.GetInDeg(); e++) {
323  const int InNId = Node.GetInNId(e);
324  if (! VisitedNId.IsKey(InNId)) { NIdQ.Push(InNId); VisitedNId.AddKey(InNId); }
325  }
326  }
327  for (int e = 0; e < Node.GetOutDeg(); e++) {
328  const int OutNId = Node.GetOutNId(e);
329  if (! VisitedNId.IsKey(OutNId)) { NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); }
330  }
331  }
332  if (VisitedNId.Len() < Graph->GetNodes()) { return false; }
333  return true;
334 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
void Push(const TVal &Val)
Adds an element at the end of the queue.
Definition: gbase.h:214
template<class PGraph >
PGraph TSnap::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.

Loads Whitespace separated file of several columns: <source node="" id>=""> <destination node="" id1>=""> <destination node="" id2>="">

Whitespace separated file of several columns: <source node="" id>=""> <destination node="" id1>=""> <destination node="" id2>=""> ... First column of each line contains a source node id followed by ids of the destination nodes. For example, '1 2 3' encodes edges 1–>2 and 1–>3. Note that this format allows for saving isolated nodes.

Definition at line 169 of file gio.h.

169  {
170  TSsParser Ss(InFNm, ssfWhiteSep, true, true, true);
171  PGraph Graph = PGraph::TObj::New();
172  while (Ss.Next()) {
173  if (! Ss.IsInt(0)) { continue; }
174  const int SrcNId = Ss.GetInt(0);
175  if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
176  for (int dst = 1; dst < Ss.Len(); dst++) {
177  const int DstNId = Ss.GetInt(dst);
178  if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
179  Graph->AddEdge(SrcNId, DstNId);
180  }
181  }
182  Graph->Defrag();
183  return Graph;
184 }
Definition: ss.h:72
Whitespace (space or tab) separated.
Definition: ss.h:11
template<class PGraph >
PGraph TSnap::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.

Loads Whitespace separated file of several columns: <source node="" id>=""> <destination node="" id1>=""> <destination node="" id2>="">, with a mapping of strings to node IDs.

Whitespace separated file of several columns: <source node="" name>=""> <destination node name 1> <destination node name 2> ... First colum of each line contains a source node name followed by ids of the destination nodes. For example, 'A B C' encodes edges A–>B and A–>C. Note that this format allows for saving isolated nodes. stores the mapping from node names to node ids.

Definition at line 193 of file gio.h.

193  {
194  TSsParser Ss(InFNm, ssfWhiteSep, true, true, true);
195  PGraph Graph = PGraph::TObj::New();
196  while (Ss.Next()) {
197  const int SrcNId = StrToNIdH.AddDatId(Ss[0]);
198  if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
199  for (int dst = 1; dst < Ss.Len(); dst++) {
200  const int DstNId = StrToNIdH.AddDatId(Ss[dst]);
201  if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
202  Graph->AddEdge(SrcNId, DstNId);
203  }
204  }
205  Graph->Defrag();
206  return Graph;
207 }
Definition: ss.h:72
TDat & AddDatId(const char *Key)
Definition: hash.h:858
Whitespace (space or tab) separated.
Definition: ss.h:11
int TSnap::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.

Definition at line 69 of file conv.cpp.

71 {
72 
73  const TAttrType NodeType = Table->GetColType(SrcCol);
74  Assert(NodeType == Table->GetColType(DstCol));
75  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
76  const TInt DstColIdx = Table->GetColIdx(DstCol);
77 
78  // node values - i.e. the unique values of src/dst col
79  //THashSet<TInt> IntNodeVals; // for both int and string node attr types.
80  THash<TFlt, TInt> FltNodeVals;
81 
82  // make single pass over all rows in the table
83  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
84  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
85  continue;
86  }
87 
88  // add src and dst nodes to graph if they are not seen earlier
89  TInt SVal, DVal;
90  if (NodeType == atFlt) {
91  return -1;
92  } else if (NodeType == atInt || NodeType == atStr) {
93  if (NodeType == atInt) {
94  SVal = (Table->IntCols)[SrcColIdx][CurrRowIdx];
95  DVal = (Table->IntCols)[DstColIdx][CurrRowIdx];
96  } else {
97  SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
98  if (strlen(Table->GetContextKey(SVal)) == 0) { continue; } //illegal value
99  DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
100  if (strlen(Table->GetContextKey(DVal)) == 0) { continue; } //illegal value
101  }
102  }
103 
104  // add edge and edge attributes
105  if (Graph.AddEdge(SVal, DVal, CurrRowIdx) == -1) { return -1; }
106 
107  // Aggregate edge attributes and add to graph
108  for (TInt i = 0; i < EdgeAttrV.Len(); i++) {
109  TStr ColName = EdgeAttrV[i];
110  TAttrType T = Table->GetColType(ColName);
111  TInt Index = Table->GetColIdx(ColName);
112  switch (T) {
113  case atInt:
114  Graph.AddIntAttrDatE(CurrRowIdx, Table->IntCols[Index][CurrRowIdx], ColName);
115  break;
116  case atFlt:
117  Graph.AddFltAttrDatE(CurrRowIdx, Table->FltCols[Index][CurrRowIdx], ColName);
118  break;
119  case atStr:
120  Graph.AddStrAttrDatE(CurrRowIdx, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
121  break;
122  }
123  }
124  }
125  return 1;
126 }
int AddFltAttrDatE(const TCrossEdgeI &EdgeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: mmnet.h:410
int AddStrAttrDatE(const TCrossEdgeI &EdgeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: mmnet.h:407
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: gbase.h:23
int AddIntAttrDatE(const TCrossEdgeI &EdgeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: mmnet.h:404
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1137
int AddEdge(const int &sourceNId, const int &destNId, int EId=-1)
Adds an edge to the CrossNet; Mode1 NId should be the sourceNId always, regardless of whether edge is...
Definition: mmnet.cpp:233
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
Definition: gbase.h:23
int TSnap::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.

Definition at line 60 of file conv.cpp.

62 {
63  Graph->AddCrossNet(Mode1, Mode2, CrossName);
64  TCrossNet& Net = Graph->GetCrossNetByName(CrossName);
65  return LoadCrossNet(Net, Table, SrcCol, DstCol, EdgeAttrV);
66 }
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.
Definition: conv.cpp:69
Implements a single CrossNet consisting of edges between two TModeNets (could be the same TModeNet) ...
Definition: mmnet.h:133
PNGraph TSnap::LoadDyNet ( const TStr FNm)

For more info see ORA Network Analysis Data (http://www.casos.cs.cmu.edu/computational_tools/data2.php)

Loads a directed network in the DyNetML format. Loads only the first network in the file FNm.

Definition at line 296 of file gio.cpp.

296  {
298  THashSet<TStr> NIdStr;
299  while (XmlLx.GetSym()!=xsyEof) {
300  if (XmlLx.Sym==xsySTag && XmlLx.TagNm=="network") {
301  PNGraph G = TNGraph::New();
302  XmlLx.GetSym();
303  while (XmlLx.TagNm=="link") {
304  TStr Str1, Val1, Str2, Val2;
305  XmlLx.GetArg(0, Str1, Val1); XmlLx.GetArg(1, Str2, Val2);
306  IAssert(Str1=="source" && Str2=="target");
307  NIdStr.AddKey(Val1); NIdStr.AddKey(Val2);
308  const int src=NIdStr.GetKeyId(Val1);
309  const int dst=NIdStr.GetKeyId(Val2);
310  if (! G->IsNode(src)) { G->AddNode(src); }
311  if (! G->IsNode(dst)) { G->AddNode(dst); }
312  G->AddEdge(src, dst);
313  XmlLx.GetSym();
314  }
315  return G;
316  }
317  }
318  return PNGraph();
319 }
#define IAssert(Cond)
Definition: bd.h:262
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
Definition: xml.h:94
int GetKeyId(const TKey &Key) const
Definition: shash.h:1328
static bool IsZipFNm(const TStr &FNm)
Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).
Definition: zipfl.h:56
static PSIn New(const TStr &FNm)
Definition: zipfl.cpp:122
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
Definition: xml.h:98
TPt< TNGraph > PNGraph
Pointer to a directed graph (TNGraph)
Definition: graph.h:16
int AddKey(const TKey &Key)
Definition: shash.h:1254
Definition: xml.h:93
Definition: dt.h:412
Definition: bd.h:196
TVec< PNGraph > TSnap::LoadDyNetGraphV ( const TStr FNm)

For more info see ORA Network Analysis Data (http://www.casos.cs.cmu.edu/computational_tools/data2.php)

Loads directed networks in the DyNetML format. Loads all the networks in the file FNm.

Definition at line 322 of file gio.cpp.

322  {
324  TVec<PNGraph> GraphV;
325  THashSet<TStr> NIdStr;
326  while (XmlLx.GetSym()!=xsyEof) {
327  if (XmlLx.Sym==xsySTag && XmlLx.TagNm=="network") {
328  PNGraph G = TNGraph::New();
329  GraphV.Add(G);
330  XmlLx.GetSym();
331  while (XmlLx.TagNm=="link") {
332  TStr Str1, Val1, Str2, Val2;
333  XmlLx.GetArg(0, Str1, Val1); XmlLx.GetArg(1, Str2, Val2);
334  IAssert(Str1=="source" && Str2=="target");
335  NIdStr.AddKey(Val1); NIdStr.AddKey(Val2);
336  const int src=NIdStr.GetKeyId(Val1);
337  const int dst=NIdStr.GetKeyId(Val2);
338  if (! G->IsNode(src)) { G->AddNode(src); }
339  if (! G->IsNode(dst)) { G->AddNode(dst); }
340  G->AddEdge(src, dst);
341  XmlLx.GetSym();
342  }
343  }
344  }
345  return GraphV;
346 }
#define IAssert(Cond)
Definition: bd.h:262
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
Definition: xml.h:94
int GetKeyId(const TKey &Key) const
Definition: shash.h:1328
static bool IsZipFNm(const TStr &FNm)
Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).
Definition: zipfl.h:56
static PSIn New(const TStr &FNm)
Definition: zipfl.cpp:122
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
Definition: xml.h:98
int AddKey(const TKey &Key)
Definition: shash.h:1254
Definition: xml.h:93
Definition: dt.h:412
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
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
template<class PGraph >
PGraph TSnap::LoadEdgeList ( const TStr InFNm,
const int &  SrcColId,
const int &  DstColId 
)

Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line (whitespace separated columns, integer node ids).

Loads the format saved by TSnap::SaveEdgeList()

Whitespace separated file of several columns: ... <source node="" id>=""> ... <destination node="" id>=""> ... SrcColId and DstColId are column indexes of source/destination (integer!) node ids. This means there is one edge per line and node IDs are assumed to be integers.

Definition at line 84 of file gio.h.

84  {
85  TSsParser Ss(InFNm, ssfWhiteSep, true, true, true);
86  PGraph Graph = PGraph::TObj::New();
87  int SrcNId, DstNId;
88 
89  while (Ss.Next()) {
90  if (! Ss.GetInt(SrcColId, SrcNId) || ! Ss.GetInt(DstColId, DstNId)) { continue; }
91  if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
92  if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
93  Graph->AddEdge(SrcNId, DstNId);
94  }
95  Graph->Defrag();
96  return Graph;
97 }
Definition: ss.h:72
Whitespace (space or tab) separated.
Definition: ss.h:11
template<class PGraph >
PGraph TSnap::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).

Loads the format saved by TSnap::SaveEdgeList() if we set Separator=''.

'Separator' separated file of several columns: ... <source node="" id>=""> ... <destination node="" id>=""> ... SrcColId and DstColId are column indexes of source/destination (integer!) node ids. This means there is one edge per line and node IDs are assumed to be integers.

Definition at line 105 of file gio.h.

105  {
106  TSsParser Ss(InFNm, Separator);
107  PGraph Graph = PGraph::TObj::New();
108  int SrcNId, DstNId;
109  while (Ss.Next()) {
110  if (! Ss.GetInt(SrcColId, SrcNId) || ! Ss.GetInt(DstColId, DstNId)) { continue; }
111  if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
112  if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
113  Graph->AddEdge(SrcNId, DstNId);
114  }
115  Graph->Defrag();
116  return Graph;
117 }
Definition: ss.h:72
PNEANet TSnap::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).

Definition at line 138 of file gio.cpp.

138  {
139  PNEANet Graph = PNEANet::New();
140  TSsParser Ss(InFNm, Separator, true, false, false);
141  bool isSchemaLine = false;
142 
143  while (isSchemaLine || Ss.Next()) {
144  isSchemaLine = false;
145  if (Ss.GetFlds() == 0) continue;
146  if (NODES_START == Ss.GetFld(0)) {
147  // Map node attribute names to column number in the file.
148  TStrIntH IntAttrNVals;
149  TStrIntH FltAttrNVals;
150  TStrIntH StrAttrNVals;
151  int NColId = -1;
152  ReadNodeSchemaFromFile(Ss, Separator, NColId, IntAttrNVals, FltAttrNVals, StrAttrNVals);
153  isSchemaLine = ReadNodesFromFile(Ss, Separator, Graph, NColId, IntAttrNVals, FltAttrNVals, StrAttrNVals);
154  } else if (EDGES_START == Ss.GetFld(0)) {
155  // Map edge attribute names to column number in the file.
156  TStrIntH IntAttrEVals;
157  TStrIntH FltAttrEVals;
158  TStrIntH StrAttrEVals;
159  int SrcColId = -1;
160  int DstColId = -1;
161  ReadEdgeSchemaFromFile(Ss, Separator, SrcColId, DstColId, IntAttrEVals, FltAttrEVals, StrAttrEVals);
162  isSchemaLine = ReadEdgesFromFile(Ss, Separator, Graph, SrcColId, DstColId, IntAttrEVals, FltAttrEVals, StrAttrEVals);
163  }
164  }
165 
166  return Graph;
167 }
const TStr EDGES_START
Definition: gio.h:6
static TPt New()
Definition: bd.h:479
int ReadNodeSchemaFromFile(TSsParser &Ss, const char &Separator, int &NId, TStrIntH &IntAttrNVals, TStrIntH &FltAttrNVals, TStrIntH &StrAttrNVals)
Definition: gio.cpp:77
Definition: ss.h:72
bool ReadNodesFromFile(TSsParser &Ss, const char &Separator, PNEANet &Graph, int &NColId, TStrIntH &IntAttrNVals, TStrIntH &FltAttrNVals, TStrIntH &StrAttrNVals)
Definition: gio.cpp:105
int ReadEdgeSchemaFromFile(TSsParser &Ss, const char &Separator, int &SrcColId, int &DstColId, TStrIntH &IntAttrEVals, TStrIntH &FltAttrEVals, TStrIntH &StrAttrEVals)
Definition: gio.cpp:6
const TStr NODES_START
Definition: gio.h:7
Definition: bd.h:196
bool ReadEdgesFromFile(TSsParser &Ss, const char &Separator, PNEANet &Graph, int &SrcColId, int &DstColId, TStrIntH &IntAttrEVals, TStrIntH &FltAttrEVals, TStrIntH &StrAttrEVals)
Definition: gio.cpp:38
template<class PGraph >
PGraph TSnap::LoadEdgeListStr ( const TStr InFNm,
const int &  SrcColId,
const int &  DstColId 
)

Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line (whitespace separated columns, arbitrary string node ids).

Loads the format saved by TSnap::SaveEdgeList(), where node IDs are strings.

Whitespace separated file of several columns: ... <source node="" id>=""> ... <destination node="" id>=""> ... SrcColId and DstColId are column indexes of source/destination (string) node ids. This means there is one edge per line and node IDs can be arbitrary STRINGs. Note that the mapping of node names to ids is discarded.

Definition at line 126 of file gio.h.

126  {
127  TSsParser Ss(InFNm, ssfWhiteSep);
128  PGraph Graph = PGraph::TObj::New();
129  TStrHash<TInt> StrToNIdH(Mega(1), true); // hash-table mapping strings to integer node ids
130  while (Ss.Next()) {
131  const int SrcNId = StrToNIdH.AddKey(Ss[SrcColId]);
132  const int DstNId = StrToNIdH.AddKey(Ss[DstColId]);
133  if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
134  if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
135  Graph->AddEdge(SrcNId, DstNId);
136  }
137  Graph->Defrag();
138  return Graph;
139 }
Definition: ss.h:72
Whitespace (space or tab) separated.
Definition: ss.h:11
#define Mega(n)
Definition: gbase.h:4
Definition: hash.h:781
template<class PGraph >
PGraph TSnap::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).

Loads the format saved by TSnap::SaveEdgeList(), where node IDs are strings and mapping of strings to node ids are stored.

Whitespace separated file of several columns: ... <source node="" id>=""> ... <destination node="" id>=""> ... SrcColId and DstColId are column indexes of source/destination (string) node ids. This means there is one edge per line and node IDs can be arbitrary STRINGs. The mapping of strings to node ids is stored in StrToNIdH. To map between node names and ids use: NId = StrToNIdH.GetKeyId(NodeName) and TStr NodeName = StrToNIdH.GetKey(NId);

Definition at line 149 of file gio.h.

149  {
150  TSsParser Ss(InFNm, ssfWhiteSep);
151  PGraph Graph = PGraph::TObj::New();
152  while (Ss.Next()) {
153  const int SrcNId = StrToNIdH.AddKey(Ss[SrcColId]);
154  const int DstNId = StrToNIdH.AddKey(Ss[DstColId]);
155  if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
156  if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
157  Graph->AddEdge(SrcNId, DstNId);
158  }
159  Graph->Defrag();
160  return Graph;
161 }
Definition: ss.h:72
Whitespace (space or tab) separated.
Definition: ss.h:11
int AddKey(const char *Key)
Definition: hash.h:968
int TSnap::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.

Definition at line 14 of file conv.cpp.

15  {
16 
17  const TAttrType NodeType = Table->GetColType(NCol);
18  const TInt NColIdx = Table->GetColIdx(NCol);
19 
20  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
21  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
22  continue;
23  }
24 
25  // add src and dst nodes to graph if they are not seen earlier
26  TInt NVal;
27  if (NodeType == atFlt) {
28  return -1;
29  } else if (NodeType == atInt || NodeType == atStr) {
30  if (NodeType == atInt) {
31  NVal = (Table->IntCols)[NColIdx][CurrRowIdx];
32  } else {
33  NVal = (Table->StrColMaps)[NColIdx][CurrRowIdx];
34  if (strlen(Table->GetContextKey(NVal)) == 0) { continue; } //illegal value
35  }
36  if (!Graph.IsNode(NVal)) {Graph.AddNode(NVal); }
37  }
38 
39  // Aggregate edge attributes and add to graph
40  for (TInt i = 0; i < NodeAttrV.Len(); i++) {
41  TStr ColName = NodeAttrV[i];
42  TAttrType T = Table->GetColType(ColName);
43  TInt Index = Table->GetColIdx(ColName);
44  switch (T) {
45  case atInt:
46  Graph.AddIntAttrDatN(NVal, Table->IntCols[Index][CurrRowIdx], ColName);
47  break;
48  case atFlt:
49  Graph.AddFltAttrDatN(NVal, Table->FltCols[Index][CurrRowIdx], ColName);
50  break;
51  case atStr:
52  Graph.AddStrAttrDatN(NVal, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
53  break;
54  }
55  }
56  }
57  return 1;
58 }
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.cpp:535
Definition: gbase.h:23
int AddFltAttrDatN(const TNodeI &NodeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: network.h:2868
int AddStrAttrDatN(const TNodeI &NodeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: network.h:2865
Definition: dt.h:1137
Definition: dt.h:412
Definition: gbase.h:23
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2385
Definition: gbase.h:23
int AddIntAttrDatN(const TNodeI &NodeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: network.h:2862
int TSnap::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.

Definition at line 6 of file conv.cpp.

7  {
8  Graph->AddModeNet(Name);
9  TModeNet& Net = Graph->GetModeNetByName(Name);
10  return LoadMode(Net, Table, NCol, NodeAttrV);
11 }
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...
Definition: conv.cpp:14
The nodes of one particular mode in a TMMNet, and their neighbor vectors as TIntV attributes...
Definition: mmnet.h:23
TIntH TSnap::LoadNodeList ( TStr  InFNmNodes)

Definition at line 671 of file centr.cpp.

671  {
672  TSsParser Ss(InFNmNodes, ssfWhiteSep, true, true, true);
673  TIntIntH Nodes;
674  int br = 0, NId;
675  while (Ss.Next()) {
676  if (Ss.GetInt(0, NId)) {
677  Nodes.AddDat(br,NId);
678  br++;
679  }
680  }
681  return Nodes;
682 }
Definition: ss.h:72
Whitespace (space or tab) separated.
Definition: ss.h:11
Definition: hash.h:97
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
PGraph TSnap::LoadPajek ( const TStr InFNm)

Loads a (directed, undirected or multi) graph from Pajek .PAJ format file.

Function supports both the 1 edge per line (<source> <destination> <weight>) as well as the 1 node per line (<source> <destination1> <destination2> ...) formats.

Definition at line 210 of file gio.h.

210  {
211  PGraph Graph = PGraph::TObj::New();
212  TSsParser Ss(InFNm, ssfSpaceSep, true, true, true);
213  while ((Ss.Len()==0 || strstr(Ss[0], "*vertices") == NULL) && ! Ss.Eof()) {
214  Ss.Next(); Ss.ToLc(); }
215  // nodes
216  bool EdgeList = true;
217  EAssert(strstr(Ss[0], "*vertices") != NULL);
218  while (Ss.Next()) {
219  Ss.ToLc();
220  if (Ss.Len()>0 && Ss[0][0] == '%') { continue; } // comment
221  if (strstr(Ss[0], "*arcslist")!=NULL || strstr(Ss[0],"*edgeslist")!=NULL) { EdgeList=false; break; }
222  if (strstr(Ss[0], "*arcs")!=NULL || strstr(Ss[0],"*edges")!=NULL) { break; } // arcs are directed, edges are undirected
223  Graph->AddNode(Ss.GetInt(0));
224  }
225  // edges
226  while (Ss.Next()) {
227  if (Ss.Len()>0 && Ss[0][0] == '%') { continue; } // comment
228  if (Ss.Len()>0 && Ss[0][0] == '*') { break; }
229  if (EdgeList) {
230  // <source> <destination> [ <weight> ]
231  if (Ss.Len() >= 2 && Ss.IsInt(0) && Ss.IsInt(1)) {
232  Graph->AddEdge(Ss.GetInt(0), Ss.GetInt(1));
233  }
234  } else {
235  // <source> <destination1> <destination2> <destination3> ...
236  const int SrcNId = Ss.GetInt(0);
237  for (int i = 1; i < Ss.Len(); i++) {
238  Graph->AddEdge(SrcNId, Ss.GetInt(i)); }
239  }
240  }
241  return Graph;
242 }
Definition: ss.h:72
Space separated.
Definition: ss.h:10
#define EAssert(Cond)
Definition: bd.h:280
template<class PGraph >
void TSnap::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).

Definition at line 353 of file alg.h.

353  {
354  CAssert(HasGraphFlag(typename PGraph::TObj, gfDirected)); // graph has to be directed
355  TIntPrV EdgeV;
356  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
357  const int SrcNId = EI.GetSrcNId();
358  const int DstNId = EI.GetDstNId();
359  if (! Graph->IsEdge(DstNId, SrcNId)) {
360  EdgeV.Add(TIntPr(DstNId, SrcNId));
361  }
362  }
363  for (int i = 0; i < EdgeV.Len(); i++) {
364  Graph->AddEdge(EdgeV[i].Val1, EdgeV[i].Val2);
365  }
366 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
#define CAssert(Cond)
Definition: bd.h:302
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::MapHits ( const TVec< PGraph > &  GraphSeq,
TVec< PTable > &  TableSeq,
TTableContext Context,
const int &  MaxIter 
)

Gets sequence of Hits tables from given GraphSeq into TableSeq.

Definition at line 636 of file centr.h.

638  {
639  int NumGraphs = GraphSeq.Len();
640  TableSeq.Reserve(NumGraphs, NumGraphs);
641  // This loop is parallelizable.
642  for (TInt i = 0; i < NumGraphs; i++) {
643  TIntFltH HubH;
644  TIntFltH AuthH;
645  GetHits(GraphSeq[i], HubH, AuthH, MaxIter);
646  PTable HubT = TTable::TableFromHashMap(HubH, "NodeId", "Hub", Context, false);
647  PTable AuthT = TTable::TableFromHashMap(AuthH, "NodeId", "Authority", Context, false);
648  PTable HitsT = HubT->Join("NodeId", AuthT, "NodeId");
649  HitsT->Rename("1.NodeId", "NodeId");
650  HitsT->Rename("1.Hub", "Hub");
651  HitsT->Rename("2.Authority", "Authority");
652  TStrV V = TStrV(3, 0);
653  V.Add("NodeId");
654  V.Add("Hub");
655  V.Add("Authority");
656  HitsT->ProjectInPlace(V);
657  TableSeq[i] = HitsT;
658  }
659 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static PTable TableFromHashMap(const THash< TInt, TInt > &H, const TStr &Col1, const TStr &Col2, TTableContext *Context, const TBool IsStrKeys=false)
Builds table from hash table of int->int.
Definition: table.h:988
void GetHits(const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
Definition: centr.h:524
Definition: dt.h:1137
TVec< TStr > TStrV
Definition: ds.h:1599
Definition: bd.h:196
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraph >
void TSnap::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.

Definition at line 621 of file centr.h.

623  {
624  int NumGraphs = GraphSeq.Len();
625  TableSeq.Reserve(NumGraphs, NumGraphs);
626  // This loop is parallelizable.
627  for (TInt i = 0; i < NumGraphs; i++) {
628  TIntFltH PRankH;
629  GetPageRank(GraphSeq[i], PRankH, C, Eps, MaxIter);
630  TableSeq[i] = TTable::TableFromHashMap(PRankH, "NodeId", "PageRank", Context, false);
631  }
632 }
void GetPageRank(const PGraph &Graph, TIntFltH &PRankH, const double &C=0.85, const double &Eps=1e-4, const int &MaxIter=100)
Definition: centr.h:240
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static PTable TableFromHashMap(const THash< TInt, TInt > &H, const TStr &Col1, const TStr &Col2, TTableContext *Context, const TBool IsStrKeys=false)
Builds table from hash table of int->int.
Definition: table.h:988
Definition: dt.h:1137
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TIntH TSnap::MaxCPGreedyBetter ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 175 of file centr.cpp.

175  {
176  TIntH GroupNodes; // buildup cpntainer of group nodes
177  TIntH NNodes; // container of neighbouring nodes
178  TIntH Nodes; // nodes sorted by vd
179  double gc = 0, gc0 = 0;
180  int addId = 0, addIdPrev = 0;
181 
182  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
183  Nodes.AddDat(NI.GetId(),NI.GetDeg());
184  }
185 
186  Nodes.SortByDat(false);
187 
188  int br = 0;
189  while (br < k) {
190  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++) {
191  if ((NI.GetDat() <= (int)gc0))
192  break;
193  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
194  if (gc>gc0) {
195  gc0 = gc;
196  addId = NI.GetKey();
197  }
198  }
199 
200  if (addId != addIdPrev){
201 
202  GroupNodes.AddDat(br,addId);
203  br++;
204  gc0=0;
205 
206  NNodes.AddDat(addId,0);
207  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
208  NNodes.AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
209  }
210  addIdPrev = addId;
211  Nodes.DelKey(addId);
212  } else {
213  br = k;
214  }
215  printf("%i,",br);
216  }
217 
218  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
219  return GroupNodes;
220 }
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
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
void DelKey(const TKey &Key)
Definition: hash.h:404
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void SortByDat(const bool &Asc=true)
Definition: hash.h:292
TIntH TSnap::MaxCPGreedyBetter1 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 223 of file centr.cpp.

223  {
224  TIntH GroupNodes;
225  TIntH NNodes;
226  TIntH Nodes;
227  double gc = 0, gc0 = 0;
228  int addId = 0, addIdPrev = 0;
229 
230  // put nodes in the container and sort them by vertex degree
231  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
232  Nodes.AddDat(NI.GetId(),NI.GetDeg());
233  }
234  Nodes.SortByDat(false);
235 
236  int br = 0;
237  while (br < k) {
238  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
239  if((NI.GetDat() < (int)gc0))
240  break;
241  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
242  if (gc>gc0) {
243  gc0 = gc;
244  addId = NI.GetKey();
245  }
246  }
247 
248  if (addId != addIdPrev){
249 
250  GroupNodes.AddDat(br,addId);
251  br++;
252  gc0=-10000000;
253 
254  NNodes.AddDat(addId,0);
255  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
256  NNodes.AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
257  }
258  addIdPrev = addId;
259  Nodes.DelKey(addId);
260  }
261  }
262 
263  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
264  return GroupNodes;
265 }
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
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
void DelKey(const TKey &Key)
Definition: hash.h:404
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void SortByDat(const bool &Asc=true)
Definition: hash.h:292
TIntH TSnap::MaxCPGreedyBetter2 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 268 of file centr.cpp.

268  {
269  TIntH GroupNodes; // buildup cpntainer of group nodes
270  TStr NNodes; // container of neighbouring nodes
271  TIntH Nodes; // nodes sorted by vd
272  double gc = 0, gc0 = 0;
273  int addId = 0, addIdPrev=0;
274 
275  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
276  Nodes.AddDat(NI.GetId(),NI.GetDeg());
277  }
278 
279  Nodes.SortByDat(false);
280 
281  int br=0;
282  while (br < k) {
283  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
284  if((NI.GetDat() <= (int)gc0))
285  break;
286  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
287  if (gc>gc0) {
288  gc0 = gc;
289  addId = NI.GetKey();
290  }
291  }
292 
293  if (addId != addIdPrev) {
294 
295  GroupNodes.AddDat(br,addId);
296  br++;
297  gc0=0;
298 
299  TInt digi = addId;
300  TStr buf = digi.GetStr();
301 
302  NNodes += " "+buf;
303 
304  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
305  TInt digi = Graph->GetNI(addId).GetNbrNId(i);
306  TStr buf = digi.GetStr();
307  NNodes += " "+buf;
308  }
309  addIdPrev = addId;
310  Nodes.DelKey(addId);
311  } else {
312  br = k;
313  }
314  printf("%i,",br);
315  }
316 
317  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
318  return GroupNodes;
319 }
TStr GetStr() const
Definition: dt.h:1200
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
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
void DelKey(const TKey &Key)
Definition: hash.h:404
Definition: dt.h:1137
Definition: dt.h:412
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void SortByDat(const bool &Asc=true)
Definition: hash.h:292
TIntH TSnap::MaxCPGreedyBetter3 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 322 of file centr.cpp.

322  {
323  TIntH GroupNodes; // buildup cpntainer of group nodes
324  const int n = Graph->GetNodes();
325  int *NNodes = new int[n]; // container of neighbouring nodes
326  int NNodes_br = 0;
327  TIntH Nodes; // nodes sorted by vd
328  double gc = 0, gc0 = 0;
329  int addId = 0, addIdPrev = 0;
330 
331  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
332  Nodes.AddDat(NI.GetId(),NI.GetDeg());
333  }
334 
335  Nodes.SortByDat(false);
336 
337  int br = 0;
338  while (br < k) {
339  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
340  if((NI.GetDat() <= (int)gc0))
341  break;
342  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes,NNodes_br);
343  if (gc>gc0){
344  gc0 = gc;
345  addId = NI.GetKey();
346  }
347  }
348 
349  if (addId != addIdPrev) {
350 
351  GroupNodes.AddDat(br,addId);
352  br++;
353  gc0=0;
354 
355  int nn = addId;
356  bool nnnew = true;
357  for (int j=0; j<NNodes_br; j++)
358  if (NNodes[j] == nn){
359  nnnew = false;
360  j = NNodes_br;
361  }
362 
363  if (nnnew){
364  NNodes[NNodes_br] = nn;
365  NNodes_br++;
366  }
367 
368  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
369  int nn = Graph->GetNI(addId).GetNbrNId(i);
370  bool nnnew = true;
371  for (int j=0; j<NNodes_br; j++) {
372  if (NNodes[j] == nn){
373  nnnew = false;
374  j = NNodes_br;
375  }
376  }
377  if (nnnew){
378  NNodes[NNodes_br] = nn;
379  NNodes_br++;
380  }
381  }
382  addIdPrev = addId;
383  Nodes.DelKey(addId);
384  } else {
385  br = k;
386  }
387  printf("%i,",br);
388  }
389 
390  delete NNodes;
391  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
392  return GroupNodes;
393 }
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
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
void DelKey(const TKey &Key)
Definition: hash.h:404
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void SortByDat(const bool &Asc=true)
Definition: hash.h:292
template<class PGraph >
void TSnap::MergeNbrs ( TIntV NeighbourV,
const typename PGraph::TObj::TNodeI &  NI 
)

Merges neighbors by removing duplicates and produces one sorted vector of neighbors.

Definition at line 526 of file triad.h.

526  {
527  int j = 0;
528  int k = 0;
529  int prev = -1;
530  int indeg = NI.GetInDeg();
531  int outdeg = NI.GetOutDeg();
532  if (indeg > 0 && outdeg > 0) {
533  int v1 = NI.GetInNId(j);
534  int v2 = NI.GetOutNId(k);
535  while (1) {
536  if (v1 <= v2) {
537  if (prev != v1) {
538  NeighbourV.Add(v1);
539  prev = v1;
540  }
541  j += 1;
542  if (j >= indeg) {
543  break;
544  }
545  v1 = NI.GetInNId(j);
546  } else {
547  if (prev != v2) {
548  NeighbourV.Add(v2);
549  prev = v2;
550  }
551  k += 1;
552  if (k >= outdeg) {
553  break;
554  }
555  v2 = NI.GetOutNId(k);
556  }
557  }
558  }
559  while (j < indeg) {
560  int v = NI.GetInNId(j);
561  if (prev != v) {
562  NeighbourV.Add(v);
563  prev = v;
564  }
565  j += 1;
566  }
567  while (k < outdeg) {
568  int v = NI.GetOutNId(k);
569  if (prev != v) {
570  NeighbourV.Add(v);
571  prev = v;
572  }
573  k += 1;
574  }
575 }
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
void TSnap::NumpyToTFltV ( TFltV FltV,
float *  FltNumpyVecIn,
int  n 
)

Converts NumpyArray to TFltV.

Fills the FltV with Numpy Array values. Assumes that the vector is of size n.

Definition at line 33 of file numpy.cpp.

33  {
34  for (int i = 0; i < n; ++i) {
35  FltV[i] = FltNumpyVecIn[i];
36  }
37 }
void TSnap::NumpyToTIntV ( TIntV IntV,
int *  IntNumpyVecIn,
int  n 
)

Converts NumpyArray to TIntV.

Fills the IntV with Numpy Array values. Assumes that IntV is of size n.

Definition at line 25 of file numpy.cpp.

25  {
26  for (int i = 0; i < n; ++i) {
27  IntV[i] = IntNumpyVecIn[i];
28  }
29 }
double TSnap::PearsonCorrelation ( PUNGraph Graph,
TIntIntH out,
int  coresize 
)

Definition at line 205 of file coreper.cpp.

205  {
206  int br_core1=0,br_periphery1=0,br_core_per1=0;
207  for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++){ // Calculate and store the degrees of each node.
208  int i = EI.GetSrcNId();
209  int j = EI.GetDstNId();
210 
211  if (out.GetDat(i)==1&&out.GetDat(j)==1 && i!=j)
212  br_core1++;
213  else if (out.GetDat(i)==0&&out.GetDat(j)==0 && i!=j)
214  br_periphery1++;
215  else
216  br_core_per1++;
217  }
218 
219  double core_quality = (double)br_core1/((((double)coresize*(double)coresize)-(double)coresize)/2);
220  int per_size = Graph->GetNodes()-coresize;
221  double periphery_quality = (((((double)per_size*(double)per_size)-(double)per_size)/2) - (double)br_periphery1)/((((double)per_size*(double)per_size)-(double)per_size)/2);
222 
223  return (double)(core_quality+periphery_quality);
224  }
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:121
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
template<class PGraph >
void TSnap::PlotClustCf ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

Plots the distribution of clustering coefficient of a Graph.

Definition at line 111 of file statplot.h.

111  {
112  TFltPrV DegToCCfV;
113  int64 ClosedTriads, OpenTriads;
114  const double CCF = GetClustCf(Graph, DegToCCfV, ClosedTriads, OpenTriads);
115  if (DescStr.Empty()) { DescStr = FNmPref; }
116  TGnuPlot GnuPlot("ccf."+FNmPref,
117  TStr::Fmt("%s. G(%d, %d). Average clustering: %.4f OpenTriads: %d (%.4f) ClosedTriads: %d (%.4f)", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(),
118  CCF, OpenTriads, OpenTriads/double(OpenTriads+ClosedTriads), ClosedTriads, ClosedTriads/double(OpenTriads+ClosedTriads)));
119  GnuPlot.AddPlot(DegToCCfV, gpwLinesPoints, "", "pt 6");
120  GnuPlot.SetXYLabel("Node degree", "Average clustering coefficient");
121  GnuPlot.SetScale(gpsLog10XY);
122  GnuPlot.SavePng();
123 }
long long int64
Definition: bd.h:27
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
double GetClustCf(const PGraph &Graph, int SampleNodes=-1)
Computes the average clustering coefficient as defined in Watts and Strogatz, Collective dynamics of ...
Definition: triad.h:137
char * CStr()
Definition: dt.h:479
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
void TSnap::PlotEigValDistr ( const PUNGraph Graph,
const int &  EigVals,
const TStr FNmPref,
TStr  DescStr 
)

Plots the distribution of components of the leading eigen-vector of the Graph adjacency matrix. Plots first EigVals values.

Definition at line 14 of file statplot.cpp.

14  {
15  const int NBuckets = 50;
16  TFltV EigValV;
17  for (int f = 1; EigValV.Empty() && f < 4; f++) {
18  TSnap::GetEigVals(Graph, f*EigVals, EigValV);
19  }
20  EigValV.Sort(true);
21  THash<TFlt, TFlt> BucketCntH;
22  double Step = (EigValV.Last()-EigValV[0]) / double(NBuckets-1);
23  for (int i = 0; i < NBuckets; i++) {
24  BucketCntH.AddDat(EigValV[0]+Step*(i+0.5), 0);
25  }
26  for (int i = 0; i < EigValV.Len(); i++) {
27  const int Bucket = (int) floor((EigValV[i]-EigValV[0]) / Step);
28  BucketCntH[Bucket] += 1;
29  }
30  TFltPrV EigCntV;
31  BucketCntH.GetKeyDatPrV(EigCntV);
32  if (DescStr.Empty()) { DescStr = FNmPref; }
33  TGnuPlot::PlotValV(EigCntV, "eigDistr."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f", DescStr.CStr(),
34  Graph->GetNodes(), Graph->GetEdges(), EigValV.Last().Val), "Eigen value", "Count", gpsAuto, false, gpwLinesPoints);
35 }
void GetEigVals(const PUNGraph &Graph, const int &EigVals, TFltV &EigValV)
Computes top EigVals eigenvalues of the adjacency matrix representing a given undirected Graph...
Definition: gsvd.cpp:308
double Val
Definition: dt.h:1388
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: gnuplot.h:7
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:500
char * CStr()
Definition: dt.h:479
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
void TSnap::PlotEigValRank ( const PUNGraph Graph,
const int &  EigVals,
const TStr FNmPref,
TStr  DescStr 
)

Plots the eigen-value rank distribution of the Graph adjacency matrix. Plots first EigVals eigenvalues.

Definition at line 5 of file statplot.cpp.

5  {
6  TFltV EigValV;
7  TSnap::GetEigVals(Graph, EigVals, EigValV);
8  EigValV.Sort(false);
9  if (DescStr.Empty()) { DescStr = FNmPref; }
10  TGnuPlot::PlotValV(EigValV, "eigVal."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f",
11  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), EigValV[0].Val), "Rank", "Eigen value", gpsLog10XY, false, gpwLinesPoints);
12 }
void GetEigVals(const PUNGraph &Graph, const int &EigVals, TFltV &EigValV)
Computes top EigVals eigenvalues of the adjacency matrix representing a given undirected Graph...
Definition: gsvd.cpp:308
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
template<class PGraph >
void TSnap::PlotHops ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr(),
const bool &  IsDir = false,
const int &  NApprox = 32 
)

Plots the cumulative distribution of the shortest path lengths of a Graph. Implementation is based on ANF.

Parameters
IsDirfalse: ignore edge directions and consider graph as undirected.

Definition at line 126 of file statplot.h.

126  {
127  TIntFltKdV DistNbrsV;
128  TSnap::GetAnf(Graph, DistNbrsV, -1, IsDir, NApprox);
129  const double EffDiam = TSnap::TSnapDetail::CalcEffDiam(DistNbrsV, 0.9);
130  if (DescStr.Empty()) { DescStr = FNmPref; }
131  TGnuPlot GnuPlot("hop."+FNmPref, TStr::Fmt("%s. Hop plot. EffDiam: %g, G(%d, %d)",
132  DescStr.CStr(), EffDiam, Graph->GetNodes(), Graph->GetEdges()));
133  GnuPlot.SetXYLabel("Number of hops", "Number of pairs of nodes");
134  GnuPlot.SetScale(gpsLog10Y);
135  GnuPlot.AddPlot(DistNbrsV, gpwLinesPoints, "", "pt 6");
136  GnuPlot.SavePng();
137 }
void GetAnf(const PGraph &Graph, const int &SrcNId, TIntFltKdV &DistNbrsV, const int &MxDist, const bool &IsDir, const int &NApprox=32)
Definition: anf.h:204
double CalcEffDiam(const TIntFltKdV &DistNbrsCdfV, const double &Percentile)
Helper function for computing a given Percentile of a (unnormalized) cumulative distribution function...
Definition: anf.cpp:7
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
template<class PGraph >
void TSnap::PlotInDegDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr(),
const bool &  PlotCCdf = false,
const bool &  PowerFit = false 
)

Plots the in-degree distribution of a Graph.

Parameters
PlotCCdfPlots the distribution as a Complementary Cummulative distribution function.
PowerFitFits a Power-Law to the distribution.

Definition at line 47 of file statplot.h.

47  {
48  TIntPrV DegCntV;
49  TSnap::GetInDegCnt(Graph, DegCntV);
50  const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes());
51  int AboveAvg=0, Above2Avg=0;
52  for (int i = 0; i < DegCntV.Len(); i++) {
53  if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; }
54  if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; }
55  }
56  if (PlotCCdf) {
57  DegCntV = TGUtil::GetCCdf(DegCntV); }
58  if (DescStr.Empty()) { DescStr = FNmPref; }
59  TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"inDegC.":"inDeg.")+FNmPref,
60  TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with in-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(),
61  Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())),
62  "In-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints);
63 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
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) ...
Definition: alg.h:179
static void GetCCdf(const TIntPrV &PdfV, TIntPrV &CCdfV)
Definition: util.cpp:33
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
void TSnap::PlotInvParticipRat ( const PUNGraph Graph,
const int &  MaxEigVecs,
const int &  TimeLimit,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

Plots the inverse participation ratio. See Spectra of "real-world" graphs: Beyond the semicircle law by Farkas, Derenyi, Barabasi and Vicsek.

Definition at line 39 of file statplot.cpp.

39  {
40  TFltPrV EigIprV;
41  GetInvParticipRat(Graph, MaxEigVecs, TimeLimit, EigIprV);
42  if (DescStr.Empty()) { DescStr = FNmPref; }
43  if (EigIprV.Empty()) { DescStr+=". FAIL"; EigIprV.Add(TFltPr(-1,-1)); return; }
44  TGnuPlot::PlotValV(EigIprV, "eigIPR."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f (%d values)",
45  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), EigIprV.Last().Val1(), EigIprV.Len()),
46  "Eigenvalue", "Inverse Participation Ratio of corresponding Eigenvector", gpsLog10Y, false, gpwPoints);
47 }
void GetInvParticipRat(const PUNGraph &Graph, int MaxEigVecs, int TimeLimit, TFltPrV &EigValIprV)
Definition: gsvd.cpp:378
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
template<class PGraph >
void TSnap::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.

Definition at line 175 of file statplot.h.

175  {
176  TIntPrV CoreEdgesV;
177  TSnap::GetKCoreEdges(Graph, CoreEdgesV);
178  if (DescStr.Empty()) { DescStr = FNmPref; }
179  TGnuPlot::PlotValV(CoreEdgesV, "coreEdges."+FNmPref, TStr::Fmt("%s. G(%d, %d).", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "k-Core", "Number of edges in the k-Core", gpsLog10Y, false, gpwLinesPoints);
180 }
int GetKCoreEdges(const PGraph &Graph, TIntPrV &CoreIdSzV)
Returns the number of edges in each core of order K (where K=0, 1, ...)
Definition: kcore.h:126
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
template<class PGraph >
void TSnap::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.

Definition at line 167 of file statplot.h.

167  {
168  TIntPrV CoreNodesV;
169  TSnap::GetKCoreNodes(Graph, CoreNodesV);
170  if (DescStr.Empty()) { DescStr = FNmPref; }
171  TGnuPlot::PlotValV(CoreNodesV, "coreNodes."+FNmPref, TStr::Fmt("%s. G(%d, %d).", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "k-Core", "Number of nodes in the k-Core", gpsLog10Y, false, gpwLinesPoints);
172 }
int GetKCoreNodes(const PGraph &Graph, TIntPrV &CoreIdSzV)
Returns the number of nodes in each core of order K (where K=0, 1, ...)
Definition: kcore.h:114
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
template<class PGraph >
void TSnap::PlotOutDegDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr(),
const bool &  PlotCCdf = false,
const bool &  PowerFit = false 
)

Plots the out-degree distribution of a Graph.

Parameters
PlotCCdfPlots the distribution as a Complementary Cumulative Distribution Function (CCDF).
PowerFitFits a Power-Law to the distribution.

Definition at line 66 of file statplot.h.

66  {
67  TIntPrV DegCntV;
68  TSnap::GetOutDegCnt(Graph, DegCntV);
69  const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes());
70  int AboveAvg=0, Above2Avg=0;
71  for (int i = 0; i < DegCntV.Len(); i++) {
72  if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; }
73  if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; }
74  }
75  if (PlotCCdf) {
76  DegCntV = TGUtil::GetCCdf(DegCntV); }
77  if (DescStr.Empty()) { DescStr = FNmPref; }
78  TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"outDegC.":"outDeg.")+FNmPref,
79  TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with out-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(),
80  Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())),
81  "Out-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints);
82 }
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) ...
Definition: alg.h:201
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static void GetCCdf(const TIntPrV &PdfV, TIntPrV &CCdfV)
Definition: util.cpp:33
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
template<class PGraph >
void TSnap::PlotSccDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

Plots the distribution of sizes of strongly connected components of a Graph.

Definition at line 98 of file statplot.h.

98  {
99  TIntPrV SccSzCnt;
100  TSnap::GetSccSzCnt(Graph, SccSzCnt);
101  if (DescStr.Empty()) { DescStr = FNmPref; }
102  TGnuPlot GnuPlot("scc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes",
103  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), SccSzCnt.Last().Val1/double(Graph->GetNodes())));
104  GnuPlot.AddPlot(SccSzCnt, gpwLinesPoints, "", "pt 6");
105  GnuPlot.SetXYLabel("Size of strongly connected component", "Number of components");
106  GnuPlot.SetScale(gpsLog10XY);
107  GnuPlot.SavePng();
108 }
void GetSccSzCnt(const PGraph &Graph, TIntPrV &SccSzCnt)
Returns a distribution of strongly connected component sizes.
Definition: cncom.h:420
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TVal1 Val1
Definition: ds.h:34
int AddPlot(const TIntV &YValV, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
Definition: gnuplot.cpp:186
char * CStr()
Definition: dt.h:479
template<class PGraph >
void TSnap::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.

Definition at line 140 of file statplot.h.

140  {
141  TIntH DistToCntH;
142  TBreathFS<PGraph> BFS(Graph);
143  // shotest paths
144  TIntV NodeIdV;
145  Graph->GetNIdV(NodeIdV); NodeIdV.Shuffle(TInt::Rnd);
146  for (int tries = 0; tries < TMath::Mn(TestNodes, Graph->GetNodes()); tries++) {
147  const int NId = NodeIdV[tries];
148  BFS.DoBfs(NId, true, false, -1, TInt::Mx);
149  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
150  DistToCntH.AddDat(BFS.NIdDistH[i]) += 1; }
151  }
152  DistToCntH.SortByKey(true);
153  TFltPrV DistNbrsPdfV;
154  for (int i = 0; i < DistToCntH.Len(); i++) {
155  DistNbrsPdfV.Add(TFltPr(DistToCntH.GetKey(i)(), DistToCntH[i]()));
156  }
157  const double EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9);
158  const double AvgDiam = TSnap::TSnapDetail::CalcAvgDiamPdf(DistNbrsPdfV);
159  const int FullDiam = (int) DistNbrsPdfV.Last().Val1;
160  if (DescStr.Empty()) { DescStr = FNmPref; }
161  TGnuPlot::PlotValV(DistNbrsPdfV, "diam."+FNmPref,
162  TStr::Fmt("%s. G(%d, %d). Diam: avg:%.2f eff:%.2f max:%d", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(),
163  AvgDiam, EffDiam, FullDiam), "Number of hops", "Number of shortest paths", gpsLog10Y, false, gpwLinesPoints);
164 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static const int Mx
Definition: dt.h:1142
static TRnd Rnd
Definition: dt.h:1146
double CalcEffDiamPdf(const TIntFltKdV &DistNbrsPdfV, const double &Percentile)
Helper function for computing a given Percentile of a (unnormalized) probability distribution functio...
Definition: anf.cpp:29
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void SortByKey(const bool &Asc=true)
Definition: hash.h:291
double CalcAvgDiamPdf(const TIntFltKdV &DistNbrsPdfV)
Helper function for computing the mean of a (unnormalized) probability distribution function...
Definition: anf.cpp:41
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
char * CStr()
Definition: dt.h:479
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
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
void TSnap::PlotSngValDistr ( const PNGraph Graph,
const int &  SngVals,
const TStr FNmPref,
TStr  DescStr 
)

Plots the rank distribution of singular values of the Graph adjacency matrix. Plots first SngVals values.

Definition at line 58 of file statplot.cpp.

58  {
59  const int NBuckets = 50;
60  TFltV SngValV;
61  for (int f = 1; SngValV.Empty() && f < 4; f++) {
62  TSnap::GetSngVals(Graph, f*SngVals, SngValV);
63  }
64  SngValV.Sort(true);
65  THash<TFlt, TFlt> BucketCntH;
66  double Step = (SngValV.Last()-SngValV[0]) / double(NBuckets-1);
67  for (int i = 0; i < NBuckets; i++) {
68  BucketCntH.AddDat(SngValV[0]+Step*(i+0.5), 0);
69  }
70  for (int i = 0; i < SngValV.Len(); i++) {
71  const int Bucket = (int) floor((SngValV[i]-SngValV[0]) / Step);
72  BucketCntH[Bucket] += 1;
73  }
74  TFltPrV EigCntV;
75  BucketCntH.GetKeyDatPrV(EigCntV);
76  if (DescStr.Empty()) { DescStr = FNmPref; }
77  TGnuPlot::PlotValV(EigCntV, "sngDistr."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f", DescStr.CStr(),
78  Graph->GetNodes(), Graph->GetEdges(), SngValV.Last().Val), "Singular value", "Count", gpsAuto, false, gpwLinesPoints);
79 }
double Val
Definition: dt.h:1388
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: gnuplot.h:7
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
void GetSngVals(const PNGraph &Graph, const int &SngVals, TFltV &SngValV)
Computes largest SngVals singular values of the adjacency matrix representing a directed Graph...
Definition: gsvd.cpp:175
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:500
char * CStr()
Definition: dt.h:479
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
void TSnap::PlotSngValRank ( const PNGraph Graph,
const int &  SngVals,
const TStr FNmPref,
TStr  DescStr 
)

Plots the rank distribution of singular values of the Graph adjacency matrix. Plots first SngVals values.

Definition at line 49 of file statplot.cpp.

49  {
50  TFltV SngValV;
51  TSnap::GetSngVals(Graph, SngVals, SngValV);
52  SngValV.Sort(false);
53  if (DescStr.Empty()) { DescStr = FNmPref; }
54  TGnuPlot::PlotValV(SngValV, "sngVal."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f",
55  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), SngValV[0].Val), "Rank", "Singular value", gpsLog10XY, false, gpwLinesPoints);
56 }
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void GetSngVals(const PNGraph &Graph, const int &SngVals, TFltV &SngValV)
Computes largest SngVals singular values of the adjacency matrix representing a directed Graph...
Definition: gsvd.cpp:175
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
void TSnap::PlotSngVec ( const PNGraph Graph,
const TStr FNmPref,
TStr  DescStr 
)

Plots the distribution of the values of the leading left singular vector of the Graph adjacency matrix. Plots first SngVals values.

Definition at line 81 of file statplot.cpp.

81  {
82  TFltV LeftSV, RightSV;
83  TSnap::GetSngVec(Graph, LeftSV, RightSV);
84  LeftSV.Sort(false);
85  RightSV.Sort(false);
86  TFltV BinV;
87  if (DescStr.Empty()) { DescStr = FNmPref; }
88  TGUtil::MakeExpBins(LeftSV, BinV, 1.01);
89  TGnuPlot::PlotValV(BinV, "sngVecL."+FNmPref, TStr::Fmt("%s. G(%d, %d). Left signular vector",
90  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "Rank", "Component of left singular vector", gpsLog10XY, false, gpwLinesPoints);
91  TGnuPlot::PlotValV(BinV, "sngVecL."+FNmPref, TStr::Fmt("%s. G(%d, %d). Right signular vector",
92  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "Rank", "Component of right singular vector", gpsLog10XY, false, gpwLinesPoints);
93 }
static void MakeExpBins(const TFltPrV &XYValV, TFltPrV &ExpXYValV, const double &BinFactor=2, const double &MinYVal=1)
Definition: util.cpp:99
void GetSngVec(const PNGraph &Graph, TFltV &LeftSV, TFltV &RightSV)
Computes the leading left and right singular vector of the adjacency matrix representing a directed G...
Definition: gsvd.cpp:225
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
template<class PGraph >
void TSnap::PlotWccDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

Plots the distribution of sizes of weakly connected components of a Graph.

Definition at line 85 of file statplot.h.

85  {
86  TIntPrV WccSzCnt;
87  TSnap::GetWccSzCnt(Graph, WccSzCnt);
88  if (DescStr.Empty()) { DescStr = FNmPref; }
89  TGnuPlot GnuPlot("wcc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes",
90  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), WccSzCnt.Last().Val1/double(Graph->GetNodes())));
91  GnuPlot.AddPlot(WccSzCnt, gpwLinesPoints, "", "pt 6");
92  GnuPlot.SetXYLabel("Size of weakly connected component", "Number of components");
93  GnuPlot.SetScale(gpsLog10XY);
94  GnuPlot.SavePng();
95 }
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
bool Empty() const
Definition: dt.h:491
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TVal1 Val1
Definition: ds.h:34
int AddPlot(const TIntV &YValV, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
Definition: gnuplot.cpp:186
char * CStr()
Definition: dt.h:479
void GetWccSzCnt(const PGraph &Graph, TIntPrV &WccSzCnt)
Returns a distribution of weakly connected component sizes.
Definition: cncom.h:337
template<class PGraph >
void TSnap::PrintInfo ( const PGraph &  Graph,
const TStr Desc = "",
const TStr OutFNm = "",
const bool &  Fast = true 
)

Prints basic graph statistics.

Parameters
Fasttrue: only computes basic statistics (that can be computed fast). For more extensive information (and longer execution times) set Fast = false.

Definition at line 87 of file gbase.h.

87  {
88  int BiDirEdges=0, ZeroNodes=0, ZeroInNodes=0, ZeroOutNodes=0, SelfEdges=0, NonZIODegNodes=0;
89  THash<TIntPr, TInt> UniqDirE, UniqUnDirE;
90  FILE *F = stdout;
91  if (! OutFNm.Empty()) F = fopen(OutFNm.CStr(), "wt");
92  if (! Desc.Empty()) { fprintf(F, "%s:", Desc.CStr()); }
93  else { fprintf(F, "Graph:"); }
94  for (int f = gfUndef; f < gfMx; f++) {
95  if (HasGraphFlag(typename PGraph::TObj, TGraphFlag(f))) {
96  fprintf(F, " %s", TSnap::GetFlagStr(TGraphFlag(f)).CStr()); }
97  }
98  // calc stat
99  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
100  if (NI.GetDeg()==0) ZeroNodes++;
101  if (NI.GetInDeg()==0) ZeroInNodes++;
102  if (NI.GetOutDeg()==0) ZeroOutNodes++;
103  if (NI.GetInDeg()!=0 && NI.GetOutDeg()!=0) NonZIODegNodes++;
104  if (! Fast || Graph->GetNodes() < 1000) {
105  const int NId = NI.GetId();
106  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
107  const int DstNId = NI.GetOutNId(edge);
108  if (Graph->IsEdge(DstNId, NId)) BiDirEdges++;
109  if (NId == DstNId) SelfEdges++;
110  UniqDirE.AddKey(TIntPr(NId, DstNId));
111  UniqUnDirE.AddKey(TIntPr(TInt::GetMn(NId, DstNId), TInt::GetMx(NId, DstNId)));
112  }
113  }
114  }
115  int64 Closed=0, Open=0;
116  double WccSz=0, SccSz=0;
117  double EffDiam=0;
118  int FullDiam=0;
119  if (! Fast) {
120  TSnap::GetTriads(Graph, Closed, Open);
121  WccSz = TSnap::GetMxWccSz(Graph);
122  SccSz = TSnap::GetMxSccSz(Graph);
123  TSnap::GetBfsEffDiam(Graph, 100, false, EffDiam, FullDiam);
124  }
125  // print info
126  fprintf(F, "\n");
127  fprintf(F, " Nodes: %d\n", Graph->GetNodes());
128  fprintf(F, " Edges: %d\n", Graph->GetEdges());
129  fprintf(F, " Zero Deg Nodes: %d\n", ZeroNodes);
130  fprintf(F, " Zero InDeg Nodes: %d\n", ZeroInNodes);
131  fprintf(F, " Zero OutDeg Nodes: %d\n", ZeroOutNodes);
132  fprintf(F, " NonZero In-Out Deg Nodes: %d\n", NonZIODegNodes);
133  if (! Fast) {
134  fprintf(F, " Unique directed edges: %d\n", UniqDirE.Len());
135  fprintf(F, " Unique undirected edges: %d\n", UniqUnDirE.Len());
136  fprintf(F, " Self Edges: %d\n", SelfEdges);
137  fprintf(F, " BiDir Edges: %d\n", BiDirEdges);
138  fprintf(F, " Closed triangles: %s\n", TUInt64::GetStr(Closed).CStr());
139  fprintf(F, " Open triangles: %s\n", TUInt64::GetStr(Open).CStr());
140  fprintf(F, " Frac. of closed triads: %f\n", Closed/double(Closed+Open));
141  fprintf(F, " Connected component size: %f\n", WccSz);
142  fprintf(F, " Strong conn. comp. size: %f\n", SccSz);
143  fprintf(F, " Approx. full diameter: %d\n", FullDiam);
144  fprintf(F, " 90%% effective diameter: %f\n", EffDiam);
145  //fprintf(F, " Core\tNodes\tEdges\n");
146  //for (int i = 0; i < CNodesV.Len(); i++) {
147  // printf(" %d\t%d\t%d\n", CNodesV[i].Val1(), CNodesV[i].Val2(), CEdgesV[i].Val2());
148  //}
149  }
150  if (! OutFNm.Empty()) { fclose(F); }
151 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
int64 GetTriads(const PGraph &Graph, int64 &ClosedTriads, int64 &OpenTriads, int SampleNodes=-1)
Computes the number of Closed and Open triads.
Definition: triad.h:246
double GetBfsEffDiam(const PGraph &Graph, const int &NTestNodes, const bool &IsDir=false)
Returns the (approximation of the) Effective Diameter (90-th percentile of the distribution of shorte...
Definition: bfsdfs.h:424
double GetMxWccSz(const PGraph &Graph)
Returns the fraction of nodes in the largest weakly connected component of a Graph.
Definition: cncom.h:436
static int GetMx(const int &Int1, const int &Int2)
Definition: dt.h:1185
default value, no flags
Definition: gbase.h:12
TStr GetFlagStr(const TGraphFlag &GraphFlag)
Returns a string representation of a flag.
Definition: gbase.cpp:5
static int GetMn(const int &Int1, const int &Int2)
Definition: dt.h:1183
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
double GetMxSccSz(const PGraph &Graph)
Returns the fraction of nodes in the largest strongly connected component of a Graph.
Definition: cncom.h:444
int AddKey(const TKey &Key)
Definition: hash.h:373
TStr GetStr() const
Definition: dt.h:1363
long long int64
Definition: bd.h:27
bool Empty() const
Definition: dt.h:491
sentinel, last value for iteration
Definition: gbase.h:19
enum TGraphFlag_ TGraphFlag
Graph Flags, used for quick testing of graph types.
char * CStr()
Definition: dt.h:479
int Len() const
Definition: hash.h:228
int TSnap::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.

Definition at line 328 of file flow.cpp.

328  {
329  int EdgeN = PRM.EdgeNum(NId);
330  int EId = -1, NbrNId = -1, ResFlow = 0;
331  int Cutoff = NI.GetInDeg();
332  if (EdgeN < Cutoff) {
333  EId = NI.GetInEId(EdgeN);
334  NbrNId = NI.GetInNId(EdgeN);
335  ResFlow = PRM.Flow(EId);
336  } else {
337  EId = NI.GetOutEId(EdgeN - Cutoff);
338  NbrNId = NI.GetOutNId(EdgeN - Cutoff);
339  ResFlow = PRM.Capacity(EId) - PRM.Flow(EId);
340  }
341  if (ResFlow > 0 && PRM.Label(NId) - 1 == PRM.Label(NbrNId)) {
342  if (EdgeN < Cutoff) {
343  PushToInNbr(PRM, NId, NbrNId, EId);
344  } else {
345  PushToOutNbr(PRM, NId, NbrNId, EId);
346  }
347  return NbrNId;
348  }
349  if (EdgeN + 1 == NI.GetDeg()) {
350  PRM.EdgeNum(NId) = 0;
351  Relabel(PRM, NId, NI);
352  } else {
353  PRM.EdgeNum(NId)++;
354  }
355  return -1;
356 }
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.
Definition: flow.cpp:289
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
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.
Definition: flow.cpp:305
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: network.h:1809
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: network.h:1833
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
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.
Definition: flow.cpp:297
void TSnap::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.

Definition at line 297 of file flow.cpp.

297  {
298  int MinPush = MIN(PRM.Flow(EId), PRM.Excess(NId));
299  PRM.Flow(EId) -= MinPush;
300  PRM.Excess(NId) -= MinPush;
301  PRM.Excess(InNId) += MinPush;
302 }
#define MIN(a, b)
Definition: bd.h:346
void TSnap::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.

Definition at line 289 of file flow.cpp.

289  {
290  int MinPush = MIN(PRM.Capacity(EId) - PRM.Flow(EId), PRM.Excess(NId));
291  PRM.Flow(EId) += MinPush;
292  PRM.Excess(NId) -= MinPush;
293  PRM.Excess(OutNId) += MinPush;
294 }
#define MIN(a, b)
Definition: bd.h:346
int TSnap::ReadEdgeSchemaFromFile ( TSsParser Ss,
const char &  Separator,
int &  SrcColId,
int &  DstColId,
TStrIntH IntAttrEVals,
TStrIntH FltAttrEVals,
TStrIntH StrAttrEVals 
)

Definition at line 6 of file gio.cpp.

6  {
7  if (EDGES_START != Ss.GetFld(0)) return -1;
8  for (int i = 1; i < Ss.GetFlds(); i++) {
9  if (SRC_ID_NAME == Ss.GetFld(i)) {
10  SrcColId = i-1;
11  continue;
12  }
13  if (DST_ID_NAME == Ss.GetFld(i)) {
14  DstColId = i-1;
15  continue;
16  }
17  TStr Attr(Ss.GetFld(i));
18  TStr AttrType;
19  TStr AttrName;
20  Attr.SplitOnCh(AttrType, ':', AttrName);
21  if (AttrType == INT_TYPE_PREFIX) {
22  IntAttrEVals.AddDat(AttrName, i-1);
23  } else if (AttrType == FLT_TYPE_PREFIX) {
24  FltAttrEVals.AddDat(AttrName, i-1);
25  } else if (AttrType == STR_TYPE_PREFIX) {
26  StrAttrEVals.AddDat(AttrName, i-1);
27  } else {
28  return -1;
29  }
30  }
31  return 0;
32 }
const TStr EDGES_START
Definition: gio.h:6
const TStr STR_TYPE_PREFIX
Definition: gio.h:14
int GetFlds() const
Returns the number of fields in the current line.
Definition: ss.h:116
const char * GetFld(const int &FldN) const
Returns the contents of the field at index FldN.
Definition: ss.h:129
const TStr DST_ID_NAME
Definition: gio.h:10
void SplitOnCh(TStr &LStr, const char &SplitCh, TStr &RStr) const
Definition: dt.cpp:901
const TStr FLT_TYPE_PREFIX
Definition: gio.h:13
Definition: dt.h:412
const TStr INT_TYPE_PREFIX
Definition: gio.h:12
const TStr SRC_ID_NAME
Definition: gio.h:9
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
bool TSnap::ReadEdgesFromFile ( TSsParser Ss,
const char &  Separator,
PNEANet Graph,
int &  SrcColId,
int &  DstColId,
TStrIntH IntAttrEVals,
TStrIntH FltAttrEVals,
TStrIntH StrAttrEVals 
)

Definition at line 38 of file gio.cpp.

38  {
39  int SrcNId, DstNId;
40  while (Ss.Next()) {
41  if (Ss.GetFlds() == 0) continue;
42  if (END_SENTINEL == Ss.GetFld(0)) { return false; }
43  if (EDGES_START == Ss.GetFld(0)) { return true; }
44  if (NODES_START == Ss.GetFld(0)) { return true; }
45  if (Ss.GetFld(0)[0] == '#') { continue; }
46  if (! Ss.GetInt(SrcColId, SrcNId) || ! Ss.GetInt(DstColId, DstNId)) { continue; }
47  if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
48  if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
49  int EId = Graph->AddEdge(SrcNId, DstNId);
50  double FltAttrVal;
51  for (TStrIntH::TIter it = FltAttrEVals.BegI(); it < FltAttrEVals.EndI(); it++) {
52  if (Ss.GetFlt(it.GetDat(), FltAttrVal)) {
53  Graph->AddFltAttrDatE(EId, FltAttrVal, it.GetKey());
54  }
55  }
56  int IntAttrVal;
57  for (TStrIntH::TIter it = IntAttrEVals.BegI(); it < IntAttrEVals.EndI(); it++) {
58  if (Ss.GetInt(it.GetDat(), IntAttrVal)) {
59  Graph->AddIntAttrDatE(EId, IntAttrVal, it.GetKey());
60  }
61  }
62  char* StrAttrVal;
63  for (TStrIntH::TIter it = StrAttrEVals.BegI(); it < StrAttrEVals.EndI(); it++) {
64  StrAttrVal = Ss.GetFld(it.GetDat());
65  if (NULL_VAL != StrAttrVal) {
66  Graph->AddStrAttrDatE(EId, TStr(StrAttrVal), it.GetKey());
67  }
68  }
69  }
70  return false;
71 }
const TStr EDGES_START
Definition: gio.h:6
TIter BegI() const
Definition: hash.h:213
bool GetInt(const int &FldN, int &Val) const
If the field FldN is an integer its value is returned in Val and the function returns true...
Definition: ss.cpp:447
TIter EndI() const
Definition: hash.h:218
int GetFlds() const
Returns the number of fields in the current line.
Definition: ss.h:116
const char * GetFld(const int &FldN) const
Returns the contents of the field at index FldN.
Definition: ss.h:129
const TStr NODES_START
Definition: gio.h:7
const TStr END_SENTINEL
Definition: gio.h:8
bool GetFlt(const int &FldN, double &Val) const
If the field FldN is a float its value is returned in Val and the function returns true...
Definition: ss.cpp:485
Definition: dt.h:412
bool Next()
Loads next line from the input file.
Definition: ss.cpp:412
const TStr NULL_VAL
Definition: gio.h:15
int TSnap::ReadNodeSchemaFromFile ( TSsParser Ss,
const char &  Separator,
int &  NId,
TStrIntH IntAttrNVals,
TStrIntH FltAttrNVals,
TStrIntH StrAttrNVals 
)

Definition at line 77 of file gio.cpp.

77  {
78  if (NODES_START != Ss.GetFld(0)) return -1;
79  for (int i = 1; i < Ss.GetFlds(); i++) {
80  if (NID_NAME == Ss.GetFld(i)) {
81  NId = i-1;
82  continue;
83  }
84  TStr Attr(Ss.GetFld(i));
85  TStr AttrType;
86  TStr AttrName;
87  Attr.SplitOnCh(AttrType, ':', AttrName);
88  if (AttrType == INT_TYPE_PREFIX) {
89  IntAttrNVals.AddDat(AttrName, i-1);
90  } else if (AttrType == FLT_TYPE_PREFIX) {
91  FltAttrNVals.AddDat(AttrName, i-1);
92  } else if (AttrType == STR_TYPE_PREFIX) {
93  StrAttrNVals.AddDat(AttrName, i-1);
94  } else {
95  return -1;
96  }
97  }
98  return 0;
99 }
const TStr STR_TYPE_PREFIX
Definition: gio.h:14
const TStr NID_NAME
Definition: gio.h:11
int GetFlds() const
Returns the number of fields in the current line.
Definition: ss.h:116
const char * GetFld(const int &FldN) const
Returns the contents of the field at index FldN.
Definition: ss.h:129
void SplitOnCh(TStr &LStr, const char &SplitCh, TStr &RStr) const
Definition: dt.cpp:901
const TStr FLT_TYPE_PREFIX
Definition: gio.h:13
const TStr NODES_START
Definition: gio.h:7
Definition: dt.h:412
const TStr INT_TYPE_PREFIX
Definition: gio.h:12
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
bool TSnap::ReadNodesFromFile ( TSsParser Ss,
const char &  Separator,
PNEANet Graph,
int &  NColId,
TStrIntH IntAttrNVals,
TStrIntH FltAttrNVals,
TStrIntH StrAttrNVals 
)

Definition at line 105 of file gio.cpp.

105  {
106  int NId;
107  while (Ss.Next()) {
108  if (Ss.GetFlds() == 0) continue;
109  if (END_SENTINEL == Ss.GetFld(0)) { return false; }
110  if (EDGES_START == Ss.GetFld(0)) { return true; }
111  if (NODES_START == Ss.GetFld(0)) { return true; }
112  if (Ss.GetFld(0)[0] == '#') { continue; }
113  if (! Ss.GetInt(NColId, NId)) { continue; }
114  if (! Graph->IsNode(NId)) { Graph->AddNode(NId); }
115  double FltAttrVal;
116  for (TStrIntH::TIter it = FltAttrNVals.BegI(); it < FltAttrNVals.EndI(); it++) {
117  if (Ss.GetFlt(it.GetDat(), FltAttrVal)) {
118  Graph->AddFltAttrDatN(NId, FltAttrVal, it.GetKey());
119  }
120  }
121  int IntAttrVal;
122  for (TStrIntH::TIter it = IntAttrNVals.BegI(); it < IntAttrNVals.EndI(); it++) {
123  if (Ss.GetInt(it.GetDat(), IntAttrVal)) {
124  Graph->AddIntAttrDatN(NId, IntAttrVal, it.GetKey());
125  }
126  }
127  char* StrAttrVal;
128  for (TStrIntH::TIter it = StrAttrNVals.BegI(); it < StrAttrNVals.EndI(); it++) {
129  StrAttrVal = Ss.GetFld(it.GetDat());
130  if (NULL_VAL != StrAttrVal) {
131  Graph->AddStrAttrDatN(NId, TStr(StrAttrVal), it.GetKey());
132  }
133  }
134  }
135  return false;
136 }
const TStr EDGES_START
Definition: gio.h:6
TIter BegI() const
Definition: hash.h:213
bool GetInt(const int &FldN, int &Val) const
If the field FldN is an integer its value is returned in Val and the function returns true...
Definition: ss.cpp:447
TIter EndI() const
Definition: hash.h:218
int GetFlds() const
Returns the number of fields in the current line.
Definition: ss.h:116
const char * GetFld(const int &FldN) const
Returns the contents of the field at index FldN.
Definition: ss.h:129
const TStr NODES_START
Definition: gio.h:7
const TStr END_SENTINEL
Definition: gio.h:8
bool GetFlt(const int &FldN, double &Val) const
If the field FldN is a float its value is returned in Val and the function returns true...
Definition: ss.cpp:485
Definition: dt.h:412
bool Next()
Loads next line from the input file.
Definition: ss.cpp:412
const TStr NULL_VAL
Definition: gio.h:15
void TSnap::ReebRefine ( PNGraph Graph,
TIntH t,
int  e,
PNGraph gFinal,
TIntH tFinal,
bool  collapse 
)

Definition at line 984 of file cmty.cpp.

984  {
985  TIntIntVH components;
986  TIntIntVH ct;
987 
988  int newId = 0; //get first new free id;
989 
990  // gett first and last t
991  int first = 429496729;
992  int last = -1;
993 
994  // smarter way of determining focus time points
995  TIntV timePoints;
996 
997  // get first and last time point
998  for (THashKeyDatI<TInt, TInt> it = t.BegI(); !it.IsEnd(); it++) {
999  if (it.GetDat() < first)
1000  first = it.GetDat();
1001  if (it.GetDat() > last)
1002  last = it.GetDat();
1003  }
1004 
1005  // adding focus timepoints
1006  // this can be put in the previous (first, last time point detection) iteration if breaking borders is not an issue
1007  for (THashKeyDatI<TInt, TInt> it = t.BegI(); !it.IsEnd(); it++) {
1008  if (it.GetDat() - (e / 2) >= first)
1009  timePoints.Add(it.GetDat() - (e / 2) /*- 0.1*/);
1010  timePoints.Add(it.GetDat());
1011  if (it.GetDat() + (e / 2) <= last)
1012  timePoints.Add(it.GetDat() + (e / 2) /*+ 0.1*/);
1013  }
1014 
1015  TIntV timePointsUnique;
1016  int prevtp = -1;
1017  //get unique time points
1018  for (int i = 0; i < timePoints.Len(); i++){
1019  if (timePoints[i] > prevtp)
1020  timePointsUnique.Add(timePoints[i]);
1021  prevtp = timePoints[i];
1022  }
1023 
1024  timePoints.Clr();
1025  timePoints = timePointsUnique;
1026 
1027  //iterate each time point
1028  for (int i = 0; i < timePoints.Len(); i++) {
1029 
1030  int focusTimePoint = timePoints[i];
1031 
1032  TIntV fnodes; // all the nodes int the focus in that step
1033 
1034  // getting nodes in focus -- in epsilon
1035  for (THashKeyDatI<TInt, TInt> it = t.BegI(); !it.IsEnd(); it++) {
1036  if ((it.GetDat() <= focusTimePoint + (e / 2)) && (it.GetDat() >= focusTimePoint - (e / 2)))
1037  fnodes.Add(it.GetKey());
1038  }
1039 
1040  // create graph from nodes in focus
1041  PNGraph g1 = TNGraph::New();
1042  for (int i = 0; i < fnodes.Len(); i++) {
1043  if (!g1->IsNode(fnodes[i]))
1044  g1->AddNode(fnodes[i]);
1045  // lower star
1046  for (int j = 0; j < Graph->GetNI(fnodes[i]).GetInDeg(); j++) {
1047  int NeighId = Graph->GetNI(fnodes[i]).GetInNId(j);
1048  if (t.GetDat(NeighId) < focusTimePoint - (e / 2)) {
1049 
1050  }
1051  else {
1052  if (!g1->IsNode(NeighId))
1053  g1->AddNode(NeighId);
1054  g1->AddEdge(NeighId, fnodes[i]);
1055  }
1056  }
1057  // upper star
1058  for (int j = 0; j < Graph->GetNI(fnodes[i]).GetOutDeg(); j++) {
1059  int NeighId = Graph->GetNI(fnodes[i]).GetOutNId(j);
1060  if (t.GetDat(NeighId) > focusTimePoint + (e / 2)) {
1061 
1062  }
1063  else {
1064  if (!g1->IsNode(NeighId))
1065  g1->AddNode(NeighId);
1066  g1->AddEdge(fnodes[i], NeighId);
1067  }
1068  }
1069  }
1070 
1071  // getting results from commponents detection and recording elements of components and timestamps of components
1072  TIntH inCompCount;
1073  TIntIntVH comps;
1074  int compBr = 0;
1075  TIntH nn_nodes;
1076 
1077  int FTP = focusTimePoint;
1078  TIntH TEdges;
1079 
1080  for (TNGraph::TNodeI NI = g1->BegNI(); NI < g1->EndNI(); NI++) {
1081 
1082 
1083  int FTPNode = NI.GetId();
1084  TNGraph::TNodeI GNI = Graph->GetNI(FTPNode);
1085  int FI, FO, RI, RO, I, O;
1086 
1087  RI = NI.GetInDeg();
1088  RO = NI.GetOutDeg();
1089 
1090  FI = Graph->GetNI(FTPNode).GetInDeg() - RI;
1091  FO = Graph->GetNI(FTPNode).GetOutDeg() - RO;
1092 
1093  if (focusTimePoint + (e / 2) == t.GetDat(NI.GetId())) { // if its on the right edge only in degree is observed
1094  RO = FO = 0;
1095  }
1096  if (focusTimePoint - (e / 2) == t.GetDat(NI.GetId())) { // if its on the left edge only out degree is observed
1097  RI = FI = 0;
1098  }
1099 
1100  I = RI + FI;
1101  O = RO + FO;
1102 
1103  // counting edges imidiately after time point
1104  int temp = 0;
1105  if (TEdges.IsKey(FTP))
1106  temp = TEdges.GetDat(FTP);
1107  TEdges.AddDat(FTP, O + temp);
1108 
1109  // FIND ELEMENTS
1110 
1111  // n - n,
1112  if (I > 1 && O > 1) {
1113  // number of nodes is in our out degree
1114  int nn = I;
1115  if (O > I)
1116  nn = O;
1117 
1118  TIntV nds;
1119  nds.Add(FTPNode);
1120  for (int i = 0; i < I; i++) {
1121  nds.Add(GNI.GetInNId(i));
1122  }
1123 
1124  for (int i = 0; i < O; i++) {
1125  nds.Add(GNI.GetOutNId(i));
1126  }
1127 
1128  for (int j = 0; j < nn; j++) {
1129  nn_nodes.AddDat(compBr);
1130  comps.AddDat(compBr, nds);
1131  compBr++;
1132  }
1133  }
1134 
1135  // 1 - n
1136  else if (I == 1 && O > 1) {
1137  for (int i = 0; i < O; i++) {
1138  TIntV nds;
1139  nds.Add(FTPNode);
1140  nds.Add(GNI.GetInNId(0));
1141  nds.Add(GNI.GetOutNId(i));
1142  comps.AddDat(compBr, nds);
1143  compBr++;
1144  }
1145  }
1146 
1147  // n - 1
1148  else if (I > 1 && O == 1) {
1149  for (int i = 0; i < I; i++) {
1150  TIntV nds;
1151  nds.Add(FTPNode);
1152  nds.Add(GNI.GetOutNId(0));
1153  nds.Add(GNI.GetInNId(i));
1154  comps.AddDat(compBr, nds);
1155  compBr++;
1156  }
1157  }
1158 
1159  // 0 - n
1160  else if (I == 0 && O > 1) {
1161  for (int i = 0; i < O; i++) {
1162  TIntV nds;
1163  nds.Add(FTPNode);
1164  nds.Add(GNI.GetOutNId(i));
1165  comps.AddDat(compBr, nds);
1166  compBr++;
1167  }
1168  }
1169 
1170  // n - 0
1171  else if (I > 1 && O == 0) {
1172  for (int i = 0; i < I; i++) {
1173  TIntV nds;
1174  nds.Add(FTPNode);
1175  nds.Add(GNI.GetInNId(i));
1176  comps.AddDat(compBr, nds);
1177  compBr++;
1178  }
1179  }
1180 
1181  // 1 - 1
1182  else if (I == 1 && O == 1) {
1183  TIntV nds;
1184  nds.Add(FTPNode);
1185  nds.Add(GNI.GetOutNId(0));
1186  nds.Add(GNI.GetInNId(0));
1187  comps.AddDat(compBr, nds);
1188  compBr++;
1189  }
1190 
1191  // 0 - 1
1192  else if (I == 0 && O == 1) {
1193  TIntV nds;
1194  nds.Add(FTPNode);
1195  nds.Add(GNI.GetOutNId(0));
1196  comps.AddDat(compBr, nds);
1197  compBr++;
1198  }
1199 
1200  // 1 - 0
1201  else if (I == 1 && O == 0) {
1202  TIntV nds;
1203  nds.Add(FTPNode);
1204  nds.Add(GNI.GetInNId(0));
1205  comps.AddDat(compBr, nds);
1206  compBr++;
1207  }
1208 
1209 
1210 
1211  } // end iterate each node
1212 
1213  // connecting inside of epsilon
1214 
1215  TIntIntVH elements;
1216  TIntH banned;
1217  for (int cc0 = 0; cc0 < comps.Len(); cc0++) {
1218  for (int cc1 = cc0; cc1 < comps.Len(); cc1++) {
1219  int smaller = comps[cc0].Len();
1220  int smaller_id = cc0;
1221  if (cc0 != cc1) {
1222  if (comps[cc1].Len() < smaller) {
1223  smaller = comps[cc1].Len();
1224  smaller_id = cc1;
1225  }
1226  int vi = TSnapDetail::vectorIntersect(comps[cc0], comps[cc1]);
1227  if (vi == smaller && !nn_nodes.IsKey(smaller_id)){
1228  banned.AddDat(smaller_id);
1229  }
1230  /*else if (smaller > 2 && vi == smaller - 1 && !nn_nodes.IsKey(smaller_id)) {
1231  TSnapDetail::transitiveTransform(comps[cc0], comps[cc1]);
1232  banned.AddDat(cc0);
1233  }*/
1234  }
1235  }
1236  }
1237 
1238  // add transitivity connection
1239 
1240  /*
1241  int max_out_tp = -1;
1242  int max_out = -1;
1243  for (THashKeyDatI<TInt, TInt> it = TEdges.BegI(); !it.IsEnd(); it++) {
1244  if (it.GetDat() > max_out) {
1245  max_out = it.GetDat();
1246  max_out_tp = it.GetKey();
1247  }
1248  }
1249  */
1250  for (int cc0 = 0; cc0 < comps.Len(); cc0++) {
1251  if (!banned.IsKey(cc0) /*&& TSnapDetail::chekIfCrossing(comps[cc0], t, first, last, max_out_tp)*/)
1252  elements.AddDat(cc0, comps[cc0]);
1253  }
1254 
1255 
1256  TIntV communitiesAtT;
1257  for (int cc = 0; cc < elements.Len(); cc++) {
1258  components.AddDat(newId, elements[cc]);
1259  communitiesAtT.Add(newId);
1260  newId++;
1261  }
1262  if (elements.Len() > 0)
1263  ct.AddDat(focusTimePoint, communitiesAtT);
1264 
1265  } // FOR
1266 
1267  // connecting neighbouring components
1268  THashKeyDatI<TInt, TIntV> it = ct.BegI();
1269  THashKeyDatI<TInt, TIntV> prelast = ct.EndI()--;
1270  prelast--;
1271  while (it < prelast) {
1272  TIntV cms0;
1273  TIntV cms1;
1274  int focusTimePoint;
1275  int focusTimePoint1;
1276  focusTimePoint = it.GetKey();
1277  cms0 = it.GetDat();
1278  it++;
1279  focusTimePoint1 = it.GetKey();
1280  cms1 = it.GetDat();
1281  if (cms0.Len() > 0 && cms1.Len() > 0) {
1282  for (int i = 0; i < cms0.Len(); i++) {
1283  for (int j = 0; j < cms1.Len(); j++) {
1284  TIntV ids0 = components.GetDat(cms0[i]);
1285  TIntV ids1 = components.GetDat(cms1[j]);
1286  int smaller = ids0.Len();
1287  if (ids1.Len() < smaller)
1288  smaller = ids1.Len();
1289 
1290  if (TSnapDetail::vectorIntersect(ids0, ids1) == smaller || (smaller > 2 && TSnapDetail::vectorIntersect(ids0, ids1) == (smaller -1 ))) {
1291  if (!gFinal->IsNode(cms0[i])) {
1292  gFinal->AddNode(cms0[i]);
1293  tFinal.AddDat(cms0[i], focusTimePoint);
1294  }
1295  if (!gFinal->IsNode(cms1[j])) {
1296  gFinal->AddNode(cms1[j]);
1297  tFinal.AddDat(cms1[j], focusTimePoint1);
1298  }
1299  gFinal->AddEdge(cms0[i], cms1[j]);
1300  }
1301  }
1302  }
1303  }
1304  }// end connecting components
1305 
1306  // collapsing chains
1307  if (collapse) {
1308  for (TNGraph::TNodeI NI = gFinal->BegNI(); NI < gFinal->EndNI(); NI++) {
1309  if (NI.GetInDeg() == 1 && NI.GetOutDeg() == 1)
1310  if (gFinal->GetNI(NI.GetInNId(0)).GetOutDeg() == 1 && gFinal->GetNI(NI.GetOutNId(0)).GetInDeg() == 1)
1311  {
1312  gFinal->AddEdge(NI.GetInNId(0), NI.GetOutNId(0));
1313  gFinal->DelEdge(NI.GetInNId(0), NI.GetId());
1314  tFinal.DelKey(NI.GetId());
1315  gFinal->DelNode(NI.GetId());
1316  }
1317  }
1318  }// end collapsing
1319 
1320 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
TIter BegI() const
Definition: hash.h:213
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
TIter EndI() const
Definition: hash.h:218
const TKey & GetKey() const
Definition: hash.h:80
void DelKey(const TKey &Key)
Definition: hash.h:404
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
const TDat & GetDat() const
Definition: hash.h:81
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
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
int vectorIntersect(TIntV &a, TIntV &b)
Definition: cmty.cpp:138
Definition: bd.h:196
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:412
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
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:416
void TSnap::ReebSimplify ( PNGraph Graph,
TIntH t,
int  e,
PNGraph gFinal,
TIntH tFinal,
bool  collapse 
)

Definition at line 844 of file cmty.cpp.

844  {
845  TIntIntVH components;
846  TIntIntVH ct;
847 
848  int newId = 0; //get first new free id;
849 
850  // gett first and last t
851  int first = 429496729;
852  int last = -1;
853 
854  // smarter way of determining focus time points
855  TIntV timePoints;
856 
857  // get first and last time point
858  for (THashKeyDatI<TInt, TInt> it = t.BegI(); !it.IsEnd(); it++) {
859  if (it.GetDat()<first)
860  first = it.GetDat();
861  if (it.GetDat()>last)
862  last = it.GetDat();
863  }
864 
865  // adding focus timepoints
866  // this can be put in the previous (first, last time point detection) iteration if breaking borders is not an issue
867  for (THashKeyDatI<TInt, TInt> it = t.BegI(); !it.IsEnd(); it++) {
868  if (it.GetDat() - (e / 2) >= first)
869  timePoints.Add(it.GetDat() - (e / 2) /*- 0.1*/);
870  timePoints.Add(it.GetDat());
871  if (it.GetDat() + (e / 2) <= last)
872  timePoints.Add(it.GetDat() + (e / 2) /*+ 0.1*/);
873  }
874 
875 
876  //iterate each time point
877  for (int i = 0; i<timePoints.Len(); i++) {
878 
879  int focusTimePoint = timePoints[i];
880 
881  TIntV fnodes; // all the nodes int the focus in that step
882 
883  // getting nodes in focus -- in epsilon
884  for (THashKeyDatI<TInt, TInt> it = t.BegI(); !it.IsEnd(); it++) {
885  if ((it.GetDat() <= focusTimePoint + (e / 2)) && (it.GetDat() >= focusTimePoint - (e / 2)))
886  fnodes.Add(it.GetKey());
887  }
888 
889  // create graph from nodes in focus
890  PNGraph g1 = TNGraph::New();
891  for (int i = 0; i<fnodes.Len(); i++) {
892  if (!g1->IsNode(fnodes[i]))
893  g1->AddNode(fnodes[i]);
894  // lower star
895  for (int j = 0; j<Graph->GetNI(fnodes[i]).GetInDeg(); j++) {
896  int NeighId = Graph->GetNI(fnodes[i]).GetInNId(j);
897  if (t.GetDat(NeighId)<focusTimePoint - (e / 2)) {
898 
899  }
900  else {
901  if (!g1->IsNode(NeighId))
902  g1->AddNode(NeighId);
903  g1->AddEdge(NeighId, fnodes[i]);
904  }
905  }
906  // upper star
907  for (int j = 0; j<Graph->GetNI(fnodes[i]).GetOutDeg(); j++) {
908  int NeighId = Graph->GetNI(fnodes[i]).GetOutNId(j);
909  if (t.GetDat(NeighId)>focusTimePoint + (e / 2)) {
910 
911  }
912  else {
913  if (!g1->IsNode(NeighId))
914  g1->AddNode(NeighId);
915  g1->AddEdge(fnodes[i], NeighId);
916  }
917  }
918  }
919 
920  // getting results from commponents detection and recording elements of components and timestamps of components
921  TCnComV CnComV;
922  GetWccs(g1, CnComV);
923  TIntV communitiesAtT;
924  for (int cc = 0; cc < CnComV.Len(); cc++) {
925  components.AddDat(newId, CnComV[cc].NIdV);
926  communitiesAtT.Add(newId);
927  newId++;
928  }
929  if (CnComV.Len() > 0)
930  ct.AddDat(focusTimePoint, communitiesAtT);
931  } // end iterate each node
932 
933  // connecting neighbouring components
935  THashKeyDatI<TInt, TIntV> prelast = ct.EndI()--;
936  prelast--;
937  while (it < prelast) {
938  TIntV cms0;
939  TIntV cms1;
940  int focusTimePoint;
941  int focusTimePoint1;
942  focusTimePoint = it.GetKey();
943  cms0 = it.GetDat();
944  it++;
945  focusTimePoint1 = it.GetKey();
946  cms1 = it.GetDat();
947  if (cms0.Len()>0 && cms1.Len() > 0) {
948  for (int i = 0; i < cms0.Len(); i++) {
949  for (int j = 0; j < cms1.Len(); j++) {
950  TIntV ids0 = components.GetDat(cms0[i]);
951  TIntV ids1 = components.GetDat(cms1[j]);
952  if (ids0.IntrsLen(ids1) > 0 || TSnapDetail::edgeIntersect(Graph, ids0, ids1)) {
953  if (!gFinal->IsNode(cms0[i])) {
954  gFinal->AddNode(cms0[i]);
955  tFinal.AddDat(cms0[i], focusTimePoint);
956  }
957  if (!gFinal->IsNode(cms1[j])) {
958  gFinal->AddNode(cms1[j]);
959  tFinal.AddDat(cms1[j], focusTimePoint1);
960  }
961  gFinal->AddEdge(cms0[i], cms1[j]);
962  }
963  }
964  }
965  }
966  }// end connecting components
967 
968  // collapsing chains
969  if (collapse) {
970  for (TNGraph::TNodeI NI = gFinal->BegNI(); NI < gFinal->EndNI(); NI++) {
971  if (NI.GetInDeg() == 1 && NI.GetOutDeg() == 1)
972  if (gFinal->GetNI(NI.GetInNId(0)).GetOutDeg() == 1 && gFinal->GetNI(NI.GetOutNId(0)).GetInDeg() == 1)
973  {
974  gFinal->AddEdge(NI.GetInNId(0), NI.GetOutNId(0));
975  gFinal->DelEdge(NI.GetInNId(0), NI.GetId());
976  tFinal.DelKey(NI.GetId());
977  gFinal->DelNode(NI.GetId());
978  }
979  }
980  }// end collapsing
981 
982 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
TIter BegI() const
Definition: hash.h:213
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
TIter EndI() const
Definition: hash.h:218
const TKey & GetKey() const
Definition: hash.h:80
void DelKey(const TKey &Key)
Definition: hash.h:404
const TDat & GetDat() const
Definition: hash.h:81
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
TSizeTy IntrsLen(const TVec< TVal, TSizeTy > &ValV) const
Returns the size of the intersection of vectors this and ValV. Assumes the vectors are sorted! ...
Definition: ds.h:1479
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
Definition: bd.h:196
bool edgeIntersect(PNGraph &graph, TIntV &a, TIntV &b)
Definition: cmty.cpp:127
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
Definition: cncom.h:376
void TSnap::Relabel ( TPRManager PRM,
const int &  NId,
const TNEANet::TNodeI NI 
)

Increases the label of a node NId to allow valid pushes to some neighbor.

Definition at line 305 of file flow.cpp.

305  {
306  int MaxLabel = PRM.GetMaxLabel();
307  int MinLabel = MaxLabel;
308  for (int EdgeN = 0; EdgeN < NI.GetInDeg(); EdgeN++) {
309  if (PRM.Flow(NI.GetInEId(EdgeN)) > 0) {
310  int InLabel = PRM.Label(NI.GetInNId(EdgeN));
311  MinLabel = MIN(MinLabel, InLabel);
312  }
313  }
314  for (int EdgeN = 0; EdgeN < NI.GetOutDeg(); EdgeN++) {
315  if (PRM.Capacity(NI.GetOutEId(EdgeN)) > PRM.Flow(NI.GetOutEId(EdgeN))) {
316  int OutLabel = PRM.Label(NI.GetOutNId(EdgeN));
317  MinLabel = MIN(MinLabel, OutLabel);
318  }
319  }
320  if (MinLabel == MaxLabel) {
321  PRM.SetLabel(NId, MaxLabel);
322  } else {
323  PRM.SetLabel(NId, MinLabel + 1);
324  }
325 }
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: network.h:1821
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:1813
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: network.h:1817
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: network.h:1835
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: network.h:1833
#define MIN(a, b)
Definition: bd.h:346
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
template<class PGraph >
int TSnap::SamplePersonalizedPageRank ( const PGraph &  Graph,
double  JumpProb,
const TIntV StartNIdV,
TRnd Rnd 
)

Definition at line 67 of file randwalk.h.

67  {
68  int locationId = StartNIdV.GetRndVal(Rnd);
69  //printf("starting walk at %d\n", locationId);
70  while (Rnd.GetUniDev() >= JumpProb) {
71  TNGraph::TNodeI location = Graph->GetNI(locationId);
72  int d = location.GetOutDeg();
73  if (d > 0)
74  locationId = location.GetOutNId(Rnd.GetUniDevInt(d));
75  else
76  locationId = StartNIdV.GetRndVal(Rnd);
77  }
78  return locationId;
79 }
const TVal & GetRndVal(TRnd &Rnd=TInt::Rnd) const
Returns a reference to a random element in the vector.
Definition: ds.h:589
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
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
template<class PGraph >
void TSnap::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>="">

Definition at line 245 of file gio.h.

245  {
246  FILE *F = fopen(OutFNm.CStr(), "wt");
247  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) { fprintf(F, "# Directed graph: %s \n", OutFNm.CStr()); }
248  else { fprintf(F, "# Undirected graph (each unordered pair of nodes is saved once): %s\n", OutFNm.CStr()); }
249  if (! Desc.Empty()) { fprintf(F, "# %s\n", Desc.CStr()); }
250  fprintf(F, "# Nodes: %d Edges: %d\n", Graph->GetNodes(), Graph->GetEdges());
251  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) { fprintf(F, "# FromNodeId\tToNodeId\n"); }
252  else { fprintf(F, "# NodeId\tNodeId\n"); }
253  for (typename PGraph::TObj::TEdgeI ei = Graph->BegEI(); ei < Graph->EndEI(); ei++) {
254  fprintf(F, "%d\t%d\n", ei.GetSrcNId(), ei.GetDstNId());
255  }
256  fclose(F);
257 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
bool Empty() const
Definition: dt.h:491
char * CStr()
Definition: dt.h:479
void TSnap::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.

Definition at line 269 of file gio.cpp.

269  {
270  FILE *F = fopen(OutFNm.CStr(), "wt");
271  fprintf(F, "# Directed network: %s \n", OutFNm.CStr());
272  if (! Desc.Empty()) { fprintf(F, "# %s\n", Desc.CStr()); }
273  fprintf(F, "# Nodes: %d Edges: %d\n", Graph->GetNodes(), Graph->GetEdges());
274 
275  TStrV IntAttrNNames;
276  TStrV FltAttrNNames;
277  TStrV StrAttrNNames;
278  Graph->GetAttrNNames(IntAttrNNames, FltAttrNNames, StrAttrNNames);
279  WriteNodeSchemaToFile(F, IntAttrNNames, FltAttrNNames, StrAttrNNames);
280  WriteNodesToFile(F, Graph, IntAttrNNames, FltAttrNNames, StrAttrNNames);
281  fprintf(F, "%s\n", END_SENTINEL.CStr());
282 
283  TStrV IntAttrENames;
284  TStrV FltAttrENames;
285  TStrV StrAttrENames;
286  Graph->GetAttrENames(IntAttrENames, FltAttrENames, StrAttrENames);
287  WriteEdgeSchemaToFile(F, IntAttrENames, FltAttrENames, StrAttrENames);
288  WriteEdgesToFile(F, Graph, IntAttrENames, FltAttrENames, StrAttrENames);
289  fprintf(F, "%s\n", END_SENTINEL.CStr());
290 
291  fclose(F);
292 }
void WriteEdgesToFile(FILE *F, const PNEANet &Graph, TStrV &IntAttrENames, TStrV &FltAttrENames, TStrV &StrAttrENames)
Definition: gio.cpp:238
const TStr END_SENTINEL
Definition: gio.h:8
void WriteEdgeSchemaToFile(FILE *F, TStrV &IntAttrENames, TStrV &FltAttrENames, TStrV &StrAttrENames)
Definition: gio.cpp:221
bool Empty() const
Definition: dt.h:491
void WriteNodesToFile(FILE *F, const PNEANet &Graph, TStrV &IntAttrNNames, TStrV &FltAttrNNames, TStrV &StrAttrNNames)
Definition: gio.cpp:188
char * CStr()
Definition: dt.h:479
void WriteNodeSchemaToFile(FILE *F, TStrV &IntAttrNNames, TStrV &FltAttrNNames, TStrV &StrAttrNNames)
Definition: gio.cpp:171
template<class PGraph >
void TSnap::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.

Save a graph in GraphVizp .DOT format.

Parameters
NIdColorHMaps node ids to node colors (see GraphViz documentation for more details).

Definition at line 387 of file gio.h.

387  {
388  const bool IsDir = HasGraphFlag(typename PGraph::TObj, gfDirected);
389  FILE *F = fopen(OutFNm.CStr(), "wt");
390  if (! Desc.Empty()) fprintf(F, "/*****\n%s\n*****/\n\n", Desc.CStr());
391  if (IsDir) { fprintf(F, "digraph G {\n"); } else { fprintf(F, "graph G {\n"); }
392  fprintf(F, " graph [splines=false overlap=false]\n"); //size=\"12,10\" ratio=fill
393  // node [width=0.3, height=0.3, label=\"\", style=filled, color=black]
394  // node [shape=box, width=0.3, height=0.3, label=\"\", style=filled, fillcolor=red]
395  fprintf(F, " node [shape=ellipse, width=0.3, height=0.3%s]\n", NodeLabels?"":", label=\"\"");
396  // node colors
397  //for (int i = 0; i < NIdColorH.Len(); i++) {
398  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
399  if (NIdColorH.IsKey(NI.GetId())) {
400  fprintf(F, " %d [style=filled, fillcolor=\"%s\"];\n", NI.GetId(), NIdColorH.GetDat(NI.GetId()).CStr()); }
401  else {
402  fprintf(F, " %d ;\n", NI.GetId());
403  }
404  }
405  // edges
406  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
407  if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 && !NIdColorH.IsKey(NI.GetId())) {
408  fprintf(F, "%d;\n", NI.GetId()); }
409  else {
410  for (int e = 0; e < NI.GetOutDeg(); e++) {
411  if (! IsDir && NI.GetId() > NI.GetOutNId(e)) { continue; }
412  fprintf(F, " %d %s %d;\n", NI.GetId(), IsDir?"->":"--", NI.GetOutNId(e));
413  }
414  }
415  }
416  if (! Desc.Empty()) {
417  fprintf(F, " label = \"\\n%s\\n\";", Desc.CStr());
418  fprintf(F, " fontsize=24;\n");
419  }
420  fprintf(F, "}\n");
421  fclose(F);
422 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
bool Empty() const
Definition: dt.h:491
char * CStr()
Definition: dt.h:479
bool IsKey(const TKey &Key) const
Definition: hash.h:258
template<class PGraph >
void TSnap::SaveGViz ( const PGraph &  Graph,
const TStr OutFNm,
const TStr Desc,
const TIntStrH NIdLabelH 
)

Save a graph in GraphVizp .DOT format.

Save a graph in GraphVizp .DOT format.

Parameters
NIdLabelHMaps node ids to node string labels.

Definition at line 425 of file gio.h.

425  {
426  const bool IsDir = Graph->HasFlag(gfDirected);
427  FILE *F = fopen(OutFNm.CStr(), "wt");
428  if (! Desc.Empty()) fprintf(F, "/*****\n%s\n*****/\n\n", Desc.CStr());
429  if (IsDir) { fprintf(F, "digraph G {\n"); } else { fprintf(F, "graph G {\n"); }
430  fprintf(F, " graph [splines=true overlap=false]\n"); //size=\"12,10\" ratio=fill
431  fprintf(F, " node [shape=ellipse, width=0.3, height=0.3]\n");
432  // node colors
433  //for (int i = 0; i < NodeLabelH.Len(); i++) {
434  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
435  fprintf(F, " %d [label=\"%s\"];\n", NI.GetId(), NIdLabelH.GetDat(NI.GetId()).CStr());
436 }
437  // edges
438  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
439  if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 && ! NIdLabelH.IsKey(NI.GetId())) {
440  fprintf(F, "%d;\n", NI.GetId()); }
441  else {
442  for (int e = 0; e < NI.GetOutDeg(); e++) {
443  if (! IsDir && NI.GetId() > NI.GetOutNId(e)) { continue; }
444  fprintf(F, " %d %s %d;\n", NI.GetId(), IsDir?"->":"--", NI.GetOutNId(e));
445  }
446  }
447  }
448  if (! Desc.Empty()) {
449  fprintf(F, " label = \"\\n%s\\n\";", Desc.CStr());
450  fprintf(F, " fontsize=24;\n");
451  }
452  fprintf(F, "}\n");
453  fclose(F);
454 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
bool Empty() const
Definition: dt.h:491
char * CStr()
Definition: dt.h:479
bool IsKey(const TKey &Key) const
Definition: hash.h:258
template<class PGraph >
void TSnap::SaveMatlabSparseMtx ( const PGraph &  Graph,
const TStr OutFNm 
)

Saves a graph in a MATLAB sparse matrix format.

Each line contains a tuple of 3 values: <source node="" id>=""><tab><destination node="" id>=""><tab>1.

Definition at line 369 of file gio.h.

369  {
370  FILE *F = fopen(OutFNm.CStr(), "wt");
371  TIntSet NIdSet(Graph->GetNodes()); // so that
372  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
373  NIdSet.AddKey(NI.GetId());
374  }
375  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
376  const int Src = NIdSet.GetKeyId(EI.GetSrcNId())+1;
377  const int Dst = NIdSet.GetKeyId(EI.GetDstNId())+1;
378  fprintf(F, "%d\t%d\t1\n", Src, Dst);
379  if (! HasGraphFlag(typename PGraph::TObj, gfDirected) && Src!=Dst) {
380  fprintf(F, "%d\t%d\t1\n", Dst, Src);
381  }
382  }
383  fclose(F);
384 }
#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
char * CStr()
Definition: dt.h:479
template<class PGraph >
void TSnap::SavePajek ( const PGraph &  Graph,
const TStr OutFNm 
)

Saves a graph in a Pajek .NET format.

Definition at line 260 of file gio.h.

260  {
261  TIntH NIdToIdH(Graph->GetNodes(), true);
262  FILE *F = fopen(OutFNm.CStr(), "wt");
263  fprintf(F, "*Vertices %d\n", Graph->GetNodes());
264  int i = 0;
265  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
266  fprintf(F, "%d \"%d\" ic Red fos 10\n", i+1, NI.GetId()); // ic: internal color, fos: font size
267  NIdToIdH.AddDat(NI.GetId(), i+1);
268  }
269  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
270  fprintf(F, "*Arcs %d\n", Graph->GetEdges()); } // arcs are directed, edges are undirected
271  else {
272  fprintf(F, "*Edges %d\n", Graph->GetEdges());
273  }
274  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
275  const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
276  const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
277  fprintf(F, "%d %d %d c Black\n", SrcNId, DstNId, 1); // width=1
278  }
279  fclose(F);
280 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
char * CStr()
Definition: dt.h:479
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
void TSnap::SavePajek ( const PGraph &  Graph,
const TStr OutFNm,
const TIntStrH NIdColorH 
)

Saves a graph in a Pajek .NET format.

NIdColorH maps node ids to node colors. Default node color is Red. See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/pajekman.pdf for a list of supported color names.

Definition at line 285 of file gio.h.

285  {
286  TIntH NIdToIdH(Graph->GetNodes(), true);
287  FILE *F = fopen(OutFNm.CStr(), "wt");
288  fprintf(F, "*Vertices %d\n", Graph->GetNodes());
289  int i = 0;
290  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
291  fprintf(F, "%d \"%d\" ic %s fos 10\n", i+1, NI.GetId(),
292  NIdColorH.IsKey(NI.GetId()) ? NIdColorH.GetDat(NI.GetId()).CStr() : "Red");
293  NIdToIdH.AddDat(NI.GetId(), i+1);
294  }
295  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
296  fprintf(F, "*Arcs %d\n", Graph->GetEdges()); } // arcs are directed, edges are undirected
297  else {
298  fprintf(F, "*Edges %d\n", Graph->GetEdges());
299  }
300  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
301  const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
302  const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
303  fprintf(F, "%d %d %d c Black\n", SrcNId, DstNId, 1);
304  }
305  fclose(F);
306 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
char * CStr()
Definition: dt.h:479
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
void TSnap::SavePajek ( const PGraph &  Graph,
const TStr OutFNm,
const TIntStrH NIdColorH,
const TIntStrH NIdLabelH 
)

Saves a graph in a Pajek .NET format.

NIdColorH maps node ids to node colors. Default node color is Red. NIdLabelH maps node ids to node string labels. See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/pajekman.pdf for a list of supported color names.

Definition at line 312 of file gio.h.

312  {
313  TIntH NIdToIdH(Graph->GetNodes(), true);
314  FILE *F = fopen(OutFNm.CStr(), "wt");
315  fprintf(F, "*Vertices %d\n", Graph->GetNodes());
316  int i = 0;
317  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
318  fprintf(F, "%d \"%s\" ic %s fos 10\n", i+1,
319  NIdLabelH.IsKey(NI.GetId()) ? NIdLabelH.GetDat(NI.GetId()).CStr() : TStr::Fmt("%d", NI.GetId()).CStr(),
320  NIdColorH.IsKey(NI.GetId()) ? NIdColorH.GetDat(NI.GetId()).CStr() : "Red");
321  NIdToIdH.AddDat(NI.GetId(), i+1);
322  }
323  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
324  fprintf(F, "*Arcs %d\n", Graph->GetEdges()); } // arcs are directed, edges are undirected
325  else {
326  fprintf(F, "*Edges %d\n", Graph->GetEdges());
327  }
328  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
329  const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
330  const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
331  fprintf(F, "%d %d %d c Black\n", SrcNId, DstNId, 1);
332  }
333  fclose(F);
334 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
template<class PGraph >
void TSnap::SavePajek ( const PGraph &  Graph,
const TStr OutFNm,
const TIntStrH NIdColorH,
const TIntStrH NIdLabelH,
const TIntStrH EIdColorH 
)

Saves a graph in a Pajek .NET format.

NIdColorH maps node ids to node colors. Default node color is Red. NIdLabelH maps node ids to node string labels. EIdColorH maps edge ids to node colors. Default edge color is Black. See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/pajekman.pdf for a list of supported color names.

Definition at line 341 of file gio.h.

341  {
342  CAssert(HasGraphFlag(typename PGraph::TObj, gfMultiGraph)); // network needs to have edge ids
343  TIntH NIdToIdH(Graph->GetNodes(), true);
344  FILE *F = fopen(OutFNm.CStr(), "wt");
345  fprintf(F, "*Vertices %d\n", Graph->GetNodes());
346  int i = 0;
347  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
348  fprintf(F, "%d \"%s\" ic %s fos 10\n", i+1,
349  NIdLabelH.IsKey(NI.GetId()) ? NIdLabelH.GetDat(NI.GetId()).CStr() : TStr::Fmt("%d", NI.GetId()).CStr(),
350  NIdColorH.IsKey(NI.GetId()) ? NIdColorH.GetDat(NI.GetId()).CStr() : "Red");
351  NIdToIdH.AddDat(NI.GetId(), i+1);
352  }
353  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
354  fprintf(F, "*Arcs %d\n", Graph->GetEdges()); } // arcs are directed, edges are undirected
355  else {
356  fprintf(F, "*Edges %d\n", Graph->GetEdges());
357  }
358  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
359  const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
360  const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
361  fprintf(F, "%d %d 1 c %s\n", SrcNId, DstNId,
362  EIdColorH.IsKey(EI.GetId()) ? EIdColorH.GetDat(EI.GetId()).CStr() : "Black");
363  }
364  fclose(F);
365 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
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
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
#define CAssert(Cond)
Definition: bd.h:302
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:479
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void TSnap::SetAllInvertSign ( TFltV ValV,
const double &  Val 
)

Definition at line 158 of file gsvd.cpp.

158  {
159  for (int i = 0; i < ValV.Len(); i++) {
160  ValV[i] = -ValV[i];
161  }
162 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
template<class PGraph >
void TSnap::TestAnf ( )

Definition at line 240 of file anf.h.

240  {
241  PGraph Graph = PGraph::TObj::New();
242  //Graph:
243  // 0 2 ----> 3
244  // ^ |
245  // | |
246  // | ^
247  // 1 5 <---- 4
248  for (int v = 0; v < 6; v++) { Graph->AddNode(v); }
249  Graph->AddEdge(2, 3);
250  Graph->AddEdge(3, 4);
251  Graph->AddEdge(4, 5);
252  Graph->AddEdge(5, 2);
253  TFltV AnfV;
254  for (int t = 0; t < 10; t++) {
255  TGraphAnf<PGraph> Anf(Graph, 128, 5, t+1);
256  TIntFltKdV DistToNbrsV;
257  Anf.GetGraphAnf(DistToNbrsV, 5, true);
258  printf("\n--seed: %d---------------------\n", t+1);
259  for (int i = 0; i < DistToNbrsV.Len(); i++) {
260  printf("dist: %d\t hops:%f\n", DistToNbrsV[i].Key(), DistToNbrsV[i].Dat());
261  }
262  AnfV.Add(DistToNbrsV.Last().Dat);
263  }
264  TMom Mom(AnfV);
265  printf("-----------\nAvgAnf: %f StDev: %f\n", Mom.GetMean(), Mom.GetSDev());//*/
266  // const int NApprox = 32;
267  /*printf("\nANF vs. SAMPLE diam test (10 runs of ANF, NApprox=%d):\n", NApprox);
268  //Graph = TGGen<PGraph>::GenGrid(20, 20);
269  Graph = TGAlg::GetMxWcc(TGGen<PGraph>::GenRnd(1000, 10000));
270  TFltV FullAnf, EffAnf;
271  for (int tryn = 0; tryn < 10; tryn++) {
272  FullAnf.Add(GetEffDiam(Graph, false, 1.0, NApprox));
273  EffAnf.Add(GetEffDiam(Graph, false, 0.9, NApprox));
274  }
275  TMom FullMom(FullAnf);
276  TMom AnfMom(EffAnf);
277  printf(" Sample FullDiam: %d\n", TGAlg::GetBfsFullDiam(Graph, 100, false));
278  printf(" Anf FullDiam: %f [%f]\n", FullMom.GetMean(), FullMom.GetSDev());
279  printf(" Sample EffDiam [90%%]: %f\n", TGAlg::GetBfsEffDiam(Graph, 100, false));
280  printf(" Anf EffDiam [90%%]: %f [%f]\n", AnfMom.GetMean(), AnfMom.GetSDev());
281  // epinions
282  printf("\nEpinions graph:\n");
283  { typedef PNGraph PGraph;
284  PGraph G = TGGen<PGraph>::GenEpinions();
285  TIntFltKdV DistToPairsV;
286  GetAnf(G, DistToPairsV, 50, true);
287  for(int i = 0; i < DistToPairsV.Len(); i++) {
288  printf("\t%d\t%f\n", DistToPairsV[i].Key, DistToPairsV[i].Dat); }
289  printf("\nUndir\n");
290  TAnf<PGraph>::GetAnf(G, DistToPairsV, 50, false);
291  for(int j = 0; j < DistToPairsV.Len(); j++) {
292  printf("\t%d\t%f\n", DistToPairsV[j].Key, DistToPairsV[j].Dat); }
293  }//*/
294 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: xmath.h:129
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Definition: anf.h:33
void TSnap::TFltVToNumpy ( TFltV FltV,
float *  FltNumpyVecOut,
int  n 
)

Converts TFltV to Numpy array.

Fills the numpyvec array with TFltV vector values. Note that only the first n values are filled.

Definition at line 15 of file numpy.cpp.

15  {
16  int limit = MIN(FltV.Len(), n);
17 
18  for (int i=0; i < limit; i++) {
19  FltNumpyVecOut[i] = static_cast<float>(FltV[i]);
20  }
21 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define MIN(a, b)
Definition: bd.h:346
void TSnap::TIntVToNumpy ( TIntV IntV,
int *  IntNumpyVecOut,
int  n 
)

Converts TIntV to Numpy array.

Fills the numpyvec array with TIntV vector values. Note that only the first n values are filled.

Definition at line 4 of file numpy.cpp.

4  {
5  int limit = MIN(IntV.Len(), n);
6 
7  for (int i=0; i < limit; i++) {
8  IntNumpyVecOut[i] = IntV[i];
9  }
10 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define MIN(a, b)
Definition: bd.h:346
template<class PGraph >
PGraph TSnap::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.

Converts table to a directed/undirected graph. Suitable for PUNGraph and PNGraph, but not for PNEANet where attributes are expected.

Definition at line 8 of file conv.h.

9 {
10  PGraph Graph = PGraph::TObj::New();
11 
12  const TAttrType NodeType = Table->GetColType(SrcCol);
13  Assert(NodeType == Table->GetColType(DstCol));
14  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
15  const TInt DstColIdx = Table->GetColIdx(DstCol);
16 
17  // make single pass over all rows in the table
18  if (NodeType == atInt) {
19  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
20  if ((Table->Next)[CurrRowIdx] == Table->Invalid) { continue; }
21  // add src and dst nodes to graph if they are not seen earlier
22  TInt SVal = (Table->IntCols)[SrcColIdx][CurrRowIdx];
23  TInt DVal = (Table->IntCols)[DstColIdx][CurrRowIdx];
24  //Using AddNodeUnchecked ensures that no error is thrown when the same node is seen twice
25  Graph->AddNodeUnchecked(SVal);
26  Graph->AddNodeUnchecked(DVal);
27  Graph->AddEdgeUnchecked(SVal, DVal);
28  }
29  } else if (NodeType == atFlt) {
30  // node values - i.e. the unique values of src/dst col
31  //THashSet<TInt> IntNodeVals; // for both int and string node attr types.
32  THash<TFlt, TInt> FltNodeVals;
33  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
34  if ((Table->Next)[CurrRowIdx] == Table->Invalid) { continue; }
35  // add src and dst nodes to graph if they are not seen earlier
36  TInt SVal, DVal;
37  TFlt FSVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
38  SVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FSVal);
39  TFlt FDVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
40  DVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FDVal);
41  Graph->AddEdge(SVal, DVal);
42  }
43  } else {
44  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
45  if ((Table->Next)[CurrRowIdx] == Table->Invalid) { continue; }
46  // add src and dst nodes to graph if they are not seen earlier
47  TInt SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
48 // if (strlen(Table->GetContextKey(SVal)) == 0) { continue; } //illegal value
49  TInt DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
50 // if (strlen(Table->GetContextKey(DVal)) == 0) { continue; } //illegal value
51  //Using AddNodeUnchecked ensures that no error is thrown when the same node is seen twice
52  Graph->AddNodeUnchecked(SVal);
53  Graph->AddNodeUnchecked(DVal);
54  Graph->AddEdgeUnchecked(SVal, DVal);
55  }
56  }
57 
58  Graph->SortNodeAdjV();
59  return Graph;
60 }
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
Definition: gbase.h:23
Definition: dt.h:1386
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1137
Definition: hash.h:97
Definition: gbase.h:23
template<class PGraphMP >
PGraphMP TSnap::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.

Definition at line 192 of file conv.h.

192  {
193  // double start = omp_get_wtime();
194  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
195  const TInt DstColIdx = Table->GetColIdx(DstCol);
196  const TAttrType NodeType = Table->GetColType(SrcCol);
197  Assert(NodeType == Table->GetColType(DstCol));
198 
199  const TInt NumRows = Table->NumValidRows;
200 
201  TIntV SrcCol1, DstCol1, SrcCol2, DstCol2;
202 
203  #pragma omp parallel sections num_threads(4)
204  {
205  #pragma omp section
206  { SrcCol1.Reserve(NumRows, NumRows); }
207  #pragma omp section
208  { SrcCol2.Reserve(NumRows, NumRows); }
209  #pragma omp section
210  { DstCol1.Reserve(NumRows, NumRows); }
211  #pragma omp section
212  { DstCol2.Reserve(NumRows, NumRows); }
213  }
214 
215  // double endResize = omp_get_wtime();
216  // printf("Resize time = %f\n", endResize-start);
217 
218  TIntPrV Partitions;
219  Table->GetPartitionRanges(Partitions, omp_get_max_threads());
220  TInt PartitionSize = Partitions[0].GetVal2()-Partitions[0].GetVal1()+1;
221 
222  // double endPartition = omp_get_wtime();
223  // printf("Partition time = %f\n", endPartition-endResize);
224 
225  omp_set_num_threads(omp_get_max_threads());
226  if (NodeType == atInt) {
227  #pragma omp parallel for schedule(static)
228  for (int i = 0; i < Partitions.Len(); i++) {
229  TRowIterator RowI(Partitions[i].GetVal1(), Table());
230  TRowIterator EndI(Partitions[i].GetVal2(), Table());
231  while (RowI < EndI) {
232  TInt RowId = RowI.GetRowIdx();
233  SrcCol1[RowId] = RowI.GetIntAttr(SrcColIdx);
234  SrcCol2[RowId] = RowI.GetIntAttr(SrcColIdx);
235  DstCol1[RowId] = RowI.GetIntAttr(DstColIdx);
236  DstCol2[RowId] = RowI.GetIntAttr(DstColIdx);
237  RowI++;
238  }
239  }
240  }
241  else if (NodeType == atStr) {
242  #pragma omp parallel for schedule(static)
243  for (int i = 0; i < Partitions.Len(); i++) {
244  TRowIterator RowI(Partitions[i].GetVal1(), Table());
245  TRowIterator EndI(Partitions[i].GetVal2(), Table());
246  while (RowI < EndI) {
247  TInt RowId = RowI.GetRowIdx();
248  SrcCol1[RowId] = RowI.GetStrMapById(SrcColIdx);
249  SrcCol2[RowId] = RowI.GetStrMapById(SrcColIdx);
250  DstCol1[RowId] = RowI.GetStrMapById(DstColIdx);
251  DstCol2[RowId] = RowI.GetStrMapById(DstColIdx);
252  RowI++;
253  }
254  }
255  }
256 
257  omp_set_num_threads(omp_get_max_threads());
258  #pragma omp parallel
259  {
260  #pragma omp single nowait
261  {
262  #pragma omp task untied shared(SrcCol1, DstCol1)
263  { TTable::QSortKeyVal(SrcCol1, DstCol1, 0, NumRows-1); }
264  }
265  #pragma omp single nowait
266  {
267  #pragma omp task untied shared(SrcCol2, DstCol2)
268  { TTable::QSortKeyVal(DstCol2, SrcCol2, 0, NumRows-1); }
269  }
270  #pragma omp taskwait
271  }
272 
273  // TTable::PSRSKeyVal(SrcCol1, DstCol1, 0, NumRows-1);
274  // TTable::PSRSKeyVal(DstCol2, SrcCol2, 0, NumRows-1);
275 
276  // TInt IsS = TTable::CheckSortedKeyVal(SrcCol1, DstCol1, 0, NumRows-1);
277  // TInt IsD = TTable::CheckSortedKeyVal(DstCol2, SrcCol2, 0, NumRows-1);
278  // printf("IsSorted = %d %d\n", IsS.Val, IsD.Val);
279 
280  // double endSort = omp_get_wtime();
281  // printf("Sort time = %f\n", endSort-endCopy);
282  //return TNGraphMP::New(10, 100);
283 
284  TInt NumThreads = omp_get_max_threads();
285  TInt PartSize = (NumRows/NumThreads);
286 
287  TIntV SrcOffsets, DstOffsets;
288  SrcOffsets.Add(0);
289  for (TInt i = 1; i < NumThreads; i++) {
290  TInt CurrOffset = i * PartSize;
291  while (CurrOffset < (i+1) * PartSize &&
292  SrcCol1[CurrOffset-1] == SrcCol1[CurrOffset]) {
293  CurrOffset++;
294  }
295  if (CurrOffset < (i+1) * PartSize) { SrcOffsets.Add(CurrOffset); }
296  }
297  SrcOffsets.Add(NumRows);
298 
299  DstOffsets.Add(0);
300  for (TInt i = 1; i < NumThreads; i++) {
301  TInt CurrOffset = i * PartSize;
302  while (CurrOffset < (i+1) * PartSize &&
303  DstCol2[CurrOffset-1] == DstCol2[CurrOffset]) {
304  CurrOffset++;
305  }
306  if (CurrOffset < (i+1) * PartSize) { DstOffsets.Add(CurrOffset); }
307  }
308  DstOffsets.Add(NumRows);
309 
310  TInt SrcPartCnt = SrcOffsets.Len()-1;
311  TInt DstPartCnt = DstOffsets.Len()-1;
312 
313  // for (TInt i = 0; i < SrcOffsets.Len(); i++) {
314  // printf("%d ", SrcOffsets[i].Val);
315  // }
316  // printf("\n");
317  // for (TInt i = 0; i < DstOffsets.Len(); i++) {
318  // printf("%d ", DstOffsets[i].Val);
319  // }
320  // printf("\n");
321 
322  TIntV SrcNodeCounts, DstNodeCounts;
323  SrcNodeCounts.Reserve(SrcPartCnt, SrcPartCnt);
324  DstNodeCounts.Reserve(DstPartCnt, DstPartCnt);
325 
326  #pragma omp parallel for schedule(dynamic)
327  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
328  if (t < SrcPartCnt) {
329  TInt i = t;
330  if (SrcOffsets[i] != SrcOffsets[i+1]) {
331  SrcNodeCounts[i] = 1;
332  TInt CurrNode = SrcCol1[SrcOffsets[i]];
333  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
334  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
335  if (j < SrcOffsets[i+1]) {
336  SrcNodeCounts[i]++;
337  CurrNode = SrcCol1[j];
338  }
339  }
340  }
341  } else {
342  TInt i = t - SrcPartCnt;
343  if (DstOffsets[i] != DstOffsets[i+1]) {
344  DstNodeCounts[i] = 1;
345  TInt CurrNode = DstCol2[DstOffsets[i]];
346  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
347  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
348  if (j < DstOffsets[i+1]) {
349  DstNodeCounts[i]++;
350  CurrNode = DstCol2[j];
351  }
352  }
353  }
354  }
355  }
356 
357  // for (TInt i = 0; i < SrcNodeCounts.Len(); i++) {
358  // printf("%d ", SrcNodeCounts[i].Val);
359  // }
360  // printf("\n");
361  // for (TInt i = 0; i < DstNodeCounts.Len(); i++) {
362  // printf("%d ", DstNodeCounts[i].Val);
363  // }
364  // printf("\n");
365 
366  TInt TotalSrcNodes = 0;
367  TIntV SrcIdOffsets;
368  for (int i = 0; i < SrcPartCnt; i++) {
369  SrcIdOffsets.Add(TotalSrcNodes);
370  TotalSrcNodes += SrcNodeCounts[i];
371  }
372 
373  TInt TotalDstNodes = 0;
374  TIntV DstIdOffsets;
375  for (int i = 0; i < DstPartCnt; i++) {
376  DstIdOffsets.Add(TotalDstNodes);
377  TotalDstNodes += DstNodeCounts[i];
378  }
379 
380  // printf("Total Src = %d, Total Dst = %d\n", TotalSrcNodes.Val, TotalDstNodes.Val);
381 
382  TIntPrV SrcNodeIds, DstNodeIds;
383  #pragma omp parallel sections
384  {
385  #pragma omp section
386  { SrcNodeIds.Reserve(TotalSrcNodes, TotalSrcNodes); }
387  #pragma omp section
388  { DstNodeIds.Reserve(TotalDstNodes, TotalDstNodes); }
389  }
390 
391  #pragma omp parallel for schedule(dynamic)
392  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
393  if (t < SrcPartCnt) {
394  TInt i = t;
395  if (SrcOffsets[i] != SrcOffsets[i+1]) {
396  TInt CurrNode = SrcCol1[SrcOffsets[i]];
397  TInt ThreadOffset = SrcIdOffsets[i];
398  SrcNodeIds[ThreadOffset] = TIntPr(CurrNode, SrcOffsets[i]);
399  TInt CurrCount = 1;
400  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
401  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
402  if (j < SrcOffsets[i+1]) {
403  CurrNode = SrcCol1[j];
404  SrcNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
405  CurrCount++;
406  }
407  }
408  }
409  } else {
410  TInt i = t - SrcPartCnt;
411  if (DstOffsets[i] != DstOffsets[i+1]) {
412  TInt CurrNode = DstCol2[DstOffsets[i]];
413  TInt ThreadOffset = DstIdOffsets[i];
414  DstNodeIds[ThreadOffset] = TIntPr(CurrNode, DstOffsets[i]);
415  TInt CurrCount = 1;
416  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
417  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
418  if (j < DstOffsets[i+1]) {
419  CurrNode = DstCol2[j];
420  DstNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
421  CurrCount++;
422  }
423  }
424  }
425  }
426  }
427 
428  // double endNode = omp_get_wtime();
429  // printf("Node time = %f\n", endNode-endSort);
430 
431  TIntTrV Nodes;
432  Nodes.Reserve(TotalSrcNodes+TotalDstNodes);
433 
434  // double endNodeResize = omp_get_wtime();
435  // printf("(NodeResize time = %f)\n", endNodeResize-endNode);
436 
437  TInt i = 0, j = 0;
438  while (i < TotalSrcNodes && j < TotalDstNodes) {
439  if (SrcNodeIds[i].Val1 == DstNodeIds[j].Val1) {
440  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, j));
441  i++;
442  j++;
443  } else if (SrcNodeIds[i].Val1 < DstNodeIds[j].Val1) {
444  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1));
445  i++;
446  } else {
447  Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j));
448  j++;
449  }
450  }
451  for (; i < TotalSrcNodes; i++) { Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1)); }
452  for (; j < TotalDstNodes; j++) { Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j)); }
453 
454  // double endMerge = omp_get_wtime();
455  // printf("Merge time = %f\n", endMerge-endNode);
456 
457  TInt NumNodes = Nodes.Len();
458  // printf("NumNodes = %d\n", NumNodes.Val);
459 
460  PGraphMP Graph = TNGraphMP::New(NumNodes, NumRows);
461  NumThreads = 1;
462  int Delta = (NumNodes+NumThreads-1)/NumThreads;
463 
464  TVec<TIntV> InVV(NumNodes);
465  TVec<TIntV> OutVV(NumNodes);
466 
467  omp_set_num_threads(NumThreads);
468  #pragma omp parallel for schedule(static,Delta)
469  for (int m = 0; m < NumNodes; m++) {
470  //double startTr = omp_get_wtime();
471  //TIntV OutV, InV;
472  TInt n, i, j;
473  Nodes[m].GetVal(n, i, j);
474  if (i >= 0) {
475  TInt Offset = SrcNodeIds[i].GetVal2();
476  TInt Sz = DstCol1.Len()-Offset;
477  if (i < SrcNodeIds.Len()-1) { Sz = SrcNodeIds[i+1].GetVal2()-Offset; }
478  //printf("OutV: %d %d %d\n", n.Val, Offset.Val, Sz.Val);
479  OutVV[m].Reserve(Sz);
480  }
481  if (j >= 0) {
482  TInt Offset = DstNodeIds[j].GetVal2();
483  TInt Sz = SrcCol2.Len()-Offset;
484  if (j < DstNodeIds.Len()-1) { Sz = DstNodeIds[j+1].GetVal2()-Offset; }
485  //printf("OutV: %d %d %d\n", n.Val, Offset.Val, Sz.Val);
486  InVV[m].Reserve(Sz);
487  }
488  //double endTr = omp_get_wtime();
489  //printf("Thread=%d, i=%d, t=%f\n", omp_get_thread_num(), m, endTr-startTr);
490  }
491 
492  // double endAlloc = omp_get_wtime();
493  // printf("Alloc time = %f\n", endAlloc-endMerge);
494 
495  NumThreads = omp_get_max_threads();
496  Delta = (NumNodes+NumThreads-1)/(10*NumThreads);
497  omp_set_num_threads(NumThreads);
498  #pragma omp parallel for schedule(dynamic)
499  for (int m = 0; m < NumNodes; m++) {
500  //double startTr = omp_get_wtime();
501  //TIntV OutV, InV;
502  TInt n, i, j;
503  Nodes[m].GetVal(n, i, j);
504  if (i >= 0) {
505  TInt Offset = SrcNodeIds[i].GetVal2();
506  TInt Sz = DstCol1.Len()-Offset;
507  if (i < SrcNodeIds.Len()-1) { Sz = SrcNodeIds[i+1].GetVal2()-Offset; }
508  //printf("OutV: %d %d %d\n", n.Val, Offset.Val, Sz.Val);
509  OutVV[m].CopyUniqueFrom(DstCol1, Offset, Sz);
510  }
511  if (j >= 0) {
512  TInt Offset = DstNodeIds[j].GetVal2();
513  TInt Sz = SrcCol2.Len()-Offset;
514  if (j < DstNodeIds.Len()-1) { Sz = DstNodeIds[j+1].GetVal2()-Offset; }
515  //printf("OutV: %d %d %d\n", n.Val, Offset.Val, Sz.Val);
516  InVV[m].CopyUniqueFrom(SrcCol2, Offset, Sz);
517  }
518  Graph->AddNodeWithEdges(n, InVV[m], OutVV[m]);
519  //double endTr = omp_get_wtime();
520  //printf("Thread=%d, i=%d, t=%f\n", omp_get_thread_num(), m, endTr-startTr);
521  }
522  Graph->SetNodes(NumNodes);
523 
524  // double endAdd = omp_get_wtime();
525  // printf("Add time = %f\n", endAdd-endAlloc);
526 
527  return Graph;
528 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static PNGraphMP New()
Static constructor that returns a pointer to the graph. Call: PNGraphMP Graph = TNGraphMP::New().
Definition: graphmp.h:141
Definition: gbase.h:23
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
#define Assert(Cond)
Definition: bd.h:251
static void QSortKeyVal(TIntV &Key, TIntV &Val, TInt Start, TInt End)
Definition: table.cpp:5378
Definition: dt.h:1137
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:171
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
Definition: gbase.h:23
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
template<class PGraphMP >
PGraphMP TSnap::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.

Definition at line 532 of file conv.h.

532  {
533  PNGraphMP Graph;
534  int MaxThreads = omp_get_max_threads();
535  int Length, Threads, Delta, Nodes, Last;
536  uint64_t NumNodesEst;
537  TInt SrcColIdx, DstColIdx;
538  TIntV InVec, OutVec;
539 
540  SrcColIdx = Table->GetColIdx(SrcCol);
541  DstColIdx = Table->GetColIdx(DstCol);
542  const TAttrType NodeType = Table->GetColType(SrcCol);
543  Assert(NodeType == Table->GetColType(DstCol));
544 
545 
546  /* Estimate number of nodes in the graph */
547  int NumRows = Table->Next.Len();
548  double Load = 10;
549  int sz = NumRows / Load;
550  int *buckets = (int *)malloc(sz * sizeof(int));
551 
552  #pragma omp parallel for
553  for (int i = 0; i < sz; i++)
554  buckets[i] = 0;
555 
556  if (NodeType == atInt) {
557  #pragma omp parallel for
558  for (int i = 0; i < NumRows; i++) {
559  int vert = Table->IntCols[DstColIdx][i];
560  buckets[vert % sz] = 1;
561  }
562  }
563  else if (NodeType == atStr ) {
564  #pragma omp parallel for
565  for (int i = 0; i < NumRows; i++) {
566  int vert = (Table->StrColMaps)[DstColIdx][i];
567  buckets[vert % sz] = 1;
568  }
569  }
570  int cnt = 0;
571  #pragma omp parallel for reduction(+:cnt)
572  for (int i = 0; i < sz; i++) {
573  if (buckets[i] == 0)
574  cnt += 1;
575  }
576 
577  NumNodesEst = sz * log ((double)sz / cnt);
578  free (buckets);
579 
580  /* Until we correctly estimate the number of nodes */
581  while (1)
582  {
583  Graph = TNGraphMP::New(NumNodesEst, 100);
584 
585  Length = Graph->Reserved();
586  Threads = MaxThreads/2;
587  Delta = (Length + Threads - 1) / Threads;
588 
589  OutVec.Gen(Length);
590  InVec.Gen(Length);
591 
592  /* build the node hash table, count the size of edge lists */
593  Last = NumRows;
594  Nodes = 0;
595  omp_set_num_threads(Threads);
596  #pragma omp parallel for schedule(static, Delta)
597  for (int CurrRowIdx = 0; CurrRowIdx < Last; CurrRowIdx++) {
598  if ((uint64_t) Nodes + 1000 >= NumNodesEst) {
599  /* need bigger hash table */
600  continue;
601  }
602 
603  TInt SVal, DVal;
604  if (NodeType == atInt) {
605  SVal = Table->IntCols[SrcColIdx][CurrRowIdx];
606  DVal = Table->IntCols[DstColIdx][CurrRowIdx];
607  }
608  else if (NodeType == atStr ) {
609  SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
610  DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
611  }
612  int SrcIdx = abs((SVal.GetPrimHashCd()) % Length);
613  if (!Graph->AddOutEdge1(SrcIdx, SVal, DVal)) {
614  #pragma omp critical
615  {
616  Nodes++;
617  }
618  }
619  __sync_fetch_and_add(&OutVec[SrcIdx].Val, 1);
620 
621  int DstIdx = abs((DVal.GetPrimHashCd()) % Length);
622  if (!Graph->AddInEdge1(DstIdx, SVal, DVal)) {
623  #pragma omp critical
624  {
625  Nodes++;
626  }
627  }
628  __sync_fetch_and_add(&InVec[DstIdx].Val, 1);
629 
630  }
631  if ((uint64_t) Nodes + 1000 >= NumNodesEst) {
632  /* We need to double our num nodes estimate */
633  Graph.Clr();
634  InVec.Clr();
635  OutVec.Clr();
636  NumNodesEst *= 2;
637  }
638  else {
639  break;
640  }
641  }
642 
643  Graph->SetNodes(Nodes);
644 
645  uint Edges = 0;
646  for (int i = 0; i < Length; i++) {
647  Edges += OutVec[i] + InVec[i];
648  }
649 
650  for (int Idx = 0; Idx < Length; Idx++) {
651  if (OutVec[Idx] > 0 || InVec[Idx] > 0) {
652  Graph->ReserveNodeDegs(Idx, InVec[Idx], OutVec[Idx]);
653  }
654  }
655 
656  /* assign edges */
657  Length = Graph->Reserved();
658  Threads = MaxThreads;
659  Delta = (Length + Threads - 1) / Threads;
660 
661  omp_set_num_threads(Threads);
662  #pragma omp parallel for schedule(static,Delta)
663  for (int CurrRowIdx = 0; CurrRowIdx < Last; CurrRowIdx++) {
664  TInt SVal, DVal;
665  if (NodeType == atInt) {
666  SVal = Table->IntCols[SrcColIdx][CurrRowIdx];
667  DVal = Table->IntCols[DstColIdx][CurrRowIdx];
668  }
669  else if (NodeType == atStr) {
670  SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
671  DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
672  }
673 
674  Graph->AddOutEdge2(SVal, DVal);
675  Graph->AddInEdge2(SVal, DVal);
676  }
677 
678  /* sort edges */
679  Length = Graph->Reserved();
680  Threads = MaxThreads*2;
681  Delta = (Length + Threads - 1) / Threads;
682 
683  omp_set_num_threads(Threads);
684  #pragma omp parallel for schedule(dynamic)
685  for (int Idx = 0; Idx < Length; Idx++) {
686  if (OutVec[Idx] > 0 || InVec[Idx] > 0) {
687  Graph->SortEdges(Idx, InVec[Idx], OutVec[Idx]);
688  }
689  }
690 
691  return Graph;
692 }
int GetPrimHashCd() const
Definition: dt.h:1171
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
unsigned int uint
Definition: bd.h:11
static PNGraphMP New()
Static constructor that returns a pointer to the graph. Call: PNGraphMP Graph = TNGraphMP::New().
Definition: graphmp.h:141
void Clr()
Definition: bd.h:502
Definition: gbase.h:23
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1137
Definition: bd.h:196
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
Definition: gbase.h:23
template<class PGraph >
PGraph TSnap::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.

Converts table to a network. Suitable for PNEANet - Requires node and edge attribute column names as vectors.

Definition at line 64 of file conv.h.

68 {
69  PGraph Graph = PGraph::TObj::New();
70 
71  const TAttrType NodeType = Table->GetColType(SrcCol);
72  Assert(NodeType == Table->GetColType(DstCol));
73  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
74  const TInt DstColIdx = Table->GetColIdx(DstCol);
75 
76  //Table->AddGraphAttributeV(SrcAttrV, false, true, false);
77  //Table->AddGraphAttributeV(DstAttrV, false, false, true);
78  //Table->AddGraphAttributeV(EdgeAttrV, true, false, true);
79 
80  // node values - i.e. the unique values of src/dst col
81  //THashSet<TInt> IntNodeVals; // for both int and string node attr types.
82  THash<TFlt, TInt> FltNodeVals;
83 
84  // node attributes
85  THash<TInt, TStrIntVH> NodeIntAttrs;
86  THash<TInt, TStrFltVH> NodeFltAttrs;
87  THash<TInt, TStrStrVH> NodeStrAttrs;
88 
89  // make single pass over all rows in the table
90  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
91  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
92  continue;
93  }
94 
95  // add src and dst nodes to graph if they are not seen earlier
96  TInt SVal, DVal;
97  if (NodeType == atFlt) {
98  TFlt FSVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
99  SVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FSVal);
100  TFlt FDVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
101  DVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FDVal);
102  } else if (NodeType == atInt || NodeType == atStr) {
103  if (NodeType == atInt) {
104  SVal = (Table->IntCols)[SrcColIdx][CurrRowIdx];
105  DVal = (Table->IntCols)[DstColIdx][CurrRowIdx];
106  } else {
107  SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
108  if (strlen(Table->GetContextKey(SVal)) == 0) { continue; } //illegal value
109  DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
110  if (strlen(Table->GetContextKey(DVal)) == 0) { continue; } //illegal value
111  }
112  if (!Graph->IsNode(SVal)) {Graph->AddNode(SVal); }
113  if (!Graph->IsNode(DVal)) {Graph->AddNode(DVal); }
114  //CheckAndAddIntNode(Graph, IntNodeVals, SVal);
115  //CheckAndAddIntNode(Graph, IntNodeVals, DVal);
116  }
117 
118  // add edge and edge attributes
119  Graph->AddEdge(SVal, DVal, CurrRowIdx);
120 
121  // Aggregate edge attributes and add to graph
122  for (TInt i = 0; i < EdgeAttrV.Len(); i++) {
123  TStr ColName = EdgeAttrV[i];
124  TAttrType T = Table->GetColType(ColName);
125  TInt Index = Table->GetColIdx(ColName);
126  switch (T) {
127  case atInt:
128  Graph->AddIntAttrDatE(CurrRowIdx, Table->IntCols[Index][CurrRowIdx], ColName);
129  break;
130  case atFlt:
131  Graph->AddFltAttrDatE(CurrRowIdx, Table->FltCols[Index][CurrRowIdx], ColName);
132  break;
133  case atStr:
134  Graph->AddStrAttrDatE(CurrRowIdx, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
135  break;
136  }
137  }
138 
139  // get src and dst node attributes into hashmaps
140  if ((Table->SrcNodeAttrV).Len() > 0) {
141  Table->AddNodeAttributes(SVal, Table->SrcNodeAttrV, CurrRowIdx, NodeIntAttrs, NodeFltAttrs, NodeStrAttrs);
142  }
143 
144  if ((Table->DstNodeAttrV).Len() > 0) {
145  Table->AddNodeAttributes(DVal, Table->DstNodeAttrV, CurrRowIdx, NodeIntAttrs, NodeFltAttrs, NodeStrAttrs);
146  }
147  }
148 
149  // aggregate node attributes and add to graph
150  if ((Table->SrcNodeAttrV).Len() > 0 || (Table->DstNodeAttrV).Len() > 0) {
151  for (TNEANet::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
152  TInt NId = NodeI.GetId();
153  if (NodeIntAttrs.IsKey(NId)) {
154  TStrIntVH IntAttrVals = NodeIntAttrs.GetDat(NId);
155  for (TStrIntVH::TIter it = IntAttrVals.BegI(); it < IntAttrVals.EndI(); it++) {
156  TInt AttrVal = Table->AggregateVector<TInt>(it.GetDat(), AggrPolicy);
157  Graph->AddIntAttrDatN(NId, AttrVal, it.GetKey());
158  }
159  }
160  if (NodeFltAttrs.IsKey(NId)) {
161  TStrFltVH FltAttrVals = NodeFltAttrs.GetDat(NId);
162  for (TStrFltVH::TIter it = FltAttrVals.BegI(); it < FltAttrVals.EndI(); it++) {
163  TFlt AttrVal = Table->AggregateVector<TFlt>(it.GetDat(), AggrPolicy);
164  Graph->AddFltAttrDatN(NId, AttrVal, it.GetKey());
165  }
166  }
167  if (NodeStrAttrs.IsKey(NId)) {
168  TStrStrVH StrAttrVals = NodeStrAttrs.GetDat(NId);
169  for (TStrStrVH::TIter it = StrAttrVals.BegI(); it < StrAttrVals.EndI(); it++) {
170  TStr AttrVal = Table->AggregateVector<TStr>(it.GetDat(), AggrPolicy);
171  Graph->AddStrAttrDatN(NId, AttrVal, it.GetKey());
172  }
173  }
174  }
175  }
176 
177  return Graph;
178 }
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TIter BegI() const
Definition: hash.h:213
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
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
TIter EndI() const
Definition: hash.h:218
Definition: gbase.h:23
Definition: dt.h:1386
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1137
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
Definition: gbase.h:23
bool IsKey(const TKey &Key) const
Definition: hash.h:258
template<class PGraph >
PGraph TSnap::ToNetwork ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TAttrAggr  AggrPolicy 
)

Calls ToNetwork with an empty attribute vector. Convenience wrapper.

Converts table to a network. Suitable for PNEANet - Assumes no node and edge attributes.

Definition at line 182 of file conv.h.

184 {
185  TStrV V;
186  return ToNetwork<PGraph>(Table, SrcCol, DstCol, V, AggrPolicy);
187 }
template<class PGraph >
PGraph TSnap::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.

Definition at line 1572 of file conv.h.

1575  {
1576  PGraph Graph = PGraph::TObj::New();
1577 
1578  const TAttrType NodeType = Table->GetColType(SrcCol);
1579  Assert(NodeType == Table->GetColType(DstCol));
1580  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
1581  const TInt DstColIdx = Table->GetColIdx(DstCol);
1582 
1583  //Table->AddGraphAttributeV(SrcAttrV, false, true, false);
1584  //Table->AddGraphAttributeV(DstAttrV, false, false, true);
1585  //Table->AddGraphAttributeV(EdgeAttrV, true, false, true);
1586 
1587  // node values - i.e. the unique values of src/dst col
1588  //THashSet<TInt> IntNodeVals; // for both int and string node attr types.
1589  THash<TFlt, TInt> FltNodeVals;
1590 
1591  // make single pass over all rows in the table
1592  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
1593  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
1594  continue;
1595  }
1596 
1597  // add src and dst nodes to graph if they are not seen earlier
1598  TInt SVal, DVal;
1599  if (NodeType == atFlt) {
1600  TFlt FSVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
1601  SVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FSVal);
1602  TFlt FDVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
1603  DVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FDVal);
1604  }
1605  else if (NodeType == atInt || NodeType == atStr) {
1606  if (NodeType == atInt) {
1607  SVal = (Table->IntCols)[SrcColIdx][CurrRowIdx];
1608  DVal = (Table->IntCols)[DstColIdx][CurrRowIdx];
1609  }
1610  else {
1611  SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
1612  // if (strlen(Table->GetContextKey(SVal)) == 0) { continue; } //illegal value
1613  DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
1614  // if (strlen(Table->GetContextKey(DVal)) == 0) { continue; } //illegal value
1615  }
1616  if (!Graph->IsNode(SVal)) {Graph->AddNode(SVal); }
1617  if (!Graph->IsNode(DVal)) {Graph->AddNode(DVal); }
1618  //CheckAndAddIntNode(Graph, IntNodeVals, SVal);
1619  //CheckAndAddIntNode(Graph, IntNodeVals, DVal);
1620  }
1621 
1622  // add edge and edge attributes
1623  Graph->AddEdge(SVal, DVal, CurrRowIdx);
1624 
1625  // Aggregate edge attributes and add to graph
1626  for (TInt i = 0; i < EdgeAttrV.Len(); i++) {
1627  TStr ColName = EdgeAttrV[i];
1628  TAttrType T = Table->GetColType(ColName);
1629  TInt Index = Table->GetColIdx(ColName);
1630  switch (T) {
1631  case atInt:
1632  Graph->AddIntAttrDatE(CurrRowIdx, Table->IntCols[Index][CurrRowIdx], ColName);
1633  break;
1634  case atFlt:
1635  Graph->AddFltAttrDatE(CurrRowIdx, Table->FltCols[Index][CurrRowIdx], ColName);
1636  break;
1637  case atStr:
1638  Graph->AddStrAttrDatE(CurrRowIdx, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
1639  break;
1640  }
1641  }
1642  }
1643  return Graph;
1644 
1645 }
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: gbase.h:23
Definition: dt.h:1386
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1137
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
Definition: gbase.h:23
template<class PGraph >
PGraph TSnap::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.

Definition at line 2010 of file conv.h.

2013  {
2014  PGraph Graph = PGraph::TObj::New();
2015 
2016  const TAttrType NodeType = Table->GetColType(SrcCol);
2017  Assert(NodeType == Table->GetColType(DstCol));
2018  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
2019  const TInt DstColIdx = Table->GetColIdx(DstCol);
2020 
2021 
2022  const TAttrType NodeTypeN = NodeTable->GetColType(NodeCol);
2023  const TInt NodeColIdx = NodeTable->GetColIdx(NodeCol);
2024  THash<TInt, TStrIntVH> NodeIntAttrs;
2025  THash<TInt, TStrFltVH> NodeFltAttrs;
2026  THash<TInt, TStrStrVH> NodeStrAttrs;
2027 
2028 
2029  //Table->AddGraphAttributeV(SrcAttrV, false, true, false);
2030  //Table->AddGraphAttributeV(DstAttrV, false, false, true);
2031  //Table->AddGraphAttributeV(EdgeAttrV, true, false, true);
2032 
2033  // node values - i.e. the unique values of src/dst col
2034  //THashSet<TInt> IntNodeVals; // for both int and string node attr types.
2035  THash<TFlt, TInt> FltNodeVals;
2036 
2037  // make single pass over all rows in the table
2038  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
2039  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
2040  continue;
2041  }
2042 
2043  // add src and dst nodes to graph if they are not seen earlier
2044  TInt SVal, DVal;
2045  if (NodeType == atFlt) {
2046  TFlt FSVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
2047  SVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FSVal);
2048  TFlt FDVal = (Table->FltCols)[SrcColIdx][CurrRowIdx];
2049  DVal = Table->CheckAndAddFltNode(Graph, FltNodeVals, FDVal);
2050  }
2051  else if (NodeType == atInt || NodeType == atStr) {
2052  if (NodeType == atInt) {
2053  SVal = (Table->IntCols)[SrcColIdx][CurrRowIdx];
2054  DVal = (Table->IntCols)[DstColIdx][CurrRowIdx];
2055  }
2056  else {
2057  SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
2058  // if (strlen(Table->GetContextKey(SVal)) == 0) { continue; } //illegal value
2059  DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
2060  // if (strlen(Table->GetContextKey(DVal)) == 0) { continue; } //illegal value
2061  }
2062  if (!Graph->IsNode(SVal)) {Graph->AddNode(SVal); }
2063  if (!Graph->IsNode(DVal)) {Graph->AddNode(DVal); }
2064  //CheckAndAddIntNode(Graph, IntNodeVals, SVal);
2065  //CheckAndAddIntNode(Graph, IntNodeVals, DVal);
2066  }
2067 
2068  // add edge and edge attributes
2069  Graph->AddEdge(SVal, DVal, CurrRowIdx);
2070 
2071  // Aggregate edge attributes and add to graph
2072  for (TInt i = 0; i < EdgeAttrV.Len(); i++) {
2073  TStr ColName = EdgeAttrV[i];
2074  TAttrType T = Table->GetColType(ColName);
2075  TInt Index = Table->GetColIdx(ColName);
2076  switch (T) {
2077  case atInt:
2078  Graph->AddIntAttrDatE(CurrRowIdx, Table->IntCols[Index][CurrRowIdx], ColName);
2079  break;
2080  case atFlt:
2081  Graph->AddFltAttrDatE(CurrRowIdx, Table->FltCols[Index][CurrRowIdx], ColName);
2082  break;
2083  case atStr:
2084  Graph->AddStrAttrDatE(CurrRowIdx, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
2085  break;
2086  }
2087  }
2088  }
2089 
2090 
2091  //Add node attribtes
2092  if (NodeAttrV.Len() > 0) {
2093  for (int CurrRowIdx = 0; CurrRowIdx < (NodeTable->Next).Len(); CurrRowIdx++) {
2094  if ((NodeTable->Next)[CurrRowIdx] == NodeTable->Invalid) {
2095  continue;
2096  }
2097  TInt NId;
2098  if (NodeTypeN == atInt) {
2099  NId = (NodeTable->IntCols)[NodeColIdx][CurrRowIdx];
2100  }
2101  else if (NodeTypeN == atStr){
2102  NId = (NodeTable->StrColMaps)[NodeColIdx][CurrRowIdx];
2103  }
2104  for (TInt i = 0; i < NodeAttrV.Len(); i++) {
2105  TStr ColName = NodeAttrV[i];
2106  TAttrType T = NodeTable->GetColType(ColName);
2107  TInt Index = NodeTable->GetColIdx(ColName);
2108  switch (T) {
2109  case atInt:
2110  Graph->AddIntAttrDatN(NId, NodeTable->IntCols[Index][CurrRowIdx], ColName);
2111  break;
2112  case atFlt:
2113  Graph->AddFltAttrDatN(NId, NodeTable->FltCols[Index][CurrRowIdx], ColName);
2114  break;
2115  case atStr:
2116  Graph->AddStrAttrDatN(NId, NodeTable->GetStrValIdx(Index, CurrRowIdx), ColName);
2117  break;
2118  }
2119  }
2120  }
2121  }
2122 
2123  return Graph;
2124 
2125 }
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: gbase.h:23
Definition: dt.h:1386
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1137
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
Definition: gbase.h:23
template<class PGraphMP >
PGraphMP TSnap::ToNetworkMP ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TStrV SrcAttrV,
TStrV DstAttrV,
TStrV EdgeAttrV,
TAttrAggr  AggrPolicy 
)
inline

Does Table to Network conversion in parallel using the sort-first algorithm. This is the recommended method to use.

Definition at line 696 of file conv.h.

699  {
701 
703  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
704  const TInt DstColIdx = Table->GetColIdx(DstCol);
705  const TInt NumRows = Table->GetNumValidRows();
706 
707  const TAttrType NodeType = Table->GetColType(SrcCol);
708  Assert(NodeType == Table->GetColType(DstCol));
709 
710 
711  TIntV SrcCol1, EdgeCol1, EdgeCol2, DstCol2;
712 
713  THash<TInt, TStrIntVH> NodeIntAttrs;
714  THash<TInt, TStrFltVH> NodeFltAttrs;
715  THash<TInt, TStrStrVH> NodeStrAttrs;
716 
717  #pragma omp parallel sections num_threads(4)
718  {
719  #pragma omp section
720  { SrcCol1.Reserve(NumRows, NumRows); }
721  #pragma omp section
722  { EdgeCol1.Reserve(NumRows, NumRows); }
723  #pragma omp section
724  { DstCol2.Reserve(NumRows, NumRows); }
725  #pragma omp section
726  { EdgeCol2.Reserve(NumRows, NumRows); }
727  }
729 
731  TIntPrV Partitions;
732  Table->GetPartitionRanges(Partitions, omp_get_max_threads());
733  TInt PartitionSize = Partitions[0].GetVal2()-Partitions[0].GetVal1()+1;
734 
735  // double endPartition = omp_get_wtime();
736  // printf("Partition time = %f\n", endPartition-endResize);
737 
738  omp_set_num_threads(omp_get_max_threads());
739  if (NodeType == atInt) {
740  #pragma omp parallel for schedule(static)
741  for (int i = 0; i < Partitions.Len(); i++) {
742  TRowIterator RowI(Partitions[i].GetVal1(), Table());
743  TRowIterator EndI(Partitions[i].GetVal2(), Table());
744  while (RowI < EndI) {
745  TInt RowId = RowI.GetRowIdx();
746  SrcCol1[RowId] = RowI.GetIntAttr(SrcColIdx);
747  EdgeCol1[RowId] = RowId;
748  DstCol2[RowId] = RowI.GetIntAttr(DstColIdx);
749  EdgeCol2[RowId] = RowId;
750  RowI++;
751  }
752  }
753  }
754  else if (NodeType == atStr) {
755  #pragma omp parallel for schedule(static)
756  for (int i = 0; i < Partitions.Len(); i++) {
757  TRowIterator RowI(Partitions[i].GetVal1(), Table());
758  TRowIterator EndI(Partitions[i].GetVal2(), Table());
759  while (RowI < EndI) {
760  TInt RowId = RowI.GetRowIdx();
761  SrcCol1[RowId] = RowI.GetStrMapById(SrcColIdx);
762  EdgeCol1[RowId] = RowId;
763  DstCol2[RowId] = RowI.GetStrMapById(DstColIdx);
764  EdgeCol2[RowId] = RowId;
765  RowI++;
766  }
767  }
768  }
770 
771  Sw->Start(TStopwatch::Sort);
772  omp_set_num_threads(omp_get_max_threads());
773  #pragma omp parallel
774  {
775  #pragma omp single nowait
776  {
777  #ifndef GLib_WIN32
778  #pragma omp task untied shared(SrcCol1, EdgeCol1)
779  #endif
780  { TTable::QSortKeyVal(SrcCol1, EdgeCol1, 0, NumRows-1); }
781  }
782  #pragma omp single nowait
783  {
784  #ifndef GLib_WIN32
785  #pragma omp task untied shared(EdgeCol2, DstCol2)
786  #endif
787  { TTable::QSortKeyVal(DstCol2, EdgeCol2, 0, NumRows-1); }
788  }
789  #ifndef GLib_WIN32
790  #pragma omp taskwait
791  #endif
792  }
793  Sw->Stop(TStopwatch::Sort);
794 
796  TInt NumThreads = omp_get_max_threads();
797  TInt PartSize = (NumRows/NumThreads);
798 
799  // Find the offset of all partitions, each of which contains a list of rows.
800  // Nodes from same sources or destinations are ensured to be kept within same partition.
801  TIntV SrcOffsets, DstOffsets;
802  SrcOffsets.Add(0);
803  for (TInt i = 1; i < NumThreads; i++) {
804  TInt CurrOffset = i * PartSize;
805  while (CurrOffset < (i+1) * PartSize &&
806  SrcCol1[CurrOffset-1] == SrcCol1[CurrOffset]) {
807  // ensure that rows from the same sources are grouped together
808  CurrOffset++;
809  }
810  if (CurrOffset < (i+1) * PartSize) { SrcOffsets.Add(CurrOffset); }
811  }
812  SrcOffsets.Add(NumRows);
813 
814  DstOffsets.Add(0);
815  for (TInt i = 1; i < NumThreads; i++) {
816  TInt CurrOffset = i * PartSize;
817  while (CurrOffset < (i+1) * PartSize &&
818  DstCol2[CurrOffset-1] == DstCol2[CurrOffset]) {
819  // ensure that rows to the same destinations are grouped together
820  CurrOffset++;
821  }
822  if (CurrOffset < (i+1) * PartSize) { DstOffsets.Add(CurrOffset); }
823  }
824  DstOffsets.Add(NumRows);
825 
826  TInt SrcPartCnt = SrcOffsets.Len()-1; // number of partitions
827  TInt DstPartCnt = DstOffsets.Len()-1; // number of partitions
828 
829  // count the number of source nodes and destination nodes in each partition
830  TIntV SrcNodeCounts, DstNodeCounts;
831  SrcNodeCounts.Reserve(SrcPartCnt, SrcPartCnt);
832  DstNodeCounts.Reserve(DstPartCnt, DstPartCnt);
833 
834  #pragma omp parallel for schedule(dynamic)
835  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
836  if (t < SrcPartCnt) {
837  TInt i = t;
838  if (SrcOffsets[i] != SrcOffsets[i+1]) {
839  SrcNodeCounts[i] = 1;
840  TInt CurrNode = SrcCol1[SrcOffsets[i]];
841  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
842  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
843  if (j < SrcOffsets[i+1]) {
844  SrcNodeCounts[i]++;
845  CurrNode = SrcCol1[j];
846  }
847  }
848  }
849  } else {
850  TInt i = t - SrcPartCnt;
851  if (DstOffsets[i] != DstOffsets[i+1]) {
852  DstNodeCounts[i] = 1;
853  TInt CurrNode = DstCol2[DstOffsets[i]];
854  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
855  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
856  if (j < DstOffsets[i+1]) {
857  DstNodeCounts[i]++;
858  CurrNode = DstCol2[j];
859  }
860  }
861  }
862  }
863  }
864 
865  TInt TotalSrcNodes = 0;
866  TIntV SrcIdOffsets;
867  for (int i = 0; i < SrcPartCnt; i++) {
868  SrcIdOffsets.Add(TotalSrcNodes);
869  TotalSrcNodes += SrcNodeCounts[i];
870  }
871 
872  TInt TotalDstNodes = 0;
873  TIntV DstIdOffsets;
874  for (int i = 0; i < DstPartCnt; i++) {
875  DstIdOffsets.Add(TotalDstNodes);
876  TotalDstNodes += DstNodeCounts[i];
877  }
878 
879  // printf("Total Src = %d, Total Dst = %d\n", TotalSrcNodes.Val, TotalDstNodes.Val);
880 
881  // find vector of (node_id, start_offset) where start_offset is the index of the first row with node_id
882  TIntPrV SrcNodeIds, DstNodeIds;
883  #pragma omp parallel sections
884  {
885  #pragma omp section
886  { SrcNodeIds.Reserve(TotalSrcNodes, TotalSrcNodes); }
887  #pragma omp section
888  { DstNodeIds.Reserve(TotalDstNodes, TotalDstNodes); }
889  }
890 
891  // Find the starting offset of each node (in both src and dst)
892  #pragma omp parallel for schedule(dynamic)
893  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
894  if (t < SrcPartCnt) {
895  TInt i = t;
896  if (SrcOffsets[i] != SrcOffsets[i+1]) {
897  TInt CurrNode = SrcCol1[SrcOffsets[i]];
898  TInt ThreadOffset = SrcIdOffsets[i];
899  SrcNodeIds[ThreadOffset] = TIntPr(CurrNode, SrcOffsets[i]);
900  TInt CurrCount = 1;
901  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
902  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
903  if (j < SrcOffsets[i+1]) {
904  CurrNode = SrcCol1[j];
905  SrcNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
906  CurrCount++;
907  }
908  }
909  }
910  } else {
911  TInt i = t - SrcPartCnt;
912  if (DstOffsets[i] != DstOffsets[i+1]) {
913  TInt CurrNode = DstCol2[DstOffsets[i]];
914  TInt ThreadOffset = DstIdOffsets[i];
915  DstNodeIds[ThreadOffset] = TIntPr(CurrNode, DstOffsets[i]);
916  TInt CurrCount = 1;
917  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
918  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
919  if (j < DstOffsets[i+1]) {
920  CurrNode = DstCol2[j];
921  DstNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
922  CurrCount++;
923  }
924  }
925  }
926  }
927  }
928  Sw->Stop(TStopwatch::Group);
929 
931  // Find the combined neighborhood (both out-neighbors and in-neighbors) of each node
932  TIntTrV Nodes;
933  Nodes.Reserve(TotalSrcNodes+TotalDstNodes);
934 
935  TInt i = 0, j = 0;
936  while (i < TotalSrcNodes && j < TotalDstNodes) {
937  if (SrcNodeIds[i].Val1 == DstNodeIds[j].Val1) {
938  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, j));
939  i++;
940  j++;
941  } else if (SrcNodeIds[i].Val1 < DstNodeIds[j].Val1) {
942  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1));
943  i++;
944  } else {
945  Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j));
946  j++;
947  }
948  }
949  for (; i < TotalSrcNodes; i++) { Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1)); }
950  for (; j < TotalDstNodes; j++) { Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j)); }
952 
954  TInt NumNodes = Nodes.Len();
955  PGraphMP Graph = PGraphMP::TObj::New(NumNodes, NumRows);
956 // NumThreads = omp_get_max_threads();
957 // int Delta = (NumNodes+NumThreads-1)/NumThreads;
958 
959  TVec<TIntV> InVV(NumNodes);
960  TVec<TIntV> OutVV(NumNodes);
961 
962 // omp_set_num_threads(NumThreads);
963  #pragma omp parallel for schedule(static,100)
964  for (int m = 0; m < NumNodes; m++) {
965  //double startTr = omp_get_wtime();
966  //TIntV OutV, InV;
967  TInt n, i, j;
968  Nodes[m].GetVal(n, i, j);
969  if (i >= 0) {
970  TInt Offset = SrcNodeIds[i].GetVal2();
971  TInt Sz = EdgeCol1.Len()-Offset;
972  if (i < SrcNodeIds.Len()-1) { Sz = SrcNodeIds[i+1].GetVal2()-Offset; }
973  OutVV[m].Reserve(Sz);
974  OutVV[m].CopyUniqueFrom(EdgeCol1, Offset, Sz);
975  }
976  if (j >= 0) {
977  TInt Offset = DstNodeIds[j].GetVal2();
978  TInt Sz = EdgeCol2.Len()-Offset;
979  if (j < DstNodeIds.Len()-1) { Sz = DstNodeIds[j+1].GetVal2()-Offset; }
980  InVV[m].Reserve(Sz);
981  InVV[m].CopyUniqueFrom(EdgeCol2, Offset, Sz);
982  }
983  Graph->AddNodeWithEdges(n, InVV[m], OutVV[m]);
984  }
985  Graph->SetNodes(NumNodes);
987 
989  omp_set_num_threads(omp_get_max_threads());
990  if (NodeType == atInt) {
991  #pragma omp parallel for schedule(static)
992  for (int i = 0; i < Partitions.Len(); i++) {
993  TRowIterator RowI(Partitions[i].GetVal1(), Table());
994  TRowIterator EndI(Partitions[i].GetVal2(), Table());
995  while (RowI < EndI) {
996  TInt RowId = RowI.GetRowIdx(); // EdgeId
997  TInt SrcId = RowI.GetIntAttr(SrcColIdx);
998  TInt DstId = RowI.GetIntAttr(DstColIdx);
999  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
1000  RowI++;
1001  for (TInt ea_i = 0; ea_i < EdgeAttrV.Len(); ea_i++) {
1002  TStr ColName = EdgeAttrV[ea_i];
1003  TAttrType T = Table->GetColType(ColName);
1004  TInt Index = Table->GetColIdx(ColName);
1005  switch (T) {
1006  case atInt:
1007  Graph->AddIntAttrDatE(RowId, Table->IntCols[Index][RowId], ColName);
1008  break;
1009  case atFlt:
1010  Graph->AddFltAttrDatE(RowId, Table->FltCols[Index][RowId], ColName);
1011  break;
1012  case atStr:
1013  Graph->AddStrAttrDatE(RowId, Table->GetStrValIdx(Index, RowId), ColName);
1014  break;
1015  }
1016  }
1017  if ((Table->SrcNodeAttrV).Len() > 0) {
1018  Table->AddNodeAttributes(SrcId, Table->SrcNodeAttrV, RowId, NodeIntAttrs, NodeFltAttrs, NodeStrAttrs);
1019  }
1020 
1021  if ((Table->DstNodeAttrV).Len() > 0) {
1022  Table->AddNodeAttributes(SrcId, Table->DstNodeAttrV, RowId, NodeIntAttrs, NodeFltAttrs, NodeStrAttrs);
1023  }
1024  }
1025  }
1026  }
1027  else if (NodeType == atStr) {
1028  #pragma omp parallel for schedule(static)
1029  for (int i = 0; i < Partitions.Len(); i++) {
1030  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1031  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1032  while (RowI < EndI) {
1033  TInt RowId = RowI.GetRowIdx(); // EdgeId
1034  TInt SrcId = RowI.GetStrMapById(SrcColIdx);
1035  TInt DstId = RowI.GetStrMapById(DstColIdx);
1036  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
1037  RowI++;
1038  for (TInt ea_i = 0; ea_i < EdgeAttrV.Len(); ea_i++) {
1039  TStr ColName = EdgeAttrV[ea_i];
1040  TAttrType T = Table->GetColType(ColName);
1041  TInt Index = Table->GetColIdx(ColName);
1042  switch (T) {
1043  case atInt:
1044  Graph->AddIntAttrDatE(RowId, Table->IntCols[Index][RowId], ColName);
1045  break;
1046  case atFlt:
1047  Graph->AddFltAttrDatE(RowId, Table->FltCols[Index][RowId], ColName);
1048  break;
1049  case atStr:
1050  Graph->AddStrAttrDatE(RowId, Table->GetStrValIdx(Index, RowId), ColName);
1051  break;
1052  }
1053  }
1054  if ((Table->SrcNodeAttrV).Len() > 0) {
1055  Table->AddNodeAttributes(SrcId, Table->SrcNodeAttrV, RowId, NodeIntAttrs, NodeFltAttrs, NodeStrAttrs);
1056  }
1057 
1058  if ((Table->DstNodeAttrV).Len() > 0) {
1059  Table->AddNodeAttributes(SrcId, Table->DstNodeAttrV, RowId, NodeIntAttrs, NodeFltAttrs, NodeStrAttrs);
1060  }
1061 
1062  }
1063  }
1064 
1065  }
1066 
1067  // aggregate node attributes and add to graph
1068  if ((Table->SrcNodeAttrV).Len() > 0 || (Table->DstNodeAttrV).Len() > 0) {
1069  for (typename PGraphMP::TObj::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
1070  TInt NId = NodeI.GetId();
1071  if (NodeIntAttrs.IsKey(NId)) {
1072  TStrIntVH IntAttrVals = NodeIntAttrs.GetDat(NId);
1073  for (TStrIntVH::TIter it = IntAttrVals.BegI(); it < IntAttrVals.EndI(); it++) {
1074  TInt AttrVal = Table->AggregateVector<TInt>(it.GetDat(), AggrPolicy);
1075  Graph->AddIntAttrDatN(NId, AttrVal, it.GetKey());
1076  }
1077  }
1078  if (NodeFltAttrs.IsKey(NId)) {
1079  TStrFltVH FltAttrVals = NodeFltAttrs.GetDat(NId);
1080  for (TStrFltVH::TIter it = FltAttrVals.BegI(); it < FltAttrVals.EndI(); it++) {
1081  TFlt AttrVal = Table->AggregateVector<TFlt>(it.GetDat(), AggrPolicy);
1082  Graph->AddFltAttrDatN(NId, AttrVal, it.GetKey());
1083  }
1084  }
1085  if (NodeStrAttrs.IsKey(NId)) {
1086  TStrStrVH StrAttrVals = NodeStrAttrs.GetDat(NId);
1087  for (TStrStrVH::TIter it = StrAttrVals.BegI(); it < StrAttrVals.EndI(); it++) {
1088  TStr AttrVal = Table->AggregateVector<TStr>(it.GetDat(), AggrPolicy);
1089  Graph->AddStrAttrDatN(NId, AttrVal, it.GetKey());
1090  }
1091  }
1092  }
1093  }
1094 
1095 
1096  Graph->SetEdges(NumRows);
1098 
1099  // double endAdd = omp_get_wtime();
1100  // printf("Add time = %f\n", endAdd-endAlloc);
1101 
1102  return Graph;
1103 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TIter BegI() const
Definition: hash.h:213
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Start(const TExperiment Exp)
Start a new experiment.
Definition: util.cpp:733
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TIter EndI() const
Definition: hash.h:218
Definition: gbase.h:23
Definition: dt.h:1386
void Stop(const TExperiment Exp)
Stop the current experiment.
Definition: util.cpp:737
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
#define Assert(Cond)
Definition: bd.h:251
static void QSortKeyVal(TIntV &Key, TIntV &Val, TInt Start, TInt End)
Definition: table.cpp:5378
Definition: dt.h:1137
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:171
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
static TStopwatch * GetInstance()
Definition: util.h:82
Definition: gbase.h:23
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
Routines to benchmark table operations.
Definition: util.h:71
template<class PGraphMP >
PGraphMP TSnap::ToNetworkMP ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TAttrAggr  AggrPolicy 
)

Calls ToNetworkMP with empty attribute vector. Convenience wrapper.

Definition at line 1107 of file conv.h.

1109 {
1110  TStrV V;
1111  return ToNetworkMP<PGraphMP>(Table, SrcCol, DstCol, V,AggrPolicy);
1112 }
template<class PGraphMP >
PGraphMP TSnap::ToNetworkMP ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TStrV EdgeAttrV,
TAttrAggr  AggrPolicy 
)
inline

Converts table to network in parallel. Use if network has only edge attributes.

Definition at line 1651 of file conv.h.

1654  {
1656 
1658  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
1659  const TInt DstColIdx = Table->GetColIdx(DstCol);
1660  const TInt NumRows = Table->GetNumValidRows();
1661 
1662  const TAttrType NodeType = Table->GetColType(SrcCol);
1663  Assert(NodeType == Table->GetColType(DstCol));
1664 
1665  TIntV SrcCol1, EdgeCol1, EdgeCol2, DstCol2;
1666 
1667  THash<TInt, TStrIntVH> NodeIntAttrs;
1668  THash<TInt, TStrFltVH> NodeFltAttrs;
1669  THash<TInt, TStrStrVH> NodeStrAttrs;
1670 
1671  #pragma omp parallel sections num_threads(4)
1672  {
1673  #pragma omp section
1674  { SrcCol1.Reserve(NumRows, NumRows); }
1675  #pragma omp section
1676  { EdgeCol1.Reserve(NumRows, NumRows); }
1677  #pragma omp section
1678  { DstCol2.Reserve(NumRows, NumRows); }
1679  #pragma omp section
1680  { EdgeCol2.Reserve(NumRows, NumRows); }
1681  }
1683 
1685  TIntPrV Partitions;
1686  Table->GetPartitionRanges(Partitions, omp_get_max_threads());
1687  TInt PartitionSize = Partitions[0].GetVal2()-Partitions[0].GetVal1()+1;
1688 
1689 
1690  // double endPartition = omp_get_wtime();
1691  // printf("Partition time = %f\n", endPartition-endResize);
1692 
1693  omp_set_num_threads(omp_get_max_threads());
1694  if (NodeType == atInt) {
1695  #pragma omp parallel for schedule(static)
1696  for (int i = 0; i < Partitions.Len(); i++) {
1697  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1698  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1699  while (RowI < EndI) {
1700  TInt RowId = RowI.GetRowIdx();
1701  SrcCol1[RowId] = RowI.GetIntAttr(SrcColIdx);
1702  EdgeCol1[RowId] = RowId;
1703  DstCol2[RowId] = RowI.GetIntAttr(DstColIdx);
1704  EdgeCol2[RowId] = RowId;
1705  RowI++;
1706  }
1707  }
1708  }
1709  else if (NodeType == atStr) {
1710  #pragma omp parallel for schedule(static)
1711  for (int i = 0; i < Partitions.Len(); i++) {
1712  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1713  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1714  while (RowI < EndI) {
1715  TInt RowId = RowI.GetRowIdx();
1716  SrcCol1[RowId] = RowI.GetStrMapById(SrcColIdx);
1717  EdgeCol1[RowId] = RowId;
1718  DstCol2[RowId] = RowI.GetStrMapById(DstColIdx);
1719  EdgeCol2[RowId] = RowId;
1720  RowI++;
1721  }
1722  }
1723  }
1725 
1726  Sw->Start(TStopwatch::Sort);
1727  omp_set_num_threads(omp_get_max_threads());
1728  #pragma omp parallel
1729  {
1730  #pragma omp single nowait
1731  {
1732  #ifndef GLib_WIN32
1733  #pragma omp task untied shared(SrcCol1, EdgeCol1)
1734  #endif
1735  { TTable::QSortKeyVal(SrcCol1, EdgeCol1, 0, NumRows-1); }
1736  }
1737  #pragma omp single nowait
1738  {
1739  #ifndef GLib_WIN32
1740  #pragma omp task untied shared(EdgeCol2, DstCol2)
1741  #endif
1742  { TTable::QSortKeyVal(DstCol2, EdgeCol2, 0, NumRows-1); }
1743  }
1744  #ifndef GLib_WIN32
1745  #pragma omp taskwait
1746  #endif
1747  }
1748  Sw->Stop(TStopwatch::Sort);
1749 
1750  Sw->Start(TStopwatch::Group);
1751  TInt NumThreads = omp_get_max_threads();
1752  TInt PartSize = (NumRows/NumThreads);
1753 
1754  // Find the offset of all partitions, each of which contains a list of rows.
1755  // Nodes from same sources or destinations are ensured to be kept within same partition.
1756  TIntV SrcOffsets, DstOffsets;
1757  SrcOffsets.Add(0);
1758  for (TInt i = 1; i < NumThreads; i++) {
1759  TInt CurrOffset = i * PartSize;
1760  while (CurrOffset < (i+1) * PartSize &&
1761  SrcCol1[CurrOffset-1] == SrcCol1[CurrOffset]) {
1762  // ensure that rows from the same sources are grouped together
1763  CurrOffset++;
1764  }
1765  if (CurrOffset < (i+1) * PartSize) { SrcOffsets.Add(CurrOffset); }
1766  }
1767  SrcOffsets.Add(NumRows);
1768 
1769  DstOffsets.Add(0);
1770  for (TInt i = 1; i < NumThreads; i++) {
1771  TInt CurrOffset = i * PartSize;
1772  while (CurrOffset < (i+1) * PartSize &&
1773  DstCol2[CurrOffset-1] == DstCol2[CurrOffset]) {
1774  // ensure that rows to the same destinations are grouped together
1775  CurrOffset++;
1776  }
1777  if (CurrOffset < (i+1) * PartSize) { DstOffsets.Add(CurrOffset); }
1778  }
1779  DstOffsets.Add(NumRows);
1780 
1781  TInt SrcPartCnt = SrcOffsets.Len()-1; // number of partitions
1782  TInt DstPartCnt = DstOffsets.Len()-1; // number of partitions
1783 
1784  // count the number of source nodes and destination nodes in each partition
1785  TIntV SrcNodeCounts, DstNodeCounts;
1786  SrcNodeCounts.Reserve(SrcPartCnt, SrcPartCnt);
1787  DstNodeCounts.Reserve(DstPartCnt, DstPartCnt);
1788 
1789  #pragma omp parallel for schedule(dynamic)
1790  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
1791  if (t < SrcPartCnt) {
1792  TInt i = t;
1793  if (SrcOffsets[i] != SrcOffsets[i+1]) {
1794  SrcNodeCounts[i] = 1;
1795  TInt CurrNode = SrcCol1[SrcOffsets[i]];
1796  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
1797  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
1798  if (j < SrcOffsets[i+1]) {
1799  SrcNodeCounts[i]++;
1800  CurrNode = SrcCol1[j];
1801  }
1802  }
1803  }
1804  } else {
1805  TInt i = t - SrcPartCnt;
1806  if (DstOffsets[i] != DstOffsets[i+1]) {
1807  DstNodeCounts[i] = 1;
1808  TInt CurrNode = DstCol2[DstOffsets[i]];
1809  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
1810  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
1811  if (j < DstOffsets[i+1]) {
1812  DstNodeCounts[i]++;
1813  CurrNode = DstCol2[j];
1814  }
1815  }
1816  }
1817  }
1818  }
1819 
1820  TInt TotalSrcNodes = 0;
1821  TIntV SrcIdOffsets;
1822  for (int i = 0; i < SrcPartCnt; i++) {
1823  SrcIdOffsets.Add(TotalSrcNodes);
1824  TotalSrcNodes += SrcNodeCounts[i];
1825  }
1826 
1827  TInt TotalDstNodes = 0;
1828  TIntV DstIdOffsets;
1829  for (int i = 0; i < DstPartCnt; i++) {
1830  DstIdOffsets.Add(TotalDstNodes);
1831  TotalDstNodes += DstNodeCounts[i];
1832  }
1833 
1834  // printf("Total Src = %d, Total Dst = %d\n", TotalSrcNodes.Val, TotalDstNodes.Val);
1835 
1836  // find vector of (node_id, start_offset) where start_offset is the index of the first row with node_id
1837  TIntPrV SrcNodeIds, DstNodeIds;
1838  #pragma omp parallel sections
1839  {
1840  #pragma omp section
1841  { SrcNodeIds.Reserve(TotalSrcNodes, TotalSrcNodes); }
1842  #pragma omp section
1843  { DstNodeIds.Reserve(TotalDstNodes, TotalDstNodes); }
1844  }
1845 
1846  // Find the starting offset of each node (in both src and dst)
1847  #pragma omp parallel for schedule(dynamic)
1848  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
1849  if (t < SrcPartCnt) {
1850  TInt i = t;
1851  if (SrcOffsets[i] != SrcOffsets[i+1]) {
1852  TInt CurrNode = SrcCol1[SrcOffsets[i]];
1853  TInt ThreadOffset = SrcIdOffsets[i];
1854  SrcNodeIds[ThreadOffset] = TIntPr(CurrNode, SrcOffsets[i]);
1855  TInt CurrCount = 1;
1856  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
1857  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
1858  if (j < SrcOffsets[i+1]) {
1859  CurrNode = SrcCol1[j];
1860  SrcNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
1861  CurrCount++;
1862  }
1863  }
1864  }
1865  } else {
1866  TInt i = t - SrcPartCnt;
1867  if (DstOffsets[i] != DstOffsets[i+1]) {
1868  TInt CurrNode = DstCol2[DstOffsets[i]];
1869  TInt ThreadOffset = DstIdOffsets[i];
1870  DstNodeIds[ThreadOffset] = TIntPr(CurrNode, DstOffsets[i]);
1871  TInt CurrCount = 1;
1872  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
1873  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
1874  if (j < DstOffsets[i+1]) {
1875  CurrNode = DstCol2[j];
1876  DstNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
1877  CurrCount++;
1878  }
1879  }
1880  }
1881  }
1882  }
1883  Sw->Stop(TStopwatch::Group);
1884 
1886  // Find the combined neighborhood (both out-neighbors and in-neighbors) of each node
1887  TIntTrV Nodes;
1888  Nodes.Reserve(TotalSrcNodes+TotalDstNodes);
1889 
1890  TInt i = 0, j = 0;
1891  while (i < TotalSrcNodes && j < TotalDstNodes) {
1892  if (SrcNodeIds[i].Val1 == DstNodeIds[j].Val1) {
1893  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, j));
1894  i++;
1895  j++;
1896  } else if (SrcNodeIds[i].Val1 < DstNodeIds[j].Val1) {
1897  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1));
1898  i++;
1899  } else {
1900  Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j));
1901  j++;
1902  }
1903  }
1904  for (; i < TotalSrcNodes; i++) { Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1)); }
1905  for (; j < TotalDstNodes; j++) { Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j)); }
1907 
1909  TInt NumNodes = Nodes.Len();
1910  PGraphMP Graph = PGraphMP::TObj::New(NumNodes, NumRows);
1911 // NumThreads = omp_get_max_threads();
1912 // int Delta = (NumNodes+NumThreads-1)/NumThreads;
1913 
1914  TVec<TIntV> InVV(NumNodes);
1915  TVec<TIntV> OutVV(NumNodes);
1916 
1917 // omp_set_num_threads(NumThreads);
1918  #pragma omp parallel for schedule(static,100)
1919  for (int m = 0; m < NumNodes; m++) {
1920  //double startTr = omp_get_wtime();
1921  //TIntV OutV, InV;
1922  TInt n, i, j;
1923  Nodes[m].GetVal(n, i, j);
1924  if (i >= 0) {
1925  TInt Offset = SrcNodeIds[i].GetVal2();
1926  TInt Sz = EdgeCol1.Len()-Offset;
1927  if (i < SrcNodeIds.Len()-1) { Sz = SrcNodeIds[i+1].GetVal2()-Offset; }
1928  OutVV[m].Reserve(Sz);
1929  OutVV[m].CopyUniqueFrom(EdgeCol1, Offset, Sz);
1930  }
1931  if (j >= 0) {
1932  TInt Offset = DstNodeIds[j].GetVal2();
1933  TInt Sz = EdgeCol2.Len()-Offset;
1934  if (j < DstNodeIds.Len()-1) { Sz = DstNodeIds[j+1].GetVal2()-Offset; }
1935  InVV[m].Reserve(Sz);
1936  InVV[m].CopyUniqueFrom(EdgeCol2, Offset, Sz);
1937  }
1938  Graph->AddNodeWithEdges(n, InVV[m], OutVV[m]);
1939  }
1940  Graph->SetNodes(NumNodes);
1942 
1944  omp_set_num_threads(omp_get_max_threads());
1945  if (NodeType == atInt) {
1946  #pragma omp parallel for schedule(static)
1947  for (int i = 0; i < Partitions.Len(); i++) {
1948  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1949  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1950  while (RowI < EndI) {
1951  TInt RowId = RowI.GetRowIdx(); // EdgeId
1952  TInt SrcId = RowI.GetIntAttr(SrcColIdx);
1953  TInt DstId = RowI.GetIntAttr(DstColIdx);
1954  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
1955  RowI++;
1956  }
1957  }
1958  }
1959  else if (NodeType == atStr) {
1960  #pragma omp parallel for schedule(static)
1961  for (int i = 0; i < Partitions.Len(); i++) {
1962  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1963  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1964  while (RowI < EndI) {
1965  TInt RowId = RowI.GetRowIdx(); // EdgeId
1966  TInt SrcId = RowI.GetStrMapById(SrcColIdx);
1967  TInt DstId = RowI.GetStrMapById(DstColIdx);
1968  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
1969  RowI++;
1970  }
1971  }
1972 
1973  }
1974 
1975  Graph->SetEdges(NumRows);
1976  Graph->SetMxEId(NumRows);
1978 
1979  // make single pass over all rows in the table to add attributes
1980  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
1981  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
1982  continue;
1983  }
1984  for (TInt ea_i = 0; ea_i < EdgeAttrV.Len(); ea_i++) {
1985  TStr ColName = EdgeAttrV[ea_i];
1986  TAttrType T = Table->GetColType(ColName);
1987  TInt Index = Table->GetColIdx(ColName);
1988  switch (T) {
1989  case atInt:
1990  Graph->AddIntAttrDatE(CurrRowIdx, Table->IntCols[Index][CurrRowIdx], ColName);
1991  break;
1992  case atFlt:
1993  Graph->AddFltAttrDatE(CurrRowIdx, Table->FltCols[Index][CurrRowIdx], ColName);
1994  break;
1995  case atStr:
1996  Graph->AddStrAttrDatE(CurrRowIdx, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
1997  break;
1998  }
1999  }
2000  }
2001  // double endAdd = omp_get_wtime();
2002  // printf("Add time = %f\n", endAdd-endAlloc);
2003 
2004  return Graph;
2005 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Start(const TExperiment Exp)
Start a new experiment.
Definition: util.cpp:733
Definition: gbase.h:23
void Stop(const TExperiment Exp)
Stop the current experiment.
Definition: util.cpp:737
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
#define Assert(Cond)
Definition: bd.h:251
static void QSortKeyVal(TIntV &Key, TIntV &Val, TInt Start, TInt End)
Definition: table.cpp:5378
Definition: dt.h:1137
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:171
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
static TStopwatch * GetInstance()
Definition: util.h:82
Definition: gbase.h:23
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Routines to benchmark table operations.
Definition: util.h:71
template<class PGraphMP >
PGraphMP TSnap::ToNetworkMP ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TStrV EdgeAttrV,
PTable  NodeTable,
const TStr NodeCol,
TStrV NodeAttrV,
TAttrAggr  AggrPolicy 
)
inline

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.

Definition at line 2131 of file conv.h.

2134  {
2136 
2138  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
2139  const TInt DstColIdx = Table->GetColIdx(DstCol);
2140  const TInt NumRows = Table->GetNumValidRows();
2141 
2142  const TAttrType NodeType = Table->GetColType(SrcCol);
2143  Assert(NodeType == Table->GetColType(DstCol));
2144 
2145 
2146  TIntV SrcCol1, EdgeCol1, EdgeCol2, DstCol2;
2147 
2148  const TAttrType NodeTypeN = NodeTable->GetColType(NodeCol);
2149  const TInt NodeColIdx = NodeTable->GetColIdx(NodeCol);
2150  THash<TInt, TStrIntVH> NodeIntAttrs;
2151  THash<TInt, TStrFltVH> NodeFltAttrs;
2152  THash<TInt, TStrStrVH> NodeStrAttrs;
2153 
2154  #pragma omp parallel sections num_threads(4)
2155  {
2156  #pragma omp section
2157  { SrcCol1.Reserve(NumRows, NumRows); }
2158  #pragma omp section
2159  { EdgeCol1.Reserve(NumRows, NumRows); }
2160  #pragma omp section
2161  { DstCol2.Reserve(NumRows, NumRows); }
2162  #pragma omp section
2163  { EdgeCol2.Reserve(NumRows, NumRows); }
2164  }
2166 
2168  TIntPrV Partitions;
2169  Table->GetPartitionRanges(Partitions, omp_get_max_threads());
2170  TInt PartitionSize = Partitions[0].GetVal2()-Partitions[0].GetVal1()+1;
2171 
2172  // double endPartition = omp_get_wtime();
2173  // printf("Partition time = %f\n", endPartition-endResize);
2174 
2175  omp_set_num_threads(omp_get_max_threads());
2176  if (NodeType == atInt) {
2177  #pragma omp parallel for schedule(static)
2178  for (int i = 0; i < Partitions.Len(); i++) {
2179  TRowIterator RowI(Partitions[i].GetVal1(), Table());
2180  TRowIterator EndI(Partitions[i].GetVal2(), Table());
2181  while (RowI < EndI) {
2182  TInt RowId = RowI.GetRowIdx();
2183  SrcCol1[RowId] = RowI.GetIntAttr(SrcColIdx);
2184  EdgeCol1[RowId] = RowId;
2185  DstCol2[RowId] = RowI.GetIntAttr(DstColIdx);
2186  EdgeCol2[RowId] = RowId;
2187  RowI++;
2188  }
2189  }
2190  }
2191  else if (NodeType == atStr) {
2192  #pragma omp parallel for schedule(static)
2193  for (int i = 0; i < Partitions.Len(); i++) {
2194  TRowIterator RowI(Partitions[i].GetVal1(), Table());
2195  TRowIterator EndI(Partitions[i].GetVal2(), Table());
2196  while (RowI < EndI) {
2197  TInt RowId = RowI.GetRowIdx();
2198  SrcCol1[RowId] = RowI.GetStrMapById(SrcColIdx);
2199  EdgeCol1[RowId] = RowId;
2200  DstCol2[RowId] = RowI.GetStrMapById(DstColIdx);
2201  EdgeCol2[RowId] = RowId;
2202  RowI++;
2203  }
2204  }
2205  }
2207 
2208  Sw->Start(TStopwatch::Sort);
2209  omp_set_num_threads(omp_get_max_threads());
2210  #pragma omp parallel
2211  {
2212  #pragma omp single nowait
2213  {
2214  #ifndef GLib_WIN32
2215  #pragma omp task untied shared(SrcCol1, EdgeCol1)
2216  #endif
2217  { TTable::QSortKeyVal(SrcCol1, EdgeCol1, 0, NumRows-1); }
2218  }
2219  #pragma omp single nowait
2220  {
2221  #ifndef GLib_WIN32
2222  #pragma omp task untied shared(EdgeCol2, DstCol2)
2223  #endif
2224  { TTable::QSortKeyVal(DstCol2, EdgeCol2, 0, NumRows-1); }
2225  }
2226  #ifndef GLib_WIN32
2227  #pragma omp taskwait
2228  #endif
2229  }
2230  Sw->Stop(TStopwatch::Sort);
2231 
2232  Sw->Start(TStopwatch::Group);
2233  TInt NumThreads = omp_get_max_threads();
2234  TInt PartSize = (NumRows/NumThreads);
2235 
2236  // Find the offset of all partitions, each of which contains a list of rows.
2237  // Nodes from same sources or destinations are ensured to be kept within same partition.
2238  TIntV SrcOffsets, DstOffsets;
2239  SrcOffsets.Add(0);
2240  for (TInt i = 1; i < NumThreads; i++) {
2241  TInt CurrOffset = i * PartSize;
2242  while (CurrOffset < (i+1) * PartSize &&
2243  SrcCol1[CurrOffset-1] == SrcCol1[CurrOffset]) {
2244  // ensure that rows from the same sources are grouped together
2245  CurrOffset++;
2246  }
2247  if (CurrOffset < (i+1) * PartSize) { SrcOffsets.Add(CurrOffset); }
2248  }
2249  SrcOffsets.Add(NumRows);
2250 
2251  DstOffsets.Add(0);
2252  for (TInt i = 1; i < NumThreads; i++) {
2253  TInt CurrOffset = i * PartSize;
2254  while (CurrOffset < (i+1) * PartSize &&
2255  DstCol2[CurrOffset-1] == DstCol2[CurrOffset]) {
2256  // ensure that rows to the same destinations are grouped together
2257  CurrOffset++;
2258  }
2259  if (CurrOffset < (i+1) * PartSize) { DstOffsets.Add(CurrOffset); }
2260  }
2261  DstOffsets.Add(NumRows);
2262 
2263  TInt SrcPartCnt = SrcOffsets.Len()-1; // number of partitions
2264  TInt DstPartCnt = DstOffsets.Len()-1; // number of partitions
2265 
2266  // count the number of source nodes and destination nodes in each partition
2267  TIntV SrcNodeCounts, DstNodeCounts;
2268  SrcNodeCounts.Reserve(SrcPartCnt, SrcPartCnt);
2269  DstNodeCounts.Reserve(DstPartCnt, DstPartCnt);
2270 
2271  #pragma omp parallel for schedule(dynamic)
2272  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
2273  if (t < SrcPartCnt) {
2274  TInt i = t;
2275  if (SrcOffsets[i] != SrcOffsets[i+1]) {
2276  SrcNodeCounts[i] = 1;
2277  TInt CurrNode = SrcCol1[SrcOffsets[i]];
2278  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
2279  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
2280  if (j < SrcOffsets[i+1]) {
2281  SrcNodeCounts[i]++;
2282  CurrNode = SrcCol1[j];
2283  }
2284  }
2285  }
2286  } else {
2287  TInt i = t - SrcPartCnt;
2288  if (DstOffsets[i] != DstOffsets[i+1]) {
2289  DstNodeCounts[i] = 1;
2290  TInt CurrNode = DstCol2[DstOffsets[i]];
2291  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
2292  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
2293  if (j < DstOffsets[i+1]) {
2294  DstNodeCounts[i]++;
2295  CurrNode = DstCol2[j];
2296  }
2297  }
2298  }
2299  }
2300  }
2301 
2302  TInt TotalSrcNodes = 0;
2303  TIntV SrcIdOffsets;
2304  for (int i = 0; i < SrcPartCnt; i++) {
2305  SrcIdOffsets.Add(TotalSrcNodes);
2306  TotalSrcNodes += SrcNodeCounts[i];
2307  }
2308 
2309  TInt TotalDstNodes = 0;
2310  TIntV DstIdOffsets;
2311  for (int i = 0; i < DstPartCnt; i++) {
2312  DstIdOffsets.Add(TotalDstNodes);
2313  TotalDstNodes += DstNodeCounts[i];
2314  }
2315 
2316  // printf("Total Src = %d, Total Dst = %d\n", TotalSrcNodes.Val, TotalDstNodes.Val);
2317 
2318  // find vector of (node_id, start_offset) where start_offset is the index of the first row with node_id
2319  TIntPrV SrcNodeIds, DstNodeIds;
2320  #pragma omp parallel sections
2321  {
2322  #pragma omp section
2323  { SrcNodeIds.Reserve(TotalSrcNodes, TotalSrcNodes); }
2324  #pragma omp section
2325  { DstNodeIds.Reserve(TotalDstNodes, TotalDstNodes); }
2326  }
2327 
2328  // Find the starting offset of each node (in both src and dst)
2329  #pragma omp parallel for schedule(dynamic)
2330  for (int t = 0; t < SrcPartCnt+DstPartCnt; t++) {
2331  if (t < SrcPartCnt) {
2332  TInt i = t;
2333  if (SrcOffsets[i] != SrcOffsets[i+1]) {
2334  TInt CurrNode = SrcCol1[SrcOffsets[i]];
2335  TInt ThreadOffset = SrcIdOffsets[i];
2336  SrcNodeIds[ThreadOffset] = TIntPr(CurrNode, SrcOffsets[i]);
2337  TInt CurrCount = 1;
2338  for (TInt j = SrcOffsets[i]+1; j < SrcOffsets[i+1]; j++) {
2339  while (j < SrcOffsets[i+1] && SrcCol1[j] == CurrNode) { j++; }
2340  if (j < SrcOffsets[i+1]) {
2341  CurrNode = SrcCol1[j];
2342  SrcNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
2343  CurrCount++;
2344  }
2345  }
2346  }
2347  } else {
2348  TInt i = t - SrcPartCnt;
2349  if (DstOffsets[i] != DstOffsets[i+1]) {
2350  TInt CurrNode = DstCol2[DstOffsets[i]];
2351  TInt ThreadOffset = DstIdOffsets[i];
2352  DstNodeIds[ThreadOffset] = TIntPr(CurrNode, DstOffsets[i]);
2353  TInt CurrCount = 1;
2354  for (TInt j = DstOffsets[i]+1; j < DstOffsets[i+1]; j++) {
2355  while (j < DstOffsets[i+1] && DstCol2[j] == CurrNode) { j++; }
2356  if (j < DstOffsets[i+1]) {
2357  CurrNode = DstCol2[j];
2358  DstNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
2359  CurrCount++;
2360  }
2361  }
2362  }
2363  }
2364  }
2365  Sw->Stop(TStopwatch::Group);
2366 
2368  // Find the combined neighborhood (both out-neighbors and in-neighbors) of each node
2369  TIntTrV Nodes;
2370  Nodes.Reserve(TotalSrcNodes+TotalDstNodes);
2371 
2372  TInt i = 0, j = 0;
2373  while (i < TotalSrcNodes && j < TotalDstNodes) {
2374  if (SrcNodeIds[i].Val1 == DstNodeIds[j].Val1) {
2375  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, j));
2376  i++;
2377  j++;
2378  } else if (SrcNodeIds[i].Val1 < DstNodeIds[j].Val1) {
2379  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1));
2380  i++;
2381  } else {
2382  Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j));
2383  j++;
2384  }
2385  }
2386  for (; i < TotalSrcNodes; i++) { Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1)); }
2387  for (; j < TotalDstNodes; j++) { Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j)); }
2389 
2391  TInt NumNodes = Nodes.Len();
2392  PGraphMP Graph = PGraphMP::TObj::New(NumNodes, NumRows);
2393 // NumThreads = omp_get_max_threads();
2394 // int Delta = (NumNodes+NumThreads-1)/NumThreads;
2395 
2396  TVec<TIntV> InVV(NumNodes);
2397  TVec<TIntV> OutVV(NumNodes);
2398 
2399 // omp_set_num_threads(NumThreads);
2400  #pragma omp parallel for schedule(static,100)
2401  for (int m = 0; m < NumNodes; m++) {
2402  //double startTr = omp_get_wtime();
2403  //TIntV OutV, InV;
2404  TInt n, i, j;
2405  Nodes[m].GetVal(n, i, j);
2406  if (i >= 0) {
2407  TInt Offset = SrcNodeIds[i].GetVal2();
2408  TInt Sz = EdgeCol1.Len()-Offset;
2409  if (i < SrcNodeIds.Len()-1) { Sz = SrcNodeIds[i+1].GetVal2()-Offset; }
2410  OutVV[m].Reserve(Sz);
2411  OutVV[m].CopyUniqueFrom(EdgeCol1, Offset, Sz);
2412  }
2413  if (j >= 0) {
2414  TInt Offset = DstNodeIds[j].GetVal2();
2415  TInt Sz = EdgeCol2.Len()-Offset;
2416  if (j < DstNodeIds.Len()-1) { Sz = DstNodeIds[j+1].GetVal2()-Offset; }
2417  InVV[m].Reserve(Sz);
2418  InVV[m].CopyUniqueFrom(EdgeCol2, Offset, Sz);
2419  }
2420  Graph->AddNodeWithEdges(n, InVV[m], OutVV[m]);
2421  }
2422  Graph->SetNodes(NumNodes);
2424 
2426  omp_set_num_threads(omp_get_max_threads());
2427  if (NodeType == atInt) {
2428  #pragma omp parallel for schedule(static)
2429  for (int i = 0; i < Partitions.Len(); i++) {
2430  TRowIterator RowI(Partitions[i].GetVal1(), Table());
2431  TRowIterator EndI(Partitions[i].GetVal2(), Table());
2432  while (RowI < EndI) {
2433  TInt RowId = RowI.GetRowIdx(); // EdgeId
2434  TInt SrcId = RowI.GetIntAttr(SrcColIdx);
2435  TInt DstId = RowI.GetIntAttr(DstColIdx);
2436  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
2437  RowI++;
2438  }
2439  }
2440  }
2441  else if (NodeType == atStr) {
2442  #pragma omp parallel for schedule(static)
2443  for (int i = 0; i < Partitions.Len(); i++) {
2444  TRowIterator RowI(Partitions[i].GetVal1(), Table());
2445  TRowIterator EndI(Partitions[i].GetVal2(), Table());
2446  while (RowI < EndI) {
2447  TInt RowId = RowI.GetRowIdx(); // EdgeId
2448  TInt SrcId = RowI.GetStrMapById(SrcColIdx);
2449  TInt DstId = RowI.GetStrMapById(DstColIdx);
2450  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
2451  RowI++;
2452  }
2453  }
2454 
2455  }
2456 
2457  Graph->SetEdges(NumRows);
2458  Graph->SetMxEId(NumRows);
2460 
2461  // make single pass over all rows in the table to add attributes
2462  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
2463  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
2464  continue;
2465  }
2466  for (TInt ea_i = 0; ea_i < EdgeAttrV.Len(); ea_i++) {
2467  TStr ColName = EdgeAttrV[ea_i];
2468  TAttrType T = Table->GetColType(ColName);
2469  TInt Index = Table->GetColIdx(ColName);
2470  switch (T) {
2471  case atInt:
2472  Graph->AddIntAttrDatE(CurrRowIdx, Table->IntCols[Index][CurrRowIdx], ColName);
2473  break;
2474  case atFlt:
2475  Graph->AddFltAttrDatE(CurrRowIdx, Table->FltCols[Index][CurrRowIdx], ColName);
2476  break;
2477  case atStr:
2478  Graph->AddStrAttrDatE(CurrRowIdx, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
2479  break;
2480  }
2481  }
2482  }
2483 
2484  // Add node attribtes
2485  if (NodeAttrV.Len() > 0) {
2486  for (int CurrRowIdx = 0; CurrRowIdx < (NodeTable->Next).Len(); CurrRowIdx++) {
2487  if ((NodeTable->Next)[CurrRowIdx] == NodeTable->Invalid) {
2488  continue;
2489  }
2490  TInt NId;
2491  if (NodeTypeN == atInt) {
2492  NId = (NodeTable->IntCols)[NodeColIdx][CurrRowIdx];
2493  }
2494  else if (NodeTypeN == atStr){
2495  NId = (NodeTable->StrColMaps)[NodeColIdx][CurrRowIdx];
2496  }
2497  for (TInt i = 0; i < NodeAttrV.Len(); i++) {
2498  TStr ColName = NodeAttrV[i];
2499  TAttrType T = NodeTable->GetColType(ColName);
2500  TInt Index = NodeTable->GetColIdx(ColName);
2501  switch (T) {
2502  case atInt:
2503  Graph->AddIntAttrDatN(NId, NodeTable->IntCols[Index][CurrRowIdx], ColName);
2504  break;
2505  case atFlt:
2506  Graph->AddFltAttrDatN(NId, NodeTable->FltCols[Index][CurrRowIdx], ColName);
2507  break;
2508  case atStr:
2509  Graph->AddStrAttrDatN(NId, NodeTable->GetStrValIdx(Index, CurrRowIdx), ColName);
2510  break;
2511  }
2512  }
2513  }
2514  }
2515  // double endAdd = omp_get_wtime();
2516  // printf("Add time = %f\n", endAdd-endAlloc);
2517 
2518  return Graph;
2519 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Start(const TExperiment Exp)
Start a new experiment.
Definition: util.cpp:733
Definition: gbase.h:23
void Stop(const TExperiment Exp)
Stop the current experiment.
Definition: util.cpp:737
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
#define Assert(Cond)
Definition: bd.h:251
static void QSortKeyVal(TIntV &Key, TIntV &Val, TInt Start, TInt End)
Definition: table.cpp:5378
Definition: dt.h:1137
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:171
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
static TStopwatch * GetInstance()
Definition: util.h:82
Definition: gbase.h:23
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Routines to benchmark table operations.
Definition: util.h:71
template<class PGraphMP >
PGraphMP TSnap::ToNetworkMP2 ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TStrV SrcAttrV,
TStrV DstAttrV,
TStrV EdgeAttrV,
TAttrAggr  AggrPolicy 
)
inline

Implements table to network conversion in parallel. Not the recommended algorithm, using ToNetworkMP instead.

Definition at line 1118 of file conv.h.

1121  {
1123 
1125  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
1126  const TInt DstColIdx = Table->GetColIdx(DstCol);
1127  const TInt NumRows = Table->NumValidRows;
1128 
1129  const TAttrType NodeType = Table->GetColType(SrcCol);
1130  Assert(NodeType == Table->GetColType(DstCol));
1131 
1132 
1133 
1134  TIntV SrcCol1, EdgeCol1, EdgeCol2, DstCol2;
1135 
1136  #pragma omp parallel sections num_threads(4)
1137  {
1138  #pragma omp section
1139  { SrcCol1.Reserve(NumRows, NumRows); }
1140  #pragma omp section
1141  { EdgeCol1.Reserve(NumRows, NumRows); }
1142  #pragma omp section
1143  { DstCol2.Reserve(NumRows, NumRows); }
1144  #pragma omp section
1145  { EdgeCol2.Reserve(NumRows, NumRows); }
1146  }
1149  TIntPrV Partitions;
1150 // int NThreads = omp_get_max_threads();
1151  const int NThreads = 40;
1152  Table->GetPartitionRanges(Partitions, NThreads);
1153  TInt PartitionSize = Partitions[0].GetVal2()-Partitions[0].GetVal1()+1;
1154 
1155  // double endPartition = omp_get_wtime();
1156  // printf("Partition time = %f\n", endPartition-endResize);
1157 
1158  if (NodeType == atInt) {
1159  #pragma omp parallel for schedule(static)
1160  for (int i = 0; i < Partitions.Len(); i++) {
1161  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1162  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1163  while (RowI < EndI) {
1164  TInt RowId = RowI.GetRowIdx();
1165  SrcCol1[RowId] = RowI.GetIntAttr(SrcColIdx);
1166  EdgeCol1[RowId] = RowId;
1167  DstCol2[RowId] = RowI.GetIntAttr(DstColIdx);
1168  EdgeCol2[RowId] = RowId;
1169  RowI++;
1170  }
1171  }
1172  }
1173  else if (NodeType == atStr) {
1174  #pragma omp parallel for schedule(static)
1175  for (int i = 0; i < Partitions.Len(); i++) {
1176  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1177  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1178  while (RowI < EndI) {
1179  TInt RowId = RowI.GetRowIdx();
1180  SrcCol1[RowId] = RowI.GetStrMapById(SrcColIdx);
1181  EdgeCol1[RowId] = RowId;
1182  DstCol2[RowId] = RowI.GetStrMapById(DstColIdx);
1183  EdgeCol2[RowId] = RowId;
1184  RowI++;
1185  }
1186  }
1187 
1188  }
1189 
1190 // printf("NumRows = %d\n", NumRows.Val);
1191 // printf("NThreads = %d\n", NThreads);
1192 // for (int i = 0; i < Partitions.Len(); i++) {
1193 // printf("Partition %d %d->%d\n", i, Partitions[i].GetVal1().Val, Partitions[i].GetVal2().Val);
1194 // }
1195  int Parts[NThreads+1];
1196  for (int i = 0; i < NThreads; i++) {
1197  Parts[i] = NumRows.Val / NThreads * i;
1198  }
1199  Parts[NThreads] = NumRows;
1200 // for (int i = 0; i < NThreads+1; i++) {
1201 // printf("Parts[%d] = %d\n", i, Parts[i]);
1202 // }
1204 
1205  Sw->Start(TStopwatch::Sort);
1206  TInt ExtremePoints[4][NThreads];
1207  omp_set_num_threads(omp_get_max_threads());
1208  #pragma omp parallel
1209  {
1210  #pragma omp for schedule(static) nowait
1211  for (int i = 0; i < NThreads; i++) {
1212  TInt StartPos = Parts[i];
1213  TInt EndPos = Parts[i+1]-1;
1214  // TODO: Handle empty partition
1215  TTable::QSortKeyVal(SrcCol1, EdgeCol1, StartPos, EndPos);
1216  ExtremePoints[0][i] = SrcCol1[StartPos];
1217  ExtremePoints[2][i] = SrcCol1[EndPos];
1218  }
1219  #pragma omp for schedule(static) nowait
1220  for (int i = 0; i < NThreads; i++) {
1221  TInt StartPos = Parts[i];
1222  TInt EndPos = Parts[i+1]-1;
1223  // TODO: Handle empty partition
1224  TTable::QSortKeyVal(DstCol2, EdgeCol2, StartPos, EndPos);
1225  ExtremePoints[1][i] = DstCol2[StartPos];
1226  ExtremePoints[3][i] = DstCol2[EndPos];
1227  }
1228  }
1229 // for (int i = 0; i < NThreads; i++) {
1230 // printf("ExtremePoints[%d] = %d-%d -> %d-%d\n", i, ExtremePoints[0][i].Val, ExtremePoints[1][i].Val, ExtremePoints[2][i].Val, ExtremePoints[3][i].Val);
1231 // }
1232 
1233  // find min points
1234  TInt MinId(INT_MAX);
1235  for (int j = 0; j < 2; j++) {
1236  for (int i = 0; i < NThreads; i++) {
1237  if (MinId > ExtremePoints[j][i]) { MinId = ExtremePoints[j][i]; }
1238  }
1239  }
1240  TInt MaxId(-1);
1241  for (int j = 2; j < 4; j++) {
1242  for (int i = 0; i < NThreads; i++) {
1243  if (MaxId < ExtremePoints[j][i]) { MaxId = ExtremePoints[j][i]; }
1244  }
1245  }
1246 // printf("MinId = %d\n", MinId.Val);
1247 // printf("MaxId = %d\n", MaxId.Val);
1248  Sw->Stop(TStopwatch::Sort);
1249 
1250  Sw->Start(TStopwatch::Group);
1251 // const int NumCollectors = omp_get_max_threads();
1252  const int NumCollectors = 20;
1253  int Range = MaxId.Val - MinId.Val;
1254  TIntV IdRanges(NumCollectors+1);
1255  for (int j = 0; j < NumCollectors; j++) {
1256  IdRanges[j] = MinId + Range/NumCollectors*j;
1257  }
1258  IdRanges[NumCollectors] = MaxId+1;
1259 // for (int i = 0; i < NumCollectors+1; i++) {
1260 // printf("IdRanges[%d] = %d\n", i, IdRanges[i].Val);
1261 // }
1262 
1263  int SrcOffsets[NThreads][NumCollectors+1];
1264  #pragma omp parallel for schedule(static)
1265  for (int i = 0; i < NThreads; i++) {
1266  int CollectorId = 0;
1267  for (int j = Parts[i]; j < Parts[i+1]; j++) {
1268  while (SrcCol1[j] >= IdRanges[CollectorId]) {
1269  SrcOffsets[i][CollectorId++] = j;
1270  }
1271  }
1272  while (CollectorId <= NumCollectors) {
1273  SrcOffsets[i][CollectorId++] = Parts[i+1];
1274  }
1275  }
1276  int DstOffsets[NThreads][NumCollectors+1];
1277  #pragma omp parallel for schedule(static)
1278  for (int i = 0; i < NThreads; i++) {
1279  int CollectorId = 0;
1280  for (int j = Parts[i]; j < Parts[i+1]; j++) {
1281  while (DstCol2[j] >= IdRanges[CollectorId]) {
1282  DstOffsets[i][CollectorId++] = j;
1283  }
1284  }
1285  while (CollectorId <= NumCollectors) {
1286  DstOffsets[i][CollectorId++] = Parts[i+1];
1287  }
1288  }
1289 // for (int i = 0; i < NThreads; i++) {
1290 // for (int j = 0; j < NumCollectors+1; j++) {
1291 // printf("SrcOffsets[%d][%d] = %d\n", i, j, SrcOffsets[i][j]);
1292 // }
1293 // }
1294 // for (int i = 0; i < NThreads; i++) {
1295 // for (int j = 0; j < NumCollectors+1; j++) {
1296 // printf("DstOffsets[%d][%d] = %d\n", i, j, DstOffsets[i][j]);
1297 // }
1298 // }
1299 
1300  TIntV SrcCollectorOffsets(NumCollectors+1);
1301  SrcCollectorOffsets[0] = 0;
1302  for (int k = 0; k < NumCollectors; k++) {
1303  int SumOffset = 0;
1304  for (int i = 0; i < NThreads; i++) {
1305  SumOffset += SrcOffsets[i][k+1] - SrcOffsets[i][k];
1306  }
1307  SrcCollectorOffsets[k+1] = SrcCollectorOffsets[k] + SumOffset;
1308  }
1309  TIntV DstCollectorOffsets(NumCollectors+1);
1310  DstCollectorOffsets[0] = 0;
1311  for (int k = 0; k < NumCollectors; k++) {
1312  int SumOffset = 0;
1313  for (int i = 0; i < NThreads; i++) {
1314  SumOffset += DstOffsets[i][k+1] - DstOffsets[i][k];
1315  }
1316  DstCollectorOffsets[k+1] = DstCollectorOffsets[k] + SumOffset;
1317  }
1318 // for (int i = 0; i < NumCollectors+1; i++) {
1319 // printf("SrcCollectorOffsets[%d] = %d\n", i, SrcCollectorOffsets[i].Val);
1320 // }
1321 // for (int i = 0; i < NumCollectors+1; i++) {
1322 // printf("DstCollectorOffsets[%d] = %d\n", i, DstCollectorOffsets[i].Val);
1323 // }
1324 
1325  TIntV SrcCol3, EdgeCol3, EdgeCol4, DstCol4;
1326  #pragma omp parallel sections num_threads(4)
1327  {
1328  #pragma omp section
1329  { SrcCol3.Reserve(NumRows, NumRows); }
1330  #pragma omp section
1331  { EdgeCol3.Reserve(NumRows, NumRows); }
1332  #pragma omp section
1333  { DstCol4.Reserve(NumRows, NumRows); }
1334  #pragma omp section
1335  { EdgeCol4.Reserve(NumRows, NumRows); }
1336  }
1337 
1338  TIntV SrcNodeCounts(NumCollectors), DstNodeCounts(NumCollectors);
1339  #pragma omp parallel for schedule(static)
1340  for (int k = 0; k < NumCollectors; k++) {
1341  int ind = SrcCollectorOffsets[k];
1342  for (int i = 0; i < NThreads; i++) {
1343  for (int j = SrcOffsets[i][k]; j < SrcOffsets[i][k+1]; j++) {
1344  SrcCol3[ind] = SrcCol1[j];
1345  EdgeCol3[ind] = EdgeCol1[j];
1346  ind++;
1347  }
1348  }
1349  TTable::QSortKeyVal(SrcCol3, EdgeCol3, SrcCollectorOffsets[k], SrcCollectorOffsets[k+1]-1);
1350  int SrcCount = 0;
1351  if (SrcCollectorOffsets[k+1] > SrcCollectorOffsets[k]) {
1352  SrcCount = 1;
1353  for (int j = SrcCollectorOffsets[k]+1; j < SrcCollectorOffsets[k+1]; j++) {
1354  if (SrcCol3[j] != SrcCol3[j-1]) { SrcCount++; }
1355  }
1356  }
1357  SrcNodeCounts[k] = SrcCount;
1358 
1359  ind = DstCollectorOffsets[k];
1360  for (int i = 0; i < NThreads; i++) {
1361  for (int j = DstOffsets[i][k]; j < DstOffsets[i][k+1]; j++) {
1362  DstCol4[ind] = DstCol2[j];
1363  EdgeCol4[ind] = EdgeCol2[j];
1364  ind++;
1365  }
1366  }
1367  TTable::QSortKeyVal(DstCol4, EdgeCol4, DstCollectorOffsets[k], DstCollectorOffsets[k+1]-1);
1368  int DstCount = 0;
1369  if (DstCollectorOffsets[k+1] > DstCollectorOffsets[k]) {
1370  DstCount = 1;
1371  for (int j = DstCollectorOffsets[k]+1; j < DstCollectorOffsets[k+1]; j++) {
1372  if (DstCol4[j] != DstCol4[j-1]) { DstCount++; }
1373  }
1374  }
1375  DstNodeCounts[k] = DstCount;
1376  }
1377 
1378  TInt TotalSrcNodes = 0;
1379  TIntV SrcIdOffsets;
1380  for (int i = 0; i < NumCollectors; i++) {
1381  SrcIdOffsets.Add(TotalSrcNodes);
1382  TotalSrcNodes += SrcNodeCounts[i];
1383  }
1384 
1385 // printf("Sorted = %d - %d\n", SrcCol3.IsSorted(), DstCol4.IsSorted());
1386 // for (int i = 0; i < NumRows-1; i++) {
1387 // if (SrcCol3[i] > SrcCol3[i+1]) { printf("i=%d: %d %d\n", i, SrcCol3[i].Val, SrcCol3[i+1].Val); }
1388 // }
1389 // for (int i = 0; i < NumRows-1; i++) {
1390 // if (DstCol4[i] > DstCol4[i+1]) { printf("i=%d: %d %d\n", i, DstCol4[i].Val, DstCol4[i+1].Val); }
1391 // }
1392 
1393  TInt TotalDstNodes = 0;
1394  TIntV DstIdOffsets;
1395  for (int i = 0; i < NumCollectors; i++) {
1396  DstIdOffsets.Add(TotalDstNodes);
1397  TotalDstNodes += DstNodeCounts[i];
1398  }
1399 
1400  // find vector of (node_id, start_offset) where start_offset is the index of the first row with node_id
1401  TIntPrV SrcNodeIds, DstNodeIds;
1402  #pragma omp parallel sections
1403  {
1404  #pragma omp section
1405  { SrcNodeIds.Reserve(TotalSrcNodes, TotalSrcNodes); }
1406  #pragma omp section
1407  { DstNodeIds.Reserve(TotalDstNodes, TotalDstNodes); }
1408  }
1409 
1410  // Find the starting offset of each node (in both src and dst)
1411  #pragma omp parallel for schedule(dynamic)
1412  for (int t = 0; t < 2*NumCollectors; t++) {
1413  if (t < NumCollectors) {
1414  TInt i = t;
1415  if (SrcCollectorOffsets[i] < SrcCollectorOffsets[i+1]) {
1416  TInt CurrNode = SrcCol3[SrcCollectorOffsets[i]];
1417  TInt ThreadOffset = SrcIdOffsets[i];
1418  SrcNodeIds[ThreadOffset] = TIntPr(CurrNode, SrcCollectorOffsets[i]);
1419  TInt CurrCount = 1;
1420  for (TInt j = SrcCollectorOffsets[i]+1; j < SrcCollectorOffsets[i+1]; j++) {
1421  while (j < SrcCollectorOffsets[i+1] && SrcCol3[j] == CurrNode) { j++; }
1422  if (j < SrcCollectorOffsets[i+1]) {
1423  CurrNode = SrcCol3[j];
1424  SrcNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
1425  CurrCount++;
1426  }
1427  }
1428  }
1429  } else {
1430  TInt i = t - NumCollectors;
1431  if (DstCollectorOffsets[i] < DstCollectorOffsets[i+1]) {
1432  TInt CurrNode = DstCol4[DstCollectorOffsets[i]];
1433  TInt ThreadOffset = DstIdOffsets[i];
1434  DstNodeIds[ThreadOffset] = TIntPr(CurrNode, DstCollectorOffsets[i]);
1435  TInt CurrCount = 1;
1436  for (TInt j = DstCollectorOffsets[i]+1; j < DstCollectorOffsets[i+1]; j++) {
1437  while (j < DstCollectorOffsets[i+1] && DstCol4[j] == CurrNode) { j++; }
1438  if (j < DstCollectorOffsets[i+1]) {
1439  CurrNode = DstCol4[j];
1440  DstNodeIds[ThreadOffset+CurrCount] = TIntPr(CurrNode, j);
1441  CurrCount++;
1442  }
1443  }
1444  }
1445  }
1446  }
1447  Sw->Stop(TStopwatch::Group);
1448 
1450  // Find the combined neighborhood (both out-neighbors and in-neighbors) of each node
1451  TIntTrV Nodes;
1452  Nodes.Reserve(TotalSrcNodes+TotalDstNodes);
1453 
1454  TInt i = 0, j = 0;
1455  while (i < TotalSrcNodes && j < TotalDstNodes) {
1456  if (SrcNodeIds[i].Val1 == DstNodeIds[j].Val1) {
1457  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, j));
1458  i++;
1459  j++;
1460  } else if (SrcNodeIds[i].Val1 < DstNodeIds[j].Val1) {
1461  Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1));
1462  i++;
1463  } else {
1464  Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j));
1465  j++;
1466  }
1467  }
1468  for (; i < TotalSrcNodes; i++) { Nodes.Add(TIntTr(SrcNodeIds[i].Val1, i, -1)); }
1469  for (; j < TotalDstNodes; j++) { Nodes.Add(TIntTr(DstNodeIds[j].Val1, -1, j)); }
1471 
1473  TInt NumNodes = Nodes.Len();
1474  PGraphMP Graph = PGraphMP::TObj::New(NumNodes, NumRows);
1475 // NumThreads = omp_get_max_threads();
1476 // int Delta = (NumNodes+NumThreads-1)/NumThreads;
1477 
1478  TVec<TIntV> InVV(NumNodes);
1479  TVec<TIntV> OutVV(NumNodes);
1480 
1481 // omp_set_num_threads(NumThreads);
1482  #pragma omp parallel for schedule(static,100)
1483  for (int m = 0; m < NumNodes; m++) {
1484  //double startTr = omp_get_wtime();
1485  //TIntV OutV, InV;
1486  TInt n, i, j;
1487  Nodes[m].GetVal(n, i, j);
1488  if (i >= 0) {
1489  TInt Offset = SrcNodeIds[i].GetVal2();
1490  TInt Sz = EdgeCol3.Len()-Offset;
1491  if (i < SrcNodeIds.Len()-1) { Sz = SrcNodeIds[i+1].GetVal2()-Offset; }
1492  OutVV[m].Reserve(Sz);
1493  OutVV[m].CopyUniqueFrom(EdgeCol3, Offset, Sz);
1494  }
1495  if (j >= 0) {
1496  TInt Offset = DstNodeIds[j].GetVal2();
1497  TInt Sz = EdgeCol4.Len()-Offset;
1498  if (j < DstNodeIds.Len()-1) { Sz = DstNodeIds[j+1].GetVal2()-Offset; }
1499  InVV[m].Reserve(Sz);
1500  InVV[m].CopyUniqueFrom(EdgeCol4, Offset, Sz);
1501  }
1502  Graph->AddNodeWithEdges(n, InVV[m], OutVV[m]);
1503  }
1504  Graph->SetNodes(NumNodes);
1506 
1508  omp_set_num_threads(omp_get_max_threads());
1509  if (NodeType == atInt) {
1510  #pragma omp parallel for schedule(static)
1511  for (int i = 0; i < Partitions.Len(); i++) {
1512  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1513  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1514  while (RowI < EndI) {
1515  TInt RowId = RowI.GetRowIdx(); // EdgeId
1516  TInt SrcId = RowI.GetIntAttr(SrcColIdx);
1517  TInt DstId = RowI.GetIntAttr(DstColIdx);
1518  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
1519  RowI++;
1520  }
1521  }
1522  }
1523  else if (NodeType == atStr) {
1524  #pragma omp parallel for schedule(static)
1525  for (int i = 0; i < Partitions.Len(); i++) {
1526  TRowIterator RowI(Partitions[i].GetVal1(), Table());
1527  TRowIterator EndI(Partitions[i].GetVal2(), Table());
1528  while (RowI < EndI) {
1529  TInt RowId = RowI.GetRowIdx(); // EdgeId
1530  TInt SrcId = RowI.GetStrMapById(SrcColIdx);
1531  TInt DstId = RowI.GetStrMapById(DstColIdx);
1532  Graph->AddEdgeUnchecked(RowId, SrcId, DstId);
1533  RowI++;
1534  }
1535  }
1536  }
1537  Graph->SetEdges(NumRows);
1539 
1540  // double endAdd = omp_get_wtime();
1541  // printf("Add time = %f\n", endAdd-endAlloc);
1542 
1543  return Graph;
1544 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
int Val
Definition: dt.h:1139
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Start(const TExperiment Exp)
Start a new experiment.
Definition: util.cpp:733
Definition: gbase.h:23
void Stop(const TExperiment Exp)
Stop the current experiment.
Definition: util.cpp:737
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
#define Assert(Cond)
Definition: bd.h:251
static void QSortKeyVal(TIntV &Key, TIntV &Val, TInt Start, TInt End)
Definition: table.cpp:5378
Definition: dt.h:1137
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:171
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
static TStopwatch * GetInstance()
Definition: util.h:82
Definition: gbase.h:23
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Routines to benchmark table operations.
Definition: util.h:71
template<class PGraphMP >
PGraphMP TSnap::ToNetworkMP2 ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TAttrAggr  AggrPolicy 
)

Calls ToNetworkMP2 with an empty attribute vector. Convenience wrapper.

Definition at line 1547 of file conv.h.

1549 {
1550  TStrV V;
1551  return ToNetworkMP2<PGraphMP>(Table, SrcCol, DstCol, V, V, V, AggrPolicy);
1552 }
void TSnap::WriteEdgeSchemaToFile ( FILE *  F,
TStrV IntAttrENames,
TStrV FltAttrENames,
TStrV StrAttrENames 
)

Definition at line 221 of file gio.cpp.

221  {
222  fprintf(F, "%s\t%s\t%s", EDGES_START.CStr(), SRC_ID_NAME.CStr(), DST_ID_NAME.CStr());
223  for(int i = 0; i < IntAttrENames.Len(); i++) {
224  fprintf(F, "\t%s:%s", INT_TYPE_PREFIX.CStr(), IntAttrENames[i].CStr());
225  }
226  for(int i = 0; i < FltAttrENames.Len(); i++) {
227  fprintf(F, "\t%s:%s", FLT_TYPE_PREFIX.CStr(), FltAttrENames[i].CStr());
228  }
229  for(int i = 0; i < StrAttrENames.Len(); i++) {
230  fprintf(F, "\t%s:%s", STR_TYPE_PREFIX.CStr(), StrAttrENames[i].CStr());
231  }
232  fprintf(F, "\n");
233 }
const TStr EDGES_START
Definition: gio.h:6
const TStr STR_TYPE_PREFIX
Definition: gio.h:14
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TStr DST_ID_NAME
Definition: gio.h:10
const TStr FLT_TYPE_PREFIX
Definition: gio.h:13
const TStr INT_TYPE_PREFIX
Definition: gio.h:12
char * CStr()
Definition: dt.h:479
const TStr SRC_ID_NAME
Definition: gio.h:9
void TSnap::WriteEdgesToFile ( FILE *  F,
const PNEANet Graph,
TStrV IntAttrENames,
TStrV FltAttrENames,
TStrV StrAttrENames 
)

Definition at line 238 of file gio.cpp.

238  {
239  for (TNEANet::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
240  fprintf(F, "%d\t%d", EI.GetSrcNId(), EI.GetDstNId());
241  for(int i = 0; i < IntAttrENames.Len(); i++) {
242  if (Graph->IsIntAttrDeletedE(EI.GetId(), IntAttrENames[i])) {
243  fprintf(F, "\t%s", NULL_VAL.CStr());
244  continue;
245  }
246  int AttrIntVal = Graph->GetIntAttrDatE(EI.GetId(), IntAttrENames[i]);
247  fprintf(F, "\t%d", AttrIntVal);
248  }
249  for(int i = 0; i < FltAttrENames.Len(); i++) {
250  if (Graph->IsFltAttrDeletedE(EI.GetId(), FltAttrENames[i])) {
251  fprintf(F, "\t%s", NULL_VAL.CStr());
252  continue;
253  }
254  double AttrFltVal = Graph->GetFltAttrDatE(EI.GetId(), FltAttrENames[i]);
255  fprintf(F, "\t%f", AttrFltVal);
256  }
257  for(int i = 0; i < StrAttrENames.Len(); i++) {
258  if (Graph->IsStrAttrDeletedE(EI.GetId(), StrAttrENames[i])) {
259  fprintf(F, "\t%s", NULL_VAL.CStr());
260  continue;
261  }
262  char * AttrStrVal = Graph->GetStrAttrDatE(EI.GetId(), StrAttrENames[i]).CStr();
263  fprintf(F, "\t%s", AttrStrVal);
264  }
265  fprintf(F, "\n");
266  }
267 }
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
const TStr NULL_VAL
Definition: gio.h:15
char * CStr()
Definition: dt.h:479
void TSnap::WriteNodeSchemaToFile ( FILE *  F,
TStrV IntAttrNNames,
TStrV FltAttrNNames,
TStrV StrAttrNNames 
)

Definition at line 171 of file gio.cpp.

171  {
172  fprintf(F, "%s\t%s", NODES_START.CStr(), NID_NAME.CStr());
173  for(int i = 0; i < IntAttrNNames.Len(); i++) {
174  fprintf(F, "\t%s:%s", INT_TYPE_PREFIX.CStr(), IntAttrNNames[i].CStr());
175  }
176  for(int i = 0; i < FltAttrNNames.Len(); i++) {
177  fprintf(F, "\t%s:%s", FLT_TYPE_PREFIX.CStr(), FltAttrNNames[i].CStr());
178  }
179  for(int i = 0; i < StrAttrNNames.Len(); i++) {
180  fprintf(F, "\t%s:%s", STR_TYPE_PREFIX.CStr(), StrAttrNNames[i].CStr());
181  }
182  fprintf(F, "\n");
183 }
const TStr STR_TYPE_PREFIX
Definition: gio.h:14
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TStr NID_NAME
Definition: gio.h:11
const TStr FLT_TYPE_PREFIX
Definition: gio.h:13
const TStr NODES_START
Definition: gio.h:7
const TStr INT_TYPE_PREFIX
Definition: gio.h:12
char * CStr()
Definition: dt.h:479
void TSnap::WriteNodesToFile ( FILE *  F,
const PNEANet Graph,
TStrV IntAttrNNames,
TStrV FltAttrNNames,
TStrV StrAttrNNames 
)

Definition at line 188 of file gio.cpp.

188  {
189  for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
190  fprintf(F, "%d", NI.GetId());
191  for(int i = 0; i < IntAttrNNames.Len(); i++) {
192  if (Graph->IsIntAttrDeletedN(NI.GetId(), IntAttrNNames[i])) {
193  fprintf(F, "\t%s", NULL_VAL.CStr());
194  continue;
195  }
196  int AttrIntVal = Graph->GetIntAttrDatN(NI.GetId(), IntAttrNNames[i]);
197  fprintf(F, "\t%d", AttrIntVal);
198  }
199  for(int i = 0; i < FltAttrNNames.Len(); i++) {
200  if (Graph->IsFltAttrDeletedN(NI.GetId(), FltAttrNNames[i])) {
201  fprintf(F, "\t%s", NULL_VAL.CStr());
202  continue;
203  }
204  double AttrFltVal = Graph->GetFltAttrDatN(NI.GetId(), FltAttrNNames[i]);
205  fprintf(F, "\t%f", AttrFltVal);
206  }
207  for(int i = 0; i < StrAttrNNames.Len(); i++) {
208  if (Graph->IsStrAttrDeletedN(NI.GetId(), StrAttrNNames[i])) {
209  fprintf(F, "\t%s", NULL_VAL.CStr());
210  continue;
211  }
212  char * AttrStrVal = Graph->GetStrAttrDatN(NI.GetId(), StrAttrNNames[i]).CStr();
213  fprintf(F, "\t%s", AttrStrVal);
214  }
215  fprintf(F, "\n");
216  }
217 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
const TStr NULL_VAL
Definition: gio.h:15
char * CStr()
Definition: dt.h:479

Variable Documentation

const TStr TSnap::CapAttrName = "capacity"

Definition at line 4 of file flow.h.

const TStr TSnap::DST_ID_NAME = ("DstNId")

Definition at line 10 of file gio.h.

const TStr TSnap::EDGES_START = ("#EDGES")

Definition at line 6 of file gio.h.

const TStr TSnap::END_SENTINEL = ("#END")

Definition at line 8 of file gio.h.

const TStr TSnap::FLT_TYPE_PREFIX = ("Flt")

Definition at line 13 of file gio.h.

const TStr TSnap::INT_TYPE_PREFIX = ("Int")

Definition at line 12 of file gio.h.

const TStr TSnap::NID_NAME = ("NId")

Definition at line 11 of file gio.h.

const TStr TSnap::NODES_START = ("#NODES")

Definition at line 7 of file gio.h.

const TStr TSnap::NULL_VAL = ("__null__")

Definition at line 15 of file gio.h.

const TStr TSnap::SRC_ID_NAME = ("SrcNId")

Definition at line 9 of file gio.h.

const TStr TSnap::STR_TYPE_PREFIX = ("Str")

Definition at line 14 of file gio.h.