问题
Suppose I have a source node S, destination node D and a set A of intermediate nodes P1,P2, P3,… in an edge-weighted undirected graph. I want to find the vertex Pi ∈ A that minimizes dist(S,Pi)+dist(D,Pi)? In addition, the overall path from S to D should contain only one node from the set A. What is an efficient algorithm for this? I don't want to go with brute-force approach.
回答1:
What do you mean by brute force?
Without assumption
If you remove the assumption about "only one node from the set A" than you could proceed as follows:
- Find shortest paths from S to all Pi (one call to Dijkstra algorithm)
- Find shortest paths from D to all Pi (one call to Dijkstra algorithm)
- Iterate through Pi and select the one minimizing d(S,Pi)+d(D,Pi)
Complexity: linear in terms of A plus complexity of your Dijkstra implementation (depends on the heap structure used)
With assumption
With your assumption, I suppose you have to run shortest path search from each Pi independently
- For each Pi
- find shortest pathto S and D in the grapg G \ A u {Pi} (remove all other Pj's)
- Iterate through Pi and select the one minimizing d(S,Pi)+d(D,Pi)
Now the complexity becoms A times the complexity of your shortest path algorithm (Dijkstra or other)
With assumption - seems optimal
Combination of two above approaches would be to create a "theoretical" graph, which would have only paths going through one point of A, so from practical point of view you would:
- Find shortest paths from S to all Pi assuming that you do not cross any other element of A, you can do it by assuming, that Pis' have no "outer" edges. this way, the dijkstra algorithm will correctly identify distances from S to Pi without crossing any other Pj's
- Find shortest paths from D to all Pi (the same as above)
- Iterate through Pi and select the one minimizing d(S,Pi)+d(D,Pi)
Complexity: linear in terms of A plus complexity of your Dijkstra implementation (depends on the heap structure used), so it is the same as the complexit of Dijkstra (which has to at least "read" all the vertices, so linear complexity in terms of A is irrelevant).
来源:https://stackoverflow.com/questions/22956923/minimum-path-between-two-vertices-passing-through-a-given-set