编写ER(Erdős-Rényi)模型的代码,并分别生成具有1000个节点和10000个节点的两个网络,其中连接概率p=3lnN/N,N分别为节点总数。
时间: 2024-09-28 19:05:55 浏览: 41
在Python中,我们可以使用`networkx`库来创建Erdős-Rényi随机图模型。下面是一个简单的例子,展示了如何生成具有1000个节点和10000个节点的两个网络,连接概率设定为`3 * ln(N) / N`:
```python
import networkx as nx
import math
# 函数来计算连接概率
def connection_probability(n):
return 3 * math.log(n) / n
# 生成1000个节点的网络
N1 = 1000
p1 = connection_probability(N1)
G1 = nx.gnp_random_graph(N1, p1)
print(f"Generated ER graph with {N1} nodes and connection probability {p1}")
# 生成10000个节点的网络
N2 = 10000
p2 = connection_probability(N2)
G2 = nx.gnp_random_graph(N2, p2)
print(f"Generated ER graph with {N2} nodes and connection probability {p2}")
# 输出每个网络的一些基本信息
print("Degree distribution for the first graph:")
degree_dist1 = nx.degree_histogram(G1)
print(degree_dist1)
print("Degree distribution for the second graph:")
degree_dist2 = nx.degree_histogram(G2)
print(degree_dist2)
阅读全文