-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKruskal'sAlgorithm.cpp
More file actions
175 lines (156 loc) · 4.33 KB
/
Kruskal'sAlgorithm.cpp
File metadata and controls
175 lines (156 loc) · 4.33 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// using Union By Rank and Path Compression by using DSU(Disjoint Set Union)
// to find parent in O(1) time
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fr first
#define sc second
#define pii pair<int,int>
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
const int32_t MOD = 1e9 + 7;
struct edge {
int src, dest, wt;
};
bool myCmp(edge a, edge b) {
return a.wt < b.wt;
}
void makeset(int* parent, int* rank, int n) { //cosntructing the required arrays for Disjoint Set
for (int i = 0;i < n;i++) {
parent[i] = i;
rank[i] = 0;
}
}
int getParent(int* parent, int i) {
if (parent[i] == i) return i;
return parent[i] = getParent(parent, parent[i]); //path compression
}
void solve() {
int n, e;
cin >> n >> e;
edge* arr = new edge[e];
for (int i = 0;i < e;i++) {
cin >> arr[i].src >> arr[i].dest >> arr[i].wt;
}
sort(arr, arr + e, myCmp);
edge* mst = new edge[n - 1]; //for n vertices, MST contains n-1 edges
int* parent = new int[n];
int* rank = new int[n];
makeset(parent, rank, n);
int count = 0;
int i = 0;
while (count < n - 1) {
edge currEdge = arr[i];
int src = currEdge.src;
int dest = currEdge.dest;
int srcParent = getParent(parent, src);
int destParent = getParent(parent, dest);
if (srcParent == destParent) {
i++;
continue;
}
if (rank[srcParent] > rank[destParent]) { //we directly connect to the src so, we check for srcParent and srcDest
parent[destParent] = srcParent;
}
else if (rank[srcParent] < rank[destParent]) {
parent[srcParent] = destParent;
}
else {
parent[srcParent] = destParent;
rank[destParent]++; //since both nodes' ranks are same depth/rank increases
}
mst[count] = currEdge;
count++;
i++;
}
int totalWeight = 0;
for (int i = 0;i < n - 1;i++) totalWeight += mst[i].wt;
cout << totalWeight << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
// solve();
return 0;
}
/**********************************************************************************/
/*
// using Union Find Algorithm
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fr first
#define sc second
#define pii pair<int,int>
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
const int32_t MOD = 1e9 + 7;
struct edge {
int src, dest, wt;
};
bool myCmp(edge a, edge b) {
return a.wt < b.wt;
}
int checkParent(int* parent, int i) { //checks top-most parent
while (parent[i] != i) {
i = parent[i];
}
return i;
}
// void setParent(int* parent, int dest, int src) { //updating parent[dest] by parent[src]
// if (parent[dest] == dest) {
// parent[dest] = parent[src];
// return;
// }
// setParent(parent, parent[dest], src);
// parent[dest] = parent[src];
// }
void solve() {
int n, e;
cin >> n >> e;
edge* arr = new edge[e];
for (int i = 0;i < e;i++) {
cin >> arr[i].src >> arr[i].dest >> arr[i].wt;
}
sort(arr, arr + e, myCmp);
edge* mst = new edge[n - 1]; //for n vertices, MST contains n-1 edges
int* parent = new int[n];
for (int i = 0;i < n;i++) parent[i] = i;
int count = 0;
int i = 0;
while (count < n - 1) {
edge currEdge = arr[i];
int srcParent = checkParent(parent, currEdge.src);
int destParent = checkParent(parent, currEdge.dest);
if (srcParent == destParent) {
i++;
continue;
}
parent[srcParent] = destParent;
mst[count] = currEdge;
count++;
i++;
}
int totalWeight = 0;
for (int i = 0;i < n - 1;i++) totalWeight += mst[i].wt;
cout << totalWeight << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
// solve();
return 0;
}
*/