Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两个节点之间的路径_Python_Networkx_Igraph - Fatal编程技术网

Python 两个节点之间的路径

Python 两个节点之间的路径,python,networkx,igraph,Python,Networkx,Igraph,我正在使用networkx处理图形。我有一个相当大的图(图中有近200个节点),我试图找到两个节点之间所有可能的路径。但是,据我所知,networkx只能找到最短的路径。我怎样才能不仅得到最短路径,而且得到所有可能的路径 UPD:路径只能包含每个节点一次 UPD2:我需要类似于find_all_paths()的函数,如这里所述:python.org/doc/articles/graphs.html,但该函数不能很好地处理大量节点和edge=(Dijkstra的算法将以类似于广度优先搜索的方式查找

我正在使用networkx处理图形。我有一个相当大的图(图中有近200个节点),我试图找到两个节点之间所有可能的路径。但是,据我所知,networkx只能找到最短的路径。我怎样才能不仅得到最短路径,而且得到所有可能的路径

UPD:路径只能包含每个节点一次


UPD2:我需要类似于find_all_paths()的函数,如这里所述:python.org/doc/articles/graphs.html,但该函数不能很好地处理大量节点和edge=(

Dijkstra的算法将以类似于广度优先搜索的方式查找最短路径(它将按深度加权的优先级队列替换为BFS的原始队列)。如果您需要一些备选方案,您可以非常轻松地扩展它以生成“N”条最短路径,但如果您需要路径显著不同(例如,安排安全货车的路线)您可能需要更聪明地选择彼此明显不同的路径。

,Python的另一个图形模块可以计算给定节点对之间的所有最短路径。计算所有路径没有意义,因为您有无限多条这样的路径

计算顶点0的所有最短路径的示例:

>>> from igraph import Graph
>>> g = Graph.Lattice([10, 10], circular=False)
>>> g.get_all_shortest_paths(0)
[...a list of 3669 shortest paths starting from vertex 0...]
如果您有igraph 0.6或更高版本(这是编写本文时的开发版本),您也可以将
get_all_最短路径
的结果限制到给定的端点顶点:

>>> g.get_all_shortest_paths(0, 15)
[[0, 1, 2, 3, 4, 14, 15],
 [0, 1, 2, 12, 13, 14, 15],
 [0, 10, 11, 12, 13, 14, 15],
 [0, 1, 11, 12, 13, 14, 15],
 [0, 1, 2, 3, 13, 14, 15],
 [0, 1, 2, 3, 4, 5, 15]]
当然,您必须小心;例如,假设您有一个100 x 100的网格图(可以通过igraph中的
graph.Lattice([100100],circular=False)
轻松生成)。从左上角节点到右下角节点的最短路径数等于从200个元素中选择100个元素的可能性数(证明:最短路径的长度有200条边,其中100条边在网格中“水平”,100条边在网格中“垂直”)。这可能不适合您的记忆,因此,即使计算这两个节点之间的所有最短路径,在这里也不太可行

如果您确实需要两个节点之间的所有路径,可以使用igraph重写您提到的网页上给出的函数,这可能比纯Python解决方案快,因为igraph的核心是用C实现的:

def find_all_paths(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return [path]
    paths = []
    for node in set(graph.neighbors(start)) - set(path):
        paths.extend(find_all_paths(graph, node, end, path))
    return paths
通过首先将图形转换为邻接列表表示,可以对其进行更多优化,因为这样可以避免对
图形的重复调用

def find_all_paths(graph, start, end):
    def find_all_paths_aux(adjlist, start, end, path):
        path = path + [start]
        if start == end:
            return [path]
        paths = []
        for node in adjlist[start] - set(path):
            paths.extend(find_all_paths_aux(adjlist, node, end, path))
        return paths

    adjlist = [set(graph.neighbors(node)) for node in xrange(graph.vcount())]
    return find_all_paths_aux(adjlist, start, end, [])

编辑:修复了第一个同样适用于igraph 0.5.3的示例,不仅适用于igraph 0.6。

此示例实际上适用于networkx,并且它是非递归的,这对于大型图形可能很好

def find_all_paths(graph, start, end):
    path  = []
    paths = []
    queue = [(start, end, path)]
    while queue:
        start, end, path = queue.pop()
        print 'PATH', path

        path = path + [start]
        if start == end:
            paths.append(path)
        for node in set(graph[start]).difference(path):
            queue.append((node, end, path))
    return paths

一般来说,这是不可能的。如果你的图表中有循环,那么有无限多的路径,例如A->B->A->B->…->B->C你需要在你的问题中添加一些约束(例如,没有循环,或者你计划如何处理循环)。虽然你可以使用龟兔算法检测路径中的循环。通过将其与广度优先搜索相结合,你可能会得到一个可接受的近似值,尽管我敢说这会有点低效。我需要类似“查找所有路径”(find_all_paths)的东西函数,如下所述:但该函数不能很好地处理大量节点和边缘=(@user285070的链接已断开,但我无法编辑注释,因此更正的注释如下:。如果您更喜欢wayback缓存:首先,感谢您的精彩帖子!但我对您的代码有一些问题。我已下载并安装了适用于Windows的igraph-Python扩展模块。然后我尝试运行您的第一个示例(g.get_all_shortest_path),但它返回错误igraph.core.InternalError:error at.\src\structural_properties.c:1032:无效模式参数,无效模式您能解释一下如何修复它吗?是的,您是对的-第一个代码示例仅在igraph 0.6中工作(这是igraph的开发分支)。在igraph 0.5.3中,get_all_shortest_paths仅接受一个源顶点ID,并提供从该节点到网络中所有其他节点的所有最短路径。因此,如果执行此操作,代码将起作用:g.get_all_shortest_paths(0)这将为您提供一个从0开始的3669条不同路径的列表;它们都是网络中从顶点0开始的最短路径。此代码是否计算任何两个节点之间的所有可能路径?包括最短路径和较长路径?