-
Notifications
You must be signed in to change notification settings - Fork 98
/
kruskals.cpp
252 lines (225 loc) · 5.43 KB
/
kruskals.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<vi> vii;
typedef deque<int> dqi;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
#define ll long long int
#define ld long double
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (ll)((x).size())
#define fr first
#define sc second
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define in(x) cin >> x
#define tc \
int t; \
cin >> t; \
while (t--)
#define no "NO" << endl
#define yes "YES" << endl
#define sp " "
#define speed \
cin.sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
int npower(int x, int n)
{
int res = 1;
while (n)
{
if (n % 2)
res = (res * x);
n /= 2;
x = (x * x);
}
return res;
}
int modpow(int x, int n, int M)
{
x = x % M;
int res = 1;
while (n)
{
if (n % 2)
{
res = (res * x) % M;
}
n /= 2;
x = (x * x) % M;
}
return res;
}
int gcd(int a, int b)
{
if (!b)
return a;
return gcd(b, a % b);
}
// Alias for edge and respective weight <<src, dest>, weight>
typedef pair<pi, int> Edge;
// Disjoint set structure <node id, subset id>
typedef pair<int, int> Disjoint;
// Creating Graph Struct
/*
* *IF BEING USED: clean() FUNCTION MUST BE USED WHEN DONE*
*
*/
struct Graph
{
int inSet(int setI);
int verts, Edg_num;
Edge *edges;
Disjoint *sets;
int n = 0;
// Constructors
Graph()
{
this->verts = 4;
this->Edg_num = 5;
this->edges = new Edge[this->Edg_num];
this->sets = new Disjoint[this->verts];
for (int i = 0; i < this->verts; i++)
{
this->sets[i].first = i;
this->sets[i].second = i;
}
add_Edge(0, 1, 5);
add_Edge(0, 2, 3);
add_Edge(0, 3, 2);
add_Edge(1, 3, 7);
add_Edge(2, 3, 2);
}
Graph(int verts, int edges)
{
this->verts = verts;
this->Edg_num = edges;
this->edges = new Edge[edges];
this->sets = new Disjoint[this->verts];
for (int i = 0; i < this->verts; i++)
{
this->sets[i].second = i;
}
}
//*****Helpers*****//
// Adds edge to graph, if attempting to add more edges than declared, edges wont be set
void add_Edge(int src, int dest, int w)
{
// If more edges than declared on creation
if (n >= Edg_num)
{
cout << "Can not add edge: {" << src << " " << dest << "}, too many edges" << endl;
return;
}
// Add edge and update
this->edges[n] = {{src, dest}, w};
n++;
}
// Utilizing selection sort to sort graph edges by weight
void sort(int size, Edge *edge)
{
int min;
for (int i = 0; i < size - 1; i++)
{
min = i;
for (int j = i + 1; j < size; j++)
{
if (edge[j].second < edge[min].second)
{
min = j;
}
}
Edge t = edge[i];
edge[i] = edge[min];
edge[min] = t;
}
}
// Cleans up memory of graph
void clean()
{
delete[] edges;
edges = NULL;
}
};
// Function Declarations
void mst(Graph g);
int main()
{
// Making default test graph
Graph x = Graph(9, 14);
x.add_Edge(0, 1, 7);
x.add_Edge(0, 7, 7);
x.add_Edge(1, 7, 11);
x.add_Edge(1, 2, 4);
x.add_Edge(7, 8, 4);
x.add_Edge(6, 7, 6);
x.add_Edge(2, 8, 3);
x.add_Edge(6, 8, 3);
x.add_Edge(2, 3, 2);
x.add_Edge(2, 5, 2);
x.add_Edge(5, 6, 1);
x.add_Edge(3, 5, 4);
x.add_Edge(3, 4, 1);
x.add_Edge(4, 5, 11);
mst(x);
x.clean();
}
// Kruskal's Algorithm
void mst(Graph g)
{
// Sorting by weight first
g.sort(g.Edg_num, g.edges);
// MST and index
int treeEdge = 0;
Edge *tree = new Edge[g.verts];
// Making trees and checking cycles
int count = 0;
while (count < g.Edg_num)
{
int sourceSet, destinationSet;
// Find if vertex subset has been merged from original subset
sourceSet = g.inSet(g.edges[count].first.first);
destinationSet = g.inSet(g.edges[count].first.second);
// If not in same subset (no cycle)
if (sourceSet != destinationSet)
{
// Add edge to minimum spanning tree
tree[treeEdge++] = g.edges[count];
// Merge subsets
g.sets[destinationSet].second = g.sets[sourceSet].second;
}
count++;
}
// Print MST
int totalW = 0;
cout << "The edges in the minimum spanning tree are:" << endl;
for (int i = 0; i < g.verts - 1; i++)
{
cout << "{" << tree[i].first.first << " " << tree[i].first.second << "} W: " << tree[i].second << endl;
totalW += tree[i].second;
}
cout << "Total weight of tree is : " << totalW << endl;
}
// Determine subset location of vertex
int Graph::inSet(int setI)
{
if (setI == sets[setI].second)
{
return setI;
}
else
{
return inSet(sets[setI].second);
}
}