社会网络的凝聚子群分析
import networkx as nx
import matplotlib.pyplot as plt
#建立网络图
G=nx.Graph()
G.add_edges_from([(1,2),(1,3),(1,4),(2,3),(2,4),(2,5),(3,4),(3,5),(4,6)])
pos = nx.kamada_kawai_layout(G)
nx.draw(G,with_labels=True,font_size=10,node_size=1000,pos=pos,node_color='gray')
plt.show()
#找出网络图中的各大派系
c1=list(nx.find_cliques(G))
print(c1)
#绘制各大派系
nx.draw(G,with_labels=True,font_size=10,node_size=1000,pos=pos,node_color='gray')
nx.draw(G,pos = pos, nodelist = c1[0], node_color = 'y',font_size=20,node_size=2000)
plt.show()
nx.draw(G,with_labels=True,font_size=10,node_size=1000,pos=pos,node_color='gray')
nx.draw(G,pos = pos, nodelist = c1[1], node_color = 'g',font_size=20,node_size=2000)
plt.show()
nx.draw(G,with_labels=True,font_size=10,node_size=1000,pos=pos,node_color='gray')
nx.draw(G,pos = pos, nodelist = c1[2], node_color = 'b',font_size=20,node_size=2000)
plt.show()
#绘制网络图
nx.draw(G,with_labels=True,font_size=20,node_size=1500,pos=nx.kamada_kawai_layout(G))
plt.show()
#k核为3的凝聚子群
kc1=nx.k_core(G,k=3)
nx.draw(kc1,with_labels=True,font_size=20,node_size=2000)
plt.show()
#求每个节点的核心度
d1=nx.core_number(G)#
#按核心度排序
sd1=sorted(d1.items(),key = lambda x:x[1],reverse = True)
print(sd1)