Search code examples
algorithmshortest-pathdijkstra

Dijkstra's algorithm for finding the midpoint with the shortest total route


I need an algorithm to select the midpoint that has the shortest route between the source node s and the destination node h. There are midpoints (intermediate vertices) b in the set of intermediate vertices B such that for every b there is a path s~b and b~h. My idea is that I'll run Dijkstra for every b starting on s and then reverse the graph to run it again starting on the destination for every b. Then find the minimal sum. However, the problem requires the algorithm to implement Dijkstra's algorithm only once and to define a new graph G' on 2|V| vertices and at most 2|E| + |V| edges. I am not sure how I can alter the graph to satisfy the condition but I think that I have to make a copy of the set of original vertices.


Solution

  • The idea is that your new graph G' should include two copies of the original graph G. Let's call the vertices in these two copies T and U.

    The edges are almost the same in both copies, except that every edge into a midpoint vertex goes to the copy of that vertex in U.

    Now, you run Dijkstra's to find the shortest path from sT to hU. Of course the path must get from T to U somehow, and it can only do that when it enters a midpoint vertex, so you have found the shortest path from s to h that passes through a midpoint.

    Note that when this kind of algorithm is implemented for real, we don't actually make two copies of the graph. We just pretend we did and augment the vertex numbers in Dijkstra's algorithm with an extra Boolean to indicate whether it refers to T or U. We are then operating on an implicit graph, as most real-life implementations of simple graph algorithms do.