-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDijkstra's Algorithm.cpp
77 lines (72 loc) · 2.04 KB
/
Dijkstra's Algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
#include<list>
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class Graph{
unordered_map<T, list<pair<T, int >>> m;
public:
void addEdge(T u, T v, int dist, bool bidir = true){
m[u].push_back(make_pair(v,dist));
if(bidir){
m[v].push_back(make_pair(u,dist));
}
}
void printAdj(){
for(auto j:m){
cout<<j.first<<"->";
for(auto l:j.second){
cout<<"("<<l.first<<","<<l.second<<")";
}
cout<<endl;
}
}
void dijsktraSSSP(T src){
unordered_map<T, int> dist;
// set all distance to inifinity
for(auto j:m){
dist[j.first] = INT_MAX;
}
// make a set to find out node with the min distance
set<pair<int,T>>s;
dist[src]=0;
s.insert(make_pair(0,src));
while(!s.empty()){
// find the pair at the front
auto p = *(s.begin());
T node = p.second;
int nodeDist = p.first;
s.erase(s.begin());
// iterate over nbr of the current node
for(auto childPair : m[node]){
if(nodeDist + childPair.second < dist[childPair.first]){
T dest = childPair.first;
auto f = s.find(make_pair(dist[dest],dest));
if(f != s.end()){
s.erase(f);
}
// insert the new pair
dist[dest] = nodeDist + childPair.second;
s.insert(make_pair(dist[dest],dest));
}
}
}
// lets print distance to all other node from src
for(auto d:dist){
cout<<d.first<<" is located at distance of "<<d.second<<endl;
}
}
};
int main() {
Graph<int>g;
g.addEdge(1,2,1);
g.addEdge(1,3,4);
g.addEdge(2,3,1);
g.addEdge(3,4,2);
g.addEdge(1,4,7);
g.dijsktraSSSP(1);
return 0;
}