We need a sense of distance between nodes to answer these questions
In order to define Distance,we need to define Path
A sequence of nodes connected by an edge.
E x a m p l e Example Example
So,How far is node A from node H?
p a t h 1 : A − B − C − E − H ( 4 h o p s ) p a t h 2 : A − B − C − F − E − H ( 5 h o p s ) path1:A-B-C-E-H(4\,\,\,hops)\\ path2:A-B-C-F-E-H(5\,\,\,hops) path1:A−B−C−E−H(4hops)path2:A−B−C−F−E−H(5hops)
Path length:Number of steps it contains from beginning to end
Distance between two nodes:the length of the shortest path between them
So,the distance between node A and H is 4
print(nx.shortest_path(G,'A','H'))
#>>['A', 'B', 'C', 'E', 'H']
print(nx.shortest_path_length(G,'A','H'))
#>>4
Breadth-first search: a systematic and efficient procedure for computing distances from a node to all other nodes in a large network by “discovering” nodes in layers
——BFS过程,因为无权值,我们只考虑距离
T=nx.bfs_tree(G,'A')
print(T.edges())
#>>[('A', 'K'), ('A', 'B'), ('B', 'C'),
# ('C', 'E'), ('C', 'F'), ('E', 'D'),
# ('E', 'H'), ('E', 'I'), ('F', 'G'), ('I', 'J')]
print(nx.shortest_path_length(G,'A'))
#>>{'A': 0, 'K': 1, 'B': 1, 'C': 2,
# 'F': 3, 'E': 3, 'G': 4, 'I': 4,
# 'D': 4, 'H': 4, 'J': 5}
——How to characterize the distance between all pairs of nodes in a graph?
Average distance between every pair of nodes
任意两点距离的平均值
print(nx.average_shortest_path_length(G))
#>>2.5272727272727273
print(nx.diameter(G))
#>>5
eccentricity(偏心率):the largest distance between node n and all other nodes
节点n到达别的节点的 最短距离的 最大值
print(nx.eccentricity(G))
#>>{'A': 5, 'K': 5, 'B': 4, 'C': 3, 'E': 3,
# 'F': 3, 'D': 4, 'H': 4, 'I': 4, 'G': 4, 'J': 5}
print(nx.radius(G))
#>>3
我们知道了什么是长短——直径半径
接下来引入远近
Periphery(周边):The periphery of a graph is the set of nodes that have eccentricity equal to the diameter.
print(nx.periphery(G))
#>>['A', 'K', 'J']
这些节点往往位于网络的外围,远离所有其他节点
Center(中心):The center of graph is the set of nodes that have eccentricity equal to the radius
当前的中心点度量方法,对点很敏感,对图中微小的变化很敏感,稍微到一个点的距离大于半径,就不属于是中心点
print(nx.center(G))
#>>['C', 'E', 'F']
中心点就是,这个点到其他每个点的距离都小于等于半径
周边点就是,这个点拥有着全图最短距离的最大值,存在着直径
——空手道俱乐部网络
import networkx as nx
G=nx.karate_club_graph()
#This network is so famous that actually on network X
#you can simply load it by using the function karate club graph.
G=nx.convert_node_labels_to_integers(G,first_label=1)
#将节点标签换为整数,第一个标签是1
print(nx.average_shortest_path_length(G))
#>>2.408199643493761 平均距离
print(nx.diameter(G))
#>>5 直径
print(nx.eccentricity(G))
#>>{1: 3, 2: 3, 3: 3, 4: 3, 5: 4, 6: 4, 7: 4, 8: 4, 9: 3, 10: 4,
# 11: 4, 12: 4, 13: 4, 14: 3, 15: 5,
# 16: 5, 17: 5, 18: 4, 19: 5, 20: 3,
# 21: 5, 22: 4, 23: 5, 24: 5, 25: 4, 26: 4,
# 27: 5, 28: 4, 29: 4, 30: 5, 31: 4, 32: 3, 33: 4, 34: 4}
print(nx.radius(G))
#>>3 半径
print(nx.periphery(G))
#>>[15, 16, 17, 19, 21, 23, 24, 27, 30] 周边
print(nx.center(G))
#>>[1, 2, 3, 4, 9, 14, 20, 32] 中心
Node34 looks pretty “central”.However,it has distance 4 to node 17
34-32-1-6-17