def RankEigvecDist(graph): """ Calculates the degree - clustering coefficient distribution of a given graph. The graph must be in snap format. ... Parameters ---------- graph : an instance of SNAP.TUNGraph()/SNAP.TNGraph() for undirected/directed graph The graph for which the degree distribution is to be calculated Returns ------- eigvec_dist : 2D numpy array with shape (2, :) The calculated eigenvector distribution for graph. Here, the eigenvector refers to the principal eigenvector, which should be positive definite due to Perrin-Frobenius theorem. eigvec_dist[0, :] : rank of the eigenvector eigvec_dist[1, :] : value of the element of the first eigenvector, sorted in the order of descending value. """ EigVec = snap.TFltV() snap.GetEigVec(graph,EigVec) eigvec = np.abs(np.array(EigVec)) N = len(eigvec) eigvec.reshape((1, N)) rank = np.arange(N).reshape((1, N)) eigvec = eigvec[np.argsort(-eigvec)] eigvec_dist = np.vstack([rank, eigvec]) return eigvec_dist
snap.GetWccSzCnt(G9, CntV) for p in CntV: print "size %d: count %d" % (p.GetVal1(), p.GetVal2()) # get degree distribution pairs (out-degree, count): snap.GetOutDegCnt(G9, CntV) for p in CntV: print "degree %d: count %d" % (p.GetVal1(), p.GetVal2()) # generate a Preferential Attachment graph on 100 nodes and out-degree of 3 G10 = snap.GenPrefAttach(100, 3) print "G10: Nodes %d, Edges %d" % (G10.GetNodes(), G10.GetEdges()) # define a vector of floats and get first eigenvector of graph adjacency matrix EigV = snap.TFltV() snap.GetEigVec(G10, EigV) nr = 0 for f in EigV: nr += 1 print "%d: %.6f" % (nr, f) # get an approximation of graph diameter diam = snap.GetBfsFullDiam(G10, 10) print "diam", diam # count the number of triads: triads = snap.GetTriads(G10) print "triads", triads # get the clustering coefficient cf = snap.GetClustCf(G10)
def intro(): # create a graph PNGraph G1 = snap.TNGraph.New() G1.AddNode(1) G1.AddNode(5) G1.AddNode(32) G1.AddEdge(1, 5) G1.AddEdge(5, 1) G1.AddEdge(5, 32) print("G1: Nodes %d, Edges %d" % (G1.GetNodes(), G1.GetEdges())) # create a directed random graph on 100 nodes and 1k edges G2 = snap.GenRndGnm(snap.PNGraph, 100, 1000) print("G2: Nodes %d, Edges %d" % (G2.GetNodes(), G2.GetEdges())) # traverse the nodes for NI in G2.Nodes(): print("node id %d with out-degree %d and in-degree %d" % (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg())) # traverse the edges for EI in G2.Edges(): print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())) # traverse the edges by nodes for NI in G2.Nodes(): for Id in NI.GetOutEdges(): print("edge (%d %d)" % (NI.GetId(), Id)) # generate a network using Forest Fire model G3 = snap.GenForestFire(1000, 0.35, 0.35) print("G3: Nodes %d, Edges %d" % (G3.GetNodes(), G3.GetEdges())) # save and load binary FOut = snap.TFOut("test.graph") G3.Save(FOut) FOut.Flush() FIn = snap.TFIn("test.graph") G4 = snap.TNGraph.Load(FIn) print("G4: Nodes %d, Edges %d" % (G4.GetNodes(), G4.GetEdges())) # save and load from a text file snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges") G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1) print("G5: Nodes %d, Edges %d" % (G5.GetNodes(), G5.GetEdges())) # generate a network using Forest Fire model G6 = snap.GenForestFire(1000, 0.35, 0.35) print("G6: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges())) # convert to undirected graph G7 = snap.ConvertGraph(snap.PUNGraph, G6) print("G7: Nodes %d, Edges %d" % (G7.GetNodes(), G7.GetEdges())) # get largest weakly connected component of G WccG = snap.GetMxWcc(G6) # get a subgraph induced on nodes {0,1,2,3,4,5} SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4)) # get 3-core of G Core3 = snap.GetKCore(G6, 3) # delete nodes of out degree 10 and in degree 5 snap.DelDegKNodes(G6, 10, 5) print("G6a: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges())) # generate a Preferential Attachment graph on 1000 nodes and node out degree of 3 G8 = snap.GenPrefAttach(1000, 3) print("G8: Nodes %d, Edges %d" % (G8.GetNodes(), G8.GetEdges())) # vector of pairs of integers (size, count) CntV = snap.TIntPrV() # get distribution of connected components (component size, count) snap.GetWccSzCnt(G8, CntV) # get degree distribution pairs (degree, count) snap.GetOutDegCnt(G8, CntV) # vector of floats EigV = snap.TFltV() # get first eigenvector of graph adjacency matrix snap.GetEigVec(G8, EigV) # get diameter of G8 snap.GetBfsFullDiam(G8, 100) # count the number of triads in G8, get the clustering coefficient of G8 snap.GetTriads(G8) snap.GetClustCf(G8)
import snap G1 = snap.TNGraph.New() G1.AddNode(1) G1.AddNode(5) G1.AddNode(32) G1.AddEdge(1, 1) G1.AddEdge(1, 5) G1.AddEdge(5, 1) G1.AddEdge(5, 32) for EI in G1.Edges(): print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())) # get first eigenvector of graph adjacency matrix EigV = snap.TFltV() snap.GetEigVec(G1, EigV)
for item in ComponentDist: print "%d nodes with out-degree %d" % (item.GetVal2(), item.GetVal1()) xval.append(item.GetVal1()) yval.append(item.GetVal2()) bins = np.arange(len(yval)) plt.hist(yval, xval, alpha=0.5, label='Nodes with Out degree') plt.title('Distribution of Out degree by Nodes') plt.xlabel('Out degree') plt.ylabel('Number of Nodes') plt.xticks(bins, rotation=90) plt.show() # vector of floats EigV = snap.TFltV() # get first eigenvector of graph adjacency matrix snap.GetEigVec(snap.ConvertGraph(snap.PUNGraph, G1), EigV) print "Leading Eigen vector of adjacency graph-" for Val in EigV: print Val # get diameter of G8 print "Diameter of the network =", snap.GetBfsFullDiam(G1, 100) TriadV = snap.TIntTrV() # count the number of triads in G8, get the clustering coefficient of G8 print "Number of Triads in the network = ", snap.GetTriads(G1, TriadV) for triple in TriadV: print "For Node Id: ", triple.Val1( ), ",Number of open Triads are: ", triple.Val2( ), ", Number of closed Triads are: ", triple.Val3()
# convert to undirected graph G7 = snap.ConvertGraph(snap.PUNGraph, G6) WccG = snap.GetMxWcc(G6) # get a subgraph induced on nodes {0,1,2,3,4,5} SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4)) # get 3-core of G Core3 = snap.GetKCore(G6, 3) # delete nodes of out degree 10 and in degree 5 snap.DelDegKNodes(G6, 10, 5) # %% # stats # generate a Preferential Attachment graph on 1000 nodes and node out degree of 3 G8 = snap.GenPrefAttach(1000, 3) # vector of pairs of integers (size, count) CntV = snap.TIntPrV() # get distribution of connected components (component size, count) snap.GetWccSzCnt(G8, CntV) # get degree distribution pairs (degree, count) snap.GetOutDegCnt(G8, CntV) # vector of floats EigV = snap.TFltV() # get first eigenvector of graph adjacency matrix snap.GetEigVec(G8, EigV) # get diameter of G8 snap.GetBfsFullDiam(G8, 100) # count the number of triads in G8, get the clustering coefficient of G8 snap.GetTriads(G8) snap.GetClustCf(G8) # %%