def print_average_ccf_and_traid_count(G, GName): DegToCCfV = snap.TFltPrV() result = snap.GetClustCfAll(G, DegToCCfV) print "Average clustering coefficient in {0}: {1:.4f}".format( GName[:-10], result[0]) print "Number of traids in {0}: {1}".format(GName[:-10], result[1])
def showClusteringCoefficient(network): DegToCCfV = snap.TFltPrV() result = snap.GetClustCfAll(network, DegToCCfV) for item in DegToCCfV: print("degree: %d, clustering coefficient: %f" % (item.GetVal1(), item.GetVal2())) print("average clustering coefficient", result[0]) print("closed triads", result[1]) print("open triads", result[2])
def gen_config_model_rewire(graph, iterations=8000, seed=42): config_graph = graph clustering_coeffs = [] ########################################################################## nOfNodes = graph.GetNodes() nOfEdges = graph.GetEdges() for j in range(iterations): while True: edges1 = graph.BegEI() eId = int(round(np.random.rand() * (nOfEdges - 1))) for i in range(eId): edges1.Next() e1 = edges1 a = e1.GetSrcNId() b = e1.GetDstNId() edges2 = graph.BegEI() e2id = int(round(np.random.rand() * (nOfEdges - 1))) for i in range(e2id): edges2.Next() e2 = edges2 c = e2.GetSrcNId() d = e2.GetDstNId() u = a if np.random.rand() > 0.5 else b v = b if u == a else a w = c if np.random.rand() > 0.5 else d x = d if w == c else c # check if graph is regular if u == w or v == x or (a == b and c == d) or graph.IsEdge( u, w) or graph.IsEdge(v, x): continue graph.DelEdge(a, b) graph.DelEdge(c, d) graph.AddEdge(u, w) graph.AddEdge(v, x) break if graph.GetNodes() != nOfNodes or graph.GetEdges() != nOfEdges: print "Graph has changed! nodes:", nOfNodes, "-->", graph.GetNodes( ), "edges:", nOfEdges, "-->", graph.GetEdges() raise RuntimeError if j % 100 == 0: clustering_coeffs.append( snap.GetClustCfAll(graph, snap.TFltPrV())[0]) ########################################################################## return config_graph, clustering_coeffs
def clustCoefDistribution(G): print "Nodes number: ", G.GetNodes() # 37444 print "Edges number: ", G.GetEdges() # 561119 DegToCCfV = snap.TFltPrV() snap.GetClustCfAll(G, DegToCCfV) X = [] Y = [] for item in DegToCCfV: X.append(item.GetVal1()) # degree Y.append(item.GetVal2()) # avg. clustering coefficient plt.plot(X, Y, 'ro', label='Collaboration Network') plt.ylabel('Average Clustering Coefficient') plt.xlabel('Number of Neighbors (degree)') plt.show()
import numpy as np import snap import matplotlib.pyplot as plt import random payoff_a = 2 payoff_b = 3 thersold = payoff_a / (payoff_a + payoff_b) nodestatusList = [] nodedegreeList = [] nodeaffectList = [] nodeinitialList = [] G = snap.LoadEdgeList(snap.PUNGraph, "data/soc-Slashdot0902.txt", 0, 1, '\t') snap.PlotClustCf(G, "project_cluster_coeff", "Undirected graph - clustering coefficient") DegToCCfV = snap.TFltPrV() result = snap.GetClustCfAll(G, DegToCCfV) for item in DegToCCfV: print("degree: %d, clustering coefficient: %f" % (item.GetVal1(), item.GetVal2())) print("average clustering coefficient", result[0]) clusterfile = open("clustering_list.txt", "w") NIdCCfH = snap.TIntFltH() snap.GetNodeClustCf(G, NIdCCfH) for item in NIdCCfH: clusterfile.write("%d %d\r\n" % (item, NIdCCfH[item]))
clustering_coef_current_node = 2 * count_edges_between_neigbours / ( current_degree * (current_degree - 1)) list_clusterning_coefs_allnodes.append(clustering_coef_current_node) return list_clusterning_coefs_allnodes list_clusterning_coefs_allnodes = clustering_coef( UGraph, mode_for_end_nodes='put_zeros') list_clusterning_coefs_allnodes gcc = np.mean(list_clusterning_coefs_allnodes) lcc = snap.GetClustCf(UGraph) #-Compute another Global Clustering Coef - Transitivity DegToCCfV = snap.TFltPrV() triads = snap.GetClustCfAll(UGraph, DegToCCfV) for item in DegToCCfV: print("degree: %d, clustering coefficient: %f" % (item.GetVal1(), item.GetVal2())) print("average clustering coefficient", result[0]) print("closed triads", result[1]) print("open triads", result[2]) closed_triads_num = triads[1] open_triads_num = triads[2] transitivity = 3 * closed_triads_num / open_triads_num print( "----------------------------------------------------------\n-------------Clustering Coef Result------------------\n----------------------------------------------------------\n Built-in Average of Local Clustering Coefs:", lcc, "\n Global Clustering Coef(Average all nodes' LocalCC):", gcc, "\n Global Clustering Coef(Transitivity=3*(#triangles)/(#triplets)):",
def clustering_coefficient(G): DegToCCfV = snap.TFltPrV() Result = snap.GetClustCfAll(G, DegToCCfV, -1) print 'Average Clustering Coefficient:', Result[0]
def average_clustering_coefficient(self): return sn.GetClustCfAll(self.gUNsn, sn.TFltPrV())[0]
Community = 0 SubGraphVector = snap.TIntV() d = defaultdict(list) for Cmty in CmtyV: Community += 1 print "Community: %d" % Community for NI in Cmty: d[Community].append(NI) NodeIdList.Add(NI) SubGraph = snap.GetSubGraph(G1, NodeIdList) diam = snap.GetBfsFullDiam(SubGraph, 100, False) print "The diametre is %d" % diam DegToCCfV = snap.TFltPrV() DegList = list() result = snap.GetClustCfAll(SubGraph, DegToCCfV) for item in DegToCCfV: DegList.append(item.GetVal1()) #print "degree: %d, clustering coefficient: %f" % (item.GetVal1(), item.GetVal2()) print "average clustering coefficient", result[0] print "closed triads", result[1] print "open triads", result[2] NumTriadEdges = snap.GetTriadEdges(SubGraph) print "The number of TriadEdges is %d" % NumTriadEdges CountEdges = snap.CntUniqUndirEdges(SubGraph) print "Directed Graph: Count of undirected edges is %d" % CountEdges CountNodes = SubGraph.GetNodes() InternalDensity = CountEdges / (CountNodes * (CountNodes - 1) * 0.5) print "The Internal Density is %f" % InternalDensity AvgDeg = 2 * CountEdges / CountNodes print "The Average Degree is %f" % AvgDeg