简体   繁体   English

在未知大小的加权有向图上,一个人如何遍历两个顶点之间从最短到最长的所有可能的非循环路径?

[英]On a weighted directed graph of unknown size, how can one iterate over all possible acyclic paths between two vertices from shortest to longest?

We can assume that all edge weights are positive, and that you can enumerate the edges leading outwards from a vertex, and likewise the edges leading inward, in O(1) time. 我们可以假设所有边缘权重都是正的,并且可以枚举从顶点向外引出的边缘,以及同样以O(1)时间向内引出的边缘。

For example, you can perform Dijkstra traversal (or A*, with an admissible heuristic) and mark each vertex's distance from the start until you locate the end vertex, then recurse over these markings in reverse as they describe the possible predecessors on an optimal path. 例如,您可以执行Dijkstra遍历(或A *,可允许的启发式方法),并标记每个顶点从起点开始的距离,直到找到终点顶点为止,然后反向递归遍历这些标记,因为它们描述了最佳路径上的可能的前辈。 That is, for each possible predecessor, you can determine if it was found on the greedy optimal path if the difference between marked distances is equal to the weight of the edge that connects them. 也就是说,对于每个可能的前任,如果标记距离之间的差等于连接它们的边的权重,则可以确定是否在贪婪的最佳路径上找到了它。

When looking at possible predecessors, the cost of the incoming edge plus the difference between the optimal distance to each vertex is equal to the loss of optimality incurred by including this edge in a solution (zero for edges on optimal paths). 当查看可能的前辈时,传入边的成本加上到每个顶点的最佳距离之间的差等于通过在解决方案中包含该边而导致的最佳性损失(最佳路径上的边为零)。 So perhaps the question becomes: How can this best be extended to yield all possible paths ranked by decreasing optimality? 所以也许问题就变成了: 如何最好地扩展这一范围,以产生通过降低最优性而排名的所有可能路径? Is there a clean way to perform a best-first traversal over this kind of meta-graph? 是否有一种干净的方法可以对这种元图进行最佳优先遍历?

This seems like the right direction to go for a useful solution. 这似乎是寻求有用解决方案的正确方向。 Perhaps a useful thing to keep in mind is that if the part of the path you have explored so far is potentially part of a solution that is suboptimal by at least x , checking for cycles need only be done along the last x distance visited (any path suboptimal by x cannot possibly contain a cycle longer than x ). 可能要记住的一件有用的事情是,如果到目前为止,您探索的那部分路径可能是解决方案的一部分,而该解决方案的优次性至少为x ,则仅需沿着最后访问的x距离进行循环检查(任何由x次优的path不能包含比x更长的循环。

Is there a more efficient approach? 有没有更有效的方法?

As a bonus question, is it also possible to do this on a graph (of known size ) with negative edge weights? 另外一个问题是,是否可以在边权重为负的图形( 已知大小 )上执行此操作? Does it become more difficult if negative cycles are introduced? 如果引入负周期会变得更加困难吗? (Remember, as we are looking only for acyclic paths this does not necessarily mean that the optimal solution runs away.) (请记住,因为我们仅在寻找非循环路径,但这并不一定意味着最佳解决方案会消失。)

For full graph of N nodes there are magnitude of (N-2)! 对于N个节点的完整图,大小为(N-2)! possible acyclic pathes from node A to node B. Think about it... This should be huge huge number and if you only need K (big enough, but reasonable number) pathes, you better got K-shortest_path mentioned in comments. 可能需要从节点A到节点B的非循环路径。请考虑一下……这应该是一个巨大的数字,如果您只需要K(足够大,但数量合理)的路径,则最好在注释中提到K-shortest_path。

If you can manage enough memory to hold all possible ways in it, there is obvious solution - generate all possible ways and sort them by weight. 如果您可以管理足够的内存以容纳所有可能的方式,则存在明显的解决方案-生成所有可能的方式并按权重对它们进行排序。 If not, you'll have to dump answers to disk and then collect them. 如果不是,则必须将答案转储到磁盘,然后再收集它们。

You can enumerate all possible ways with modified BFS - "visited" array is passed to recursive call instead of being global boolean array. 您可以使用修改后的BFS枚举所有可能的方法-将“ visited”数组传递给递归调用,而不是全局布尔数组。 When you visit destination, add it to global solutions map (key - weight, value - list of pathes with this weight). 当您访问目的地时,将其添加到全局解决方案图(键-权重,值-具有此权重的路径列表)。

If you cannot afford holding all pathes in memory, you can dump them to temporaty files. 如果您负担不起在内存中保留所有路径,则可以将它们转储到临时文件中。 Naive solution for this: open file with name 0-padded up to 10 digits - or whatever fits your needs - weight, and add one line for path. 朴素的解决方案:打开名称为0的文件,最多填充10位数字-或任何适合您需要的数字-权重,然后为路径添加一行。 After all pathes are collected, read files in appropriate order and form final result. 收集所有路径后,以适当的顺序读取文件并形成最终结果。

NB It's better not to open/append/close files if you can. 注意:如果可以的话,最好不要打开/附加/关闭文件。 You can collect pathes in map and dump only longest list when total number of pathes exceeds some limit, for example. 例如,您可以在地图中收集路径,并且仅当路径总数超过某个限制时才转储最长的列表。

归功于nm:Wikipedia上有关K最短路径路由的文章描述了各种现代方法,包括用于Dijkstra推广的伪代码,并链接到2011年有关K *的论文,该论文也利用了启发式方法。

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

相关问题 有向非循环图中两个顶点之间的最大加权路径 - Maximum weighted path between two vertices in a directed acyclic Graph 如何在有向图和线性时间中找到两个顶点之间不同的最短路径的数量? - How to find the number of different shortest paths between two vertices, in directed graph and with linear-time? 在有向完整加权图中访问所有顶点的最短路径 - Shortest path that visits all vertices in a directed complete weighted graph 检查有向无环图中两个顶点之间是否存在路径 - 查询 - Check if there exist a path between two vertices in directed acyclic graph - queries 拓扑排序的加权有向非循环图上的最长路径搜索,但有效路径的最大边数 - Longest path search on a topologically sorted weighted directed acyclic graph, but with a maximum edge count for valid paths 在有向加权图中找到最短的顶点序列 - Finding shortest sequence of vertices in directed weighted graph 如何找到无向图中两个给定顶点之间的所有最短路径? - How to find all the shortest paths between two given vertices in an undirected graph? 在有向无环图中查找两个节点之间的路径数 - Finding number of paths between two nodes in a directed acyclic graph 消除两个顶点之间的所有最短路径 - FInding All Shortest Paths Between Two Vertices 在有向无环加权图中找到前3条最长路径 - Finding top 3 longest path in directed acyclic weighted graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM