-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmst_serial.cc
More file actions
79 lines (72 loc) · 1.94 KB
/
mst_serial.cc
File metadata and controls
79 lines (72 loc) · 1.94 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
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
const int MAX = 1e6 + 5;
typedef pair<long long, int> PII;
bool marked[MAX];
vector <PII> adj[MAX];
vector<long long int> parent(MAX);
/**
* Prims Algorithm to find MST using Priority Queue
**/
long long prim(int x) {
priority_queue<PII, vector<PII>, greater<PII> > Q;
int y;
long long minimumCost = 0;
PII p;
// init
Q.push(make_pair(0, x));
parent[0] = -1;
while(!Q.empty()) {
// get minimum edge and corresponding node
p = Q.top();
Q.pop();
x = p.second;
long long int value = p.second;
// Check if this node is added
if(marked[x] == true)
continue;
// add edge and node to mst
minimumCost += p.first;
marked[x] = true;
// update neighbours for the node
for(int i = 0;i < adj[x].size();++i) {
y = adj[x][i].second;
if(marked[y] == false){
parent[adj[x][i].second] = value;
Q.push(adj[x][i]);
}
}
}
return minimumCost;
}
int main() {
// inputs and init
int nodes, edges, x, y;
long long weight, minimumCost;
cin >> nodes >> edges;
for(int i = 0;i < edges;++i) {
cin >> x >> y >> weight;
adj[x].push_back(make_pair(weight, y));
adj[y].push_back(make_pair(weight, x));
}
// Check execution time
clock_t begin = clock();
// get sum of edges in MST
minimumCost = prim(0);
// Stop
clock_t end = clock();
// Parent of nodes in MST
// for(long long int i = 0; i < nodes; ++i){
// cout<<i<<"-"<<parent[i]<<"\n";
// }
// Result
cout << "Sum of Edges in MST: " << minimumCost << endl;
double elapsed_time = double(end - begin) / CLOCKS_PER_SEC;
std::cout<<"Execution time: "<<elapsed_time<<"\n";
return 0;
}