SNAP Library 3.0, User Reference  2016-07-20 17:56:49
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)<. More...
 
template<class PGraph >
int CntUniqDirEdges (const PGraph &Graph)
 Counts unique directed edges in the graph Graph. Nodes (u,v)<. More...
 
template<class PGraph >
int CntUniqBiDirEdges (const PGraph &Graph)
 Counts unique bidirectional edges in the graph Graph. Edge is bidirectional is there exist directed edges in both directions: (u,v) and (v,u) More...
 
template<class PGraph >
int CntSelfEdges (const PGraph &Graph)
 Counts the number fo 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 bool &IsDir, const TFltV &Attr, const bool &Normalized)
 
double GetWeightedClosenessCentr (const PNEANet Graph, const int &NId, const bool &IsDir, const TFltV &Attr, const bool &Normalized)
 
void GetWeightedBetweennessCentr (const PNEANet Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &IsDir, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent, const TFltV &Attr)
 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 bool &IsDir, const double &NodeFrac)
 
void GetWeightedBetweennessCentr (const PNEANet Graph, TIntFltH &NodeBtwH, const TFltV &Attr, const bool &IsDir, const double &NodeFrac)
 
void GetWeightedBetweennessCentr (const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const bool &IsDir, const double &NodeFrac)
 
template<class PGraph >
double GetFarnessCentr (const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)
 
template<class PGraph >
double GetFarnessCentrMP (const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)
 
template<class PGraph >
double GetClosenessCentr (const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)
 
template<class PGraph >
double GetClosenessCentrMP (const PGraph &Graph, const int &NId, const bool &IsDir, const bool &Normalized=true)
 
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 bool &IsDir=false, const double &NodeFrac=1.0)
 
template<class PGraph >
void GetBetweennessCentr (const PGraph &Graph, TIntPrFltH &EdgeBtwH, const bool &IsDir=false, const double &NodeFrac=1.0)
 
template<class PGraph >
void GetBetweennessCentr (const PGraph &Graph, TIntFltH &NIdBtwH, TIntPrFltH &EdgeBtwH, const bool &IsDir=false, const double &NodeFrac=1.0)
 
template<class PGraph >
void GetBetweennessCentr (const PGraph &Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &IsDir, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent)
 
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)
 
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 &SrcAttrs, TStrV &DstAttrs, TStrV &EdgeAttrs, TAttrAggr AggrPolicy)
 Converts table to a network. Suitable for PNEANet - Requires node and edge attribute column names as vectors. 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...
 
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...
 
void GetMergeSortedV (TIntV &NeighbourV, TNGraph::TNodeI NI)
 
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 CountTriangles (const PGraph &Graph)
 Returns the number of triangles in graph Graph, original version. More...
 
template<class PGraph >
int64 GetTriangleCnt (const PGraph &Graph)
 Returns the number of triangles in graph Graph, newer version. More...
 
template<class PGraph >
void MergeNbrs (TIntV &NeighbourV, const typename PGraph::TObj::TNodeI &NI)
 Merges 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.

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:547
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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 }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
double TSnap::BorgattiEverettMeasure ( PUNGraph Graph,
TIntIntH out,
double  coresize,
int  type 
)

Definition at line 186 of file coreper.cpp.

186  {
187 
188  double sum = 0.0;
189  for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++){ // Calculate and store the degrees of each node.
190  int i = EI.GetSrcNId();
191  int j = EI.GetDstNId();
192  if (type == 1) {
193  if (out.GetDat(i) == 1 || out.GetDat(j) == 1)
194  sum += 1;
195  }
196  else {
197  if (out.GetDat(i) == 1 && out.GetDat(j) == 1)
198  sum += 1;
199  }
200  }
201 
202  return sum/(((coresize*coresize)-coresize)/2);
203  }
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:117
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:241
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:239
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 129 of file casc.cpp.

129  {
130  // Attribute to Int mapping
131  TInt SIdx = P->GetColIdx(C1);
132  TInt DIdx = P->GetColIdx(C2);
133  TInt StIdx = P->GetColIdx(C3);
134  TInt DuIdx = P->GetColIdx(C4);
135  TIntV MapV, PhyV;
136  TStrV SortBy;
137  SortBy.Add(C3);
138  P->Order(SortBy);
139  int count = 0;
140  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
141  MapV.Add(RI.GetRowIdx());
142  PhyV.Add(count++);
143  }
144  // After sort attach with each row a rank helpful for sorting
145  P->StoreIntCol("Physical",PhyV);
146  TInt PIdx = P->GetColIdx("Physical");
147  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
148  // Check for top cascades
149  if (NI.GetInDeg() != 0) { continue;}
150  TIntV CurCasc;
151  TSnapQueue<TInt> EventQ;
152  THashSet<TInt> VisitedH;
153  TInt NId = NI.GetId();
154  EventQ.Push(NId);
155  VisitedH.AddKey(NId);
156  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,NId));
157  while (! EventQ.Empty()) {
158  TNGraph::TNodeI CNI = Graph->GetNI(EventQ.Top().Val); //Get Current Node
159  EventQ.Pop();
160  // Go over the outdegree nodes of the currernt node
161  for (int e = 0; e < CNI.GetOutDeg(); e++) {
162  TInt CId = CNI.GetOutNId(e);
163  if ( !VisitedH.IsKey(CId)) {
164  EventQ.Push(CId);
165  VisitedH.AddKey(CId);
166  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,CId));
167  }
168  }
169  }
170  CurCasc.Sort();
171  TIntV ToAddV;
172  if (Print && VisitedH.Len() > 1) {
173  printf("__casacade__\t%d\n",VisitedH.Len());
174  }
175  for (TIntV::TIter VI = CurCasc.BegI(); VI < CurCasc.EndI(); VI++) {
176  ToAddV.Add(MapV.GetVal(VI->Val));
177  if (Print && VisitedH.Len() > 1) {
178  int PIdx = MapV.GetVal(VI->Val).Val;
179  int PSource = P->GetIntValAtRowIdx(SIdx,PIdx).Val;
180  int PDest = P->GetIntValAtRowIdx(DIdx,PIdx).Val;
181  int PStart = P->GetIntValAtRowIdx(StIdx,PIdx).Val;
182  int PDur = P->GetIntValAtRowIdx(DuIdx,PIdx).Val;
183  printf("%d\t%d\t%d\t%d\t%d\n",PIdx,PSource,PDest,PStart,PDur);
184  }
185  }
186  if (ToAddV.Len() > 1) {
187  TopCascVV.Add(ToAddV);
188  }
189  }
190  return;
191 }
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:567
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:479
int Val
Definition: dt.h:1046
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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:339
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:621
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
int AddKey(const TKey &Key)
Definition: shash.h:1254
Definition: dt.h:1044
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:481
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
Definition: ds.h:565
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:339
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:574
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:372
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 194 of file casc.cpp.

194  {
195  // Attribute to Int mapping
196  TInt SIdx = P->GetColIdx(C1);
197  TInt DIdx = P->GetColIdx(C2);
198  TInt StIdx = P->GetColIdx(C3);
199  TInt DuIdx = P->GetColIdx(C4);
200  TIntV MapV, PhyV;
201  TStrV SortBy;
202  SortBy.Add(C3);
203  P->Order(SortBy);
204  int count = 0;
205  for (TRowIterator RI = P->BegRI(); RI < P-> EndRI(); RI++) {
206  MapV.Add(RI.GetRowIdx());
207  PhyV.Add(count++);
208  }
209  P->StoreIntCol("Physical",PhyV);
210  TInt PIdx = P->GetColIdx("Physical");
211  TIntV GNodeV;
212  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
213  if (NI.GetInDeg() == 0) { GNodeV.Add(NI.GetId()); }
214  }
215  TVec<TIntV> ThTopCascVV; // for each thread
216  #pragma omp parallel private(ThTopCascVV) num_threads(10)
217  {
218  #pragma omp for schedule(dynamic,1000)
219  for (int i = 0; i < GNodeV.Len(); i++) {
220  TIntV CurCasc;
221  TSnapQueue<TInt> EventQ;
222  THashSet<TInt> VisitedH;
223  TInt NId = GNodeV[i];
224  EventQ.Push(NId);
225  VisitedH.AddKey(NId);
226  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,NId));
227  while (! EventQ.Empty()) {
228  TNGraph::TNodeI CNI = Graph->GetNI(EventQ.Top().Val); //Get Current Node
229  EventQ.Pop();
230  // Go over the outdegree nodes of the currernt node
231  for (int e = 0; e < CNI.GetOutDeg(); e++) {
232  TInt CId = CNI.GetOutNId(e);
233  if ( !VisitedH.IsKey(CId)) {
234  EventQ.Push(CId);
235  VisitedH.AddKey(CId);
236  CurCasc.Add(P->GetIntValAtRowIdx(PIdx,CId));
237  }
238  }
239  }
240  CurCasc.Sort();
241  TIntV ToAddV;
242  for (TIntV::TIter VI = CurCasc.BegI(); VI < CurCasc.EndI(); VI++) {
243  ToAddV.Add(MapV.GetVal(VI->Val));
244  }
245  if (ToAddV.Len() > 1) { ThTopCascVV.Add(ToAddV);}
246  }
247  #pragma omp critical
248  {
249  for (int j = 0; j < ThTopCascVV.Len(); j++) {
250  TopCascVV.Add(ThTopCascVV[j]);
251  }
252  }
253  }
254  return;
255 }
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:567
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:479
int Val
Definition: dt.h:1046
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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:339
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:621
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
int AddKey(const TKey &Key)
Definition: shash.h:1254
Definition: dt.h:1044
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
Definition: ds.h:565
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:339
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:574
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:372
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 120 of file casc.cpp.

120  {
121  if (SortParam) {
122  return CascGraphSource(P, C1, C2, C3, C4, W);
123  }
124  else {
125  return CascGraphTime(P, C1, C2, C3, C4, W);
126  }
127 }
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:56
PNGraph TSnap::CascGraphSource ( PTable  P,
const TStr  C1,
const TStr  C2,
const TStr  C3,
const TStr  C4,
const TInt  W 
)

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

Definition at line 3 of file casc.cpp.

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

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

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:171
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:547
Definition: ss.h:72
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:69
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:807
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:155
char GetCh(const int &ChN) const
Definition: dt.h:483
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: dt.h:412
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:207
bool IsKey(const TKey &Key) const
Definition: hash.h:216
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
void DelSelfEdges(const PGraph &Graph)
Removes all the self-edges from the graph.
Definition: alg.h:419
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
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.

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:797
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
void CmtyEvolutionFileBatch(TStr InFNm, TIntIntHH &sizesCont, TIntIntHH &cCont, TIntIntVH &edges, double alpha, double beta, int CmtyAlg)
Definition: cmty.cpp:488
Definition: hash.h:88
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void TSnap::CmtyEvolutionJson ( TStr Json,
TIntIntVH sizesContV,
TIntIntVH cContV,
TIntIntVH edges 
)

Definition at line 723 of file cmty.cpp.

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:1107
int Len() const
Definition: dt.h:487
TIter BegI() const
Definition: hash.h:171
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:69
Definition: dt.h:1044
int Len() const
Definition: hash.h:186
void InsStr(const int &BChN, const TStr &Str)
Definition: dt.cpp:825
TStr TSnap::CmtyTest ( TStr  InFNm,
int  CmtyAlg 
)

Definition at line 825 of file cmty.cpp.

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
template<class PGraph >
int TSnap::CntDegNodes ( const PGraph &  Graph,
const int &  NodeDeg 
)

Returns the number of nodes with degree NodeDeg.

Definition at line 105 of file alg.h.

105  {
106  int Cnt = 0;
107  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
108  if (NI.GetDeg() == NodeDeg) Cnt++;
109  }
110  return Cnt;
111 }
template<class PGraph >
int TSnap::CntEdgesToSet ( const PGraph &  Graph,
const int &  NId,
const TIntSet NodeSet 
)

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

Definition at line 123 of file alg.h.

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

Returns the number of nodes with in-degree NodeInDeg.

Definition at line 87 of file alg.h.

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

Returns the number of nodes with degree greater than 0.

Definition at line 114 of file alg.h.

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

Returns the number of nodes with out-degree NodeOutDeg.

Definition at line 96 of file alg.h.

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

Counts the number fo 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 is there exist directed edges in both directions: (u,v) and (v,u)

Definition at line 316 of file alg.h.

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

Counts unique directed edges in the graph Graph. Nodes (u,v)<.

> (where <code>u!=v</code>) are connected via an directted edge if there exists a directed edge from node \c u to node \c v.

Definition at line 301 of file alg.h.

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

Counts unique undirected edges in the graph Graph. Nodes (u,v)<.

> (where <code>u!=v</code>) are connected via an undirected edge if there exists an edge in either direction <code>(u,v)</code> or <code>(v,u)</code>.

Definition at line 279 of file alg.h.

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

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

Definition at line 1447 of file cmty.cpp.

1447  {
1448  return TSnapDetail::TCNMQMatrix::CmtyCMN(Graph, CmtyV);
1449 }
double TSnap::CommunityGirvanNewman ( PUNGraph Graph,
TCnComV CmtyV 
)

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

Definition at line 312 of file cmty.cpp.

312  {
313  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 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1047
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
double _GirvanNewmanGetModularity(const PUNGraph &G, const TIntH &OutDegH, const int &OrigEdges, TCnComV &CnComV)
Definition: cmty.cpp:37
void CmtyGirvanNewmanStep(PUNGraph &Graph, TIntV &Cmty1, TIntV &Cmty2)
A single step of Girvan-Newman clustering procedure.
Definition: cmty.cpp:15
template<class POutGraph , class PInGraph >
POutGraph TSnap::ConvertESubGraph ( const PInGraph &  InGraph,
const TIntV EIdV,
const bool &  RenumberNodes = false 
)

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

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

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

Definition at line 403 of file subgraph.h.

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:547
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
Definition: gbase.h:14
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
#define CAssert(Cond)
Definition: bd.h:302
template<class POutGraph , class PInGraph >
POutGraph TSnap::ConvertGraph ( const PInGraph &  InGraph,
const bool &  RenumberNodes = false 
)

Performs conversion of graph InGraph with an optional node renumbering.

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

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

Definition at line 288 of file subgraph.h.

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
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 >
int64 TSnap::CountTriangles ( const PGraph &  Graph)

Returns the number of triangles in graph Graph, original version.

Definition at line 358 of file triad.h.

358  {
360  TIntV MapV;
361 
362  int ind = 0;
363  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
364  H.AddDat(NI.GetId(), ind);
365  MapV.Add(NI.GetId());
366  ind += 1;
367  }
368 
369  TVec<TIntV> HigherDegNbrV(ind);
370 
371 #ifdef USE_OPENMP
372 #pragma omp parallel for schedule(dynamic)
373 #endif
374  for (int i = 0; i < ind; i++) {
375  TNGraph::TNodeI NI = Graph->GetNI(MapV[i]);
376  TIntV NbrV;
377 
378  GetMergeSortedV(NbrV, NI);
379 
380  TIntV V;
381  for (int j = 0; j < NbrV.Len(); j++) {
382  TInt Vert = NbrV[j];
383  TInt Deg = Graph->GetNI(Vert).GetDeg();
384  if (Deg > NI.GetDeg() ||
385  (Deg == NI.GetDeg() && Vert > NI.GetId())) {
386  V.Add(Vert);
387  }
388  }
389 
390  HigherDegNbrV[i] = V;
391 
392  }
393 
394  int64 cnt = 0;
395 #ifdef USE_OPENMP
396 #pragma omp parallel for schedule(dynamic) reduction(+:cnt)
397 #endif
398  for (int i = 0; i < HigherDegNbrV.Len(); i++) {
399  for (int j = 0; j < HigherDegNbrV[i].Len(); j++) {
400  TInt NbrInd = H.GetDat(HigherDegNbrV[i][j]);
401 
402  int64 num = GetCommon(HigherDegNbrV[i], HigherDegNbrV[NbrInd]);
403  cnt += num;
404  }
405  }
406 
407  return cnt;
408 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
void GetMergeSortedV(TIntV &NeighbourV, TNGraph::TNodeI NI)
Definition: triad.cpp:3
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:358
Definition: dt.h:1044
int GetId() const
Returns ID of the current node.
Definition: graph.h:356
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:54
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class PGraph >
void TSnap::DelDegKNodes ( PGraph &  Graph,
const int &  OutDegK,
const int &  InDegK 
)

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

Definition at line 445 of file alg.h.

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

Removes nodes with ids stored in NIdV from the graph.

Definition at line 425 of file alg.h.

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

Removes all the self-edges from the graph.

Definition at line 419 of file alg.h.

419  {
420  TSnapDetail::TDelSelfEdges<PGraph, HasGraphFlag(typename PGraph::TObj, gfMultiGraph)>
421  ::Do(Graph);
422 }
template<class PGraph >
void TSnap::DelZeroDegNodes ( PGraph &  Graph)

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

Definition at line 432 of file alg.h.

432  {
433  TIntV DelNIdV;
434  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
435  if (NI.GetDeg() == 0) {
436  DelNIdV.Add(NI.GetId());
437  }
438  }
439  for (int i = 0; i < DelNIdV.Len(); i++) {
440  Graph->DelNode(DelNIdV[i]);
441  }
442 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
template<class PGraph >
void TSnap::DrawGViz ( const PGraph &  Graph,
const TGVizLayout Layout,
const TStr PltFNm,
const TStr Desc = TStr(),
const bool &  NodeLabels = false,
const TIntStrH NIdColorH = TIntStrH() 
)

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

Useful for drawing small (<100 node) graphs.

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

Definition at line 34 of file gviz.h.

34  {
35  const TStr Ext = PltFNm.GetFExt();
36  const TStr GraphFNm = PltFNm.GetSubStr(0, PltFNm.Len()-Ext.Len()) + "dot";
37  SaveGViz(Graph, GraphFNm, Desc, NodeLabels, NIdColorH);
38  TSnap::TSnapDetail::GVizDoLayout(GraphFNm, PltFNm, Layout);
39 }
int Len() const
Definition: dt.h: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
template<class PGraph >
void TSnap::DrawGViz ( const PGraph &  Graph,
const TGVizLayout Layout,
const TStr PltFNm,
const TStr Desc,
const TIntStrH NodeLabelH 
)

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

Useful for drawing small (<100 node) graphs.

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

Definition at line 42 of file gviz.h.

42  {
43  const TStr Ext = PltFNm.GetFExt();
44  const TStr GraphFNm = PltFNm.GetSubStr(0, PltFNm.Len()-Ext.Len()) + "dot";
45  SaveGViz(Graph, GraphFNm, Desc, NodeLabelH);
46  TSnap::TSnapDetail::GVizDoLayout(GraphFNm, PltFNm, Layout);
47 }
int Len() const
Definition: dt.h: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
TIntFltH TSnap::EventImportance ( const PNGraph Graph,
const int  k 
)

Event importance.

Definition at line 527 of file centr.cpp.

527  {
528  TIntFltH NodeList; // values for nodese
529 
530  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
531  NodeList.AddDat(NI.GetId(),NI.GetOutDeg());
532  }
533 
534 
535  for (THashKeyDatI<TInt,TFlt> NI = NodeList.BegI(); NI < NodeList.EndI(); NI++){
536  int outdeg = Graph->GetNI(NI.GetKey()).GetOutDeg();
537  int indeg = Graph->GetNI(NI.GetKey()).GetInDeg();
538 
539  if (outdeg>1 && indeg>0){
540  double val = (1-(1/(double)outdeg))/(double)indeg;
541  for(int i=0; i<(outdeg+indeg);i++){
542  int NId = Graph->GetNI(NI.GetKey()).GetNbrNId(i);
543  if (Graph->GetNI(NI.GetKey()).IsInNId(NId) == true){
544  NodeList.AddDat(NId,NodeList.GetDat(NId)+val);
545  }
546 
547  }
548  }
549 
550  }
551 
552  return NodeList;
553 }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:479
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
TIter BegI() const
Definition: hash.h:171
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TIter EndI() const
Definition: hash.h:176
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
TIntFltH TSnap::EventImportance1 ( const PNGraph Graph,
const int  k 
)

Definition at line 556 of file centr.cpp.

556  {
557  TIntFltH NodeList; // values for nodese
558 
559  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
560  NodeList.AddDat(NI.GetId(),NI.GetOutDeg());
561  }
562 
563 
564  for (THashKeyDatI<TInt,TFlt> NI = NodeList.BegI(); NI < NodeList.EndI(); NI++){
565  int outdeg = Graph->GetNI(NI.GetKey()).GetOutDeg();
566  int indeg = Graph->GetNI(NI.GetKey()).GetInDeg();
567 
568  if (outdeg>1 && indeg>0){
569  double val = (1-(1/(double)outdeg))/(double)indeg;
570  for(int i=0; i<(outdeg+indeg);i++){
571  int NId = Graph->GetNI(NI.GetKey()).GetNbrNId(i);
572  if (Graph->GetNI(NI.GetKey()).IsInNId(NId) == true){
573  NodeList.AddDat(NId,NodeList.GetDat(NId)+val);
574  }
575 
576  }
577  }
578 
579  }
580 
581  return NodeList;
582 }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:479
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
TIter BegI() const
Definition: hash.h:171
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TIter EndI() const
Definition: hash.h:176
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
int TSnap::FastCorePeriphery ( PUNGraph Graph,
TIntIntH out 
)

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

Definition at line 12 of file coreper.cpp.

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

Definition at line 56 of file coreper.cpp.

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

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

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

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

Definition at line 71 of file flow.cpp.

71  {
72  int MidPtNId = IntFlowBiDBFS(Net, CapIndex, Flow, FwdNodeQ, PredEdgeH, BwdNodeQ, SuccEdgeH, SrcNId, SnkNId);
73  if (MidPtNId == -1) { return 0; }
74  int MinAug = TInt::Mx, NId = MidPtNId, AugFlow = 0;
75  // Build the path from the midpoint back to the source by tracing through the PredEdgeH
76  for (int EId = PredEdgeH.GetDat(NId); NId != SrcNId; EId = PredEdgeH.GetDat(NId)) {
77  MidToSrcAugV.Add(EId);
78  const TNEANet::TEdgeI &EI = Net->GetEI(EId);
79  if (EI.GetSrcNId() == NId) {
80  NId = EI.GetDstNId();
81  AugFlow = Flow[EId];
82  } else {
83  NId = EI.GetSrcNId();
84  AugFlow = Net->GetIntAttrIndDatE(EId, CapIndex) - Flow[EId];
85  }
86  if (AugFlow < MinAug) { MinAug = AugFlow; }
87  }
88  NId = MidPtNId;
89  // Build the path from the midpoint back to the sink by tracing through the SuccEdgeH
90  for (int EId = SuccEdgeH.GetDat(NId); NId != SnkNId; EId = SuccEdgeH.GetDat(NId)) {
91  MidToSnkAugV.Add(EId);
92  const TNEANet::TEdgeI &EI = Net->GetEI(EId);
93  if (EI.GetDstNId() == NId) {
94  NId = EI.GetSrcNId();
95  AugFlow = Flow[EId];
96  } else {
97  NId = EI.GetDstNId();
98  AugFlow = Net->GetIntAttrIndDatE(EId, CapIndex) - Flow[EId];
99  }
100  if (AugFlow < MinAug) { MinAug = AugFlow; }
101  }
102  return MinAug;
103 }
static const int Mx
Definition: dt.h:1049
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
int GetDstNId() const
Returns the destination of the edge.
Definition: network.h:1726
int GetSrcNId() const
Returns the source of the edge.
Definition: network.h:1724
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:1707
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int TSnap::findMinimum ( TIntV Frontier,
TIntFltH NIdDistH 
)

Definition at line 685 of file centr.cpp.

685  {
686  TFlt minimum = TInt::Mx;
687  int min_index = 0;
688  for (int i = 0; i < Frontier.Len(); i++) {
689  int NId = Frontier.GetVal(i);
690  if (NIdDistH[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:1130
static const int Mx
Definition: dt.h:1049
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
Definition: dt.h:1293
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:621
template<class PGraph >
PGraph TSnap::GenBaraHierar ( const int &  Levels,
const bool &  IsDir 
)

Generates a Ravasz-Barabasi deterministic scale-free graph.

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

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

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

Definition at line 174 of file ggen.h.

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

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

Definition at line 104 of file ggen.h.

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

Generates a random undirect graph with a given degree sequence.

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

Definition at line 119 of file ggen.cpp.

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:547
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:263
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
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
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568
PUNGraph TSnap::GenConfModel ( const PUNGraph G)

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

Definition at line 335 of file ggen.cpp.

335  {
336  TIntV DegSeqV(G->GetNodes(), 0);
337  TSnap::GetDegSeqV(G, DegSeqV);
338  return TSnap::GenConfModel(DegSeqV);
339 }
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
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
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.

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:425
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
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 IDs 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:523
Directed graph.
Definition: graph.h:307
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
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:534
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:372
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.

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:547
static TRnd Rnd
Definition: dt.h:1053
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:263
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
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:1259
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
Definition: graph.cpp:137
PNGraph TSnap::GenForestFire ( const int &  Nodes,
const double &  FwdProb,
const double &  BckProb 
)

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

Definition at line 442 of file ggen.cpp.

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

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:129
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1130
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:547
TVal1 Val1
Definition: ds.h:131
static double Sqr(const double &x)
Definition: xmath.h:12
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
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:132
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
TTriple< TFlt, TFlt, TFlt > TFltTr
Definition: ds.h:180
static double Pi
Definition: xmath.h:8
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:207
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:574
TVal3 Val3
Definition: ds.h:133
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
template<class PGraph >
PGraph TSnap::GenGrid ( const int &  Rows,
const int &  Cols,
const bool &  IsDir = true 
)

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

Definition at line 65 of file ggen.h.

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

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

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

Definition at line 310 of file ggen.cpp.

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:1053
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:263
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
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.

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
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
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:263
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
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
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
const char * GetStr() const
Definition: tm.h:368
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568
PNGraph TSnap::GenRewire ( const PNGraph OrigGraph,
const int &  NSwitch,
TRnd Rnd 
)

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

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

Definition at line 216 of file ggen.cpp.

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.Val1), NewE2(E1.Val2, E2.Val2);
241  if (NewE1.Val1!=NewE2.Val1 && NewE1.Val2!=NewE2.Val1 && NewE1.Val2!=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:479
Definition: tm.h:355
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
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:438
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 IDs SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
Directed graph.
Definition: graph.h:307
Definition: ds.h:32
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
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:534
TVal2 Val2
Definition: ds.h:35
const char * GetStr() const
Definition: tm.h:368
PBPGraph TSnap::GenRewire ( const PBPGraph OrigGraph,
const int &  NSwitch,
TRnd Rnd 
)

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

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

Definition at line 263 of file ggen.cpp.

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:888
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:856
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:982
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
const char * GetStr() const
Definition: tm.h:368
PNGraph TSnap::GenRMat ( const int &  Nodes,
const int &  Edges,
const double &  A,
const double &  B,
const double &  C,
TRnd Rnd 
)

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

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

Definition at line 478 of file ggen.cpp.

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:425
#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 IDs 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:307
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:534
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
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.

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
PBPGraph TSnap::GenRndBipart ( const int &  LeftNodes,
const int &  RightNodes,
const int &  Edges,
TRnd Rnd 
)

Generates a random bipartite graph.

Definition at line 5 of file ggen.cpp.

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

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

Definition at line 18 of file ggen.cpp.

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

Generates an Erdos-Renyi random graph.

Definition at line 218 of file ggen.h.

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

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

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

Definition at line 34 of file ggen.cpp.

34  {
35  TIntV DegSeqV;
36  uint DegSum=0;
37  for (int n = 0; n < Nodes; n++) {
38  const int Val = (int) TMath::Round(Rnd.GetPowerDev(PowerExp));
39  if (! (Val >= 1 && Val < Nodes/2)) { n--; continue; } // skip nodes with too large degree
40  DegSeqV.Add(Val);
41  DegSum += Val;
42  }
43  printf("%d nodes, %u edges\n", Nodes, DegSum);
44  if (DegSum % 2 == 1) { DegSeqV[0] += 1; }
45  if (ConfModel) {
46  // use configuration model -- fast but does not exactly obey the degree sequence
47  return GenConfModel(DegSeqV, Rnd);
48  } else {
49  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
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
double GetPowerDev(const double &AlphaSlope)
Definition: dt.h:47
PUNGraph TSnap::GenSmallWorld ( const int &  Nodes,
const int &  NodeOutDeg,
const double &  RewireProb,
TRnd Rnd 
)

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

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

Definition at line 412 of file ggen.cpp.

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:263
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
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
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
template<class PGraph >
PGraph TSnap::GenStar ( const int &  Nodes,
const bool &  IsDir = true 
)

Generates a graph with star topology. Node id 0 is in the center and then links to all other nodes.

Definition at line 91 of file ggen.h.

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

Generates a tree graph of Levels levels with every parent having Fanout children.

Definition at line 133 of file ggen.h.

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

Returns 1-components: maximal connected components of that can be disconnected from the Graph by removing a single edge.

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

Definition at line 98 of file cncom.cpp.

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

Distribution of sizes of 1-components, maximal number of components that can be disconnected from the Graph by removing a single edge.

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

Definition at line 70 of file cncom.cpp.

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

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

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

Definition at line 205 of file anf.h.

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

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

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

Definition at line 211 of file anf.h.

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

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

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

Definition at line 217 of file anf.h.

217  {
218  TIntFltKdV DistNbrsV;
219  TGraphAnf<PGraph> Anf(Graph, NApprox, 5, 0);
220  Anf.GetGraphAnf(DistNbrsV, -1, IsDir);
221  return TSnap::TSnapDetail::CalcEffDiam(DistNbrsV, Percentile);
222 }
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:429
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 225 of file anf.h.

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

Returns articulation points of a Graph.

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

Definition at line 48 of file cncom.cpp.

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

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 450 of file centr.h.

450  {
451  TIntPrFltH EdgeBtwH;
452  TIntV NIdV; Graph->GetNIdV(NIdV);
453  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
454  NIdV.Shuffle(TInt::Rnd);
455  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
456  NIdV.DelLast(); }
457  }
458  GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, IsDir, true, EdgeBtwH, false);
459 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
static TRnd Rnd
Definition: dt.h:1053
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
void DelLast()
Removes the last element of the vector.
Definition: ds.h:635
template<class PGraph >
void TSnap::GetBetweennessCentr ( const PGraph &  Graph,
TIntPrFltH EdgeBtwH,
const bool &  IsDir = false,
const double &  NodeFrac = 1.0 
)

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 462 of file centr.h.

462  {
463  TIntFltH NodeBtwH;
464  TIntV NIdV; Graph->GetNIdV(NIdV);
465  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
466  NIdV.Shuffle(TInt::Rnd);
467  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
468  NIdV.DelLast(); }
469  }
470  GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, IsDir, false, EdgeBtwH, true);
471 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
static TRnd Rnd
Definition: dt.h:1053
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
void DelLast()
Removes the last element of the vector.
Definition: ds.h:635
template<class PGraph >
void TSnap::GetBetweennessCentr ( const PGraph &  Graph,
TIntFltH NIdBtwH,
TIntPrFltH EdgeBtwH,
const bool &  IsDir = false,
const double &  NodeFrac = 1.0 
)

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 474 of file centr.h.

474  {
475  TIntV NIdV; Graph->GetNIdV(NIdV);
476  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
477  NIdV.Shuffle(TInt::Rnd);
478  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
479  NIdV.DelLast(); }
480  }
481  GetBetweennessCentr<PGraph> (Graph, NIdV, NodeBtwH, IsDir, true, EdgeBtwH, true);
482 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
static TRnd Rnd
Definition: dt.h:1053
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
void DelLast()
Removes the last element of the vector.
Definition: ds.h:635
template<class PGraph >
void TSnap::GetBetweennessCentr ( const PGraph &  Graph,
const TIntV BtwNIdV,
TIntFltH NodeBtwH,
const bool &  IsDir,
const bool &  DoNodeCent,
TIntPrFltH EdgeBtwH,
const bool &  DoEdgeCent 
)

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

Definition at line 374 of file centr.h.

374  {
375  if (DoNodeCent) { NodeBtwH.Clr(); }
376  if (DoEdgeCent) { EdgeBtwH.Clr(); }
377  const int nodes = Graph->GetNodes();
378  TIntS S(nodes);
379  TIntQ Q(nodes);
380  TIntIntVH P(nodes); // one vector for every node
381  TIntFltH delta(nodes);
382  TIntH sigma(nodes), d(nodes);
383  // init
384  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
385  if (DoNodeCent) {
386  NodeBtwH.AddDat(NI.GetId(), 0); }
387  if (DoEdgeCent) {
388  for (int e = 0; e < NI.GetOutDeg(); e++) {
389  if (NI.GetId() < NI.GetOutNId(e)) {
390  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
391  }
392  }
393  }
394  sigma.AddDat(NI.GetId(), 0);
395  d.AddDat(NI.GetId(), -1);
396  P.AddDat(NI.GetId(), TIntV());
397  delta.AddDat(NI.GetId(), 0);
398  }
399  // calc betweeness
400  for (int k=0; k < BtwNIdV.Len(); k++) {
401  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
402  // reset
403  for (int i = 0; i < sigma.Len(); i++) {
404  sigma[i]=0; d[i]=-1; delta[i]=0; P[i].Clr(false);
405  }
406  S.Clr(false);
407  Q.Clr(false);
408  sigma.AddDat(NI.GetId(), 1);
409  d.AddDat(NI.GetId(), 0);
410  Q.Push(NI.GetId());
411  while (! Q.Empty()) {
412  const int v = Q.Top(); Q.Pop();
413  const typename PGraph::TObj::TNodeI NI2 = Graph->GetNI(v);
414  S.Push(v);
415  const int VDat = d.GetDat(v);
416  for (int e = 0; e < NI2.GetOutDeg(); e++) {
417  const int w = NI2.GetOutNId(e);
418  if (d.GetDat(w) < 0) { // find w for the first time
419  Q.Push(w);
420  d.AddDat(w, VDat+1);
421  }
422  //shortest path to w via v ?
423  if (d.GetDat(w) == VDat+1) {
424  sigma.AddDat(w) += sigma.GetDat(v);
425  P.GetDat(w).Add(v);
426  }
427  }
428  }
429  while (! S.Empty()) {
430  const int w = S.Top();
431  const double SigmaW = sigma.GetDat(w);
432  const double DeltaW = delta.GetDat(w);
433  const TIntV NIdV = P.GetDat(w);
434  S.Pop();
435  for (int i = 0; i < NIdV.Len(); i++) {
436  const int NId = NIdV[i];
437  const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
438  delta.AddDat(NId) += c;
439  if (DoEdgeCent) {
440  EdgeBtwH.AddDat(TIntPr(TMath::Mn(NId, w), TMath::Mx(NId, w))) += c; }
441  }
442  double factor = (IsDir) ? 1.0 : 2.0;
443  if (DoNodeCent && w != NI.GetId()) {
444  NodeBtwH.AddDat(w) += delta.GetDat(w)/factor; }
445  }
446  }
447 }
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:547
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:807
TVec< TInt > TIntV
Definition: ds.h:1529
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
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.

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

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

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:1049
TKeyDat< TInt, TFlt > TIntFltKd
Definition: ds.h:380
static TRnd Rnd
Definition: dt.h:1053
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
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:551
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
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.

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:1049
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TKeyDat< TInt, TFlt > TIntFltKd
Definition: ds.h:380
static TRnd Rnd
Definition: dt.h:1053
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
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:551
Definition: dt.h:1044
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
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.

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

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:425
static const int Mx
Definition: dt.h:1049
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 IDs 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:477
void TSnap::GetBiCon ( const PUNGraph Graph,
TCnComV BiCnComV 
)

Returns all bi-connected components of a Graph.

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

Definition at line 42 of file cncom.cpp.

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

Returns a distribution of bi-connected component sizes.

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

Definition at line 31 of file cncom.cpp.

31  {
32  TCnComV BiCnComV;
33  GetBiCon(Graph, BiCnComV);
34  TIntH SzCntH;
35  for (int c =0; c < BiCnComV.Len(); c++) {
36  SzCntH.AddDat(BiCnComV[c].Len()) += 1;
37  }
38  SzCntH.GetKeyDatPrV(SzCntV);
39  SzCntV.Sort();
40 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
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:458
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class PGraph >
double TSnap::GetClosenessCentr ( const PGraph &  Graph,
const int &  NId,
const bool &  IsDir,
const bool &  Normalized = true 
)

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, IsDir, Normalized);
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 &  IsDir,
const bool &  Normalized = true 
)

Definition at line 169 of file centr.h.

169  {
170  const double Farness = GetFarnessCentrMP<PGraph> (Graph, NId, IsDir, Normalized);
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 117 of file triad.h.

117  {
118  TIntTrV NIdCOTriadV;
119  GetTriads(Graph, NIdCOTriadV, SampleNodes);
120  if (NIdCOTriadV.Empty()) { return 0.0; }
121  double SumCcf = 0.0;
122  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
123  const double OpenCnt = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
124  if (OpenCnt > 0) {
125  SumCcf += NIdCOTriadV[i].Val2() / OpenCnt; }
126  }
127  IAssert(SumCcf>=0);
128  return SumCcf / double(NIdCOTriadV.Len());
129 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
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:278
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 131 of file triad.h.

131  {
132  TIntTrV NIdCOTriadV;
133  GetTriads(Graph, NIdCOTriadV, SampleNodes);
134  THash<TInt, TFltPr> DegSumCnt;
135  double SumCcf = 0.0;
136  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
137  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
138  const double Ccf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
139  TFltPr& SumCnt = DegSumCnt.AddDat(Graph->GetNI(NIdCOTriadV[i].Val1).GetDeg());
140  SumCnt.Val1 += Ccf;
141  SumCnt.Val2 += 1;
142  SumCcf += Ccf;
143  }
144  // get average clustering coefficient for each degree
145  DegToCCfV.Gen(DegSumCnt.Len(), 0);
146  for (int d = 0; d < DegSumCnt.Len(); d++) {
147  DegToCCfV.Add(TFltPr(DegSumCnt.GetKey(d).Val, double(DegSumCnt[d].Val1()/DegSumCnt[d].Val2())));
148  }
149  DegToCCfV.Sort();
150  return SumCcf / double(NIdCOTriadV.Len());
151 }
int Val
Definition: dt.h:1046
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
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:278
Definition: ds.h:32
Definition: hash.h:88
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:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
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 154 of file triad.h.

154  {
155  TIntTrV NIdCOTriadV;
156  GetTriads(Graph, NIdCOTriadV, SampleNodes);
157  THash<TInt, TFltPr> DegSumCnt;
158  double SumCcf = 0.0;
159  int64 closedTriads = 0;
160  int64 openTriads = 0;
161  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
162  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
163  const double Ccf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
164  closedTriads += NIdCOTriadV[i].Val2;
165  openTriads += NIdCOTriadV[i].Val3;
166  TFltPr& SumCnt = DegSumCnt.AddDat(Graph->GetNI(NIdCOTriadV[i].Val1).GetDeg());
167  SumCnt.Val1 += Ccf;
168  SumCnt.Val2 += 1;
169  SumCcf += Ccf;
170  }
171  // get average clustering coefficient for each degree
172  DegToCCfV.Gen(DegSumCnt.Len(), 0);
173  for (int d = 0; d < DegSumCnt.Len(); d++) {
174  DegToCCfV.Add(TFltPr(DegSumCnt.GetKey(d).Val, DegSumCnt[d].Val1()/DegSumCnt[d].Val2()));
175  }
176  //if(closedTriads/3 > (uint64) TInt::Mx) { WarnNotify(TStr::Fmt("[%s line %d] %g closed triads.\n", __FILE__, __LINE__, float(closedTriads/3)).CStr()); }
177  //if(openTriads > (uint64) TInt::Mx) { WarnNotify(TStr::Fmt("[%s line %d] %g open triads.\n", __FILE__, __LINE__, float(openTriads/3)).CStr()); }
178  ClosedTriads = closedTriads/int64(3); // each triad is counted 3 times
179  OpenTriads = openTriads;
180  DegToCCfV.Sort();
181  return SumCcf / double(NIdCOTriadV.Len());
182 }
int Val
Definition: dt.h:1046
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
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:278
Definition: ds.h:32
long long int64
Definition: bd.h:27
Definition: hash.h:88
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:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
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 725 of file triad.h.

725  {
726  TIntV NbrV;
727  return GetCmnNbrs(Graph, NId1, NId2, NbrV);
728 }
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:732
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 732 of file triad.h.

732  {
733  if (! Graph->IsNode(NId1) || ! Graph->IsNode(NId2)) { NbrV.Clr(false); return 0; }
734  typename PGraph::TObj::TNodeI NI1 = Graph->GetNI(NId1);
735  typename PGraph::TObj::TNodeI NI2 = Graph->GetNI(NId2);
736  NbrV.Clr(false);
737  NbrV.Reserve(TMath::Mn(NI1.GetDeg(), NI2.GetDeg()));
738  TIntSet NSet1(NI1.GetDeg()), NSet2(NI2.GetDeg());
739  for (int i = 0; i < NI1.GetDeg(); i++) {
740  const int nid = NI1.GetNbrNId(i);
741  if (nid!=NId1 && nid!=NId2) {
742  NSet1.AddKey(nid); }
743  }
744  for (int i = 0; i < NI2.GetDeg(); i++) {
745  const int nid = NI2.GetNbrNId(i);
746  if (NSet1.IsKey(nid)) {
747  NSet2.AddKey(nid);
748  }
749  }
750  NSet2.GetKeyV(NbrV);
751  return NbrV.Len();
752 }
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:547
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
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:515
template<>
int TSnap::GetCmnNbrs< PUNGraph > ( const PUNGraph Graph,
const int &  NId1,
const int &  NId2,
TIntV NbrV 
)
inline

Definition at line 755 of file triad.h.

755  {
756  if (! Graph->IsNode(NId1) || ! Graph->IsNode(NId2)) { NbrV.Clr(false); return 0; }
757  const TUNGraph::TNodeI NI1 = Graph->GetNI(NId1);
758  const TUNGraph::TNodeI NI2 = Graph->GetNI(NId2);
759  int i=0, j=0;
760  NbrV.Clr(false);
761  NbrV.Reserve(TMath::Mn(NI1.GetDeg(), NI2.GetDeg()));
762  while (i < NI1.GetDeg() && j < NI2.GetDeg()) {
763  const int nid = NI1.GetNbrNId(i);
764  while (j < NI2.GetDeg() && NI2.GetNbrNId(j) < nid) { j++; }
765  if (j < NI2.GetDeg() && nid==NI2.GetNbrNId(j) && nid!=NId1 && nid!=NId2) {
766  IAssert(NbrV.Empty() || NbrV.Last() < nid);
767  NbrV.Add(nid);
768  j++;
769  }
770  i++;
771  }
772  return NbrV.Len();
773 }
#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:547
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:207
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int TSnap::GetCommon ( TIntV A,
TIntV B 
)

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

Definition at line 54 of file triad.cpp.

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

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

Definition at line 223 of file alg.h.

223  {
224  TIntH DegToCntH;
225  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
226  DegToCntH.AddDat(NI.GetDeg())++; }
227  DegToCntV.Gen(DegToCntH.Len(), 0);
228  for (int i = 0; i < DegToCntH.Len(); i++) {
229  DegToCntV.Add(TIntPr(DegToCntH.GetKey(i), DegToCntH[i])); }
230  DegToCntV.Sort();
231 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class PGraph >
void TSnap::GetDegCnt ( const PGraph &  Graph,
TFltPrV DegToCntV 
)

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

Definition at line 234 of file alg.h.

234  {
235  TIntH DegToCntH;
236  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
237  DegToCntH.AddDat(NI.GetDeg())++; }
238  DegToCntV.Gen(DegToCntH.Len(), 0);
239  for (int i = 0; i < DegToCntH.Len(); i++) {
240  DegToCntV.Add(TFltPr(DegToCntH.GetKey(i).Val, DegToCntH[i].Val)); }
241  DegToCntV.Sort();
242 }
int Val
Definition: dt.h:1046
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
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 }
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
template<class PGraph >
void TSnap::GetDegSeqV ( const PGraph &  Graph,
TIntV DegV 
)

Returns a degree sequence vector.

Definition at line 245 of file alg.h.

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

Returns an in- and out-degree sequence vectors.

Definition at line 253 of file alg.h.

253  {
254  InDegV.Gen(Graph->GetNodes(), 0);
255  OutDegV.Gen(Graph->GetNodes(), 0);
256  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
257  InDegV.Add(NI.GetInDeg());
258  OutDegV.Add(NI.GetOutDeg());
259  }
260 }
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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.

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.

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:547
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
network with data on edges
Definition: gbase.h:16
#define CAssert(Cond)
Definition: bd.h:302
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
Definition: bd.h:426
void TSnap::GetEdgeBridges ( const PUNGraph Graph,
TIntPrV EdgeV 
)

Returns bridge edges of a Graph.

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

Definition at line 55 of file cncom.cpp.

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

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

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

Definition at line 76 of file cmty.h.

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

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

Definition at line 82 of file subgraph.cpp.

82  {
83  PUNGraph NewGraphPt = TUNGraph::New();
84  TUNGraph& NewGraph = *NewGraphPt;
85  NewGraph.AddNode(CtrNId);
86  const TUNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
87  for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
88  NewGraph.AddNode(CtrNode.GetInNId(i));
89  }
90  ArndEdges = 0;
91  for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
92  int NbrNId = CtrNode.GetInNId(i);
93  const TUNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
94  for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
95  int NbrNbrNId = NbrNode.GetInNId(j);
96  if (NewGraph.IsNode(NbrNbrNId)) {
97  if (!NewGraph.IsEdge(NbrNId, NbrNbrNId)) {
98  NewGraph.AddEdge(NbrNId, NbrNbrNId);
99  }
100  } else {
101  ArndEdges++;
102  }
103  }
104  }
105  return NewGraphPt;
106 }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
Undirected graph.
Definition: graph.h:32
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
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:88
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:207
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:97
PNGraph TSnap::GetEgonet ( const PNGraph Graph,
const int  CtrNId,
int &  InEdges,
int &  OutEdges 
)

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

Definition at line 108 of file subgraph.cpp.

108  {
109  PNGraph NewGraphPt = TNGraph::New();
110  TNGraph& NewGraph = *NewGraphPt;
111  NewGraph.AddNode(CtrNId);
112  const TNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
113  for (int i = 0; i < CtrNode.GetDeg(); ++i) {
114  if (!NewGraph.IsNode(CtrNode.GetNbrNId(i))) {
115  NewGraph.AddNode(CtrNode.GetNbrNId(i));
116  }
117  }
118  InEdges = 0;
119  OutEdges = 0;
120  for (int i = 0; i < CtrNode.GetDeg(); ++i) {
121  int NbrNId = CtrNode.GetNbrNId(i);
122  const TNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
123  for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
124  int NbrNbrNId = NbrNode.GetInNId(j);
125  if (NewGraph.IsNode(NbrNbrNId)) {
126  NewGraph.AddEdge(NbrNbrNId, NbrNId);
127  } else {
128  InEdges++;
129  }
130  }
131  for (int j = 0; j < NbrNode.GetOutDeg(); ++j) {
132  int NbrNbrNId = NbrNode.GetOutNId(j);
133  if (NewGraph.IsNode(NbrNbrNId)) {
134  NewGraph.AddEdge(NbrNId, NbrNbrNId);
135  } else {
136  OutEdges++;
137  }
138  }
139  }
140  return NewGraphPt;
141 }
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:376
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
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 IDs 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:477
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:358
Directed graph.
Definition: graph.h:307
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:360
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:368
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:372
void TSnap::GetEigenVectorCentr ( const PUNGraph Graph,
TIntFltH NIdEigenH,
const double &  Eps = 1e-4,
const int &  MaxIter = 100 
)

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

Definition at line 11 of file centr.cpp.

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

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

Definition at line 308 of file gsvd.cpp.

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

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

Definition at line 336 of file gsvd.cpp.

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

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:547
bool IsAllValVNeg(TFltV &ValV, const bool &InvertSign)
Definition: gsvd.cpp:163
void GetCol(const int &ColN, TVec< TVal > &Vec) const
Definition: ds.h:2324
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
Definition: ds.h:2157
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
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.

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:547
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
Definition: gbase.h:14
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
#define CAssert(Cond)
Definition: bd.h:302
template<class PGraph >
PGraph TSnap::GetESubGraph ( const PGraph &  Graph,
const TIntPrV EdgeV 
)

Definition at line 227 of file subgraph.h.

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:547
template<class PGraph >
double TSnap::GetFarnessCentr ( const PGraph &  Graph,
const int &  NId,
const bool &  IsDir,
const bool &  Normalized = true 
)

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

Definition at line 123 of file centr.h.

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

Definition at line 142 of file centr.h.

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

Returns a string representation of a flag.

Definition at line 5 of file gbase.cpp.

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

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

Definition at line 169 of file centr.cpp.

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

Definition at line 59 of file centr.cpp.

59  {
60  int deg;
61  TIntH NN;
62  for (TUNGraph::TNodeI NI = Group->BegNI(); NI < Group->EndNI(); NI++) {
63  deg = Graph->GetNI(NI.GetId()).GetDeg();
64  for (int i=0; i<deg; i++) {
65  if (Group->IsNode(Graph->GetNI(NI.GetId()).GetNbrNId(i))==0)
66  NN.AddDat(Graph->GetNI(NI.GetId()).GetNbrNId(i),NI.GetId());
67  }
68  }
69  return (double)NN.Len();
70 }
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:207
int GetId() const
Returns ID of the current node.
Definition: graph.h:84
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
double TSnap::GetGroupDegreeCentr ( const PUNGraph Graph,
const TIntH GroupNodes 
)

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

Definition at line 85 of file centr.cpp.

85  {
86  int deg;
87  TIntH NN;
88  TIntH GroupNodes1;
89 
90  for (THashKeyDatI<TInt,TInt> NI = GroupNodes.BegI(); NI < GroupNodes.EndI(); NI++)
91  GroupNodes1.AddDat(NI.GetDat(),NI.GetDat());
92 
93  for (THashKeyDatI<TInt,TInt> NI = GroupNodes1.BegI(); NI < GroupNodes1.EndI(); NI++){
94  TUNGraph::TNodeI node = Graph->GetNI(NI.GetKey());
95  deg = node.GetDeg();
96  for (int j = 0; j < deg; j++){
97  if (GroupNodes1.IsKey(node.GetNbrNId(j))==0 && NN.IsKey(node.GetNbrNId(j))==0)
98  NN.AddDat(node.GetNbrNId(j),NI.GetKey());
99  }
100  }
101 
102  return (double)NN.Len();
103 }
TIter BegI() const
Definition: hash.h:171
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
TIter EndI() const
Definition: hash.h:176
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
bool IsKey(const TKey &Key) const
Definition: hash.h:216
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
double TSnap::GetGroupDegreeCentr0 ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Definition at line 72 of file centr.cpp.

72  {
73  int deg;
74  TIntH NN;
75  for (int i = 0; i<GroupNodes.Len(); i++) {
76  deg = Graph->GetNI(GroupNodes.GetDat(i)).GetDeg();
77  for (int j = 0; j < deg; j++) {
78  if (GroupNodes.IsKey(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j))==0)
79  NN.AddDat(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j),GroupNodes.GetDat(i));
80  }
81  }
82  return (double)NN.Len();
83 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
bool IsKey(const TKey &Key) const
Definition: hash.h:216
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
double TSnap::GetGroupFarnessCentr ( const PUNGraph Graph,
const TIntH GroupNodes 
)

Definition at line 105 of file centr.cpp.

105  {
106  TIntH* NDistH = new TIntH[GroupNodes.Len()];
107 
108  for (int i=0; i<GroupNodes.Len(); i++){
109  NDistH[i](Graph->GetNodes());
110  TSnap::GetShortPath<PUNGraph>(Graph, GroupNodes.GetDat(i), NDistH[i], true, TInt::Mx);
111  }
112 
113  int min, dist, sum=0, len=0;
114  for (PUNGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
115  if(NDistH[0].IsKey(NI.GetId()))
116  min = NDistH[0].GetDat(NI.GetId());
117  else
118  min = -1;
119  for (int j=1; j<GroupNodes.Len(); j++){
120  if (NDistH[j].IsKey(NI.GetId()))
121  dist = NDistH[j].GetDat(NI.GetId());
122  else
123  dist = -1;
124  if ((dist < min && dist != -1) || (dist > min && min == -1))
125  min = dist;
126  }
127  if (min>0){
128  sum += min;
129  len++;
130  }
131 
132  }
133 
134  if (len > 0) { return sum/double(len); }
135  else { return 0.0; }
136 }
static const int Mx
Definition: dt.h:1049
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
int Len() const
Definition: hash.h:186
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 485 of file centr.h.

485  {
486  const int NNodes = Graph->GetNodes();
487  NIdHubH.Gen(NNodes);
488  NIdAuthH.Gen(NNodes);
489  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
490  NIdHubH.AddDat(NI.GetId(), 1.0);
491  NIdAuthH.AddDat(NI.GetId(), 1.0);
492  }
493  double Norm=0;
494  for (int iter = 0; iter < MaxIter; iter++) {
495  // update authority scores
496  Norm = 0;
497  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
498  double& Auth = NIdAuthH.GetDat(NI.GetId()).Val;
499  Auth = 0;
500  for (int e = 0; e < NI.GetInDeg(); e++) {
501  Auth += NIdHubH.GetDat(NI.GetInNId(e)); }
502  Norm += Auth*Auth;
503  }
504  Norm = sqrt(Norm);
505  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
506  // update hub scores
507  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
508  double& Hub = NIdHubH.GetDat(NI.GetId()).Val;
509  Hub = 0;
510  for (int e = 0; e < NI.GetOutDeg(); e++) {
511  Hub += NIdAuthH.GetDat(NI.GetOutNId(e)); }
512  Norm += Hub*Hub;
513  }
514  Norm = sqrt(Norm);
515  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
516  }
517  // make sure Hub and Authority scores normalize to L2 norm 1
518  Norm = 0.0;
519  for (int i = 0; i < NIdHubH.Len(); i++) { Norm += TMath::Sqr(NIdHubH[i]); }
520  Norm = sqrt(Norm);
521  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
522  Norm = 0.0;
523  for (int i = 0; i < NIdAuthH.Len(); i++) { Norm += TMath::Sqr(NIdAuthH[i]); }
524  Norm = sqrt(Norm);
525  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
526 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
static double Sqr(const double &x)
Definition: xmath.h:12
void Gen(const int &ExpectVals)
Definition: hash.h:180
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class PGraph >
void TSnap::GetHitsMP ( const PGraph &  Graph,
TIntFltH NIdHubH,
TIntFltH NIdAuthH,
const int &  MaxIter = 20 
)

Definition at line 530 of file centr.h.

530  {
531  const int NNodes = Graph->GetNodes();
532  TIntV NV;
533  NIdHubH.Gen(NNodes);
534  NIdAuthH.Gen(NNodes);
535  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
536  NV.Add(NI.GetId());
537  NIdHubH.AddDat(NI.GetId(), 1.0);
538  NIdAuthH.AddDat(NI.GetId(), 1.0);
539  }
540  double Norm=0;
541  for (int iter = 0; iter < MaxIter; iter++) {
542  // update authority scores
543  Norm = 0;
544  #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
545  for (int i = 0; i < NNodes; i++) {
546  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
547  double& Auth = NIdAuthH.GetDat(NI.GetId()).Val;
548  Auth = 0;
549  for (int e = 0; e < NI.GetInDeg(); e++) {
550  Auth += NIdHubH.GetDat(NI.GetInNId(e)); }
551  Norm = Norm + Auth*Auth;
552  }
553  Norm = sqrt(Norm);
554  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
555  // update hub scores
556  #pragma omp parallel for reduction(+:Norm) schedule(dynamic,1000)
557  for (int i = 0; i < NNodes; i++) {
558  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NV[i]);
559  double& Hub = NIdHubH.GetDat(NI.GetId()).Val;
560  Hub = 0;
561  for (int e = 0; e < NI.GetOutDeg(); e++) {
562  Hub += NIdAuthH.GetDat(NI.GetOutNId(e)); }
563  Norm = Norm + Hub*Hub;
564  }
565  Norm = sqrt(Norm);
566  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
567  }
568  // make sure Hub and Authority scores normalize to L2 norm 1
569  Norm = 0.0;
570  for (int i = 0; i < NIdHubH.Len(); i++) { Norm += TMath::Sqr(NIdHubH[i]); }
571  Norm = sqrt(Norm);
572  for (int i = 0; i < NIdHubH.Len(); i++) { NIdHubH[i] /= Norm; }
573  Norm = 0.0;
574  for (int i = 0; i < NIdAuthH.Len(); i++) { Norm += TMath::Sqr(NIdAuthH[i]); }
575  Norm = sqrt(Norm);
576  for (int i = 0; i < NIdAuthH.Len(); i++) { NIdAuthH[i] /= Norm; }
577 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
static double Sqr(const double &x)
Definition: xmath.h:12
void Gen(const int &ExpectVals)
Definition: hash.h:180
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class PGraph >
void TSnap::GetInDegCnt ( const PGraph &  Graph,
TIntPrV DegToCntV 
)

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

Definition at line 179 of file alg.h.

179  {
180  TIntH DegToCntH;
181  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
182  DegToCntH.AddDat(NI.GetInDeg())++; }
183  DegToCntV.Gen(DegToCntH.Len(), 0);
184  for (int i = 0; i < DegToCntH.Len(); i++) {
185  DegToCntV.Add(TIntPr(DegToCntH.GetKey(i), DegToCntH[i])); }
186  DegToCntV.Sort();
187 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class PGraph >
void TSnap::GetInDegCnt ( const PGraph &  Graph,
TFltPrV DegToCntV 
)

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

Definition at line 190 of file alg.h.

190  {
191  TIntH DegToCntH;
192  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
193  DegToCntH.AddDat(NI.GetInDeg())++; }
194  DegToCntV.Gen(DegToCntH.Len(), 0);
195  for (int i = 0; i < DegToCntH.Len(); i++) {
196  DegToCntV.Add(TFltPr(DegToCntH.GetKey(i).Val, DegToCntH[i].Val)); }
197  DegToCntV.Sort();
198 }
int Val
Definition: dt.h:1046
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
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.

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:547
void GetCol(const int &ColN, TVec< TVal > &Vec) const
Definition: ds.h:2324
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
Definition: ds.h:2157
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
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:574
int GetCols() const
Definition: ds.h:2187
template<class PGraph >
PGraph TSnap::GetKCore ( const PGraph &  Graph,
const int &  K 
)

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

Definition at line 106 of file kcore.h.

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

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

Definition at line 126 of file kcore.h.

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

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

Definition at line 114 of file kcore.h.

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

778  {
779  TIntV NbrV;
780  return GetLen2Paths(Graph, NId1, NId2, NbrV);
781 }
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:786
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 786 of file triad.h.

786  {
787  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId1);
788  NbrV.Clr(false);
789  NbrV.Reserve(NI.GetOutDeg());
790  for (int e = 0; e < NI.GetOutDeg(); e++) {
791  const typename PGraph::TObj::TNodeI MidNI = Graph->GetNI(NI.GetOutNId(e));
792  if (MidNI.IsOutNId(NId2)) {
793  NbrV.Add(MidNI.GetId());
794  }
795  }
796  return NbrV.Len();
797 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int TSnap::GetMaxFlowIntEK ( PNEANet Net,
const int &  SrcNId,
const int &  SnkNId 
)

Returns the maximum integer valued flow in the network Net from source SrcNId to sink SnkNId.

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

Definition at line 105 of file flow.cpp.

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

Returns the maximum integer valued flow in the network Net from source SrcNId to sink SnkNId.

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

Definition at line 410 of file flow.cpp.

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

Definition at line 3 of file triad.cpp.

3  {
4  int j = 0;
5  int k = 0;
6  int prev = -1;
7  int indeg = NI.GetInDeg();
8  int outdeg = NI.GetOutDeg();
9  if (indeg > 0 && outdeg > 0) {
10  int v1 = NI.GetInNId(j);
11  int v2 = NI.GetOutNId(k);
12  while (1) {
13  if (v1 <= v2) {
14  if (prev != v1) {
15  NeighbourV.Add(v1);
16  prev = v1;
17  }
18  j += 1;
19  if (j >= indeg) {
20  break;
21  }
22  v1 = NI.GetInNId(j);
23  } else {
24  if (prev != v2) {
25  NeighbourV.Add(v2);
26  prev = v2;
27  }
28  k += 1;
29  if (k >= outdeg) {
30  break;
31  }
32  v2 = NI.GetOutNId(k);
33  }
34  }
35  }
36  while (j < indeg) {
37  int v = NI.GetInNId(j);
38  if (prev != v) {
39  NeighbourV.Add(v);
40  prev = v;
41  }
42  j += 1;
43  }
44  while (k < outdeg) {
45  int v = NI.GetOutNId(k);
46  if (prev != v) {
47  NeighbourV.Add(v);
48  prev = v;
49  }
50  k += 1;
51  }
52 }
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:360
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:368
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:372
template<typename PGraph >
double TSnap::GetModularity ( const PGraph &  G,
const TIntV NIdV,
int  GEdges = -1 
)

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

Definition at line 46 of file cmty.h.

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

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

Definition at line 66 of file cmty.h.

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

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

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

Definition at line 486 of file cncom.h.

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

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

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

Definition at line 126 of file cncom.cpp.

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

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

Definition at line 143 of file alg.h.

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

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

Definition at line 155 of file alg.h.

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

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

Definition at line 167 of file alg.h.

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

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

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

Definition at line 469 of file cncom.h.

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

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

Definition at line 444 of file cncom.h.

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

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

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

Definition at line 452 of file cncom.h.

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

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

Definition at line 436 of file cncom.h.

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

Returns clustering coefficient of a particular node.

Considers the graph as undirected.

Definition at line 185 of file triad.h.

185  {
186  int Open, Closed;
187  GetNodeTriads(Graph, NId, Open, Closed);
188  //const double Deg = Graph->GetNI(NId).GetDeg();
189  return (Open+Closed)==0 ? 0 : double(Open)/double(Open+Closed);
190 }
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:677
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 193 of file triad.h.

193  {
194  TIntTrV NIdCOTriadV;
195  GetTriads(Graph, NIdCOTriadV);
196  NIdCCfH.Clr(false);
197  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
198  const int D = NIdCOTriadV[i].Val2()+NIdCOTriadV[i].Val3();
199  const double CCf = D!=0 ? NIdCOTriadV[i].Val2() / double(D) : 0.0;
200  NIdCCfH.AddDat(NIdCOTriadV[i].Val1, CCf);
201  }
202 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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:278
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class PGraph >
int TSnap::GetNodeEcc ( const PGraph &  Graph,
const int &  NId,
const bool &  IsDir = false 
)

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

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

Definition at line 177 of file centr.h.

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

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

Definition at line 263 of file alg.h.

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

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

Definition at line 271 of file alg.h.

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

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:547
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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.

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:1049
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:458
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
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 637 of file triad.h.

637  {
638  int ClosedTriads=0, OpenTriads=0;
639  return GetNodeTriads(Graph, NId, ClosedTriads, OpenTriads);
640 }
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:677
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 644 of file triad.h.

644  {
645  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
646  ClosedTriads=0; OpenTriads=0;
647  if (NI.GetDeg() < 2) { return 0; }
648  // find neighborhood
649  TIntSet NbrSet(NI.GetDeg());
650  for (int e = 0; e < NI.GetOutDeg(); e++) {
651  if (NI.GetOutNId(e) != NI.GetId()) { // exclude self edges
652  NbrSet.AddKey(NI.GetOutNId(e)); }
653  }
654  if (Graph->HasFlag(gfDirected)) {
655  for (int e = 0; e < NI.GetInDeg(); e++) {
656  if (NI.GetInNId(e) != NI.GetId()) { // exclude self edges
657  NbrSet.AddKey(NI.GetInNId(e)); }
658  }
659  }
660  // count connected neighbors
661  for (int srcNbr = 0; srcNbr < NbrSet.Len(); srcNbr++) {
662  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrSet.GetKey(srcNbr));
663  for (int dstNbr = srcNbr+1; dstNbr < NbrSet.Len(); dstNbr++) {
664  const int dstNId = NbrSet.GetKey(dstNbr);
665  if (SrcNode.IsNbrNId(dstNId)) { ClosedTriads++; }
666  else { OpenTriads++; }
667  }
668  }
669  return ClosedTriads;
670 }
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
template<class PGraph >
int TSnap::GetNodeTriads ( const PGraph &  Graph,
const int &  NId,
const TIntSet GroupSet,
int &  InGroupEdgesX,
int &  InOutGroupEdgesX,
int &  OutGroupEdgesX 
)

Returns the number of triads between a node NId and a subset of its neighbors GroupSet.

Considers the graph as undirected.

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

Definition at line 677 of file triad.h.

677  {
678  const typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
679  const bool IsDir = Graph->HasFlag(gfDirected);
680  InGroupEdges=0; InOutGroupEdges=0; OutGroupEdges=0;
681  if (NI.GetDeg() < 2) { return 0; }
682  // find neighborhood
683  TIntSet NbrSet(NI.GetDeg());
684  for (int e = 0; e < NI.GetOutDeg(); e++) {
685  if (NI.GetOutNId(e) != NI.GetId()) { // exclude self edges
686  NbrSet.AddKey(NI.GetOutNId(e)); }
687  }
688  if (IsDir) {
689  for (int e = 0; e < NI.GetInDeg(); e++) {
690  if (NI.GetInNId(e) != NI.GetId()) {
691  NbrSet.AddKey(NI.GetInNId(e)); }
692  }
693  }
694  // count connected neighbors
695  for (int srcNbr = 0; srcNbr < NbrSet.Len(); srcNbr++) {
696  const int NbrId = NbrSet.GetKey(srcNbr);
697  const bool NbrIn = GroupSet.IsKey(NbrId);
698  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrId);
699  for (int dstNbr = srcNbr+1; dstNbr < NbrSet.Len(); dstNbr++) {
700  const int DstNId = NbrSet.GetKey(dstNbr);
701  if (SrcNode.IsNbrNId(DstNId)) { // triad (NId, NbrId, DstNid)
702  bool DstIn = GroupSet.IsKey(DstNId);
703  if (NbrIn && DstIn) { InGroupEdges++; }
704  else if (NbrIn || DstIn) { InOutGroupEdges++; }
705  else { OutGroupEdges++; }
706  }
707  }
708  }
709  return InGroupEdges;
710 }
bool IsKey(const TKey &Key) const
Definition: shash.h:1148
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
template<class PGraph >
void TSnap::GetNodeWcc ( const PGraph &  Graph,
const int &  NId,
TIntV CnCom 
)

Returns (via output parameter CnCom) all nodes that are in the same connected component as node NId.

Definition at line 277 of file cncom.h.

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

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

Definition at line 201 of file alg.h.

201  {
202  TIntH DegToCntH;
203  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
204  DegToCntH.AddDat(NI.GetOutDeg())++; }
205  DegToCntV.Gen(DegToCntH.Len(), 0);
206  for (int i = 0; i < DegToCntH.Len(); i++) {
207  DegToCntV.Add(TIntPr(DegToCntH.GetKey(i), DegToCntH[i])); }
208  DegToCntV.Sort();
209 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class PGraph >
void TSnap::GetOutDegCnt ( const PGraph &  Graph,
TFltPrV DegToCntV 
)

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

Definition at line 212 of file alg.h.

212  {
213  TIntH DegToCntH;
214  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
215  DegToCntH.AddDat(NI.GetOutDeg())++; }
216  DegToCntV.Gen(DegToCntH.Len(), 0);
217  for (int i = 0; i < DegToCntH.Len(); i++) {
218  DegToCntV.Add(TFltPr(DegToCntH.GetKey(i).Val, DegToCntH[i].Val)); }
219  DegToCntV.Sort();
220 }
int Val
Definition: dt.h:1046
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class PGraph >
void TSnap::GetPageRank ( const PGraph &  Graph,
TIntFltH PRankH,
const double &  C = 0.85,
const double &  Eps = 1e-4,
const int &  MaxIter = 100 
)

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

Definition at line 240 of file centr.h.

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

Definition at line 200 of file centr.h.

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

Definition at line 306 of file centr.h.

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

Definition at line 88 of file randwalk.h.

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

Returns a random subgraph of graph Graph with NEdges edges.

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

Definition at line 452 of file subgraph.h.

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:1053
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:574
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.

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:1130
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
static TRnd Rnd
Definition: dt.h:1053
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:1271
template<class PGraph >
double TSnap::GetRndWalkRestartBidirectional ( const PGraph &  Graph,
double  JumpProb,
int  StartNId,
int  TargetNId,
double  minProbability = -1.0,
double  relativeError = 0.1,
bool  proveRelativeError = false,
bool  PrintTimeForTuning = false 
)

Definition at line 135 of file randwalk.h.

142  {
143  return GetPersonalizedPageRankBidirectional(Graph, JumpProb, TIntV::GetV(StartNId), TargetNId,
144  minProbability, relativeError, proveRelativeError, PrintTimeForTuning);
145  }
static TVec< TInt, TSizeTy > GetV(const TInt &Val1)
Returns a vector on element Val1.
Definition: ds.h:817
double GetPersonalizedPageRankBidirectional(const PGraph &Graph, double JumpProb, const TIntV &StartNIdV, int TargetNId, double MinProbability=-1.0, double RelativeError=0.1, bool provableRelativeError=false, bool PrintTimeForTuning=false)
Definition: randwalk.h:88
template<class PGraph >
void TSnap::GetSccs ( const PGraph &  Graph,
TCnComV CnComV 
)

Returns all strongly connected components in a Graph.

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

Definition at line 428 of file cncom.h.

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

Returns a distribution of strongly connected component sizes.

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

Definition at line 420 of file cncom.h.

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

Definition at line 487 of file bfsdfs.h.

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:547
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:528
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:621
Definition: bd.h:196
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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.

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:547
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:589
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:528
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:621
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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.

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

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:502
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
int Len() const
Definition: hash.h:186
void TSnap::GetSngVals ( const PNGraph Graph,
const int &  SngVals,
TFltV SngValV 
)

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

Definition at line 175 of file gsvd.cpp.

175  {
176  const int Nodes = Graph->GetNodes();
177  IAssert(SngVals > 0);
178  if (Nodes < 100) {
179  // perform full SVD
180  TFltVV AdjMtx(Nodes+1, Nodes+1);
181  TFltVV LSingV, RSingV;
182  TIntH NodeIdH;
183  // create adjecency matrix
184  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
185  NodeIdH.AddKey(NodeI.GetId()); }
186  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
187  const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1;
188  for (int e = 0; e < NodeI.GetOutDeg(); e++) {
189  const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges
190  if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
191  }
192  }
193  try { // can fail to converge but results seem to be good
194  TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); }
195  catch(...) {
196  printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); }
197  } else {
198  // Lanczos
199  TNGraphMtx GraphMtx(Graph);
200  int CalcVals = int(2*SngVals);
201  //if (CalcVals > Nodes) { CalcVals = int(2*Nodes); }
202  //if (CalcVals > Nodes) { CalcVals = Nodes; }
203  //while (SngValV.Len() < SngVals && CalcVals < 10*SngVals) {
204  try {
205  if (SngVals > 4) {
206  TSparseSVD::SimpleLanczosSVD(GraphMtx, 2*SngVals, SngValV, false); }
207  else { TFltVV LSingV, RSingV; // this is much more precise, but also much slower
208  TSparseSVD::LanczosSVD(GraphMtx, SngVals, 3*SngVals, ssotFull, SngValV, LSingV, RSingV); }
209  }
210  catch(...) {
211  printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*SngVals, SngValV.Len()); }
212  if (SngValV.Len() < SngVals) {
213  printf(" ***TRIED %d GOT %d values** \n", CalcVals, SngValV.Len()); }
214  // CalcVals += SngVals;
215  //}
216  }
217  SngValV.Sort(false);
218  //if (SngValV.Len() > SngVals) {
219  // SngValV.Del(SngVals, SngValV.Len()-1); }
220  //else {
221  // while (SngValV.Len() < SngVals) SngValV.Add(1e-6); }
222  //IAssert(SngValV.Len() == SngVals);
223 }
#define IAssert(Cond)
Definition: bd.h:262
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:479
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:547
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:438
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:1254
Definition: ds.h:2157
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:424
int AddKey(const TKey &Key)
Definition: hash.h:331
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
void TSnap::GetSngVec ( const PNGraph Graph,
TFltV LeftSV,
TFltV RightSV 
)

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

Definition at line 225 of file gsvd.cpp.

225  {
226  const int Nodes = Graph->GetNodes();
227  TFltVV LSingV, RSingV;
228  TFltV SngValV;
229  if (Nodes < 500) {
230  // perform full SVD
231  TFltVV AdjMtx(Nodes+1, Nodes+1);
232  TIntH NodeIdH;
233  // create adjecency matrix
234  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
235  NodeIdH.AddKey(NodeI.GetId()); }
236  for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
237  const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1;
238  for (int e = 0; e < NodeI.GetOutDeg(); e++) {
239  const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges
240  if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
241  }
242  }
243  try { // can fail to converge but results seem to be good
244  TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); }
245  catch(...) {
246  printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); }
247  } else { // Lanczos
248  TNGraphMtx GraphMtx(Graph);
249  TSparseSVD::LanczosSVD(GraphMtx, 1, 8, ssotFull, SngValV, LSingV, RSingV);
250  }
251  TFlt MxSngVal = TFlt::Mn;
252  int ValN = 0;
253  for (int i = 0; i < SngValV.Len(); i++) {
254  if (MxSngVal < SngValV[i]) { MxSngVal = SngValV[i]; ValN = i; } }
255  LSingV.GetCol(ValN, LeftSV);
256  RSingV.GetCol(ValN, RightSV);
257  IsAllValVNeg(LeftSV, true);
258  IsAllValVNeg(RightSV, true);
259 }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:479
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:547
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:438
static void LanczosSVD(const TMatrix &Matrix, int NumSV, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &SgnValV, TFltVV &LeftSgnVecVV, TFltVV &RightSgnVecVV)
Definition: linalg.cpp:1454
void GetCol(const int &ColN, TVec< TVal > &Vec) const
Definition: ds.h:2324
Definition: dt.h:1293
Definition: ds.h:2157
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:424
int AddKey(const TKey &Key)
Definition: hash.h:331
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
static const double Mn
Definition: dt.h:1297
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.

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:479
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:547
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:438
static void LanczosSVD(const TMatrix &Matrix, int NumSV, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &SgnValV, TFltVV &LeftSgnVecVV, TFltVV &RightSgnVecVV)
Definition: linalg.cpp:1454
void GetCol(const int &ColN, TVec< TVal > &Vec) const
Definition: ds.h:2324
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
Definition: ds.h:2157
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:551
int GetKeyId(const TKey &Key) const
Definition: hash.h:424
int AddKey(const TKey &Key)
Definition: hash.h:331
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
PUNGraph TSnap::GetSubGraph ( const PUNGraph Graph,
const TIntV NIdV,
const bool &  RenumberNodes = false 
)

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

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

Definition at line 7 of file subgraph.cpp.

7  {
8  //if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
9  PUNGraph NewGraphPt = TUNGraph::New();
10  TUNGraph& NewGraph = *NewGraphPt;
11  NewGraph.Reserve(NIdV.Len(), -1);
12  TIntSet NIdSet(NIdV.Len());
13  for (int n = 0; n < NIdV.Len(); n++) {
14  if (Graph->IsNode(NIdV[n])) {
15  NIdSet.AddKey(NIdV[n]);
16  if (! RenumberNodes) { NewGraph.AddNode(NIdV[n]); }
17  else { NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n])); }
18  }
19  }
20  if (! RenumberNodes) {
21  for (int n = 0; n < NIdSet.Len(); n++) {
22  const int SrcNId = NIdSet[n];
23  const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
24  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
25  const int OutNId = NI.GetOutNId(edge);
26  if (NIdSet.IsKey(OutNId)) {
27  NewGraph.AddEdge(SrcNId, OutNId); }
28  }
29  }
30  } else {
31  for (int n = 0; n < NIdSet.Len(); n++) {
32  const int SrcNId = NIdSet[n];
33  const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
34  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
35  const int OutNId = NI.GetOutNId(edge);
36  if (NIdSet.IsKey(OutNId)) {
37  NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId)); }
38  }
39  }
40  }
41  return NewGraphPt;
42 }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:90
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:263
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
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:102
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:207
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.

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:425
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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 IDs 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:477
int AddKey(const TKey &Key)
Definition: shash.h:1254
Directed graph.
Definition: graph.h:307
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:534
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:372
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.

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:1049
template<class PGraph >
int TSnap::GetTreeRootNId ( const PGraph &  Graph)

Definition at line 80 of file alg.h.

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

Definition at line 484 of file alg.h.

484  {
485  CAssert(HasGraphFlag(typename PGraph::TObj, gfDirected));
486  Sig.Gen(Graph->GetNodes(), 0);
487  TSnapQueue<int> NIdQ(Graph->GetNodes());
488  NIdQ.Push(RootNId);
489  int LastPos = 0, NodeCnt = 1;
490  while (! NIdQ.Empty()) {
491  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
492  IAssert(Node.GetInDeg()==0 || Node.GetOutDeg()==0); // child points or is-pointed to by the parent
493  if (Node.GetInDeg() != 0) {
494  for (int e = 0; e < Node.GetInDeg(); e++) {
495  NIdQ.Push(Node.GetInNId(e)); }
496  } else if (Node.GetOutDeg() != 0) {
497  for (int e = 0; e < Node.GetOutDeg(); e++) {
498  NIdQ.Push(Node.GetOutNId(e)); }
499  }
500  Sig.Add(Node.GetInDeg());
501  if (--NodeCnt == 0) {
502  for (int i = LastPos; i < Sig.Len(); i++) NodeCnt += Sig[i];
503  Sig.QSort(LastPos, Sig.Len()-1, false);
504  LastPos = Sig.Len();
505  }
506  }
507 }
#define IAssert(Cond)
Definition: bd.h:262
void QSort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Quick sorts the values between positions MnLValN...MxLValN.
Definition: ds.h:1241
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
#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:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
template<class PGraph >
void TSnap::GetTreeSig ( const PGraph &  Graph,
const int &  RootNId,
TIntV Sig,
TIntPrV NodeMap 
)

Definition at line 511 of file alg.h.

511  {
512  CAssert(HasGraphFlag(typename PGraph::TObj, gfDirected));
513  NodeMap.Gen(Graph->GetNodes(), 0);
514  Sig.Gen(Graph->GetNodes(), 0);
515  TSnapQueue<int> NIdQ(Graph->GetNodes());
516  NIdQ.Push(RootNId);
517  int LastPos = 0, NodeCnt = 1;
518  while (! NIdQ.Empty()) {
519  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
520  IAssert(Node.GetInDeg()==0 || Node.GetOutDeg()==0); // child points or is-pointed to by the parent
521  if (Node.GetInDeg() != 0) {
522  for (int e = 0; e < Node.GetInDeg(); e++) {
523  NIdQ.Push(Node.GetInNId(e)); }
524  NodeMap.Add(TIntPr(Node.GetInDeg(), Node.GetId()));
525  } else if (Node.GetOutDeg() != 0) {
526  for (int e = 0; e < Node.GetOutDeg(); e++) {
527  NIdQ.Push(Node.GetOutNId(e)); }
528  NodeMap.Add(TIntPr(Node.GetOutDeg(), Node.GetId()));
529  }
530  if (--NodeCnt == 0) {
531  for (int i = LastPos; i < NodeMap.Len(); i++) {
532  NodeCnt += NodeMap[i].Val1; }
533  NodeMap.QSort(LastPos, NodeMap.Len()-1, false);
534  LastPos = NodeMap.Len();
535  }
536  }
537  for (int i = 0; i < NodeMap.Len(); i++) {
538  Sig.Add(NodeMap[i].Val1); // degree dignature
539  NodeMap[i].Val1 = NodeMap[i].Val2;
540  NodeMap[i].Val2 = i;
541  }
542 }
#define IAssert(Cond)
Definition: bd.h:262
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void QSort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Quick sorts the values between positions MnLValN...MxLValN.
Definition: ds.h:1241
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
#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:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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 601 of file triad.h.

601  {
602  const bool IsDir = Graph->HasFlag(gfDirected);
603  TIntSet NbrH;
604  int TriadEdges = 0;
605  for(typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
606  NbrH.Clr(false);
607  for (int e = 0; e < NI.GetOutDeg(); e++) {
608  if (NI.GetOutNId(e) != NI.GetId()) {
609  NbrH.AddKey(NI.GetOutNId(e)); }
610  }
611  if (IsDir) {
612  for (int e = 0; e < NI.GetInDeg(); e++) {
613  if (NI.GetInNId(e) != NI.GetId()) {
614  NbrH.AddKey(NI.GetInNId(e)); }
615  }
616  }
617  for (int e = 0; e < NI.GetOutDeg(); e++) {
618  if (!IsDir && NI.GetId()<NI.GetOutNId(e)) { continue; } // for undirected graphs count each edge only once
619  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NI.GetOutNId(e));
620  bool Triad=false;
621  for (int e1 = 0; e1 < SrcNode.GetOutDeg(); e1++) {
622  if (NbrH.IsKey(SrcNode.GetOutNId(e1))) { Triad=true; break; }
623  }
624  if (IsDir && ! Triad) {
625  for (int e1 = 0; e1 < SrcNode.GetInDeg(); e1++) {
626  if (NbrH.IsKey(SrcNode.GetInNId(e1))) { Triad=true; break; }
627  }
628  }
629  if (Triad) { TriadEdges++; }
630  }
631  }
632  return TriadEdges;
633 }
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
Definition: shash.h:1243
bool IsKey(const TKey &Key) const
Definition: shash.h:1148
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
template<class PGraph >
void TSnap::GetTriadParticip ( const PGraph &  Graph,
TIntPrV TriadCntV 
)

Triangle Participation Ratio: For each node counts how many triangles it participates in and then returns a set of pairs (number of triangles, number of such nodes).

Considers the graph as undirected.

Definition at line 714 of file triad.h.

714  {
715  TIntH TriadCntH;
716  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
717  const int Triads = GetNodeTriads(Graph, NI.GetId());
718  TriadCntH.AddDat(Triads) += 1;
719  }
720  TriadCntH.GetKeyDatPrV(TriadCntV);
721  TriadCntV.Sort();
722 }
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:677
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:458
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
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 205 of file triad.h.

205  {
206  int64 OpenTriads, ClosedTriads;
207  return GetTriads(Graph, ClosedTriads, OpenTriads, SampleNodes);
208 }
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:278
long long int64
Definition: bd.h:27
template<class PGraph >
void TSnap::GetTriads ( const PGraph &  Graph,
TIntTrV NIdCOTriadV,
int  SampleNodes = -1 
)

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

Considers the graph as undirected.

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

Definition at line 278 of file triad.h.

278  {
279  const bool IsDir = Graph->HasFlag(gfDirected);
280  TIntSet NbrH;
281  TIntV NIdV;
282  //TRnd Rnd(0);
283  TRnd Rnd(1);
284  int NNodes;
285  TIntV Nbrs;
286  int NId;
287 
288  int64 hcount;
289 
290  hcount = 0;
291 
292  NNodes = Graph->GetNodes();
293  Graph->GetNIdV(NIdV);
294  NIdV.Shuffle(Rnd);
295  if (SampleNodes == -1) {
296  SampleNodes = NNodes;
297  }
298 
299  int MxId = -1;
300  for (int i = 0; i < NNodes; i++) {
301  if (NIdV[i] > MxId) {
302  MxId = NIdV[i];
303  }
304  }
305 
306  TVec<TIntV> NbrV(MxId + 1);
307 
308  if (IsDir) {
309  // get in and out neighbors
310  for (int node = 0; node < NNodes; node++) {
311  int NId = NIdV[node];
312  NbrV[NId] = TIntV();
313  GetUniqueNbrV(Graph, NId, NbrV[NId]);
314  }
315  } else {
316  // get only out neighbors
317  for (int node = 0; node < NNodes; node++) {
318  int NId = NIdV[node];
319  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
320  NbrV[NId] = TIntV();
321  NbrV[NId].Reserve(NI.GetOutDeg());
322  NbrV[NId].Reduce(0);
323  for (int i = 0; i < NI.GetOutDeg(); i++) {
324  NbrV[NId].Add(NI.GetOutNId(i));
325  }
326  }
327  }
328 
329  NIdCOTriadV.Clr(false);
330  NIdCOTriadV.Reserve(SampleNodes);
331  for (int node = 0; node < SampleNodes; node++) {
332  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[node]);
333  int NLen;
334 
335  NId = NI.GetId();
336  hcount++;
337  if (NI.GetDeg() < 2) {
338  NIdCOTriadV.Add(TIntTr(NId, 0, 0)); // zero triangles
339  continue;
340  }
341 
342  Nbrs = NbrV[NId];
343  NLen = Nbrs.Len();
344 
345  // count connected neighbors
346  int OpenCnt1 = 0, CloseCnt1 = 0;
347  for (int srcNbr = 0; srcNbr < NLen; srcNbr++) {
348  int Count = GetCommon(NbrV[NbrV[NId][srcNbr]],Nbrs);
349  CloseCnt1 += Count;
350  }
351  CloseCnt1 /= 2;
352  OpenCnt1 = (NLen*(NLen-1))/2 - CloseCnt1;
353  NIdCOTriadV.Add(TIntTr(NId, CloseCnt1, OpenCnt1));
354  }
355 }
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:800
Definition: dt.h:11
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
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:54
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
TVec< TInt > TIntV
Definition: ds.h:1529
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:170
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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 211 of file triad.h.

211  {
212  TIntTrV NIdCOTriadV;
213  GetTriads(Graph, NIdCOTriadV, SampleNodes);
214  uint64 closedTriads = 0;
215  uint64 openTriads = 0;
216  for (int i = 0; i < NIdCOTriadV.Len(); i++) {
217  closedTriads += NIdCOTriadV[i].Val2;
218  openTriads += NIdCOTriadV[i].Val3;
219  }
220  //IAssert(closedTriads/3 < (uint64) TInt::Mx);
221  //IAssert(openTriads < (uint64) TInt::Mx);
222  ClosedTriads = int64(closedTriads/3); // each triad is counted 3 times
223  OpenTriads = int64(openTriads);
224  return ClosedTriads;
225 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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:278
long long int64
Definition: bd.h:27
template<class PGraph >
void TSnap::GetTriads_v0 ( const PGraph &  Graph,
TIntTrV NIdCOTriadV,
int  SampleNodes 
)

Definition at line 230 of file triad.h.

230  {
231  const bool IsDir = Graph->HasFlag(gfDirected);
232  TIntSet NbrH;
233  TIntV NIdV;
234  TRnd Rnd(0);
235 
236  Graph->GetNIdV(NIdV);
237  NIdV.Shuffle(Rnd);
238  if (SampleNodes == -1) {
239  SampleNodes = Graph->GetNodes(); }
240  NIdCOTriadV.Clr(false);
241  NIdCOTriadV.Reserve(SampleNodes);
242  for (int node = 0; node < SampleNodes; node++) {
243  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NIdV[node]);
244  if (NI.GetDeg() < 2) {
245  NIdCOTriadV.Add(TIntTr(NI.GetId(), 0, 0)); // zero triangles
246  continue;
247  }
248  // find neighborhood
249  NbrH.Clr(false);
250  for (int e = 0; e < NI.GetOutDeg(); e++) {
251  if (NI.GetOutNId(e) != NI.GetId()) {
252  NbrH.AddKey(NI.GetOutNId(e)); }
253  }
254  if (IsDir) {
255  for (int e = 0; e < NI.GetInDeg(); e++) {
256  if (NI.GetInNId(e) != NI.GetId()) {
257  NbrH.AddKey(NI.GetInNId(e)); }
258  }
259  }
260  // count connected neighbors
261  int OpenCnt=0, CloseCnt=0;
262  for (int srcNbr = 0; srcNbr < NbrH.Len(); srcNbr++) {
263  const typename PGraph::TObj::TNodeI SrcNode = Graph->GetNI(NbrH.GetKey(srcNbr));
264  for (int dstNbr = srcNbr+1; dstNbr < NbrH.Len(); dstNbr++) {
265  const int dstNId = NbrH.GetKey(dstNbr);
266  if (SrcNode.IsNbrNId(dstNId)) { CloseCnt++; } // is edge
267  else { OpenCnt++; }
268  }
269  }
270  IAssert(2*(OpenCnt+CloseCnt) == NbrH.Len()*(NbrH.Len()-1));
271  NIdCOTriadV.Add(TIntTr(NI.GetId(), CloseCnt, OpenCnt));
272  }
273 }
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:971
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:1271
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:170
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
template<class PGraph >
int64 TSnap::GetTriangleCnt ( const PGraph &  Graph)

Returns the number of triangles in graph Graph, newer version.

Definition at line 411 of file triad.h.

411  {
412  struct timeval start, end;
413  struct timeval startall, endall;
414  float delta;
415  TTmProfiler Profiler;
416  int TimerId = Profiler.AddTimer("Profiler");
417  int TimerAll = Profiler.AddTimer("ProfilerAll");
418  const int NNodes = Graph->GetNodes();
419 
420  TIntV MapV(NNodes);
422  NV.Reduce(0);
423 
424  Profiler.ResetTimer(TimerAll);
425  Profiler.StartTimer(TimerAll);
426  gettimeofday(&startall, NULL);
427 
428  Profiler.ResetTimer(TimerId);
429  Profiler.StartTimer(TimerId);
430  gettimeofday(&start, NULL);
431 
432  int MxId = -1;
433  int ind = 0;
434  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
435  NV.Add(NI);
436  int Id = NI.GetId();
437  if (Id > MxId) {
438  MxId = Id;
439  }
440  MapV[ind] = Id;
441  ind++;
442  }
443 
444  TIntV IndV(MxId+1);
445 
446  for (int j = 0; j < NNodes; j++) {
447  IndV[MapV[j]] = j;
448  }
449 
450  gettimeofday(&end, NULL);
451  Profiler.StopTimer(TimerId);
452  delta = ((end.tv_sec - start.tv_sec) * 1000000u +
453  end.tv_usec - start.tv_usec) / 1.e6;
454  printf("__nodemap__\ttime %7.3f\tcpu %8.3f\n", delta, Profiler.GetTimerSec(TimerId));
455 
456  Profiler.ResetTimer(TimerId);
457  Profiler.StartTimer(TimerId);
458  gettimeofday(&start, NULL);
459 
460  ind = MapV.Len();
461 
462  Profiler.ResetTimer(TimerId);
463  Profiler.StartTimer(TimerId);
464  gettimeofday(&start, NULL);
465 
466  TVec<TIntV> HigherDegNbrV(ind);
467 
468  for (int i = 0; i < ind; i++) {
469  HigherDegNbrV[i] = TVec<TInt>();
470  HigherDegNbrV[i].Reserve(NV[i].GetDeg());
471  HigherDegNbrV[i].Reduce(0);
472  }
473 
474  gettimeofday(&end, NULL);
475  Profiler.StopTimer(TimerId);
476  delta = ((end.tv_sec - start.tv_sec) * 1000000u +
477  end.tv_usec - start.tv_usec) / 1.e6;
478  printf("__valloc__\ttime %7.3f\tcpu %8.3f\n", delta, Profiler.GetTimerSec(TimerId));
479 
480  Profiler.ResetTimer(TimerId);
481  Profiler.StartTimer(TimerId);
482  gettimeofday(&start, NULL);
483 
484 #ifdef USE_OPENMP
485 #pragma omp parallel for schedule(dynamic)
486 #endif
487  for (int i = 0; i < ind; i++) {
488  typename PGraph::TObj::TNodeI NI = NV[i];
489  //HigherDegNbrV[i] = TVec<TInt>();
490  //HigherDegNbrV[i].Reserve(NI.GetDeg());
491  //HigherDegNbrV[i].Reduce(0);
492 
493  MergeNbrs<PGraph>(HigherDegNbrV[i], NI);
494 
495  int k = 0;
496  for (int j = 0; j < HigherDegNbrV[i].Len(); j++) {
497  TInt Vert = HigherDegNbrV[i][j];
498  TInt Deg = NV[IndV[Vert]].GetDeg();
499  if (Deg > NI.GetDeg() ||
500  (Deg == NI.GetDeg() && Vert > NI.GetId())) {
501  HigherDegNbrV[i][k] = Vert;
502  k++;
503  }
504  }
505  HigherDegNbrV[i].Reduce(k);
506  }
507 
508  gettimeofday(&end, NULL);
509  Profiler.StopTimer(TimerId);
510  delta = ((end.tv_sec - start.tv_sec) * 1000000u +
511  end.tv_usec - start.tv_usec) / 1.e6;
512  printf("__sort__\ttime %7.3f\tcpu %8.3f\n", delta, Profiler.GetTimerSec(TimerId));
513 
514  Profiler.ResetTimer(TimerId);
515  Profiler.StartTimer(TimerId);
516  gettimeofday(&start, NULL);
517 
518  int64 cnt = 0;
519 #ifdef USE_OPENMP
520 #pragma omp parallel for schedule(dynamic) reduction(+:cnt)
521 #endif
522  for (int i = 0; i < HigherDegNbrV.Len(); i++) {
523  for (int j = 0; j < HigherDegNbrV[i].Len(); j++) {
524  //TInt NbrInd = H.GetDat(HigherDegNbrV[i][j]);
525  TInt NbrInd = IndV[HigherDegNbrV[i][j]];
526 
527  int64 num = GetCommon(HigherDegNbrV[i], HigherDegNbrV[NbrInd]);
528  cnt += num;
529  }
530  }
531 
532  gettimeofday(&end, NULL);
533  Profiler.StopTimer(TimerId);
534  delta = ((end.tv_sec - start.tv_sec) * 1000000u +
535  end.tv_usec - start.tv_usec) / 1.e6;
536  printf("__count__\ttime %7.3f\tcpu %8.3f\n", delta, Profiler.GetTimerSec(TimerId));
537 
538  gettimeofday(&endall, NULL);
539  Profiler.StopTimer(TimerAll);
540  delta = ((endall.tv_sec - startall.tv_sec) * 1000000u +
541  endall.tv_usec - startall.tv_usec) / 1.e6;
542  printf("__all__ \ttime %7.3f\tcpu %8.3f\n", delta, Profiler.GetTimerSec(TimerAll));
543 
544  return cnt;
545 }
double GetTimerSec(const int &TimerId) const
Definition: tm.cpp:1293
int AddTimer(const TStr &TimerNm)
Definition: tm.cpp:1272
void StopTimer(const TStr &TimerNm)
Definition: tm.h:419
void StartTimer(const TStr &TimerNm)
Definition: tm.h:416
Definition: dt.h:1044
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:54
void ResetTimer(const TStr &TimerNm)
Definition: tm.h:423
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
template<class PGraph >
PGraph TSnap::GetUnDir ( const PGraph &  Graph)

Returs an undirected version of the graph. For every edge (u,v) an edge (v,u) is added (if it does not yet exist).

Definition at line 345 of file alg.h.

345  {
346  PGraph NewGraphPt = PGraph::New();
347  *NewGraphPt = *Graph;
348  MakeUnDir(NewGraphPt);
349  return NewGraphPt;
350 }
void MakeUnDir(const PGraph &Graph)
Makes the graph undirected. For every edge (u,v) an edge (v,u) is added (if it does not yet exist)...
Definition: alg.h:353
template<class PGraph >
void TSnap::GetUniqueNbrV ( const PGraph &  Graph,
const int &  NId,
TIntV NbrV 
)

Returns sorted vector NbrV containing unique in or out neighbors of node NId in graph Graph.

Definition at line 800 of file triad.h.

800  {
801  typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
802  NbrV.Reserve(NI.GetDeg());
803  NbrV.Reduce(0);
804 
805  int j = 0;
806  int k = 0;
807  int Prev = -1;
808  int InDeg = NI.GetInDeg();
809  int OutDeg = NI.GetOutDeg();
810  if (InDeg > 0 && OutDeg > 0) {
811  int v1 = NI.GetInNId(j);
812  int v2 = NI.GetOutNId(k);
813  while (1) {
814  if (v1 <= v2) {
815  if (Prev != v1) {
816  if (v1 != NId) {
817  NbrV.Add(v1);
818  Prev = v1;
819  }
820  }
821  j += 1;
822  if (j >= InDeg) {
823  break;
824  }
825  v1 = NI.GetInNId(j);
826  } else {
827  if (Prev != v2) {
828  if (v2 != NId) {
829  NbrV.Add(v2);
830  }
831  Prev = v2;
832  }
833  k += 1;
834  if (k >= OutDeg) {
835  break;
836  }
837  v2 = NI.GetOutNId(k);
838  }
839  }
840  }
841  while (j < InDeg) {
842  int v = NI.GetInNId(j);
843  if (Prev != v) {
844  if (v != NId) {
845  NbrV.Add(v);
846  }
847  Prev = v;
848  }
849  j += 1;
850  }
851  while (k < OutDeg) {
852  int v = NI.GetOutNId(k);
853  if (Prev != v) {
854  if (v != NId) {
855  NbrV.Add(v);
856  }
857  Prev = v;
858  }
859  k += 1;
860  }
861 }
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:528
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
template<class PGraph >
void TSnap::GetWccs ( const PGraph &  Graph,
TCnComV CnComV 
)

Returns all weakly connected components in a Graph.

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

Definition at line 376 of file cncom.h.

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

Returns a distribution of weakly connected component sizes.

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

Definition at line 337 of file cncom.h.

337  {
338  THashSet<TInt> VisitedNId(Graph->GetNodes());
339  TIntH SzToCntH;
340  TSnapQueue<int> NIdQ(Graph->GetNodes()+1);
341  typename PGraph::TObj::TNodeI NI;
342  int Cnt = 0;
343  // zero degree nodes
344  for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
345  if (NI.GetDeg() == 0) { Cnt++; VisitedNId.AddKey(NI.GetId()); }
346  }
347  if (Cnt > 0) SzToCntH.AddDat(1, Cnt);
348  // the rest of the nodes
349  for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
350  if (! VisitedNId.IsKey(NI.GetId())) {
351  VisitedNId.AddKey(NI.GetId());
352  NIdQ.Clr(false); NIdQ.Push(NI.GetId());
353  Cnt = 0;
354  while (! NIdQ.Empty()) {
355  const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
356  if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
357  for (int e = 0; e < Node.GetInDeg(); e++) {
358  const int InNId = Node.GetInNId(e);
359  if (! VisitedNId.IsKey(InNId)) { NIdQ.Push(InNId); VisitedNId.AddKey(InNId); }
360  }
361  }
362  for (int e = 0; e < Node.GetOutDeg(); e++) {
363  const int OutNId = Node.GetOutNId(e);
364  if (! VisitedNId.IsKey(OutNId)) { NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); }
365  }
366  Cnt++;
367  }
368  SzToCntH.AddDat(Cnt) += 1;
369  }
370  }
371  SzToCntH.GetKeyDatPrV(WccSzCnt);
372  WccSzCnt.Sort(true);
373 }
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
int AddKey(const TKey &Key)
Definition: shash.h:1254
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
Definition: gbase.h:13
void TSnap::GetWeightedBetweennessCentr ( const PNEANet  Graph,
const TIntV BtwNIdV,
TIntFltH NodeBtwH,
const bool &  IsDir,
const bool &  DoNodeCent,
TIntPrFltH EdgeBtwH,
const bool &  DoEdgeCent,
const TFltV Attr 
)

Computes (approximate) weighted Beetweenness Centrality of all nodes and all edges of the network.

Definition at line 752 of file centr.cpp.

752  {
753  if (DoNodeCent) { NodeBtwH.Clr(); }
754  if (DoEdgeCent) { EdgeBtwH.Clr(); }
755  const int nodes = Graph->GetNodes();
756  TIntS S(nodes);
757  TIntQ Q(nodes);
758  TIntIntVH P(nodes); // one vector for every node
759  TIntFltH delta(nodes);
760  TIntFltH sigma(nodes), d(nodes);
761  // init
762  for (PNEANet::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
763  if (DoNodeCent) {
764  NodeBtwH.AddDat(NI.GetId(), 0); }
765  if (DoEdgeCent) {
766  for (int e = 0; e < NI.GetOutDeg(); e++) {
767  if (NI.GetId() < NI.GetOutNId(e)) {
768  EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
769  }
770  }
771  }
772  sigma.AddDat(NI.GetId(), 0);
773  d.AddDat(NI.GetId(), -1);
774  P.AddDat(NI.GetId(), TIntV());
775  delta.AddDat(NI.GetId(), 0);
776  }
777  // calc betweeness
778  for (int k=0; k < BtwNIdV.Len(); k++) {
779  const PNEANet::TObj::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
780  // reset
781  for (int i = 0; i < sigma.Len(); i++) {
782  sigma[i]=0; d[i]=-1; delta[i]=0; P[i].Clr(false);
783  }
784  S.Clr(false);
785  Q.Clr(false);
786  sigma.AddDat(NI.GetId(), 1);
787  d.AddDat(NI.GetId(), 0);
788  Q.Push(NI.GetId());
789  while (! Q.Empty()) {
790  const int v = Q.Top(); Q.Pop();
791  const PNEANet::TObj::TNodeI NI2 = Graph->GetNI(v);
792  S.Push(v);
793  const double VDat = d.GetDat(v);
794  for (int e = 0; e < NI2.GetOutDeg(); e++) {
795  const int w = NI2.GetOutNId(e);
796  const int eid = NI2.GetOutEId(e);
797 
798  if (d.GetDat(w) < 0) { // find w for the first time
799  Q.Push(w);
800  d.AddDat(w, VDat+Attr[eid]);
801  }
802  //shortest path to w via v ?
803  if (d.GetDat(w) == VDat+Attr[eid]) {
804  sigma.AddDat(w) += sigma.GetDat(v);
805  P.GetDat(w).Add(v);
806  }
807  }
808  }
809 
810  while (! S.Empty()) {
811  const int w = S.Top();
812  const double SigmaW = sigma.GetDat(w);
813  const double DeltaW = delta.GetDat(w);
814  const TIntV NIdV = P.GetDat(w);
815  S.Pop();
816  for (int i = 0; i < NIdV.Len(); i++) {
817  const int NId = NIdV[i];
818  const double c = (sigma.GetDat(NId)*1.0/SigmaW) * (1+DeltaW);
819  delta.AddDat(NId) += c;
820  if (DoEdgeCent) {
821  EdgeBtwH.AddDat(TIntPr(TMath::Mn(NId, w), TMath::Mx(NId, w))) += c; }
822  }
823  double factor = (IsDir) ? 1.0 : 2.0;
824  if (DoNodeCent && w != NI.GetId()) {
825  NodeBtwH.AddDat(w) += delta.GetDat(w)/factor; }
826  }
827  }
828 }
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:547
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:807
TVec< TInt > TIntV
Definition: ds.h:1529
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void TSnap::GetWeightedBetweennessCentr ( const PNEANet  Graph,
TIntFltH NIdBtwH,
TIntPrFltH EdgeBtwH,
const TFltV Attr,
const bool &  IsDir = false,
const double &  NodeFrac = 1.0 
)

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 830 of file centr.cpp.

830  {
831  TIntV NIdV; Graph->GetNIdV(NIdV);
832  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
833  NIdV.Shuffle(TInt::Rnd);
834  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
835  NIdV.DelLast(); }
836  }
837  GetWeightedBetweennessCentr(Graph, NIdV, NodeBtwH, IsDir, true, EdgeBtwH, true, Attr
838  );
839 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void GetWeightedBetweennessCentr(const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const bool &IsDir, const double &NodeFrac)
Definition: centr.cpp:852
static TRnd Rnd
Definition: dt.h:1053
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
void DelLast()
Removes the last element of the vector.
Definition: ds.h:635
void TSnap::GetWeightedBetweennessCentr ( const PNEANet  Graph,
TIntFltH NIdBtwH,
const TFltV Attr,
const bool &  IsDir = false,
const double &  NodeFrac = 1.0 
)

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 841 of file centr.cpp.

841  {
842  TIntPrFltH EdgeBtwH;
843  TIntV NIdV; Graph->GetNIdV(NIdV);
844  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
845  NIdV.Shuffle(TInt::Rnd);
846  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
847  NIdV.DelLast(); }
848  }
849  GetWeightedBetweennessCentr(Graph, NIdV, NodeBtwH, IsDir, true, EdgeBtwH, false, Attr);
850 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void GetWeightedBetweennessCentr(const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const bool &IsDir, const double &NodeFrac)
Definition: centr.cpp:852
static TRnd Rnd
Definition: dt.h:1053
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
void DelLast()
Removes the last element of the vector.
Definition: ds.h:635
void TSnap::GetWeightedBetweennessCentr ( const PNEANet  Graph,
TIntPrFltH EdgeBtwH,
const TFltV Attr,
const bool &  IsDir = false,
const double &  NodeFrac = 1.0 
)

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 852 of file centr.cpp.

852  {
853  TIntFltH NodeBtwH;
854  TIntV NIdV; Graph->GetNIdV(NIdV);
855  if (NodeFrac < 1.0) { // calculate beetweenness centrality for a subset of nodes
856  NIdV.Shuffle(TInt::Rnd);
857  for (int i = int((1.0-NodeFrac)*NIdV.Len()); i > 0; i--) {
858  NIdV.DelLast(); }
859  }
860  GetWeightedBetweennessCentr(Graph, NIdV, NodeBtwH, IsDir, false, EdgeBtwH, true, Attr);
861 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void GetWeightedBetweennessCentr(const PNEANet Graph, TIntPrFltH &EdgeBtwH, const TFltV &Attr, const bool &IsDir, const double &NodeFrac)
Definition: centr.cpp:852
static TRnd Rnd
Definition: dt.h:1053
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
void DelLast()
Removes the last element of the vector.
Definition: ds.h:635
double TSnap::GetWeightedClosenessCentr ( const PNEANet  Graph,
const int &  NId,
const bool &  IsDir,
const TFltV Attr,
const bool &  Normalized = true 
)

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

Definition at line 745 of file centr.cpp.

745  {
746  const double Farness = GetWeightedFarnessCentr(Graph, NId, IsDir, Attr, Normalized);
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 bool &IsDir, const TFltV &Attr, const bool &Normalized)
Definition: centr.cpp:726
double TSnap::GetWeightedFarnessCentr ( const PNEANet  Graph,
const int &  NId,
const bool &  IsDir,
const TFltV Attr,
const bool &  Normalized = true 
)

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

Definition at line 726 of file centr.cpp.

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

Weighted PageRank (TODO: Use template)

Definition at line 396 of file centr.cpp.

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

Definition at line 443 of file centr.cpp.

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

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

Definition at line 700 of file centr.cpp.

701  {
702  TIntV frontier;
703 
704  NIdDistH.Clr(false); NIdDistH.AddDat(SrcNId, 0);
705  frontier.Add(SrcNId);
706  while (! frontier.Empty()) {
707  const int NId = findMinimum(frontier, NIdDistH);
708  const PNEANet::TObj::TNodeI NodeI = Graph->GetNI(NId);
709  for (int v = 0; v < NodeI.GetOutDeg(); v++) {
710  int DstNId = NodeI.GetOutNId(v);
711  int EId = NodeI.GetOutEId(v);
712 
713  if (! NIdDistH.IsKey(DstNId)) {
714  NIdDistH.AddDat(DstNId, NIdDistH.GetDat(NId) + Attr[EId]);
715  frontier.Add(DstNId);
716  } else {
717  if (NIdDistH[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:220
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
bool IsKey(const TKey &Key) const
Definition: hash.h:216
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void TSnap::GlobalRelabel ( PNEANet Net,
TPRManager &  PRM,
const int &  SrcNId,
const int &  SnkNId 
)

Implements the Global Relabeling heuristic.

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

Definition at line 363 of file flow.cpp.

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

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
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
Definition: cncom.h:88
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
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:1271
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
int GetId() const
Returns ID of the current node.
Definition: graph.h:84
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
double Equation(TIntFltH &PAlpha, double &SumPAlphaLogPAlpha, TIntFltH &Qi)
Definition: cmty.cpp:109
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void SortByDat(const bool &Asc=true)
Definition: hash.h:250
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.

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:64
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
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
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
int Len() const
Definition: hash.h:186
void SortByDat(const bool &Asc=true)
Definition: hash.h:250
int TSnap::Intersect ( TUNGraph::TNodeI  Node,
TIntH  NNodes 
)

Intersect.

Definition at line 584 of file centr.cpp.

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

Intersect.

Definition at line 597 of file centr.cpp.

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

Intersect.

Definition at line 621 of file centr.cpp.

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

Definition at line 650 of file centr.cpp.

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

Definition at line 4 of file flow.cpp.

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

Definition at line 163 of file gsvd.cpp.

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

Tests whether the Graph is (weakly) connected.

Definition at line 305 of file cncom.h.

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

Definition at line 460 of file alg.h.

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

Tests whether the Graph is weakly connected.

Definition at line 310 of file cncom.h.

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

Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in a single line.

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

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

Definition at line 169 of file gio.h.

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

Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in a single line.

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

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

Definition at line 193 of file gio.h.

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

Loads the edges from the TTable and EdgeAttrV specifies columns containing edge attributes.

Definition at line 69 of file conv.cpp.

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

Loads a crossnet from Mode1 to Mode2, with name CrossName, from the provided TTable. EdgeAttrV specifies edge attributes.

Definition at line 60 of file conv.cpp.

62 {
63  Graph->AddCrossNet(Mode1, Mode2, CrossName);
64  TCrossNet& Net = Graph->GetCrossNetByName(CrossName);
65  return LoadCrossNet(Net, Table, SrcCol, DstCol, EdgeAttrV);
66 }
int LoadCrossNet(TCrossNet &Graph, PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV)
Loads the edges from the TTable and EdgeAttrV specifies columns containing edge attributes.
Definition: conv.cpp:69
Implements a single CrossNet consisting of edges between two TModeNets (could be the same TModeNet) ...
Definition: mmnet.h:124
PNGraph TSnap::LoadDyNet ( const TStr FNm)

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

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

Definition at line 296 of file gio.cpp.

296  {
298  THashSet<TStr> NIdStr;
299  while (XmlLx.GetSym()!=xsyEof) {
300  if (XmlLx.Sym==xsySTag && XmlLx.TagNm=="network") {
301  PNGraph G = TNGraph::New();
302  XmlLx.GetSym();
303  while (XmlLx.TagNm=="link") {
304  TStr Str1, Val1, Str2, Val2;
305  XmlLx.GetArg(0, Str1, Val1); XmlLx.GetArg(1, Str2, Val2);
306  IAssert(Str1=="source" && Str2=="target");
307  NIdStr.AddKey(Val1); NIdStr.AddKey(Val2);
308  const int src=NIdStr.GetKeyId(Val1);
309  const int dst=NIdStr.GetKeyId(Val2);
310  if (! G->IsNode(src)) { G->AddNode(src); }
311  if (! G->IsNode(dst)) { G->AddNode(dst); }
312  G->AddEdge(src, dst);
313  XmlLx.GetSym();
314  }
315  return G;
316  }
317  }
318  return PNGraph();
319 }
#define IAssert(Cond)
Definition: bd.h:262
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
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 IDs 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:477
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
TVec< PNGraph > TSnap::LoadDyNetGraphV ( const TStr FNm)

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

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

Definition at line 322 of file gio.cpp.

322  {
324  TVec<PNGraph> GraphV;
325  THashSet<TStr> NIdStr;
326  while (XmlLx.GetSym()!=xsyEof) {
327  if (XmlLx.Sym==xsySTag && XmlLx.TagNm=="network") {
328  PNGraph G = TNGraph::New();
329  GraphV.Add(G);
330  XmlLx.GetSym();
331  while (XmlLx.TagNm=="link") {
332  TStr Str1, Val1, Str2, Val2;
333  XmlLx.GetArg(0, Str1, Val1); XmlLx.GetArg(1, Str2, Val2);
334  IAssert(Str1=="source" && Str2=="target");
335  NIdStr.AddKey(Val1); NIdStr.AddKey(Val2);
336  const int src=NIdStr.GetKeyId(Val1);
337  const int dst=NIdStr.GetKeyId(Val2);
338  if (! G->IsNode(src)) { G->AddNode(src); }
339  if (! G->IsNode(dst)) { G->AddNode(dst); }
340  G->AddEdge(src, dst);
341  XmlLx.GetSym();
342  }
343  }
344  }
345  return GraphV;
346 }
#define IAssert(Cond)
Definition: bd.h:262
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
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 IDs 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:477
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:574
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
template<class PGraph >
PGraph TSnap::LoadEdgeList ( const TStr InFNm,
const int &  SrcColId,
const int &  DstColId 
)

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

Loads the format saved by TSnap::SaveEdgeList()

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

Definition at line 84 of file gio.h.

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

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

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

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

Definition at line 105 of file gio.h.

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

Loads a network from the text file InFNm with 1 node/edge per line ('Separator' separated columns, integer node id(s) + node/edge attributes).

Definition at line 138 of file gio.cpp.

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

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

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

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

Definition at line 126 of file gio.h.

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

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

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

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

Definition at line 149 of file gio.h.

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

Loads the nodes specified in column NCol from the TTable with the attributes specified in NodeAttrV.

Definition at line 14 of file conv.cpp.

15  {
16 
17  const TAttrType NodeType = Table->GetColType(NCol);
18  const TInt NColIdx = Table->GetColIdx(NCol);
19 
20  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
21  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
22  continue;
23  }
24 
25  // add src and dst nodes to graph if they are not seen earlier
26  TInt NVal;
27  if (NodeType == atFlt) {
28  return -1;
29  } else if (NodeType == atInt || NodeType == atStr) {
30  if (NodeType == atInt) {
31  NVal = (Table->IntCols)[NColIdx][CurrRowIdx];
32  } else {
33  NVal = (Table->StrColMaps)[NColIdx][CurrRowIdx];
34  if (strlen(Table->GetContextKey(NVal)) == 0) { continue; } //illegal value
35  }
36  if (!Graph.IsNode(NVal)) {Graph.AddNode(NVal); }
37  }
38 
39  // Aggregate edge attributes and add to graph
40  for (TInt i = 0; i < NodeAttrV.Len(); i++) {
41  TStr ColName = NodeAttrV[i];
42  TAttrType T = Table->GetColType(ColName);
43  TInt Index = Table->GetColIdx(ColName);
44  switch (T) {
45  case atInt:
46  Graph.AddIntAttrDatN(NVal, Table->IntCols[Index][CurrRowIdx], ColName);
47  break;
48  case atFlt:
49  Graph.AddFltAttrDatN(NVal, Table->FltCols[Index][CurrRowIdx], ColName);
50  break;
51  case atStr:
52  Graph.AddStrAttrDatN(NVal, Table->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:547
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: network.cpp:381
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:2267
int AddStrAttrDatN(const TNodeI &NodeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: network.h:2262
Definition: dt.h:1044
Definition: dt.h:412
Definition: gbase.h:23
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:1985
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:2257
int TSnap::LoadModeNetToNet ( PMMNet  Graph,
const TStr Name,
PTable  Table,
const TStr NCol,
TStrV NodeAttrV 
)

Loads a mode, with name Name, into the PMMNet from the TTable. NCol specifies the node id column and NodeAttrV the node attributes.

Definition at line 6 of file conv.cpp.

7  {
8  Graph->AddModeNet(Name);
9  TModeNet& Net = Graph->GetModeNetByName(Name);
10  return LoadMode(Net, Table, NCol, NodeAttrV);
11 }
int LoadMode(TModeNet &Graph, PTable Table, const TStr &NCol, TStrV &NodeAttrV)
Loads the nodes specified in column NCol from the TTable with the attributes specified in NodeAttrV...
Definition: conv.cpp:14
The nodes of one particular mode in a TMMNet, and their neighbor vectors as TIntV attributes...
Definition: mmnet.h:23
TIntH TSnap::LoadNodeList ( TStr  InFNmNodes)

Definition at line 671 of file centr.cpp.

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

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

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

Definition at line 210 of file gio.h.

210  {
211  PGraph Graph = PGraph::TObj::New();
212  TSsParser Ss(InFNm, ssfSpaceSep, true, true, true);
213  while ((Ss.Len()==0 || strstr(Ss[0], "*vertices") == NULL) && ! Ss.Eof()) {
214  Ss.Next(); Ss.ToLc(); }
215  // nodes
216  bool EdgeList = true;
217  EAssert(strstr(Ss[0], "*vertices") != NULL);
218  while (Ss.Next()) {
219  Ss.ToLc();
220  if (Ss.Len()>0 && Ss[0][0] == '%') { continue; } // comment
221  if (strstr(Ss[0], "*arcslist")!=NULL || strstr(Ss[0],"*edgeslist")!=NULL) { EdgeList=false; break; }
222  if (strstr(Ss[0], "*arcs")!=NULL || strstr(Ss[0],"*edges")!=NULL) { break; } // arcs are directed, edges are undirected
223  Graph->AddNode(Ss.GetInt(0));
224  }
225  // edges
226  while (Ss.Next()) {
227  if (Ss.Len()>0 && Ss[0][0] == '%') { continue; } // comment
228  if (Ss.Len()>0 && Ss[0][0] == '*') { break; }
229  if (EdgeList) {
230  // <source> <destination> <weight>
231  if (Ss.Len() >= 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
template<class PGraph >
void TSnap::MakeUnDir ( const PGraph &  Graph)

Makes the graph undirected. For every edge (u,v) an edge (v,u) is added (if it does not yet exist).

Definition at line 353 of file alg.h.

353  {
354  CAssert(HasGraphFlag(typename PGraph::TObj, gfDirected)); // graph has to be directed
355  TIntPrV EdgeV;
356  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
357  const int SrcNId = EI.GetSrcNId();
358  const int DstNId = EI.GetDstNId();
359  if (! Graph->IsEdge(DstNId, SrcNId)) {
360  EdgeV.Add(TIntPr(DstNId, SrcNId));
361  }
362  }
363  for (int i = 0; i < EdgeV.Len(); i++) {
364  Graph->AddEdge(EdgeV[i].Val1, EdgeV[i].Val2);
365  }
366 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
#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:574
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 1743 of file table.h.

1744  {
1745  int NumGraphs = GraphSeq.Len();
1746  TableSeq.Reserve(NumGraphs, NumGraphs);
1747  // This loop is parallelizable.
1748  for (TInt i = 0; i < NumGraphs; i++){
1749  TIntFltH HubH;
1750  TIntFltH AuthH;
1751  GetHits(GraphSeq[i], HubH, AuthH, MaxIter);
1752  PTable HubT = TTable::TableFromHashMap(HubH, "NodeId", "Hub", Context, false);
1753  PTable AuthT = TTable::TableFromHashMap(AuthH, "NodeId", "Authority", Context, false);
1754  PTable HitsT = HubT->Join("NodeId", AuthT, "NodeId");
1755  HitsT->Rename("1.NodeId", "NodeId");
1756  HitsT->Rename("1.Hub", "Hub");
1757  HitsT->Rename("2.Authority", "Authority");
1758  TStrV V = TStrV(3, 0);
1759  V.Add("NodeId");
1760  V.Add("Hub");
1761  V.Add("Authority");
1762  HitsT->ProjectInPlace(V);
1763  TableSeq[i] = HitsT;
1764  }
1765  }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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:979
void GetHits(const PGraph &Graph, TIntFltH &NIdHubH, TIntFltH &NIdAuthH, const int &MaxIter=20)
Definition: centr.h:485
Definition: dt.h:1044
TVec< TStr > TStrV
Definition: ds.h:1534
Definition: bd.h:196
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
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 1729 of file table.h.

1730  {
1731  int NumGraphs = GraphSeq.Len();
1732  TableSeq.Reserve(NumGraphs, NumGraphs);
1733  // This loop is parallelizable.
1734  for (TInt i = 0; i < NumGraphs; i++){
1735  TIntFltH PRankH;
1736  GetPageRank(GraphSeq[i], PRankH, C, Eps, MaxIter);
1737  TableSeq[i] = TTable::TableFromHashMap(PRankH, "NodeId", "PageRank", Context, false);
1738  }
1739  }
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:547
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:979
Definition: dt.h:1044
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TIntH TSnap::MaxCPGreedyBetter ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 175 of file centr.cpp.

175  {
176  TIntH GroupNodes; // buildup cpntainer of group nodes
177  TIntH NNodes; // container of neighbouring nodes
178  TIntH Nodes; // nodes sorted by vd
179  double gc = 0, gc0 = 0;
180  int addId = 0, addIdPrev = 0;
181 
182  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
183  Nodes.AddDat(NI.GetId(),NI.GetDeg());
184  }
185 
186  Nodes.SortByDat(false);
187 
188  int br = 0;
189  while (br < k) {
190  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++) {
191  if ((NI.GetDat() <= (int)gc0))
192  break;
193  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
194  if (gc>gc0) {
195  gc0 = gc;
196  addId = NI.GetKey();
197  }
198  }
199 
200  if (addId != addIdPrev){
201 
202  GroupNodes.AddDat(br,addId);
203  br++;
204  gc0=0;
205 
206  NNodes.AddDat(addId,0);
207  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
208  NNodes.AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
209  }
210  addIdPrev = addId;
211  Nodes.DelKey(addId);
212  } else {
213  br = k;
214  }
215  printf("%i,",br);
216  }
217 
218  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
219  return GroupNodes;
220 }
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
TIter BegI() const
Definition: hash.h:171
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
TIter EndI() const
Definition: hash.h:176
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
void DelKey(const TKey &Key)
Definition: hash.h:362
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void SortByDat(const bool &Asc=true)
Definition: hash.h:250
TIntH TSnap::MaxCPGreedyBetter1 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 223 of file centr.cpp.

223  {
224  TIntH GroupNodes;
225  TIntH NNodes;
226  TIntH Nodes;
227  double gc = 0, gc0 = 0;
228  int addId = 0, addIdPrev = 0;
229 
230  // put nodes in the container and sort them by vertex degree
231  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
232  Nodes.AddDat(NI.GetId(),NI.GetDeg());
233  }
234  Nodes.SortByDat(false);
235 
236  int br = 0;
237  while (br < k) {
238  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
239  if((NI.GetDat() < (int)gc0))
240  break;
241  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
242  if (gc>gc0) {
243  gc0 = gc;
244  addId = NI.GetKey();
245  }
246  }
247 
248  if (addId != addIdPrev){
249 
250  GroupNodes.AddDat(br,addId);
251  br++;
252  gc0=-10000000;
253 
254  NNodes.AddDat(addId,0);
255  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
256  NNodes.AddDat(Graph->GetNI(addId).GetNbrNId(i),0);
257  }
258  addIdPrev = addId;
259  Nodes.DelKey(addId);
260  }
261  }
262 
263  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
264  return GroupNodes;
265 }
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
TIter BegI() const
Definition: hash.h:171
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
TIter EndI() const
Definition: hash.h:176
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
void DelKey(const TKey &Key)
Definition: hash.h:362
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void SortByDat(const bool &Asc=true)
Definition: hash.h:250
TIntH TSnap::MaxCPGreedyBetter2 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 268 of file centr.cpp.

268  {
269  TIntH GroupNodes; // buildup cpntainer of group nodes
270  TStr NNodes; // container of neighbouring nodes
271  TIntH Nodes; // nodes sorted by vd
272  double gc = 0, gc0 = 0;
273  int addId = 0, addIdPrev=0;
274 
275  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
276  Nodes.AddDat(NI.GetId(),NI.GetDeg());
277  }
278 
279  Nodes.SortByDat(false);
280 
281  int br=0;
282  while (br < k) {
283  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
284  if((NI.GetDat() <= (int)gc0))
285  break;
286  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes);
287  if (gc>gc0) {
288  gc0 = gc;
289  addId = NI.GetKey();
290  }
291  }
292 
293  if (addId != addIdPrev) {
294 
295  GroupNodes.AddDat(br,addId);
296  br++;
297  gc0=0;
298 
299  TInt digi = addId;
300  TStr buf = digi.GetStr();
301 
302  NNodes += " "+buf;
303 
304  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
305  TInt digi = Graph->GetNI(addId).GetNbrNId(i);
306  TStr buf = digi.GetStr();
307  NNodes += " "+buf;
308  }
309  addIdPrev = addId;
310  Nodes.DelKey(addId);
311  } else {
312  br = k;
313  }
314  printf("%i,",br);
315  }
316 
317  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
318  return GroupNodes;
319 }
TStr GetStr() const
Definition: dt.h:1107
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
TIter BegI() const
Definition: hash.h:171
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
TIter EndI() const
Definition: hash.h:176
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
void DelKey(const TKey &Key)
Definition: hash.h:362
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
Definition: dt.h:1044
Definition: dt.h:412
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void SortByDat(const bool &Asc=true)
Definition: hash.h:250
TIntH TSnap::MaxCPGreedyBetter3 ( const PUNGraph Graph,
const int  k 
)

Returns centrality Maximum k group.

Definition at line 322 of file centr.cpp.

322  {
323  TIntH GroupNodes; // buildup cpntainer of group nodes
324  const int n = Graph->GetNodes();
325  int *NNodes = new int[n]; // container of neighbouring nodes
326  int NNodes_br = 0;
327  TIntH Nodes; // nodes sorted by vd
328  double gc = 0, gc0 = 0;
329  int addId = 0, addIdPrev = 0;
330 
331  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
332  Nodes.AddDat(NI.GetId(),NI.GetDeg());
333  }
334 
335  Nodes.SortByDat(false);
336 
337  int br = 0;
338  while (br < k) {
339  for (THashKeyDatI<TInt,TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
340  if((NI.GetDat() <= (int)gc0))
341  break;
342  gc = NI.GetDat()-Intersect(Graph->GetNI(NI.GetKey()),NNodes,NNodes_br);
343  if (gc>gc0){
344  gc0 = gc;
345  addId = NI.GetKey();
346  }
347  }
348 
349  if (addId != addIdPrev) {
350 
351  GroupNodes.AddDat(br,addId);
352  br++;
353  gc0=0;
354 
355  int nn = addId;
356  bool nnnew = true;
357  for (int j=0; j<NNodes_br; j++)
358  if (NNodes[j] == nn){
359  nnnew = false;
360  j = NNodes_br;
361  }
362 
363  if (nnnew){
364  NNodes[NNodes_br] = nn;
365  NNodes_br++;
366  }
367 
368  for (int i=0; i<Graph->GetNI(addId).GetDeg(); i++) {
369  int nn = Graph->GetNI(addId).GetNbrNId(i);
370  bool nnnew = true;
371  for (int j=0; j<NNodes_br; j++) {
372  if (NNodes[j] == nn){
373  nnnew = false;
374  j = NNodes_br;
375  }
376  }
377  if (nnnew){
378  NNodes[NNodes_br] = nn;
379  NNodes_br++;
380  }
381  }
382  addIdPrev = addId;
383  Nodes.DelKey(addId);
384  } else {
385  br = k;
386  }
387  printf("%i,",br);
388  }
389 
390  delete NNodes;
391  // gcFinal = GetGroupDegreeCentr(Graph, GroupNodes);
392  return GroupNodes;
393 }
int Intersect(TUNGraph::TNodeI Node, int *NNodes, int NNodes_br)
Intersect.
Definition: centr.cpp:621
TIter BegI() const
Definition: hash.h:171
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
TIter EndI() const
Definition: hash.h:176
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
void DelKey(const TKey &Key)
Definition: hash.h:362
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void SortByDat(const bool &Asc=true)
Definition: hash.h:250
template<class PGraph >
void TSnap::MergeNbrs ( TIntV NeighbourV,
const typename PGraph::TObj::TNodeI &  NI 
)

Merges neighbors.

Definition at line 547 of file triad.h.

547  {
548  int j = 0;
549  int k = 0;
550  int prev = -1;
551  int indeg = NI.GetInDeg();
552  int outdeg = NI.GetOutDeg();
553  //while (j < NI.GetInDeg() && k < NI.GetOutDeg()) {
554  if (indeg > 0 && outdeg > 0) {
555  int v1 = NI.GetInNId(j);
556  int v2 = NI.GetOutNId(k);
557  while (1) {
558  if (v1 <= v2) {
559  if (prev != v1) {
560  NeighbourV.Add(v1);
561  prev = v1;
562  }
563  j += 1;
564  if (j >= indeg) {
565  break;
566  }
567  v1 = NI.GetInNId(j);
568  } else {
569  if (prev != v2) {
570  NeighbourV.Add(v2);
571  prev = v2;
572  }
573  k += 1;
574  if (k >= outdeg) {
575  break;
576  }
577  v2 = NI.GetOutNId(k);
578  }
579  }
580  }
581  while (j < indeg) {
582  int v = NI.GetInNId(j);
583  if (prev != v) {
584  NeighbourV.Add(v);
585  prev = v;
586  }
587  j += 1;
588  }
589  while (k < outdeg) {
590  int v = NI.GetOutNId(k);
591  if (prev != v) {
592  NeighbourV.Add(v);
593  prev = v;
594  }
595  k += 1;
596  }
597 }
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
void TSnap::NumpyToTFltV ( TFltV FltV,
float *  FltNumpyVecIn,
int  n 
)

Converts NumpyArray to TFltV.

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

Definition at line 33 of file numpy.cpp.

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

Converts NumpyArray to TIntV.

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

Definition at line 25 of file numpy.cpp.

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

Definition at line 205 of file coreper.cpp.

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

Plots the distribution of clustering coefficient of a Graph.

Definition at line 111 of file statplot.h.

111  {
112  TFltPrV DegToCCfV;
113  int64 ClosedTriads, OpenTriads;
114  const double CCF = GetClustCf(Graph, DegToCCfV, ClosedTriads, OpenTriads);
115  if (DescStr.Empty()) { DescStr = FNmPref; }
116  TGnuPlot GnuPlot("ccf."+FNmPref,
117  TStr::Fmt("%s. G(%d, %d). Average clustering: %.4f OpenTriads: %d (%.4f) ClosedTriads: %d (%.4f)", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(),
118  CCF, OpenTriads, OpenTriads/double(OpenTriads+ClosedTriads), ClosedTriads, ClosedTriads/double(OpenTriads+ClosedTriads)));
119  GnuPlot.AddPlot(DegToCCfV, gpwLinesPoints, "", "pt 6");
120  GnuPlot.SetXYLabel("Node degree", "Average clustering coefficient");
121  GnuPlot.SetScale(gpsLog10XY);
122  GnuPlot.SavePng();
123 }
long long int64
Definition: bd.h:27
bool Empty() const
Definition: dt.h: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:117
char * CStr()
Definition: dt.h:476
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
void TSnap::PlotEigValDistr ( const PUNGraph Graph,
const int &  EigVals,
const TStr FNmPref,
TStr  DescStr 
)

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

Definition at line 14 of file statplot.cpp.

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

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

Definition at line 5 of file statplot.cpp.

5  {
6  TFltV EigValV;
7  TSnap::GetEigVals(Graph, EigVals, EigValV);
8  EigValV.Sort(false);
9  if (DescStr.Empty()) { DescStr = FNmPref; }
10  TGnuPlot::PlotValV(EigValV, "eigVal."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f",
11  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), EigValV[0].Val), "Rank", "Eigen value", gpsLog10XY, false, gpwLinesPoints);
12 }
void GetEigVals(const PUNGraph &Graph, const int &EigVals, TFltV &EigValV)
Computes top EigVals eigenvalues of the adjacency matrix representing a given undirected Graph...
Definition: gsvd.cpp:308
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
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
template<class PGraph >
void TSnap::PlotHops ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr(),
const bool &  IsDir = false,
const int &  NApprox = 32 
)

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

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

Definition at line 126 of file statplot.h.

126  {
127  TIntFltKdV DistNbrsV;
128  TSnap::GetAnf(Graph, DistNbrsV, -1, IsDir, NApprox);
129  const double EffDiam = TSnap::TSnapDetail::CalcEffDiam(DistNbrsV, 0.9);
130  if (DescStr.Empty()) { DescStr = FNmPref; }
131  TGnuPlot GnuPlot("hop."+FNmPref, TStr::Fmt("%s. Hop plot. EffDiam: %g, G(%d, %d)",
132  DescStr.CStr(), EffDiam, Graph->GetNodes(), Graph->GetEdges()));
133  GnuPlot.SetXYLabel("Number of hops", "Number of pairs of nodes");
134  GnuPlot.SetScale(gpsLog10Y);
135  GnuPlot.AddPlot(DistNbrsV, gpwLinesPoints, "", "pt 6");
136  GnuPlot.SavePng();
137 }
void GetAnf(const PGraph &Graph, const int &SrcNId, TIntFltKdV &DistNbrsV, const int &MxDist, const bool &IsDir, const int &NApprox=32)
Definition: anf.h:205
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:429
template<class PGraph >
void TSnap::PlotInDegDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr(),
const bool &  PlotCCdf = false,
const bool &  PowerFit = false 
)

Plots the in-degree distribution of a Graph.

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

Definition at line 47 of file statplot.h.

47  {
48  TIntPrV DegCntV;
49  TSnap::GetInDegCnt(Graph, DegCntV);
50  const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes());
51  int AboveAvg=0, Above2Avg=0;
52  for (int i = 0; i < DegCntV.Len(); i++) {
53  if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; }
54  if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; }
55  }
56  if (PlotCCdf) {
57  DegCntV = TGUtil::GetCCdf(DegCntV); }
58  if (DescStr.Empty()) { DescStr = FNmPref; }
59  TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"inDegC.":"inDeg.")+FNmPref,
60  TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with in-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(),
61  Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())),
62  "In-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints);
63 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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
void TSnap::PlotInvParticipRat ( const PUNGraph Graph,
const int &  MaxEigVecs,
const int &  TimeLimit,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

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

Definition at line 39 of file statplot.cpp.

39  {
40  TFltPrV EigIprV;
41  GetInvParticipRat(Graph, MaxEigVecs, TimeLimit, EigIprV);
42  if (DescStr.Empty()) { DescStr = FNmPref; }
43  if (EigIprV.Empty()) { DescStr+=". FAIL"; EigIprV.Add(TFltPr(-1,-1)); return; }
44  TGnuPlot::PlotValV(EigIprV, "eigIPR."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f (%d values)",
45  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), EigIprV.Last().Val1(), EigIprV.Len()),
46  "Eigenvalue", "Inverse Participation Ratio of corresponding Eigenvector", gpsLog10Y, false, gpwPoints);
47 }
void GetInvParticipRat(const PUNGraph &Graph, int MaxEigVecs, int TimeLimit, TFltPrV &EigValIprV)
Definition: gsvd.cpp:377
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
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:574
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:429
template<class PGraph >
void TSnap::PlotKCoreEdges ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

Plots the k-Core edge-size distribution: Core k vs. number of edges in k-core.

Definition at line 175 of file statplot.h.

175  {
176  TIntPrV CoreEdgesV;
177  TSnap::GetKCoreEdges(Graph, CoreEdgesV);
178  if (DescStr.Empty()) { DescStr = FNmPref; }
179  TGnuPlot::PlotValV(CoreEdgesV, "coreEdges."+FNmPref, TStr::Fmt("%s. G(%d, %d).", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "k-Core", "Number of edges in the k-Core", gpsLog10Y, false, gpwLinesPoints);
180 }
int GetKCoreEdges(const PGraph &Graph, TIntPrV &CoreIdSzV)
Returns the number of edges in each core of order K (where K=0, 1, ...)
Definition: kcore.h:126
bool Empty() const
Definition: dt.h: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
template<class PGraph >
void TSnap::PlotKCoreNodes ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

Plots the k-Core node-size distribution: Core k vs. number of nodes in k-core.

Definition at line 167 of file statplot.h.

167  {
168  TIntPrV CoreNodesV;
169  TSnap::GetKCoreNodes(Graph, CoreNodesV);
170  if (DescStr.Empty()) { DescStr = FNmPref; }
171  TGnuPlot::PlotValV(CoreNodesV, "coreNodes."+FNmPref, TStr::Fmt("%s. G(%d, %d).", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "k-Core", "Number of nodes in the k-Core", gpsLog10Y, false, gpwLinesPoints);
172 }
int GetKCoreNodes(const PGraph &Graph, TIntPrV &CoreIdSzV)
Returns the number of nodes in each core of order K (where K=0, 1, ...)
Definition: kcore.h:114
bool Empty() const
Definition: dt.h: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
template<class PGraph >
void TSnap::PlotOutDegDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr(),
const bool &  PlotCCdf = false,
const bool &  PowerFit = false 
)

Plots the out-degree distribution of a Graph.

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

Definition at line 66 of file statplot.h.

66  {
67  TIntPrV DegCntV;
68  TSnap::GetOutDegCnt(Graph, DegCntV);
69  const double AvgDeg = 2*Graph->GetEdges()/double(Graph->GetNodes());
70  int AboveAvg=0, Above2Avg=0;
71  for (int i = 0; i < DegCntV.Len(); i++) {
72  if (DegCntV[i].Val1 > AvgDeg) { AboveAvg += DegCntV[i].Val2; }
73  if (DegCntV[i].Val1 > 2*AvgDeg) { Above2Avg += DegCntV[i].Val2; }
74  }
75  if (PlotCCdf) {
76  DegCntV = TGUtil::GetCCdf(DegCntV); }
77  if (DescStr.Empty()) { DescStr = FNmPref; }
78  TGnuPlot::PlotValV(DegCntV, (PlotCCdf?"outDegC.":"outDeg.")+FNmPref,
79  TStr::Fmt("%s. G(%d, %d). %d (%.4f) nodes with out-deg > avg deg (%.1f), %d (%.4f) with >2*avg.deg", DescStr.CStr(),
80  Graph->GetNodes(), Graph->GetEdges(), AboveAvg, AboveAvg/double(Graph->GetNodes()), AvgDeg, Above2Avg, Above2Avg/double(Graph->GetNodes())),
81  "Out-degree", PlotCCdf?"Count (CCDF)":"Count", gpsLog10XY, PowerFit, gpwLinesPoints);
82 }
void GetOutDegCnt(const PGraph &Graph, TIntPrV &DegToCntV)
Returns an out-degree histogram: a set of pairs (out-degree, number of nodes of such out-degree) ...
Definition: alg.h:201
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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
template<class PGraph >
void TSnap::PlotSccDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

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

Definition at line 98 of file statplot.h.

98  {
99  TIntPrV SccSzCnt;
100  TSnap::GetSccSzCnt(Graph, SccSzCnt);
101  if (DescStr.Empty()) { DescStr = FNmPref; }
102  TGnuPlot GnuPlot("scc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes",
103  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), SccSzCnt.Last().Val1/double(Graph->GetNodes())));
104  GnuPlot.AddPlot(SccSzCnt, gpwLinesPoints, "", "pt 6");
105  GnuPlot.SetXYLabel("Size of strongly connected component", "Number of components");
106  GnuPlot.SetScale(gpsLog10XY);
107  GnuPlot.SavePng();
108 }
void GetSccSzCnt(const PGraph &Graph, TIntPrV &SccSzCnt)
Returns a distribution of strongly connected component sizes.
Definition: cncom.h:420
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
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
template<class PGraph >
void TSnap::PlotShortPathDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr(),
int  TestNodes = TInt::Mx 
)

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

Definition at line 140 of file statplot.h.

140  {
141  TIntH DistToCntH;
142  TBreathFS<PGraph> BFS(Graph);
143  // shotest paths
144  TIntV NodeIdV;
145  Graph->GetNIdV(NodeIdV); NodeIdV.Shuffle(TInt::Rnd);
146  for (int tries = 0; tries < TMath::Mn(TestNodes, Graph->GetNodes()); tries++) {
147  const int NId = NodeIdV[tries];
148  BFS.DoBfs(NId, true, false, -1, TInt::Mx);
149  for (int i = 0; i < BFS.NIdDistH.Len(); i++) {
150  DistToCntH.AddDat(BFS.NIdDistH[i]) += 1; }
151  }
152  DistToCntH.SortByKey(true);
153  TFltPrV DistNbrsPdfV;
154  for (int i = 0; i < DistToCntH.Len(); i++) {
155  DistNbrsPdfV.Add(TFltPr(DistToCntH.GetKey(i)(), DistToCntH[i]()));
156  }
157  const double EffDiam = TSnap::TSnapDetail::CalcEffDiamPdf(DistNbrsPdfV, 0.9);
158  const double AvgDiam = TSnap::TSnapDetail::CalcAvgDiamPdf(DistNbrsPdfV);
159  const int FullDiam = (int) DistNbrsPdfV.Last().Val1;
160  if (DescStr.Empty()) { DescStr = FNmPref; }
161  TGnuPlot::PlotValV(DistNbrsPdfV, "diam."+FNmPref,
162  TStr::Fmt("%s. G(%d, %d). Diam: avg:%.2f eff:%.2f max:%d", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(),
163  AvgDiam, EffDiam, FullDiam), "Number of hops", "Number of shortest paths", gpsLog10Y, false, gpwLinesPoints);
164 }
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static const int Mx
Definition: dt.h:1049
static TRnd Rnd
Definition: dt.h:1053
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:551
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void SortByKey(const bool &Asc=true)
Definition: hash.h:249
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:1271
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:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
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:210
void TSnap::PlotSngValDistr ( const PNGraph Graph,
const int &  SngVals,
const TStr FNmPref,
TStr  DescStr 
)

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

Definition at line 58 of file statplot.cpp.

58  {
59  const int NBuckets = 50;
60  TFltV SngValV;
61  for (int f = 1; SngValV.Empty() && f < 4; f++) {
62  TSnap::GetSngVals(Graph, f*SngVals, SngValV);
63  }
64  SngValV.Sort(true);
65  THash<TFlt, TFlt> BucketCntH;
66  double Step = (SngValV.Last()-SngValV[0]) / double(NBuckets-1);
67  for (int i = 0; i < NBuckets; i++) {
68  BucketCntH.AddDat(SngValV[0]+Step*(i+0.5), 0);
69  }
70  for (int i = 0; i < SngValV.Len(); i++) {
71  const int Bucket = (int) floor((SngValV[i]-SngValV[0]) / Step);
72  BucketCntH[Bucket] += 1;
73  }
74  TFltPrV EigCntV;
75  BucketCntH.GetKeyDatPrV(EigCntV);
76  if (DescStr.Empty()) { DescStr = FNmPref; }
77  TGnuPlot::PlotValV(EigCntV, "sngDistr."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f", DescStr.CStr(),
78  Graph->GetNodes(), Graph->GetEdges(), SngValV.Last().Val), "Singular value", "Count", gpsAuto, false, gpwLinesPoints);
79 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
double Val
Definition: dt.h:1295
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:438
Definition: gnuplot.h:7
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
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:88
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:458
char * CStr()
Definition: dt.h:476
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
Definition: gnuplot.h:398
void TSnap::PlotSngValRank ( const PNGraph Graph,
const int &  SngVals,
const TStr FNmPref,
TStr  DescStr 
)

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

Definition at line 49 of file statplot.cpp.

49  {
50  TFltV SngValV;
51  TSnap::GetSngVals(Graph, SngVals, SngValV);
52  SngValV.Sort(false);
53  if (DescStr.Empty()) { DescStr = FNmPref; }
54  TGnuPlot::PlotValV(SngValV, "sngVal."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f",
55  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), SngValV[0].Val), "Rank", "Singular value", gpsLog10XY, false, gpwLinesPoints);
56 }
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:438
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
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
void TSnap::PlotSngVec ( const PNGraph Graph,
const TStr FNmPref,
TStr  DescStr 
)

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

Definition at line 81 of file statplot.cpp.

81  {
82  TFltV LeftSV, RightSV;
83  TSnap::GetSngVec(Graph, LeftSV, RightSV);
84  LeftSV.Sort(false);
85  RightSV.Sort(false);
86  TFltV BinV;
87  if (DescStr.Empty()) { DescStr = FNmPref; }
88  TGUtil::MakeExpBins(LeftSV, BinV, 1.01);
89  TGnuPlot::PlotValV(BinV, "sngVecL."+FNmPref, TStr::Fmt("%s. G(%d, %d). Left signular vector",
90  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "Rank", "Component of left singular vector", gpsLog10XY, false, gpwLinesPoints);
91  TGnuPlot::PlotValV(BinV, "sngVecL."+FNmPref, TStr::Fmt("%s. G(%d, %d). Right signular vector",
92  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges()), "Rank", "Component of right singular vector", gpsLog10XY, false, gpwLinesPoints);
93 }
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:438
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:1254
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
template<class PGraph >
void TSnap::PlotWccDistr ( const PGraph &  Graph,
const TStr FNmPref,
TStr  DescStr = TStr() 
)

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

Definition at line 85 of file statplot.h.

85  {
86  TIntPrV WccSzCnt;
87  TSnap::GetWccSzCnt(Graph, WccSzCnt);
88  if (DescStr.Empty()) { DescStr = FNmPref; }
89  TGnuPlot GnuPlot("wcc."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest component has %f nodes",
90  DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), WccSzCnt.Last().Val1/double(Graph->GetNodes())));
91  GnuPlot.AddPlot(WccSzCnt, gpwLinesPoints, "", "pt 6");
92  GnuPlot.SetXYLabel("Size of weakly connected component", "Number of components");
93  GnuPlot.SetScale(gpsLog10XY);
94  GnuPlot.SavePng();
95 }
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
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
template<class PGraph >
void TSnap::PrintInfo ( const PGraph &  Graph,
const TStr Desc = "",
const TStr OutFNm = "",
const bool &  Fast = true 
)

Prints basic graph statistics.

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

Definition at line 87 of file gbase.h.

87  {
88  int BiDirEdges=0, ZeroNodes=0, ZeroInNodes=0, ZeroOutNodes=0, SelfEdges=0, NonZIODegNodes=0;
89  THash<TIntPr, TInt> UniqDirE, UniqUnDirE;
90  FILE *F = stdout;
91  if (! OutFNm.Empty()) F = fopen(OutFNm.CStr(), "wt");
92  if (! Desc.Empty()) { fprintf(F, "%s:", Desc.CStr()); }
93  else { fprintf(F, "Graph:"); }
94  for (int f = gfUndef; f < gfMx; f++) {
95  if (HasGraphFlag(typename PGraph::TObj, TGraphFlag(f))) {
96  fprintf(F, " %s", TSnap::GetFlagStr(TGraphFlag(f)).CStr()); }
97  }
98  // calc stat
99  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
100  if (NI.GetDeg()==0) ZeroNodes++;
101  if (NI.GetInDeg()==0) ZeroInNodes++;
102  if (NI.GetOutDeg()==0) ZeroOutNodes++;
103  if (NI.GetInDeg()!=0 && NI.GetOutDeg()!=0) NonZIODegNodes++;
104  if (! Fast || Graph->GetNodes() < 1000) {
105  const int NId = NI.GetId();
106  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
107  const int DstNId = NI.GetOutNId(edge);
108  if (Graph->IsEdge(DstNId, NId)) BiDirEdges++;
109  if (NId == DstNId) SelfEdges++;
110  UniqDirE.AddKey(TIntPr(NId, DstNId));
111  UniqUnDirE.AddKey(TIntPr(TInt::GetMn(NId, DstNId), TInt::GetMx(NId, DstNId)));
112  }
113  }
114  }
115  int64 Closed=0, Open=0;
116  double WccSz=0, SccSz=0;
117  double EffDiam=0;
118  int FullDiam=0;
119  if (! Fast) {
120  TSnap::GetTriads(Graph, Closed, Open);
121  WccSz = TSnap::GetMxWccSz(Graph);
122  SccSz = TSnap::GetMxSccSz(Graph);
123  TSnap::GetBfsEffDiam(Graph, 100, false, EffDiam, FullDiam);
124  }
125  // print info
126  fprintf(F, "\n");
127  fprintf(F, " Nodes: %d\n", Graph->GetNodes());
128  fprintf(F, " Edges: %d\n", Graph->GetEdges());
129  fprintf(F, " Zero Deg Nodes: %d\n", ZeroNodes);
130  fprintf(F, " Zero InDeg Nodes: %d\n", ZeroInNodes);
131  fprintf(F, " Zero OutDeg Nodes: %d\n", ZeroOutNodes);
132  fprintf(F, " NonZero In-Out Deg Nodes: %d\n", NonZIODegNodes);
133  if (! Fast) {
134  fprintf(F, " Unique directed edges: %d\n", UniqDirE.Len());
135  fprintf(F, " Unique undirected edges: %d\n", UniqUnDirE.Len());
136  fprintf(F, " Self Edges: %d\n", SelfEdges);
137  fprintf(F, " BiDir Edges: %d\n", BiDirEdges);
138  fprintf(F, " Closed triangles: %s\n", TUInt64::GetStr(Closed).CStr());
139  fprintf(F, " Open triangles: %s\n", TUInt64::GetStr(Open).CStr());
140  fprintf(F, " Frac. of closed triads: %f\n", Closed/double(Closed+Open));
141  fprintf(F, " Connected component size: %f\n", WccSz);
142  fprintf(F, " Strong conn. comp. size: %f\n", SccSz);
143  fprintf(F, " Approx. full diameter: %d\n", FullDiam);
144  fprintf(F, " 90%% effective diameter: %f\n", EffDiam);
145  //fprintf(F, " Core\tNodes\tEdges\n");
146  //for (int i = 0; i < CNodesV.Len(); i++) {
147  // printf(" %d\t%d\t%d\n", CNodesV[i].Val1(), CNodesV[i].Val2(), CEdgesV[i].Val2());
148  //}
149  }
150  if (! OutFNm.Empty()) { fclose(F); }
151 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
int64 GetTriads(const PGraph &Graph, int64 &ClosedTriads, int64 &OpenTriads, int SampleNodes=-1)
Computes the number of Closed and Open triads.
Definition: triad.h:211
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:1092
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:1090
#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:331
TStr GetStr() const
Definition: dt.h:1270
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:186
int TSnap::PushRelabel ( TPRManager &  PRM,
const int &  NId,
const TNEANet::TNodeI NI 
)

Returns the ID of the neighbor that NId pushes to, -1 if no push was made.

Definition at line 328 of file flow.cpp.

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

Returns flow from a node NId to a neighbor InNId over edge EId.

Definition at line 297 of file flow.cpp.

297  {
298  int MinPush = MIN(PRM.Flow(EId), PRM.Excess(NId));
299  PRM.Flow(EId) -= MinPush;
300  PRM.Excess(NId) -= MinPush;
301  PRM.Excess(InNId) += MinPush;
302 }
#define MIN(a, b)
Definition: bd.h:346
void TSnap::PushToOutNbr ( TPRManager &  PRM,
const int &  NId,
const int &  OutNId,
const int &  EId 
)

Pushes flow from a node NId to a neighbor OutNId over edge EId.

Definition at line 289 of file flow.cpp.

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

Definition at line 6 of file gio.cpp.

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

Definition at line 38 of file gio.cpp.

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

Definition at line 77 of file gio.cpp.

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

Definition at line 105 of file gio.cpp.

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

Definition at line 982 of file cmty.cpp.

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:479
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
TIter BegI() const
Definition: hash.h:171
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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:220
TIter EndI() const
Definition: hash.h:176
const TKey & GetKey() const
Definition: hash.h:71
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:362
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
const TDat & GetDat() const
Definition: hash.h:72
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:69
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs 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:807
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
int vectorIntersect(TIntV &a, TIntV &b)
Definition: cmty.cpp:138
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:360
bool IsKey(const TKey &Key) const
Definition: hash.h:216
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:368
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int Len() const
Definition: hash.h:186
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:372
void TSnap::ReebSimplify ( PNGraph Graph,
TIntH t,
int  e,
PNGraph gFinal,
TIntH tFinal,
bool  collapse 
)

Definition at line 842 of file cmty.cpp.

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:479
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
TIter BegI() const
Definition: hash.h:171
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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:220
TIter EndI() const
Definition: hash.h:176
const TKey & GetKey() const
Definition: hash.h:71
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:362
const TDat & GetDat() const
Definition: hash.h:72
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:69
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs 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:807
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
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:1414
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
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:360
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:368
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
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:372
void TSnap::Relabel ( TPRManager &  PRM,
const int &  NId,
const TNEANet::TNodeI NI 
)

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

Definition at line 305 of file flow.cpp.

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

Definition at line 67 of file randwalk.h.

67  {
68  int locationId = StartNIdV.GetRndVal(Rnd);
69  //printf("starting walk at %d\n", locationId);
70  while (Rnd.GetUniDev() >= JumpProb) {
71  TNGraph::TNodeI location = Graph->GetNI(locationId);
72  int d = location.GetOutDeg();
73  if (d > 0)
74  locationId = location.GetOutNId(Rnd.GetUniDevInt(d));
75  else
76  locationId = StartNIdV.GetRndVal(Rnd);
77  }
78  return locationId;
79 }
const TVal & GetRndVal(TRnd &Rnd=TInt::Rnd) const
Returns a reference to a random element in the vector.
Definition: ds.h:561
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
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:372
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.

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
void TSnap::SaveEdgeListNet ( const PNEANet Graph,
const TStr OutFNm,
const TStr Desc 
)

Saves a network into a text file. Each line encodes either an edge or a node, along with its attributes.

Definition at line 269 of file gio.cpp.

269  {
270  FILE *F = fopen(OutFNm.CStr(), "wt");
271  fprintf(F, "# Directed network: %s \n", OutFNm.CStr());
272  if (! Desc.Empty()) { fprintf(F, "# %s\n", Desc.CStr()); }
273  fprintf(F, "# Nodes: %d Edges: %d\n", Graph->GetNodes(), Graph->GetEdges());
274 
275  TStrV IntAttrNNames;
276  TStrV FltAttrNNames;
277  TStrV StrAttrNNames;
278  Graph->GetAttrNNames(IntAttrNNames, FltAttrNNames, StrAttrNNames);
279  WriteNodeSchemaToFile(F, IntAttrNNames, FltAttrNNames, StrAttrNNames);
280  WriteNodesToFile(F, Graph, IntAttrNNames, FltAttrNNames, StrAttrNNames);
281  fprintf(F, "%s\n", END_SENTINEL.CStr());
282 
283  TStrV IntAttrENames;
284  TStrV FltAttrENames;
285  TStrV StrAttrENames;
286  Graph->GetAttrENames(IntAttrENames, FltAttrENames, StrAttrENames);
287  WriteEdgeSchemaToFile(F, IntAttrENames, FltAttrENames, StrAttrENames);
288  WriteEdgesToFile(F, Graph, IntAttrENames, FltAttrENames, StrAttrENames);
289  fprintf(F, "%s\n", END_SENTINEL.CStr());
290 
291  fclose(F);
292 }
void WriteEdgesToFile(FILE *F, const PNEANet &Graph, TStrV &IntAttrENames, TStrV &FltAttrENames, TStrV &StrAttrENames)
Definition: gio.cpp:238
const TStr END_SENTINEL
Definition: gio.h:8
void WriteEdgeSchemaToFile(FILE *F, TStrV &IntAttrENames, TStrV &FltAttrENames, TStrV &StrAttrENames)
Definition: gio.cpp:221
bool Empty() const
Definition: dt.h: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
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
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.

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:220
#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:216
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.

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:220
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:216
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.

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

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

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:220
#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:216
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
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.

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:220
#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:216
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
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.

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:220
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:216
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void TSnap::SetAllInvertSign ( TFltV ValV,
const double &  Val 
)

Definition at line 158 of file gsvd.cpp.

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

Definition at line 241 of file anf.h.

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

Converts TFltV to Numpy array.

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

Definition at line 15 of file numpy.cpp.

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

Converts TIntV to Numpy array.

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

Definition at line 4 of file numpy.cpp.

4  {
5  int limit = MIN(IntV.Len(), n);
6 
7  for (int i=0; i < limit; i++) {
8  IntNumpyVecOut[i] = IntV[i];
9  }
10 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
#define MIN(a, b)
Definition: bd.h:346
template<class PGraph >
PGraph TSnap::ToGraph ( PTable  Table,
const TStr SrcCol,
const TStr DstCol,
TAttrAggr  AggrPolicy 
)

Sequentially converts the table into a graph with links from nodes in SrcCol to those in DstCol.

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

Definition at line 8 of file conv.h.

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

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

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

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

Converts the Table into a graph with edges from SrcCol to DstCol, and attribute vector defined by the arguments.

Definition at line 65 of file conv.h.

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

185 {
186  TStrV V;
187  return ToNetwork<PGraph>(Table, SrcCol, DstCol, V, AggrPolicy);
188 }
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
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 1574 of file conv.h.

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

Converts table to network sequentially. Takes edges from Table and nodes explicitly from NodeCol in NodeTable, with attribute vectors passed as columns in corresponding tables.

Definition at line 2014 of file conv.h.

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

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

Definition at line 698 of file conv.h.

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

Calls ToNetworkMP with empty attribute vector. Convenience wrapper.

Definition at line 1109 of file conv.h.

1111 {
1112  TStrV V;
1113  return ToNetworkMP<PGraphMP>(Table, SrcCol, DstCol, V,AggrPolicy);
1114 }
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
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 1653 of file conv.h.

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

Converts table to network in parallel. Takes edges from Table and nodes explicitly from NodeCol in NodeTable, with attribute vectors passed as columns in corresponding tables.

Definition at line 2134 of file conv.h.

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

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

Definition at line 1120 of file conv.h.

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

Calls ToNetworkMP2 with an empty attribute vector. Convenience wrapper.

Definition at line 1549 of file conv.h.

1551 {
1552  TStrV V;
1553  return ToNetworkMP2<PGraphMP>(Table, SrcCol, DstCol, V, V, V, AggrPolicy);
1554 }
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
void TSnap::WriteEdgeSchemaToFile ( FILE *  F,
TStrV IntAttrENames,
TStrV FltAttrENames,
TStrV StrAttrENames 
)

Definition at line 221 of file gio.cpp.

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

Definition at line 238 of file gio.cpp.

238  {
239  for (TNEANet::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
240  fprintf(F, "%d\t%d", EI.GetSrcNId(), EI.GetDstNId());
241  for(int i = 0; i < IntAttrENames.Len(); i++) {
242  if (Graph->IsIntAttrDeletedE(EI.GetId(), IntAttrENames[i])) {
243  fprintf(F, "\t%s", NULL_VAL.CStr());
244  continue;
245  }
246  int AttrIntVal = Graph->GetIntAttrDatE(EI.GetId(), IntAttrENames[i]);
247  fprintf(F, "\t%d", AttrIntVal);
248  }
249  for(int i = 0; i < FltAttrENames.Len(); i++) {
250  if (Graph->IsFltAttrDeletedE(EI.GetId(), FltAttrENames[i])) {
251  fprintf(F, "\t%s", NULL_VAL.CStr());
252  continue;
253  }
254  double AttrFltVal = Graph->GetFltAttrDatE(EI.GetId(), FltAttrENames[i]);
255  fprintf(F, "\t%f", AttrFltVal);
256  }
257  for(int i = 0; i < StrAttrENames.Len(); i++) {
258  if (Graph->IsStrAttrDeletedE(EI.GetId(), StrAttrENames[i])) {
259  fprintf(F, "\t%s", NULL_VAL.CStr());
260  continue;
261  }
262  char * AttrStrVal = Graph->GetStrAttrDatE(EI.GetId(), StrAttrENames[i]).CStr();
263  fprintf(F, "\t%s", AttrStrVal);
264  }
265  fprintf(F, "\n");
266  }
267 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1707
const TStr NULL_VAL
Definition: gio.h:15
char * CStr()
Definition: dt.h:476
void TSnap::WriteNodeSchemaToFile ( FILE *  F,
TStrV IntAttrNNames,
TStrV FltAttrNNames,
TStrV StrAttrNNames 
)

Definition at line 171 of file gio.cpp.

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

Definition at line 188 of file gio.cpp.

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

Variable Documentation

const TStr TSnap::CapAttrName = "capacity"

Definition at line 4 of file flow.h.

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

Definition at line 10 of file gio.h.

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

Definition at line 6 of file gio.h.

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

Definition at line 8 of file gio.h.

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

Definition at line 13 of file gio.h.

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

Definition at line 12 of file gio.h.

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

Definition at line 11 of file gio.h.

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

Definition at line 7 of file gio.h.

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

Definition at line 15 of file gio.h.

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

Definition at line 9 of file gio.h.

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

Definition at line 14 of file gio.h.