tynbl.github.io

网络连通性

import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib notebook
G = nx.Graph()
G.add_edges_from([
        ('A', 'K'), 
        ('A', 'B'),
        ('A', 'C'),
        ('B', 'C'),
        ('B', 'K'),
        ('C', 'E'),
        ('C', 'F'),
        ('D', 'E'),
        ('E', 'F'),
        ('E', 'H'),
        ('F', 'G'),
        ('I', 'J')
    ])

plt.figure()
nx.draw_networkx(G)
<IPython.core.display.Javascript object>

1. 整体聚类系数

nx.transitivity(G)
0.4090909090909091

2. 局部聚类系数

print('节点F的聚类系数:', nx.clustering(G, 'F'))
print('节点A的聚类系数:', nx.clustering(G, 'A'))
print('节点J的聚类系数:', nx.clustering(G, 'J'))
节点F的聚类系数: 0.3333333333333333
节点A的聚类系数: 0.6666666666666666
节点J的聚类系数: 0.0

3. 平均聚类系数

nx.average_clustering(G)
0.28787878787878785

4. 节点间距离

# 去掉AC边
G.remove_edge('A', 'C')
plt.figure()
nx.draw_networkx(G)
<IPython.core.display.Javascript object>

nx.shortest_path(G, 'A', 'H')
['A', 'B', 'C', 'E', 'H']
nx.shortest_path_length(G, 'A', 'H')
4