Repository navigation
Expand file tree
/
Copy pathKruskal.cpp
More file actions
450 lines (359 loc) · 9.06 KB
/
Copy pathKruskal.cpp
File metadata and controls
450 lines (359 loc) · 9.06 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "Header.h"
template <typename T>
void DynamicArray<T>::addTail(T*& data)
{
T** tempArr = new T * [current_size + 1];
for (int i = 0; i < current_size; i++)
{
tempArr[i] = dane[i];
}
delete[] dane;
dane = tempArr;
current_size++;
dane[current_size - 1] = data;
}
template <typename T>
void DynamicArray<T>::deleteAll()
{
for (int i = 0; i < current_size; i++)
{
delete dane[i];
dane[i] = nullptr;
}
delete[] dane;
dane = nullptr;
current_size = 0;
}
template <typename T>
void DynamicArray<T>::displayAllElements()
{
for (int i = 0; i < current_size; i++)
{
std::cout << "Data at index " << i << ":" << std::endl;
std::cout << dane[i] << "\n\n";
}
}
template <typename T>
template <typename Comparator>
void DynamicArray<T>::Merge(Comparator comp, int left, int middle, int right)
{
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;
T** leftArray = new T * [n1];
T** rightArray = new T * [n2];
for (i = 0; i < n1; i++)
leftArray[i] = dane[left + i];
for (j = 0; j < n2; j++)
rightArray[j] = dane[middle + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (!comp(*leftArray[i], *rightArray[j])) {
dane[k] = leftArray[i];
i++;
}
else {
dane[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1) {
dane[k] = leftArray[i];
i++;
k++;
}
while (j < n2) {
dane[k] = rightArray[j];
j++;
k++;
}
delete[] leftArray;
delete[] rightArray;
}
template <typename T>
template <typename Comparator>
void DynamicArray<T>::MergeSortHelper(Comparator comp, int left, int right)
{
if (left < right) {
int middle = left + (right - left) / 2;
MergeSortHelper(comp, left, middle);
MergeSortHelper(comp, middle + 1, right);
Merge(comp, left, middle, right);
}
}
template <typename T>
template <typename Comparator>
void DynamicArray<T>::MergeSort(Comparator comp)
{
MergeSortHelper(comp, 0, current_size - 1);
}
template <typename T>
void DynamicArray<T>::changeData(T*& data, int index)
{
if (index < 0 || index > current_size - 1)
{
std::cout << "Index out of range" << std::endl;
return;
}
else
{
delete dane[index];
dane[index] = data;
std::cout << "Data under the current index has been changed" << std::endl;
}
}
template <typename T>
T* DynamicArray<T>::returnData(int index)
{
if (index < 0 || index > current_size - 1)
{
std::cout << "Index out of range" << std::endl;
return nullptr;
}
else
{
return dane[index];
}
}
int RandomNumber(int from, int to)
{
std::random_device rd;
std::default_random_engine e{ rd() };
std::uniform_int_distribution <unsigned> u(from, to);
return u(e);
}
int UnionFind::FindRepresentative(int NodeIndex)
{
FindOpCounter++;
if (ParentIndex[NodeIndex] == NodeIndex)
{
return NodeIndex;
}
else
return FindRepresentative(ParentIndex[NodeIndex]);
}
int UnionFind::PathCompression(int NodeIndex)
{
FindOpCounter++;
if (ParentIndex[NodeIndex] != NodeIndex)
{
ParentIndex[NodeIndex] = PathCompression(ParentIndex[NodeIndex]);
}
return ParentIndex[NodeIndex];
}
void UnionFind::CombineTwoSets(int iNode, int jNode)
{
int iRep = FindRepresentative(iNode);
int jRep = FindRepresentative(jNode);
if (iRep != jRep)
{
ParentIndex[iRep] = jRep;
}
}
void UnionFind::UnionByRank(int iNode, int jNode)
{
if (Rank[iNode] > Rank[jNode])
{
ParentIndex[jNode] = iNode;
}
else if (Rank[iNode] < Rank[jNode])
{
ParentIndex[iNode] = jNode;
}
else
{
ParentIndex[jNode] = iNode;
Rank[iNode]++;
}
}
template <typename Comparator>
DynamicArray<Edge> KruskalAlgorithmVer1(Graph graph, Comparator comp)
{
UnionFind unia = UnionFind(graph.size);
clock_t s1 = clock();
graph.Edges.MergeSort(comp);
clock_t e1 = clock();
double time1 = (e1 - s1) / (double)CLOCKS_PER_SEC;
DynamicArray<Edge> MST;
clock_t s2 = clock();
for (int i = 0; i < graph.Edges.current_size; i++)
{
int rep1 = unia.FindRepresentative(graph.Edges[i]->IndexOne);
int rep2 = unia.FindRepresentative(graph.Edges[i]->IndexTwo);
if (rep1 != rep2)
{
MST.addTail(graph.Edges[i]);
unia.CombineTwoSets(graph.Edges[i]->IndexOne, graph.Edges[i]->IndexTwo);
}
}
clock_t e2 = clock();
double time2 = (e2 - s2) / (double)CLOCKS_PER_SEC;
double suma = 0;
for (int i = 0; i < MST.current_size; i++)
{
suma += MST.dane[i]->weight;
}
std::cout << "Number of edges: " << MST.current_size << '\n';
std::cout << "Total weight: " << suma << '\n';
std::cout << "Sorting time: " << time1 << "\n";
std::cout << "Main loop time: " << time2 << "\n";
std::cout << "Number of find operations: " << unia.FindOpCounter << std::endl;
return MST;
}
template <typename Comparator>
DynamicArray<Edge> KruskalAlgorithmVer2(Graph graph, Comparator comp)
{
UnionFind unia = UnionFind(graph.size);
clock_t s1 = clock();
graph.Edges.MergeSort(comp);
clock_t e1 = clock();
double time1 = (e1 - s1) / (double)CLOCKS_PER_SEC;
DynamicArray<Edge> MST;
clock_t s2 = clock();
for (int i = 0; i < graph.Edges.current_size; i++)
{
int rep1 = unia.PathCompression(graph.Edges[i]->IndexOne);
int rep2 = unia.PathCompression(graph.Edges[i]->IndexTwo);
if (rep1 != rep2)
{
MST.addTail(graph.Edges[i]);
unia.UnionByRank(rep1, rep2);
}
}
clock_t e2 = clock();
double time2 = (e2 - s2) / (double)CLOCKS_PER_SEC;
double suma = 0.0;
for (int i = 0; i < MST.current_size; i++)
{
suma = suma + MST.dane[i]->weight;
}
std::cout << "Number of edges: " << MST.current_size << '\n';
std::cout << "Total weight: " << suma << '\n';
std::cout << "Sorting time: " << time1 << "\n";
std::cout << "Main loop time: " << time2 << "\n";
std::cout << "Number of find operations: " << unia.FindOpCounter++ << std::endl;
return MST;
}
int main()
{
//---------------------------FIRST FILE--------------------------
std::fstream example1;
example1.open("g1.txt");
Graph graph1;
std::string line;
std::getline(example1, line);
graph1.size = std::stoi(line);
for (int i = 0; i < graph1.size; i++)
{
std::string line2;
std::getline(example1, line2);
std::stringstream input_stringstream(line2);
std::string words[2];
int j = 0;
while ((j < 2) && (input_stringstream >> words[j]))
{
j++;
}
if (j == 2)
{
std::string x = words[0];
float X = std::stof(x);
std::string y = words[1];
float Y = std::stof(y);
Node* newNode = new Node(X, Y);
graph1.Nodes.addTail(newNode);
}
}
std::string line3;
std::getline(example1, line3);
int EdgesNumber = std::stoi(line3);
for (int i = 0; i < EdgesNumber; i++)
{
std::string line3;
std::getline(example1, line3);
std::stringstream input_stringstream(line3);
std::string words[3];
int j = 0;
while ((j < 3) && (input_stringstream >> words[j]))
{
j++;
}
if (j == 3)
{
std::string index1 = words[0];
int Indexone = std::stoi(index1);
std::string index2 = words[1];
int Indextwo = std::stoi(index2);
std::string weigh = words[2];
double weighN = std::stod(weigh);
Edge* newEdge = new Edge(Indexone, Indextwo, weighN);
graph1.Edges.addTail(newEdge);
}
}
EdgeCompare comp;
DynamicArray<Edge> MST1 = KruskalAlgorithmVer1(graph1, comp);
DynamicArray<Edge> MST2 = KruskalAlgorithmVer2(graph1, comp);
example1.close();
// ---------------------NEXT FILE-------------------------
std::fstream example2;
example2.open("g2.txt");
Graph graph2;
std::string lineN;
std::getline(example2, lineN);
graph2.size = std::stoi(lineN);
for (int i = 0; i < graph2.size; i++)
{
std::string lineN2;
std::getline(example2, lineN2);
std::stringstream input_stringstream(lineN2);
std::string words[2];
int j = 0;
while ((j < 2) && (input_stringstream >> words[j]))
{
j++;
}
if (j == 2)
{
std::string x = words[0];
float X = std::stof(x);
std::string y = words[1];
float Y = std::stof(y);
Node* newNode = new Node(X, Y);
graph2.Nodes.addTail(newNode);
}
}
std::string lineN3;
std::getline(example2, lineN3);
int EdgesNumber2 = std::stoi(lineN3);
for (int i = 0; i < EdgesNumber2; i++)
{
std::string lineN3;
std::getline(example2, lineN3);
std::stringstream input_stringstream(lineN3);
std::string words[3];
int j = 0;
while ((j < 3) && (input_stringstream >> words[j]))
{
j++;
}
if (j == 3)
{
std::string index1 = words[0];
int Indexone = std::stoi(index1);
std::string index2 = words[1];
int Indextwo = std::stoi(index2);
std::string weigh = words[2];
double weighN = std::stod(weigh);
Edge* newEdge = new Edge(Indexone, Indextwo, weighN);
graph2.Edges.addTail(newEdge);
}
}
EdgeCompare comp2;
DynamicArray<Edge> MSTN1 = KruskalAlgorithmVer1(graph2, comp2);
DynamicArray<Edge> MSTN2 = KruskalAlgorithmVer2(graph2, comp2);
example2.close();
return 0;
}