Shivam164's blog

By Shivam164, history, 4 years ago, In English

As while running Dijkstra's algorithm using a priority queue, we are keeping extra unnecessary nodes in the priority queue which we would not need in the future, but we can remove such nodes while inserting a new node(of the same value) using set.

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
4 years ago, hide # |
← Rev. 2  
Vote: I like it -20 Vote: I do not like it

Priority Queue and Set's Insert has the same time complexity of $$$O(logN)$$$, but still Priority Queue is faster if you actually use it, and it takes up less memory than Set, because Set can be unbalanced while Deleting a node, and it needs some extra operations when it does. Also, we don't really need to 'delete' something inside the Priority Queue when we use Dijkstra, because we can use a trick which you will know if you know Dijkstra.

»
4 years ago, hide # |
← Rev. 2  
Vote: I like it +1 Vote: I do not like it

Approach 1) Use a set and once you find a better root to node distance, update the set

Approach 2) Use a priority queue and once you find a better root to node distance, insert the new distance in the priority queue.

Complexity of Dijkstra with approach #1 is $$$O(ElogV)$$$, while the second one has complexity $$$O(ElogE) = O(ElogV^2) = O(ElogV)$$$. Generally the heap is more than twice as fast as the set, so the constant factor of approach #2 is better.

There's also an approach involving Fibonacci heaps that I'm not familiar with that might be even faster.

  • »
    »
    4 years ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    If you want to solve just Dijkstra in an usual cp setting, binary heaps are simply better. For fibonacci heaps to beat binary heaps, log V needs to be larger than the constant factor in fibonacci heaps. Plus binary heaps work well with the cache. If we have really large and dense graphs or we are storing complex objects in heaps that need more time to evaluate <, then fibonacci heaps can be favored.

»
3 years ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

it's possible to write PQ dijkstra without unnecessary nodes 169797615