刻画编辑社区
现在,您将把之前学到的 BFS 算法与极大团的概念结合起来,用 Arc plot 可视化这一网络。
Github 用户协作网络中的最大极大团已被赋给子图 G_lmc。请注意,在 NetworkX 2.x 及更高版本中,G.subgraph(nodelist) 只会返回原图的不可变视图。若要获得可变版本,必须显式请求 .copy()。
本练习是课程的一部分
Python 网络分析入门
练习说明
- 从该团向外扩展 1 度,并将这些用户添加到子图中。在第一个
for循环内:- 使用
.add_nodes_from()和.neighbors()方法,从G的邻居中向G_lmc添加节点。 - 使用
.add_edges_from()方法,在当前节点与其所有邻居之间向G_lmc添加边。为此,您需要使用zip()函数创建一个由元组组成的列表,内容为当前节点与其每个邻居。zip()的第一个参数应为[node]*len(list(G.neighbors(node))),第二个参数应为该node的所有邻居。
- 使用
- 在每个节点的元数据中记录其度中心性分数。
- 在第二个
for循环中,将nx.degree_centrality(G_lmc)[n]赋给G_lmc.nodes[n]['degree centrality']。
- 在第二个
- 使用 Arc plot 可视化该网络,并按度中心性对节点排序(可通过关键字参数
sort_by='degree centrality'实现)。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import necessary modules
from nxviz import arc
import matplotlib.pyplot as plt
# Identify the largest maximal clique: largest_max_clique
largest_max_clique = set(sorted(nx.find_cliques(G), key=lambda x: len(x))[-1])
# Create a subgraph from the largest_max_clique: G_lmc
G_lmc = G.subgraph(largest_max_clique).copy()
# Go out 1 degree of separation
for node in list(G_lmc.nodes()):
G_lmc.add_nodes_from(____)
G_lmc.add_edges_from(zip(____, ____))
# Record each node's degree centrality score
for n in G_lmc.nodes():
____ = ____
# Create the Arc plot: a
a = ____
# Draw the Arc plot to the screen
a
plt.show()