python networkx进行最短路径分析_Python中networkx中shortest_path使用的是哪一种最短路径方法...

本文介绍了一个根据源节点和目标节点的不同情况选择不同路径搜索算法的方法。具体包括Dijkstra算法、广度优先搜索(BFS)及深度优先搜索(DFS)等,并详细解释了每种情况下算法的选择依据。

不全是。依据传入的参数决定调用哪种算法。

看源码:至少涉及了dijkstra、广度优先/深度优先算法。if source is None:

if target is None:

## Find paths between all pairs.

if weight is None:

paths=nx.all_pairs_shortest_path(G)

else:

paths=nx.all_pairs_dijkstra_path(G,weight=weight)

else:

## Find paths from all nodes co-accessible to the target.

directed = G.is_directed()

if directed:

G.reverse(copy=False)

if weight is None:

paths=nx.single_source_shortest_path(G,target)

else:

paths=nx.single_source_dijkstra_path(G,target,weight=weight)

# Now flip the paths so they go from a source to the target.

for target in paths:

paths[target] = list(reversed(paths[target]))

if directed:

G.reverse(copy=False)

else:

if target is None:

## Find paths to all nodes accessible from the source.

if weight is None:

paths=nx.single_source_shortest_path(G,source)

else:

paths=nx.single_source_dijkstra_path(G,source,weight=weight)

else:

## Find shortest source-target path.

if weight is None:

paths=nx.bidirectional_shortest_path(G,source,target)

else:

paths=nx.dijkstra_path(G,source,target,weight)

追问 : 这个没包含source确定,arget不确定的情况哦?

追答 :可能是我贴的有问题,其实源码中是配对的(从1开始数,第4个else和第1个if配对)。建议自行查看源码,可以用wingIDE4.1,非常方便。另这里贴出的是networkx.shortest_paths.shortest_path方法的源码,另有networkx.shortest_path方法(使用标准的BFS)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值