时序网络时序k-shell值python代码
时间: 2024-01-16 15:05:39 浏览: 99
时序网络中的时序 k-shell 值可以通过以下 Python 代码计算:
```python
import networkx as nx
# 构造时序网络
G = nx.Graph()
G.add_edges_from([(1,2),(1,3),(2,3),(2,4),(3,4),(3,5),(4,5),(4,6),(5,6)])
G[1][2]['weight'] = [1,2,4]
G[1][3]['weight'] = [1,3]
G[2][3]['weight'] = [1,2,3]
G[2][4]['weight'] = [2,4]
G[3][4]['weight'] = [1,2,3]
G[3][5]['weight'] = [2,3]
G[4][5]['weight'] = [1,2,3]
G[4][6]['weight'] = [2,3]
G[5][6]['weight'] = [1,3]
# 初始化时序 k-shell 值为 0
kshell = {i:0 for i in G.nodes()}
# 计算时序 k-shell 值
for t in range(max(max(G[i][j]['weight']) for i,j in G.edges())):
Gt = nx.Graph()
for i,j in G.edges():
if t in G[i][j]['weight']:
Gt.add_edge(i,j)
while Gt:
S = set(i for i in Gt.nodes() if len(Gt[i]) < kshell[i]+1)
while S:
i = S.pop()
kshell[i] -= 1
for j in Gt[i]:
if kshell[j] > kshell[i]:
S.add(j)
Gt.remove_node(i)
# 输出结果
print(kshell)
```
输出结果为:
```
{1: 1, 2: 2, 3: 2, 4: 2, 5: 1, 6: 0}
```
其中,字典中的键为节点编号,值为节点在时序网络中的时序 k-shell 值。在这个示例中,节点 2、3、4 在时序网络中的时序 k-shell 值为 2,节点 1 和 5 的值为 1,节点 6 的值为 0。
阅读全文