(社会舆情) 小世界网络,规则网络,随机网络
networkx
import networkx as ne #导入建网络模型包,命名ne
import matplotlib.pyplot as plt #导入科学绘图包,命名plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(15,12))
plt.subplot(2,2,1)
ws=ne.watts_strogatz_graph(30,5,0)
ps=ne.circular_layout(ws)#布置框架
ne.draw(ws,ps,with_labels=False,node_size=30)
plt.title("规则网络(节点数=30,邻接个数=5,p=0)")
d1=ne.average_shortest_path_length(ws)
print("平均最短路径为(p=0):%lf"%d1)
plt.subplot(2,2,2)
ws=ne.watts_strogatz_graph(30,5,0.3)
ps=ne.circular_layout(ws)#布置框架
ne.draw(ws,ps,with_labels=False,node_size=30)
plt.title("小世界网络(节点数=30,邻接个数=5,p=0.3)")
d2=ne.average_shortest_path_length(ws)
print("平均最短路径为(p=0.3):%lf"%d2)
plt.subplot(2,2,3)
ws=ne.watts_strogatz_graph(30,5,0.6)
ps=ne.circular_layout(ws)#布置框架
ne.draw(ws,ps,with_labels=False,node_size=30)
plt.title("小世界网络(节点数=30,邻接个数=5,p=0.6)")
d3=ne.average_shortest_path_length(ws)
print("平均最短路径为(p=0.6):%lf"%d3)
plt.subplot(2,2,4)
ws=ne.watts_strogatz_graph(30,5,1)
ps=ne.circular_layout(ws)#布置框架
ne.draw(ws,ps,with_labels=False,node_size=30)
plt.title("随机网络(节点数=30,邻接个数=5,p=1)")
d4=ne.average_shortest_path_length(ws)
print("平均最短路径为(p=1):%lf"%d4)
plt.show()