8000 Add details on no-decrease-key Dijkstra's algorithm · cp-algorithms/cp-algorithms@13ca02d · GitHub
[go: up one dir, main page]

Skip to content

Commit 13ca02d

Browse files
jxuadamant-pwn
authored andcommitted
Add details on no-decrease-key Dijkstra's algorithm
Resolves #994
1 parent 230b5c2 commit 13ca02d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/graph/dijkstra_sparse.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ A vertex with the smallest distance gets extracted, and for each successful rela
8686
8787
### priority_queue
8888
89-
The main difference to the implementation with `set` is that we cannot remove elements from the `priority_queue` (although heaps can support that operation in theory).
90-
Therefore we have to cheat a little bit.
89+
The main difference to the implementation with `set` is that in many languages, including C++, we cannot remove elements from the `priority_queue` (although heaps can support that operation in theory).
90+
Therefore we have to use a workaround:
9191
We simply don't delete the old pair from the queue.
9292
As a result a vertex can appear multiple times with different distance in the queue at the same time.
9393
Among these pairs we are only interested in the pairs where the first element is equal to the corresponding value in $d[]$, all the other pairs are old.
@@ -135,6 +135,8 @@ void dijkstra(int s, vector<int> & d, vector<int> & p) {
135135

136136
In practice the `priority_queue` version is a little bit faster than the version with `set`.
137137

138+
Interestingly, a [2007 technical report](https://www3.cs.stonybrook.edu/~rezaul/papers/TR-07-54.pdf) concluded the variant of the algorithm not using decrease-key operations ran faster than the decrease-key variant, with a greater performance gap for sparse graphs.
139+
138140
### Getting rid of pairs
139141

140142
You can improve the performance a little bit more if you don't store pairs in the containers, but only the vertex indices.

0 commit comments

Comments
 (0)
0