Hi codeforces
Do you know how to optimize Dijkstra Algorithm to O(n)? and is it posible?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
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