简体   繁体   English

在访问某些顶点时在加权图中找到最短路径

[英]Finding the shortest path in weighted graph while visiting certain vertices

I'm trying to find the shortest path from A to Z in an undirectional weighted graph. 我试图在无向加权图中找到从A到Z的最短路径。 However, the resulting path should also go through a set of unordered vertices (let's just say B, C and D). 但是,生成的路径也应该经过一组无序的顶点(让我们说B,C和D)。
I haven't found any answers to this exact problem but I've encountered similar questions which defined the problem as NP-Hard (so not really solvable for every graph as far as I understand). 我没有找到这个确切问题的任何答案,但是我遇到了类似的问题,将问题定义为NP-Hard(据我所知,并不是每个图形都能真正解决)。

So is there still an algorithm that atleast attempts to find a path which visits all of these vertices and breaks in case it can't find a path after exhausting a reasonable amount of options? 因此,是否存在一种算法,它至少会尝试找到一条访问所有这些顶点并折断的路径,以防在用尽合理数量的选项后找不到路径?

If not, the alternative approach in my case would be to have these must-visit vertices ordered. 如果没有,我的替代方法是将这些必须访问的顶点排序。 Is there a better approach to finding a path than just splitting the path up and calculating the best path for every segment (A->B,B->C, etc)? 是否有比找到路径更好的方法,而不仅仅是将路径拆分并为每个段计算最佳路径(A-> B,B-> C等)?

Given a set of vertices S and an origin vertice v , you can use Depth First Search to sort the vertices in S from closest to furthest from v , that is make a table of distances. 给定一组顶点S和一个原始顶点v ,您可以使用“深度优先搜索”对S中的顶点进行排序,使其与v的距离最接近,最远,即形成一个距离表。

Creating that table in fact gives you a new graph composed only of the vertices S in O(nm) , where n is the size of the graph and m the size of the set S . 实际上,创建该表会给您一个仅由O(nm)中的顶点S组成的新图,其中n是图的大小, m是集合S的大小。

You are now looking for the shortest path passing through all the vertices of this new graph. 现在,您正在寻找穿过此新图的所有顶点的最短路径。 Your problem is thus equivalent to the Travelling Salesman problem on that smaller graph. 因此,您的问题等同于该较小图上的Traveling Salesman问题。

Note that it is fairly simple to enforce the ending point of a Travelling Salesman algorithm by adding a dummy node between your starting and end nodes . 请注意,通过在开始节点和结束节点之间添加虚拟节点来强制执行Traveling Salesman算法的终点非常简单。

It's now up to you to choose a known heuristic to apply on the smaller graph. 现在,您可以选择一种已知的启发式方法来应用于较小的图形。

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

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