-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
790 lines (643 loc) · 30.1 KB
/
Copy pathdijkstra.cpp
File metadata and controls
790 lines (643 loc) · 30.1 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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
#include <bits/stdc++.h>
#include <fstream>
#include <string>
#include <chrono>
#include <windows.h>
#include <psapi.h> //to get memory info
using namespace std;
string printMemoryUsage() {
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;
return "Memória utilizada pelo processo: " + to_string(virtualMemUsedByMe / 1024) + " KB\n";
}
/* Creating node structure */
struct node {
int vertex;
float weight;
node* back;
node* next;
};
/* Creating graph structure */
struct graph {
/*Getting graph type [ matrix | list ]*/
// if graph_type == 1 ~> List
// if graph_type == 0 ~> Matrix
bool graph_type = 1;
/*Edges pairs*/
vector <vector <float>> graph_edges;
/*graph's info*/
int n = 0; //number of vectors
int m = 0; //number of edges
int G_min = 0, G_max = 0, Medi_g = 0; //maximum, minimum, medium and median of the degrees
double G_med = 0;
double dt = 0; //execution time to create the structure. Only not 0 when start() executed
int diam = -1;
string mem_graph;
/*Creating the basics structures*/
vector <vector <bool>> matrix; // matrix
vector <vector <int>> CC; // conected components
vector <vector <int>> sizesCC; //sizes of each CC
int quantCC = 0; // quantity of CC
vector <int> G_list; //getting the degrees of each vertex
vector <node*> Linklist; //creating the linked-list
vector <node*> TailLL; //creating a vector to keep all the last itens of the linked list
vector<vector<float>> weight_matrix; // matriz de pesos
float inf = numeric_limits<float>::infinity(); //creating the "number" with infinit value
//-----------------------------------------------------------------------------------------------------------------------
/*Executing the other functions to work properly*/
graph(const vector<vector<float>>& edges, int num_vertex, int num_edges, bool gt = 1){
graph_edges = edges;
n = num_vertex;
m = (int)graph_edges.size();
graph_type = gt;
//As soon as the structure graph is called, all these functions are also called
start();
cout << "Start ok\n";
getinfo();
cout << "getinfo ok\n";
/*
ConctComp();
cout << "CC ok\n";
diameter(true);
cout << "diameter ok\n";*/
}
//-----------------------------------------------------------------------------------------------------------------------
/* Starting the graph */
void start() {
if (graph_type) {
//initiating the G_list
for (int i=0; i<=n; i++) {
G_list.push_back(0); //adding a vertex in teh degree's list
node* aux = new node;
aux->vertex = i;
aux->next = nullptr;
aux->back = nullptr;
Linklist.push_back(aux);
TailLL.push_back(aux);
}
//placing all the edges
for (auto item : graph_edges){
int a = item[0], b = item[1];
float w = item[2]; //including weight
// creating edge a -> b
node* auxA = new node;
auxA->vertex = b;
auxA->next = Linklist[a];
auxA->weight = w;
if (Linklist[a] != nullptr) Linklist[a]->back = auxA;
Linklist[a] = auxA;
// creating edge b -> a
node* auxB = new node;
auxB->vertex = a;
auxB->next = Linklist[b];
auxB->weight = w;
if (Linklist[b] != nullptr) Linklist[b]->back = auxB;
Linklist[b] = auxB;
// adding a degree to a and b
G_list[a]++;
G_list[b]++;
}
mem_graph = printMemoryUsage();
} else {
/*matrix estructure*/
/*creating marix nxn with 0's*/
for (int i=0; i<=n; i++){
vector <bool> support;
vector <float> support_weigth;
for (int j=0; j<=n; j++){support.push_back(0); support_weigth.push_back(0);}
matrix.push_back(support);
weight_matrix.push_back(support_weigth);
G_list.push_back(0);
}
/*Placing edges*/
for (auto item : graph_edges){
matrix[item[0]][item[1]] = 1;
matrix[item[1]][item[0]] = 1;
weight_matrix[item[0]][item[1]] = item[2];
weight_matrix[item[1]][item[0]] = item[2];
G_list[item[0]] += 1;
G_list[item[1]] += 1;
}
mem_graph = printMemoryUsage();
}
}
//-----------------------------------------------------------------------------------------------------------------------
/*Getting all the information needed*/
void getinfo() {
G_min = n; //seting G_min for the max value (the biggest degree a vertex can have is n-1, that's why I settle it n)
for (int i=1; i<=n; i++){
double value = G_list[i];
if (value < G_min) G_min = value; //getting lowest degree
if (value > G_max) G_max = value; //getting highest degree
G_med += value;
}
G_med = G_med / (double) n; //getting the medium degree
//creating a copy of G_list to find the median
vector <int> Copy_G_list;
for (int i=0; i<n; i++) {Copy_G_list.push_back(G_list[i]);}
sort(Copy_G_list.begin(), Copy_G_list.end()); //sorting the copy list
//getting the median
if (n % 2 == 1) {Medi_g = Copy_G_list[(n/2)+1];} //if the number of vertexes are even
else {Medi_g = (Copy_G_list[(n/2)-1] + Copy_G_list[n/2]) / 2;} //if the number of vertexes are odd
}
//-----------------------------------------------------------------------------------------------------------------------
/*Implementing Dijkstra - With vectors*/
//-----------------------------------------------------------------------------------------------------------------------
/*Implementing Dijkstra - With Heap*/
//-----------------------------------------------------------------------------------------------------------------------
/*Implementing BFS*/
vector <vector <int>> BFS(int s, bool diam_detect = false){
if (graph_type){
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
queue <int> Q; //creating the queue for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
visit_stats[s] = 1; //marking s as visited
Q.push(s); //placing s in the queue
while (Q.size() > 0){ //While there is any item on the queue
int v = Q.front(); //getting the head
Q.pop(); //deleting the head
node* aux = new node; aux = Linklist[v]; //creating a auxiliar node
while (aux != nullptr) {
int v_aux = aux->vertex;
if (!visit_stats[v_aux]) {
visit_stats[v_aux] = 1;
parent[v_aux] = v;
level[v_aux] = level[v] + 1;
Q.push(v_aux);
}
aux = aux->next;
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> aux = {parent[i], level[i]};
ret.push_back(aux);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("BFS", ret, diam_detect, dt);
return ret;
} else {
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
queue <int> Q; //creating the queue for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
visit_stats[s] = 1; //marking s as visited
Q.push(s); //placing s in the queue
while (Q.size() > 0){ //while there is any item on the queue
int v = Q.front(); //getting the head
Q.pop(); //deleting the head
for (int i=1; i<=n; i++){ //the matrix representation uses matrix[v][i] to say if i is a neighbor of v
if (matrix[v][i] != 0){ //if they are neighbors
if (visit_stats[i] == 0){ //if not visited yet
visit_stats[i] = 1; //mark as visited
parent[i] = v; //getting parent
level[i] = level[v] + 1; //setting level
Q.push(i); //placing the neighbor in the queue
}
}
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> aux = {parent[i], level[i]};
ret.push_back(aux);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("BFS", ret, diam_detect, dt);
return ret;
}
}
//-----------------------------------------------------------------------------------------------------------------------
/*Implementing DFS*/
vector <vector <int>> DFS(int s, bool diam_detect = false){
if (graph_type) {
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
stack <int> P; //creating the queue for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
P.push(s); //placing s in the queue
while (!P.empty()){
int v = P.top();
P.pop();
if (!visit_stats[v]){
visit_stats[v] = 1;
vector<int> neighbors;
node* aux = Linklist[v];
while (aux != nullptr) {
neighbors.push_back(aux->vertex);
aux = aux->next;
}
sort(neighbors.rbegin(), neighbors.rend());
for (int w : neighbors) {
P.push(w);
if (!visit_stats[w]) {
parent[w] = v;
level[w] = level[v] + 1;
}
}
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> aux = {parent[i], level[i]};
ret.push_back(aux);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("DFS", ret, diam_detect, dt);
return ret;
} else {
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
stack <int> P; //creating the stack for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
P.push(s); //adding s to the stack
while (!P.empty()){
int u = P.top(); //getting the highest element
P.pop(); //removing the highest element
if (visit_stats[u] == 0){ //verifying if u was already visited
visit_stats[u] = 1; //marking u as visited
for (int j=n; j>=1; j--){ //looking for the next neighbor
if (matrix[u][j] != 0){
if (visit_stats[j] == 0){ //if the neighbor wasn't visited
parent[j] = u; //setting parent
level[j] = level[u] + 1; //setting level
P.push(j); //putting neighbor in the stack
}
}
}
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> aux = {parent[i], level[i]};
ret.push_back(aux);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("DFS", ret, diam_detect, dt);
return ret;
}
}
//-----------------------------------------------------------------------------------------------------------------------
/*Getting the distance between the vertex a & b (obs: the distance between two vertex )*/
int dist(int a, int b){
vector <vector <int>> bfs_res = BFS(a, true); //creating a vector to receive the BFS values
return bfs_res[b][1];
}
//-----------------------------------------------------------------------------------------------------------------------
/*Getting the diameter of the graph*/
void diameter(bool complete = 0){
if (complete){
int big = 0; //setting the counter
for (int i = 1; i<=n; i++){
vector <vector <int>> l = BFS(1, true); //doing the BFS
for (auto j : l) {
if (j[1] > big) {big = j[1];} //finding the biggest distance
}
}
diam = big;
} else if (quantCC == 1) {
int big = 0; //setting the counter
vector <vector <int>> l = BFS(1, true); //doing the BFS
for (auto i : l) {
if (i[1] > big) {big = i[1];} //finding the biggest distance
}
int big2 = 0; //setting the counter
vector <vector <int>> l2 = BFS(big, true); //doing the BFS
for (auto i : l) {
if (i[1] > big2) {big2 = i[1];} //finding the biggest distance
}
diam = big2;
}
}
//-----------------------------------------------------------------------------------------------------------------------
/*Getting all connected components*/
void ConctComp() {
//Making atributtes empty
CC.clear();
sizesCC.clear();
quantCC = 0;
//Placing the first item because the vertex 0 doesn't exist
CC.push_back({});
sizesCC.push_back({0, 0});
//Marking first vertex as visited
vector<bool> visited(n+1, 0);
if (graph_type) {
for (int start = 1; start <= n; start++) {
if (!visited[start]) {
quantCC++;
vector<int> CC_itens;
int ctng_CC = 0;
queue<int> Q;
Q.push(start);
visited[start] = true;
while (!Q.empty()) {
int v = Q.front(); Q.pop();
CC_itens.push_back(v);
ctng_CC++;
node* aux = Linklist[v];
while (aux != nullptr) {
int w = aux->vertex;
if (!visited[w]) {
visited[w] = true;
Q.push(w);
}
aux = aux->next;
}
}
CC.push_back(CC_itens);
sizesCC.push_back({ctng_CC, quantCC});
}
}
} else {
for (int start = 1; start <= n; start++) {
if (!visited[start]) {
//Checking if found a new component
quantCC++;
CC.push_back({});
sizesCC.push_back({0, quantCC});
//creating the stack to get the Conected Components
stack<int> P;
P.push(start);
visited[start] = 1;
while (!P.empty()) { //While there is no more vertex in the CC
int u = P.top();
P.pop();
//Add u to current component
CC.back().push_back(u);
sizesCC.back()[0]++;
//Explore neighbors
for (int v = 1; v <= n; v++) {
if (matrix[u][v] != 0 && !visited[v]) {
visited[v] = 1;
P.push(v);
}
}
}
}
}
}
sort(sizesCC.begin(), sizesCC.end());
}
//-------------------------------------------------------------------------------------------------------------------------
/*Creating a function to create and/or modify a file*/
void createFile(string name, vector <vector <int>> s, bool get_diam, int t){
if (!get_diam) {
if (name == "BFS"){
ofstream testFile("bfs_output.txt", std::ios::app);
/*testFile << "BFS ~ ";
testFile << "Levels: [ ";
for (auto par : s){
testFile << par[1] << ' ';
} testFile << "] ";
testFile << "| Parents: [ ";
for (auto par : s){
testFile << par[0] << ' ';
} testFile << "]";*/
testFile << " | Runtime: " << t << "ms\n";
testFile.close();
} else {
ofstream testFile("dfs_output.txt", std::ios::app);
/*testFile << "DFS ~ ";
testFile << "Levels: [ ";
for (auto par : s){
testFile << par[1] << ' ';
} testFile << "] ";
testFile << "| Parents: [ ";
for (auto par : s){
testFile << par[0] << ' ';
} testFile << "]";*/
testFile << " | Runtime: " << t << "ms\n";
testFile.close();
}
}
}
//-------------------------------------------------------------------------------------------------------------------------
/*Creating output grafics*/
void print(){
if (graph_type) {
for (int i = 1; i <= n; i++) {
cout << i << " -> ";
node* current = Linklist[i];
while (true) {
cout << current->vertex << "(" << current->weight << ") -> ";
current = current->next;
if (current->next == nullptr) break;
}
cout << "\n";
}
} else {
for (int i = 1; i <= n;+ i++){
cout << "| ";
for (int j = 1; j <= n; j++) {
if (matrix[i][j]) {cout << weight_matrix[i][j] << " ";}
else{cout << " . " << " ";}
}
cout << " |\n";
}
}
}
int BFS_time(int s, bool diam_detect = false){
if (graph_type){
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
queue <int> Q; //creating the queue for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
visit_stats[s] = 1; //marking s as visited
Q.push(s); //placing s in the queue
while (Q.size() > 0){ //While there is any item on the queue
int v = Q.front(); //getting the head
Q.pop(); //deleting the head
node* aux = new node; aux = Linklist[v]; //creating a auxiliar node
for (int i=1; i<=G_list[v]; i++){ //for each node neighbor
int v_aux = aux->vertex; //getting vertex number
if (!visit_stats[v_aux]) { //if not visited
visit_stats[v_aux] = 1; //mark as visited
parent[v_aux] = v; //getting parent
level[v_aux] = level[v] + 1; //setting level
Q.push(v_aux); //placing in the queue
}
aux = aux->next; //getting next neighbor
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> auxl = {parent[i], level[i]};
ret.push_back(auxl);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("BFS", ret, diam_detect, dt);
return dt;
} else {
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
queue <int> Q; //creating the queue for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
visit_stats[s] = 1; //marking s as visited
Q.push(s); //placing s in the queue
while (Q.size() > 0){ //while there is any item on the queue
int v = Q.front(); //getting the head
Q.pop(); //deleting the head
for (int i=1; i<=n; i++){ //the matrix representation uses matrix[v][i] to say if i is a neighbor of v
if (matrix[v][i] != 0){ //if they are neighbors
if (visit_stats[i] == 0){ //if not visited yet
visit_stats[i] = 1; //mark as visited
parent[i] = v; //getting parent
level[i] = level[v] + 1; //setting level
Q.push(i); //placing the neighbor in the queue
}
}
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> aux = {parent[i], level[i]};
ret.push_back(aux);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("BFS", ret, diam_detect, dt);
return dt;
}
}
int DFS_time(int s, bool diam_detect = false){
if (graph_type) {
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
stack <int> P; //creating the queue for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
P.push(s); //placing s in the queue
while (!P.empty()){
int v = P.top();
P.pop();
if (!visit_stats[v]){
visit_stats[v] = 1;
vector<int> neighbors;
node* aux = Linklist[v];
while (aux != nullptr) {
neighbors.push_back(aux->vertex);
aux = aux->next;
}
sort(neighbors.rbegin(), neighbors.rend());
for (int w : neighbors) {
P.push(w);
if (!visit_stats[w]) {
parent[w] = v;
level[w] = level[v] + 1;
}
}
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> aux = {parent[i], level[i]};
ret.push_back(aux);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("DFS", ret, diam_detect, dt);
return dt;
} else {
auto start_time = chrono::high_resolution_clock::now(); //getting initial time
vector <bool> visit_stats(n+1, 0); //creating a vector to mark if the vertex was already visited
stack <int> P; //creating the stack for getting the next item to be visited
vector <int> parent(n+1, 0); //vector to register the parent of each vertex
vector <int> level(n+1, 0); //vector to register the level of each vertex
P.push(s); //adding s to the stack
while (!P.empty()){
int u = P.top(); //getting the highest element
P.pop(); //removing the highest element
if (visit_stats[u] == 0){ //verifying if u was already visited
visit_stats[u] = 1; //marking u as visited
for (int j=n; j>=1; j--){ //looking for the next neighbor
if (matrix[u][j] != 0){
if (visit_stats[j] == 0){ //if the neighbor wasn't visited
parent[j] = u; //setting parent
level[j] = level[u] + 1; //setting level
P.push(j); //putting neighbor in the stack
}
}
}
}
}
vector <vector <int>> ret;
for (int i=0; i<=n; i++){
vector <int> aux = {parent[i], level[i]};
ret.push_back(aux);
}
auto end_time = chrono::high_resolution_clock::now(); //getting ending time
chrono::duration<double,std::milli> duration = end_time - start_time;
dt = duration.count(); //em ms
createFile("DFS", ret, diam_detect, dt);
return dt;
}
}
};
//-------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------
int main() {
//opening the data file
ifstream infile("data.txt");
//getting the number of lines
int nlines; infile >> nlines;
cout << nlines << '\n';
//setting line to indentify if its weightened or not
string line;
//setting error string
string error_str = "A biblioteca ainda nao implementa caminhos minimos com pesos negativos\n";
//creating the a vector of vectors to keep all edges information
vector <vector <float>> edges;
//getting n and m
int n = nlines;
int m = 0;
//stopping point
int last1, last2;
//getting all the edges of the graph
while (getline(infile, line)){
stringstream ss(line);
float a, b; ss >> a >> b;
float w = 0;
if (ss >> w) {if (w < 0) {cout << error_str; throw;}}
if (a == last1 && b == last2){break;}
else {
vector <float> line = {a, b, w};
edges.push_back(line);
last1 = a;
last2 = b;
m++;
}
}
sort(edges.begin(), edges.end());
//closing the data file
infile.close();
//opening the output_data file
//ofstream outD("out_data.txt", std::ios::app);
graph testL(edges, n, m);
graph testM(edges, n, m, 0);
testL.print();
cout << "\n\n";
testM.print();
cout << "=================================================\n";
//outD.close();
return 0;
}