查找在向标定图的两个节点之间的所有可能路径节点、路径、两个

2023-09-11 23:28:32 作者:不再期待

可能重复:   找到两个特定节点之间的路径?

给定一个有向图,如何找到两个节点之间的所有可能的路径,并返回这些路径

Given a directed graph, how to find ALL the possible paths between two nodes and return those paths.

如果没有在Java中,请推荐给我的算法吧。我搜索,我发现正在使用BFS或DFS,但我没能看到这是在我的情况比较好。而如何跟踪所有的路径,不仅最短的一个。

If not in Java, please recommend me with the algorithm for it. I searched and what I found is using BFS or DFS, but I'm not able to see which is better in my case. And how to keep track of all paths not only the shortest one.

例如,给出如下图:

1 - > 2

1 - > 3

2 - > 3

3 - > 4

有关节点1和4之间的路径,输出应该是:

For paths between nodes 1 and 4, the output should be:

第一个道路:1 - > 2 - > 3 - > 4

First Path: 1 -> 2 -> 3 -> 4

第二条路径:1 - > 3 - > 4

Second Path: 1 -> 3 -> 4

推荐答案

见深度优先搜索Algorithme的 http://en.wikipedia.org/wiki/Depth-first_search

see the Depth-first search Algorithme http://en.wikipedia.org/wiki/Depth-first_search