Repository navigation
Expand file tree
/
Copy pathdijkstras.cpp
More file actions
83 lines (68 loc) · 1.97 KB
/
Copy pathdijkstras.cpp
File metadata and controls
83 lines (68 loc) · 1.97 KB
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
#include<bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
const int M = 100;
const int MAXI = 100000;
vector<pp> Adj[M];
int dist[M];
priority_queue<pp, vector<pp>, greater<pp>> pq;
void init(int src){
for(int i=0;i<M;i++){
dist[i] = MAXI;
Adj[i].clear();
}
while(!pq.empty()){pq.pop();}
dist[src] = 0;
return;
}
void add_edge(int u, int v, int w){
Adj[u].push_back({v, w});
Adj[v].push_back({u, w});
return;
}
void display_dist(int V){
for(int i=0;i<V;i++){cout<<i<<"--->"<<dist[i]<<"\n";}
return;
}
void dijkstras(int src, int V){
pq.push({dist[src], src});
int counter = 1;
while(!pq.empty()){ // O(V) -> considering every vertex will be added to the path only once, when they get popped out of the heap for the first time.
int u = pq.top().second; // having a counter to make sure the loop braks after V iterations.
if(counter > V){break;}
counter++;
pq.pop(); // O(logE) =O(logV)
int w;
for(auto it=Adj[u].begin();it!=Adj[u].end();it++){ // O(Ev)
int v = it->first;
int w = it->second;
if(dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
pq.push({dist[v], v}); // O(logV)
}
}
}
return;
}
int main(){
int V=9;
init(0);
add_edge(0, 1, 4);
add_edge(0, 7, 8);
add_edge(1, 2, 8);
add_edge(1, 7, 11);
add_edge(2, 3, 7);
add_edge(2, 8, 2);
add_edge(2, 5, 4);
add_edge(3, 4, 9);
add_edge(3, 5, 14);
add_edge(4, 5, 10);
add_edge(5, 6, 2);
add_edge(6, 7, 1);
add_edge(6, 8, 6);
add_edge(7, 8, 7);
dijkstras(0, V);
display_dist(V);
return 0;
}
// Time Complexity: O(V)*(O(1)+O(logV)+O(Ev*logV)) = O(VEv*logV) = O(ElogV)