【问题标题】:Source algorithm for networkx shortest_path(G[, source, target, weight]) functionnetworkx shortest_path(G[, source, target, weight]) 函数的源算法
【发布时间】:2014-09-20 03:21:28
【问题描述】:

我正在使用 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]) 函数所基于的源算法。我需要它,因为我必须报告我使用的算法。我搜索了一些 stackoverflow 页面,例如 Networkx - Shortest path length 和 All shortest paths for weighted graphs with networkx? 但他们并没有完全回答我的问题,我也仔细查看了 networkx 文档和谷歌上的其他文章,但没有找到答案。有人可以帮我提供这些信息。谢谢

【问题讨论】:

    标签: python algorithm graph networkx shortest-path


    【解决方案1】:

    这是一种广度优先搜索算法 (BFS)。这是单一来源问题的整个 NetworkX 代码。它也用于所有对的最短路径计算。对于源-目标最短路径,使用 BFS 的双向版本。这不是很好的文档,但文档和代码在http://networkx.github.io/documentation/networkx-1.9/reference/generated/networkx.algorithms.shortest_paths.generic.shortest_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
    

    【讨论】:

    • 感谢您的回复,我想我需要的关键字是 BFS。这将使我能够进一步研究。我很感激
    【解决方案2】:

    这也是为“典型”的情况运行Dijkstra,看源码,原来只是一些条件子句:

    http://networkx.lanl.gov/_modules/networkx/algorithms/shortest_paths/generic.html#shortest_path

     ...
     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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2017-07-02
      • 2020-09-10
      • 2022-11-20
      • 1970-01-01
      相关资源
      最近更新 更多