简体   繁体   中英

Find the shortest Path between two nodes (vertices)

I have a list of interconnected edges ( E ), how can I find the shortest path connecting from one vertex to another?

I am thinking about using lowest common ancestors , but the edges don't have a clearly defined root, so I don't think the solution works.

Shortest path is defined by the minimum number of vertexes traversed.

Note: There could be a multi-path connecting two vertices, so obviously breadth first search won't work

Dijkstra的算法将为您完成此任务。

I'm not sure if you need a path between every pair of nodes or between two particular nodes. Since someone has already given an answer addressing the former, I will address the latter.

If you don't have any prior knowledge about the graph (if you do, you can use a heuristic-based search such as A* ) then you should use a breadth-first search .

Floyd-Warshall算法可能是您问题的可能解决方案,但也有其他解决方案来解决所有对最短路径问题。

Shortest path is defined by the minimum number of vertexes treversed

it is same as minimum number of edges plus one.

you can use standard breadth first search and it will work fine. If you have more than one path connecting two vertices just save one of them it will not affect anything, because weight of every edge is 1.

Additional 2 cents. Take a look at networkx . There are interesting algos already implemented for what you need, and you can choose the best suited.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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