简体   繁体   中英

Dijkstra’s algorithm to compute the shortest paths from a given source vertex s in O ((V+E) log W) time

G (V, E) is a weighted, directed graph with non negative weight function W : E -> {0, 1, 2, 3, 4... W } where W is any non negative integer. I want to modify the Dijkstra's algorithm to compute the shortest paths from a given source vertex s in O ((V+E) log W) time.

Let's take a standard Dijkstra's algorithm with a priority queue and change it a little bit:

  1. We'll have a map from an int (a distance) to a vector to keep all vertices with the given distance.

  2. We'll pop the element from the first vector to get the current vertex at each iteration and delete a key if the vector becomes empty.

  3. We'll push an element to a corresponding vector when we add it to the queue.

There are at most W + 1 different keys in the map at any point. Why? Let v be the current vertex and d[v] be its distance. All keys less than d[v] were deleted. Any undiscovered vertex is not in the map. For any discovered vertex u d[u] <= d[v] + max_edge_lenght = d[v] + W . Thus, any key in the map must be in the [d[v], d[v] + W] range.

The size of the map is O(W) so every operation with works in O(log W) time (pushing/popping an element from the vector is O(1) ).

Of course, it's useful only as long as W is relatively small (otherwise, you can go with a standard O((V + E) log V) implementation).

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