Dijkstra 算法的时间复杂度

时间:2021-06-21 10:07:52

标签: graph dijkstra

我在很多文章中也看到过,dijkstra 的时间复杂度是 O(V + ElogV) 但是时间复杂度不应该是O(V + ElogE)吗?

这是我的解释

Set all nodes distance to infinity and source distance to 0 -> O(V) time
Add source node to priority queue

//priority queue will have a maximum of |E| elements and popping or inserting from priority queue                                                                                          
//is done in O(Log|E|) time. So the total complexity for the while loop is O(ElogE)

while the priority queue is not empty:
    pop min from the priority queue -> O(logE) time
    if the popped element is visited:
         continue
    else mark the popped element as visited
    for all edges from popped node (u, v, w):
         if dist[v] > dist[u] + w:
              dist[v] = dist[u] + w
              add (v, w) to the priority queue -> O(logE)

那么时间复杂度不应该是O(V) + O(ElogE) = O(V + ElogE)吗?

0 个答案:

没有答案