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.
| # | User | Rating |
|---|---|---|
| 1 | Kevin114514 | 3722 |
| 2 | Benq | 3676 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3196 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | maroonrk | 153 |
| 2 | Qingyu | 152 |
| 3 | nik_exists | 151 |
| 4 | AmShZ | 147 |
| 4 | maspy | 147 |
| 6 | Um_nik | 143 |
| 7 | Errichto | 139 |
| 8 | adamant | 136 |
| 9 | BledDest | 134 |
| 10 | AksLolCoding | 132 |
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.
| Name |
|---|



Priority Queue and Set's
Inserthas 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.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.
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.
it's possible to write PQ dijkstra without unnecessary nodes 169797615