Hi codeforces
Do you know how to optimize Dijkstra Algorithm to O(n)? and is it posible?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
Hi codeforces
Do you know how to optimize Dijkstra Algorithm to O(n)? and is it posible?
Name |
---|
its pretty easy. proof:
let v = # of vertices.
let e = # of edges.
then, let n = V + ElogV. this gets us a O(n) time complexity. q.e.d
Not bad, but TL say "it is nlog"
We can optimize it using data structures in $$$\mathcal{O}((V + E)\log V)$$$
as an example we can use a Fibonacci Heap as follows:-
Replace the binary heap with a Fibonacci Heap for the priority queue. Fibonacci Heaps can decrease the time complexity of Dijkstra's algorithm to $$$\mathcal{O}((V + E)\log V)$$$. They have better amortized time complexity for decrease-key operations, which are common in Dijkstra's algorithm.
UPD: as I know : No We Can't Optimize, even it's kind of wired thing to only use $$$E$$$ and ignore $$$V$$$ and vice versa.
I know this , and I want to know : can we do this like O(E)
Fibonacci heaps have a very large constant factor so for the purposes of competitive programming they are rarely faster than priority queues.
You're right sometimes it'll give very large constant factor , so alternative optimization is merging Buckets
In practice, you can achieve efficient results by combining Dijkstra's algorithm with a bucket-based data structure. Divide the vertices into buckets based on their distance from the source. Process vertices in a bucket in non-decreasing order of distance. This reduces the number of vertices to consider in each iteration.
which will reduce the storage , still The complexity $$$\mathcal{O}((V+E)\log(V))$$$
I heard that people could solve it in O(n) time if the edge weights are all integers.
I read your question wrong
It is in fact possible to find the publication for it: https://sci-hub.et-fine.com/10.1145/316542.316548
There is a theoretical linear-time algorithm for undirected SSSP with integer weights, yes: Thorup, 1999.
As you might expect, though, it's very complicated to implement in practice, still slower than the usual Dijkstra in typical cases, and the current word size we have in modern computers is still too small to make the algorithm perfectly linear (current implementations make some compensations to this and modified the algorithm in several places).
Just use bitsets bro