简体   繁体   中英

Why Does The Dikstra Algorithm Run In O(V + E log V) Instead Of O(V ^ 2)?

Where V is the number of vertices and E is the number of edges, in worst case scenario, all nodes are connected, and at every node, you look at all other nodes. So won't that just be O(V ^ 2) ? I looked it up and found that it is actually O(V + E log V) , but with no explanation.

NO, Dijkstra's best implementation using Fibonacci heap as priority queue runs in O(|E|+|V|*log|V|) according to here . Bare in mind that in a dense Graph where |E| is O(|v|^2) , O(|E|+|V|*log|V|) becomes equal to O(|V|^2) where it is the worst case of the algorithm, but in none of these cases it runs in O(|V|+|E|log|V|) .

Your analysis disregards the cost of the priority queue. Still, you're not entirely wrong.

It takes O(|E|) decrease-key operations, since nearly every edge you visit might do this, and O(|V|) delete-min operations, since you have to do this to take vertices out of the queue.

If you use a Fibonacci heap as the priority queue, then decrease-key takes constant time, but delete-min takes O(log |V|) time, so you get O(|E| + |V| log |V|). In terms of |V|only, |E| is replaced with O(|V| 2 ), since it might go that high, and that dominates the |V| log |V| term giving total complexity of O(|V| 2 ).

So you are correct if you want to give a bound in terms of |V|only, but giving a bound in terms of |V|and |E| separately is preferred, because it conveys more information . It tells you that it is faster than O(|V| 2 ) for sparse graphs, which may be important.

Note that The O(V + E log V) you looked up is for for an implementation that uses a different kind of priority queue, like a binary heap, instead of a Fibonacci heap. This is common in practice.

You are only considering the fully connected graph situation. the big-O notation is for the worst case given certain input parameters, and since the complexity given uses V and E, that means that the fully-connected graph is irrelevant. For graphs in general, Dijkstra's algorithm will finish on O(V+ElogV) or faster; for the fully connected graph, wher E is approximately (V^2)/2, it will finish in O(V^2), which is indeed faster than O(V+((V^2)/2)logV).

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