有向网络k-shell值python代码
时间: 2024-01-16 20:05:39 浏览: 25
有向网络的 k-shell 值计算方式与无向网络类似,可以通过以下 Python 代码计算:
```python
import networkx as nx
# 构造有向图
G = nx.DiGraph()
G.add_edges_from([(1,2),(1,3),(2,3),(2,4),(3,4),(3,5),(4,5),(4,6),(5,6)])
# 计算 k-shell 值
kshell = nx.core_number(G)
# 输出结果
print(kshell)
```
输出结果为:
```
{1: 2, 2: 2, 3: 2, 4: 2, 5: 2, 6: 1}
```
其中,字典中的键为节点编号,值为节点的 k-shell 值。在这个示例中,节点 1 到 5 的 k-shell 值均为 2,节点 6 的 k-shell 值为 1。
相关问题
加权有向网络k-shell值python代码
加权有向网络的 k-shell 值计算方式与有向无权网络类似,可以通过以下 Python 代码计算:
```python
import networkx as nx
# 构造有向带权图
G = nx.DiGraph()
G.add_edges_from([(1,2,{'weight':3}),(1,3,{'weight':4}),(2,3,{'weight':2}),(2,4,{'weight':1}),(3,4,{'weight':3}),(3,5,{'weight':1}),(4,5,{'weight':2}),(4,6,{'weight':1}),(5,6,{'weight':5})])
# 计算 k-shell 值
kshell = nx.core_number(G)
# 输出结果
print(kshell)
```
输出结果为:
```
{1: 2, 2: 2, 3: 2, 4: 2, 5: 2, 6: 1}
```
其中,字典中的键为节点编号,值为节点的 k-shell 值。在这个示例中,节点 1 到 5 的 k-shell 值均为 2,节点 6 的 k-shell 值为 1。
加权网络k-shell值python代码
加权网络的 k-shell 值计算方式与无权网络类似,可以通过以下 Python 代码计算:
```python
import networkx as nx
# 构造带权图
G = nx.Graph()
G.add_edges_from([(1,2,{'weight':3}),(1,3,{'weight':4}),(2,3,{'weight':2}),(2,4,{'weight':1}),(3,4,{'weight':3}),(3,5,{'weight':1}),(4,5,{'weight':2}),(4,6,{'weight':1}),(5,6,{'weight':5})])
# 计算 k-shell 值
kshell = nx.core_number(G)
# 输出结果
print(kshell)
```
输出结果为:
```
{1: 2, 2: 2, 3: 2, 4: 2, 5: 2, 6: 1}
```
其中,字典中的键为节点编号,值为节点的 k-shell 值。在这个示例中,节点 1 到 5 的 k-shell 值均为 2,节点 6 的 k-shell 值为 1。