我正在使用networkx做一些工作,并使用了两种最短路径算法,即:
shortest_path(G[, source, target, weight])
dijkstra_path(G, source, target[, weight])
我知道dijkstra_path(G, source, target[, weight])
函数基于dijkstra的最短路径算法。我想知道shortest_path(G[, source, target, weight])
函数所基于的源算法。我需要它,因为我必须报告我使用的算法。我搜索过一些像Networkx - Shortest path length和All shortest paths for weighted graphs with networkx?这样的堆叠溢出页面,但它们没有完全回答我的问题,我还仔细查看了networkx文档和谷歌上的其他文章,没有找到答案。有谁能帮我查一下这个信息吗。谢谢
发布于 2014-07-28 13:11:41
这是一种宽度优先搜索算法(BFS)。下面是针对单源问题的整个NetworkX代码。它还用于全对最短路径的计算。对于源-目标最短路径,使用BFS的双向版本。这不是很好的文档,但是文档和代码在path.html
def single_source_shortest_path(G,source,cutoff=None):
level=0 # the current level
nextlevel={source:1} # list of nodes to check at next level
paths={source:[source]} # paths dictionary (paths to key from source)
if cutoff==0:
return paths
while nextlevel:
thislevel=nextlevel
nextlevel={}
for v in thislevel:
for w in G[v]:
if w not in paths:
paths[w]=paths[v]+[w]
nextlevel[w]=1
level=level+1
if (cutoff is not None and cutoff <= level): break
return paths
发布于 2014-07-28 12:34:10
它还为“典型”情况运行Dijkstra,查看源代码,这表明它只是一些条件子句:
...
if source is None:
if target is None:
if weight is None:
paths=nx.all_pairs_shortest_path(G)
else:
paths=nx.all_pairs_dijkstra_path(G,weight=weight)
else:
raise nx.NetworkXError(\
"Target given but no source specified.")
else: # source specified
if target is None:
if weight is None:
paths=nx.single_source_shortest_path(G,source)
else:
paths=nx.single_source_dijkstra_path(G,source,weight=weight)
else:
# 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)
return paths
https://stackoverflow.com/questions/25003702
复制相似问题