-
Notifications
You must be signed in to change notification settings - Fork 492
/
DijsktraSSSp.cpp
128 lines (99 loc) Β· 3.2 KB
/
DijsktraSSSp.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<bits/stdc++.h>
#include<iostream>
#include<unordered_map>
using namespace std;
//making a template to store data of different datatypes in hashmap
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){
//Lets push the edge in map using its predefined functions
m[u].push_back(make_pair(v,dist));
//If an edge is bidirectional
if(bidir){
m[v].push_back(make_pair(u,dist));
}
}
void printAdj(){
cout<<"Adj List of Given Graph : "<<endl<<endl;
//Lets print the adj list
//I will use foreach loop to iterate over each object in map using first and second
//First iterate over hashmap
for(auto j:m){
cout<<j.first<<"->";
//Second iterate over list
for(auto l:j.second){
cout<<"("<<l.first<<","<<l.second<<")";
}
cout<<endl;
}
}
void dijsktraSSSp(T src){
//Will make an Unordered map t store distance with city
map<T,int> dist;
//Set all the distance to infinity initially
for(auto j:m){ //Note here we are using m
dist[j.first]=INT_MAX;
}
//Make a set to find out node with minimum distance and set sets data in sorted order
set<pair<int,T>> s; //Every set will be having a pair
//dist of src=0
dist[src]=0;
s.insert(make_pair(0,src));
while(!s.empty()){
//This will point to the begin node always of the set
auto p= *(s.begin());
T Setnode = p.second; //Node name will always be second one as defined set earlier
int setnodeDist = p.first;
s.erase(s.begin());
//Iterator over neighbours/children of current node which is just removed
//Childpair will give pairs of a particular node
for(auto childPair:m[Setnode]){
if(setnodeDist+childPair.second< dist[childPair.first]){ //infinity we stored basically
//In the set updation of a praticular node is not possible
//We have to remove the old pair and insert a new pair to simulate updation
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]=setnodeDist+childPair.second;
s.insert(make_pair(dist[dest],dest));
}
}
}
cout<<"After Traversal : "<<endl;
//Lets print distance to all other nodes from source
for(auto d:dist){
cout<<d.first<<" is located at "<<d.second<<endl;
}
}
};
int main(){
//lets make a Graph
Graph<int> g;
int edges,a,b,c;
cin>>edges;
while(edges>0){
cin>>a>>b>>c;
g.addEdge(a,b,c); //public function is defined above
//g.addEdge(1,3,4);
//g.addEdge(2,3,1);
//g.addEdge(3,4,2);
//g.addEdge(1,4,7);
edges--;
}
g.printAdj(); //To print
g.dijsktraSSSp(0);
/*Graph<string> city;
city.addEdge("Amritsar","Delhi",1);
city.addEdge("Amritsar","Jaipur",4);
city.addEdge("Amritsar","Mumbai",7);
city.addEdge("Delhi","Jaipur",1);
city.addEdge("Jaipur","Mumbai",4);
city.printAdj(); //To print
city.dijsktraSSSp("Amritsar"); */
return 0;
}