-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathprimsAlgoCppEfficient
66 lines (54 loc) · 1.57 KB
/
primsAlgoCppEfficient
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
#include<bits/stdc++.h>
using namespace std;
int main(){
int N,m;
cin >> N >> m;
vector<pair<int,int> > adj[N];
int a,b,wt;
for(int i = 0; i<m ; i++){
cin >> a >> b >> wt;
adj[a].push_back(make_pair(b,wt));
adj[b].push_back(make_pair(a,wt));
}
int parent[N];
int key[N];
bool mstSet[N];
for (int i = 0; i < N; i++)
key[i] = INT_MAX, mstSet[i] = false;
priority_queue< pair<int,int>, vector <pair<int,int>> , greater<pair<int,int>> > pq;
key[0] = 0;
parent[0] = -1;
pq.push({0, 0});
// Run the loop till all the nodes have been visited
// because in the brute code we checked for mstSet[node] == false while computing the minimum
// but here we simply take the minimal from the priority queue, so a lot of times a node might be taken twice
// hence its better to keep running till all the nodes have been taken.
// try the following case:
// Credits: Srejan Bera
// 6 7
// 0 1 5
// 0 2 10
// 0 3 100
// 1 3 50
// 1 4 200
// 3 4 250
// 4 5 50
while(!pq.empty())
{
int u = pq.top().second;
pq.pop();
mstSet[u] = true;
for (auto it : adj[u]) {
int v = it.first;
int weight = it.second;
if (mstSet[v] == false && weight < key[v]) {
parent[v] = u;
key[v] = weight;
pq.push({key[v], v});
}
}
}
for (int i = 1; i < N; i++)
cout << parent[i] << " - " << i <<" \n";
return 0;
}