我认为问题在于文档中对 nodelist 的解释。
根据文档:
nodelist (list, optional) – 行和列按 nodelist 中的节点排序。如果 nodelist 为 None 则排序由 G.nodes() 生成。
但它没有提到节点列表实际上修改了图形。它使用节点列表中的节点制作另一个图。
假设您的图表有 4 个节点 [1,2,3,4],但您将节点列表定义为 [2,3,4]。当您阅读文档时,您认为该函数将计算原始图中节点 2、3 和 4 之间的距离矩阵。但是,它似乎从图中删除了节点 1(从技术上改变了原始图),然后计算了 2、3 和 4 之间的距离矩阵。如果节点 1 连接节点 2 和 3,则可能会出现问题。
Sample Graph with 4 nodes
Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[1,2,3,4])
print(Dist_Mat)
[[0. 1. 1. 1.]
[1. 0. 2. 2.]
[1. 2. 0. 2.]
[1. 2. 2. 0.]]
Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[2,3,4])
print(Dist_Mat)
[[ 0. inf inf]
[inf 0. inf]
[inf inf 0.]]
请在下面找到代码
import networkx as nx
import matplotlib.pyplot as plt
import nxviz as nv
G=nx.Graph()
G.add_node(1)
G.nodes[1]['N']=10
G.add_nodes_from([(2,{'N':20}),3,4])
G.add_edge(1,2)
G.edges[1,2]['E']=120
G.add_edges_from([(1,3,{'E':130}),(1,4)])
G.nodes()
pos = {0: (0, 0),
1: (1, 0),
2: (0, 1),
3: (1, 1),
4: (0.5, 2.0)}
print(G.nodes(data=True))
print(G.edges(data=True))
nx.draw(G, pos, with_labels=True, font_weight='bold')
FIG=nv.CircosPlot(G,node_size=1)
FIG.draw();plt.show()
Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[1, 2,3,4])
print(Dist_Mat)
Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[2,3,4])
print(Dist_Mat)