简体   繁体   中英

Dijsktra's algorithm in O((V+E)*W)

Let's say i have a directed graph G(V,E) with integral positive weights on it's edges.If i know that for every edge e ∈ E weight(e) ∈ {0,1,..2^W}(where W is relatively small) how can i implement dijsktra's algorithm in O((V+E)*W)?

I tried modifying Dial's algorithm where if weight(e) ∈ {0,1,..,W} i could use buckets and implement dijsktra in O(V*W+E) but i can't seem to have good results.

When I have to implement Dijkstra's algorithm, I usually do it using a binary heap without a decrease_key operation.

To do that, you put (cost,vertex) records in the heap, and whenever the cost for a vertex goes down, you just put in a new one. When you pop a vertex out of the heap, then, just ignore it if it's already done. You have to keep track of which vertexes are done anyway, so that is easy.

This takes a bit more space, but the complexity remains O(|E| log V) (which is the same as O(|V+E| log |V|) or O(|E| log |E|) for the connected component that the algorithm will traverse )

Now the factor of log|E| above derives from the fact that there are up to |E| elements in the heap. If you group all elements with the same priority into lists and put those lists in the priority queue, then the that log |E| factor turns into log(number of distinct priorities). Note that a heap array ceases to be an appropriate structure for the priority queue, but various kinds of ordered trees work fine. It's easy when you don't need decrease_key.

In your case, the difference in cost between vertexes at the front of the priority queue, and the highest cost in the priority queue is at most 2^W. That implies that there are at most 2^W distinct priorities in the queue and the complexity of Dijkstra's algorithm using this kind of priority queue is O(|E|W).

Since you don't want to allocate 2^W buckets, a Patricia tree of linked lists would make a viable priority queue for your case.

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