且构网

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

查找两个图节点之间的所有路径

更新时间:2023-01-26 08:00:22

寻找所有可能的路径是一个难题,因为简单路径的数量是指数级的.即使找到第 k 个最短路径 [或最长路径] 也是 NP-Hard.

Finding all possible paths is a hard problem, since there are exponential number of simple paths. Even finding the kth shortest path [or longest path] are NP-Hard.

找到从 st 的所有路径 [或所有路径达到特定长度的所有路径] 的一种可能解决方案是 BFS,不保留 visited 集,或者对于加权版本 - 您可能想要使用 统一成本搜索

One possible solution to find all paths [or all paths up to a certain length] from s to t is BFS, without keeping a visited set, or for the weighted version - you might want to use uniform cost search

请注意,在每个具有循环的图中 [它不是 DAG]是 st 之间的无限路径.

Note that also in every graph which has cycles [it is not a DAG] there might be infinite number of paths between s to t.