复杂网络加权度的代码
时间: 2023-12-08 20:04:41 浏览: 340
复杂网络编程代码
以下是计算复杂网络加权度的 Python 代码:
```python
import networkx as nx
# 构建一个有向图
G = nx.DiGraph()
G.add_edges_from([(1, 2, {'weight': 0.5}), (1, 3, {'weight': 0.8}), (2, 3, {'weight': 0.3})])
# 计算每个节点的入度和出度权重之和
in_weight = dict(G.in_degree(weight='weight'))
out_weight = dict(G.out_degree(weight='weight'))
# 计算每个节点的加权度
weighted_degree = {}
for node in G.nodes():
weighted_degree[node] = in_weight.get(node, 0) + out_weight.get(node, 0)
print(weighted_degree)
```
输出结果为:
```
{1: 1.3, 2: 0.8, 3: 1.1}
```
其中,节点 1 的加权度为入度权重之和加上出度权重之和,即 0.5 + 0.8 = 1.3。
阅读全文