I have learnt Dijkstra's recently and couldn't implement it effectively. Can some one post your Dijkstra's algo implementation in (c or c++) using stl's. I will use it as reference to implement my code. Thanks in advance..
# | 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 | nor | 152 |
I have learnt Dijkstra's recently and couldn't implement it effectively. Can some one post your Dijkstra's algo implementation in (c or c++) using stl's. I will use it as reference to implement my code. Thanks in advance..
Name |
---|
http://e-maxx.ru/algo/dijkstra
Hello, You can try this question : EZDIJKST, it's a direct implementation of the algorithm. You can refer My Solution which uses STL set for reference.
This is my typical implementation of Dijkstra using C++11 and priority_queue: Dijkstra (this code finds the shortest path from node 1 to all other nodes)
why you use if(dis>d[u]) continue?
Because otherwise it would simply be a BFS that keeps going only when new distance would be lower than current minimum distance for the node, and that would be very slow in most cases.
http://ideone.com/xEBzsj
Hello!
First of all, I would suggest you to write your own version of the code (for testing you have 20C — Dijkstra? problem here on Codeforces), because you will learn how the algorithm works and how to use it. However I will give you my code :). Keep in mind you can find more versions of Dijkstra on geekforgeeks, e-maxx, youtube, ...
Here is my Dijkstra template: https://github.com/Sprdalo/Compete/blob/master/dijkstra.sublime-snippet
To see how to use it go to my submit for this problem which is given here.
Checked with C. Dijkstra?.
https://github.com/okwedook/olymp/blob/master/Dijkstra.cpp
This is an actually fast and memory efficient (and also veeery short) implementation. It uses a few template features I use, but you can look at template.cpp in the same repo.
Thanks for your solution, I dont know I can take the path like that. But can I ask this question
If we only need to calculate the distance without taking the path. Is this the optimized code or we can still improve it ?
Your code uses pairs to store the set. It does actually increase memory usage (I'm only storing the indices) and time usage (as you are also comparing pairs, while I do only one comparison), But you should test your code, it seems pretty neat.
What is "neat". Is it mean it is almost similar ?
It's good in my opinion.
How about prim algorithm. How do you implement without using heap of pair ?
You can use the same idea as used in my Dijkstra imple. Just store the indices of unused vertices and compare them using a custom comparator.
Ok thanks for your method
Hi! Why are we making flag false in line 16? Can you please give an example of such a case.
It's a bug:) Must be i.f instead of f.
SPyofgame In your priority queue implementation , priority_queue<pi, vpi, less > pq , In this line why you use less ? Maybe we should use greater. Isn't ?
Yup, it should be
greater
, thanks for reminding me. For some reasons I push wrong code :'(Dijkstra's (CP-Algorithms)
Take a look at the priority queue based implementation. You could probably modify it a bit to make it shorter to code.
Dijkstra's With Path Printing
I use this implementation. It is from this book.
include <bits/stdc++.h>
using namespace std;
define infinity 1<<30 //2^30
struct node{ int u; int cost; node(int _u, int _cost){ u = _u; cost = _cost; } bool operator < ( const node& p ) const { return cost > p.cost; } //Operator overloading
}; void dijstkra(int n, vectorg[], vectorcost[], int source){
} int main(){
}
I tried to submit my code of dijkstra's on codeforces C. Dijkstra? but I am running into a weird error. I have detailed my question here on stackoverflow: https://stackoverflow.com/questions/71748407/how-can-a-node-in-dijkstra-be-relaxed-after-it-has-already-been-processed?noredirect=1#comment126796300_71748407
basically, the question is, if I relax a node v, then that means there is no way that node has been processed (i.e. it hasn't been popped from the heap and we haven't found the min dist to that node yet). This means that processed[v] == true should never evaluate to true when we are relaxing the node v. But it does in the testcases of codeforces question
You comparison approach in PQ might be wrong because you are updating values used for comparison dynamically. As such it might "corrupt" whole data structure. So my advice is: store the distance and index directly in PQ, instead using dist vector.
It is just my guess though (I didn't test it)
That is not the case because if I comment the if statement my code is accepted. and it is pretty efficient also. and if you see this: https://cp-algorithms.com/graph/dijkstra_sparse.html#:~:text=version%20with%20set.-,Getting,-rid%20of%20pairs
This also suggests to improve the speed and memory by only storing the index of the vertex
If you look closely, cp-algorithms states that using just indices in a c++ priority_queue does not work. This destroys the invariants of the data structure, and after you change the distance array, it will not always give back the "real" minimum element in the priority_queue, making the algorithm now wrong. With an std::set, it is possible, because you can first remove and then reinsert the element that is affected.