且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得两个节点之间最小路径的权重?

更新时间:2022-02-01 09:34:49

您在寻找 single_source_dijkstra

from networkx.algorithms.shortest_paths.weighted import single_source_dijkstra

single_source_dijkstra(G,s,t)






示例

import networkx as nx
from networkx.algorithms.shortest_paths.weighted import single_source_dijkstra

G = nx.Graph()

G.add_edge('a', 'b', weight=0.6)
G.add_edge('a', 'c', weight=6)
G.add_edge('c', 'd', weight=0.1)
G.add_edge('c', 'e', weight=0.7)
G.add_edge('c', 'f', weight=0.9)
G.add_edge('a', 'd', weight=0.3)

single_source_dijkstra(G,'b','f')

输出

(1.9, ['b', 'a', 'd', 'c', 'f'])