Блог пользователя Shivam164

Автор Shivam164, история, 4 года назад, По-английски

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.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
4 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -20 Проголосовать: не нравится

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 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

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 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    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 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

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