自我图#

使用NetworkX的ego_graph()函数返回Barabási-Albert网络中最大枢纽的主要自我网络的示例。

plot ego graph
from operator import itemgetter

import matplotlib.pyplot as plt
import networkx as nx

# 创建一个BA模型图 - 使用种子以确保可重复性
n = 1000
m = 2
seed = 20532
G = nx.barabasi_albert_graph(n, m, seed=seed)

# 找到度最大的节点
node_and_degree = G.degree()
(largest_hub, degree) = sorted(node_and_degree, key=itemgetter(1))[-1]

# 创建主枢纽的自我图
hub_ego = nx.ego_graph(G, largest_hub)

# 绘制图表
pos = nx.spring_layout(hub_ego, seed=seed)  # 设定布局种子以确保可重复性
nx.draw(hub_ego, pos, node_color="b", node_size=50, with_labels=False)

# 将自我描绘得又大又红
options = {"node_size": 300, "node_color": "r"}
nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], **options)
plt.show()

Total running time of the script: (0 minutes 0.039 seconds)

Gallery generated by Sphinx-Gallery