def degreeDistribution(self): result = snap.TFltPrV() resultMap = collections.defaultdict(lambda:0) snap.GetDegCnt(self.rawGraph, result) for x in result: resultMap[int(x.GetVal1())] = int(x.GetVal2()) return resultMap
def getGraphInfo(G): Degree = [] DegreeCount = [] CluDegree = [] Clu = [] DegToCntV = snap.TIntPrV() snap.GetDegCnt(G, DegToCntV) DegreeCountMax = 0 for item in DegToCntV: tempy = item.GetVal2() if tempy > DegreeCountMax: DegreeCountMax = tempy Degree.append(item.GetVal1()) DegreeCount.append(tempy) CfVec = snap.TFltPrV() Cf = snap.GetClustCf(G, CfVec, -1) for pair in CfVec: CluDegree.append(pair.GetVal1()) Clu.append(pair.GetVal2()) return DegreeCountMax,Degree,DegreeCount,CluDegree,Clu
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 Q1_3(): """ Code for Q1.3 """ C_erdosRenyi = calcClusteringCoefficient(erdosRenyi) C_smallWorld = calcClusteringCoefficient(smallWorld) C_collabNet = calcClusteringCoefficient(collabNet) print('Clustering Coefficient for Erdos Renyi Network: %f' % C_erdosRenyi) print('Clustering Coefficient for Small World Network: %f' % C_smallWorld) print('Clustering Coefficient for Collaboration Network: %f' % C_collabNet) CfVec = snap.TFltPrV() C_erdosRenyi = snap.GetClustCf(erdosRenyi, CfVec, -1) CfVec = snap.TFltPrV() C_smallWorld = snap.GetClustCf(smallWorld, CfVec, -1) CfVec = snap.TFltPrV() C_collabNet = snap.GetClustCf(collabNet, CfVec, -1) print('Clustering Coefficient for Erdos Renyi Network: %f' % C_erdosRenyi) print('Clustering Coefficient for Small World Network: %f' % C_smallWorld) print('Clustering Coefficient for Collaboration Network: %f' % C_collabNet)
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 analyze_diameter(nodes, edges): G = snap.TUNGraph.New() idxs = {} for i, v in enumerate(nodes): idxs[v] = i for v in nodes: G.AddNode(idxs[v]) for e in edges: G.AddEdge(idxs[e[0]], idxs[e[1]]) G.AddEdge(idxs[e[1]], idxs[e[0]]) DegToCCfV = snap.TFltPrV() print('Clustering Coefficient: ', snap.GetClustCf(G, -1)) print('Effective Diameter: ', snap.GetBfsEffDiam(G, min(len(nodes), 10000), False))
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]))
def clustering_coefficient(G): DegToCCfV = snap.TFltPrV() Result = snap.GetClustCfAll(G, DegToCCfV, -1) print 'Average Clustering Coefficient:', Result[0]
' ') nodes = graph.GetNodes() edges = graph.GetEdges() InDegV = snap.TIntPrV() snap.GetNodeInDegV(graph, InDegV) sum_deg = 0 max_deg = 0 min_deg = 10000 for item in InDegV: cur_deg = item.GetVal2() sum_deg += cur_deg min_deg = min(min_deg, cur_deg) max_deg = max(max_deg, cur_deg) avg_deg = sum_deg / nodes CfVec = snap.TFltPrV() Cf = snap.GetClustCf(graph, CfVec, -1) clus_coef = snap.GetClustCf(graph, -1) ### BASIC CHECKS OF GRAPHS ### print("Nodes: ", graph.GetNodes()) print("Edges: ", graph.GetEdges()) print("Maximum Degree: ", max_deg) print("Minimum Degree: ", min_deg) print("Clustering Coefficient: ", clus_coef) print("Average degree: ", avg_deg) density = edges / (nodes * (nodes - 1)) print("Density: ", density) initiator = np.array([[0, density, density], [density, 0, density], [density, density, 0]])
node_id = G.GetRndNId(Rnd) node_cluster_coeff = snap.GetNodeClustCf(G, node_id) print("Clustering coefficient of random node {}: {}".format( node_id, round(node_cluster_coeff, 4))) node_id = G.GetRndNId(Rnd) node_num_triads = snap.GetNodeTriads(G, node_id) print("Number of triads random node {} participates: {}".format( node_id, node_num_triads)) triad_edge = snap.GetTriadEdges(G) print("Number of edges that participate in at least one triad: {}".format( triad_edge)) cf_dist = snap.TFltPrV() coeff = snap.GetClustCf(G, cf_dist, -1) degree_coeff = {} for pair in cf_dist: degree_coeff[pair.GetVal1()] = pair.GetVal2() # Plot Degree Distribution plot_filename = 'clustering_coeff_' + graph_filename[:-6] + '.png' plot_filedir = os.path.join(plotpath, plot_filename) plt.figure() plt.scatter(list(degree_coeff.keys()), list(degree_coeff.values()), s=10) plt.xlabel("Degree") plt.ylabel("Clustering Coefficient") plt.title("Clustering Coefficient Distribution ({})".format( graph_filename[:-6])) plt.savefig(plot_filedir)
Dir.AddEdge(wordA, wordB) step4 += 1 # Creating edges between regular nodes across synsets for i in range(149039): print i supernodeA, supernodeB = random.sample(synsets.keys(), 2) regnodeA = random.choice(synsets[supernodeA]) regnodeB = random.choice(synsets[supernodeB]) Dir.AddEdge(regnodeA, regnodeB) step2 += 1 print Dir.GetNodes() print Dir.GetEdges() CfVec = snap.TFltPrV() Cf = snap.GetClustCf(Dir, CfVec, -1) print "Avg Clustering Coefficient: %f" % (Cf) node_degrees = [node.GetOutDeg() for node in Dir.Nodes()] min_degree = min(node_degrees) max_degree = max(node_degrees) degree_distr = [0] * (max_degree + 1) for degree in node_degrees: degree_distr[degree] += 1 def normalize(vector): total = float(sum(vector)) return [elem / total for elem in vector]
def average_clustering_coefficient(self): return sn.GetClustCfAll(self.gUNsn, sn.TFltPrV())[0]