find shortest path in a graph that compulsorily vi

2019-07-08 10:07发布

问题:

I have an undirected graph with about 1000 nodes and 2000 edges, a start node and an end node. I have to traverse from the start node to the end node passing through all the compulsory edges(which are about 10) while its not necessary to traverse through all the vertices or nodes. Is there an easy solution to this like some minor changes in the existing graph traversing algorithms? How do I do it?

Thanks for help.:)

This question is different from Find the shortest path in a graph which visits certain nodes as my question is regarding compulsory edges not vertices.

EDIT: The compulsory edges can be traversed in any order.

回答1:

To start with a related problem, say you have a graph G = (V, E), 10 specific edges you must traverse in a given order E' = 1, ..., e10 > ∈ E, and a start and end nodes s, v ∈ V. You need to find the shortest distance from s to v using E' in the given order.

You could do this by making 10 copies of the graph. Start with a single copy (i.e., isomorphic t G = (V, E)), except that e1 moves from the first copy to the second copy. In the second copy (again isomorphic t G = (V, E)), remove e1, and have e2 move from the second copy to the third copy. Etc. In the resulting graph, run any algorithm to get from s in the first copy to e in the 10th copy.

Explanation: imagine intuitively that your graph G is drawn on a 2d sheet of paper. photocopy it so that you have 10 copies, and stack them up to a pile of 10 papers (imagine them with a bit of space between each two, though). Now change the graphs a bit so that the only way to go up to the second sheet, from the first sheet, is through an edge e1 leading from the bottom sheet to the second sheet. The only way to go up to the third sheet, from the second sheet, is through an edge e2 leading from the second sheet to the third sheet, and so on. You problem is to find the shortest path starting at the node corresponding to s on the bottom sheet, and ending at the node corresponding to e on the top sheet.

To solve the original problem, just repeat this with all possible permutations of E'. Note that there are 10! ~ 3.5e6 possibilities, which isn't all that much.