可以写一个用python生成的BA无标度网络模型吗
时间: 2024-05-08 21:15:34 浏览: 127
可以的,以下是一个用Python生成BA无标度网络模型的示例代码:
```python
import networkx as nx
import random
def generate_ba_network(num_nodes, m):
G = nx.Graph()
# Add m nodes to the graph to start
G.add_nodes_from(list(range(m)))
# Create a list of nodes to use for adding edges
node_list = list(range(m))
# Add edges preferentially to existing nodes
for i in range(m, num_nodes):
# Choose m nodes randomly from the existing nodes
targets = random.sample(node_list, m)
# Add edges to the new node
G.add_edges_from([(i, t) for t in targets])
# Add the new node to the list of nodes
node_list.append(i)
return G
```
上面的代码使用networkx库生成一个BA无标度网络模型。它接受两个参数:num_nodes是生成的节点数,m是每个新节点连接到的现有节点数。它返回一个networkx图对象。
阅读全文