-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataset.cpp
More file actions
392 lines (345 loc) · 11 KB
/
Dataset.cpp
File metadata and controls
392 lines (345 loc) · 11 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "Dataset.h"
#include <algorithm>
#include <math.h>
#include <time.h>
#include <limits.h>
#include <fstream>
using namespace std;
// This constructor reads 2d-datapoints from file
Dataset::Dataset(string fname, int n) {
N = n;
K = sqrt(n);
clusters.resize(K);
ifstream in;
in.open(fname);
double x, y;
Point p;
while (!in.eof()) {
in >> x >> y;
datapoints.push_back(Point(x, y));
}
in.close();
}
// This function checks all datapoints that which cluster it should belong based on Distance Formula
void Dataset::assignPoints() {
for (int i = 0; i < N; i++) {
double mindist = INT_MAX;
for (int j = 0; j < K; j++) {
double dist = datapoints[i].calDistance(clusters[j].centeriod);
if (dist < mindist) {
mindist = dist;
datapoints[i].clusterID = j + 1;
}
}
}
}
// This function checks Old and New centeriods
bool Dataset::compareCenters(vector<Point>& Newcenters) {
for (int i = 0; i < K; i++) {
if (clusters[i].centeriod != Newcenters[i])
return false;
}
return true;
}
// This function applies Kmeans to Dataset and make clusters
void Dataset::Kmeans() {
vector<Point> centers;
while (centers.size() < K)
{
int rid = rand() % N;
if (std::find(centers.begin(), centers.end(), datapoints[rid]) == centers.end())
{
centers.push_back(datapoints[rid]);
}
}
for (int i = 0; i < centers.size(); i++) {
clusters[i].centeriod = centers[i];
}
while (true)
{
//assigning points to clusters
assignPoints();
cout << "Centeriods:" << endl;
for (int i = 0; i < centers.size(); i++) {
cout << clusters[i].centeriod << endl;
}
cout << endl;
cout << "-----Points with ClusterID-----" << endl;
for (int i = 0; i < N; i++) {
cout << datapoints[i] << ": ID = " << datapoints[i].clusterID << endl;
}
cout << endl;
//recompute means of centeriods
vector<double> total(K, 0);
vector<double> sumx(K, 0);
vector<double> sumy(K, 0);
for (int i = 0; i < N; i++)
{
int cID = datapoints[i].clusterID;
total[cID - 1]++;
sumx[cID - 1] += datapoints[i].x;
sumy[cID - 1] += datapoints[i].y;
}
//assign new centers
vector<Point> Newcenters(K);
for (int i = 0; i < K; i++)
{
Point temp;
temp.x = sumx[i] / total[i];
temp.y = sumy[i] / total[i];
Newcenters[i] = temp;
}
// check if there is change in centeriods
if (compareCenters(Newcenters) == 1) {
break;
}
else {
for (int i = 0; i < K; i++)
clusters[i].centeriod = Newcenters[i];
}
}
// Assigning datapoints to each cluster
for (int i = 0; i < N; i++) {
int ind = datapoints[i].clusterID;
clusters[ind - 1].clusterpoints.push_back(datapoints[i]);
}
cout << "Centeriods:" << endl;
for (int i = 0; i < K; i++) {
cout << clusters[i].centeriod << endl;
}
cout << endl;
}
// This function makes MST of each clusters by calling function from cluster struct
void Dataset::makeMST() {
for (int i = 0; i < K; i++) {
clusters[i].makeClusterMST();
cout << "MST of ClusterID : " << i + 1 << endl;
clusters[i].printMST();
cout << endl;
}
}
// Divide and Conquer Using K-means
void Dataset::DAC() {
Kmeans();
makeMST();
}
// This function calculates the midpoints of MST of centers
Point Dataset::calMSTCentralMidPoint(Point p1, Point p2) {
Point p;
p.x = (p1.x + p2.x) / 2;
p.y = (p1.y + p2.y) / 2;
return p;
}
// Function to find min weight vertex
int Dataset::findMinWeightVertex(vector<bool>& visited, vector<double>& key, int size) {
int mindist = INT_MAX, index;
for (int i = 0; i < size; i++) {
if (key[i] < mindist && visited[i] == false) {
mindist = key[i];
index = i;
}
}
return index;
}
// This function is Combine Algorithm function that combines the MSTs of clusters based on MST of center
void Dataset::CA(vector<ConnectingEdge>& MST)
{
for (int i = 0; i < K; i++)
{
clusters[i].centeriod.clusterID = i + 1;
}
// Making a graph matrix for the representation of edges
vector<vector<double>> graphMatrix;
for (int i = 0; i < K; i++) {
graphMatrix.push_back(vector<double>());
for (int j = 0; j < K; j++) {
graphMatrix[i].push_back(clusters[i].centeriod.calDistance(clusters[j].centeriod));
}
}
// Apply Prims Algorithm to find MST of centers
MSTCentral.clear();
MSTCentral.resize(K);
vector<double> key(K);
vector<bool> visited(K);
for (int i = 0; i < K; i++) {
key[i] = INT_MAX;
visited[i] = false;
}
key[0] = 0;
MSTCentral[0] = Point(INT_MIN, INT_MIN);
for (int i = 0; i < K - 1; i++) {
int m = findMinWeightVertex(visited, key, K);
visited[m] = true;
for (int n = 0; n < K; n++) {
if (graphMatrix[m][n] != 0 && visited[n] == false && graphMatrix[m][n] < key[n]) {
MSTCentral[n] = clusters[m].centeriod;
key[n] = graphMatrix[m][n];
}
}
}
printMSTCentral();
cout << endl;
// Finding the edge between two MSTs
ConnectEdges.clear();
for (int i = 1; i < K; i++)
{
ConnectEdges.push_back(DCE(MSTCentral[i], clusters[i].centeriod));
}
// Combining the MSTs of all Clusters
MST.resize(N);
int n = 1;
for (int i = 0; i < K; i++)
{
for (int j = 1; j < clusters[i].MST.size(); j++) {
MST[n].PointA = clusters[i].MST[j];
MST[n].PointB = clusters[i].clusterpoints[j];
n++;
}
}
for (int i = 0; i < ConnectEdges.size(); i++) {
MST[n].PointA = ConnectEdges[i].PointA;
MST[n].PointB = ConnectEdges[i].PointB;
n++;
}
}
// This function Detects the Connecting Edge between two MSTs
ConnectingEdge Dataset::DCE(Point p1, Point p2)
{
int min = INT_MAX;
Point a, b;
// finding point a in 1st Cluster that is closest to centeriod of 2nd Cluster
for (int i = 0; i < datapoints.size(); i++) {
if (datapoints[i].clusterID == p2.clusterID) {
double dist = datapoints[i].calDistance(p1);
if (dist < min) {
min = dist;
b = datapoints[i];
}
}
}
// finding point b in 2nd Cluster that is closest to centeriod of 1st Cluster
min = INT_MAX;
for (int i = 0; i < datapoints.size(); i++) {
if (datapoints[i].clusterID == p1.clusterID) {
double dist = datapoints[i].calDistance(p2);
if (dist < min) {
min = dist;
a = datapoints[i];
}
}
}
ConnectingEdge Edge;
Edge.PointA = a;
Edge.PointB = b;
return Edge;
}
// This function finds Secondary Approximate MST
void Dataset::SAM() {
vector<Point> newcenters;
for (int i = 1; i < K; i++) {
newcenters.push_back(calMSTCentralMidPoint(MSTCentral[i], clusters[i].centeriod));
}
// Partitioning the Dataset based on Refinement Stage
clusters.clear();
clusters.resize(K - 1);
cout << "Centeriods:" << endl;
for (int i = 0; i < clusters.size(); i++) {
clusters[i].centeriod = newcenters[i];
cout << clusters[i].centeriod << endl;
}
cout << endl;
for (int i = 0; i < datapoints.size(); i++) {
datapoints[i].clusterID = 0;
}
// Assiging Points to new clusters
this->K = K - 1;
assignPoints();
for (int i = 0; i < N; i++) {
int ind = datapoints[i].clusterID;
clusters[ind - 1].clusterpoints.push_back(datapoints[i]);
}
// Making MSTs of each cluster and combining them
cout << endl;
makeMST();
CA(MST2);
printMST2();
}
void Dataset::printMST1() {
cout << "---------MST1---------" << endl;
for (int i = 1; i < MST1.size(); i++)
{
cout << MST1[i].PointA << " - " << MST1[i].PointB << endl;
}
}
void Dataset::printMST2() {
cout << "---------MST2---------" << endl;
for (int i = 1; i < MST2.size(); i++)
{
cout << MST2[i].PointA << " - " << MST2[i].PointB << endl;
}
}
void Dataset::printFinalMST() {
cout << "---------FinalMST---------" << endl;
for (int i = 1; i < FinalMST.size(); i++)
{
cout << FinalMST[i] << " - " << datapoints[i] << endl;
}
}
void Dataset::printMSTCentral() {
cout << "-------MSTofCenters-------" << endl;
for (int i = 1; i < K; i++)
cout << MSTCentral[i].clusterID << " - " << clusters[i].centeriod.clusterID << endl;
}
// A utility function to find index of point
int Dataset::findind(Point p) {
for (int i = 0; i < datapoints.size(); i++) {
if (p.x == datapoints[i].x && p.y == datapoints[i].y)
return i;
}
return -1;
}
// Fast MST
void Dataset::FMST() {
cout << "\n-------Divide And Conquer Stage-------" << endl;
DAC();
CA(MST1);
printMST1();
cout << "\n-------Refinement Stage-------" << endl;
SAM();
// Making a graph matrix for the representation of edges
vector<vector<double>> graphMatrix(N, vector<double>(N, 0));
for (int j = 1; j < N; j++) {
int m = findind(MST1[j].PointA);
int n = findind(MST1[j].PointB);
graphMatrix[m][n] = MST1[j].PointA.calDistance(MST1[j].PointB);
graphMatrix[n][m] = MST1[j].PointA.calDistance(MST1[j].PointB);
}
for (int j = 1; j < N; j++) {
int m = findind(MST2[j].PointA);
int n = findind(MST2[j].PointB);
graphMatrix[m][n] = MST2[j].PointA.calDistance(MST2[j].PointB);
graphMatrix[n][m] = MST2[j].PointA.calDistance(MST2[j].PointB);
}
// Apply Prims Algorithm to final Approximate MST
FinalMST.resize(N);
vector<double> key(N);
vector<bool> visited(N);
for (int i = 0; i < N; i++) {
key[i] = INT_MAX;
visited[i] = false;
}
key[0] = 0;
FinalMST[0] = Point(INT_MIN, INT_MIN);
for (int i = 0; i < N - 1; i++) {
int m = findMinWeightVertex(visited, key, N);
visited[m] = true;
for (int n = 0; n < N; n++) {
if (graphMatrix[m][n] != 0 && visited[n] == false && graphMatrix[m][n] < key[n]) {
FinalMST[n] = datapoints[m];
key[n] = graphMatrix[m][n];
}
}
}
cout << endl;
printFinalMST();
}