简体   繁体   中英

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. However, the resulting path should also go through a set of unordered vertices (let's just say B, C and 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).

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)?

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.

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 .

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.

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 .

It's now up to you to choose a known heuristic to apply on the smaller graph.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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