SNAP Library 4.0, Developer Reference  2017-07-27 13:18:06
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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). GetBfsEffDiam3. 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. GetBfsEffDiam4. 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, TVec< TFltV > &LeftSV, TVec< TFltV > &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, TVec< TFltV > &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 &ArndEdges)
 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 &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. More...
 
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 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...
 
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 &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 >
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 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

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

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

Definition at line 369 of file alg.h.

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

369  {
370  TIntV EdgeV;
371  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
372  const int NId = NI.GetId();
373  if (! Graph->IsEdge(NId, NId)) {
374  EdgeV.Add(NId);
375  }
376  }
377  for (int i = 0; i < EdgeV.Len(); i++) {
378  Graph->AddEdge(EdgeV[i], EdgeV[i]);
379  }
380 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

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

Definition at line 157 of file centr.cpp.

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

Definition at line 138 of file centr.cpp.

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.

References THash< TKey, TDat, THashFunc >::GetDat().

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

Here is the call graph for this function:

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

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

Definition at line 135 of file casc.cpp.

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

135  {
136  // Attribute to Int mapping
137  TInt SIdx = P->GetColIdx(C1);
138  TInt DIdx = P->GetColIdx(C2);
139  TInt StIdx = P->GetColIdx(C3);
140  TInt DuIdx = P->GetColIdx(C4);
141  TIntV MapV, PhyV;
142  TStrV SortBy;
143  SortBy.Add(C3);
144  P->Order(SortBy);
145  int count = 0;
146  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
147  MapV.Add(RI.GetRowIdx());
148  PhyV.Add(count++);
149  }
150  // After sort attach with each row a rank helpful for sorting
151  P->StoreIntCol("Physical",PhyV);
152  TInt PIdx = P->GetColIdx("Physical");
153  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
154  // Check for top cascades
155  if (NI.GetInDeg() != 0) { continue;}
156  TIntV CurCasc;
157  TSnapQueue<TInt> EventQ;
158  THashSet<TInt> VisitedH;
159  TInt NId = NI.GetId();
160  EventQ.Push(NId);
161  VisitedH.AddKey(NId);
162  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,NId));
163  while (! EventQ.Empty()) {
164  TNGraph::TNodeI CNI = Graph->GetNI(EventQ.Top().Val); //Get Current Node
165  EventQ.Pop();
166  // Go over the outdegree nodes of the currernt node
167  for (int e = 0; e < CNI.GetOutDeg(); e++) {
168  TInt CId = CNI.GetOutNId(e);
169  if ( !VisitedH.IsKey(CId)) {
170  EventQ.Push(CId);
171  VisitedH.AddKey(CId);
172  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,CId));
173  }
174  }
175  }
176  CurCasc.Sort();
177  TIntV ToAddV;
178  if (Print && VisitedH.Len() > 1) {
179  printf("__casacade__\t%d\n",VisitedH.Len());
180  }
181  for (TIntV::TIter VI = CurCasc.BegI(); VI < CurCasc.EndI(); VI++) {
182  ToAddV.Add(MapV.GetVal(VI->Val));
183  if (Print && VisitedH.Len() > 1) {
184  int PIdx = MapV.GetVal(VI->Val).Val;
185  int PSource = P->GetIntValAtRowIdx(SIdx,PIdx).Val;
186  int PDest = P->GetIntValAtRowIdx(DIdx,PIdx).Val;
187  int PStart = P->GetIntValAtRowIdx(StIdx,PIdx).Val;
188  int PDur = P->GetIntValAtRowIdx(DuIdx,PIdx).Val;
189  printf("%d\t%d\t%d\t%d\t%d\n",PIdx,PSource,PDest,PStart,PDur);
190  }
191  }
192  if (ToAddV.Len() > 1) {
193  TopCascVV.Add(ToAddV);
194  }
195  }
196  return;
197 }
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:595
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
int Val
Definition: dt.h:1136
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
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:198
bool Empty() const
Tests whether the queue is empty (contains no elements).
Definition: gbase.h:186
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:1134
int Len() const
Definition: shash.h:1121
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:402
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:201
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
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:196
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:412

Here is the call graph for this function:

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

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

Definition at line 200 of file casc.cpp.

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

200  {
201  // Attribute to Int mapping
202  TInt SIdx = P->GetColIdx(C1);
203  TInt DIdx = P->GetColIdx(C2);
204  TInt StIdx = P->GetColIdx(C3);
205  TInt DuIdx = P->GetColIdx(C4);
206  TIntV MapV, PhyV;
207  TStrV SortBy;
208  SortBy.Add(C3);
209  P->Order(SortBy);
210  int count = 0;
211  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
212  MapV.Add(RI.GetRowIdx());
213  PhyV.Add(count++);
214  }
215  P->StoreIntCol("Physical",PhyV);
216  TInt PIdx = P->GetColIdx("Physical");
217  TIntV GNodeV;
218  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
219  if (NI.GetInDeg() == 0) { GNodeV.Add(NI.GetId()); }
220  }
221  TVec<TIntV> ThTopCascVV; // for each thread
222  #pragma omp parallel private(ThTopCascVV) num_threads(10)
223  {
224  #pragma omp for schedule(dynamic,1000)
225  for (int i = 0; i < GNodeV.Len(); i++) {
226  TIntV CurCasc;
227  TSnapQueue<TInt> EventQ;
228  THashSet<TInt> VisitedH;
229  TInt NId = GNodeV[i];
230  EventQ.Push(NId);
231  VisitedH.AddKey(NId);
232  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,NId));
233  while (! EventQ.Empty()) {
234  TNGraph::TNodeI CNI = Graph->GetNI(EventQ.Top().Val); //Get Current Node
235  EventQ.Pop();
236  // Go over the outdegree nodes of the currernt node
237  for (int e = 0; e < CNI.GetOutDeg(); e++) {
238  TInt CId = CNI.GetOutNId(e);
239  if ( !VisitedH.IsKey(CId)) {
240  EventQ.Push(CId);
241  VisitedH.AddKey(CId);
242  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,CId));
243  }
244  }
245  }
246  CurCasc.Sort();
247  TIntV ToAddV;
248  for (TIntV::TIter VI = CurCasc.BegI(); VI < CurCasc.EndI(); VI++) {
249  ToAddV.Add(MapV.GetVal(VI->Val));
250  }
251  if (ToAddV.Len() > 1) { ThTopCascVV.Add(ToAddV);}
252  }
253  #pragma omp critical
254  {
255  for (int j = 0; j < ThTopCascVV.Len(); j++) {
256  TopCascVV.Add(ThTopCascVV[j]);
257  }
258  }
259  }
260  return;
261 }
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:595
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
int Val
Definition: dt.h:1136
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
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:198
bool Empty() const
Tests whether the queue is empty (contains no elements).
Definition: gbase.h:186
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:1134
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:402
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:201
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
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:196
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:412

Here is the call graph for this function:

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

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

Definition at line 126 of file casc.cpp.

References CascGraphSource(), and CascGraphTime().

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

Here is the call graph for this function:

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

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

Definition at line 3 of file casc.cpp.

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

Referenced by CascGraph().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Definition at line 59 of file casc.cpp.

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

Referenced by CascGraph().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 488 of file cmty.cpp.

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

Referenced by CmtyEvolutionFileBatchV().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 439 of file cmty.cpp.

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

Referenced by CmtyTest().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 723 of file cmty.cpp.

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

Referenced by CmtyTest().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 825 of file cmty.cpp.

References CmtyEvolutionFileBatchV(), and CmtyEvolutionJson().

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

Here is the call graph for this function:

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

Returns the number of nodes with degree NodeDeg.

Definition at line 105 of file alg.h.

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

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

Here is the caller graph for this function:

template<class PGraph >
int TSnap::CntEdgesToSet ( const PGraph &  Graph,
const int &  NId,
const TIntSet NodeSet 
)

Returns the number of nodes in NodeSet that have an edge to the node NId.

Definition at line 123 of file alg.h.

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

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

Here is the call graph for this function:

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

Returns the number of nodes with in-degree NodeInDeg.

Definition at line 87 of file alg.h.

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

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

Here is the caller graph for this function:

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

Returns the number of nodes with degree greater than 0.

Definition at line 114 of file alg.h.

Referenced by TTimeNet::PlotMedianDegOverTm().

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

Here is the caller graph for this function:

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

Returns the number of nodes with out-degree NodeOutDeg.

Definition at line 96 of file alg.h.

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

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

Here is the caller graph for this function:

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

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

Definition at line 334 of file alg.h.

334  {
335  int Cnt = 0;
336  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
337  for (int e = 0; e < NI.GetOutDeg(); e++) {
338  if (NI.GetId() == NI.GetOutNId(e)) { Cnt++; }
339  }
340  }
341  return Cnt;
342 }
template<class PGraph >
int TSnap::CntUniqBiDirEdges ( const PGraph &  Graph)

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

Definition at line 316 of file alg.h.

References CntUniqUndirEdges(), and gfDirected.

Referenced by TGStat::TakeBasicStat().

316  {
317  if (! Graph->HasFlag(gfDirected)) { // graph is undirected
318  return CntUniqUndirEdges(Graph); // then every edge is bi-directional
319  }
320  TIntSet NbrSet;
321  int Cnt = 0;
322  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
323  const int SrcId = NI.GetId();
324  for (int e = 0; e < NI.GetOutDeg(); e++) {
325  const int DstId = NI.GetOutNId(e);
326  if (DstId <= SrcId) { continue; } // count each un-dir edge only once
327  if (Graph->IsEdge(DstId, SrcId)) { Cnt++; }
328  }
329  }
330  return Cnt;
331 }
int CntUniqUndirEdges(const PGraph &Graph)
Counts unique undirected edges in the graph Graph. Nodes (u,v) (where u!=v) are connected via an undi...
Definition: alg.h:279
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Definition at line 301 of file alg.h.

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

Referenced by TGStat::TakeBasicStat().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Definition at line 279 of file alg.h.

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

Referenced by CntUniqBiDirEdges().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Definition at line 1447 of file cmty.cpp.

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

1447  {
1448  return TSnapDetail::TCNMQMatrix::CmtyCMN(Graph, CmtyV);
1449 }

Here is the call graph for this function:

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

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

Definition at line 312 of file cmty.cpp.

References TSnap::TSnapDetail::_GirvanNewmanGetModularity(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Clr(), TSnap::TSnapDetail::CmtyGirvanNewmanStep(), TVec< TVal, TSizeTy >::Len(), and TVec< TVal, TSizeTy >::Swap().

312  {
313  TIntH OutDegH;
314  const int NEdges = Graph->GetEdges();
315  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
316  OutDegH.AddDat(NI.GetId(), NI.GetOutDeg());
317  }
318  double BestQ = -1; // modularity
319  TCnComV CurCmtyV;
320  CmtyV.Clr();
321  TIntV Cmty1, Cmty2;
322  while (true) {
323  TSnapDetail::CmtyGirvanNewmanStep(Graph, Cmty1, Cmty2);
324  const double Q = TSnapDetail::_GirvanNewmanGetModularity(Graph, OutDegH, NEdges, CurCmtyV);
325  //printf("current modularity: %f\n", Q);
326  if (Q > BestQ) {
327  BestQ = Q;
328  CmtyV.Swap(CurCmtyV);
329  }
330  if (Cmty1.Len() == 0 || Cmty2.Len() == 0) { break; }
331  }
332  return BestQ;
333 }
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
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
double _GirvanNewmanGetModularity(const PUNGraph &G, const TIntH &OutDegH, const int &OrigEdges, TCnComV &CnComV)
Definition: cmty.cpp:37
void CmtyGirvanNewmanStep(PUNGraph &Graph, TIntV &Cmty1, TIntV &Cmty2)
A single step of Girvan-Newman clustering procedure.
Definition: cmty.cpp:15

Here is the call graph for this function:

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

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

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

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

Definition at line 403 of file subgraph.h.

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

403  {
404  CAssert(HasGraphFlag(typename PInGraph::TObj, gfMultiGraph)); // needs to have explicit edges
405  POutGraph NewGraphPt = POutGraph::TObj::New();
406  typename POutGraph::TObj& NewGraph = *NewGraphPt;
407  NewGraph.Reserve(-1, EIdV.Len());
408  if (! RenumberNodes) {
409  for (int edge = 0; edge < EIdV.Len(); edge++) {
410  const int EId = EIdV[edge];
411  IAssert(InGraph->IsEdge(EId));
412  const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
413  const int SrcNId = EI.GetSrcNId();
414  const int DstNId = EI.GetDstNId();
415  if (! NewGraph.IsNode(SrcNId)) {
416  NewGraph.AddNode(SrcNId); }
417  if (! NewGraph.IsNode(DstNId)) {
418  NewGraph.AddNode(DstNId); }
419  NewGraph.AddEdge(SrcNId, DstNId);
420  }
421  } else {
422  // renumber nodes so that node ids are 0...N-1
423  TIntSet NIdSet(InGraph->GetNodes());
424  for (int edge = 0; edge < EIdV.Len(); edge++) {
425  const int EId = EIdV[edge];
426  IAssert(InGraph->IsEdge(EId));
427  const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
428  const int SrcNId = NIdSet.AddKey(EI.GetSrcNId()); // map node ids
429  const int DstNId = NIdSet.AddKey(EI.GetDstNId());
430  if (! NewGraph.IsNode(SrcNId)) {
431  NewGraph.AddNode(SrcNId); }
432  if (! NewGraph.IsNode(DstNId)) {
433  NewGraph.AddNode(DstNId); }
434  NewGraph.AddEdge(SrcNId, DstNId);
435  }
436  }
437  return NewGraphPt;
438 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
Definition: gbase.h:14
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
#define CAssert(Cond)
Definition: bd.h:302

Here is the call graph for this function:

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

Performs conversion of graph InGraph with an optional node renumbering.

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

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

Definition at line 288 of file subgraph.h.

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

288  {
289  POutGraph OutGraphPt = POutGraph::TObj::New();
290  typename POutGraph::TObj& OutGraph = *OutGraphPt;
291  OutGraph.Reserve(InGraph->GetNodes(), InGraph->GetEdges());
292  if (! RenumberNodes) {
293  for (typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
294  OutGraph.AddNode(NI.GetId());
295  }
296  for (typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
297  OutGraph.AddEdge(EI.GetSrcNId(), EI.GetDstNId());
298  if (! HasGraphFlag(typename PInGraph::TObj, gfDirected) && HasGraphFlag(typename POutGraph::TObj, gfDirected)) { // add edge in the other direction
299  OutGraph.AddEdge(EI.GetDstNId(), EI.GetSrcNId()); }
300  }
301  } else { // renumber nodes so that node ids are 0...N-1
302  TIntSet NIdSet(InGraph->GetNodes());
303  for (typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
304  const int nid = NIdSet.AddKey(NI.GetId());
305  OutGraph.AddNode(nid);
306  }
307  for (typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
308  const int SrcNId = NIdSet.GetKeyId(EI.GetSrcNId());
309  const int DstNId = NIdSet.GetKeyId(EI.GetDstNId());
310  OutGraph.AddEdge(SrcNId, DstNId);
311  if (! HasGraphFlag(typename PInGraph::TObj, gfDirected) && HasGraphFlag(typename POutGraph::TObj, gfDirected)) {
312  OutGraph.AddEdge(DstNId, SrcNId); }
313  }
314  }
315  //OutGraph.Defrag();
316  return OutGraphPt;
317 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13

Here is the call graph for this function:

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

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

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

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

Definition at line 398 of file subgraph.h.

398  {
399  return TSnapDetail::TConvertSubGraph<POutGraph, PInGraph, HasGraphFlag(typename PInGraph::TObj, gfMultiGraph)>::Do(InGraph, NIdV, RenumberNodes);
400 }
template<class PGraph >
void TSnap::DelDegKNodes ( PGraph &  Graph,
const int &  OutDegK,
const int &  InDegK 
)

Removes all the node of out-degree OutDegK and all the nodes of in-degree InDegK from the graph.

Definition at line 445 of file alg.h.

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

445  {
446  TIntV DelNIdV;
447  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
448  if (NI.GetOutDeg() == OutDegK || NI.GetInDeg() == InDegK) {
449  DelNIdV.Add(NI.GetId());
450  }
451  }
452  for (int i = 0; i < DelNIdV.Len(); i++) {
453  Graph->DelNode(DelNIdV[i]);
454  }
455 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

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

Removes nodes with ids stored in NIdV from the graph.

Definition at line 425 of file alg.h.

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

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

Here is the call graph for this function:

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

Removes all the self-edges from the graph.

Definition at line 419 of file alg.h.

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

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

Here is the caller graph for this function:

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

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

Definition at line 432 of file alg.h.

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Useful for drawing small (<100 node) graphs.

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

Definition at line 34 of file gviz.h.

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

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

34  {
35  const TStr Ext = PltFNm.GetFExt();
36  const TStr GraphFNm = PltFNm.GetSubStr(0, PltFNm.Len()-Ext.Len()) + "dot";
37  SaveGViz(Graph, GraphFNm, Desc, NodeLabels, NIdColorH);
38  TSnap::TSnapDetail::GVizDoLayout(GraphFNm, PltFNm, Layout);
39 }
int Len() const
Definition: dt.h:487
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:386
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
TStr GetFExt() const
Definition: dt.cpp:1421
void GVizDoLayout(const TStr &GraphInFNm, TStr OutFNm, const TGVizLayout &Layout)
Runs GraphViz layout engine over a graph saved in the file GraphInFNm with output saved to OutFNm...
Definition: gviz.cpp:5
Definition: dt.h:412

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Useful for drawing small (<100 node) graphs.

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

Definition at line 42 of file gviz.h.

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

42  {
43  const TStr Ext = PltFNm.GetFExt();
44  const TStr GraphFNm = PltFNm.GetSubStr(0, PltFNm.Len()-Ext.Len()) + "dot";
45  SaveGViz(Graph, GraphFNm, Desc, NodeLabelH);
46  TSnap::TSnapDetail::GVizDoLayout(GraphFNm, PltFNm, Layout);
47 }
int Len() const
Definition: dt.h:487
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:386
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
TStr GetFExt() const
Definition: dt.cpp:1421
void GVizDoLayout(const TStr &GraphInFNm, TStr OutFNm, const TGVizLayout &Layout)
Runs GraphViz layout engine over a graph saved in the file GraphInFNm with output saved to OutFNm...
Definition: gviz.cpp:5
Definition: dt.h:412

Here is the call graph for this function:

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

Event importance.

Definition at line 527 of file centr.cpp.

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

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 }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
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
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

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

Definition at line 556 of file centr.cpp.

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

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 }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
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
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

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

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

Definition at line 12 of file coreper.cpp.

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

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

Here is the call graph for this function:

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

Definition at line 56 of file coreper.cpp.

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

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

Here is the call graph for this function:

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

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

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

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

Definition at line 71 of file flow.cpp.

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

Referenced by GetMaxFlowIntEK().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 685 of file centr.cpp.

References TVec< TVal, TSizeTy >::Del(), TVec< TVal, TSizeTy >::GetVal(), TVec< TVal, TSizeTy >::Len(), and TInt::Mx.

Referenced by GetWeightedShortestPath().

685  {
686  TFlt minimum = TInt::Mx;
687  int min_index = 0;
688  for (int i = 0; i < Frontier.Len(); i++) {
689  int NId = Frontier.GetVal(i);
690  if (NIdDistH[NId] < minimum) {
691  minimum = NIdDistH[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:1139
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: dt.h:1383
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649

Here is the call graph for this function:

Here is the caller graph for this function:

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

Generates a Ravasz-Barabasi deterministic scale-free graph.

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

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

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

Definition at line 174 of file ggen.h.

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

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

Here is the call graph for this function:

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

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

Definition at line 104 of file ggen.h.

References gfDirected.

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

Generates a random undirect graph with a given degree sequence.

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

Definition at line 119 of file ggen.cpp.

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

PUNGraph TSnap::GenConfModel ( const PUNGraph G)

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

Definition at line 335 of file ggen.cpp.

References GenConfModel(), and GetDegSeqV().

335  {
336  TIntV DegSeqV(G->GetNodes(), 0);
337  TSnap::GetDegSeqV(G, DegSeqV);
338  return TSnap::GenConfModel(DegSeqV);
339 }
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:119

Here is the call graph for this function:

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

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

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

Definition at line 453 of file ggen.cpp.

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

453  {
454  PNGraph GraphPt = TNGraph::New();
455  TNGraph& Graph = *GraphPt;
456  Graph.Reserve(Nodes, Nodes);
457  const int startNId = Graph.AddNode();
458  Graph.AddEdge(startNId, startNId);
459  for (int n = 1; n < Nodes; n++) {
460  const int rnd = Graph.GetRndNId();
461  const int NId = Graph.AddNode();
462  if (Rnd.GetUniDev() < Beta) {
463  Graph.AddEdge(NId, rnd); }
464  else {
465  const TNGraph::TNodeI NI = Graph.GetNI(rnd);
466  const int rnd2 = Rnd.GetUniDevInt(NI.GetOutDeg());
467  Graph.AddEdge(NId, NI.GetOutNId(rnd2));
468  }
469  }
470  return GraphPt;
471 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:477
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
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:595
Directed graph.
Definition: graph.h:342
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:402
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
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:606
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:412

Here is the call graph for this function:

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

Generates a random graph with exact degree sequence.

Generates a random graph with exact degree sequence DegSeqV. 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 58 of file ggen.cpp.

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

Referenced by GenRndDegK(), and GenRndPowerLaw().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Definition at line 442 of file ggen.cpp.

References TForestFire::GenGraph().

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

Here is the call graph for this function:

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

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

Definition at line 119 of file ggen.h.

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

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

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

Definition at line 361 of file ggen.cpp.

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

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

Here is the call graph for this function:

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

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

Definition at line 65 of file ggen.h.

References gfDirected.

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

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

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

Definition at line 310 of file ggen.cpp.

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

310  {
311  PUNGraph GraphPt = PUNGraph::New();
312  TUNGraph& Graph = *GraphPt;
313  Graph.Reserve(Nodes, NodeOutDeg*Nodes);
314  TIntV NIdV(NodeOutDeg*Nodes, 0);
315  // first edge
316  Graph.AddNode(0); Graph.AddNode(1);
317  NIdV.Add(0); NIdV.Add(1);
318  Graph.AddEdge(0, 1);
319  TIntSet NodeSet;
320  for (int node = 2; node < Nodes; node++) {
321  NodeSet.Clr(false);
322  while (NodeSet.Len() < NodeOutDeg && NodeSet.Len() < node) {
323  NodeSet.AddKey(NIdV[TInt::Rnd.GetUniDevInt(NIdV.Len())]);
324  }
325  const int N = Graph.AddNode();
326  for (int i = 0; i < NodeSet.Len(); i++) {
327  Graph.AddEdge(N, NodeSet[i]);
328  NIdV.Add(N);
329  NIdV.Add(NodeSet[i]);
330  }
331  }
332  return GraphPt;
333 }
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
static TRnd Rnd
Definition: dt.h:1143
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:298
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

Here is the call graph for this function:

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

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

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

Definition at line 165 of file ggen.cpp.

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

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

Definition at line 216 of file ggen.cpp.

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

216  {
217  const int Nodes = OrigGraph->GetNodes();
218  const int Edges = OrigGraph->GetEdges();
219  PNGraph GraphPt = TNGraph::New();
220  TNGraph& Graph = *GraphPt;
221  Graph.Reserve(Nodes, -1);
222  TExeTm ExeTm;
223  // generate a graph that satisfies the constraints
224  printf("Randomizing edges (%d, %d)...\n", Nodes, Edges);
225  TIntPrSet EdgeSet(Edges);
226  for (TNGraph::TNodeI NI = OrigGraph->BegNI(); NI < OrigGraph->EndNI(); NI++) {
227  const int NId = NI.GetId();
228  for (int e = 0; e < NI.GetOutDeg(); e++) {
229  EdgeSet.AddKey(TIntPr(NId, NI.GetOutNId(e))); }
230  Graph.AddNode(NI);
231  }
232  // edge switching
233  uint skip=0;
234  for (uint swps = 0; swps < 2*uint(Edges)*uint(NSwitch); swps++) {
235  const int keyId1 = EdgeSet.GetRndKeyId(Rnd);
236  const int keyId2 = EdgeSet.GetRndKeyId(Rnd);
237  if (keyId1 == keyId2) { skip++; continue; }
238  const TIntPr& E1 = EdgeSet[keyId1];
239  const TIntPr& E2 = EdgeSet[keyId2];
240  TIntPr NewE1(E1.Val1, E2.Val2), NewE2(E2.Val1, E1.Val2);
241  if (NewE1.Val1!=NewE1.Val2 && NewE2.Val1!=NewE2.Val2 && NewE1.Val1!=NewE2.Val1 && NewE1.Val2!=NewE2.Val2 && ! EdgeSet.IsKey(NewE1) && ! EdgeSet.IsKey(NewE2)) {
242  EdgeSet.DelKeyId(keyId1); EdgeSet.DelKeyId(keyId2);
243  EdgeSet.AddKey(TIntPr(NewE1));
244  EdgeSet.AddKey(TIntPr(NewE2));
245  } else { skip++; }
246  if (swps % Edges == 0) {
247  printf("\r %uk/%uk: %uk skip [%s]", swps/1000u, 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
248  if (ExeTm.GetSecs() > 2*3600) { printf(" *** Time limit!\n"); break; } // time limit 2 hours
249  }
250  }
251  printf("\r total %uk switchings attempted, %uk skiped [%s]\n", 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
252  for (int e = 0; e < EdgeSet.Len(); e++) {
253  Graph.AddEdge(EdgeSet[e].Val1, EdgeSet[e].Val2); }
254  return GraphPt;
255 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
Definition: tm.h:355
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:477
unsigned int uint
Definition: bd.h:11
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
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:342
Definition: ds.h:32
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
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:606
TVal2 Val2
Definition: ds.h:35
const char * GetStr() const
Definition: tm.h:368

Here is the call graph for this function:

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

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

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

Definition at line 263 of file ggen.cpp.

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

263  {
264  const int Nodes = OrigGraph->GetNodes();
265  const int Edges = OrigGraph->GetEdges();
266  PBPGraph GraphPt = TBPGraph::New();
267  TBPGraph& Graph = *GraphPt;
268  Graph.Reserve(Nodes, -1);
269  TExeTm ExeTm;
270  // generate a graph that satisfies the constraints
271  printf("Randomizing edges (%d, %d)...\n", Nodes, Edges);
272  TIntPrSet EdgeSet(Edges);
273  for (TBPGraph::TNodeI NI = OrigGraph->BegLNI(); NI < OrigGraph->EndLNI(); NI++) {
274  const int NId = NI.GetId();
275  for (int e = 0; e < NI.GetOutDeg(); e++) {
276  EdgeSet.AddKey(TIntPr(NId, NI.GetOutNId(e))); } // edges left-->right
277  Graph.AddNode(NI.GetId(), true); } // left nodes
278  for (TBPGraph::TNodeI NI = OrigGraph->BegRNI(); NI < OrigGraph->EndRNI(); NI++) {
279  Graph.AddNode(NI.GetId(), false); } // right nodes
280  IAssert(EdgeSet.Len() == Edges);
281  // edge switching
282  uint skip=0;
283  for (uint swps = 0; swps < 2*uint(Edges)*uint(NSwitch); swps++) {
284  const int keyId1 = EdgeSet.GetRndKeyId(Rnd);
285  const int keyId2 = EdgeSet.GetRndKeyId(Rnd);
286  if (keyId1 == keyId2) { skip++; continue; }
287  const TIntPr& E1 = EdgeSet[keyId1];
288  const TIntPr& E2 = EdgeSet[keyId2];
289  TIntPr NewE1(E1.Val1, E2.Val2), NewE2(E2.Val1, E1.Val2);
290  if (NewE1!=NewE2 && NewE1.Val1!=NewE1.Val2 && NewE2.Val1!=NewE2.Val2 && ! EdgeSet.IsKey(NewE1) && ! EdgeSet.IsKey(NewE2)) {
291  EdgeSet.DelKeyId(keyId1); EdgeSet.DelKeyId(keyId2);
292  EdgeSet.AddKey(TIntPr(NewE1));
293  EdgeSet.AddKey(TIntPr(NewE2));
294  } else { skip++; }
295  if (swps % Edges == 0) {
296  printf("\r %uk/%uk: %uk skip [%s]", swps/1000u, 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
297  if (ExeTm.GetSecs() > 2*3600) { printf(" *** Time limit!\n"); break; } // time limit 2 hours
298  }
299  }
300  printf("\r total %uk switchings attempted, %uk skiped [%s]\n", 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
301  for (int e = 0; e < EdgeSet.Len(); e++) {
302  Graph.AddEdge(EdgeSet[e].Val1, EdgeSet[e].Val2); }
303  return GraphPt;
304 }
#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:960
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:928
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:1054
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
const char * GetStr() const
Definition: tm.h:368

Here is the call graph for this function:

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

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

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

Definition at line 478 of file ggen.cpp.

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

Referenced by GenRMatEpinions().

478  {
479  PNGraph GraphPt = TNGraph::New();
480  TNGraph& Graph = *GraphPt;
481  Graph.Reserve(Nodes, Edges);
482  IAssert(A+B+C < 1.0);
483  int rngX, rngY, offX, offY;
484  int Depth=0, Collisions=0, Cnt=0, PctDone=0;
485  const int EdgeGap = Edges / 100 + 1;
486  // sum of parameters (probabilities)
487  TVec<double> sumA(128, 0), sumAB(128, 0), sumAC(128, 0), sumABC(128, 0); // up to 2^128 vertices ~ 3.4e38
488  for (int i = 0; i < 128; i++) {
489  const double a = A * (Rnd.GetUniDev() + 0.5);
490  const double b = B * (Rnd.GetUniDev() + 0.5);
491  const double c = C * (Rnd.GetUniDev() + 0.5);
492  const double d = (1.0 - (A+B+C)) * (Rnd.GetUniDev() + 0.5);
493  const double abcd = a+b+c+d;
494  sumA.Add(a / abcd);
495  sumAB.Add((a+b) / abcd);
496  sumAC.Add((a+c) / abcd);
497  sumABC.Add((a+b+c) / abcd);
498  }
499  // nodes
500  for (int node = 0; node < Nodes; node++) {
501  IAssert(Graph.AddNode(-1) == node);
502  }
503  // edges
504  for (int edge = 0; edge < Edges; ) {
505  rngX = Nodes; rngY = Nodes; offX = 0; offY = 0;
506  Depth = 0;
507  // recurse the matrix
508  while (rngX > 1 || rngY > 1) {
509  const double RndProb = Rnd.GetUniDev();
510  if (rngX>1 && rngY>1) {
511  if (RndProb < sumA[Depth]) { rngX/=2; rngY/=2; }
512  else if (RndProb < sumAB[Depth]) { offX+=rngX/2; rngX-=rngX/2; rngY/=2; }
513  else if (RndProb < sumABC[Depth]) { offY+=rngY/2; rngX/=2; rngY-=rngY/2; }
514  else { offX+=rngX/2; offY+=rngY/2; rngX-=rngX/2; rngY-=rngY/2; }
515  } else
516  if (rngX>1) { // row vector
517  if (RndProb < sumAC[Depth]) { rngX/=2; rngY/=2; }
518  else { offX+=rngX/2; rngX-=rngX/2; rngY/=2; }
519  } else
520  if (rngY>1) { // column vector
521  if (RndProb < sumAB[Depth]) { rngX/=2; rngY/=2; }
522  else { offY+=rngY/2; rngX/=2; rngY-=rngY/2; }
523  } else { Fail; }
524  Depth++;
525  }
526  // add edge
527  const int NId1 = offX;
528  const int NId2 = offY;
529  if (NId1 != NId2 && ! Graph.IsEdge(NId1, NId2)) {
530  Graph.AddEdge(NId1, NId2);
531  if (++Cnt > EdgeGap) {
532  Cnt=0; printf("\r %d%% edges", ++PctDone); }
533  edge++;
534  } else {
535  Collisions++; }
536  }
537  printf("\r RMat: nodes:%d, edges:%d, Iterations:%d, Collisions:%d (%.1f%%).\n", Nodes, Edges,
538  Edges+Collisions, Collisions, 100*Collisions/double(Edges+Collisions));
539  Graph.Defrag();
540  return GraphPt;
541 }
#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:477
#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:342
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:606
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Here is the call graph for this function:

Here is the caller graph for this function:

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 547 of file ggen.cpp.

References GenRMat().

547  {
548  return GenRMat(75888, 508837, 0.550, 0.228, 0.212);
549 }
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:478

Here is the call graph for this function:

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.

References TRnd::GetUniDevInt(), IAssertR, and TBPGraph::New().

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:1054
Definition: bd.h:196
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), GenDegSeq(), GenRewire(), and IAssert.

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:58
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:263
Definition: bd.h:196

Here is the call graph for this function:

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.

References edge, TStr::Fmt(), TRnd::GetUniDevInt(), IAssert, and IAssertR.

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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), GenConfModel(), GenDegSeq(), GenRewire(), TRnd::GetPowerDev(), and TMath::Round().

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  PUNGraph G = TSnap::GenDegSeq(DegSeqV, Rnd);
50  return TSnap::GenRewire(G, 10, Rnd);
51  }
52 }
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:165
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:58
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:335
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
double GetPowerDev(const double &AlphaSlope)
Definition: dt.h:47

Here is the call graph for this function:

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 412 of file ggen.cpp.

References TUNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TUNGraph::AddNode(), TUNGraph::Defrag(), edge, TStr::Fmt(), TRnd::GetUniDev(), TRnd::GetUniDevInt(), IAssert, IAssertR, THashSet< TKey, THashFunc >::IsKey(), THashSet< TKey, THashFunc >::Len(), TUNGraph::New(), and TUNGraph::Reserve().

412  {
413  THashSet<TIntPr> EdgeSet(Nodes*NodeOutDeg);
414 
415  IAssertR(Nodes > NodeOutDeg, TStr::Fmt("Insufficient nodes for out degree, %d!", NodeOutDeg));
416  for (int node = 0; node < Nodes; node++) {
417  const int src = node;
418  for (int edge = 1; edge <= NodeOutDeg; edge++) {
419  int dst = (node+edge) % Nodes; // edge to next neighbor
420  if (Rnd.GetUniDev() < RewireProb) { // random edge
421  dst = Rnd.GetUniDevInt(Nodes);
422  while (dst == src || EdgeSet.IsKey(TIntPr(src, dst))) {
423  dst = Rnd.GetUniDevInt(Nodes); }
424  }
425  EdgeSet.AddKey(TIntPr(src, dst));
426  }
427  }
428  PUNGraph GraphPt = TUNGraph::New();
429  TUNGraph& Graph = *GraphPt;
430  Graph.Reserve(Nodes, EdgeSet.Len());
431  int node;
432  for (node = 0; node < Nodes; node++) {
433  IAssert(Graph.AddNode(node) == node);
434  }
435  for (int edge = 0; edge < EdgeSet.Len(); edge++) {
436  Graph.AddEdge(EdgeSet[edge].Val1, EdgeSet[edge].Val2);
437  }
438  Graph.Defrag();
439  return GraphPt;
440 }
#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:298
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

Here is the call graph for this function:

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.

References gfDirected.

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.

References edge.

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.

References THashSet< TKey, THashFunc >::AddKey(), TVec< TVal, TSizeTy >::Clr(), TVec< TVal, TSizeTy >::Del(), TVec< TVal, TSizeTy >::Empty(), GetEdgeBridges(), GetWccs(), IAssert, TVec< TVal, TSizeTy >::Len(), and TUNGraph::New().

Referenced by TLocClustStat::BagOfWhiskers(), TLocClustStat::BagOfWhiskers2(), and TLocClust::DrawWhiskers().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THashSet< TKey, THashFunc >::AddKey(), TVec< TVal, TSizeTy >::Clr(), TVec< TVal, TSizeTy >::Del(), TVec< TVal, TSizeTy >::Empty(), GetEdgeBridges(), GetWccs(), GetWccSzCnt(), IAssert, TVec< TVal, TSizeTy >::Len(), and TUNGraph::New().

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

Here is the call graph for this function:

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.

References TGraphAnf< PGraph >::GetNodeAnf().

Referenced by PlotHops(), and TGStat::TakeDiam().

204  {
205  TGraphAnf<PGraph> Anf(Graph, NApprox, 5, 0);
206  Anf.GetNodeAnf(SrcNId, DistNbrsV, MxDist, IsDir);
207 }
Definition: anf.h:33

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TGraphAnf< PGraph >::GetGraphAnf().

210  {
211  TGraphAnf<PGraph> Anf(Graph, NApprox, 5, 0);
212  Anf.GetGraphAnf(DistNbrsV, MxDist, IsDir);
213 }
Definition: anf.h:33

Here is the call graph for this function:

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.

References TSnap::TSnapDetail::CalcEffDiam(), and TGraphAnf< PGraph >::GetGraphAnf().

Referenced by GetAnfEffDiam(), TKroneckerLL::GradDescentConvergence(), TTimeNet::PlotEffDiam(), and TTimeNENet::PlotEffDiam().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TMom::Add(), TMom::Def(), GetAnfEffDiam(), and TMom::GetMean().

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

Here is the call graph for this function:

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.

References TCnCom::GetDfsVisitor().

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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::DelLast(), TVec< TVal, TSizeTy >::Len(), TInt::Rnd, and TVec< TVal, TSizeTy >::Shuffle().

Referenced by TSnap::TSnapDetail::CmtyGirvanNewmanStep().

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:1143
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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::DelLast(), TVec< TVal, TSizeTy >::Len(), TInt::Rnd, and TVec< TVal, TSizeTy >::Shuffle().

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:1143
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::DelLast(), TVec< TVal, TSizeTy >::Len(), TInt::Rnd, and TVec< TVal, TSizeTy >::Shuffle().

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:1143
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Clr(), TSStack< TVal >::Clr(), TQQueue< TVal >::Clr(), TSStack< TVal >::Empty(), TQQueue< TVal >::Empty(), THash< TKey, TDat, THashFunc >::GetDat(), gfDirected, TVec< TVal, TSizeTy >::Len(), TMath::Mn(), TMath::Mx(), TSStack< TVal >::Pop(), TQQueue< TVal >::Pop(), TSStack< TVal >::Push(), TQQueue< TVal >::Push(), TSStack< TVal >::Top(), and TQQueue< TVal >::Top().

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

Here is the call graph for this function:

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 415 of file bfsdfs.h.

Referenced by GetBfsEffDiam(), GetBfsFullDiam(), TTimeNet::PlotMissingPast(), and PrintInfo().

415  {
416  int FullDiam;
417  double EffDiam;
418  GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam);
419  return EffDiam;
420 }
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:458

Here is the caller graph for this function:

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 423 of file bfsdfs.h.

References GetBfsEffDiam().

423  {
424  double AvgDiam;
425  EffDiam = -1; FullDiam = -1;
426  return GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam, AvgDiam);
427 }
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:458

Here is the call graph for this function:

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

Definition at line 430 of file bfsdfs.h.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TSnap::TSnapDetail::CalcEffDiamPdf(), TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::GetKey(), TVec< TVal, TSizeTy >::Last(), THash< TKey, TDat, THashFunc >::Len(), TMath::Mn(), TInt::Mx, TBreathFS< PGraph >::NIdDistH, TInt::Rnd, TVec< TVal, TSizeTy >::Shuffle(), and TVec< TVal, TSizeTy >::Sort().

430  {
431  EffDiam = -1; FullDiam = -1; AvgSPL = -1;
432  TIntFltH DistToCntH;
433  TBreathFS<PGraph> BFS(Graph);
434  // shotest paths
435  TIntV NodeIdV;
436  Graph->GetNIdV(NodeIdV); NodeIdV.Shuffle(TInt::Rnd);
437  for (int tries = 0; tries < TMath::Mn(NTestNodes, Graph->GetNodes()); tries++) {
438  const int NId = NodeIdV[tries];
439  BFS.DoBfs(NId, true, ! IsDir, -1, TInt::Mx);
440  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
441  DistToCntH.AddDat(BFS.NIdDistH[i]) += 1; }
442  }
443  TIntFltKdV DistNbrsPdfV;
444  double SumPathL=0, PathCnt=0;
445  for (int i = 0; i < DistToCntH.Len(); i++) {
446  DistNbrsPdfV.Add(TIntFltKd(DistToCntH.GetKey(i), DistToCntH[i]));
447  SumPathL += DistToCntH.GetKey(i) * DistToCntH[i];
448  PathCnt += DistToCntH[i];
449  }
450  DistNbrsPdfV.Sort();
451  EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9); // effective diameter (90-th percentile)
452  FullDiam = DistNbrsPdfV.Last().Key; // approximate full diameter (max shortest path length over the sampled nodes)
453  AvgSPL = SumPathL/PathCnt; // average shortest path length
454  return EffDiam;
455 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static const int Mx
Definition: dt.h:1139
TKeyDat< TInt, TFlt > TIntFltKd
Definition: ds.h:381
static TRnd Rnd
Definition: dt.h:1143
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

Here is the call graph for this function:

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

Definition at line 458 of file bfsdfs.h.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TSnap::TSnapDetail::CalcEffDiamPdf(), TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::IsKeyGetDat(), TVec< TVal, TSizeTy >::Last(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TMath::Mn(), TInt::Mx, TBreathFS< PGraph >::NIdDistH, TInt::Rnd, TVec< TVal, TSizeTy >::Shuffle(), and TVec< TVal, TSizeTy >::Sort().

458  {
459  EffDiam = -1;
460  FullDiam = -1;
461 
462  TIntFltH DistToCntH;
463  TBreathFS<PGraph> BFS(Graph);
464  // shotest paths
465  TIntV NodeIdV(SubGraphNIdV); NodeIdV.Shuffle(TInt::Rnd);
466  TInt Dist;
467  for (int tries = 0; tries < TMath::Mn(NTestNodes, SubGraphNIdV.Len()); tries++) {
468  const int NId = NodeIdV[tries];
469  BFS.DoBfs(NId, true, ! IsDir, -1, TInt::Mx);
470  for (int i = 0; i < SubGraphNIdV.Len(); i++) {
471  if (BFS.NIdDistH.IsKeyGetDat(SubGraphNIdV[i], Dist)) {
472  DistToCntH.AddDat(Dist) += 1;
473  }
474  }
475  }
476  TIntFltKdV DistNbrsPdfV;
477  for (int i = 0; i < DistToCntH.Len(); i++) {
478  DistNbrsPdfV.Add(TIntFltKd(DistToCntH.GetKey(i), DistToCntH[i]));
479  }
480  DistNbrsPdfV.Sort();
481  EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9); // effective diameter (90-th percentile)
482  FullDiam = DistNbrsPdfV.Last().Key; // approximate full diameter (max shortest path length over the sampled nodes)
483  return EffDiam; // average shortest path length
484 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static const int Mx
Definition: dt.h:1139
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:1143
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:1134
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

Here is the call graph for this function:

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 407 of file bfsdfs.h.

References GetBfsEffDiam().

Referenced by TGStat::TakeDiam().

407  {
408  int FullDiam;
409  double EffDiam;
410  GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam);
411  return FullDiam;
412 }
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:458

Here is the call graph for this function:

Here is the caller graph for this function:

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 323 of file bfsdfs.h.

References TNGraph::AddEdge(), TNGraph::AddNode(), TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::GetDat(), THash< TKey, TDat, THashFunc >::GetKey(), TNGraph::IsNode(), THash< TKey, TDat, THashFunc >::Len(), TInt::Mx, TNGraph::New(), TBreathFS< PGraph >::NIdDistH, and THash< TKey, TDat, THashFunc >::SortByDat().

323  {
324  TBreathFS<PGraph> BFS(Graph);
325  BFS.DoBfs(StartNId, FollowOut, FollowIn, -1, TInt::Mx);
326  PNGraph Tree = TNGraph::New();
327  BFS.NIdDistH.SortByDat();
328  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
329  const int NId = BFS.NIdDistH.GetKey(i);
330  const int Dist = BFS.NIdDistH[i];
331  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
332  if (!Tree->IsNode(NId)) {
333  Tree->AddNode(NId);
334  }
335  if (FollowOut) {
336  for (int e = 0; e < NI.GetInDeg(); e++) {
337  const int Prev = NI.GetInNId(e);
338  if (Tree->IsNode(Prev) && BFS.NIdDistH.GetDat(Prev)==Dist-1) {
339  Tree->AddEdge(Prev, NId); }
340  }
341  }
342  if (FollowIn) {
343  for (int e = 0; e < NI.GetOutDeg(); e++) {
344  const int Prev = NI.GetOutNId(e);
345  if (Tree->IsNode(Prev) && BFS.NIdDistH.GetDat(Prev)==Dist-1) {
346  Tree->AddEdge(Prev, NId); }
347  }
348  }
349  }
350  return Tree;
351 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:477
static const int Mx
Definition: dt.h:1139
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:542

Here is the call graph for this function:

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.

References TCnCom::GetDfsVisitor().

Referenced by GetBiConSzCnt(), GetEdgeBridges(), and GetMxBiCon().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), GetBiCon(), THash< TKey, TDat, THashFunc >::GetKeyDatPrV(), TVec< TVal, TSizeTy >::Len(), and TVec< TVal, TSizeTy >::Sort().

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

Here is the call graph for this function:

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 113 of file triad.h.

References TVec< TVal, TSizeTy >::Empty(), GetTriads(), IAssert, and TVec< TVal, TSizeTy >::Len().

Referenced by TTimeNet::PlotCCfOverTm(), PlotClustCf(), and TGStat::TakeClustCf().

113  {
114  TIntTrV NIdCOTriadV;
115  GetTriads(Graph, NIdCOTriadV, SampleNodes);
116  if (NIdCOTriadV.Empty()) { return 0.0; }
117  double SumCcf = 0.0;
118  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
119  const double OpenCnt = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
120  if (OpenCnt > 0) {
121  SumCcf += NIdCOTriadV[i].Val2() / OpenCnt; }
122  }
123  IAssert(SumCcf>=0);
124  return SumCcf / double(NIdCOTriadV.Len());
125 }
#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:274

Here is the call graph for this function:

Here is the caller graph for this function:

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 127 of file triad.h.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), GetTriads(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TVec< TVal, TSizeTy >::Sort(), TInt::Val, TPair< TVal1, TVal2 >::Val1, and TPair< TVal1, TVal2 >::Val2.

127  {
128  TIntTrV NIdCOTriadV;
129  GetTriads(Graph, NIdCOTriadV, SampleNodes);
130  THash<TInt, TFltPr> DegSumCnt;
131  double SumCcf = 0.0;
132  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
133  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
134  const double Ccf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
135  TFltPr& SumCnt = DegSumCnt.AddDat(Graph->GetNI(NIdCOTriadV[i].Val1).GetDeg());
136  SumCnt.Val1 += Ccf;
137  SumCnt.Val2 += 1;
138  SumCcf += Ccf;
139  }
140  // get average clustering coefficient for each degree
141  DegToCCfV.Gen(DegSumCnt.Len(), 0);
142  for (int d = 0; d < DegSumCnt.Len(); d++) {
143  DegToCCfV.Add(TFltPr(DegSumCnt.GetKey(d).Val, double(DegSumCnt[d].Val1()/DegSumCnt[d].Val2())));
144  }
145  DegToCCfV.Sort();
146  return SumCcf / double(NIdCOTriadV.Len());
147 }
int Val
Definition: dt.h:1136
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
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:274
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

Here is the call graph for this function:

template<class PGraph >
double TSnap::GetClustCf ( 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.

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 150 of file triad.h.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), GetTriads(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TVec< TVal, TSizeTy >::Sort(), TInt::Val, TPair< TVal1, TVal2 >::Val1, and TPair< TVal1, TVal2 >::Val2.

150  {
151  TIntTrV NIdCOTriadV;
152  GetTriads(Graph, NIdCOTriadV, SampleNodes);
153  THash<TInt, TFltPr> DegSumCnt;
154  double SumCcf = 0.0;
155  int64 closedTriads = 0;
156  int64 openTriads = 0;
157  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
158  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
159  const double Ccf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
160  closedTriads += NIdCOTriadV[i].Val2;
161  openTriads += NIdCOTriadV[i].Val3;
162  TFltPr& SumCnt = DegSumCnt.AddDat(Graph->GetNI(NIdCOTriadV[i].Val1).GetDeg());
163  SumCnt.Val1 += Ccf;
164  SumCnt.Val2 += 1;
165  SumCcf += Ccf;
166  }
167  // get average clustering coefficient for each degree
168  DegToCCfV.Gen(DegSumCnt.Len(), 0);
169  for (int d = 0; d < DegSumCnt.Len(); d++) {
170  DegToCCfV.Add(TFltPr(DegSumCnt.GetKey(d).Val, DegSumCnt[d].Val1()/DegSumCnt[d].Val2()));
171  }
172  //if(closedTriads/3 > (uint64) TInt::Mx) { WarnNotify(TStr::Fmt("[%s line %d] %g closed triads.\n", __FILE__, __LINE__, float(closedTriads/3)).CStr()); }
173  //if(openTriads > (uint64) TInt::Mx) { WarnNotify(TStr::Fmt("[%s line %d] %g open triads.\n", __FILE__, __LINE__, float(openTriads/3)).CStr()); }
174  ClosedTriads = closedTriads/int64(3); // each triad is counted 3 times
175  OpenTriads = openTriads;
176  DegToCCfV.Sort();
177  return SumCcf / double(NIdCOTriadV.Len());
178 }
int Val
Definition: dt.h:1136
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
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:274
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

Here is the call graph for this function:

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 659 of file triad.h.

Referenced by TTimeNENet::GetTriadEdges().

659  {
660  TIntV NbrV;
661  return GetCmnNbrs(Graph, NId1, NId2, NbrV);
662 }
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:666

Here is the caller graph for this function:

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 666 of file triad.h.

References THashSet< TKey, THashFunc >::AddKey(), TVec< TVal, TSizeTy >::Clr(), TVec< TVal, TSizeTy >::Len(), TMath::Mn(), and TVec< TVal, TSizeTy >::Reserve().

666  {
667  if (! Graph->IsNode(NId1) || ! Graph->IsNode(NId2)) { NbrV.Clr(false); return 0; }
668  typename PGraph::TObj::TNodeI NI1 = Graph->GetNI(NId1);
669  typename PGraph::TObj::TNodeI NI2 = Graph->GetNI(NId2);
670  NbrV.Clr(false);
671  NbrV.Reserve(TMath::Mn(NI1.GetDeg(), NI2.GetDeg()));
672  TIntSet NSet1(NI1.GetDeg()), NSet2(NI2.GetDeg());
673  for (int i = 0; i < NI1.GetDeg(); i++) {
674  const int nid = NI1.GetNbrNId(i);
675  if (nid!=NId1 && nid!=NId2) {
676  NSet1.AddKey(nid); }
677  }
678  for (int i = 0; i < NI2.GetDeg(); i++) {
679  const int nid = NI2.GetNbrNId(i);
680  if (NSet1.IsKey(nid)) {
681  NSet2.AddKey(nid);
682  }
683  }
684  NSet2.GetKeyV(NbrV);
685  return NbrV.Len();
686 }
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

Here is the call graph for this function:

template<>
int TSnap::GetCmnNbrs< PUNGraph > ( const PUNGraph Graph,
const int &  NId1,
const int &  NId2,
TIntV NbrV 
)
inline

Definition at line 689 of file triad.h.

References TUNGraph::TNodeI::GetDeg(), TUNGraph::TNodeI::GetNbrNId(), IAssert, and TMath::Mn().

689  {
690  if (! Graph->IsNode(NId1) || ! Graph->IsNode(NId2)) { NbrV.Clr(false); return 0; }
691  const TUNGraph::TNodeI NI1 = Graph->GetNI(NId1);
692  const TUNGraph::TNodeI NI2 = Graph->GetNI(NId2);
693  int i=0, j=0;
694  NbrV.Clr(false);
695  NbrV.Reserve(TMath::Mn(NI1.GetDeg(), NI2.GetDeg()));
696  while (i < NI1.GetDeg() && j < NI2.GetDeg()) {
697  const int nid = NI1.GetNbrNId(i);
698  while (j < NI2.GetDeg() && NI2.GetNbrNId(j) < nid) { j++; }
699  if (j < NI2.GetDeg() && nid==NI2.GetNbrNId(j) && nid!=NId1 && nid!=NId2) {
700  IAssert(NbrV.Empty() || NbrV.Last() < nid);
701  NbrV.Add(nid);
702  j++;
703  }
704  i++;
705  }
706  return NbrV.Len();
707 }
#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

Here is the call graph for this function:

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.

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

Referenced by GetTriads(), and GetTriangleCnt().

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:1134

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Sort().

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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Sort().

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:1136
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

Here is the call graph for this function:

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.

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

Referenced by GenConfModel().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

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

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

Here is the call graph for this function:

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 248 of file subgraph.h.

References CAssert, gfEdgeDat, and HasGraphFlag.

248  {
249  CAssert(HasGraphFlag(typename PGraph::TObj, gfEdgeDat));
250  PGraph NewGraphPt = PGraph::TObj::New();
251  typename PGraph::TObj& NewGraph = *NewGraphPt;
252  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
253  if ((Cmp==1 && EI()>EDat) || (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat)) {
254  if (! NewGraph.IsNode(EI.GetSrcNId())) {
255  NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
256  }
257  if (! NewGraph.IsNode(EI.GetDstNId())) {
258  NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
259  }
260  NewGraph.AddEdge(EI);
261  }
262  }
263  return NewGraphPt;
264 }
#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 268 of file subgraph.h.

References CAssert, gfEdgeDat, HasGraphFlag, and TVec< TVal, TSizeTy >::Len().

268  {
269  CAssert(HasGraphFlag(typename PGraph::TObj, gfEdgeDat));
270  PGraph NewGraphPt = PGraph::TObj::New();
271  typename PGraph::TObj& NewGraph = *NewGraphPt;
272  NewGraph.Reserve(NIdV.Len(), -1);
273  for (int n = 0; n < NIdV.Len(); n++) {
274  NewGraph.AddNode(Graph->GetNI(NIdV[n]));
275  }
276  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
277  if (NewGraph.IsNode(EI.GetSrcNId()) && NewGraph.IsNode(EI.GetDstNId()) &&
278  ((Cmp==1 && EI()>EDat)|| (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat))) {
279  NewGraph.AddEdge(EI); }
280  }
281  NewGraph.Defrag();
282  return NewGraphPt;
283 }
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

Here is the call graph for this function:

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.

References THashSet< TKey, THashFunc >::AddKey(), GetBiCon(), THashSet< TKey, THashFunc >::GetKeyV(), TVec< TVal, TSizeTy >::Len(), TMath::Mn(), and TMath::Mx().

Referenced by Get1CnCom(), and Get1CnComSzCnt().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THashSet< TKey, THashFunc >::AddKey(), and TVec< TVal, TSizeTy >::Len().

Referenced by TLocClustStat::TCutInfo::TCutInfo().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TUNGraph::AddEdge(), TUNGraph::AddNode(), TUNGraph::TNodeI::GetInDeg(), TUNGraph::TNodeI::GetInNId(), TUNGraph::IsEdge(), TUNGraph::IsNode(), and TUNGraph::New().

Referenced by AddEgonetFeatures().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TNGraph::AddEdge(), TNGraph::AddNode(), TNGraph::TNodeI::GetDeg(), TNGraph::TNodeI::GetInDeg(), TNGraph::TNodeI::GetInNId(), TNGraph::TNodeI::GetNbrNId(), TNGraph::GetNI(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), TNGraph::IsNode(), and TNGraph::New().

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:416
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:477
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
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:542
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:398
Directed graph.
Definition: graph.h:342
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:402
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:400
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:408
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:412

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Gen(), THash< TKey, TDat, THashFunc >::GetDat(), THash< TKey, TDat, THashFunc >::GetKey(), IAssert, THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Len().

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

Here is the call graph for this function:

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.

References TSparseSVD::Lanczos(), TVec< TVal, TSizeTy >::Len(), TSparseSVD::SimpleLanczos(), TVec< TVal, TSizeTy >::Sort(), and ssotFull.

Referenced by PlotEigValDistr(), and PlotEigValRank().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVVec< TVal, TSizeTy >::GetCol(), IsAllValVNeg(), TSparseSVD::Lanczos(), and ssotFull.

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:2389

Here is the call graph for this function:

void TSnap::GetEigVec ( const PUNGraph Graph,
const int &  EigVecs,
TFltV EigValV,
TVec< TFltV > &  EigVecV 
)

Computes top EigVecs eigenvalues and eigenvectors of the adjacency matrix representing a given undirected Graph.

Definition at line 346 of file gsvd.cpp.

References TVec< TVal, TSizeTy >::Add(), TVVec< TVal, TSizeTy >::GetCol(), IsAllValVNeg(), TSparseSVD::Lanczos(), TVec< TVal, TSizeTy >::Last(), TVec< TVal, TSizeTy >::Len(), TVec< TVal, TSizeTy >::Sort(), and ssotFull.

346  {
347  const int Nodes = Graph->GetNodes();
348  // Lanczos
349  TUNGraphMtx GraphMtx(Graph);
350  int CalcVals = int(2*EigVecs);
351  if (CalcVals > Nodes) { CalcVals = Nodes; }
352  TFltVV EigVecVV;
353  //while (EigValV.Len() < EigVecs && CalcVals < 10*EigVecs) {
354  try {
355  TSparseSVD::Lanczos(GraphMtx, EigVecs, 2*EigVecs, ssotFull, EigValV, EigVecVV, false); }
356  catch(...) {
357  printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); }
358  if (EigValV.Len() < EigVecs) {
359  printf(" ***TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); }
360  // CalcVals += EigVecs;
361  //}
362  TFltIntPrV EigValIdV;
363  for (int i = 0; i < EigValV.Len(); i++) {
364  EigValIdV.Add(TFltIntPr(EigValV[i], i));
365  }
366  EigValIdV.Sort(false);
367  EigValV.Sort(false);
368  for (int v = 0; v < EigValIdV.Len(); v++) { // vector components are not sorted!!!
369  EigVecV.Add();
370  EigVecVV.GetCol(EigValIdV[v].Val2, EigVecV.Last());
371  }
372  IsAllValVNeg(EigVecV[0], true);
373 }
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:2389
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

Here is the call graph for this function:

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 206 of file subgraph.h.

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

Referenced by GetRndESubGraph(), and TTimeNENet::TimeGrowth().

206  {
207  CAssert(HasGraphFlag(typename PGraph::TObj, gfMultiGraph));
208  PGraph NewGraphPt = PGraph::TObj::New();
209  typename PGraph::TObj& NewGraph = *NewGraphPt;
210  NewGraph.Reserve(-1, EIdV.Len());
211  for (int edge = 0; edge < EIdV.Len(); edge++) {
212  const int EId = EIdV[edge];
213  IAssert(Graph->IsEdge(EId));
214  const typename PGraph::TObj::TEdgeI EI = Graph->GetEI(EId);
215  if (! NewGraph.IsNode(EI.GetSrcNId())) {
216  NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
217  }
218  if (! NewGraph.IsNode(EI.GetDstNId())) {
219  NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
220  }
221  NewGraph.AddEdge(EI);
222  }
223  return NewGraphPt;
224 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
Definition: gbase.h:14
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
#define CAssert(Cond)
Definition: bd.h:302

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
PGraph TSnap::GetESubGraph ( const PGraph &  Graph,
const TIntPrV EdgeV 
)

Definition at line 227 of file subgraph.h.

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

227  {
228  PGraph NewGraphPt = PGraph::TObj::New();
229  typename PGraph::TObj& NewGraph = *NewGraphPt;
230  NewGraph.Reserve(-1, EdgeV.Len());
231  for (int edge = 0; edge < EdgeV.Len(); edge++) {
232  const int SrcNId = EdgeV[edge].Val1;
233  const int DstNId = EdgeV[edge].Val2;
234  const typename PGraph::TObj::TEdgeI EI = Graph->GetEI(SrcNId, DstNId);
235  if (! NewGraph.IsNode(EI.GetSrcNId())) {
236  NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
237  }
238  if (! NewGraph.IsNode(EI.GetDstNId())) {
239  NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
240  }
241  NewGraph.AddEdge(EI);
242  }
243  return NewGraphPt;
244 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575

Here is the call graph for this function:

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

References THashKeyDatI< TKey, TDat >::EndI, and TInt::Mx.

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

References THashKeyDatI< TKey, TDat >::EndI, and TInt::Mx.

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

References FailR, gfBipart, gfDirected, gfEdgeDat, gfMultiGraph, gfNodeDat, gfSources, and gfUndef.

Referenced by TBigNet< TNodeData, IsDir >::DumpFlags(), and PrintInfo().

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

Here is the caller graph for this function:

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.

References GetGroupFarnessCentr().

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

Here is the call graph for this function:

double TSnap::GetGroupDegreeCentr ( const PUNGraph Graph,
const PUNGraph Group 
)

Definition at line 59 of file centr.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), and THash< TKey, TDat, THashFunc >::Len().

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

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::EndI(), TUNGraph::TNodeI::GetDeg(), TUNGraph::TNodeI::GetNbrNId(), THash< TKey, TDat, THashFunc >::IsKey(), and THash< TKey, TDat, THashFunc >::Len().

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

Here is the call graph for this function:

double TSnap::GetGroupDegreeCentr0 ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Definition at line 72 of file centr.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::GetDat(), THash< TKey, TDat, THashFunc >::IsKey(), and THash< TKey, TDat, THashFunc >::Len().

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

Here is the call graph for this function:

double TSnap::GetGroupFarnessCentr ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Definition at line 105 of file centr.cpp.

References THash< TKey, TDat, THashFunc >::GetDat(), THash< TKey, TDat, THashFunc >::Len(), and TInt::Mx.

Referenced by GetGroupClosenessCentr().

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:1139
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
int Len() const
Definition: hash.h:228

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Gen(), THash< TKey, TDat, THashFunc >::GetDat(), THash< TKey, TDat, THashFunc >::Len(), and TMath::Sqr().

Referenced by MapHits().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

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

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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Sort().

Referenced by PlotInDegDistr(), and TGStat::TakeDegDistr().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Sort().

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:1136
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

Here is the call graph for this function:

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 377 of file gsvd.cpp.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), TVec< TVal, TSizeTy >::Empty(), TVVec< TVal, TSizeTy >::GetCol(), TVVec< TVal, TSizeTy >::GetCols(), TSnap::TSnapDetail::GetInvParticipRatEig(), TExeTm::GetStr(), TSparseSVD::Lanczos2(), TVec< TVal, TSizeTy >::Len(), TMath::Mn(), TVec< TVal, TSizeTy >::Sort(), and ssotFull.

Referenced by PlotInvParticipRat().

377  {
378  TUNGraphMtx GraphMtx(Graph);
379  TFltVV EigVecVV;
380  TFltV EigValV;
381  TExeTm ExeTm;
382  if (MaxEigVecs<=1) { MaxEigVecs=1000; }
383  int EigVecs = TMath::Mn(Graph->GetNodes(), MaxEigVecs);
384  printf("start %d vecs...", EigVecs);
385  try {
386  TSparseSVD::Lanczos2(GraphMtx, EigVecs, TimeLimit, ssotFull, EigValV, EigVecVV, false);
387  } catch(...) {
388  printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", EigVecs, EigValV.Len()); }
389  printf(" ***TRIED %d GOT %d values in %s\n", EigVecs, EigValV.Len(), ExeTm.GetStr());
390  TFltV EigVec;
391  EigValIprV.Clr();
392  if (EigValV.Empty()) { return; }
393  for (int v = 0; v < EigVecVV.GetCols(); v++) {
394  EigVecVV.GetCol(v, EigVec);
395  EigValIprV.Add(TFltPr(EigValV[v], TSnapDetail::GetInvParticipRatEig(EigVec)));
396  }
397  EigValIprV.Sort();
398 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
double GetInvParticipRatEig(const TFltV &EigVec)
Definition: gsvd.cpp:401
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:2389
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:2252

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TKCore< PGraph >::GetCoreK(), TKCore< PGraph >::GetNIdV(), and GetSubGraph().

Referenced by ChibaNishizekiWeighter::Initialize().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), TKCore< PGraph >::GetCoreEdges(), TKCore< PGraph >::GetCurK(), and TKCore< PGraph >::GetNextCore().

Referenced by PlotKCoreEdges().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), TKCore< PGraph >::GetCoreNodes(), TKCore< PGraph >::GetCurK(), and TKCore< PGraph >::GetNextCore().

Referenced by PlotKCoreNodes().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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 712 of file triad.h.

712  {
713  TIntV NbrV;
714  return GetLen2Paths(Graph, NId1, NId2, NbrV);
715 }
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:720
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 720 of file triad.h.

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

720  {
721  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId1);
722  NbrV.Clr(false);
723  NbrV.Reserve(NI.GetOutDeg());
724  for (int e = 0; e < NI.GetOutDeg(); e++) {
725  const typename PGraph::TObj::TNodeI MidNI = Graph->GetNI(NI.GetOutNId(e));
726  if (MidNI.IsOutNId(NId2)) {
727  NbrV.Add(MidNI.GetId());
728  }
729  }
730  return NbrV.Len();
731 }
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

Here is the call graph for this function:

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.

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

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

Here is the call graph for this function:

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.

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

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

Here is the call graph for this function:

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.

References CapAttrName, FindAugV(), TNEANet::TEdgeI::GetDstNId(), TNEANet::TEdgeI::GetSrcNId(), IAssert, and TVec< TVal, TSizeTy >::Len().

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

Here is the call graph for this function:

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.

References TSnap::TPRManager::Capacity(), TSnap::TPRManager::Excess(), TSnap::TPRManager::Flow(), TSnap::TPRManager::GetMaxLabel(), TNEANet::TNodeI::GetOutDeg(), TNEANet::TNodeI::GetOutEId(), TNEANet::TNodeI::GetOutNId(), GlobalRelabel(), TSnap::TPRManager::HasActive(), IAssert, TSnap::TPRManager::IsActive(), TSnap::TPRManager::Label(), TSnap::TPRManager::PopActive(), TSnap::TPRManager::PushActive(), PushRelabel(), and TSnap::TPRManager::SetLabel().

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

Here is the call graph for this function:

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.

References THashSet< TKey, THashFunc >::AddKey(), and TVec< TVal, TSizeTy >::Len().

Referenced by GetModularity().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

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

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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Empty(), GetBiCon(), GetSubGraph(), and TVec< TVal, TSizeTy >::Len().

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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Empty(), GetBiCon(), GetSubGraph(), and TVec< TVal, TSizeTy >::Len().

Referenced by TGStat::TakeBccStat().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), EAssertR, TVec< TVal, TSizeTy >::Empty(), TRnd::GetUniDevInt(), TVec< TVal, TSizeTy >::Len(), and TInt::Rnd.

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:1143
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), EAssertR, TVec< TVal, TSizeTy >::Empty(), TRnd::GetUniDevInt(), TVec< TVal, TSizeTy >::Len(), and TInt::Rnd.

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:1143
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), EAssertR, TVec< TVal, TSizeTy >::Empty(), TRnd::GetUniDevInt(), TVec< TVal, TSizeTy >::Len(), and TInt::Rnd.

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:1143
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Empty(), GetSccs(), GetSubGraph(), and TVec< TVal, TSizeTy >::Len().

Referenced by TGStat::TakeSccStat().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

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

Referenced by PrintInfo().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Empty(), GetSubGraph(), GetWccs(), and TVec< TVal, TSizeTy >::Len().

Referenced by main(), TKronMtx::PlotCmpGraphs(), TTimeNet::PlotEffDiam(), TTimeNENet::PlotEffDiam(), TTimeNet::PlotMissingPast(), TLocClustStat::Run(), and TGStat::TakeStat().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

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

Referenced by PrintInfo().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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 181 of file triad.h.

References GetNodeTriads().

181  {
182  int Open, Closed;
183  GetNodeTriads(Graph, NId, Open, Closed);
184  //const double Deg = Graph->GetNI(NId).GetDeg();
185  return (Open+Closed)==0 ? 0 : double(Open)/double(Open+Closed);
186 }
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:611

Here is the call graph for this function:

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.

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 189 of file triad.h.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Clr(), GetTriads(), and TVec< TVal, TSizeTy >::Len().

189  {
190  TIntTrV NIdCOTriadV;
191  GetTriads(Graph, NIdCOTriadV);
192  NIdCCfH.Clr(false);
193  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
194  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
195  const double CCf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
196  NIdCCfH.AddDat(NIdCOTriadV[i].Val1, CCf);
197  }
198 }
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:274
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

Here is the call graph for this function:

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.

References TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::Len(), TInt::Mx, and TBreathFS< PGraph >::NIdDistH.

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:1139

Here is the call graph for this function:

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.

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

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

Here is the call graph for this function:

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.

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

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

Here is the call graph for this function:

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 366 of file bfsdfs.h.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), and TBreathFS< PGraph >::NIdDistH.

366  {
367  TBreathFS<PGraph> BFS(Graph);
368  BFS.DoBfs(StartNId, true, !IsDir, -1, Hop);
369  NIdV.Clr(false);
370  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
371  if (BFS.NIdDistH[i] == Hop) {
372  NIdV.Add(BFS.NIdDistH.GetKey(i)); }
373  }
374  return NIdV.Len();
375 }
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

Here is the call graph for this function:

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 378 of file bfsdfs.h.

References THash< TKey, TDat, THashFunc >::AddDat(), TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::GetKeyDatPrV(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TInt::Mx, TBreathFS< PGraph >::NIdDistH, and TVec< TVal, TSizeTy >::Sort().

378  {
379  TBreathFS<PGraph> BFS(Graph);
380  BFS.DoBfs(StartNId, true, !IsDir, -1, TInt::Mx);
381  TIntH HopCntH;
382  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
383  HopCntH.AddDat(BFS.NIdDistH[i]) += 1;
384  }
385  HopCntH.GetKeyDatPrV(HopCntV);
386  HopCntV.Sort();
387  return HopCntV.Len();
388 }
static const int Mx
Definition: dt.h:1139
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

Here is the call graph for this function:

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 571 of file triad.h.

Referenced by GetNodeClustCf(), and GetTriadParticip().

571  {
572  int ClosedTriads=0, OpenTriads=0;
573  return GetNodeTriads(Graph, NId, ClosedTriads, OpenTriads);
574 }
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:611

Here is the caller graph for this function:

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 578 of file triad.h.

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

578  {
579  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
580  ClosedTriads=0; OpenTriads=0;
581  if (NI.GetDeg() < 2) { return 0; }
582  // find neighborhood
583  TIntSet NbrSet(NI.GetDeg());
584  for (int e = 0; e < NI.GetOutDeg(); e++) {
585  if (NI.GetOutNId(e) != NI.GetId()) { // exclude self edges
586  NbrSet.AddKey(NI.GetOutNId(e)); }
587  }
588  if (Graph->HasFlag(gfDirected)) {
589  for (int e = 0; e < NI.GetInDeg(); e++) {
590  if (NI.GetInNId(e) != NI.GetId()) { // exclude self edges
591  NbrSet.AddKey(NI.GetInNId(e)); }
592  }
593  }
594  // count connected neighbors
595  for (int srcNbr = 0; srcNbr < NbrSet.Len(); srcNbr++) {
596  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrSet.GetKey(srcNbr));
597  for (int dstNbr = srcNbr+1; dstNbr < NbrSet.Len(); dstNbr++) {
598  const int dstNId = NbrSet.GetKey(dstNbr);
599  if (SrcNode.IsNbrNId(dstNId)) { ClosedTriads++; }
600  else { OpenTriads++; }
601  }
602  }
603  return ClosedTriads;
604 }
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13

Here is the call graph for this function:

template<class PGraph >
int TSnap::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 611 of file triad.h.

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

611  {
612  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
613  const bool IsDir = Graph->HasFlag(gfDirected);
614  InGroupEdges=0; InOutGroupEdges=0; OutGroupEdges=0;
615  if (NI.GetDeg() < 2) { return 0; }
616  // find neighborhood
617  TIntSet NbrSet(NI.GetDeg());
618  for (int e = 0; e < NI.GetOutDeg(); e++) {
619  if (NI.GetOutNId(e) != NI.GetId()) { // exclude self edges
620  NbrSet.AddKey(NI.GetOutNId(e)); }
621  }
622  if (IsDir) {
623  for (int e = 0; e < NI.GetInDeg(); e++) {
624  if (NI.GetInNId(e) != NI.GetId()) {
625  NbrSet.AddKey(NI.GetInNId(e)); }
626  }
627  }
628  // count connected neighbors
629  for (int srcNbr = 0; srcNbr < NbrSet.Len(); srcNbr++) {
630  const int NbrId = NbrSet.GetKey(srcNbr);
631  const bool NbrIn = GroupSet.IsKey(NbrId);
632  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrId);
633  for (int dstNbr = srcNbr+1; dstNbr < NbrSet.Len(); dstNbr++) {
634  const int DstNId = NbrSet.GetKey(dstNbr);
635  if (SrcNode.IsNbrNId(DstNId)) { // triad (NId, NbrId, DstNid)
636  bool DstIn = GroupSet.IsKey(DstNId);
637  if (NbrIn && DstIn) { InGroupEdges++; }
638  else if (NbrIn || DstIn) { InOutGroupEdges++; }
639  else { OutGroupEdges++; }
640  }
641  }
642  }
643  return InGroupEdges;
644 }
bool IsKey(const TKey &Key) const
Definition: shash.h:1148
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13

Here is the call graph for this function:

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

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Gen(), gfDirected, HasGraphFlag, and TSnapQueue< TVal >::Push().

Referenced by TSnap::TSnapDetail::CmtyGirvanNewmanStep().

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:201
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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Sort().

Referenced by PlotOutDegDistr(), and TGStat::TakeDegDistr().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), THash< TKey, TDat, THashFunc >::Len(), and TVec< TVal, TSizeTy >::Sort().

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:1136
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

Here is the call graph for this function:

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.

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

Referenced by MapPageRank().

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:1383
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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

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

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

Here is the call graph for this function:

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.

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

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:1383
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

Here is the call graph for this function:

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.

References anonymous_namespace{randwalk.h}::ApproxContributionsBalanced(), THash< TKey, TDat, THashFunc >::GetDatWithDefault(), TVec< TVal, TSizeTy >::Len(), SamplePersonalizedPageRank(), and anonymous_namespace{randwalk.h}::WallClockTime().

Referenced by GetRndWalkRestartBidirectional().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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 452 of file subgraph.h.

References TVec< TVal, TSizeTy >::Add(), CAssert, GetESubGraph(), gfMultiGraph, HasGraphFlag, IAssert, and TInt::Rnd.

452  {
453  CAssert(! HasGraphFlag(typename PGraph::TObj, gfMultiGraph));
454  TIntPrV EdgeV(Graph->GetEdges(), 0);
455  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
456  EdgeV.Add(TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
457  }
458  EdgeV.Shuffle(TInt::Rnd);
459  EdgeV.Del(NEdges, EdgeV.Len()-1);
460  IAssert(EdgeV.Len() == NEdges);
461  return GetESubGraph(Graph, EdgeV);
462 }
#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:227
static TRnd Rnd
Definition: dt.h:1143
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

Here is the call graph for this function:

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 441 of file subgraph.h.

References TVec< TVal, TSizeTy >::Del(), GetSubGraph(), IAssert, TVec< TVal, TSizeTy >::Len(), TInt::Rnd, and TVec< TVal, TSizeTy >::Shuffle().

441  {
442  IAssert(NNodes <= Graph->GetNodes());
443  TIntV NIdV;
444  Graph->GetNIdV(NIdV);
445  NIdV.Shuffle(TInt::Rnd);
446  NIdV.Del(NNodes, NIdV.Len()-1);
447  IAssert(NIdV.Len() == NNodes);
448  return GetSubGraph(Graph, NIdV);
449 }
#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:1143
PGraph GetSubGraph(const PGraph &Graph, const TIntV &NIdV)
Returns an induced subgraph of graph Graph with NIdV nodes.
Definition: subgraph.h:200
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335

Here is the call graph for this function:

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.

References GetPersonalizedPageRankBidirectional(), and TVec< TInt >::GetV().

142  {
143  return GetPersonalizedPageRankBidirectional(Graph, JumpProb, TIntV::GetV(StartNId), TargetNId,
144  minProbability, relativeError, proveRelativeError, PrintTimeForTuning);
145  }
static TVec< TInt, TSizeTy > 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

Here is the call graph for this function:

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.

References TSccVisitor< PGraph, OnlyCount >::CnComV, TCnCom::GetDfsVisitor(), and TVec< TVal, TSizeTy >::Sort().

Referenced by GetMxScc(), and GetMxSccSz().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TCnCom::GetDfsVisitor(), THash< TKey, TDat, THashFunc >::GetKeyDatPrV(), TSccVisitor< PGraph, OnlyCount >::SccCntH, and TVec< TVal, TSizeTy >::Sort().

Referenced by PlotSccDistr(), and TGStat::TakeConnComp().

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

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
int TSnap::GetShortestDistances ( const PGraph &  Graph,
const int &  StartNId,
const bool &  FollowOut,
const bool &  FollowIn,
TIntV ShortestDists 
)

Definition at line 487 of file bfsdfs.h.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Empty(), TVec< TVal, TSizeTy >::Gen(), TVec< TVal, TSizeTy >::GetVal(), TVec< TVal, TSizeTy >::Len(), TStdOut::New(), and TVec< TVal, TSizeTy >::Reduce().

487  {
488  PSOut StdOut = TStdOut::New();
489  int MxNId = Graph->GetMxNId();
490  int NonNodeDepth = 2147483647; // INT_MAX
491  int InfDepth = 2147483646; // INT_MAX - 1
492  ShortestDists.Gen(MxNId);
493  for (int NId = 0; NId < MxNId; NId++) {
494  if (Graph->IsNode(NId)) { ShortestDists[NId] = InfDepth; }
495  else { ShortestDists[NId] = NonNodeDepth; }
496  }
497 
498  TIntV Vec1(MxNId, 0); // ensure enough capacity
499  TIntV Vec2(MxNId, 0); // ensure enough capacity
500 
501  ShortestDists[StartNId] = 0;
502  TIntV* PCurV = &Vec1;
503  PCurV->Add(StartNId);
504  TIntV* PNextV = &Vec2;
505  int Depth = 0; // current depth
506  while (!PCurV->Empty()) {
507  Depth++; // increase depth
508  for (int i = 0; i < PCurV->Len(); i++) {
509  int NId = PCurV->GetVal(i);
510  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
511  for (int e = 0; e < NI.GetOutDeg(); e++) {
512  const int OutNId = NI.GetOutNId(e);
513  if (ShortestDists[OutNId].Val == InfDepth) {
514  ShortestDists[OutNId] = Depth;
515  PNextV->Add(OutNId);
516  }
517  }
518  }
519  // swap pointer, no copying
520  TIntV* Tmp = PCurV;
521  PCurV = PNextV;
522  PNextV = Tmp;
523  // clear next
524  PNextV->Reduce(0); // reduce length, does not initialize new array
525  }
526  return Depth-1;
527 }
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

Here is the call graph for this function:

template<class PGraph >
int TSnap::GetShortestDistancesMP2 ( const PGraph &  Graph,
const int &  StartNId,
const bool &  FollowOut,
const bool &  FollowIn,
TIntV ShortestDists 
)

Definition at line 531 of file bfsdfs.h.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::AddMP(), TVec< TVal, TSizeTy >::Empty(), TVec< TVal, TSizeTy >::Gen(), TVec< TVal, TSizeTy >::GetVal(), TVec< TVal, TSizeTy >::Len(), and TVec< TVal, TSizeTy >::Reduce().

531  {
532  int MxNId = Graph->GetMxNId();
533  int NonNodeDepth = 2147483647; // INT_MAX
534  int InfDepth = 2147483646; // INT_MAX - 1
535  ShortestDists.Gen(MxNId);
536  #pragma omp parallel for schedule(dynamic,10000)
537  for (int NId = 0; NId < MxNId; NId++) {
538  if (Graph->IsNode(NId)) { ShortestDists[NId] = InfDepth; }
539  else { ShortestDists[NId] = NonNodeDepth; }
540  }
541 
542  TIntV Vec1(MxNId, 0); // ensure enough capacity
543  TIntV Vec2(MxNId, 0); // ensure enough capacity
544 
545  ShortestDists[StartNId] = 0;
546  TIntV* PCurV = &Vec1;
547  PCurV->Add(StartNId);
548  TIntV* PNextV = &Vec2;
549  int Depth = 0; // current depth
550 
551  while (!PCurV->Empty()) {
552  Depth++; // increase depth
553  #pragma omp parallel for schedule(dynamic,10000)
554  for (int i = 0; i < PCurV->Len(); i++) {
555  int NId = PCurV->GetVal(i);
556  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
557  for (int e = 0; e < NI.GetOutDeg(); e++) {
558  const int OutNId = NI.GetOutNId(e);
559  if (__sync_bool_compare_and_swap(&(ShortestDists[OutNId].Val), InfDepth, Depth)) {
560  PNextV->AddMP(OutNId);
561  }
562  }
563  }
564 // #pragma omp parallel for schedule(dynamic,10000)
565 // for (int NId = 0; NId < MxNId; NId++) {
566 // if (ShortestDists[NId] == InfDepth) {
567 // typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
568 // for (int e = 0; e < NI.GetInDeg(); e++) {
569 // const int InNId = NI.GetInNId(e);
570 // if (ShortestDists[InNId] < Depth) {
571 // ShortestDists[NId] = Depth;
572 // PNextV->AddMP(NId);
573 // break;
574 // }
575 // }
576 // }
577 // }
578  // swap pointer, no copying
579  TIntV* Tmp = PCurV;
580  PCurV = PNextV;
581  PNextV = Tmp;
582  // clear next
583  PNextV->Reduce(0); // reduce length, does not initialize new array
584  }
585  return Depth-1;
586 }
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

Here is the call graph for this function:

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 400 of file bfsdfs.h.

References TBreathFS< PGraph >::DoBfs(), TBreathFS< PGraph >::GetHops(), and TInt::Mx.

400  {
401  TBreathFS<PGraph> BFS(Graph);
402  BFS.DoBfs(SrcNId, true, ! IsDir, DstNId, TInt::Mx);
403  return BFS.GetHops(SrcNId, DstNId);
404 }
static const int Mx
Definition: dt.h:1139

Here is the call graph for this function:

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 391 of file bfsdfs.h.

References THash< TKey, TDat, THashFunc >::Clr(), TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::Len(), TBreathFS< PGraph >::NIdDistH, and THash< TKey, TDat, THashFunc >::Swap().

391  {
392  TBreathFS<PGraph> BFS(Graph);
393  BFS.DoBfs(SrcNId, true, ! IsDir, -1, MaxDist);
394  NIdToDistH.Clr();
395  NIdToDistH.Swap(BFS.NIdDistH);
396  return NIdToDistH[NIdToDistH.Len()-1];
397 }
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

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddKey(), TVVec< TVal, TSizeTy >::At(), TNGraph::BegNI(), TNGraph::EndNI(), TNGraph::GetEdges(), THash< TKey, TDat, THashFunc >::GetKeyId(), TNGraph::GetNodes(), IAssert, TSparseSVD::LanczosSVD(), TVec< TVal, TSizeTy >::Len(), TSparseSVD::SimpleLanczosSVD(), TVec< TVal, TSizeTy >::Sort(), ssotFull, and TSvd::Svd1Based().

Referenced by TKroneckerLL::GradDescentConvergence(), PlotSngValDistr(), PlotSngValRank(), and TGStat::TakeSpectral().

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
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
static void SimpleLanczosSVD(const TMatrix &Matrix, const int &CalcSV, TFltV &SngValV, const bool &DoLocalReortoP=false)
Definition: linalg.cpp:1440
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
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
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddKey(), TVVec< TVal, TSizeTy >::At(), TNGraph::BegNI(), TNGraph::EndNI(), TVVec< TVal, TSizeTy >::GetCol(), TNGraph::GetEdges(), THash< TKey, TDat, THashFunc >::GetKeyId(), TNGraph::GetNodes(), IsAllValVNeg(), TSparseSVD::LanczosSVD(), TVec< TVal, TSizeTy >::Len(), TFlt::Mn, ssotFull, and TSvd::Svd1Based().

Referenced by PlotSngVec(), and TGStat::TakeSpectral().

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 }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
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
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
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:1383
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
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2389
static const double Mn
Definition: dt.h:1387

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::GetSngVec ( const PNGraph Graph,
const int &  SngVecs,
TFltV SngValV,
TVec< TFltV > &  LeftSV,
TVec< TFltV > &  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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddKey(), TVVec< TVal, TSizeTy >::At(), TNGraph::BegNI(), TVec< TVal, TSizeTy >::Clr(), TNGraph::EndNI(), TVVec< TVal, TSizeTy >::GetCol(), TNGraph::GetEdges(), THash< TKey, TDat, THashFunc >::GetKeyId(), TNGraph::GetNodes(), IsAllValVNeg(), TSparseSVD::LanczosSVD(), TVec< TVal, TSizeTy >::Last(), TVec< TVal, TSizeTy >::Len(), TVec< TVal, TSizeTy >::Sort(), ssotFull, and TSvd::Svd1Based().

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
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
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
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
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
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2389
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

Here is the call graph for this function:

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.

References TUNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TUNGraph::AddNode(), edge, TUNGraph::TNodeI::GetOutDeg(), TUNGraph::TNodeI::GetOutNId(), TVec< TVal, TSizeTy >::Len(), TUNGraph::New(), and TUNGraph::Reserve().

Referenced by ProcessedGraph::countClique(), ProcessedGraph::countDirTriadMotif(), TCodaAnalyzer::Draw2ModeCommunity(), TLocClust::DrawWhiskers(), TKCore< PGraph >::GetCoreG(), GetKCore(), GetMxBiCon(), GetMxScc(), GetMxWcc(), GetRndSubGraph(), TTimeNet::PlotCCfOverTm(), TTimeNet::PlotMedianDegOverTm(), TTimeNet::PlotMissingPast(), TLocClustStat::PlotPhiInOut(), TAGMFast::SetGraph(), TCoda::SetGraph(), TKroneckerLL::SetGraph(), TCesna::SetGraph(), and TMAGFitBern::SetGraph().

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:298
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

Here is the call graph for this function:

Here is the caller graph for this function:

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 200 of file subgraph.h.

200  {
201  return TSnapDetail::TGetSubGraph<PGraph, HasGraphFlag(typename PGraph::TObj, gfMultiGraph)>
202  ::Do(Graph, NIdV);
203 }
PNGraph TSnap::GetSubGraph ( const PNGraph Graph,
const TIntV NIdV,
const bool &  RenumberNodes 
)

Definition at line 45 of file subgraph.cpp.

References TNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TNGraph::AddNode(), edge, TNGraph::GetNI(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), TNGraph::IsNode(), TVec< TVal, TSizeTy >::Len(), TNGraph::New(), and TNGraph::Reserve().

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:477
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
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
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:542
int AddKey(const TKey &Key)
Definition: shash.h:1254
Directed graph.
Definition: graph.h:342
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:402
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:606
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:412

Here is the call graph for this function:

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 354 of file bfsdfs.h.

References TBreathFS< PGraph >::DoBfs(), THash< TKey, TDat, THashFunc >::Len(), TMath::Mx(), TInt::Mx, and TBreathFS< PGraph >::NIdDistH.

354  {
355  TBreathFS<PGraph> BFS(Graph);
356  BFS.DoBfs(StartNId, FollowOut, FollowIn, -1, TInt::Mx);
357  TreeSz = BFS.NIdDistH.Len();
358  TreeDepth = 0;
359  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
360  TreeDepth = TMath::Mx(TreeDepth, BFS.NIdDistH[i].Val);
361  }
362  return TreeSz;
363 }
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
static const int Mx
Definition: dt.h:1139

Here is the call graph for this function:

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

Definition at line 80 of file alg.h.

References Assert, and IsTree().

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

Here is the call graph for this function:

template<class PGraph >
void TSnap::GetTreeSig ( const PGraph &  Graph,
const int &  RootNId,
TIntV Sig 
)

Definition at line 484 of file alg.h.

References TVec< TVal, TSizeTy >::Add(), CAssert, TVec< TVal, TSizeTy >::Gen(), gfDirected, HasGraphFlag, IAssert, TVec< TVal, TSizeTy >::Len(), TSnapQueue< TVal >::Push(), and TVec< TVal, TSizeTy >::QSort().

Referenced by TGHash< TDat >::AddKey(), TGHash< TDat >::GetNodeMap(), and TGHash< TDat >::IsGetKeyId().

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:201
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

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
void TSnap::GetTreeSig ( const PGraph &  Graph,
const int &  RootNId,
TIntV Sig,
TIntPrV NodeMap 
)

Definition at line 511 of file alg.h.

References TVec< TVal, TSizeTy >::Add(), CAssert, TVec< TVal, TSizeTy >::Gen(), gfDirected, HasGraphFlag, IAssert, TVec< TVal, TSizeTy >::Len(), TSnapQueue< TVal >::Push(), and TVec< TVal, TSizeTy >::QSort().

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:201
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

Here is the call graph for this function:

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 535 of file triad.h.

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

535  {
536  const bool IsDir = Graph->HasFlag(gfDirected);
537  TIntSet NbrH;
538  int TriadEdges = 0;
539  for(typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
540  NbrH.Clr(false);
541  for (int e = 0; e < NI.GetOutDeg(); e++) {
542  if (NI.GetOutNId(e) != NI.GetId()) {
543  NbrH.AddKey(NI.GetOutNId(e)); }
544  }
545  if (IsDir) {
546  for (int e = 0; e < NI.GetInDeg(); e++) {
547  if (NI.GetInNId(e) != NI.GetId()) {
548  NbrH.AddKey(NI.GetInNId(e)); }
549  }
550  }
551  for (int e = 0; e < NI.GetOutDeg(); e++) {
552  if (!IsDir && NI.GetId()<NI.GetOutNId(e)) { continue; } // for undirected graphs count each edge only once
553  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NI.GetOutNId(e));
554  bool Triad=false;
555  for (int e1 = 0; e1 < SrcNode.GetOutDeg(); e1++) {
556  if (NbrH.IsKey(SrcNode.GetOutNId(e1))) { Triad=true; break; }
557  }
558  if (IsDir && ! Triad) {
559  for (int e1 = 0; e1 < SrcNode.GetInDeg(); e1++) {
560  if (NbrH.IsKey(SrcNode.GetInNId(e1))) { Triad=true; break; }
561  }
562  }
563  if (Triad) { TriadEdges++; }
564  }
565  }
566  return TriadEdges;
567 }
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

Here is the call graph for this function:

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 648 of file triad.h.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::GetKeyDatPrV(), GetNodeTriads(), and TVec< TVal, TSizeTy >::Sort().

Referenced by TGStat::TakeTriadPart().

648  {
649  TIntH TriadCntH;
650  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
651  const int Triads = GetNodeTriads(Graph, NI.GetId());
652  TriadCntH.AddDat(Triads) += 1;
653  }
654  TriadCntH.GetKeyDatPrV(TriadCntV);
655  TriadCntV.Sort();
656 }
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:611
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

Here is the call graph for this function:

Here is the caller graph for this function:

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 201 of file triad.h.

References GetTriads().

201  {
202  int64 OpenTriads, ClosedTriads;
203  return GetTriads(Graph, ClosedTriads, OpenTriads, SampleNodes);
204 }
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:274
long long int64
Definition: bd.h:27

Here is the call graph for this function:

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 274 of file triad.h.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), GetCommon(), GetUniqueNbrV(), gfDirected, TVec< TVal, TSizeTy >::Len(), TVec< TVal, TSizeTy >::Reduce(), TVec< TVal, TSizeTy >::Reserve(), and TVec< TVal, TSizeTy >::Shuffle().

274  {
275  const bool IsDir = Graph->HasFlag(gfDirected);
276  TIntSet NbrH;
277  TIntV NIdV;
278  //TRnd Rnd(0);
279  TRnd Rnd(1);
280  int NNodes;
281  TIntV Nbrs;
282  int NId;
283 
284  int64 hcount;
285 
286  hcount = 0;
287 
288  NNodes = Graph->GetNodes();
289  Graph->GetNIdV(NIdV);
290  NIdV.Shuffle(Rnd);
291  if (SampleNodes == -1) {
292  SampleNodes = NNodes;
293  }
294 
295  int MxId = -1;
296  for (int i = 0; i < NNodes; i++) {
297  if (NIdV[i] > MxId) {
298  MxId = NIdV[i];
299  }
300  }
301 
302  TVec<TIntV> NbrV(MxId + 1);
303 
304  if (IsDir) {
305  // get in and out neighbors
306  for (int node = 0; node < NNodes; node++) {
307  int NId = NIdV[node];
308  NbrV[NId] = TIntV();
309  GetUniqueNbrV(Graph, NId, NbrV[NId]);
310  }
311  } else {
312  // get only out neighbors
313  for (int node = 0; node < NNodes; node++) {
314  int NId = NIdV[node];
315  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
316  NbrV[NId] = TIntV();
317  NbrV[NId].Reserve(NI.GetOutDeg());
318  NbrV[NId].Reduce(0);
319  for (int i = 0; i < NI.GetOutDeg(); i++) {
320  NbrV[NId].Add(NI.GetOutNId(i));
321  }
322  }
323  }
324 
325  NIdCOTriadV.Clr(false);
326  NIdCOTriadV.Reserve(SampleNodes);
327  for (int node = 0; node < SampleNodes; node++) {
328  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[node]);
329  int NLen;
330 
331  NId = NI.GetId();
332  hcount++;
333  if (NI.GetDeg() < 2) {
334  NIdCOTriadV.Add(TIntTr(NId, 0, 0)); // zero triangles
335  continue;
336  }
337 
338  Nbrs = NbrV[NId];
339  NLen = Nbrs.Len();
340 
341  // count connected neighbors
342  int OpenCnt1 = 0, CloseCnt1 = 0;
343  for (int srcNbr = 0; srcNbr < NLen; srcNbr++) {
344  int Count = GetCommon(NbrV[NbrV[NId][srcNbr]],Nbrs);
345  CloseCnt1 += Count;
346  }
347  CloseCnt1 /= 2;
348  OpenCnt1 = (NLen*(NLen-1))/2 - CloseCnt1;
349  NIdCOTriadV.Add(TIntTr(NId, CloseCnt1, OpenCnt1));
350  }
351 }
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:734
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

Here is the call graph for this function:

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 207 of file triad.h.

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

Referenced by GetClustCf(), GetNodeClustCf(), GetTriads(), PrintInfo(), and TKronMomentsFit::TKronMomentsFit().

207  {
208  TIntTrV NIdCOTriadV;
209  GetTriads(Graph, NIdCOTriadV, SampleNodes);
210  uint64 closedTriads = 0;
211  uint64 openTriads = 0;
212  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
213  closedTriads += NIdCOTriadV[i].Val2;
214  openTriads += NIdCOTriadV[i].Val3;
215  }
216  //IAssert(closedTriads/3 < (uint64) TInt::Mx);
217  //IAssert(openTriads < (uint64) TInt::Mx);
218  ClosedTriads = int64(closedTriads/3); // each triad is counted 3 times
219  OpenTriads = int64(openTriads);
220  return ClosedTriads;
221 }
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:274
long long int64
Definition: bd.h:27

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
void TSnap::GetTriads_v0 ( const PGraph &  Graph,
TIntTrV NIdCOTriadV,
int  SampleNodes 
)

Definition at line 226 of file triad.h.

References TVec< TVal, TSizeTy >::Add(), THashSet< TKey, THashFunc >::AddKey(), TVec< TVal, TSizeTy >::Clr(), THashSet< TKey, THashFunc >::Clr(), THashSet< TKey, THashFunc >::GetKey(), gfDirected, IAssert, THashSet< TKey, THashFunc >::Len(), TVec< TVal, TSizeTy >::Reserve(), and TVec< TVal, TSizeTy >::Shuffle().

226  {
227  const bool IsDir = Graph->HasFlag(gfDirected);
228  TIntSet NbrH;
229  TIntV NIdV;
230  TRnd Rnd(0);
231 
232  Graph->GetNIdV(NIdV);
233  NIdV.Shuffle(Rnd);
234  if (SampleNodes == -1) {
235  SampleNodes = Graph->GetNodes(); }
236  NIdCOTriadV.Clr(false);
237  NIdCOTriadV.Reserve(SampleNodes);
238  for (int node = 0; node < SampleNodes; node++) {
239  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[node]);
240  if (NI.GetDeg() < 2) {
241  NIdCOTriadV.Add(TIntTr(NI.GetId(), 0, 0)); // zero triangles
242  continue;
243  }
244  // find neighborhood
245  NbrH.Clr(false);
246  for (int e = 0; e < NI.GetOutDeg(); e++) {
247  if (NI.GetOutNId(e) != NI.GetId()) {
248  NbrH.AddKey(NI.GetOutNId(e)); }
249  }
250  if (IsDir) {
251  for (int e = 0; e < NI.GetInDeg(); e++) {
252  if (NI.GetInNId(e) != NI.GetId()) {
253  NbrH.AddKey(NI.GetInNId(e)); }
254  }
255  }
256  // count connected neighbors
257  int OpenCnt=0, CloseCnt=0;
258  for (int srcNbr = 0; srcNbr < NbrH.Len(); srcNbr++) {
259  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrH.GetKey(srcNbr));
260  for (int dstNbr = srcNbr+1; dstNbr < NbrH.Len(); dstNbr++) {
261  const int dstNId = NbrH.GetKey(dstNbr);
262  if (SrcNode.IsNbrNId(dstNId)) { CloseCnt++; } // is edge
263  else { OpenCnt++; }
264  }
265  }
266  IAssert(2*(OpenCnt+CloseCnt) == NbrH.Len()*(NbrH.Len()-1));
267  NIdCOTriadV.Add(TIntTr(NI.GetId(), CloseCnt, OpenCnt));
268  }
269 }
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

Here is the call graph for this function:

template<class PGraph >
int64 TSnap::GetTriangleCnt ( const PGraph &  Graph)

Returns the number of triangles in graph Graph.

Definition at line 410 of file triad.h.

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

410  {
411  const int NNodes = Graph->GetNodes();
412 
413  TIntV MapV(NNodes);
415  NV.Reduce(0);
416 
417  int MxId = -1;
418  int ind = 0;
419  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
420  NV.Add(NI);
421  int Id = NI.GetId();
422  if (Id > MxId) {
423  MxId = Id;
424  }
425  MapV[ind] = Id;
426  ind++;
427  }
428 
429  TIntV IndV(MxId+1);
430 
431  for (int j = 0; j < NNodes; j++) {
432  IndV[MapV[j]] = j;
433  }
434 
435  ind = MapV.Len();
436 
437  TVec<TIntV> HigherDegNbrV(ind);
438 
439  for (int i = 0; i < ind; i++) {
440  HigherDegNbrV[i] = TVec<TInt>();
441  HigherDegNbrV[i].Reserve(NV[i].GetDeg());
442  HigherDegNbrV[i].Reduce(0);
443  }
444 
445 #ifdef USE_OPENMP
446 #pragma omp parallel for schedule(dynamic)
447 #endif
448  for (int i = 0; i < ind; i++) {
449  typename PGraph::TObj::TNodeI NI = NV[i];
450  MergeNbrs<PGraph>(HigherDegNbrV[i], NI);
451 
452  int k = 0;
453  for (int j = 0; j < HigherDegNbrV[i].Len(); j++) {
454  TInt Vert = HigherDegNbrV[i][j];
455  TInt Deg = NV[IndV[Vert]].GetDeg();
456  if (Deg > NI.GetDeg() ||
457  (Deg == NI.GetDeg() && Vert > NI.GetId())) {
458  HigherDegNbrV[i][k] = Vert;
459  k++;
460  }
461  }
462  HigherDegNbrV[i].Reduce(k);
463  }
464 
465  int64 cnt = 0;
466 #ifdef USE_OPENMP
467 #pragma omp parallel for schedule(dynamic) reduction(+:cnt)
468 #endif
469  for (int i = 0; i < HigherDegNbrV.Len(); i++) {
470  for (int j = 0; j < HigherDegNbrV[i].Len(); j++) {
471  TInt NbrInd = IndV[HigherDegNbrV[i][j]];
472 
473  int64 num = GetCommon(HigherDegNbrV[i], HigherDegNbrV[NbrInd]);
474  cnt += num;
475  }
476  }
477 
478  return cnt;
479 }
Definition: dt.h:1134
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

Here is the call graph for this function:

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.

References MakeUnDir().

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

Here is the call graph for this function:

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 734 of file triad.h.

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

Referenced by GetTriads().

734  {
735  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
736  NbrV.Reserve(NI.GetDeg());
737  NbrV.Reduce(0);
738 
739  int j = 0;
740  int k = 0;
741  int Prev = -1;
742  int InDeg = NI.GetInDeg();
743  int OutDeg = NI.GetOutDeg();
744  if (InDeg > 0 && OutDeg > 0) {
745  int v1 = NI.GetInNId(j);
746  int v2 = NI.GetOutNId(k);
747  while (1) {
748  if (v1 <= v2) {
749  if (Prev != v1) {
750  if (v1 != NId) {
751  NbrV.Add(v1);
752  Prev = v1;
753  }
754  }
755  j += 1;
756  if (j >= InDeg) {
757  break;
758  }
759  v1 = NI.GetInNId(j);
760  } else {
761  if (Prev != v2) {
762  if (v2 != NId) {
763  NbrV.Add(v2);
764  }
765  Prev = v2;
766  }
767  k += 1;
768  if (k >= OutDeg) {
769  break;
770  }
771  v2 = NI.GetOutNId(k);
772  }
773  }
774  }
775  while (j < InDeg) {
776  int v = NI.GetInNId(j);
777  if (Prev != v) {
778  if (v != NId) {
779  NbrV.Add(v);
780  }
781  Prev = v;
782  }
783  j += 1;
784  }
785  while (k < OutDeg) {
786  int v = NI.GetOutNId(k);
787  if (Prev != v) {
788  if (v != NId) {
789  NbrV.Add(v);
790  }
791  Prev = v;
792  }
793  k += 1;
794  }
795 }
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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THashSet< TKey, THashFunc >::AddKey(), TVec< TVal, TSizeTy >::Clr(), THashSet< TKey, THashFunc >::Clr(), THashSet< TKey, THashFunc >::Gen(), gfDirected, HasGraphFlag, and TVec< TVal, TSizeTy >::Sort().

Referenced by TSnap::TSnapDetail::_GirvanNewmanGetModularity(), Get1CnCom(), Get1CnComSzCnt(), TCliqueOverlap::GetCPMCommunities(), GetMxWcc(), GetMxWccSz(), ReebSimplify(), MotifCluster::SpectralCut(), and SummarizeConnectedComponents().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THashSet< TKey, THashFunc >::AddKey(), gfDirected, HasGraphFlag, and TVec< TVal, TSizeTy >::Sort().

Referenced by Get1CnComSzCnt(), PlotWccDistr(), and TGStat::TakeConnComp().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Clr(), TSStack< TVal >::Clr(), TQQueue< TVal >::Clr(), TSStack< TVal >::Empty(), TQQueue< TVal >::Empty(), THash< TKey, TDat, THashFunc >::GetDat(), gfDirected, TVec< TVal, TSizeTy >::Len(), TMath::Mn(), TMath::Mx(), TSStack< TVal >::Pop(), TQQueue< TVal >::Pop(), TSStack< TVal >::Push(), TQQueue< TVal >::Push(), TSStack< TVal >::Top(), and TQQueue< TVal >::Top().

Referenced by GetWeightedBetweennessCentr().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::DelLast(), GetWeightedBetweennessCentr(), TVec< TVal, TSizeTy >::Len(), TInt::Rnd, and TVec< TVal, TSizeTy >::Shuffle().

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:1143
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::DelLast(), GetWeightedBetweennessCentr(), TVec< TVal, TSizeTy >::Len(), TInt::Rnd, and TVec< TVal, TSizeTy >::Shuffle().

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:1143
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::DelLast(), GetWeightedBetweennessCentr(), TVec< TVal, TSizeTy >::Len(), TInt::Rnd, and TVec< TVal, TSizeTy >::Shuffle().

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:1143
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

Here is the call graph for this function:

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.

References GetWeightedFarnessCentr().

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

Here is the call graph for this function:

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.

References THashKeyDatI< TKey, TDat >::EndI, and GetWeightedShortestPath().

Referenced by GetWeightedClosenessCentr().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

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

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:1383
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Gen(), THash< TKey, TDat, THashFunc >::GetDat(), TNEANet::TNodeI::GetId(), TNEANet::TNodeI::GetInDeg(), TNEANet::TNodeI::GetInNId(), and TVec< TVal, TSizeTy >::Len().

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:1383
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Clr(), TVec< TVal, TSizeTy >::Empty(), findMinimum(), THash< TKey, TDat, THashFunc >::GetDat(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by GetWeightedFarnessCentr().

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[DstNId] > NIdDistH.GetDat(NId) + Attr[EId]) {
718  NIdDistH[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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TSnap::TPRManager::Capacity(), TQQueue< TVal >::Empty(), TSnap::TPRManager::Excess(), TSnap::TPRManager::Flow(), TNEANet::TNodeI::GetInDeg(), TNEANet::TNodeI::GetInEId(), TNEANet::TNodeI::GetInNId(), TSnap::TPRManager::GetMaxLabel(), TNEANet::TNodeI::GetOutDeg(), TNEANet::TNodeI::GetOutEId(), TNEANet::TNodeI::GetOutNId(), TSnap::TPRManager::IsActive(), TSnap::TPRManager::Label(), TQQueue< TVal >::Pop(), TQQueue< TVal >::Push(), TSnap::TPRManager::PushActive(), TSnap::TPRManager::RemoveActive(), TSnap::TPRManager::SetLabel(), and TQQueue< TVal >::Top().

Referenced by GetMaxFlowIntPR().

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:2645
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:2649
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:2647
void Push(const TVal &Val)
Definition: ds.h:2652

Here is the call graph for this function:

Here is the caller graph for this function:

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 337 of file cmty.cpp.

References TCnCom::Add(), TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TSnap::TSnapDetail::Equation(), THash< TKey, TDat, THashFunc >::GetDat(), TUNGraph::TNodeI::GetDeg(), TUNGraph::TNodeI::GetNbrNId(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TSnap::TSnapDetail::MapEquationNew2Modules(), TRnd::Randomize(), TVec< TVal, TSizeTy >::Shuffle(), and THash< TKey, TDat, THashFunc >::SortByDat().

337  {
338 
339  TIntFltH PAlpha; // probability of visiting node alpha
340  TIntH Module; // module of each node
341  TIntFltH Qi; // probability of leaving each module
342 
343  double SumPAlphaLogPAlpha = 0.0;
344  int Br = 0;
345  const int e = Graph->GetEdges();
346 
347  // initial values
348  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
349  int nodeId = NI.GetId();
350  int nodeDeg = NI.GetDeg();
351  float d = ((float)nodeDeg / (float)(2 * e));
352  PAlpha.AddDat(nodeId, d);
353  SumPAlphaLogPAlpha += d*log(d);
354  Module.AddDat(nodeId, Br);
355  Qi.AddDat(Br, 1.0);
356  Br += 1;
357  }
358 
359  double MinCodeLength = TSnapDetail::Equation(PAlpha, SumPAlphaLogPAlpha, Qi);
360  double NewCodeLength, PrevIterationCodeLength = 0.0;
361  int OldModule, NewModule;
362 
363  TIntV nodes;
364  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
365  nodes.Add(NI.GetId());
366 
367  do {
368  PrevIterationCodeLength = MinCodeLength;
369  TRnd rnd;
370  rnd.Randomize();
371  nodes.Shuffle(rnd);
372  for (int ndcounter = 0; ndcounter<nodes.Len(); ndcounter++) {
373  MinCodeLength = TSnapDetail::Equation(PAlpha, SumPAlphaLogPAlpha, Qi);
374  int nodeId = nodes[ndcounter];
375  TUNGraph::TNodeI NI = Graph->GetNI(nodeId);
376  for (int i = 0; i<NI.GetDeg(); i++) {
377 
378  OldModule = Module.GetDat(nodeId);
379  NewModule = Module.GetDat(NI.GetNbrNId(i));
380 
381  if (OldModule != NewModule){
382 
383  Module.AddDat(nodeId, NewModule);
384 
385  TSnapDetail::MapEquationNew2Modules(Graph, Module, Qi, OldModule, NewModule);
386  NewCodeLength = TSnapDetail::Equation(PAlpha, SumPAlphaLogPAlpha, Qi);
387  if (NewCodeLength<MinCodeLength) {
388  MinCodeLength = NewCodeLength;
389  OldModule = NewModule;
390  }
391  else {
392  Module.AddDat(nodeId, OldModule);
393  }
394  }
395  }
396  }
397  } while (MinCodeLength<PrevIterationCodeLength);
398 
399  Module.SortByDat(true);
400 
401  int Mod = -1;
402  for (int i = 0; i<Module.Len(); i++) {
403  if (Module[i]>Mod){
404  Mod = Module[i];
405  TCnCom t;
406  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
407  if (Module.GetDat(NI.GetId()) == Mod)
408  t.Add(NI.GetId());
409  }
410  CmtyV.Add(t);
411  }
412  }
413 
414  return MinCodeLength;
415 }
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

Here is the call graph for this function:

double TSnap::InfomapOnline ( PUNGraph Graph,
int  n1,
int  n2,
TIntFltH PAlpha,
double &  SumPAlphaLogPAlpha,
TIntFltH Qi,
TIntH Module,
int &  Br,
TCnComV CmtyV 
)

Definition at line 417 of file cmty.cpp.

References TCnCom::Add(), TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::GetDat(), TSnap::TSnapDetail::InfomapOnlineIncrement(), THash< TKey, TDat, THashFunc >::Len(), and THash< TKey, TDat, THashFunc >::SortByDat().

417  {
418 
419  double MinCodeLength = TSnapDetail::InfomapOnlineIncrement(Graph, n1, n2, PAlpha, SumPAlphaLogPAlpha, Qi, Module, Br);
420 
421  Module.SortByDat(true);
422 
423  int Mod = -1;
424  for (int i = 0; i<Module.Len(); i++) {
425  if (Module[i]>Mod){
426  Mod = Module[i];
427  TCnCom t;
428  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
429  if (Module.GetDat(NI.GetId()) == Mod)
430  t.Add(NI.GetId());
431  }
432  CmtyV.Add(t);
433  }
434  }
435 
436  return MinCodeLength;
437 }
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

Here is the call graph for this function:

int TSnap::Intersect ( TUNGraph::TNodeI  Node,
TIntH  NNodes 
)

Intersect.

Definition at line 584 of file centr.cpp.

References TUNGraph::TNodeI::GetDeg(), TUNGraph::TNodeI::GetId(), TUNGraph::TNodeI::GetNbrNId(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by FastCorePeripheryGC(), MaxCPGreedyBetter(), MaxCPGreedyBetter1(), MaxCPGreedyBetter2(), and MaxCPGreedyBetter3().

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

Here is the call graph for this function:

Here is the caller graph for this function:

int TSnap::Intersect ( TUNGraph::TNodeI  Node,
TStr  NNodes 
)

Intersect.

Definition at line 597 of file centr.cpp.

References TStr::CStr(), TUNGraph::TNodeI::GetDeg(), TUNGraph::TNodeI::GetId(), TUNGraph::TNodeI::GetNbrNId(), TInt::GetStr(), and TStr::IsStrIn().

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:1197
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
Definition: dt.h:1134
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:476
int GetId() const
Returns ID of the current node.
Definition: graph.h:88
bool IsStrIn(const TStr &Str) const
Definition: dt.h:554

Here is the call graph for this function:

int TSnap::Intersect ( TUNGraph::TNodeI  Node,
int *  NNodes,
int  NNodes_br 
)

Intersect.

Definition at line 621 of file centr.cpp.

References TUNGraph::TNodeI::GetDeg(), TUNGraph::TNodeI::GetId(), and TUNGraph::TNodeI::GetNbrNId().

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

Here is the call graph for this function:

int TSnap::Intersect1 ( TUNGraph::TNodeI  Node,
TStr  NNodes 
)

Definition at line 650 of file centr.cpp.

References TStr::CStr(), TUNGraph::TNodeI::GetDeg(), TUNGraph::TNodeI::GetId(), TUNGraph::TNodeI::GetNbrNId(), TInt::GetStr(), and TStr::SearchStr().

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:1197
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:1134
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:476
int GetId() const
Returns ID of the current node.
Definition: graph.h:88

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), TQQueue< TVal >::Empty(), TNEANet::TNodeI::GetInDeg(), TNEANet::TNodeI::GetInEId(), TNEANet::TNodeI::GetInNId(), TNEANet::TNodeI::GetOutDeg(), TNEANet::TNodeI::GetOutEId(), TNEANet::TNodeI::GetOutNId(), THash< TKey, TDat, THashFunc >::IsKey(), TQQueue< TVal >::Pop(), TQQueue< TVal >::Push(), and TQQueue< TVal >::Top().

Referenced by FindAugV().

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:2645
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:2649
int GetInDeg() const
Returns in-degree of the current node.
Definition: network.h:1811
const TVal & Top() const
Definition: ds.h:2647
void Push(const TVal &Val)
Definition: ds.h:2652
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

Here is the caller graph for this function:

bool TSnap::IsAllValVNeg ( TFltV ValV,
const bool &  InvertSign 
)

Definition at line 163 of file gsvd.cpp.

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

Referenced by GetEigVec(), and GetSngVec().

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

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
bool TSnap::IsConnected ( const PGraph &  Graph)

Tests whether the Graph is (weakly) connected.

Definition at line 305 of file cncom.h.

References IsWeaklyConn().

Referenced by IsTree().

305  {
306  return IsWeaklyConn(Graph);
307 }
bool IsWeaklyConn(const PGraph &Graph)
Tests whether the Graph is weakly connected.
Definition: cncom.h:310

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
bool TSnap::IsTree ( const PGraph &  Graph,
int &  RootNIdX 
)

Definition at line 460 of file alg.h.

References IsConnected().

Referenced by TGHash< TDat >::AddKey(), TGHash< TDat >::GetNodeMap(), GetTreeRootNId(), and TGHash< TDat >::IsGetKeyId().

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

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
bool TSnap::IsWeaklyConn ( const PGraph &  Graph)

Tests whether the Graph is weakly connected.

Definition at line 310 of file cncom.h.

References gfDirected, HasGraphFlag, and TSnapQueue< TVal >::Push().

Referenced by IsConnected().

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:201

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TSsParser::GetInt(), TSsParser::IsInt(), TSsParser::Len(), TSsParser::Next(), and ssfWhiteSep.

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

Here is the call graph for this function:

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.

References TStrHash< TDat, TStringPool, THashFunc >::AddDatId(), TSsParser::Len(), TSsParser::Next(), and ssfWhiteSep.

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

Here is the call graph for this function:

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.

References TCrossNet::AddEdge(), TCrossNet::AddFltAttrDatE(), TCrossNet::AddIntAttrDatE(), TCrossNet::AddStrAttrDatE(), Assert, atFlt, atInt, atStr, and TVec< TVal, TSizeTy >::Len().

Referenced by LoadCrossNetToNet().

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->GetStrVal(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:1134
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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References LoadCrossNet().

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

Here is the call graph for this function:

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.

References TNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TNGraph::AddNode(), TXmlLx::GetArg(), THashSet< TKey, THashFunc >::GetKeyId(), TXmlLx::GetSym(), IAssert, TNGraph::IsNode(), TZipIn::IsZipFNm(), TZipIn::New(), TFIn::New(), TNGraph::New(), TXmlLx::Sym, TXmlLx::TagNm, xspTruncate, xsyEof, and xsySTag.

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:477
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
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
static PSIn New(const TStr &FNm)
Definition: zipfl.cpp:122
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
Definition: xml.h:98
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:542
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TNGraph::AddEdge(), THashSet< TKey, THashFunc >::AddKey(), TNGraph::AddNode(), TXmlLx::GetArg(), THashSet< TKey, THashFunc >::GetKeyId(), TXmlLx::GetSym(), IAssert, TNGraph::IsNode(), TZipIn::IsZipFNm(), TZipIn::New(), TFIn::New(), TNGraph::New(), TXmlLx::Sym, TXmlLx::TagNm, xspTruncate, xsyEof, and xsySTag.

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:477
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
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
static PSIn New(const TStr &FNm)
Definition: zipfl.cpp:122
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
Definition: xml.h:98
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:542
int AddKey(const TKey &Key)
Definition: shash.h:1254
Definition: xml.h:93
Definition: dt.h:412
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

Here is the call graph for this function:

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.

References TSsParser::GetInt(), TSsParser::Next(), and ssfWhiteSep.

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

Here is the call graph for this function:

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.

References TSsParser::GetInt(), and TSsParser::Next().

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

Here is the call graph for this function:

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.

References EDGES_START, TSsParser::GetFld(), TSsParser::GetFlds(), TPt< TRec >::New(), TSsParser::Next(), NODES_START, ReadEdgeSchemaFromFile(), ReadEdgesFromFile(), ReadNodeSchemaFromFile(), and ReadNodesFromFile().

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

Here is the call graph for this function:

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.

References TStrHash< TDat, TStringPool, THashFunc >::AddKey(), Mega, TSsParser::Next(), and ssfWhiteSep.

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

Here is the call graph for this function:

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.

References TStrHash< TDat, TStringPool, THashFunc >::AddKey(), TSsParser::Next(), and ssfWhiteSep.

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

Here is the call graph for this function:

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.

References TNEANet::AddFltAttrDatN(), TNEANet::AddIntAttrDatN(), TNEANet::AddNode(), TNEANet::AddStrAttrDatN(), atFlt, atInt, atStr, TNEANet::IsNode(), and TVec< TVal, TSizeTy >::Len().

Referenced by LoadModeNetToNet().

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->GetStrVal(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:444
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:2664
int AddStrAttrDatN(const TNodeI &NodeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: network.h:2659
Definition: dt.h:1134
Definition: dt.h:412
Definition: gbase.h:23
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2298
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:2654

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References LoadMode().

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

Here is the call graph for this function:

TIntH TSnap::LoadNodeList ( TStr  InFNmNodes)

Definition at line 671 of file centr.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), TSsParser::GetInt(), TSsParser::Next(), and ssfWhiteSep.

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

Here is the call graph for this function:

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.

References EAssert, TSsParser::Eof(), TSsParser::GetInt(), TSsParser::IsInt(), TSsParser::Len(), TSsParser::Next(), ssfSpaceSep, and TSsParser::ToLc().

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() >= 3 && Ss.IsInt(0) && Ss.IsInt(1)) {
232  Graph->AddEdge(Ss.GetInt(0), Ss.GetInt(1)); }
233  } else {
234  // <source> <destination1> <destination2> <destination3> ...
235  const int SrcNId = Ss.GetInt(0);
236  for (int i = 1; i < Ss.Len(); i++) {
237  Graph->AddEdge(SrcNId, Ss.GetInt(i)); }
238  }
239  }
240  return Graph;
241 }
Definition: ss.h:72
Space separated.
Definition: ss.h:10
#define EAssert(Cond)
Definition: bd.h:280

Here is the call graph for this function:

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.

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

Referenced by GetUnDir().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), GetHits(), TVec< TVal, TSizeTy >::Len(), TVec< TVal, TSizeTy >::Reserve(), and TTable::TableFromHashMap().

Referenced by GetMapHitsIterator().

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:1134
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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References GetPageRank(), TVec< TVal, TSizeTy >::Len(), TVec< TVal, TSizeTy >::Reserve(), and TTable::TableFromHashMap().

Referenced by GetMapPageRank().

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:1134
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543

Here is the call graph for this function:

Here is the caller graph for this function:

TIntH TSnap::MaxCPGreedyBetter ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 175 of file centr.cpp.

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

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

Here is the call graph for this function:

TIntH TSnap::MaxCPGreedyBetter1 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 223 of file centr.cpp.

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

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

Here is the call graph for this function:

TIntH TSnap::MaxCPGreedyBetter2 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 268 of file centr.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::DelKey(), THash< TKey, TDat, THashFunc >::EndI(), TInt::GetStr(), Intersect(), and THash< TKey, TDat, THashFunc >::SortByDat().

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:1197
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:1134
Definition: dt.h:412
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

TIntH TSnap::MaxCPGreedyBetter3 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 322 of file centr.cpp.

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

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

Here is the call graph for this function:

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 482 of file triad.h.

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

482  {
483  int j = 0;
484  int k = 0;
485  int prev = -1;
486  int indeg = NI.GetInDeg();
487  int outdeg = NI.GetOutDeg();
488  if (indeg > 0 && outdeg > 0) {
489  int v1 = NI.GetInNId(j);
490  int v2 = NI.GetOutNId(k);
491  while (1) {
492  if (v1 <= v2) {
493  if (prev != v1) {
494  NeighbourV.Add(v1);
495  prev = v1;
496  }
497  j += 1;
498  if (j >= indeg) {
499  break;
500  }
501  v1 = NI.GetInNId(j);
502  } else {
503  if (prev != v2) {
504  NeighbourV.Add(v2);
505  prev = v2;
506  }
507  k += 1;
508  if (k >= outdeg) {
509  break;
510  }
511  v2 = NI.GetOutNId(k);
512  }
513  }
514  }
515  while (j < indeg) {
516  int v = NI.GetInNId(j);
517  if (prev != v) {
518  NeighbourV.Add(v);
519  prev = v;
520  }
521  j += 1;
522  }
523  while (k < outdeg) {
524  int v = NI.GetOutNId(k);
525  if (prev != v) {
526  NeighbourV.Add(v);
527  prev = v;
528  }
529  k += 1;
530  }
531 }
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::GetDat().

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

Here is the call graph for this function:

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.

References TGnuPlot::AddPlot(), TStr::CStr(), TStr::Empty(), TStr::Fmt(), GetClustCf(), gpsLog10XY, gpwLinesPoints, TGnuPlot::SavePng(), TGnuPlot::SetScale(), and TGnuPlot::SetXYLabel().

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:488
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:113
char * CStr()
Definition: dt.h:476
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), TStr::CStr(), TStr::Empty(), TVec< TVal, TSizeTy >::Empty(), TStr::Fmt(), GetEigVals(), THash< TKey, TDat, THashFunc >::GetKeyDatPrV(), gpsAuto, gpwLinesPoints, TVec< TVal, TSizeTy >::Last(), TVec< TVal, TSizeTy >::Len(), TGnuPlot::PlotValV(), TVec< TVal, TSizeTy >::Sort(), and TFlt::Val.

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:1385
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:488
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:476
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

Here is the call graph for this function:

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.

References TStr::CStr(), TStr::Empty(), TStr::Fmt(), GetEigVals(), gpsLog10XY, gpwLinesPoints, TGnuPlot::PlotValV(), and TVec< TVal, TSizeTy >::Sort().

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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TGnuPlot::AddPlot(), TSnap::TSnapDetail::CalcEffDiam(), TStr::CStr(), TStr::Empty(), TStr::Fmt(), GetAnf(), gpsLog10Y, gpwLinesPoints, TGnuPlot::SavePng(), TGnuPlot::SetScale(), and TGnuPlot::SetXYLabel().

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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Here is the call graph for this function:

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

References TStr::CStr(), TStr::Empty(), TStr::Fmt(), TGUtil::GetCCdf(), GetInDegCnt(), gpsLog10XY, gpwLinesPoints, TVec< TVal, TSizeTy >::Len(), and TGnuPlot::PlotValV().

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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TStr::CStr(), TStr::Empty(), TVec< TVal, TSizeTy >::Empty(), TStr::Fmt(), GetInvParticipRat(), gpsLog10Y, gpwPoints, TVec< TVal, TSizeTy >::Last(), TVec< TVal, TSizeTy >::Len(), and TGnuPlot::PlotValV().

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:377
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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TStr::CStr(), TStr::Empty(), TStr::Fmt(), GetKCoreEdges(), gpsLog10Y, gpwLinesPoints, and TGnuPlot::PlotValV().

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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TStr::CStr(), TStr::Empty(), TStr::Fmt(), GetKCoreNodes(), gpsLog10Y, gpwLinesPoints, and TGnuPlot::PlotValV().

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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TStr::CStr(), TStr::Empty(), TStr::Fmt(), TGUtil::GetCCdf(), GetOutDegCnt(), gpsLog10XY, gpwLinesPoints, TVec< TVal, TSizeTy >::Len(), and TGnuPlot::PlotValV().

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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TGnuPlot::AddPlot(), TStr::CStr(), TStr::Empty(), TStr::Fmt(), GetSccSzCnt(), gpsLog10XY, gpwLinesPoints, TVec< TVal, TSizeTy >::Last(), and TPair< TVal1, TVal2 >::Val1.

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:488
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:476

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TSnap::TSnapDetail::CalcAvgDiamPdf(), TSnap::TSnapDetail::CalcEffDiamPdf(), TStr::CStr(), TBreathFS< PGraph >::DoBfs(), TStr::Empty(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::GetKey(), gpsLog10Y, gpwLinesPoints, TVec< TVal, TSizeTy >::Last(), THash< TKey, TDat, THashFunc >::Len(), TMath::Mn(), TInt::Mx, TBreathFS< PGraph >::NIdDistH, TGnuPlot::PlotValV(), TInt::Rnd, TVec< TVal, TSizeTy >::Shuffle(), and THash< TKey, TDat, THashFunc >::SortByKey().

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:1139
static TRnd Rnd
Definition: dt.h:1143
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:488
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:476
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

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), TStr::CStr(), TStr::Empty(), TVec< TVal, TSizeTy >::Empty(), TStr::Fmt(), TNGraph::GetEdges(), THash< TKey, TDat, THashFunc >::GetKeyDatPrV(), TNGraph::GetNodes(), GetSngVals(), gpsAuto, gpwLinesPoints, TVec< TVal, TSizeTy >::Last(), TVec< TVal, TSizeTy >::Len(), TGnuPlot::PlotValV(), TVec< TVal, TSizeTy >::Sort(), and TFlt::Val.

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 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
double Val
Definition: dt.h:1385
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
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:488
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:476
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

Here is the call graph for this function:

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.

References TStr::CStr(), TStr::Empty(), TStr::Fmt(), TNGraph::GetEdges(), TNGraph::GetNodes(), GetSngVals(), gpsLog10XY, gpwLinesPoints, TGnuPlot::PlotValV(), and TVec< TVal, TSizeTy >::Sort().

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 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TStr::CStr(), TStr::Empty(), TStr::Fmt(), TNGraph::GetEdges(), TNGraph::GetNodes(), GetSngVec(), gpsLog10XY, gpwLinesPoints, TGUtil::MakeExpBins(), TGnuPlot::PlotValV(), and TVec< TVal, TSizeTy >::Sort().

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 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
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:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * CStr()
Definition: dt.h:476
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

Here is the call graph for this function:

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.

References TGnuPlot::AddPlot(), TStr::CStr(), TStr::Empty(), TStr::Fmt(), GetWccSzCnt(), gpsLog10XY, gpwLinesPoints, TVec< TVal, TSizeTy >::Last(), and TPair< TVal1, TVal2 >::Val1.

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:488
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:476
void GetWccSzCnt(const PGraph &Graph, TIntPrV &WccSzCnt)
Returns a distribution of weakly connected component sizes.
Definition: cncom.h:337

Here is the call graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddKey(), TStr::CStr(), edge, TStr::Empty(), GetBfsEffDiam(), GetFlagStr(), TInt::GetMn(), TInt::GetMx(), GetMxSccSz(), GetMxWccSz(), TUInt64::GetStr(), GetTriads(), gfMx, gfUndef, HasGraphFlag, and THash< TKey, TDat, THashFunc >::Len().

Referenced by main(), and TKronMomentsFit::Test().

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:207
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:415
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:1182
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:1180
#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:1360
long long int64
Definition: bd.h:27
bool Empty() const
Definition: dt.h:488
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:476
int Len() const
Definition: hash.h:228

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TSnap::TPRManager::Capacity(), TSnap::TPRManager::EdgeNum(), TSnap::TPRManager::Flow(), TNEANet::TNodeI::GetDeg(), TNEANet::TNodeI::GetInDeg(), TNEANet::TNodeI::GetInEId(), TNEANet::TNodeI::GetInNId(), TNEANet::TNodeI::GetOutEId(), TNEANet::TNodeI::GetOutNId(), TSnap::TPRManager::Label(), PushToInNbr(), PushToOutNbr(), and Relabel().

Referenced by GetMaxFlowIntPR().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TSnap::TPRManager::Excess(), TSnap::TPRManager::Flow(), and MIN.

Referenced by PushRelabel().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TSnap::TPRManager::Capacity(), TSnap::TPRManager::Excess(), TSnap::TPRManager::Flow(), and MIN.

Referenced by PushRelabel().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THash< TKey, TDat, THashFunc >::AddDat(), DST_ID_NAME, EDGES_START, FLT_TYPE_PREFIX, TSsParser::GetFld(), TSsParser::GetFlds(), INT_TYPE_PREFIX, TStr::SplitOnCh(), SRC_ID_NAME, and STR_TYPE_PREFIX.

Referenced by LoadEdgeListNet().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THash< TKey, TDat, THashFunc >::BegI(), EDGES_START, END_SENTINEL, THash< TKey, TDat, THashFunc >::EndI(), TSsParser::GetFld(), TSsParser::GetFlds(), TSsParser::GetFlt(), TSsParser::GetInt(), TSsParser::Next(), NODES_START, and NULL_VAL.

Referenced by LoadEdgeListNet().

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

Here is the call graph for this function:

Here is the caller graph for this function:

int TSnap::ReadNodeSchemaFromFile ( TSsParser Ss,
const char &  Separator,
int &  NId,
TStrIntH IntAttrNVals,
TStrIntH FltAttrNVals,
TStrIntH StrAttrNVals 
)

Definition at line 77 of file gio.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), FLT_TYPE_PREFIX, TSsParser::GetFld(), TSsParser::GetFlds(), INT_TYPE_PREFIX, NID_NAME, NODES_START, TStr::SplitOnCh(), and STR_TYPE_PREFIX.

Referenced by LoadEdgeListNet().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References THash< TKey, TDat, THashFunc >::BegI(), EDGES_START, END_SENTINEL, THash< TKey, TDat, THashFunc >::EndI(), TSsParser::GetFld(), TSsParser::GetFlds(), TSsParser::GetFlt(), TSsParser::GetInt(), TSsParser::Next(), NODES_START, and NULL_VAL.

Referenced by LoadEdgeListNet().

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

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::ReebRefine ( PNGraph Graph,
TIntH t,
int  e,
PNGraph gFinal,
TIntH tFinal,
bool  collapse 
)

Definition at line 982 of file cmty.cpp.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TNGraph::AddEdge(), TNGraph::AddNode(), THash< TKey, TDat, THashFunc >::BegI(), TNGraph::BegNI(), TVec< TVal, TSizeTy >::Clr(), TNGraph::DelEdge(), THash< TKey, TDat, THashFunc >::DelKey(), TNGraph::DelNode(), THash< TKey, TDat, THashFunc >::EndI(), TNGraph::EndNI(), THashKeyDatI< TKey, TDat >::GetDat(), THash< TKey, TDat, THashFunc >::GetDat(), TVec< TVal, TSizeTy >::GetDat(), TNGraph::TNodeI::GetInDeg(), TNGraph::TNodeI::GetInNId(), THashKeyDatI< TKey, TDat >::GetKey(), TNGraph::GetNI(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), THashKeyDatI< TKey, TDat >::IsEnd(), THash< TKey, TDat, THashFunc >::IsKey(), TNGraph::IsNode(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TNGraph::New(), and TSnap::TSnapDetail::vectorIntersect().

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

Here is the call graph for this function:

void TSnap::ReebSimplify ( PNGraph Graph,
TIntH t,
int  e,
PNGraph gFinal,
TIntH tFinal,
bool  collapse 
)

Definition at line 842 of file cmty.cpp.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TNGraph::AddEdge(), TNGraph::AddNode(), THash< TKey, TDat, THashFunc >::BegI(), TNGraph::BegNI(), TNGraph::DelEdge(), THash< TKey, TDat, THashFunc >::DelKey(), TNGraph::DelNode(), TSnap::TSnapDetail::edgeIntersect(), THash< TKey, TDat, THashFunc >::EndI(), TNGraph::EndNI(), THashKeyDatI< TKey, TDat >::GetDat(), THash< TKey, TDat, THashFunc >::GetDat(), TVec< TVal, TSizeTy >::GetDat(), TNGraph::TNodeI::GetInDeg(), TNGraph::TNodeI::GetInNId(), THashKeyDatI< TKey, TDat >::GetKey(), TNGraph::GetNI(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), GetWccs(), TVec< TVal, TSizeTy >::IntrsLen(), THashKeyDatI< TKey, TDat >::IsEnd(), TNGraph::IsNode(), TVec< TVal, TSizeTy >::Len(), and TNGraph::New().

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

Here is the call graph for this function:

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.

References TSnap::TPRManager::Capacity(), TSnap::TPRManager::Flow(), TNEANet::TNodeI::GetInDeg(), TNEANet::TNodeI::GetInEId(), TNEANet::TNodeI::GetInNId(), TSnap::TPRManager::GetMaxLabel(), TNEANet::TNodeI::GetOutDeg(), TNEANet::TNodeI::GetOutEId(), TNEANet::TNodeI::GetOutNId(), TSnap::TPRManager::Label(), MIN, and TSnap::TPRManager::SetLabel().

Referenced by PushRelabel().

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

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PGraph >
int TSnap::SamplePersonalizedPageRank ( const PGraph &  Graph,
double  JumpProb,
const TIntV StartNIdV,
TRnd Rnd 
)

Definition at line 67 of file randwalk.h.

References TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), TVec< TVal, TSizeTy >::GetRndVal(), TRnd::GetUniDev(), and TRnd::GetUniDevInt().

Referenced by GetPersonalizedPageRankBidirectional().

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:402
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
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:412

Here is the call graph for this function:

Here is the caller graph for this function:

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 244 of file gio.h.

References TStr::CStr(), TStr::Empty(), gfDirected, and HasGraphFlag.

Referenced by main().

244  {
245  FILE *F = fopen(OutFNm.CStr(), "wt");
246  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) { fprintf(F, "# Directed graph: %s \n", OutFNm.CStr()); }
247  else { fprintf(F, "# Undirected graph (each unordered pair of nodes is saved once): %s\n", OutFNm.CStr()); }
248  if (! Desc.Empty()) { fprintf(F, "# %s\n", Desc.CStr()); }
249  fprintf(F, "# Nodes: %d Edges: %d\n", Graph->GetNodes(), Graph->GetEdges());
250  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) { fprintf(F, "# FromNodeId\tToNodeId\n"); }
251  else { fprintf(F, "# NodeId\tNodeId\n"); }
252  for (typename PGraph::TObj::TEdgeI ei = Graph->BegEI(); ei < Graph->EndEI(); ei++) {
253  fprintf(F, "%d\t%d\n", ei.GetSrcNId(), ei.GetDstNId());
254  }
255  fclose(F);
256 }
#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:488
char * CStr()
Definition: dt.h:476

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TStr::CStr(), TStr::Empty(), END_SENTINEL, WriteEdgeSchemaToFile(), WriteEdgesToFile(), WriteNodeSchemaToFile(), and WriteNodesToFile().

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:488
void WriteNodesToFile(FILE *F, const PNEANet &Graph, TStrV &IntAttrNNames, TStrV &FltAttrNNames, TStrV &StrAttrNNames)
Definition: gio.cpp:188
char * CStr()
Definition: dt.h:476
void WriteNodeSchemaToFile(FILE *F, TStrV &IntAttrNNames, TStrV &FltAttrNNames, TStrV &StrAttrNNames)
Definition: gio.cpp:171

Here is the call graph for this function:

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 386 of file gio.h.

References TStr::CStr(), TStr::Empty(), THash< TKey, TDat, THashFunc >::GetDat(), gfDirected, HasGraphFlag, and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by DrawGViz().

386  {
387  const bool IsDir = HasGraphFlag(typename PGraph::TObj, gfDirected);
388  FILE *F = fopen(OutFNm.CStr(), "wt");
389  if (! Desc.Empty()) fprintf(F, "/*****\n%s\n*****/\n\n", Desc.CStr());
390  if (IsDir) { fprintf(F, "digraph G {\n"); } else { fprintf(F, "graph G {\n"); }
391  fprintf(F, " graph [splines=false overlap=false]\n"); //size=\"12,10\" ratio=fill
392  // node [width=0.3, height=0.3, label=\"\", style=filled, color=black]
393  // node [shape=box, width=0.3, height=0.3, label=\"\", style=filled, fillcolor=red]
394  fprintf(F, " node [shape=ellipse, width=0.3, height=0.3%s]\n", NodeLabels?"":", label=\"\"");
395  // node colors
396  //for (int i = 0; i < NIdColorH.Len(); i++) {
397  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
398  if (NIdColorH.IsKey(NI.GetId())) {
399  fprintf(F, " %d [style=filled, fillcolor=\"%s\"];\n", NI.GetId(), NIdColorH.GetDat(NI.GetId()).CStr()); }
400  else {
401  fprintf(F, " %d ;\n", NI.GetId());
402  }
403  }
404  // edges
405  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
406  if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 && !NIdColorH.IsKey(NI.GetId())) {
407  fprintf(F, "%d;\n", NI.GetId()); }
408  else {
409  for (int e = 0; e < NI.GetOutDeg(); e++) {
410  if (! IsDir && NI.GetId() > NI.GetOutNId(e)) { continue; }
411  fprintf(F, " %d %s %d;\n", NI.GetId(), IsDir?"->":"--", NI.GetOutNId(e));
412  }
413  }
414  }
415  if (! Desc.Empty()) {
416  fprintf(F, " label = \"\\n%s\\n\";", Desc.CStr());
417  fprintf(F, " fontsize=24;\n");
418  }
419  fprintf(F, "}\n");
420  fclose(F);
421 }
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:488
char * CStr()
Definition: dt.h:476
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

Here is the caller graph for this function:

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 424 of file gio.h.

References TStr::CStr(), TStr::Empty(), THash< TKey, TDat, THashFunc >::GetDat(), gfDirected, and THash< TKey, TDat, THashFunc >::IsKey().

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

Here is the call graph for this function:

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 368 of file gio.h.

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

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

Here is the call graph for this function:

template<class PGraph >
void TSnap::SavePajek ( const PGraph &  Graph,
const TStr OutFNm 
)

Saves a graph in a Pajek .NET format.

Definition at line 259 of file gio.h.

References THash< TKey, TDat, THashFunc >::AddDat(), TStr::CStr(), gfDirected, and HasGraphFlag.

259  {
260  TIntH NIdToIdH(Graph->GetNodes(), true);
261  FILE *F = fopen(OutFNm.CStr(), "wt");
262  fprintf(F, "*Vertices %d\n", Graph->GetNodes());
263  int i = 0;
264  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
265  fprintf(F, "%d \"%d\" ic Red fos 10\n", i+1, NI.GetId()); // ic: internal color, fos: font size
266  NIdToIdH.AddDat(NI.GetId(), i+1);
267  }
268  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
269  fprintf(F, "*Arcs %d\n", Graph->GetEdges()); } // arcs are directed, edges are undirected
270  else {
271  fprintf(F, "*Edges %d\n", Graph->GetEdges());
272  }
273  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
274  const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
275  const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
276  fprintf(F, "%d %d %d c Black\n", SrcNId, DstNId, 1); // width=1
277  }
278  fclose(F);
279 }
#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:476
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

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 284 of file gio.h.

References THash< TKey, TDat, THashFunc >::AddDat(), TStr::CStr(), THash< TKey, TDat, THashFunc >::GetDat(), gfDirected, HasGraphFlag, and THash< TKey, TDat, THashFunc >::IsKey().

284  {
285  TIntH NIdToIdH(Graph->GetNodes(), true);
286  FILE *F = fopen(OutFNm.CStr(), "wt");
287  fprintf(F, "*Vertices %d\n", Graph->GetNodes());
288  int i = 0;
289  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
290  fprintf(F, "%d \"%d\" ic %s fos 10\n", i+1, NI.GetId(),
291  NIdColorH.IsKey(NI.GetId()) ? NIdColorH.GetDat(NI.GetId()).CStr() : "Red");
292  NIdToIdH.AddDat(NI.GetId(), i+1);
293  }
294  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
295  fprintf(F, "*Arcs %d\n", Graph->GetEdges()); } // arcs are directed, edges are undirected
296  else {
297  fprintf(F, "*Edges %d\n", Graph->GetEdges());
298  }
299  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
300  const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
301  const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
302  fprintf(F, "%d %d %d c Black\n", SrcNId, DstNId, 1);
303  }
304  fclose(F);
305 }
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:476
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

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 311 of file gio.h.

References THash< TKey, TDat, THashFunc >::AddDat(), TStr::CStr(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::GetDat(), gfDirected, HasGraphFlag, and THash< TKey, TDat, THashFunc >::IsKey().

311  {
312  TIntH NIdToIdH(Graph->GetNodes(), true);
313  FILE *F = fopen(OutFNm.CStr(), "wt");
314  fprintf(F, "*Vertices %d\n", Graph->GetNodes());
315  int i = 0;
316  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
317  fprintf(F, "%d \"%s\" ic %s fos 10\n", i+1,
318  NIdLabelH.IsKey(NI.GetId()) ? NIdLabelH.GetDat(NI.GetId()).CStr() : TStr::Fmt("%d", NI.GetId()).CStr(),
319  NIdColorH.IsKey(NI.GetId()) ? NIdColorH.GetDat(NI.GetId()).CStr() : "Red");
320  NIdToIdH.AddDat(NI.GetId(), i+1);
321  }
322  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
323  fprintf(F, "*Arcs %d\n", Graph->GetEdges()); } // arcs are directed, edges are undirected
324  else {
325  fprintf(F, "*Edges %d\n", Graph->GetEdges());
326  }
327  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
328  const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
329  const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
330  fprintf(F, "%d %d %d c Black\n", SrcNId, DstNId, 1);
331  }
332  fclose(F);
333 }
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:476
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

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 340 of file gio.h.

References THash< TKey, TDat, THashFunc >::AddDat(), CAssert, TStr::CStr(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::GetDat(), gfDirected, gfMultiGraph, HasGraphFlag, and THash< TKey, TDat, THashFunc >::IsKey().

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

Here is the call graph for this function:

void TSnap::SetAllInvertSign ( TFltV ValV,
const double &  Val 
)

Definition at line 158 of file gsvd.cpp.

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

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

Here is the call graph for this function:

template<class PGraph >
void TSnap::TestAnf ( )

Definition at line 240 of file anf.h.

References TVec< TVal, TSizeTy >::Add(), TGraphAnf< PGraph >::GetGraphAnf(), TMom::GetMean(), TMom::GetSDev(), TVec< TVal, TSizeTy >::Last(), and TVec< TVal, TSizeTy >::Len().

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

Here is the call graph for this function:

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.

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

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

Here is the call graph for this function:

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.

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

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

Here is the call graph for this function:

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.

References Assert, atFlt, and atInt.

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:1383
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1134
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.

References TVec< TVal, TSizeTy >::Add(), Assert, atInt, atStr, TVec< TVal, TSizeTy >::CopyUniqueFrom(), TRowIterator::GetIntAttr(), TRowIterator::GetRowIdx(), TRowIterator::GetStrMapById(), TVec< TVal, TSizeTy >::GetVal(), TVec< TVal, TSizeTy >::Len(), TNGraphMP::New(), TTable::QSortKeyVal(), and TVec< TVal, TSizeTy >::Reserve().

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:1134
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

Here is the call graph for this function:

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.

References Assert, atInt, atStr, TPt< TRec >::Clr(), TVec< TVal, TSizeTy >::Clr(), TVec< TVal, TSizeTy >::Gen(), TInt::GetPrimHashCd(), and TNGraphMP::New().

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:1168
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:1134
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

Here is the call graph for this function:

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.

References Assert, atFlt, atInt, atStr, THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::EndI(), THash< TKey, TDat, THashFunc >::GetDat(), THash< TKey, TDat, THashFunc >::IsKey(), and TVec< TVal, TSizeTy >::Len().

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->GetStrVal(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:1383
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1134
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

Here is the call graph for this function:

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.

References Assert, atFlt, atInt, atStr, and TVec< TVal, TSizeTy >::Len().

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->GetStrVal(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:1383
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1134
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
Definition: gbase.h:23

Here is the call graph for this function:

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.

References Assert, atFlt, atInt, atStr, and TVec< TVal, TSizeTy >::Len().

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->GetStrVal(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->GetStrVal(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:1383
#define Assert(Cond)
Definition: bd.h:251
Definition: dt.h:1134
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
Definition: gbase.h:23

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TStopwatch::AddEdges, TStopwatch::AddNeighborhoods, TStopwatch::AllocateColumnCopies, Assert, atFlt, atInt, atStr, THash< TKey, TDat, THashFunc >::BegI(), TStopwatch::CopyColumns, TVec< TVal, TSizeTy >::CopyUniqueFrom(), THash< TKey, TDat, THashFunc >::EndI(), THash< TKey, TDat, THashFunc >::GetDat(), TStopwatch::GetInstance(), TRowIterator::GetIntAttr(), TRowIterator::GetRowIdx(), TRowIterator::GetStrMapById(), TVec< TVal, TSizeTy >::GetVal(), TStopwatch::Group, THash< TKey, TDat, THashFunc >::IsKey(), TVec< TVal, TSizeTy >::Len(), TStopwatch::MergeNeighborhoods, TTable::QSortKeyVal(), TVec< TVal, TSizeTy >::Reserve(), TStopwatch::Sort, TStopwatch::Start(), and TStopwatch::Stop().

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->GetStrVal(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->GetStrVal(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:1383
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:1134
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TStopwatch::AddEdges, TStopwatch::AddNeighborhoods, TStopwatch::AllocateColumnCopies, Assert, atFlt, atInt, atStr, TStopwatch::CopyColumns, TVec< TVal, TSizeTy >::CopyUniqueFrom(), TStopwatch::GetInstance(), TRowIterator::GetIntAttr(), TRowIterator::GetRowIdx(), TRowIterator::GetStrMapById(), TVec< TVal, TSizeTy >::GetVal(), TStopwatch::Group, TVec< TVal, TSizeTy >::Len(), TStopwatch::MergeNeighborhoods, TTable::QSortKeyVal(), TVec< TVal, TSizeTy >::Reserve(), TStopwatch::Sort, TStopwatch::Start(), and TStopwatch::Stop().

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->GetStrVal(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:1134
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TStopwatch::AddEdges, TStopwatch::AddNeighborhoods, TStopwatch::AllocateColumnCopies, Assert, atFlt, atInt, atStr, TStopwatch::CopyColumns, TVec< TVal, TSizeTy >::CopyUniqueFrom(), TStopwatch::GetInstance(), TRowIterator::GetIntAttr(), TRowIterator::GetRowIdx(), TRowIterator::GetStrMapById(), TVec< TVal, TSizeTy >::GetVal(), TStopwatch::Group, TVec< TVal, TSizeTy >::Len(), TStopwatch::MergeNeighborhoods, TTable::QSortKeyVal(), TVec< TVal, TSizeTy >::Reserve(), TStopwatch::Sort, TStopwatch::Start(), and TStopwatch::Stop().

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->GetStrVal(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->GetStrVal(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:1134
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

Here is the call graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TStopwatch::AddEdges, TStopwatch::AddNeighborhoods, TStopwatch::AllocateColumnCopies, Assert, atInt, atStr, TStopwatch::CopyColumns, TVec< TVal, TSizeTy >::CopyUniqueFrom(), TStopwatch::GetInstance(), TRowIterator::GetIntAttr(), TRowIterator::GetRowIdx(), TRowIterator::GetStrMapById(), TVec< TVal, TSizeTy >::GetVal(), TStopwatch::Group, TVec< TVal, TSizeTy >::Len(), TStopwatch::MergeNeighborhoods, TTable::QSortKeyVal(), TVec< TVal, TSizeTy >::Reserve(), TStopwatch::Sort, TStopwatch::Start(), TStopwatch::Stop(), and TInt::Val.

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:1136
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:1134
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

Here is the call graph for this function:

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.

References TStr::CStr(), DST_ID_NAME, EDGES_START, FLT_TYPE_PREFIX, INT_TYPE_PREFIX, TVec< TVal, TSizeTy >::Len(), SRC_ID_NAME, and STR_TYPE_PREFIX.

Referenced by SaveEdgeListNet().

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:476
const TStr SRC_ID_NAME
Definition: gio.h:9

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::WriteEdgesToFile ( FILE *  F,
const PNEANet Graph,
TStrV IntAttrENames,
TStrV FltAttrENames,
TStrV StrAttrENames 
)

Definition at line 238 of file gio.cpp.

References TStr::CStr(), TVec< TVal, TSizeTy >::Len(), and NULL_VAL.

Referenced by SaveEdgeListNet().

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:476

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::WriteNodeSchemaToFile ( FILE *  F,
TStrV IntAttrNNames,
TStrV FltAttrNNames,
TStrV StrAttrNNames 
)

Definition at line 171 of file gio.cpp.

References TStr::CStr(), FLT_TYPE_PREFIX, INT_TYPE_PREFIX, TVec< TVal, TSizeTy >::Len(), NID_NAME, NODES_START, and STR_TYPE_PREFIX.

Referenced by SaveEdgeListNet().

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:476

Here is the call graph for this function:

Here is the caller graph for this function:

void TSnap::WriteNodesToFile ( FILE *  F,
const PNEANet Graph,
TStrV IntAttrNNames,
TStrV FltAttrNNames,
TStrV StrAttrNNames 
)

Definition at line 188 of file gio.cpp.

References TStr::CStr(), TVec< TVal, TSizeTy >::Len(), and NULL_VAL.

Referenced by SaveEdgeListNet().

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:476

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

const TStr TSnap::CapAttrName = "capacity"

Definition at line 4 of file flow.h.

Referenced by GetMaxFlowIntEK(), and TSnap::TPRManager::TPRManager().

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

Definition at line 10 of file gio.h.

Referenced by ReadEdgeSchemaFromFile(), and WriteEdgeSchemaToFile().

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

Definition at line 8 of file gio.h.

Referenced by ReadEdgesFromFile(), ReadNodesFromFile(), and SaveEdgeListNet().

const TStr TSnap::FLT_TYPE_PREFIX = ("Flt")
const TStr TSnap::INT_TYPE_PREFIX = ("Int")
const TStr TSnap::NID_NAME = ("NId")

Definition at line 11 of file gio.h.

Referenced by ReadNodeSchemaFromFile(), and WriteNodeSchemaToFile().

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

Definition at line 15 of file gio.h.

Referenced by ReadEdgesFromFile(), ReadNodesFromFile(), WriteEdgesToFile(), and WriteNodesToFile().

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

Definition at line 9 of file gio.h.

Referenced by ReadEdgeSchemaFromFile(), and WriteEdgeSchemaToFile().

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