-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkruskals_algorithm_mst.cpp
96 lines (77 loc) · 1.62 KB
/
kruskals_algorithm_mst.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
class UF{
private:
vector<int> sizes;
vector<int> nodes;
int root(int u){
int i = u;
while(nodes[i] != i){
nodes[i] = nodes[nodes[i]];
i = nodes[i];
}
return i;
}
public:
UF(int N){
sizes = vector<int>(N, 1);
nodes = vector<int>(N, 0);
for(int i = 0;i<N;i++){
nodes[i] = i;
}
}
bool connected(int u, int v){
int p = root(u);
int q = root(v);
return (p == q);
}
void unite(int u, int v){
int p = root(u);
int q = root(v);
if(sizes[p] <= sizes[q]){
nodes[p] = q;
sizes[q] += sizes[p];
}
else{
nodes[q] = p;
sizes[p] += sizes[q];
}
}
};
class Compare
{
public:
bool operator() (vector<int> edge1, vector<int> edge2)
{
return (edge1[2] > edge2[2]);
}
};
class Solution {
public:
int minimumCost(int n, vector<vector<int>>& connections) {
priority_queue<
vector<int>,
vector<vector<int>>,
Compare
> pq(connections.begin(), connections.end());
UF uf(n+1);
int cost = 0;
int mst_edge_count = 0;
while(!pq.empty() and (mst_edge_count<(n-1))){
auto edge = pq.top();
pq.pop();
int u = edge[0];
int v = edge[1];
int w = edge[2];
if(uf.connected(u,v)){
continue;
}
uf.unite(u, v);
cost += w;
mst_edge_count++;
}
int retval = -1;
if(mst_edge_count == n-1){
retval = cost;
}
return retval;
}
};