def test_cycles(self): K_undir = approx.all_pairs_node_connectivity(self.cycle) for source in K_undir: for target, k in K_undir[source].items(): assert_true(k == 2) K_dir = approx.all_pairs_node_connectivity(self.directed_cycle) for source in K_dir: for target, k in K_dir[source].items(): assert_true(k == 1)
def test_paths(self): K_undir = approx.all_pairs_node_connectivity(self.path) for source in K_undir: for target, k in K_undir[source].items(): assert_true(k == 1) K_dir = approx.all_pairs_node_connectivity(self.directed_path) for source in K_dir: for target, k in K_dir[source].items(): if source < target: assert_true(k == 1) else: assert_true(k == 0)
def test_cutoff(self): for G in [self.K10, self.K5, self.K20]: for mp in [2, 3, 4]: paths = approx.all_pairs_node_connectivity(G, cutoff=mp) for source in paths: for target, K in paths[source].items(): assert_true(K == mp)
def measure_connectivity(G,grouping = None): overall_conn = approx.node_connectivity(G) print('Minimum number of nodes that must be removed to disconnect studets: '+str(overall_conn)) pairwise_conn = approx.all_pairs_node_connectivity(G) plt.title('Distribution of Min Removed Nodes to Disconnect Pair') plt.hist(pairwise_conn.values()) avg_cluster = approx.average_clustering(G) print('Mean of the fraction of triangles that actually exist over all possible triangles in each neighborhood: '+str(avg_cluster))
def measure_between_group_connectivity(induced, name = '', plots = False): overall_conn = approx.node_connectivity(induced) print('Minimum number of nodes that must be removed to disconnect studets: '+str(overall_conn)) pairwise_conn = approx.all_pairs_node_connectivity(induced) avg_cluster = approx.average_clustering(induced) print('Mean of the fraction of triangles that actually exist over all possible triangles in each neighborhood: '+str(avg_cluster)) avg_cluster = approx.average_clustering(induced) print('Mean of the fraction of triangles that actually exist over all possible triangles in each neighborhood: '+str(avg_cluster)) if plots: p.pairwise_conn_dist(pairwise_conn, name)
def measure_connectivity(G, out, grouping=None): # overall_conn = approx.node_connectivity(G) # print('Minimum number of nodes that must be removed to disconnect studets: '+str(overall_conn)) pairwise_conn = approx.all_pairs_node_connectivity(G) plt.title('Distribution of Min Removed Nodes to Disconnect Pair') connlist = [] for subdict in pairwise_conn.values(): avgconn = sum(list(subdict.values())) / len(subdict.values()) connlist.append(avgconn) temp = pd.DataFrame(connlist, columns=['pairwise_conn']) temp = temp.reset_index() temp["rank"] = temp['pairwise_conn'].rank(method='average', ascending=False) temp = temp.sort_values(by=['index'], ascending=False) plt.scatter(y=temp['pairwise_conn'], x=temp['rank'], alpha=0.7) plt.xlabel('Rank') plt.ylabel('Count') plt.savefig(out + 'zipf_pairwiseconnrank.png') plt.close() avg_cluster = approx.average_clustering(G) print( 'Mean of the fraction of triangles that actually exist over all possible triangles in each neighborhood: ' + str(avg_cluster))
def test_all_pairs_connectivity_nbunch(self): G = nx.complete_graph(5) nbunch = [0, 2, 3] C = approx.all_pairs_node_connectivity(G, nbunch=nbunch) assert_equal(len(C), len(nbunch))
def test_complete(self): for G in [self.K10, self.K5, self.K20]: K = approx.all_pairs_node_connectivity(G) for source in K: for target, k in K[source].items(): assert_true(k == len(G)-1)
def test_complete(self): for G in [self.K10, self.K5, self.K20]: K = approx.all_pairs_node_connectivity(G) for source in K: for target, k in K[source].items(): assert_true(k == len(G) - 1)
def projection_plot(l, partition, name): plt.figure(figsize=(14, 16)) plt.axis('off') pos = nx.spring_layout(l, k=0.15) nx.draw_networkx(l, node_size=4, with_labels=False, node_color='blue', edge_color='black', width=0.8, pos=pos, alpha=0.7) # pos = nx.spring_layout(l, k = 2.5) # nx.draw_networkx(l,node_size = 3500, with_labels = True, # node_color = 'peachpuff', edge_color = 'black', # width = 2, pos = pos, font_size = 48, alpha = 0.7) # ax = plt.gca() # to get the current axis # ax.collections[0].set_edgecolor("#000000") plt.savefig(name + '_projection.png') plt.close() plt.figure(figsize=(14, 16)) plt.axis('off') c = cp._color_nodes(l, partition, 0) s = cp._size_nodes(l, partition, 2) nx.draw_networkx(l, node_size=s, with_labels=False, node_color=c, edge_color='black', width=0.1, pos=pos, alpha=0.7) # ax = plt.gca() # to get the current axis # ax.collections[0].set_edgecolor("#000000") plt.savefig(name + '_communities_projection.png') plt.close() plt.figure(figsize=(14, 16)) plt.axis('off') pairwise_conn = approx.all_pairs_node_connectivity(l) avg_conn_dict = pss.get_avg_conn_from_pairwise(pairwise_conn) nx.set_node_attributes(l, avg_conn_dict, 'avg_connectivity') cmap = plt.get_cmap('Purples') cmap = truncate_colormap(cmap, 0.2, 0.9) groups = set(nx.get_node_attributes(l, 'avg_connectivity').values()) mapping = dict(zip(sorted(groups), count())) nodes = l.nodes() colors = [mapping[l.node[n]['avg_connectivity']] for n in nodes] nc = nx.draw_networkx(l, pos=pos, nodelist=nodes, node_color=colors, with_labels=False, node_size=4, cmap=cmap, alpha=0.7, edge_color='black', width=0.1) # nc = nx.draw_networkx(l, pos = pos, nodelist=nodes, node_color=colors, # with_labels=True, node_size=3500, cmap=cmap, # alpha = 0.7, edge_color = 'black', width = 2, font_size = 48) # plt.colorbar(nc) # ax = plt.gca() # to get the current axis # ax.collections[0].set_edgecolor("#000000") plt.savefig(name + '_connectivity_projection.png') plt.close()
# -*- coding: utf-8 -*- import networkx as nx from networkx.algorithms import approximation G = nx.Graph() G.add_edges_from([(1, 2), (2, 3), (3, 5), (1, 5), (5, 6), (7, 8), (3, 7)]) # 两个不同节点和非相邻节点之间的成对或本地节点连接是断开它们时必须删除的节点的最小数目(最小分离切割集) res = approximation.all_pairs_node_connectivity(G) print res