简体   繁体   中英

Create an algorithm for computing the shortest path with constraints on the vertices in O(m + nlogn)

So I'm trying to write an algorithm for computing the shortest path with constraints on the vertices you can visit in O(m + nlogn) time. In this problem, we are given an indirect weighted (non negative) graph G = (V, E), a set of vertices X ⊂ V, and two vertices s, t ∈ V \ X. The graph is given with adjacency lists and we can assume that it takes O(1) time to determine if a vertex is in X. I'm trying to write an algorithm which can compute the shortest path between s and t in G such that the path includes at most one vertex from X if such a path exists in O(m + nlogn) time. I know that this algorithm would require a modified Dijkstra's algorithm but I'm unsure how to proceed from here. Could anyone please help me out? Thank you

Construct a new graph by taking two disjoint copies of your graph G (call them G0 and G1), and for each edge (v, x) in G (with x in X), add an additional edge in the combined graph from v in G0 to x in G1. This new graph has twice as many vertices as G, and at most three times as many edges.

The shortest path from s in G0 to t in G1 is the shortest path from s to t in G going through at least one vertex of X.

On this new graph, Dijkstra's algorithm (with the priority queue implemented with a fibonacci heap) works in time O(3m + 2n log 2n) = O(m + n log n).

A possibility is to double the number of vertices not in X .

For each vertex v not in X , you create v0 and v1 : v0 is accessible only without passing from a vertex in X , v1 is accessible by passing through one and only one vertex in X .

Let us call w another vertex. Then:

   if w is in X, v not in X:
        length (w, v0) = infinite and dist(v1) = min (dist(v1), dist(w) + length(w, v))

    if w is in X, v in X:
        length (w, v) = infinite 

    if w is not in X, v not in X:
        dist (v0) = min (dist(v0), dist (w0) + length (w, v))
        dist (v1) = min (dist(v1), dist (w1) + length (w, v))

    if w is not in X, v is in X:
        dist (v) = min (dist(v), dist (w0) + length (w, v))

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