自我中心网络Ego Graph
Author: Drew Conway,chen xiang yu (drew.conway@nyu.edu,cxy_0809@qq.com)
from operator import itemgetter
import matplotlib.pyplot as plt
import networkx as nx
if __name__ == '__main__':
# Create a BA model graph
n = 1000
m = 2
G = nx.generators.barabasi_albert_graph(n, m)
# find node with largest degree
node_and_degree = G.degree()
(largest_hub, degree) = sorted(node_and_degree, key=itemgetter(1))[-1]
# Create ego graph of main hub
hub_ego = nx.ego_graph(G, largest_hub)
# Draw graph
pos = nx.spring_layout(hub_ego)
nx.draw(hub_ego, pos, node_color='r', node_size=60, with_labels=True)
# Draw ego as large and red
nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], node_size=300, node_color='black')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210204084849153.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzM4MTI3NDg3,size_16,color_FFFFFF,t_70)
CSDN搜索:Python Newworkx实例。