简体   繁体   English

非加权图如何做最短路径算法?

[英]How to do shortest path algorithm for unweighted graph?

I'm trying to find the shortest path from a vertex to another of a connected, unweighted graph. 我试图找到从一个顶点到另一个相连的,未加权图的最短路径。

In this problem,the distance from a vertex to its adjacent vertex will be equal to 1.ie., if a graph with edges (a,b),(a,c) is considered, the distance from a to b and c will be 1 and the distance from b to c will be 2. Also, an adjacency list is maintained to store all the adjacent vertices of each vertex. 在此问题中,从顶点到其相邻顶点的距离将等于1。即,如果考虑具有边(a,b),(a,c)的图,则从a到b和c的距离将为为1,从b到c的距离为2。此外,维护邻接表以存储每个顶点的所有相邻顶点。

So, is there any algorithms to find all the shortest paths for the given problem?? 那么,是否有任何算法可以找到给定问题的所有最短路径?

You could use dijkstra's algorithm to find the distance. 您可以使用dijkstra的算法来查找距离。

Here's one way to do using networkx 这是使用networkx的一种方法

In [28]: import networkx as nx

Create a grpah with nodes a, b, c where links are a, b and 'a, c' 创建一个节点为a, b, c的grpah a, b, c其中链接为a, b和“ a,c”

In [29]: g = nx.Graph()

In [30]: g.add_edge('a', 'b')

In [31]: g.add_edge('a', 'c')

Then using nx.dijkstra_path_length() find the distance between b and c 然后使用nx.dijkstra_path_length()找到b and c之间的距离

In [32]: nx.dijkstra_path_length(g, 'b', 'c')
Out[32]: 2

Also, you can find the path trail using dijkstra_path() 另外,您可以使用dijkstra_path()找到路径轨迹

In [33]: nx.dijkstra_path(g, 'b', 'c')
Out[33]: ['b', 'a', 'c']

You could also use shortest_path() for path between b and c 您还可以将shortest_path()用于b and c之间的路径

In [34]: nx.shortest_path(g, source='b',target='c')
Out[34]: ['b', 'a', 'c']

You can find all the paths with a function then choose the path with minimum length. 您可以找到具有功能的所有路径,然后选择最小长度的路径。

But note that this problem is more based on your search algorithm, for example with a BFS algorithm : 但请注意,此问题更多地取决于您的搜索算法,例如BFS算法:

You can use the following function that return a generator of paths : 您可以使用以下函数返回路径生成器:

def all_paths(graph, start, goal):
    queue = [(start, [start])]
    while queue:
        (v, path) = queue.pop(0)
        for next in graph[v] - set(path):
            if next == goal:
                yield path + [next]
            else:
                queue.append((next, path + [next])) 

And find the minimum path with min functions with len as its key : 并以len为键找到具有min函数的最小路径:

min_path = min(all_paths(graph, start, goal),key=len)

You can use BFS from that point: http://en.wikipedia.org/wiki/Breadth-first_search 您可以从那时开始使用BFS: http : //en.wikipedia.org/wiki/Breadth-first_search

You can also use Floyd–Warshall algorithm which runs O(n^3) if time is not an issue and you want simplicity: http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm 您也可以使用Floyd–Warshall算法,如果时间不成问题,并且想要简化操作,则运行O(n ^ 3): http : //en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

Dijkstra algorithm solve "the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized". Dijkstra算法解决了“在图形中找到两个顶点(或节点)之间的路径,以使其组成边的权重之和最小化的问题”。

http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

So, i think that you can solve this with Dijkstra where the distance from a vertex to its adjacent vertex is equal for every path between two vertex. 因此,我认为您可以使用Dijkstra解决此问题,其中对于两个顶点之间的每条路径,从顶点到其相邻顶点的距离都相等。

Anyway, you can use BFS http://en.wikipedia.org/wiki/Breadth-first_search 无论如何,您都可以使用BFS http://en.wikipedia.org/wiki/Breadth-first_search

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM