-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphAlgo.cpp
516 lines (455 loc) · 10.7 KB
/
graphAlgo.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
* CS 3345 HON
* Project 3, GRAPHS
* Used compiler g++ 4.8.5 - 11 with compile command : g++ -std=c++11 -o CMLQP3 CMLQP3.cpp
* date 11 / 20 / 20
*/
//NOTE: on average takes about 5 seconds for large test files
#include <iostream>
#include <fstream>
#include <iomanip>
#include <limits.h>
#include <math.h>
#include <algorithm>
//#include <ios>
//#include <time.h>
//a function used to round numbers to 2 decimal places
float roundx(float val)
{
float value = (int)(val * 100 + .5);
return (float) value / 100;
}
//class for edges
class Edge
{
public:
int x;
int y;
double w;
//creating my edge
Edge(int x, int y, double w)
{
this->x = x;
this->y = y;
this->w = w;
}
//default constructor
Edge()
{
x = 0;
y = 0;
w = 0;
}
};
//used to compare edges in Kruskals(std::sort)
bool operator<(Edge const& a, Edge const& b)
{
if (a.w < b.w)
{
return true;
}
else if (a.w > b.w)
{
return false;
}
else
{
return a.x < b.x;
}
}
//class for disjoint sets(used for Kruskals)
class DisjointSet
{
public:
int* rank;
int* parent;
int n;
DisjointSet(int n);
int find(int u);
void graphUnion(int x, int y);
};
//constructor
DisjointSet::DisjointSet(int n)
{
this->n = n;
parent = new int[n + 1];
rank = new int[n + 1];
for (int i = 0; i <= n; i++)
{
rank[i] = 0;
parent[i] = i;
}
}
//find the parent of the input
int DisjointSet::find(int u)
{
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
// Union by rank
void DisjointSet::graphUnion(int x, int y)
{
x = find(x);
y = find(y);
if (rank[x] > rank[y])
{
parent[y] = x;
}
else
{
parent[x] = y;
}
if (rank[x] == rank[y])
{
rank[y]++;
}
}
//the graph class
class Graph
{
public:
//variables used throughout
double** adjMatrix;
int numVertices;
int** dist;
int** path;
int edgeCount;
Edge* e;
//basic utility functions
Graph(int);
~Graph();
void setEdgeCount(int);
void addEdge(int, int, double);
void display();
void transform();
//key calculating algorithms
float kruskal();
int floyd();
void printPath(int u, int v, int);
int color();
};
//constructor of graph
Graph::Graph(int numVertices)
{
this->numVertices = numVertices;
this->edgeCount = 0;
e = new Edge[(numVertices * numVertices - 1) / 2];
dist = new int* [numVertices];
path = new int* [numVertices];
adjMatrix = new double* [numVertices];
for (int i = 0; i < numVertices; i++)
{
dist[i] = new int[numVertices];
adjMatrix[i] = new double[numVertices];
path[i] = new int[numVertices];
for (int j = 0; j < numVertices; j++)
{
adjMatrix[i][j] = 0;
dist[i][j] = 0;
path[i][j] = 0;
}
}
}
//destructor of graph
Graph::~Graph()
{
for (int i = 0; i < numVertices; i++)
{
delete[] adjMatrix[i];
delete[] dist[i];
delete[] path[i];
}
delete[] dist;
delete[] path;
delete[] adjMatrix;
delete[] e;
}
//sets the edge count (used for the edge array)
void Graph::setEdgeCount(int x)
{
edgeCount = x;
}
//adds an edge to the graphwith its weight
void Graph::addEdge(int x, int y, double val)
{
adjMatrix[x][y] = adjMatrix[y][x] = val;
//adjMatrix[y][x] = val;
Edge* z = new Edge(x, y, val);
e[edgeCount] = *z;
edgeCount++;
}
//display the adjcency matrix of the graph (for debugging purposes)
void Graph::display()
{
for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
{
std::cout << adjMatrix[i][j] << " ";
}
// Newline for new row
std::cout << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
{
std::cout << dist[i][j] << " ";
}
// Newline for new row
std::cout << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
{
std::cout << path[i][j] << " ";
}
// Newline for new row
std::cout << std::endl;
}
std::cout << std::endl;
}
//transform the weights of the graphs to 1's and zero's
void Graph::transform()
{
for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
{
if (adjMatrix[i][j] != 0)
{
if (adjMatrix[i][j] == INT_MAX)
{
adjMatrix[i][j] = 0;
dist[i][j] = adjMatrix[i][j];
path[i][j] = j;
}
else
{
adjMatrix[i][j] = 1;
dist[i][j] = adjMatrix[i][j];
path[i][j] = j;
}
}
if (i == j)
{
path[i][j] = i;
}
}
}
}
// return the total weight of the MST and print the individual weights
float Graph::kruskal()
{
double minCost = 0, min = 0;
int edgeC = 0;
//sort the edges according to weight
std::sort(e, e + edgeCount);
// create disjoint sets
DisjointSet ds(numVertices);
while (edgeC < numVertices - 1)
{
for (int i = 0; i < edgeCount; i++)
{
int a = e[i].x;
int b = e[i].y;
int aSet = ds.find(a);
int bSet = ds.find(b);
//no cycle will be created
if (aSet != bSet)
{
min = e[i].w;
std::cout << a + 1 << " " << b + 1 << " " << std::fixed << std::setprecision(2) << roundx(min) << std::endl;
minCost += min;
ds.graphUnion(aSet, bSet);
edgeC++;
}
}
}
return roundx(minCost);
}
//calculates the diameter and finds calculate all paths
int Graph::floyd()
{
int n = numVertices;
int di = 0;
//basic implementation of kruskals for unweighted graph
for (int k = 0; k < n; ++k)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if ((dist[i][k] != 0) && (dist[k][j] != 0) && (i != j))
{
if ((dist[i][k] + dist[k][j] < dist[i][j]) || (dist[i][j] == 0))
{
dist[i][j] = dist[j][i] = dist[i][k] + dist[k][j];
path[i][j] = path[j][i] = path[i][k];
}
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
//graph is symettric so we only need to visit upper triangular to find max
if (i < j && i != 0)
{
break;
}
else
{
if (dist[i][j] > di)
{
di = dist[i][j];
}
if (i == 0 && i != j)
{
printPath(i, j, dist[i][j]);
}
}
}
}
return di;
}
//prints out the shortest path
void Graph::printPath(int u, int v, int d)
{
std::cout << u + 1 << " ";
while (u != v)
{
u = path[u][v];
std::cout << u + 1 << " ";
}
std::cout << d << std::endl;
}
//colors the graph and returns the number of colors used
int Graph::color()
{
int numColors = 0;
int* color = new int[numVertices];
bool* used = new bool[numVertices];
color[0] = 0;
//initialize all other vertices as empty
for (int i = 1; i < numVertices; i++)
{
color[i] = -1;
}
//initialize all colors as unused
for (int i = 0; i < numVertices; i++)
{
used[i] = false;
}
//for all other numVertices - 1 vertices
for (int i = 1; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
{
if (adjMatrix[i][j]) {
if (color[j] != -1)
used[color[j]] = true;
}
}
int a;
for (a = 0; a < numVertices; a++)
{
if (!used[a])
{
break;
}
}
color[i] = a; //assign found color in the list
for (int k = 0; k < numVertices; k++)
{
if (adjMatrix[i][k])
{
if (color[k] != -1)
{
used[color[k]] = false;
}
}
}
}
//get the number of colors used
for (int u = 0; u < numVertices; u++)
{
int j = 0;
for (j = 0; j < u; j++)
if (color[u] == color[j])
{
break;
}
if (u == j)
{
numColors++;
}
}
return numColors;
}
int main()
{
//to measure time running
/*
time_t start, end;
time(&start);
std::ios_base::sync_with_stdio(false);
*/
std::ifstream myfile;
int num;
int edgeCout = 0;
double x = 0, y = 0, radius = 0, sqrtr = 0;
myfile.open("GraphData.txt");
myfile >> num;
double* xar = new double[num];
double* yar = new double[num];
Graph g(num);
//save all x and y values to be compared later
for (int i = 0; i < num; i++)
{
myfile >> x >> y;
xar[i] = x;
yar[i] = y;
}
//get the radius
myfile >> radius;
//compare all vertices to one another
for (int i = 0; i < num; i++)
{
for (int k = 1 + i; k < num; k++)
{
sqrtr = sqrt((pow(xar[i] - xar[k], 2) + (pow(yar[i] - yar[k], 2))));
//if the distance is less than the radius, create an edge
if (sqrtr <= radius)
{
g.addEdge(i, k, sqrtr);
edgeCout++;
}
else
{
g.addEdge(i, k, INT_MAX);
edgeCout++;
}
}
}
g.setEdgeCount(edgeCout);
//what gets outputted
std::cout << g.kruskal() << std::endl;
g.transform();
std::cout << g.floyd() << std::endl;
std::cout << g.color() << std::endl;
myfile.close();
/*
time(&end);
double time_taken = double(end - start);
std::cout << "Time taken by program is : " << std::fixed
<< time_taken << std::setprecision(5);
std::cout << " sec " << std::endl;
*/
}