Which algorithm can I use to find the next to shortest path in a graph?

前端 未结 8 687
独厮守ぢ
独厮守ぢ 2020-12-05 10:50

I want to find the next shortest path between 2 vertices in a graph and the path has a positive cost.The next shortest path is allowed to share edges of the shortest path .W

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 11:33

    This answer assumes you are looking for the edge-disjoint second shortest path, which means the second shortest path cannot share any common edges with the shortest path.

    Recall that the maximum flow in a network between two nodes A and B gives you the number of edge-disjoint paths between those two nodes. Also recall that algorithms such as Edmonds-Karp work by sending flow over a shortest path at each step.

    So this problem only has a solution if the max flow between your two nodes is > 1, where each edge has a capacity of 1. If it does, find two augmenting paths as described in the Edmonds-Karp algorithm, and the second one is your second shortest.

    See this problem and this solution to it (The description is in Chinese. I can't translate it, and babelfish can't really do it either, but won't admit it. The code is easy to follow though) for an example.

提交回复
热议问题