def set_clustering_distribution(self): # only indirected G_undirected = self.G.to_undirected() clustering_distributions = [] txt = '' # unweighted self.unweighted_clustering_distribution = nx.clustering(G_undirected) statistics = self.Stats.get_distribution_info(self.unweighted_clustering_distribution) #storing complete distribution for statistical analysis self.Stats.ks_store(self.unweighted_clustering_distribution, "unweighted clustering distribution") clustering_distributions.extend(statistics[:5]) clustering_distributions.extend(statistics[5]) txt += ',average clustering coeficient (unweighted)' + self.standard_text_distribution # # weighted self.weighted_clustering_distribution = nx.clustering(G_undirected, G_undirected.nodes(), self.weight_id) # statistics = self.Stats.get_distribution_info(self.weighted_clustering_distribution) # #storing complete distribution for statistical analysis # self.Stats.ks_store(self.weighted_clustering_distribution, "weighted clustering distribution") # clustering_distributions.extend(statistics[:5]) # clustering_distributions.extend(statistics[5]) # txt += ',average clustering coeficient (weighted)' + self.standard_text_distribution return [clustering_distributions,txt]
def test_path(self): G = nx.path_graph(10) assert_equal(list(nx.clustering(G).values()), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) assert_equal(nx.clustering(G), {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0})
def calculate_network_measures(net, analyser): deg=nx.degree_centrality(net) clust=[] if(net.is_multigraph()): net = analyser.flatGraph(net) if(nx.is_directed(net)): tmp_net=net.to_undirected() clust=nx.clustering(tmp_net) else: clust=nx.clustering(net) if(nx.is_directed(net)): tmp_net=net.to_undirected() paths=nx.shortest_path(tmp_net, source=None, target=None, weight=None) else: paths=nx.shortest_path(net, source=None, target=None, weight=None) lengths = [map(lambda a: len(a[1]), x[1].items()[1:]) for x in paths.items()] all_lengths=[] for a in lengths: all_lengths.extend(a) max_value=max(all_lengths) #all_lengths = [x / float(max_value) for x in all_lengths] return deg.values(),clust.values(),all_lengths
def stdCCnx(G): '''returns the standard deviation of clustering coefficients in a subgraph''' nodeCCs = [] for CC in nx.clustering(G): # print CC nodeCCs.append(nx.clustering(G,CC)) return np.std(nodeCCs)
def meanCCnx(G): '''returns the mean clustering coefficient in a subgraph''' nodeCCs = [] for CC in nx.clustering(G): # print CC nodeCCs.append(nx.clustering(G,CC)) return np.mean(nodeCCs)
def test_k5(self): G = nx.complete_graph(5) assert_equal(list(nx.clustering(G,weight='weight').values()),[1, 1, 1, 1, 1]) assert_equal(nx.average_clustering(G,weight='weight'),1) G.remove_edge(1,2) assert_equal(list(nx.clustering(G,weight='weight').values()), [5./6., 1.0, 1.0, 5./6., 5./6.]) assert_equal(nx.clustering(G,[1,4],weight='weight'),{1: 1.0, 4: 0.83333333333333337})
def test_cubical(self): G = nx.cubical_graph() assert_equal(list(nx.clustering(G).values()), [0, 0, 0, 0, 0, 0, 0, 0]) assert_equal(nx.clustering(G,1),0) assert_equal(list(nx.clustering(G,[1,2]).values()),[0, 0]) assert_equal(nx.clustering(G,1),0) assert_equal(nx.clustering(G,[1,2]),{1: 0, 2: 0})
def clustering_coefficient_distribution(G, return_dictionary=False): """Returns the distribution of clustering coefficients, amenable to applications similar to Borges, Coppersmith, Meyer, and Priebe 2011. If return_dictionary is specified, we return a dictionary indexed by vertex name, rather than just the values (as returned by default). """ if return_dictionary: return nx.clustering(G) else: return nx.clustering(G).values()
def node_data(self, node): ''' Returns node data related with the network structure ''' if not self.graph.is_multigraph(): clustering = nx.clustering(self.graph, node) original_clustering = nx.clustering(self.original_graph, node) else: clustering = None original_clustering = None return {'degree': self.graph.degree(node), 'clustering': clustering, 'original-degree': self.original_graph.degree(node), 'original-clustering': original_clustering}
def main(): #print 'main running!' #g=nx.read_adjlist("te.adj",nodetype=int) #ad=list() #mi=list() #su=list() ##print sel3(g,3,ad,mi,su) g=nx.Graph() g=nx.read_pajek("a.net") sh(g) nx.clustering(g)
def main(): graph = FBGraph(auth) data = graph.get_friends() for line in data: print line friends = graph.make_friends(data) graph.add_friend_nodes(friends) graph.add_likes(friends) friend_graph = graph.get_friend_graph() print '\n' print nx.clustering(friend_graph) print '\n' for edge in sorted(friend_graph.edges(data=True), key= lambda x: -1*x[2].get('weight', 1)): print edge nx.draw_random(friend_graph)
def get_node_features(self, graph, node): """ Node features based on NetSimile paper :param node: :type node: :return: :rtype: """ """ degree of node cluserting coef of node avg number of node's two-hop away neighbors avg clustering coef of Neighbors(node) number of edges in node i's egonet number of outgoing edges from ego(node) number of neighbors(ego(node)) """ neighbors = graph.neighbors(node) degree = graph.degree(node) cl_coef = networkx.clustering(graph, node) nbrs_two_hops = 0.0 nbrs_cl_coef = 0.0 for neighbor in neighbors: nbrs_two_hops += graph.degree(neighbor) nbrs_cl_coef += networkx.clustering(graph, neighbor) try: avg_two_hops = nbrs_two_hops / degree avg_cl_coef = nbrs_cl_coef / degree except ZeroDivisionError: avg_two_hops = 0.0 avg_cl_coef = 0.0 egonet = networkx.ego_graph(graph, node) ego_size = egonet.size() ego_out = 0 ego_nbrs = set() for ego_node in egonet: for nbr in graph.neighbors(ego_node): if nbr not in neighbors: ego_out += 1 ego_nbrs.add(nbr) return [degree, cl_coef, avg_two_hops, avg_cl_coef, ego_size, ego_out, len(ego_nbrs)]
def run(self): data = self.get_friends() for line in data: print line friends = self.make_friends(data) self.add_friend_nodes(friends) self.add_likes(friends) friend_graph = self.get_friend_graph() self.prune_friends() print '\n' print nx.clustering(friend_graph) print '\n' for edge in sorted(friend_graph.edges(data=True), key= lambda x: -1*x[2].get('weight', 1)): print edge nx.draw(friend_graph) plt.savefig('graph.png')
def show_network_metrics(G): ''' Print the local and global metrics of the network ''' print(nx.info(G)) # density print("Density of the network") print(nx.density(G)) # average betweeness print("Average betweeness of the network") print(np.sum(list(nx.betweenness_centrality(G).values()))/len(nx.betweenness_centrality(G))) # Average clustering coefficient print("Average clustering coefficient:") print(nx.average_clustering(G)) #create metrics dataframe by_node_metrics = pd.DataFrame({"Betweeness_Centrality":nx.betweenness_centrality(G),"Degree_Centrality":nx.degree_centrality(G), "Clustering_Coefficient":nx.clustering(G), "Triangels":nx.algorithms.cluster.triangles(G)}) print(by_node_metrics) by_node_metrics.to_excel("metrics.xlsx")
def build_graph(): pair_list = TwitterUser.get_top_100_pair() DG = nx.DiGraph() DG.add_edges_from([(foer, twitter_user) for twitter_user, foer in pair_list]) betweenness = nx.betweenness_centrality(DG) closeness = nx.closeness_centrality(DG) edge_betweenness = nx.edge_betweenness(DG) clustering_co = nx.clustering(nx.Graph(DG)) page_rank = nx.pagerank(DG) for twitter_id in DG.nodes(): t = TwitterUser.get_by_id(twitter_id) node = DG.node[twitter_id] node['user_id'] = t.user_id node['label'] = t.scrn_name node['follower_count'] = t.foer_cnt node['friend_count'] = t.friend_cnt node['status_count'] = t.status_cnt node['location'] = t.location node['verified'] = t.verified node['twitter_age'] = (date.today() - t.created_at).days node['daily_tweet'] = t.status_cnt*1.0/node['twitter_age'] node['indegree'] = len([(id, foer) for id, foer in pair_list if id == twitter_id]) node['outdegree'] = len([(id, foer) for id, foer in pair_list if foer == twitter_id]) node['cluster'] = clustering_co[twitter_id] node['betweenness'] = betweenness[twitter_id] node['closeness'] = closeness[twitter_id] node['page_rank'] = page_rank[twitter_id] for out_n, in_n in DG.edges(): DG[out_n][in_n]['edge_betweenness'] = edge_betweenness[(out_n,in_n)] return DG
def nodal_summaryOut(graph): """Compute statistics for individual nodes. Parameters ---------- graph: networkx graph An undirected graph. Returns ------- dictionary The keys of this dictionary are lp (which refers to path length), clust (clustering coefficient), b_cen (betweenness centrality), c_cen (closeness centrality), nod_eff (nodal efficiency), loc_eff (local efficiency), and deg (degree). The values are arrays (or lists, in some cases) of metrics, in ascending order of node labels. """ lp = nodal_pathlengths(graph) clust_dict = nx.clustering(graph) clust = np.array([clust_dict[n] for n in sorted(clust_dict)]) b_cen_dict = nx.betweenness_centrality(graph) b_cen = np.array([b_cen_dict[n] for n in sorted(b_cen_dict)]) c_cen_dict = nx.closeness_centrality(graph) c_cen = np.array([c_cen_dict[n] for n in sorted(c_cen_dict)]) nod_eff = nodal_efficiency(graph) loc_eff = local_efficiency(graph) deg_dict = graph.degree() deg = [deg_dict[n] for n in sorted(deg_dict)] return dict(lp=lp, clust=clust, b_cen=b_cen, c_cen=c_cen, nod_eff=nod_eff, loc_eff=loc_eff, deg=deg)
def __init__(self, n=1000, k=10, p=0.02947368): self.n = n self.k = k self.p = p self.ws = nx.watts_strogatz_graph(self.n, self.k, self.p, seed='nsll') nx.set_node_attributes(self.ws, 'SIR', 'S') self.clustering = nx.clustering(self.ws) self.betweenness = nx.betweenness_centrality(self.ws) p_r_0 = 0.001 r_0 = int(self.n * p_r_0) if r_0 < 1: r_0 = 1 random.seed('nsll') self.r = random.sample(self.ws.nodes(), r_0) i_0 = 4 if i_0 < r_0: i_0 += 1 random.seed('nsll') self.infected = random.sample(self.ws.nodes(), i_0) for n in self.infected: self.ws.node[n]['SIR'] = 'I' for n in self.r: self.ws.node[n]['SIR'] = 'R' self.s = self.n - len(self.infected) - len(self.r) print(self.r) print(self.infected)
def __init__(self, graph, node_1=None, node_2=None): self.graph = graph self.node_1 = node_1 self.node_2 = node_2 self.clustering_dict = nx.clustering(graph) self.betweenness_dict = nx.betweenness_centrality(graph) self.average_neighbor_degree_dict = nx.average_neighbor_degree(graph) self.attributes_map = { "adamic_adar_similarity": self.adamic_adar_similarity, "average_clustering_coefficient": self.average_clustering_coefficient, "average_neighbor_degree_sum": self.average_neighbor_degree_sum, "betweenness_centrality": self.betweenness_centrality, "closeness_centrality_sum": self.closeness_centrality_sum, "clustering_coefficient_sum": self.clustering_coefficient_sum, "common_neighbors": self.common_neighbors, "cosine": self.cosine, "jaccard_coefficient": self.jaccard_coefficient, "katz_measure": self.katz_measure, "preferential_attachment": self.preferential_attachment, "square_clustering_coefficient_sum": self.square_clustering_coefficient_sum, "sum_of_neighbors": self.sum_of_neighbors, "sum_of_papers": self.sum_of_papers, "get_shortest_path_length": self.get_shortest_path_length, "get_second_shortest_path_length": self.get_second_shortest_path_length } if(self.node_1 != None and self.node_2 != None): self.neighbors_1 = self.all_neighbors(self.node_1) self.neighbors_2 = self.all_neighbors(self.node_2)
def calculate_local_clustering_coeff(graph): hash_clus = nx.clustering(graph) clus = 0 nodes = graph.nodes() for itr in graph.nodes(): clus = clus + hash_clus[itr] print(clus/len(nodes))
def plot_clustering_spectrum (graph, path): """Plot the clusttering spectrum of the graph and save the figure at the given path. On X-axis we have degrees and on Y-axis we have average clustering coefficients of the nodes that have that degree""" node_to_degree = graph.degree() node_to_clustering = nx.clustering(graph) degree_to_clustering = {} # calculate average clustering coefficients for nodes with certain degree for node in node_to_degree: deg = node_to_degree[node] tmp = degree_to_clustering.get(deg, []) tmp.append(node_to_clustering[node]) degree_to_clustering[deg] = tmp for degree in degree_to_clustering: tmp = degree_to_clustering[degree] degree_to_clustering[degree] = float(sum(tmp)) / len(tmp) x = sorted(degree_to_clustering.keys(), reverse = True) y = [degree_to_clustering[i] for i in x] plt.loglog(x, y, 'b-', marker = '.') plt.title("Clustering Spectrum") plt.ylabel("Average clustering coefficient") plt.xlabel("Degree") plt.axis('tight') plt.savefig(path)
def test_fast_versions_properties_threshold_graphs(self): cs='ddiiddid' G=nxt.threshold_graph(cs) assert_equal(nxt.density('ddiiddid'), nx.density(G)) assert_equal(sorted(nxt.degree_sequence(cs)), sorted(G.degree().values())) ts=nxt.triangle_sequence(cs) assert_equal(ts, list(nx.triangles(G).values())) assert_equal(sum(ts) // 3, nxt.triangles(cs)) c1=nxt.cluster_sequence(cs) c2=list(nx.clustering(G).values()) assert_almost_equal(sum([abs(c-d) for c,d in zip(c1,c2)]), 0) b1=nx.betweenness_centrality(G).values() b2=nxt.betweenness_sequence(cs) assert_true(sum([abs(c-d) for c,d in zip(b1,b2)]) < 1e-14) assert_equal(nxt.eigenvalues(cs), [0, 1, 3, 3, 5, 7, 7, 8]) # Degree Correlation assert_true(abs(nxt.degree_correlation(cs)+0.593038821954) < 1e-12) assert_equal(nxt.degree_correlation('diiiddi'), -0.8) assert_equal(nxt.degree_correlation('did'), -1.0) assert_equal(nxt.degree_correlation('ddd'), 1.0) assert_equal(nxt.eigenvalues('dddiii'), [0, 0, 0, 0, 3, 3]) assert_equal(nxt.eigenvalues('dddiiid'), [0, 1, 1, 1, 4, 4, 7])
def describe(G, ny_tri, chems): global describeNetwork ''' Describe the network: degrees, clustering, and centrality measures ''' # Degree # The number of connections a node has to other nodes. degrees= nx.degree(G) degrees_df = pd.DataFrame(degrees.items(), columns=['Facility', 'Degrees']) values = sorted(set(degrees.values())) hist = [degrees.values().count(x) for x in values] plt.figure() plt.plot(values, hist,'ro-') # degree plt.xlabel('Degree') plt.ylabel('Number of nodes') plt.title('Degree Distribution') plt.savefig('output/degree_distribution.png') # Clustering coefficients # The bipartie clustering coefficient is a measure of local density of connections. clust_coefficients = nx.clustering(G) clust_coefficients_df = pd.DataFrame(clust_coefficients.items(), columns=['Facility', 'Clustering Coefficient']) clust_coefficients_df = clust_coefficients_df.sort('Clustering Coefficient', ascending=False) #print clust_coefficients_df # Node centrality measures FCG=list(nx.connected_component_subgraphs(G, copy=True))[0] # Current flow betweenness centrality # Current-flow betweenness centrality uses an electrical current model for information spreading # in contrast to betweenness centrality which uses shortest paths. betweeness = nx.current_flow_betweenness_centrality(FCG) betweeness_df = pd.DataFrame(betweeness.items(), columns=['Facility', 'Betweeness']) betweeness_df = betweeness_df.sort('Betweeness', ascending=False) # Closeness centrality # The closeness of a node is the distance to all other nodes in the graph # or in the case that the graph is not connected to all other nodes in the connected component containing that node. closeness = nx.closeness_centrality(FCG) closeness_df = pd.DataFrame(closeness.items(), columns=['Facility', 'Closeness']) closeness_df = closeness_df.sort('Closeness', ascending=False) # Eigenvector centrality # Eigenvector centrality computes the centrality for a node based on the centrality of its neighbors. # In other words, how connected a node is to other highly connected nodes. eigenvector = nx.eigenvector_centrality(FCG) eigenvector_df = pd.DataFrame(eigenvector.items(), columns=['Facility', 'Eigenvector']) eigenvector_df = eigenvector_df.sort('Eigenvector', ascending=False) # Create dataframe of facility info fac_info = ny_tri[['tri_facility_id','facility_name', 'primary_naics', 'parent_company_name']].drop_duplicates() fac_info.rename(columns={'facility_name':'Facility'}, inplace=True) # Merge everything describeNetwork = degrees_df.merge( clust_coefficients_df,on='Facility').merge( betweeness_df,on='Facility').merge( closeness_df, on='Facility').merge( eigenvector_df, on='Facility').merge( fac_info, on='Facility', how='left').merge( chems, on='Facility', how='left') describeNetwork = describeNetwork.sort('Degrees', ascending=False) describeNetwork.to_csv('output/describeNetwork.csv')
def distance_cc_corr(G): # Rank correlation between distance (edge lengths) and clustering coefficient. # Usage: rho,p = distance_cc_corr(G) # Input: # G: An undirected networkx graph object # Returns: # rho: rank correlation coefficient # p: the associated p-value nodes = G.nodes() edge_distances = get_edge_distances(G) mean_edge_distances = {} for node in G.nodes(): current_edges = [] for edge in edge_distances: if node in edge: current_edges.append(edge_distances[edge]) mean_edge_distances[node] = np.mean(current_edges) cc_dict = nx.clustering(G.to_undirected()); cc = [cc_dict[node] for node in nodes] edists = [mean_edge_distances[node] for node in nodes] distance_clustering = stats.spearmanr(edists,cc) rho = distance_clustering.correlation p = distance_clustering.pvalue return rho,p
def cc_deg_corr(G,connectome=False): # Correlation between clustering and degree. # Usage: r,p = cc_deg_corr(G) # Input: # G: an undirected networkx graph object # Returns: # r: Spearman rank correlation coefficient # p: the associated p-value # # Note: The argument connectome=True should be set if running # with the connectome. The connectome has 213 independent # nodes because it is mirrored along the hemispheric divide # in order to construct a complete graph. nodes = G.nodes() cc_dict = nx.clustering(G) deg_dict = G.degree() cc = [cc_dict[node] for node in nodes] deg = [deg_dict[node] for node in nodes] if connectome: cc = np.array(cc)[0:len(nodes)/2] deg = np.array(deg)[0:len(nodes)/2] corr = stats.spearmanr(cc,deg) r = corr.correlation; p = corr.pvalue return r,p
def calc_clustering_coefficient(g, dest_file): """ calc_clustering_coefficient(g) Calculate & plot clustering coefficient of the graph g and writes data to the created data output file :param g: graph as source :return: --- """ func_intro = "\n\nClustering Co-Efficient ..." logging.info(cs_ref, func_intro) print func_intro with open(dest_file, "a") as dat_file: dat_file.write(func_intro) cce = nx.clustering(g) # calculate clustering co-efficient with open(dest_file, "a") as dat_file: dat_file.write("\n\tClustering Coefficients for nodes in graph = \t" + str(cce)) average_cce = nx.average_clustering(g) with open(dest_file, "a") as dat_file: dat_file.write("\n\tAverage Clustering Coefficient for graph = \t" + str(average_cce)) for edge in g.edges(): # plot clustering co-efficient if floor(edge[0] / 5.) != floor(edge[1] / 5.): if random.random() < 0.95: g.remove_edge(edge[0], edge[1]) plt.figure(3) fixed_pos = {1: (0, 0), 10: (1, 1), 30: (1, 0), 50: (0, 1)} pos = nx.spring_layout(g, fixed=fixed_pos.keys(), pos=fixed_pos) nx.draw_networkx(g, pos=pos) plt.title("Clustering Co-efficient" + src_file) plt.savefig("plots/cs1_clustering_coefficient.png") plt.show()
def __init__(self, G_list, max_num_nodes, features='id'): self.max_num_nodes = max_num_nodes self.adj_all = [] self.len_all = [] self.feature_all = [] for G in G_list: adj = nx.to_numpy_matrix(G) # the diagonal entries are 1 since they denote node probability self.adj_all.append( np.asarray(adj) + np.identity(G.number_of_nodes())) self.len_all.append(G.number_of_nodes()) if features == 'id': self.feature_all.append(np.identity(max_num_nodes)) elif features == 'deg': degs = np.sum(np.array(adj), 1) degs = np.expand_dims(np.pad(degs, [0, max_num_nodes - G.number_of_nodes()], 0), axis=1) self.feature_all.append(degs) elif features == 'struct': degs = np.sum(np.array(adj), 1) degs = np.expand_dims(np.pad(degs, [0, max_num_nodes - G.number_of_nodes()], 'constant'), axis=1) clusterings = np.array(list(nx.clustering(G).values())) clusterings = np.expand_dims(np.pad(clusterings, [0, max_num_nodes - G.number_of_nodes()], 'constant'), axis=1) self.feature_all.append(np.hstack([degs, clusterings]))
def findhotspotssubg(G, direction, extent): if direction == 'x': boundaries = [extent[0],extent[1]] elif direction == 'y': boundaries = [extent[2],extent[3]] elif direction == 'z': boundaries = [extent[4],extent[5]] else: print "findhotspotssubg: Direction not available." return if boundaries[1] == boundaries[0]: pass else: components = nx.connected_component_subgraphs(G) hubs = [] clust = [] for i in range(len(components)): # convert subgraph from MultiGraph to Graph nodes = components[i].nodes() edges = components[i].edges() h = nx.Graph() h.add_nodes_from(nodes) h.add_edges_from(edges) # get degree and clustering degree_sequence = nx.degree(h).values() hub = max(degree_sequence) hclust = nx.clustering(h).values() hclust_max = max(hclust) hubs.append(hub) clust.append(hclust_max) return hubs, clust
def OnClustering(self,e): self.rtb.SetValue("") self.PushStatusText("Calculte the CC", SB_INFO) #cStr="The average clustering of the graph is :\n"+str(nx.average_clustering(self.g))+"\n" cStr="The Clustering of the graph is :\n"+str(nx.clustering(self.g)) self.rtb.SetValue(cStr)
def draw_graph(label_flag=True, remove_isolated=True, different_size=True, iso_level=10, node_size=40): G=build_graph(fb.get_friends_network()) betweenness=nx.betweenness_centrality(G) degree=nx.degree_centrality(G) degree_num=[ degree[v] for v in G] maxdegree=max(degree_num);mindegree=min(degree_num); print maxdegree,mindegree clustering=nx.clustering(G) print nx.transitivity(G) # Judge whether remove the isolated point from graph if remove_isolated is True: H = nx.empty_graph() for SG in nx.connected_component_subgraphs(G): if SG.number_of_nodes() > iso_level: H = nx.union(SG, H) G = H # Ajust graph for better presentation if different_size is True: L = nx.degree(G) G.dot_size = {} for k, v in L.items(): G.dot_size[k] = v #node_size = [betweenness[v] *1000 for v in G] node_size = [G.dot_size[v] * 10 for v in G] node_color= [((degree[v]-mindegree))/(maxdegree-mindegree) for v in G] #edge_width = [getcommonfriends(u,v) for u,v in G.edges()] pos = nx.spring_layout(G, iterations=15) nx.draw_networkx_edges(G, pos, alpha=0.05) nx.draw_networkx_nodes(G, pos, node_size=node_size, node_color=node_color, vmin=0.0,vmax=1.0, alpha=0.3) # Judge whether shows label if label_flag is True: nx.draw_networkx_labels(G, pos, font_size=6,alpha=0.1) #nx.draw_graphviz(G) plt.show() return G
def nodal_summaryOut(G, n_nodes): """ Compute statistics for individual nodes Parameters ---------- G: graph data output from mkgraph out: array output from nodal_summaryOut, so can keep appending cost: cost value for these calculations n_nodes: number of nodes in graph. Returns ------- A dict with: lp, clust, b_cen, c_cen, nod_eff, loc_eff, degree.""" lp = nodal_pathlengths(G,n_nodes) #can't use the regular one, because it substitutes [] for disconnected nodes clust = np.array(nx.clustering(G).values()) b_cen = np.array(nx.betweenness_centrality(G).values()) c_cen = np.array(nx.closeness_centrality(G).values()) nod_eff=nodal_efficiency(G) loc_eff=local_efficiency(G) deg = G.degree().values() return dict(lp=lp, clust=clust, b_cen=b_cen, c_cen=c_cen, nod_eff=nod_eff, loc_eff=loc_eff,deg=deg)
nhood.remove_edges_from(chords) # Draw the neighborhood and the ego-centric network for g, ofile in zip((nhood, egonet), ("neighborhood", "egonet")): nx.draw_networkx_edges(g, pos, alpha=0.7, **dzcnapy.attrs) nx.draw_networkx_nodes(g, pos, **dzcnapy.attrs) nx.draw_networkx_labels(g, pos, **dzcnapy.medium_attrs) dzcnapy.set_extent(pos, plt) dzcnapy.plot(ofile) # This part of the script calculates degrees and clustering coefficients # and plots a scatter plot of them F = nx.Graph(G) deg = pd.Series(dict(nx.degree(G))) cc = pd.Series({e: nx.clustering(F, e) for e in F}) deg_cc = pd.concat([deg, cc], axis=1) deg_cc.columns = ("Degree", "CC") deg_cc.groupby("Degree").mean().reset_index()\ .plot(kind="scatter", x="Degree", y="CC", s=100) plt.xscale("log") plt.ylim(ymin=0) plt.grid() dzcnapy.plot("deg_cc") # A study of centralities dgr = nx.degree_centrality(G) clo = nx.closeness_centrality(G) har = nx.harmonic_centrality(G) eig = nx.eigenvector_centrality(G) bet = nx.betweenness_centrality(G)
plt.savefig("/Users/jennie/Documents/analytics/xanax/Networkx_xanax.png") print("node degree clustering") for v in nx.nodes(G): print('%s %d %f' % (v, nx.degree(G, v), nx.clustering(G, v))) # print the adjacency list to terminal try: nx.write_adjlist(G, sys.stdout) except TypeError: # Python 3.x nx.write_adjlist(G, sys.stdout.buffer) nx.draw(G,node_size=50, with_labels=True) plt.show() # Model Selection #model = sir.SIRModel(h)
G.add_edge(ac[0], ac[1], weight=len(intersect_set)) # nx.draw(G) # plt.show() print G.number_of_nodes() print G.number_of_edges() #degree d_d = nx.degree(G) #degree centrality degree_centrality = nx.degree_centrality(G) #clustering coefficient d_cc = nx.clustering(G) #betweeness centrality d_bc = nx.betweenness_centrality(G) writeToFile(d_d, user_degree) d_degree = {} for key in d_d.keys(): print key, d_d[key] degree = d_d[key] if degree in d_degree.keys(): c = d_degree[degree] c += 1 d_degree[degree] = c else:
ias,ds,nodes=[],[],[i for i in range(len(B))] while(len(clv)!=0): x=clv.pop(0) ias.append(list(Degree.keys())[list(Degree.values()).index(x)]) Degree.pop(list(Degree.keys())[list(Degree.values()).index(x)]) ds=list(set(nodes)-set(ias)) ''' #High Clustering Coefficient Heuristic import numpy as np import networkx as nx import UserNetwork #A=[[0,1,1,0,1,0,0,1,0,0,0],[1,0,1,1,0,1,0,0,0,1,0],[1,1,0,1,1,0,1,0,0,0,0],[0,1,1,0,0,1,1,0,0,0,0],[1,0,1,0,0,0,0,1,1,0,0],[0,1,0,1,0,0,1,0,0,1,0],[0,0,1,1,0,1,0,0,1,1,0],[1,0,0,0,1,0,0,0,1,0,0],[0,0,0,0,1,0,1,1,0,1,0],[0,1,0,0,0,1,1,0,1,0,0],[0,0,0,0,0,0,0,0,0,0,0]] A=UserNetwork.a A=np.matrix(A) G=nx.from_numpy_matrix(A) cl=(nx.clustering(G)) clv=list(cl.values()) clv.sort() n=int(input('Enter the seed set size:')) clv=clv[-n:] ias,ds,nodes=[],[],[i for i in range(len(A))] while(len(clv)!=0): x=clv.pop(0) ias.append(list(cl.keys())[list(cl.values()).index(x)]) cl.pop(list(cl.keys())[list(cl.values()).index(x)]) ds=list(set(nodes)-set(ias)) '''#Major Degree Heuristic import UserNetwork A=UserNetwork.a
def clustering_worker(param): G, bins = param clustering_coeffs_list = list(nx.clustering(G).values()) hist, _ = np.histogram( clustering_coeffs_list, bins=bins, range=(0.0, 1.0), density=False) return hist
def extended_stats(G, connectivity=False, anc=False, ecc=False, bc=False, cc=False): """ Calculate extended topological stats and metrics for a graph. Many of these algorithms have an inherently high time complexity. Global topological analysis of large complex networks is extremely time consuming and may exhaust computer memory. Consider using function arguments to not run metrics that require computation of a full matrix of paths if they will not be needed. Parameters ---------- G : networkx multidigraph connectivity : bool if True, calculate node and edge connectivity anc : bool if True, calculate average node connectivity ecc : bool if True, calculate shortest paths, eccentricity, and topological metrics that use eccentricity bc : bool if True, calculate node betweenness centrality cc : bool if True, calculate node closeness centrality Returns ------- stats : dict dictionary of network measures containing the following elements (some only calculated/returned optionally, based on passed parameters): - avg_neighbor_degree - avg_neighbor_degree_avg - avg_weighted_neighbor_degree - avg_weighted_neighbor_degree_avg - degree_centrality - degree_centrality_avg - clustering_coefficient - clustering_coefficient_avg - clustering_coefficient_weighted - clustering_coefficient_weighted_avg - pagerank - pagerank_max_node - pagerank_max - pagerank_min_node - pagerank_min - node_connectivity - node_connectivity_avg - edge_connectivity - eccentricity - diameter - radius - center - periphery - closeness_centrality - closeness_centrality_avg - betweenness_centrality - betweenness_centrality_avg """ stats = {} full_start_time = time.time() # create a DiGraph from the MultiDiGraph, for those metrics that require it G_dir = nx.DiGraph(G) # create an undirected Graph from the MultiDiGraph, for those metrics that # require it G_undir = nx.Graph(G) # get the largest strongly connected component, for those metrics that # require strongly connected graphs G_strong = get_largest_component(G, strongly=True) # average degree of the neighborhood of each node, and average for the graph avg_neighbor_degree = nx.average_neighbor_degree(G) stats['avg_neighbor_degree'] = avg_neighbor_degree stats['avg_neighbor_degree_avg'] = sum( avg_neighbor_degree.values()) / len(avg_neighbor_degree) # average weighted degree of the neighborhood of each node, and average for # the graph avg_weighted_neighbor_degree = nx.average_neighbor_degree(G, weight='length') stats['avg_weighted_neighbor_degree'] = avg_weighted_neighbor_degree stats['avg_weighted_neighbor_degree_avg'] = sum( avg_weighted_neighbor_degree.values()) / len( avg_weighted_neighbor_degree) # degree centrality for a node is the fraction of nodes it is connected to degree_centrality = nx.degree_centrality(G) stats['degree_centrality'] = degree_centrality stats['degree_centrality_avg'] = sum( degree_centrality.values()) / len(degree_centrality) # calculate clustering coefficient for the nodes stats['clustering_coefficient'] = nx.clustering(G_undir) # average clustering coefficient for the graph stats['clustering_coefficient_avg'] = nx.average_clustering(G_undir) # calculate weighted clustering coefficient for the nodes stats['clustering_coefficient_weighted'] = nx.clustering(G_undir, weight='length') # average clustering coefficient (weighted) for the graph stats['clustering_coefficient_weighted_avg'] = nx.average_clustering( G_undir, weight='length') # pagerank: a ranking of the nodes in the graph based on the structure of # the incoming links pagerank = nx.pagerank(G_dir, weight='length') stats['pagerank'] = pagerank # node with the highest page rank, and its value pagerank_max_node = max(pagerank, key=lambda x: pagerank[x]) stats['pagerank_max_node'] = pagerank_max_node stats['pagerank_max'] = pagerank[pagerank_max_node] # node with the lowest page rank, and its value pagerank_min_node = min(pagerank, key=lambda x: pagerank[x]) stats['pagerank_min_node'] = pagerank_min_node stats['pagerank_min'] = pagerank[pagerank_min_node] # if True, calculate node and edge connectivity if connectivity: start_time = time.time() # node connectivity is the minimum number of nodes that must be removed # to disconnect G or render it trivial stats['node_connectivity'] = nx.node_connectivity(G_strong) # edge connectivity is equal to the minimum number of edges that must be # removed to disconnect G or render it trivial stats['edge_connectivity'] = nx.edge_connectivity(G_strong) log('Calculated node and edge connectivity in {:,.2f} seconds'.format( time.time() - start_time)) # if True, calculate average node connectivity if anc: # mean number of internally node-disjoint paths between each pair of # nodes in G, i.e., the expected number of nodes that must be removed to # disconnect a randomly selected pair of non-adjacent nodes start_time = time.time() stats['node_connectivity_avg'] = nx.average_node_connectivity(G) log('Calculated average node connectivity in {:,.2f} seconds'.format( time.time() - start_time)) # if True, calculate shortest paths, eccentricity, and topological metrics # that use eccentricity if ecc: # precompute shortest paths between all nodes for eccentricity-based # stats start_time = time.time() sp = { source: dict( nx.single_source_dijkstra_path_length(G_strong, source, weight='length')) for source in G_strong.nodes() } log('Calculated shortest path lengths in {:,.2f} seconds'.format( time.time() - start_time)) # eccentricity of a node v is the maximum distance from v to all other # nodes in G eccentricity = nx.eccentricity(G_strong, sp=sp) stats['eccentricity'] = eccentricity # diameter is the maximum eccentricity diameter = nx.diameter(G_strong, e=eccentricity) stats['diameter'] = diameter # radius is the minimum eccentricity radius = nx.radius(G_strong, e=eccentricity) stats['radius'] = radius # center is the set of nodes with eccentricity equal to radius center = nx.center(G_strong, e=eccentricity) stats['center'] = center # periphery is the set of nodes with eccentricity equal to the diameter periphery = nx.periphery(G_strong, e=eccentricity) stats['periphery'] = periphery # if True, calculate node closeness centrality if cc: # closeness centrality of a node is the reciprocal of the sum of the # shortest path distances from u to all other nodes start_time = time.time() closeness_centrality = nx.closeness_centrality(G, distance='length') stats['closeness_centrality'] = closeness_centrality stats['closeness_centrality_avg'] = sum( closeness_centrality.values()) / len(closeness_centrality) log('Calculated closeness centrality in {:,.2f} seconds'.format( time.time() - start_time)) # if True, calculate node betweenness centrality if bc: # betweenness centrality of a node is the sum of the fraction of # all-pairs shortest paths that pass through node start_time = time.time() betweenness_centrality = nx.betweenness_centrality(G, weight='length') stats['betweenness_centrality'] = betweenness_centrality stats['betweenness_centrality_avg'] = sum( betweenness_centrality.values()) / len(betweenness_centrality) log('Calculated betweenness centrality in {:,.2f} seconds'.format( time.time() - start_time)) log('Calculated extended stats in {:,.2f} seconds'.format(time.time() - full_start_time)) return stats
import networkx as nx g = nx.read_edgelist('edge_list.csv', create_using=nx.Graph(), delimiter=',', nodetype=str) # clustering coefficient calculation ccs = nx.clustering(g) #average clustering coefficient calculation avg_clust = sum(ccs.values()) / len(ccs) print("average clustering coefficient = ", avg_clust)
""" Name: Generate Graph: regular graph Author: Jia_qiu Wang(王佳秋) Data: December, 2016 function: """ import networkx as nx g = nx.Graph() g.add_edges_from([(1, 2), (1, 3)]) g.add_node("spam") nx.connected_components(g) # 将node有关联的显示一个数组中。返回的为一个数组 # sorted(nx.degree(g).values()) print(sorted(nx.degree(g).values())) # 聚合 print(nx.clustering(g)) # 显示等级 print(nx.degree(g)) nx.draw(g) nx.draw_random(g) nx.draw_circular(g)
def networkx_stats(graph, digraph, node_dict): lccs = nx.clustering(graph) for node_id, lcc in lccs.items(): node = node_dict[node_id] node.lcc = lcc
def signature(self): G = self.G edges = G.edges() nodes = G.nodes() graph_feature = [] # 2D: node_num * feature dimention feature_num = 6 # 6 basic metrics from Netsimile & 4 custom defined for node in nodes: feature = [] # for each node neighbor_num = len(G.neighbors(node)) # 1: number of neighbors feature.append(neighbor_num) coefficient = nx.clustering(G, node) # 2: clustering coefficient feature.append(coefficient) up_two_hop = nx.single_source_shortest_path_length(G, node, cutoff=2) # all nodes which has shortest path to "node" with length less than 2 up_one_hop = nx.single_source_shortest_path_length(G, node, cutoff=1) two_hop = {k:v for k,v in up_two_hop.items() if k not in up_one_hop} two_hop_avg = len(two_hop)/(neighbor_num) # 3: average number of node's two-hop away neighbours feature.append(two_hop_avg) coefficient_sum = 0 for vertex in G.neighbors(node): coefficient_sum += nx.clustering(G, vertex) coefficient_avg = coefficient_sum/neighbor_num feature.append(coefficient_avg) # 4: average clustering coefficient of neighbours ego_net = nx.ego_graph(G, node, undirected=True) # egonet of 'node', type: graph edge_num = len(ego_net.edges()) # 5: number of edges in egonet of 'node' feature.append(edge_num) ego_neighbor_num = 0 # 6: number of egonet's neighbors egonet_no_center = nx.ego_graph(G, node, undirected=True, center=False) # exclude the center vertex for vertex in egonet_no_center.nodes(): G_degree = G.degree(vertex) ego_degree = egonet_no_center.degree(vertex)+1 ego_neighbor_num += (G_degree - ego_degree) feature.append(ego_neighbor_num) graph_feature.append(feature) ################## Normalization for 'statistical hypothesis testing' ################## # L2_norm = LA.norm(graph_feature, ord=2, axis=0) # vactor, length=6 (#features) # # Each feature column divide by corresponding element in L2_norm # normalized_mtx = graph_feature / L2_norm[None,:] ########################### Aggregation ########################### graph_signature = [] feature_matrix = np.asmatrix(graph_feature) # convert 2D array to matrix # print('graph matrix shape: ', feature_matrix.shape) for i in range(0,feature_num): column = feature_matrix[:,i] median = float(st.median(column)) # aggregator 1 graph_signature.append(median) mean = np.mean(column) # aggregator 2 graph_signature.append(mean) std_dev = np.std(column) # aggregator 3 graph_signature.append(std_dev) skew = float(scipy.stats.skew(column)) # aggregator 4 graph_signature.append(skew) kurtosis = float(scipy.stats.kurtosis(column)) # aggregator 5 graph_signature.append(kurtosis) # print('signature length: ', len(graph_signature)) # above: local attributes # below: global attributes # 7: max node degree # graph_signature.append() # 8: largest cycle size # 9: longest path # 10: edge density among nodes in cycle return graph_signature
# If your Graph has more than one component, this will return False: print(nx.is_connected(G)) components = nx.connected_components(G) # using nx.connected_components to get the list of components largest_component = max(components, key=len) # using the max() command to find the largest one subgraph = G.subgraph(largest_component) # Create a "subgraph" of just the largest component diameter = nx.diameter(subgraph) # Then calculate the diameter of the subgraph, just like you did with density. print("Network diameter of largest component:", diameter) # print the diameter of graph # Betweenness Centrality betweenness_dict = nx.betweenness_centrality(G) # Run betweenness centrality # Assign each to an attribute in your network nx.set_node_attributes(G, betweenness_dict, 'betweenness') sorted_betweenness = sorted(betweenness_dict.items(), reverse=True) print("Top 20 nodes by betweenness centrality:") for b in sorted_betweenness[:20]: # print top 20 nodes by betweenness centrality print(b) # Clustering Coefficient G=G.to_undirected() ccs=nx.clustering(G) # Clustering coefficient of all the nodes average_ccs=sum(ccs.values())/len(ccs) # Average Clustering coefficient print(average_ccs) # print the average clustering coefficient # Eigenvector Centrality G = nx.path_graph(10) centrality = nx.eigenvector_centrality(G) # calculate the Eigenvector Centrality print(['%s %0.2f'%(node,centrality[node]) for node in centrality])
def local_clustering_coefficient(G, nodes=[]): if len(nodes) > 0: return nx.clustering(G, nodes) return nx.clustering(G)
def properties_subgraphs(full_g, g, most_central_characters, mcc_by_chapter): """ :param full_g: entity graph :param g: list of chapter entity graphs :param most_central_characters: list of book's main characters :param mcc_by_chapter: book's main characters for each chapter Retrieves properties of the graphs """ print('\n-- COMPUTE PROPETIES OF ENTITY GRAPH FOR EACH CHAPTER --') # List of most important characters in novel and in each chapter most_important_entities = list(most_central_characters.keys()) mie_by_chapter = [[item[0] for item in sublist] for sublist in mcc_by_chapter] n_chap = len(mie_by_chapter) # Number of characters mentioned (nodes) and interactions (edges) # Info on varying number of entities and interactions nodes = [graph.number_of_nodes() for graph in g] edges = [graph.number_of_edges() for graph in g] # Number of characters among 10 most influential mentioned in each chap prop_important_entities_seen = \ [len(set(most_important_entities).intersection(mie_by_chapter[i])) / 10 for i in range(len(mie_by_chapter))] # Number of central characters in each chapter, wrt k-core full_k_core = list(nx.k_core(full_g)) k_core = [list(nx.k_core(graph)) for graph in g] # Proportion of full graph k core in each chapter k_core prop_k_core = [ len(set(k_core[i]).intersection(full_k_core)) / len(full_k_core) for i in range(len(k_core)) ] # Store when each entity is introduced and disappears dict_entities = {} # key = entity, value = [chap_intro, chap_last_seen] for entity in list(full_g.nodes()): dict_entities[entity] = [0, 0] introduced = [] for i, graph in enumerate(g): seen_entities = list(graph.nodes()) for ent in seen_entities: dict_entities[ent][1] = i + 1 if ent not in set(introduced): dict_entities[ent][0] = i + 1 introduced += seen_entities # Compute statistics existing_ent = [] introduced_ent = [] disappeared_ent = [] for j in range(1, n_chap + 1): exist = intro = disap = 0 for entity, val in dict_entities.items(): if val[0] == j: intro += 1 if val[1] == j - 1: disap += 1 if val[0] <= j and val[1] >= j: exist += 1 existing_ent.append(exist) # list of existing entities at each chapter introduced_ent.append( intro) # list of all introduced entities in each chapter disappeared_ent.append( disap) # list of all entities that disappear from next chapter # Distance between first and second most important characters dist_importance = [] for i in range(len(mie_by_chapter)): m = mcc_by_chapter[i][0][1] s = mcc_by_chapter[i][1][1] dist_importance.append((m - s) / m) # Find strongest edges for each chapter # Help us follow plot and study relations connexions = [] for graph in g: connexion = 0 weight = 0 for edge in graph.edges(data=True): if edge[2]['weight'] > weight: connexion = (edge[0], edge[1]) weight = edge[2]['weight'] connexions.append(connexion) # Av weighted degree graph weighted_degree = [] for graph in g: count = 0 for edge in graph.edges(data=True): count += edge[2]['weight'] weighted_degree.append(count / graph.number_of_edges()) # Isolated nodes number_isolated_nodes = [len(list(nx.isolates(graph))) for graph in g] # Cliques # Info about relation between characters and evolution interactions cliques = [list(nx.find_cliques(graph)) for graph in g] # list all cliques number_of_cliques = [len(item) for item in cliques] largest_clique = [nx.graph_clique_number(graph) for graph in g] max_cliques = [] for clique in cliques: length = 0 for element in clique: if len(element) > length: max_element = element length = len(element) max_cliques.append(max_element) # Visualise cliques (gephi) # Compute a measure of how similar cliques are cliques_sim = [] for i in range(len(max_cliques)): sim = 0 for j in range(len(max_cliques)): if i != j: sim += len(set(max_cliques[i]).intersection(max_cliques[j])) cliques_sim.append(sim / (len(max_cliques) - 1)) # Av number of similar characters accross clique clique_similarity = np.sum(cliques_sim) / len(max_cliques) #print('overall clique similarity', clique_similarity) # Clustering av_clustering_coef = [nx.average_clustering(graph) for graph in g] cc = [ sorted( nx.clustering(graph, nodes=most_important_entities, weight='weight')) for graph in g ] # Overlap with clustering and cliques (iou) inter_cc_cliques = [ set(max_cliques[i]).intersection(cc[i]) for i in range(len(cc)) ] overlap = np.average([ len(set(max_cliques[i]).intersection(cc[i])) / len(set(max_cliques[i]).union(cc[i])) for i in range(len(cc)) ]) for i, (chap_k_core, connexion, max_cliq, core_part) in enumerate( zip(k_core, connexions, max_cliques, inter_cc_cliques)): print("\nFor chapter n°", i) print('k-core: ', chap_k_core) print('strongest edges', connexion) print('biggest clique', max_cliq) print('core part :', core_part) # PLOTS print('PLOTS entity chapter graph properties') # plot_prop([nodes, edges], ['nodes', 'edges'], n_chap) plot_prop([ nodes, edges, number_of_cliques, existing_ent, introduced_ent, disappeared_ent ], [ 'entities', 'interactions', 'cliques', 'existing ent', 'introduced_ent', 'disappeared_ent' ], n_chap, num=1) plot_prop([ prop_k_core, prop_important_entities_seen, dist_importance, av_clustering_coef ], [ 'proportion core entities', 'proportion important entities', 'importance diff 1st/2nd character', 'clustering coef' ], n_chap, num=2) plot_prop( [weighted_degree, number_isolated_nodes, largest_clique, cliques_sim], [ 'graph weighted degree', 'number of isolated nodes', 'largest clique size', 'clique similarity' ], n_chap, num=3)
def solve(graph, num_buses, bus_size, constraints): #TODO: Write this method as you like. We'd recommend changing the arguments here as well print('start solve') num_kids = len(graph.nodes()) max_bus_size = min(bus_size, num_kids - num_buses + 1) for i in range(len(constraints)): constraints[i] = set(constraints[i]) #constraints = sorted(constraints, key=lambda c: -len(c)) def isrowdy(group): for constraint in constraints: if constraint.issubset(group): return constraint return None clusters = [] copy = graph.copy() while copy.nodes(): clustering = nx.clustering(copy) start = max([entry for entry in clustering.items()], key=lambda e: e[1])[0] print('\tinitial node: ' + start, end='') cluster = set([start]) fringe = set(copy.neighbors(start)) fgraph = nx.Graph() fgraph.add_node(start) for node in fringe: fgraph.add_node(node) for neighbor in copy.neighbors(node): if neighbor in cluster: fgraph.add_edge(node, neighbor) while fringe and len(cluster) < max_bus_size: degrees = fgraph.degree() candidates = sorted( fringe, key=lambda node: -(degrees[node] + clustering[node])) index = -1 for i in range(len(candidates)): if not isrowdy(cluster.union(set([candidates[i]]))): index = i break if index == -1: break candidate = candidates[index] fringe.remove(candidate) cluster.add(candidate) for neighbor in copy.neighbors(candidate): if neighbor in fringe: fgraph.add_edge(candidate, neighbor) elif neighbor not in cluster: fringe.add(neighbor) fgraph.add_node(neighbor) for neighbor_neighbor in copy.neighbors(neighbor): if neighbor_neighbor in cluster: fgraph.add_edge(neighbor, neighbor_neighbor) cgraph = nx.Graph() for node in cluster: cgraph.add_node(node) for neighbor in copy.neighbors(node): if neighbor in cluster: cgraph.add_edge(node, neighbor) while False: cdegrees = cgraph.degree() loneliest = min(cluster, key=lambda node: cdegrees[node]) fcopy = fgraph.copy() fcopy.remove_node(loneliest) fdegrees = fcopy.degree() friendly_list = sorted(fringe, key=lambda node: -fdegrees[node]) friendliest = None difference = cluster.difference(set([loneliest])) for node in friendly_list: if fdegrees[node] <= cdegrees[loneliest]: break if fdegrees[node] > cdegrees[loneliest] and not isrowdy( difference.union(set([node]))): friendliest = node break if not friendliest: break cgraph.remove_node(loneliest) fgraph.add_node(loneliest) fgraph.remove_node(friendliest) cgraph.add_node(friendliest) cluster.remove(loneliest) fringe.add(loneliest) fringe.remove(friendliest) cluster.add(friendliest) for neighbor in copy.neighbors(loneliest): if neighbor in fringe: fgraph.add_edge(loneliest, neighbor) elif neighbor not in cluster: fgraph.add_node(neighbor) for neighbor_neighbor in copy.neighbors(neighbor): if neighbor_neighbor in cluster: fgraph.add_edge(neighbor, neighbor_neighbor) for neighbor in copy.neighbors(friendliest): if neighbor in cluster: cgraph.add_edge(friendliest, neighbor) clusters.append(cluster) copy.remove_nodes_from(cluster) print('\tsize: ' + str(len(cluster))) clusters = sorted(clusters, key=lambda c: -len(c)) #print(clusters) buses = [] if len(clusters) < num_buses: while len(clusters) < num_buses: source = 0 while source + 1 < len(clusters) and len(clusters[source + 1]) > 1: source += 1 subgraph = graph.subgraph(clusters[source]) degrees = subgraph.degree() loneliest = min([node for node in clusters[source]], key=lambda node: degrees[node]) clusters[source].remove(loneliest) clusters.append(set([loneliest])) for cluster in clusters: buses.append(cluster) elif len(clusters) > num_buses: for i in range(num_buses - 1): buses.append(clusters[i]) buses.append(set()) for i in range(num_buses - 1, len(clusters)): added = False for j in range(num_buses): if len(buses[j]) + len( clusters[i]) <= bus_size and not isrowdy( buses[j].union(clusters[i])): buses[j].update(clusters[i]) added = True break if added: continue for j in range(num_buses - 1, -1, -1): if len(buses[j]) + len(clusters[i]) <= bus_size: buses[j].update(clusters[i]) added = True break if added: continue for j in range(num_buses - 1, -1, -1): while clusters[i] and len(buses[j]) < bus_size: buses[j].add(clusters[i].pop()) if not len(buses[-1]): source = 0 while source + 1 < len(buses) and len(buses[source + 1]) > 1: source += 1 subgraph = graph.subgraph(buses[source]) degrees = subgraph.degree() loneliest = min([node for node in buses[source]], key=lambda node: degrees[node]) buses[source].remove(loneliest) buses[-1].add(loneliest) else: for i in range(num_buses): buses.append(clusters[i]) print('end solve') print(buses) return [list(bus) for bus in buses]
def __init__(self, G_list, features='default', normalize=True, assign_feat='default', max_num_nodes=0): self.adj_all = [] self.len_all = [] self.feature_all = [] self.label_all = [] self.assign_feat_all = [] if max_num_nodes == 0: self.max_num_nodes = max([G.number_of_nodes() for G in G_list]) else: self.max_num_nodes = max_num_nodes #if features == 'default': self.feat_dim = util.node_dict(G_list[0])[0]['feat'].shape[0] for G in G_list: adj = np.array(nx.to_numpy_matrix(G)) if normalize: sqrt_deg = np.diag( 1.0 / np.sqrt(np.sum(adj, axis=0, dtype=float).squeeze())) adj = np.matmul(np.matmul(sqrt_deg, adj), sqrt_deg) self.adj_all.append(adj) self.len_all.append(G.number_of_nodes()) self.label_all.append(G.graph['label']) # feat matrix: max_num_nodes x feat_dim if features == 'default': f = np.zeros((self.max_num_nodes, self.feat_dim), dtype=float) for i, u in enumerate(G.nodes()): f[i, :] = util.node_dict(G)[u]['feat'] self.feature_all.append(f) elif features == 'id': self.feature_all.append(np.identity(self.max_num_nodes)) elif features == 'deg-num': degs = np.sum(np.array(adj), 1) degs = np.expand_dims(np.pad( degs, [0, self.max_num_nodes - G.number_of_nodes()], 0), axis=1) self.feature_all.append(degs) elif features == 'deg': self.max_deg = 10 degs = np.sum(np.array(adj), 1).astype(int) degs[degs > max_deg] = max_deg feat = np.zeros((len(degs), self.max_deg + 1)) feat[np.arange(len(degs)), degs] = 1 feat = np.pad(feat, ((0, self.max_num_nodes - G.number_of_nodes()), (0, 0)), 'constant', constant_values=0) f = np.zeros((self.max_num_nodes, self.feat_dim), dtype=float) for i, u in enumerate(util.node_iter(G)): f[i, :] = util.node_dict(G)[u]['feat'] feat = np.concatenate((feat, f), axis=1) self.feature_all.append(feat) elif features == 'struct': self.max_deg = 10 degs = np.sum(np.array(adj), 1).astype(int) degs[degs > 10] = 10 feat = np.zeros((len(degs), self.max_deg + 1)) feat[np.arange(len(degs)), degs] = 1 degs = np.pad(feat, ((0, self.max_num_nodes - G.number_of_nodes()), (0, 0)), 'constant', constant_values=0) clusterings = np.array(list(nx.clustering(G).values())) clusterings = np.expand_dims(np.pad( clusterings, [0, self.max_num_nodes - G.number_of_nodes()], 'constant'), axis=1) g_feat = np.hstack([degs, clusterings]) if 'feat' in util.node_dict(G)[0]: node_feats = np.array([ util.node_dict(G)[i]['feat'] for i in range(G.number_of_nodes()) ]) node_feats = np.pad( node_feats, ((0, self.max_num_nodes - G.number_of_nodes()), (0, 0)), 'constant') g_feat = np.hstack([g_feat, node_feats]) self.feature_all.append(g_feat) if assign_feat == 'id': self.assign_feat_all.append( np.hstack((np.identity(self.max_num_nodes), self.feature_all[-1]))) else: self.assign_feat_all.append(self.feature_all[-1]) self.feat_dim = self.feature_all[0].shape[1] self.assign_feat_dim = self.assign_feat_all[0].shape[1]
G.number_of_nodes() G.number_of_edges() G.nodes() G.edges() G.neighbors(1) # Grau nx.degree(G) # Verificar um vértice e suas relações a = nx.complete_graph(5) nx.draw(a, width=1, font_size=16, with_labels=True, alpha=0.4) # Agrupamentos nx.clustering(G) from sklearn.cluster import SpectralClustering sc = SpectralClustering(20, affinity='precomputed', n_init=100) sc.fit(matrix_adj) print(sc.labels_) np.savetxt('text.txt', mat, fmt='%.2f') # Isomorfismo G5 = nx.complete_graph(5) G10 = nx.complete_graph(10) nx.draw(G5, width=1, font_size=16, with_labels=True, alpha=0.4) nx.draw(G10, width=1, font_size=16, with_labels=True,
# -*- coding: utf-8 -*- """ Created on Wed Nov 13 11:41:06 2019 @author: Asif """ from numpy import genfromtxt import numpy as np import networkx as nx import pandas as pd mydata = genfromtxt('2d-Graph.csv', delimiter=',') # print(mydata) # print(type(mydata)) adjacency = mydata[1:,:] # print(adjacency) # adjacency = mydata[1:7,:6] G = nx.DiGraph(adjacency) # nx.draw(G) nx.clustering(G,weight="weight") nx.average_clustering(G,weight="weight") r=nx.degree_assortativity_coefficient(G,weight="weight") print("%3.1f"%r)
G = nx.DiGraph() G_init = nx.DiGraph() weights = V.numpy() weights_init = V_init.numpy() edges = torch.transpose(indices, 1, 0).numpy() cnt = 0 for edge in edges: G.add_nodes_from(edge) G_init.add_nodes_from(edge) G.add_edge(edge[0], edge[1], weight=weights[cnt]) G_init.add_edge(edge[0], edge[1], weight=weights_init[cnt]) cnt += 1 #print (weights[:50]) #print (nx.clustering(G)) weights_init = [G_init[u][v]['weight'] for u, v in G_init.edges()] nx.clustering(G_init) nx.draw(G_init, width=weights_init) #plt.show() weights = [G[u][v]['weight'] for u, v in G.edges()] nx.clustering(G) nx.draw(G, width=weights_init) #plt.show() ###Check few rows### torch.set_printoptions(sci_mode=False) if rand: print('============initial weights============') #values row 0 print('Row 0:\n', V_init[torch.where(indices[0] == 0)[0]]) #values row 25 print('Row 25:\n', V_init[torch.where(indices[0] == 25)[0]])
'Instance': k, 'Target Node': t, 'Time': execution_t1, 'Objective Value': flow_value }) df1 = df1.append(row) #clustering coefficient st2 = time.time() for c in range(30): for clu in range(100): clustering_coefficient = nx.clustering(G) e2 = time.time() execution_t2 = e2 - st2 row = pd.DataFrame({ 'Structural_Characteristic': ['Clustering Coefficient'], 'Instance': k, 'Target Node': t, 'Time': execution_t2, 'Objective Value': flow_value }) df2 = df2.append(row)
power_law_fits['brain'] = power_law_fit_brain[0] fits_r_squared['brain'] = power_law_fit_brain[2]**2 ############################################################################## # plot clustering vs. degree and nodal_efficiencies for brain and three models ############################################################################## fig, axs = plt.subplots(1, 2, facecolor=FACE_COLOR, figsize=FIG_SIZE, tight_layout=True) for a_ctr, ax in enumerate(axs): # brain if a_ctr == 0: cc = nx.clustering(G_brain.to_undirected()).values() deg = nx.degree(G_brain.to_undirected()).values() ax.scatter(deg, cc, s=SCATTER_SIZE, lw=0, alpha=ALPHA_DEG_VS_CC, c=COLORS['brain'], zorder=1000) elif a_ctr == 1: hist_connectome = ax.hist(G_brain.nodal_efficiency, bins=BINS_NODAL_EFFICIENCY, color=COLORS['brain']) # other graphs
def extended_stats(G, connectivity=False, anc=False, ecc=False, bc=False, cc=False): """ Calculate extended topological stats and metrics for a graph. Many of these algorithms have an inherently high time complexity. Global topological analysis of large complex networks is extremely time consuming and may exhaust computer memory. Consider using function arguments to not run metrics that require computation of a full matrix of paths if they will not be needed. Parameters ---------- G : networkx.MultiDiGraph input graph connectivity : bool if True, calculate node and edge connectivity anc : bool if True, calculate average node connectivity ecc : bool if True, calculate shortest paths, eccentricity, and topological metrics that use eccentricity bc : bool if True, calculate node betweenness centrality cc : bool if True, calculate node closeness centrality Returns ------- stats : dict dictionary of network measures containing the following elements (some only calculated/returned optionally, based on passed parameters): - avg_neighbor_degree - avg_neighbor_degree_avg - avg_weighted_neighbor_degree - avg_weighted_neighbor_degree_avg - degree_centrality - degree_centrality_avg - clustering_coefficient - clustering_coefficient_avg - clustering_coefficient_weighted - clustering_coefficient_weighted_avg - pagerank - pagerank_max_node - pagerank_max - pagerank_min_node - pagerank_min - node_connectivity - node_connectivity_avg - edge_connectivity - eccentricity - diameter - radius - center - periphery - closeness_centrality - closeness_centrality_avg - betweenness_centrality - betweenness_centrality_avg """ stats = dict() # create DiGraph from the MultiDiGraph, for those metrics that need it D = utils_graph.get_digraph(G, weight="length") # create undirected Graph from the DiGraph, for those metrics that need it Gu = nx.Graph(D) # get largest strongly connected component, for those metrics that require # strongly connected graphs Gs = utils_graph.get_largest_component(G, strongly=True) # average degree of the neighborhood of each node, and average for the graph avg_neighbor_degree = nx.average_neighbor_degree(G) stats["avg_neighbor_degree"] = avg_neighbor_degree stats["avg_neighbor_degree_avg"] = sum( avg_neighbor_degree.values()) / len(avg_neighbor_degree) # avg weighted degree of neighborhood of each node, and average for graph avg_wtd_nbr_deg = nx.average_neighbor_degree(G, weight="length") stats["avg_weighted_neighbor_degree"] = avg_wtd_nbr_deg stats["avg_weighted_neighbor_degree_avg"] = sum( avg_wtd_nbr_deg.values()) / len(avg_wtd_nbr_deg) # degree centrality for a node is the fraction of nodes it is connected to degree_centrality = nx.degree_centrality(G) stats["degree_centrality"] = degree_centrality stats["degree_centrality_avg"] = sum( degree_centrality.values()) / len(degree_centrality) # calculate clustering coefficient for the nodes stats["clustering_coefficient"] = nx.clustering(Gu) # average clustering coefficient for the graph stats["clustering_coefficient_avg"] = nx.average_clustering(Gu) # calculate weighted clustering coefficient for the nodes stats["clustering_coefficient_weighted"] = nx.clustering(Gu, weight="length") # average clustering coefficient (weighted) for the graph stats["clustering_coefficient_weighted_avg"] = nx.average_clustering( Gu, weight="length") # pagerank: a ranking of the nodes in the graph based on the structure of # the incoming links pagerank = nx.pagerank(D, weight="length") stats["pagerank"] = pagerank # node with the highest page rank, and its value pagerank_max_node = max(pagerank, key=lambda x: pagerank[x]) stats["pagerank_max_node"] = pagerank_max_node stats["pagerank_max"] = pagerank[pagerank_max_node] # node with the lowest page rank, and its value pagerank_min_node = min(pagerank, key=lambda x: pagerank[x]) stats["pagerank_min_node"] = pagerank_min_node stats["pagerank_min"] = pagerank[pagerank_min_node] # if True, calculate node and edge connectivity if connectivity: # node connectivity is the minimum number of nodes that must be removed # to disconnect G or render it trivial stats["node_connectivity"] = nx.node_connectivity(Gs) # edge connectivity is equal to the minimum number of edges that must be # removed to disconnect G or render it trivial stats["edge_connectivity"] = nx.edge_connectivity(Gs) utils.log("Calculated node and edge connectivity") # if True, calculate average node connectivity if anc: # mean number of internally node-disjoint paths between each pair of # nodes in G, i.e., the expected number of nodes that must be removed to # disconnect a randomly selected pair of non-adjacent nodes stats["node_connectivity_avg"] = nx.average_node_connectivity(G) utils.log("Calculated average node connectivity") # if True, calculate shortest paths, eccentricity, and topological metrics # that use eccentricity if ecc: # precompute shortest paths between all nodes for eccentricity-based # stats sp = { source: dict( nx.single_source_dijkstra_path_length(Gs, source, weight="length")) for source in Gs.nodes() } utils.log("Calculated shortest path lengths") # eccentricity of a node v is the maximum distance from v to all other # nodes in G eccentricity = nx.eccentricity(Gs, sp=sp) stats["eccentricity"] = eccentricity # diameter is the maximum eccentricity diameter = nx.diameter(Gs, e=eccentricity) stats["diameter"] = diameter # radius is the minimum eccentricity radius = nx.radius(Gs, e=eccentricity) stats["radius"] = radius # center is the set of nodes with eccentricity equal to radius center = nx.center(Gs, e=eccentricity) stats["center"] = center # periphery is the set of nodes with eccentricity equal to the diameter periphery = nx.periphery(Gs, e=eccentricity) stats["periphery"] = periphery # if True, calculate node closeness centrality if cc: # closeness centrality of a node is the reciprocal of the sum of the # shortest path distances from u to all other nodes closeness_centrality = nx.closeness_centrality(G, distance="length") stats["closeness_centrality"] = closeness_centrality stats["closeness_centrality_avg"] = sum( closeness_centrality.values()) / len(closeness_centrality) utils.log("Calculated closeness centrality") # if True, calculate node betweenness centrality if bc: # betweenness centrality of a node is the sum of the fraction of # all-pairs shortest paths that pass through node # networkx 2.4+ implementation cannot run on Multi(Di)Graphs, so use DiGraph betweenness_centrality = nx.betweenness_centrality(D, weight="length") stats["betweenness_centrality"] = betweenness_centrality stats["betweenness_centrality_avg"] = sum( betweenness_centrality.values()) / len(betweenness_centrality) utils.log("Calculated betweenness centrality") utils.log("Calculated extended stats") return stats
# Local Clustering Coefficient # Fraction of pairs of the node’s friends that are friends with each other. # LCC = (no. of pairs of node's friends who are friends(A)) / (no. of pairs of node's friend(B)) # B = n(n-1)/2 A = np.zeros([50, 50]) for i in range(50): for j in range(50): A[i, j] = np.round(np.random.random()) G5 = nx.from_numpy_matrix(A) nx.draw(G5, with_labels=True) plt.show() nx.clustering(G5, 1) # Global Clustering Coefficient # Simple Average nx.average_clustering(G5) # Transtivity # Ratio of number of triangles and number of “open triads” in a network. nx.transitivity(G5) G6 = nx.complete_graph(4) nx.draw(G6) plt.show() nx.transitivity(G6)
def extract_network_metrics(mdg, ts, team=True): met = {} dsg = extract_dpsg(mdg, ts, team) if team: pre = 'full:' else: pre = 'user:'******'nodes_count'] = dsg.number_of_nodes() met[pre + 'edges_count'] = dsg.number_of_edges() met[pre + 'density'] = nx.density(dsg) met[pre + 'betweenness'] = nx.betweenness_centrality(dsg) met[pre + 'avg_betweenness'] = float(sum( met[pre + 'betweenness'].values())) / float( len(met[pre + 'betweenness'].values())) met[pre + 'betweenness_count'] = nx.betweenness_centrality(dsg, weight='count') met[pre + 'avg_betweenness_count'] = float( sum(met[pre + 'betweenness_count'].values())) / float( len(met[pre + 'betweenness_count'].values())) met[pre + 'betweenness_effort'] = nx.betweenness_centrality( dsg, weight='effort') met[pre + 'avg_betweenness_effort'] = float( sum(met[pre + 'betweenness_effort'].values())) / float( len(met[pre + 'betweenness_effort'].values())) met[pre + 'in_degree'] = dsg.in_degree() met[pre + 'avg_in_degree'] = float(sum(met[pre + 'in_degree'].values())) / float( len(met[pre + 'in_degree'].values())) met[pre + 'out_degree'] = dsg.out_degree() met[pre + 'avg_out_degree'] = float(sum( met[pre + 'out_degree'].values())) / float( len(met[pre + 'out_degree'].values())) met[pre + 'degree'] = dsg.degree() met[pre + 'avg_degree'] = float(sum(met[pre + 'degree'].values())) / float( len(met[pre + 'degree'].values())) met[pre + 'degree_count'] = dsg.degree(weight='count') met[pre + 'avg_degree_count'] = float( sum(met[pre + 'degree_count'].values())) / float( len(met[pre + 'degree_count'].values())) met[pre + 'degree_effort'] = dsg.degree(weight='effort') met[pre + 'avg_degree_effort'] = float( sum(met[pre + 'degree_effort'].values())) / float( len(met[pre + 'degree_effort'].values())) met[pre + 'pagerank'] = nx.pagerank(dsg, weight='count') # added by Alberto usg = dsg.to_undirected() met[pre + 'clustering'] = nx.clustering(usg) # added by Alberto louvain = extract_louvain_modularity(usg) met[pre + 'partitions'] = louvain['partitions'] met[pre + 'louvain_modularity'] = louvain['modularity'] connected_components = nx.connected_component_subgraphs(usg) shortest_paths = [ nx.average_shortest_path_length(g) for g in connected_components if g.size() > 1 ] if len(shortest_paths) > 0: met[pre + 'avg_distance'] = max(shortest_paths) else: met[pre + 'avg_distance'] = None return met
B = {} B = l_Balance(G_init) #print B ##************************计算邻域鲁棒性以被等价替代******************************************************* # G_R = {} # G_init_Robustness={} # #for (x,y) in flights: make_link(G,x,y) # for (x,y) in G_init.edges(): make_link(G_R,x,y) # #print G_R # for v in G_R.keys(): # clustering_coefficient(G_R,v), " "+v # G_init_Robustness[v]=clustering_coefficient(G_R,v) # #clustering_coefficient(G_R,v), " "+v # #print G_init_Robustness G_c = nx.clustering(G_init) S = l_Connection_strength(G_init) #print S time_newci_start = time.time() newci, newci91, newci19, newci_b, newci_s = New_Collective_Influence(G_init) time_newci = time.time() - time_newci_start #print "newci" #print time_newci #print newci newCI_sort = sorted(newci.items(), key=operator.itemgetter(1), reverse=True) #print newCI_sort res_list_NewCI = [x for x, _ in newCI_sort] #print res_list_NewCI # file.write("time_newci"+str(time_newci)+'\n') # file.write(str(res_list_NewCI)+'\n')
components = nx.connected_component_subgraphs(g) print "There are ", len(components), "component" for component in components: print "length:", len(component) # <markdowncell> # Plotting Ego Graph of a person. # <codecell> name = "Abhinav Pandey" ego = nx.ego_graph(g, name, radius=1) nx.draw(ego) plot.show() print "Clustering:", nx.clustering(g, name) # <markdowncell> # Calculating Cliques in Graph. These are closed faternity like terrorist organization. # <codecell> clique = nx.find_cliques(g) clique = list(clique) sorted_clique = sorted(clique, key=lambda x: len(x)) sorted_clique[-1] # <codecell> # <codecell>
def test_clustering(self): G = nx.Graph() assert_equal(list(nx.clustering(G, weight='weight').values()), []) assert_equal(nx.clustering(G), {})
## ## ## # Bridging centrality (C_Bdg) measures the extent to which a node or an edge is located between well-connected regions # C_Bdg(i) = C_Btw(i) x BC(i) # # where BC(i) is the bridging coefficient that assesses the local # bridging characteristics in the neighborhood of node i, which is defined as # # BC(i) = degree(i)^-1 / SUM_{v in ngbd(i)} [1 / degree(v)] ## ## ## print 'Betweenness...' betweenness_dic = nx.betweenness_centrality(G) print 'Clustering...' clustering_dic = nx.clustering(G) # Getting maximum degree print 'Degree...' degree_dic = G.degree() max_degree = int(max(degree_dic.iteritems(), key=operator.itemgetter(1))[1]) def BC(G, i): SUM = 0 for neighbor in G.neighbors(i): SUM += (1. / G.degree(neighbor)) #print neighbor, G.degree(neighbor), '- new sum:', SUM return 1 / (G.degree(i) * SUM)
def test_triangle_and_edge(self): G = nx.Graph() G.add_cycle([0, 1, 2]) G.add_edge(0, 4, weight=2) assert_equal(nx.clustering(G)[0], 1.0 / 3.0) assert_equal(nx.clustering(G, weight='weight')[0], 1.0 / 6.0)
noeuds_a_garder.append(nv) compt += 1 for noeud in noeuds_a_garder: G.remove_node(noeud) for noeud in list(G.nodes()): G2.remove_node(noeud) #////////////////////////////////////////////////////////////////////////////////////cetait le nettoyage du graphe # ici l'on va insérer nos calculs dans un fichier csv c = csv.writer( open("C:/Users/Tiffany/Desktop/CodeProgrammation/Resultat_Fonction.csv", "w")) c.writerow([ "ID", "X", "Y", "DEGREE", "clustering_coefficient", "closeness_centrality" ]) noeuds = list(nx.nodes(G2)) k = 1 superliste = [] for l in noeuds: superliste.append([ k, l[0], l[1], nx.degree(G2, (l[0], l[1])), nx.clustering(G2, (l[0], l[1])), nx.closeness_centrality(G2, (l[0], l[1])) ]) k = k + 1 c.writerows(superliste) print("fini")
def find_from_uid_list(xnr_user_no, nodes=None, path=PATH, file_name=FILE_NAME, com_type='copra', G=None): #得到用户集 print 'get users...', type(G) if not G: G = get_users(xnr_user_no, nodes) else: G = G.subgraph(nodes, xnr_user_no) node_clus = nx.clustering(G) #50w print 'number of users:', len(node_clus) nodes = [i for i in node_clus if node_clus[i] > 0] #7w print 'node clustering > 0:', len(nodes) allG = G print 'allg', allG.number_of_nodes() #根据聚集系数大于0筛选用户 G = G.subgraph(nodes) try: G.remove_node('') except: pass # G.remove_nodes_from(list(set(node_clus)-set(nodes))) #80w边 # G.remove_node('') print 'number of edges:', G.number_of_edges( ), ' number of nodes:', G.number_of_nodes() degree_dict = nx.degree_centrality(G) print 'find coms using ', com_type start = time.time() #选择采用的划分方法 if com_type in ['oslom', 'slpa']: #存到文件里,调包用 f = open(path + file_name, 'w') count = 0 for s, r in G.edges(): if s and r: f.write(s + ' ' + r + ' ' + str(G[s][r]['weight']) + '\n') count += 1 if count % 100 == 0: print count f.close() print('total nodes count:', len(nodes), ' total edges count:', count) if com_type == 'oslom': coms = oslom_coms(path, file_name) else: coms = slpa_coms(path, file_name) coms_list = coms.values() else: #传边 file_path = './facebook_data/' + ts2datetime(int( time.time())) + '_' + str(int(time.time())) coms_list = find_community(degree_dict, G, file_path) print 'find community time:', time.time() - start print 'post process...' coms_list = post_process(allG, coms_list) return G, allG, coms_list