且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找到所有的路径与DFS图

更新时间:2023-11-21 19:55:58

您是在正确的轨道 - 回溯是一种巧妙的方法来解决它。

You are on the right track - backtracking is a neat way to solve it.

要获得所有路径的使用相同的边缘只有一次: 您使用的边缘后, findAllPaths() - 从边集[删除的连接删除的 LinkedHashSet 这种边]的每个顶点的 - 并调用递归

To get all paths that "uses the same edge only once": after you use an edge in findAllPaths() - delete it from the set of edges [delete the connection from the LinkedHashSet of each vertex of this edge] - and invoke recursively.

在从递归返回 - 。不要忘了清洁环境,并重新添加该边两个顶点

After you return from the recursion - don't forget to "clean up the environment" and add this edge back to both vertices.

您将需要确保你没有碰到遍历集合,而修改它的麻烦。 [你不能做到这一点 - 这样做的结果是意想不到的 - 所以你可能会需要发送的 LinkedHashSet S的副本[没有相关的边缘 - 而不是原来的

You will need to make sure you don't run into troubles of iterating collection while modifying it. [You cannot do it - the result of doing so is unexpected] - so you will probably need to send a copy of the LinkedHashSets [without the relevant edge] - and not the original one.