def gen_config_model_rewire(graph, iterations=10000): """Generate Configuration model by random rewiring from graph""" config_graph = graph clustering_coeffs = [] count = 1 clustering_coeffs.append(snap.GetClustCf(config_graph)) while count <= iterations: if count % 100 == 0: clustering_coeffs.append(snap.GetClustCf(config_graph)) a = config_graph.GetRndNId() b = config_graph.GetRndNId() while not config_graph.IsEdge(a, b): a = config_graph.GetRndNId() b = config_graph.GetRndNId() c = config_graph.GetRndNId() d = config_graph.GetRndNId() while (not config_graph.IsEdge(c, d)) or (a, b) == (c, d): c = config_graph.GetRndNId() d = config_graph.GetRndNId() e1, e2 = (a, b), (c, d) i, j = randint(0, 1), randint(0, 1) u, v, w, x = e1[i], e1[1 - i], e2[j], e2[1 - j] if not (config_graph.IsEdge(u, w) or config_graph.IsEdge(v, x)): if (u != w) and (v != x): config_graph.DelEdge(a, b) config_graph.DelEdge(c, d) config_graph.AddEdge(u, w) config_graph.AddEdge(v, x) count += 1 return config_graph, clustering_coeffs
def Q1_2(): """ Code for Q1.2 """ C_erdosRenyi = calcClusteringCoefficient(erdosRenyi) C_smallWorld = calcClusteringCoefficient(smallWorld) C_collabNet = calcClusteringCoefficient(collabNet) print('Clustering Coefficient for Erdos Renyi Network: %f' % C_erdosRenyi) print(snap.GetClustCf(erdosRenyi, -1)) print('Clustering Coefficient for Small World Network: %f' % C_smallWorld) print(snap.GetClustCf(smallWorld, -1)) print('Clustering Coefficient for Collaboration Network: %f' % C_collabNet) print(snap.GetClustCf(collabNet, -1))
def average_clustering_coefficient(self, node_level=False): """ Measurement: average_clustering_coefficient Description: Calculate the average clustering coefficient of the graph Input: Graph Output: Float """ if SNAP_LOADED: return sn.GetClustCf(self.gUNsn) else: if not node_level: graphs = {'all':self.gUNig} else: graphs = self.gUNigs meas = {} for key,graph in graphs.items(): meas[key] = self.gUNig.transitivity_avglocal_undirected(mode='zero') if not node_level: return meas['all'] else: return meas
def get_properties_with_sanppy(extension, input_folder): id_map = {} next_id = 0 graph = snap.PNGraph.New() files_read = 0 for file in os.listdir(input_folder): if not extension or file.endswith(extension): files_read += 1 with open(os.path.join(input_folder, file)) as file: for line in file.readlines(): edge = line.rstrip().split() n1 = id_map.get(edge[0], next_id) if n1 == next_id: id_map[edge[0]] = n1 next_id += 1 n2 = id_map.get(edge[1], next_id) if n2 == next_id: id_map[edge[1]] = n2 next_id += 1 if not graph.IsNode(n1): graph.AddNode(n1) if not graph.IsNode(n2): graph.AddNode(n2) graph.AddEdge(n1, n2) ef_diam_l, ef_diam_h, diam, sp = snap.GetBfsEffDiamAll(graph, 10, True) properties = [ snap.CntNonZNodes(graph), snap.CntUniqDirEdges(graph), snap.IsConnected(graph), snap.CntNonZNodes(graph) / snap.CntUniqDirEdges(graph), snap.GetClustCf(graph), sp, diam, snap.CntUniqDirEdges(graph) / (snap.CntNonZNodes(graph) * snap.CntNonZNodes(graph)) ] return dict(zip(get_property_names(), properties))
def clustering_coefficient(input): print("Loading graph...") FIn = snap.TFIn(input) graph = snap.TNGraph.Load(FIn) print("Calculating clustering coefficient...") print ("Clustering Coefficient:", snap.GetClustCf (graph, -1))
def calcClusteringCoefficient(Graph): """ :param - Graph: snap.PUNGraph object representing an undirected graph return type: float returns: clustering coeffient of `Graph """ ############################################################################ # TODO: Your code here! C = 0.0 total = 0.0 for node in Graph.Nodes(): ki = node.GetDeg() numConnectedNeighbors = 0.0 if ki >= 2: neighborIDs = [node.GetNbrNId(i) for i in xrange(node.GetDeg())] for neighborID in neighborIDs: neighbor = Graph.GetNI(neighborID) candidates = [ neighbor.GetNbrNId(i) for i in xrange(neighbor.GetDeg()) ] for candidate in candidates: if node.IsNbrNId(candidate): numConnectedNeighbors += 1 total += numConnectedNeighbors / (ki * (ki - 1)) C = total / float(Graph.GetNodes()) # Sanity check. assert abs(C - snap.GetClustCf(Graph)) < 1e-8 ############################################################################ return C
def analyzeMisc(FNGraph): # LCC, average distances, clustering t1 = time.time() print "Started calculating miscellaneous network statistics:" print '\tPercentage of nodes in LCC in Football network: %.3f' % (snap.GetMxWccSz(FNGraph) * 100.0) GraphClustCoeff = snap.GetClustCf (FNGraph, -1) print "\tClustering coefficient: %.3f" % GraphClustCoeff diam = snap.GetBfsFullDiam(FNGraph, 1432, False) print "\tNetwork diameter: %.3f\n" % diam print "\tCalculating average distance..." avgDist = 0 iter1 = 0 allNodes1 = FNGraph.GetNodes() for NI in FNGraph.Nodes(): if(iter1 % 100 == 0): print "\t\tCalculated for %d nodes" % iter1 NIdToDistH = snap.TIntH() snap.GetShortPath(FNGraph, NI.GetId(), NIdToDistH) singleDistSum = 0 for item in NIdToDistH: singleDistSum += NIdToDistH[item] avgDist += (1.0/allNodes1) * float(singleDistSum)/(allNodes1-1) iter1 += 1 print "\tNetwork average distance: %.3f" % avgDist print "\nFinished calculating in %f seconds\n" % (time.time() - t1)
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 test_clustering_coefficient(): dataname = "example" # (13, 20) # dataname = "polbooks" # (105, 441) # dataname = "polblogs" # (1224,16715) # s_CC = 0.225958517359 # dataname = "polblogs-wcc" # (1222,16714) # dataname = "as20graph" # (6474,12572) filename = "../_data/" + dataname + ".gr" ### read graph for DETERMINISTIC G start = time.clock() print "filename =", filename G = ig.Graph.Read_Edgelist(filename, directed=False) print "#nodes =", G.vcount() # igraph s_CC = G.transitivity_undirected() print "s_CC =", s_CC s_agv_local_CC = G.transitivity_avglocal_undirected() print "s_agv_local_CC =", s_agv_local_CC # SNAP aG = convert_networkx_to_SNAP(G) print "convert to SNAP graph: DONE" s_CC = sn.GetClustCf(aG) print "s_CC =", s_CC
def projection(Graph): if Graph == 'campaign': G2 = readGraph("../processed-data/campaignNetworks_v2.txt") elif Graph == 'bill': G2 = readGraph("../processed-data/legislator_bill_edge_list_graph.txt") else: raise ValueError("Invalid graph: please use 'campaign' or 'bill'. ") H = snap.TUNGraph.New() for i in G2.Nodes(): for j in G2.Nodes(): if (i.GetId() < j.GetId() and j.GetId() < 10000): #10000 is the upper limit for candidate nodes NbrV = snap.TIntV() Num = snap.GetLen2Paths(G2, i.GetId(), j.GetId(), NbrV) if Num > 0: if H.IsNode(i.GetId()) == False: H.AddNode(i.GetId()) if H.IsNode(j.GetId()) == False: H.AddNode(j.GetId()) if H.IsEdge(i.GetId(), j.GetId()) == False: H.AddEdge(i.GetId(),j.GetId()) print "Compressed Graph Node count total: %d" % (H.GetNodes()) print "Compressed Edge count total: %d" % (H.GetEdges()) GraphClustCoeff = snap.GetClustCf(H, -1) print Graph + " Network Clustering coefficient: %f" % GraphClustCoeff snap.SaveEdgeList(H, "../processed-data/"+Graph+"_projection.txt", Graph + " network - Save projected network info as tab-separated list of edges, using unified candidate node IDs") return
def run(graph_file): merged_graph = json_utils.read(graph_file) snap_graph = snap.TNGraph.New() for key in merged_graph: screen_name = key followers = merged_graph[key] screen_name_hash = hash_screen_name(screen_name) if not snap_graph.IsNode(screen_name_hash): snap_graph.AddNode(screen_name_hash) for follower in followers: follower_hash = hash_screen_name(follower) if not snap_graph.IsNode(follower_hash): snap_graph.AddNode(follower_hash) snap_graph.AddEdge(screen_name_hash, follower_hash) nodes_count = snap_graph.GetNodes() edges_count = snap_graph.GetEdges() print("nodes count = " + str(nodes_count)) print("edges count = " + str(edges_count)) cluster_coeff = snap.GetClustCf(snap_graph, -1) print "Clustering coefficient: %f" % cluster_coeff return snap_graph
def evaluate(snap_graph): # diameter, avg path length result = snap.GetBfsEffDiamAll(snap_graph, 10, True) print 'diamter =', result[2] print 'avgSPL =', result[3] # cluster coefficient cc = snap.GetClustCf(snap_graph, -1) print 'ClustCf =', cc
def clustering_coefficient(): # Count triads Triads = snap.GetTriads(G) print("# of triads", Triads) # Calculate clustering coefficient CC = snap.GetClustCf(G) print("clustering coefficient", CC)
def get_basic_stats(G): print("************") print("Basic stats") num_nodes = G.GetNodes() num_edges = G.GetEdges() print ('Num Nodes %i' % num_nodes) print ('Num Edges %i' % num_edges) print ('Clustering Coefficient %f' % snap.GetClustCf(G)) print 'Density', float(2*num_edges)/(num_nodes*(num_nodes-1))
def constructGraph(dic): G = snap.TUNGraph().New() for v, val in dic.iteritems(): if not G.IsNode(v): G.AddNode(v) for w in val.keys(): if not G.IsNode(w): G.AddNode(w) G.AddEdge(v, w) print snap.GetClustCf(G, -1) return G
def gen_config_model_rewire(graph, iterations=10000): config_graph = graph cfs = [] ########################################################################## #TODO: Your code here t = iterations check = 100 edges = [] n = config_graph.GetNodes() e = config_graph.GetEdges() for edge in config_graph.Edges(): edges.append((edge.GetSrcNId(), edge.GetDstNId())) np.random.shuffle(edges) edges = set(edges) i = 0 while i < t: if i % 100 == 0: cf = snap.GetClustCf(config_graph, 1000) cfs.append(cf) e1 = edges.pop() e2 = edges.pop() if e1[0] == e1[1] or e2[0] == e2[1]: edges.add(e1) edges.add(e2) continue if np.random.rand() < 0.5: u = e1[0] v = e1[1] else: u = e1[1] v = e1[0] if np.random.rand() < 0.5: w = e2[0] x = e2[1] else: w = e2[1] x = e2[0] if not config_graph.IsEdge(u, w) and not config_graph.IsEdge( v, x) and u != w and v != x: edges.add((u, w)) edges.add((v, x)) config_graph.DelEdge(e1[0], e1[1]) config_graph.DelEdge(e2[0], e2[1]) config_graph.AddEdge(u, w) config_graph.AddEdge(v, x) i += 1 else: edges.add(e1) edges.add(e2) continue assert (config_graph.GetNodes() == n) assert (config_graph.GetEdges() == e) ########################################################################## return config_graph, cfs
def average_clustering_coefficient(self, sample_node=False): ''' Computes the average clustering coefficient as defined in Watts and Strogatz, Collective dynamics of ‘small-world’ networks :param sample_node: compute clustering coefficient only for a random sample of SampleNodes nodes. Useful for approximate but quick computations. ''' snap = self.snap GraphClustCoeff = snap.GetClustCf(self.graph, -1 if not sample_node else 1) return GraphClustCoeff
def printGenericInformation(graph, name): print("Generic informations of %s" % name) print('Nodes', graph.GetNodes()) print('Edges', graph.GetEdges()) print('Average degree (In+Out)', float(graph.GetEdges()) / float(graph.GetNodes())) print('Diameter', snap.GetBfsFullDiam(graph, 10)) print('Clustering coefficient', snap.GetClustCf(graph)) print('Triangles', snap.GetTriangleCnt(graph)) print('---------------------------------------')
def print_info(graph): for NI in graph.Nodes(): print("node: %d, out-degree %d, in-degree %d" % (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg())) print("Number of nodes: ", graph.GetNodes()) print("Number of edges: ", graph.GetEdges()) print("Maximum degree: ", graph.GetNI(snap.GetMxDegNId(graph)).GetDeg()) print("Diameter (approximate): ", snap.GetBfsFullDiam(graph, 10)) print("Triangles: ", snap.GetTriads(graph)) print("Clustering coefficient: ", snap.GetClustCf(graph))
def getFeatures(trees): # Root Node degree: rootNodes = loadMapping( "processed_data/half/half_dfTree_rootNode_mapping.txt") G = snap.LoadEdgeList(snap.PNGraph, "raw_data/higgs-social_network.edgelist", 0, 1) node_community_mapping, communities = load_communities() rootDegs = [] rootSocialDegs = [] strVirals = [] dfNodeCnt = [] dfEdgeCnt = [] coeffs = [] # Community features rootComm = [] largestComm = [] for i in rootNodes: index = int(i[0]) if index in trees: t = trees[index][0] nodeId = int(i[1]) nd = t.GetNI(nodeId) deg = nd.GetDeg() rootDegs.append(deg) rootSocialDegs.append(G.GetNI(nodeId).GetDeg()) strVirals.append(getStructuralVirality(trees[index][1])) dfNodeCnt.append(t.GetNodes()) dfEdgeCnt.append(t.GetEdges()) coeffs.append(snap.GetClustCf(t, -1)) rootComm.append(communities[node_community_mapping[nodeId]]) largestComm.append( getLargestComm(t, node_community_mapping, communities)) # print index ft = pd.DataFrame(np.transpose( [rootDegs, rootSocialDegs, strVirals, dfNodeCnt, dfEdgeCnt]), columns=[ "Root Degree", "Root Degree in Social Graph", "Structural Virality", "DF Node Count", "DF Edge Count" ]) ftWithComm = pd.DataFrame(np.transpose([ rootDegs, rootSocialDegs, strVirals, dfNodeCnt, dfEdgeCnt, rootComm, largestComm ]), columns=[ "Root Degree", "Root Degree in Social Graph", "Structural Virality", "DF Node Count", "DF Edge Count", "Root Node Community", "Largest Community Index" ]) return ft, ftWithComm
def getBasicProps(graph): # Assuming unweighted undirected graph return { 'num_nodes': graph.GetNodes(), 'num_edges': graph.GetEdges(), 'avg_deg': 2. * graph.GetEdges() / graph.GetNodes(), 'gamma': getGamma(graph), 'deg_centr': getDegCentr(graph), 'num_tris': snap.GetTriads(graph, -1), 'global_cc': snap.GetClustCf(graph, -1), }
def cosponsorGraphAnalysis(CoSponsor): print "Number of edges -" , CoSponsor.GetEdges() print "Number of nodes -" , CoSponsor.GetNodes() print "Average clustering coefficient", snap.GetClustCf(CoSponsor) result = snap.GetTriadsAll(CoSponsor) print "Number of closed triads", result[0] print "Number of open triads", result[2] pass
def analyze(graph): n = graph.GetNodes() m = graph.GetEdges() maxSCCsize = snap.GetMxSccSz(graph) maxWCCsize = snap.GetMxWccSz(graph) avgDegree = (m * float(2)) / n # estimate power law exponent degs = [] degCounts = [] DegToCntV = snap.TIntPrV() snap.GetDegCnt(graph, DegToCntV) for item in DegToCntV: degs.append(item.GetVal1()) degCounts.append(item.GetVal2()) xMin = min(degs) - 0.5 m = graph.GetNodes() alphaMLLE = 1 + (m / (sum([np.log(i / xMin) * degCounts[degs.index(i)] for i in degs]))) # erdos-renyi clustering coefficient graphER = snap.GenRndGnm(snap.PUNGraph, n, m) avgClustCoeffER = snap.GetClustCf(graphER, -1) # average shortest path graphWCC = snap.GetMxWcc(graph) avgClustCoeff = snap.GetClustCf(graphWCC, -1) numSamples = min(graphWCC.GetNodes(), 617) # all nodes or sample size Rnd = snap.TRnd(42) Rnd.Randomize() shortPathList = [] for i in xrange(numSamples): s = graphWCC.GetRndNId(Rnd) NIdToDistH = snap.TIntH() snap.GetShortPath(graphWCC, s, NIdToDistH) for item in NIdToDistH: shortPathList.append(NIdToDistH[item]) avgShortPath = np.mean(shortPathList) return avgClustCoeff, maxSCCsize, maxWCCsize, avgDegree, alphaMLLE, avgClustCoeffER, avgShortPath
def Q1_2(): """ Code for Q1.2 """ 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) print 'Computed using library functions...' C_erdosRenyi = snap.GetClustCf(erdosRenyi, 5242) C_smallWorld = snap.GetClustCf(smallWorld, 5242) C_collabNet = snap.GetClustCf(collabNet, 5242) 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 processNetwork(Graph, id_to_groups): with open("../../data/fastinf_graph_noweights_features.txt", "w+") as f: f.write("RELATED GROUPS GRAPH:\n") f.write('Edges: %d\n' % Graph.GetEdges()) f.write('Nodes: %d\n\n' % Graph.GetNodes()) MxWcc = snap.GetMxWcc(Graph) f.write("MAX WCC:\n") f.write('Edges: %f ' % MxWcc.GetEdges()) f.write('Nodes: %f \n' % MxWcc.GetNodes()) f.write('Node List: ') for node in MxWcc.Nodes(): f.write('%d, ' % node.GetId()) f.write('\n') for node in MxWcc.Nodes(): f.write('%s, ' % id_to_groups[node.GetId()]) f.write("\n\nALL WCCs:") Components = snap.TCnComV() snap.GetWccs(Graph, Components) for i, CnCom in enumerate(Components): if CnCom.Len() < 10: continue f.write('\nWcc%d: ' % i) for nodeid in CnCom: f.write('%d, ' % nodeid) MxScc = snap.GetMxScc(Graph) f.write("\n\nMAX SCC:\n") f.write('Edges: %f ' % MxScc.GetEdges()) f.write('Nodes: %f \n' % MxScc.GetNodes()) f.write('Node List: ') for node in MxScc.Nodes(): f.write('%d, ' % node.GetId()) f.write('\n') for node in MxScc.Nodes(): f.write('%s, ' % id_to_groups[node.GetId()]) f.write("\n\nALL SCCs:") Components = snap.TCnComV() snap.GetSccs(Graph, Components) for i, CnCom in enumerate(Components): if CnCom.Len() < 10: continue f.write('\nScc%d: ' % i) for nodeid in CnCom: f.write('%d, ' % nodeid) f.write('\n\nCLUSTERING AND COMMUNITIES:\n') f.write('Clustering coefficient: %f\n' % snap.GetClustCf(Graph, -1)) f.write('Num Triads: %d\n' % snap.GetTriads(Graph, -1)) Nodes = snap.TIntV() for node in Graph.Nodes(): Nodes.Add(node.GetId()) f.write('Modularity: %f' % snap.GetModularity(Graph, Nodes))
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 calcClusteringCoefficient(Graph): """ :param - Graph: snap.PUNGraph object representing an undirected graph return type: float returns: clustering coeffient of Graph """ ############################################################################ # TODO: Your code here! If you filled out calcClusteringCoefficientSingleNode, # you'll probably want to call it in a loop here C = snap.GetClustCf(Graph, -1) ############################################################################ return C
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 Q1_1a(): NUM_SAMPLE_NETWORKS = 100 np.random.seed(RANDOM_SEED) powerGrid = loadPowerGridNetwork() dist = [node.GetDeg() for node in powerGrid.Nodes()] clusteringCoeffs = [ snap.GetClustCf(GenerateConfigurationModel(dist)) for _ in xrange(NUM_SAMPLE_NETWORKS) ] print("The mean of the average clustering coefficient for %s random " "network%s with the same degree distribution as the power " "grid network is %s." % (NUM_SAMPLE_NETWORKS, "" if NUM_SAMPLE_NETWORKS <= 1 else "s", np.mean(clusteringCoeffs)))
def average_clustering_coefficient(self): """ Measurement: average_clustering_coefficient Description: Calculate the average clustering coefficient of the graph Input: Graph Output: Float """ if SNAP_LOADED: return sn.GetClustCf(self.gUNsn) else: return self.gUNig.transitivity_avglocal_undirected(mode='zero')