python - Barabasi-Albert模型的度分布
问题描述
我已经能够运行这个:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
n = 20
m = 3
G_barabasi = nx.barabasi_albert_graph(n,m)
plt.figure(figsize=(12,8))
nx.draw(G_barabasi, node_size=4)
plt.show()
上面的代码能够绘制节点和边。
但是,我需要获得 Barabasi-Albert 模型的分布度,或者更确切地说是幂律度分布。
解决方案
我们可以使用nx.degree_histogram
,它返回网络中度数的频率列表,其中度数是列表中的相应索引。
x
通常,在绘制度分布时会采用和轴的对数y
,这有助于了解网络 x 是否是无标度的(度分布遵循幂律的网络),这与Barabási-Albert 模型一样,我们可以使用plt.loglog
为了那个原因:
import networkx as nx
import matplotlib.pyplot as plt
n = 2000
m = 3
G_barabasi = nx.barabasi_albert_graph(n,m)
degree_freq = nx.degree_histogram(G_barabasi)
degrees = range(len(degree_freq))
plt.figure(figsize=(12, 8))
plt.loglog(degrees[m:], degree_freq[m:],'go-')
plt.xlabel('Degree')
plt.ylabel('Frequency')