-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
337 lines (257 loc) · 9.87 KB
/
main.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
#include "Timing.h"
#include "Memory.h"
#include "ListGraph.h"
#include "MatrixGraph.h"
#include "WeightedGraph.h"
#include <iostream>
#include <vector>
#include <string>
#include <climits>
#include <random>
#include <fstream>
#include <iomanip>
using namespace std;
void timeBreadthFirstSearch(UnweightedGraph& graph, int iterations);
void timeDepthFirstSearch(UnweightedGraph& graph, int iterations);
void timeEccentricity(WeightedGraph& graph, int iterations);
int caseStudy(UnweightedGraph& graph);
int weightedGraphCaseStudy(WeightedGraph& graph);
int interactiveUsage(UnweightedGraph& graph);
void getMst(WeightedGraph& graph);
void distColab(ostream& output);
int main(void) {
string path;
int executionMode;
int dataStructure;
int graphType;
cout << "Type the graph file path\n";
cin >> path;
bool isWeightedGraph = Graph::isWeightedGraph(path);
if (isWeightedGraph) {
WeightedGraph* graph;
graph = new WeightedGraph();
graph->loadGraphFromFilePath(path);
return weightedGraphCaseStudy(*graph);
}
cout << "Pick an execution mode:\n";
cout << " 1 - Case studies;\n";
cout << " 2 - Interactive library;\n";
cin >> executionMode;
cout << "Pick a structure for representing your graph:\n";
cout << " 1 - Adjacency list;\n";
cout << " 2 - Adjancecy matrix;\n";
cin >> dataStructure;
UnweightedGraph* graph;
if (dataStructure == 1) {
graph = new ListGraph();
} else if (dataStructure == 2) {
graph = new MatrixGraph();
} else {
cout << "Invalid choice";
cin.get();
return 0;
}
auto startMemory = getVirtualMemory();
bool loadedFileSuccessfully = graph->loadGraphFromFilePath(path);
auto memory = getVirtualMemory();
cout << "Memory in KB " << memory - startMemory << "\n";
if (!loadedFileSuccessfully) {
cout << "File not found";
cin.get();
return 0;
}
if (executionMode == 1) {
return caseStudy(*graph);
} else if (executionMode == 2) {
return interactiveUsage(*graph);
} else {
cout << "Invalid execution mode";
cin.get();
return 0;
}
cin.get();
return 0;
}
void timeBreadthFirstSearch(UnweightedGraph& graph, int iterations)
{
auto graphSize = graph.getGraphSize();
INIT_TIMER();
START_TIMER();
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(1, graphSize + 1); // guaranteed unbiased
for (int i = 0; i < iterations; i++)
{
auto startNode = uni(rng);
vector<int> parent(graphSize, UINT_MAX);
vector<int> level(graphSize);
graph.breadthFirstSearch(startNode, parent, level);
}
STOP_TIMER();
PRINT_TIMER("Timing BFS", iterations);
}
void timeDepthFirstSearch(UnweightedGraph& graph, int iterations)
{
auto graphSize = graph.getGraphSize();
INIT_TIMER();
START_TIMER();
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(1, graphSize + 1); // guaranteed unbiased
for (int i = 0; i < iterations; i++)
{
auto startNode = uni(rng);
vector<int> parent(graphSize, UINT_MAX);
vector<int> level(graphSize);
graph.depthFirstSearch(startNode, parent, level);
}
STOP_TIMER();
PRINT_TIMER("Timing DFS", iterations);
}
int caseStudy(UnweightedGraph& graph)
{
int initialVertices[3] = {1, 2, 3};
int targetParents[3] = {10, 20, 30};
INIT_TIMER();
timeBreadthFirstSearch(graph, 1000);
timeDepthFirstSearch(graph, 1000);
for (int j = 0; j < 3; j++) {
vector<int> graphBfsParents(graph.getGraphSize(), UINT_MAX);
vector<int> graphDfsParents(graph.getGraphSize(), UINT_MAX);
vector<int> graphBfsLevels(graph.getGraphSize());
vector<int> graphDfsLevels(graph.getGraphSize());
graph.breadthFirstSearch(initialVertices[j], graphBfsParents, graphBfsLevels);
graph.depthFirstSearch(initialVertices[j], graphDfsParents, graphDfsLevels);
for (int k = 0; k < 3; k++) {
cout << "Parent of vertex " << targetParents[k] << " on BFS spanning tree (starting at vertex ";
cout << initialVertices[j] << "): " << graphBfsParents[targetParents[k] - 1] << "\n";
cout << "Parent of vertex " << targetParents[k] << " on DFS spanning tree (starting at vertex ";
cout << initialVertices[j] << "): " << graphDfsParents[targetParents[k] - 1] << "\n";
}
}
vector<int> graphParents10(graph.getGraphSize(), UINT_MAX);
vector<int> graphLevels10(graph.getGraphSize());
vector<int> graphParents20(graph.getGraphSize(), UINT_MAX);
vector<int> graphLevels20(graph.getGraphSize());
graph.breadthFirstSearch(10, graphParents10, graphLevels10);
graph.breadthFirstSearch(20, graphParents20, graphLevels20);
cout << "Distance between vertices 10 and 20: " << graphLevels10[19] << "\n";
cout << "Distance between vertices 10 and 30: " << graphLevels10[29] << "\n";
cout << "Distance between vertices 20 and 30: " << graphLevels20[29] << "\n";
list<list<int> > connectedComponents = graph.getConnectedComponents();
unsigned int maxCounter = 0;
unsigned int minCounter = UINT_MAX;
cout << "Number of connected components: " << connectedComponents.size() << "\n";
for (auto it: connectedComponents) {
if (it.size() < minCounter) {
minCounter = it.size();
}
if (it.size() > maxCounter) {
maxCounter = it.size();
}
}
cout << "Size of the biggest connected component: " << maxCounter << "\n";
cout << "Size of the smallest connected component: " << minCounter << "\n";
START_TIMER();
auto diameter = graph.getGraphDiameter();
cout << "Graph diameter: " << diameter << "\n";
STOP_TIMER();
PRINT_TIMER("Diameter calculated", 1);
cout << "--------------------------\n\n";
cin.get();
return 0;
}
int interactiveUsage(UnweightedGraph& graph) {
cout << "Writing results to file `graph_analysis.txt`...";
ofstream output;
output.open("graph_analysis.txt");
list<list<int> > connectedComponents = graph.getConnectedComponents();
output << "Number of vertices: " << graph.getGraphSize() << "\n";
output << "Number of edges: " << graph.getGraphEdgesNumber() << "\n";
output << "Minimum degree: " << graph.getGraphMinimumDegree() << "\n";
output << "Maximum degree: " << graph.getGraphMaximumDegree() << "\n";
output << setprecision(10) << "Mean degree: " << graph.getGraphMeanDegree() << "\n";
output << "Median degree: " << graph.getGraphMedianDegree() << "\n";
output << "Number of connected components: " << connectedComponents.size() << "\n";
list<list<int> >::iterator it;
for (auto l: connectedComponents) {
output << "Connect component of size " << l.size() << "\n";
for (auto cl: l) {
output << cl << " ";
}
output << "\n";
}
output.close();
cin.get();
return 0;
}
int weightedGraphCaseStudy(WeightedGraph& graph)
{
auto dist = graph.dijkstra(1, -1);
cout << setprecision(10) << "Dist from 1 to 10: " << dist[10] << "\n";
cout << setprecision(10) << "Dist from 1 to 20: " << dist[20] << "\n";
cout << setprecision(10) << "Dist from 1 to 30: " << dist[30] << "\n";
cout << setprecision(10) << "Dist from 1 to 40: " << dist[40] << "\n";
cout << setprecision(10) << "Dist from 1 to 50: " << dist[50] << "\n";
cout << setprecision(10) << "Eccentricity of vertex 10: " << graph.getEccentricity(10) << "\n";
cout << setprecision(10) << "Eccentricity of vertex 20: " << graph.getEccentricity(20) << "\n";
cout << setprecision(10) << "Eccentricity of vertex 30: " << graph.getEccentricity(30) << "\n";
cout << setprecision(10) << "Eccentricity of vertex 40: " << graph.getEccentricity(40) << "\n";
cout << setprecision(10) << "Eccentricity of vertex 50: " << graph.getEccentricity(50) << "\n";
timeEccentricity(graph, 100);
getMst(graph);
distColab(cout);
return 0;
}
void timeEccentricity(WeightedGraph& graph, int iterations)
{
auto graphSize = graph.getGraphSize();
INIT_TIMER();
START_TIMER();
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(1, graphSize + 1); // guaranteed unbiased
for (int i = 0; i < iterations; i++)
{
auto startNode = uni(rng);
graph.getEccentricity(startNode);
}
STOP_TIMER();
PRINT_TIMER("Timing eccentricity", iterations);
}
void getMst(WeightedGraph& graph)
{
ofstream output;
output.open("mst.txt");
float weight = 0;
INIT_TIMER();
START_TIMER();
auto mst = graph.mst(1, &weight, &output);
STOP_TIMER();
PRINT_TIMER("Timing MST", 1);
cout << "MST Total weight: " << weight << endl;
}
void distColab(ostream& output)
{
output << "Distancia de colaboradores" << endl;
WeightedGraph graph;
graph.loadGraphFromFilePath("./assets/rede_colaboracao.txt");
auto labelProvider = LabelProvider("./assets/rede_colaboracao_vertices.txt");
auto dijkstra = labelProvider.getFromLabel("Edsger W. Dijkstra");
string labels[] = { "Alan M. Turing", "J. B. Kruskal", "Jon M. Kleinberg", "Éva Tardos", "Daniel R. Figueiredo" };
vector<int> previous (graph.getGraphSize());
auto dist = graph.dijkstra(dijkstra, -1, &previous);
for (auto label : labels)
{
auto other = labelProvider.getFromLabel(label);
output << label << "\n";
output << "Distancia: " << dist[other - 1] << endl;
output << "Caminho: ";
for (int node : graph.getPath(other, previous))
{
output << labelProvider.getLabel(node) << ", ";
}
output << endl;
output << endl;
}
}