-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht2.c
1332 lines (1068 loc) · 33.8 KB
/
t2.c
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/******************
* CONSOLE COLORS *
******************/
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define RESET "\x1b[0m"
#define BRIGHT_RED "\x1b[91m"
#define BRIGHT_GREEN "\x1b[92m"
#define BRIGHT_YELLOW "\x1b[93m"
#define BRIGHT_BLUE "\x1b[94m"
#define BRIGHT_MAGENTA "\x1b[95m"
#define BRIGHT_CYAN "\x1b[96m"
#define BOLD "\x1b[1m"
#define BOLD_OFF "\x1b[21m"
#define FAINT "\x1b[2m"
#define FAINT_OFF "\x1b[22m"
#define ITALIC "\x1b[3m"
#define ITALIC_OFF "\x1b[23m"
#define UNDERLINE "\x1b[4m"
#define UNDERLINE_OFF "\x1b[24m"
#define REVERSE "\x1b[7m"
#define REVERSE_OFF "\x1b[27m"
/*
* FIXME: Update console output colors
*
* BLUE: values
* WHITE: labels
* GREEN: good-result
* RED: bad-result
* MAGENTA: actions
* YELLOW: information
*
* BOLD: header
* UNDERLINE: tree-root label
* REVERSE: critical
*/
// Verificar se existe outra exigencia
// BUG: Atualizar peso das arestas do novo vertice, deve levar em consideracao o caminho forward e reverse: https://i.imgur.com/vMrwMOY.png
// FIXME: Unify graph printing code
// FIXME: Improve verbose debugging
/*************************
* STRUCTURE DEFINITIONS *
*************************/
typedef struct vector_ {
int size;
int count;
void **data;
} vector;
typedef struct edge_ {
int destination;
float weight;
} edge;
typedef struct vertex_ {
int id;
float latitude;
float longitude;
int edge_count;
edge *edges;
int origins_count;
struct vertex_ **origins;
} vertex;
/********************
* GLOBAL VARIABLES *
********************/
// File paths
char *input_path;
char *compressed_graph_path;
char *compressed_paths_path;
// File handles
FILE *input_file;
FILE *compressed_output_file;
FILE *compresed_paths_file;
// FIXME: deprecated variable
int total_vertex_count;
// Compression stats
int wiped_vertices = 0;
int wiped_edges = 0;
// The main graph
vector *vertices;
// List of paths found
vector *one_way_paths_found;
vector *two_way_paths_found;
/********************
* VECTOR FUNCTIONS *
********************/
/***
* Initializes vector `v`
*
* @param v
*/
void vector_init(vector *v) {
v->data = NULL;
v->size = 0;
v->count = 0;
}
/***
* Adds new pointer to vector
*
* @param v - vector to hold data
* @param e - pointer to be added
*/
void vector_add(vector *v, void *e) {
if (v->size == 0) {
v->size = 10;
v->data = malloc(sizeof(void *) * v->size);
memset(v->data, '\0', sizeof(void) * v->size);
}
// Reallocate when last slot is used
if (v->size == v->count) {
v->size *= 2;
v->data = realloc(v->data, sizeof(void *) * v->size);
}
v->data[v->count] = e;
v->count++;
}
/***
* Set value in specific position inside vector
*
* @param v - vector to modify
* @param index - index of pointer to replace
* @param e - replacement pointer
*/
void vector_set(vector *v, int index, void *e) {
if (index >= v->count) {
return;
}
v->data[index] = e;
}
/***
* Returns pointer from `index`
*
* @param v - vector to access
* @param index - index of pointer
* @return pointer from index `index` inside `v`
*/
void *vector_get(vector *v, int index) {
if (index >= v->count) {
return NULL;
}
return v->data[index];
}
/**
* Removes pointer from index `index`
*
* @param v - vector to modify
* @param index - position of pointer to delete
*/
void vector_delete(vector *v, int index) {
if (index >= v->count) {
return;
}
v->data[index] = NULL;
int i, j;
void **newarr = (void **) malloc(sizeof(void *) * v->count * 2);
for (i = 0, j = 0; i < v->count; i++) {
if (v->data[i] != NULL) {
newarr[j] = v->data[i];
j++;
}
}
free(v->data);
v->data = newarr;
v->count--;
}
/**
* Free memory allocated to vecto data
*
* @param v - vector to free
*/
void vector_free(vector *v) {
free(v->data);
}
/******************
* PATH FUNCTIONS *
******************/
/**
* Checks if vertex with ID `id` is in `path`
*
* @param id - vertex ID to be checked
* @param path - list of vertices forming a path
* @return if `id` is in `path`
*/
bool in_path(int id, vector *path) {
vertex *v;
for (int i = 0; i < path->count; ++i) {
v = vector_get(path, i);
if (v->id == id) {
return true;
}
}
return false;
}
/**
* Checks if vertex with ID `id` is in `path`
*
* @param needle - vertex to be checked
* @param path - list of vertices forming a path
* @return if `needle` is in `path`
*/
bool in_path_vert(vertex *needle, vector *path) {
return in_path(needle->id, path);
}
/****************
* INDEX AND ID *
***************/
/***
* Convert ID to array index
*
* @param id - id to be converted
* @return array index associated with `id`
*/
int id_to_index(int id) {
return id - 1;
}
/**
* Convert array index to vertex ID
*
* @param index - index to be converted
* @return id associated with array index `index`
*/
int index_to_id(int index) {
return index + 1;
}
/********************
* VERTEX FUNCTIONS *
********************/
/***
* Gets vertex with ID `id`
*
* @param id - ID to be returned
* @return vertex with ID `id`
*/
vertex *get_vertex_by_id(int id) {
return vector_get(vertices, id_to_index(id));
}
/***
* Check and returns `edge` from `origin` to `destination`
*
* @param origin - vertex ID
* @param destination - vertex ID
* @return edge from `origin` to `destination`
*/
edge *vertex_has_destination(int origin, int destination) {
vertex *v = get_vertex_by_id(origin);
for (int i = 0; i < v->edge_count; i++) {
v = get_vertex_by_id(origin);
if (v->edges[i].destination == destination) {
return &v->edges[i];
}
}
return NULL;
}
/***
* Check and returns `edge` from `origin` to `destination`
*
* @param origin - vertex ID
* @param destination - vertex ID
* @return edge from `origin` to `destination`
*/
edge *vertex_has_destination_vert(vertex *origin, vertex *destination) {
return vertex_has_destination(origin->id, destination->id);
}
/**
* Check if vertex with ID `v` has another vertex, with ID `origin`, pointing to it.
*
* @param v - vertex ID to check
* @param origin - origin to be checked
* @return if `origin` points to `v`
*/
bool vertex_has_origin(int v, int origin) {
vertex *v1 = get_vertex_by_id(v);
for (int i = 0; i < v1->origins_count; ++i) {
if (v1->origins[i]->id == origin) {
return true;
}
}
return false;
}
/**
* Check if vertex `v` has vertex `origin`, pointing to it.
*
* @param v - vertex to check
* @param origin - origin to be checked
* @return if `origin` points to `v`
*/
bool vertex_has_origin_vert(vertex *v, vertex *origin) {
return vertex_has_origin(v->id, origin->id);
}
/******************
* PATH FUNCTIONS *
******************/
/**
* Check if paths are equal
*
* @param path1 - path a
* @param path2 - path b
* @return if path `a` == `b`
*/
bool path_equals(vector *path1, vector *path2) {
vertex *v;
for (int i = 0; i < path1->count; ++i) {
v = vector_get(path1, i);
if (!in_path(v->id, path2)) {
return false;
}
}
return true;
}
/**
* If a list of paths, contains a path
*
* @param path1 - path to be checked
* @param list - list of paths
* @return if `path1` is in `list`
*/
bool path_exists(vector *path1, vector *list) {
vector *path2;
for (int i = 0; i < list->count; ++i) {
path2 = vector_get(list, i);
if (path_equals(path1, path2)) {
return true;
}
}
return false;
}
/*******************
* PATH VALIDATION *
*******************/
bool validate_one_way_path_length(vector *path) {
return path->count >= 4;
}
bool validate_two_way_path_length(vector *path) {
return path->count >= 4;
}
bool validate_one_way_path_head(vector *path) {
vertex *v1, *v2;
v1 = vector_get(path, 0);
v2 = vector_get(path, 1);
// Avoid isolated cycles and check if one way path condition is valid
return (v1->edge_count > 1 || v1->origins_count > 1) &&
vertex_has_destination_vert(v1, v2) != NULL;
}
bool validate_two_way_path_head(vector *path) {
vertex *v1, *v2;
v1 = vector_get(path, 0);
v2 = vector_get(path, 1);
// Avoid isolated cycles and check if one way path condition is valid
return (v1->edge_count > 2 || v1->origins_count > 2) &&
vertex_has_destination_vert(v1, v2) != NULL &&
vertex_has_origin_vert(v1, v2);
}
bool validate_one_way_path_internals(vector *path) {
for (int i = 1; i < path->count - 1; ++i) {
if (!vertex_has_origin_vert((vertex *) vector_get(path, i), (vertex *) vector_get(path, i - 1)) ||
vertex_has_destination_vert((vertex *) vector_get(path, i), (vertex *) vector_get(path, i + 1)) == NULL) {
return false;
}
}
return true;
}
bool validate_two_way_path_internals(vector *path) {
for (int i = 1; i < path->count - 1; ++i) {
if (
!vertex_has_origin_vert((vertex *) vector_get(path, i), (vertex *) vector_get(path, i - 1)) ||
!vertex_has_origin_vert((vertex *) vector_get(path, i), (vertex *) vector_get(path, i + 1)) ||
vertex_has_destination_vert((vertex *) vector_get(path, i), (vertex *) vector_get(path, i + 1)) == NULL ||
vertex_has_destination_vert((vertex *) vector_get(path, i), (vertex *) vector_get(path, i - 1)) == NULL
) {
return false;
}
}
return true;
}
bool validate_one_way_path_tail(vector *path) {
vertex *v1, *v2;
v1 = vector_get(path, path->count - 1);
v2 = vector_get(path, path->count - 2);
// Avoid isolated cycles and check if one way path condition is valid
return (v1->edge_count > 1 || v1->origins_count > 1) &&
vertex_has_origin_vert(v1, v2);
}
bool validate_two_way_path_tail(vector *path) {
vertex *v1, *v2;
v1 = vector_get(path, path->count - 1);
v2 = vector_get(path, path->count - 2);
// Avoid isolated cycles and check if one way path condition is valid
return (v1->edge_count > 2 || v1->origins_count > 2) &&
vertex_has_origin_vert(v1, v2) &&
vertex_has_destination_vert(v1, v2) != NULL;
}
bool validate_one_way_path(vector *path) {
return validate_one_way_path_length(path) &&
validate_one_way_path_head(path) &&
validate_one_way_path_internals(path) &&
validate_one_way_path_tail(path);
}
bool validate_two_way_path(vector *path) {
return validate_two_way_path_length(path) &&
validate_two_way_path_head(path) &&
validate_two_way_path_internals(path) &&
validate_two_way_path_tail(path);
}
/**
* Prints user-friedly list of edges
*
* @param edges
* @param edge_count
*/
void print_edges(edge *edges, int edge_count) {
for (int i = 0; i < edge_count; ++i) {
printf("\tDestination: %d\n"
"\tWeight: %f\n", edges[i].destination, edges[i].weight);
}
}
/***
* Prints user-friendly main graph to screen
*/
void print_graph() {
vertex *vert;
printf(BLUE);
for (int i = 0; i < vertices->count; ++i) {
vert = vector_get(vertices, i);
printf("SERIALIZING VERTEX [ %d / %d ]\n"
"ID : %d\n"
"Latitude: %f\n"
"Longitude: %f\n"
"Edge Count: %d\n", vert->id, vertices->count, vert->id, vert->latitude, vert->longitude,
vert->edge_count);
print_edges(vert->edges, vert->edge_count);
}
printf(RESET);
}
/****************
* STDOUT UTILS *
****************/
/***
* Prints character `c`, `amount` times.
*
* @param c - character to be repeated
* @param amount - amount of times to repeated
*/
void repeat_char(char *c, int amount) {
for (int i = 0; i < amount; ++i) {
printf("%s", c);
}
}
/***
* Prints big 'billboard' on screen
*
* @param text - text in the middle of the billboard
*/
void spam(char *text) {
int tab_size = 8;
printf(BOLD "\n");
repeat_char("#", strlen(text) + tab_size * 2);
printf("\n#");
repeat_char(" ", tab_size - 1);
printf(text);
repeat_char(" ", tab_size - 1);
printf("#\n");
repeat_char("#", strlen(text) + tab_size * 2);
printf(RESET "\n\n");
}
/***
* Prints user-friendly path vertex list
*
* @param path
*/
void print_path(vector *path) {
vertex *v;
for (int i = 0; i < path->count; ++i) {
v = (vector_get(path, i));
if (i == 0) {
printf("< ");
}
printf("%d", v->id);
if (i == path->count - 1) {
printf(" >\n");
} else {
printf(", ");
}
}
}
/********************
* GRAPH PROCESSING *
********************/
/***
* Finds compressible path, starting from a base vertex
*
* @param base - vertex used as a base
* @param path - resulting path found
* @param degree - degree of path to be searched
* @return - size of found path
*/
int find_compressible_path(vertex *base, vector *path, int degree) {
int size = 1; // base is added to path as step 1
vertex *origin = base;
vertex *old_origin = NULL;
vertex *destination = base;
vertex *old_destination = NULL;
spam("Finding compressible path");
printf(MAGENTA);
printf("Running find_compressible_path from vertex %d\n", base->id);
printf(RESET);
// Temporary placeholder for back-path
vector back_path;
// FIXME: POINTERS POINTERS POINTERS
// Init placeholder
vector_init(&back_path);
// Moves origin back until path cannot be increased
while (origin->origins_count == degree && origin->edge_count == degree) {
if (!in_path_vert(origin->origins[0], &back_path)) {
origin = origin->origins[0];
} else if (!in_path_vert(origin->origins[1], &back_path) && degree == 2) {
origin = origin->origins[1];
} else {
break;
}
vector_add(&back_path, origin);
printf(MAGENTA "Moving origin to vertex %d\n" RESET, origin->id);
// Avoids looping
if (origin == base) {
break;
}
size++;
}
// Reverse back-path to keep everything ordered
for (int i = back_path.count - 1; i >= 0; --i) {
vector_add(path, vector_get(&back_path, i));
}
// Add the initial vertex
vector_add(path, base);
// Continue expanding forward while path can be increased
while (destination->edge_count == degree && destination->origins_count == degree) {
if (!in_path(destination->edges[0].destination, path)) {
destination = get_vertex_by_id(destination->edges[0].destination);
} else if (!in_path(destination->edges[1].destination, path) && degree == 2) {
destination = get_vertex_by_id(destination->edges[1].destination);
} else {
break;
}
vector_add(path, destination);
printf(MAGENTA "Moving destination to vertex %d\n" RESET, destination->id);
// Avoids infinite looping
if (destination == base) {
break;
}
size++;
}
return size;
}
/***
* Sums every edge in the path, that points to a vertex in the path
*
* @param path - path to sum it's edges
* @return sum of internal edges
*/
float calculate_internal_edge_weights(vector *path) {
vertex *v, *v2;
float total_weight = 0;
for (int j = 0; j < path->count; j++) {
v = vector_get(path, j);
// Edge vertices (head/tail) destinations should only be summed if they point to vertices in the path
if (j == 0 || j == path->count - 1) {
for (int i = 0; i < v->edge_count; ++i) {
v2 = vector_get(path, j + (j == 0 ? 1 : -1));
if (v->edges[i].destination == v2->id) {
printf(MAGENTA "Adding %d to %d is in path, adding %f\n" RESET, v->id, v->edges[i].destination,
v->edges[i].weight);
total_weight += v->edges[i].weight;
continue;
}
}
} else {
// Internal vertices destinations should always be summed (since they always point to the path)
for (int k = 0; k < v->edge_count; ++k) {
total_weight += v->edges[k].weight;
}
}
}
return total_weight;
}
/**
* Parses main graph file
*/
void parse_graph_file() {
vertex *v;
char parameterType;
int total_edge_count, max_degree;
printf("Reading file header... \n\n");
// Read file header and prepare for vertex reading
fscanf(input_file, "%c %d %d %d\n", ¶meterType, &total_vertex_count, &total_edge_count, &max_degree);
printf("ParameterType: %c\n"
"Total Vertex Count: %d\n"
"Total Edge Count: %d\n"
"Max Graph Degree: %d\n", parameterType, total_vertex_count, total_edge_count, max_degree);
// Init vertices vector
vertices = malloc(sizeof(vector));
vector_init(vertices);
int id, edge_count;
float latitude, longitude;
/**
* Starts reading each line for vertex information
*/
for (int vertex_index = 0; vertex_index < total_vertex_count; vertex_index++) {
v = malloc(sizeof(vertex));
printf(GREEN "\nREADING VERTEX [ %d / %d ]\n" RESET, vertex_index + 1,
total_vertex_count);
fscanf(input_file, "%c %d %f %f %d", ¶meterType, &id, &latitude, &longitude, &edge_count);
printf("|\tParameter Type: " BRIGHT_BLUE "%c\n" RESET
"|\tID: " BRIGHT_BLUE "%d\n" RESET
"|\tLatitude: " BRIGHT_BLUE "%f\n" RESET
"|\tLongitude: " BRIGHT_BLUE "%f\n" RESET
"|\tEdge Count: " BRIGHT_BLUE "%d\n" RESET, parameterType, id, latitude, longitude, edge_count);
// Save vertex information to struct
v->id = id;
v->latitude = latitude;
v->longitude = longitude;
v->edge_count = edge_count;
v->edges = malloc(sizeof(edge) * edge_count);
v->origins_count = 0;
vector_add(vertices, v);
int destination;
float weight;
/**
* Reads edge information for current vertex
*/
for (int edge_index = 0; edge_index < edge_count; edge_index++) {
fscanf(input_file, "%d %f", &destination, &weight);
printf("|\t|\t" GREEN "READING EDGE [ %d / %d ]\n" RESET
"|\t|\t|\tDestination: " BRIGHT_BLUE "%d\n" RESET
"|\t|\t|\tWeight: " BRIGHT_BLUE "%f\n" RESET, edge_index + 1, edge_count, destination, weight);
v->edges[edge_index].destination = destination;
v->edges[edge_index].weight = weight;
}
// Consume new line character to avoid miss-alignment
fscanf(input_file, "\n");
printf(GREEN "ENDING VERTEX [ %d / %d ]\n" RESET, vertex_index + 1, total_vertex_count);
}
}
/***
* Performs entire graph search for compressible paths
*/
void search_compressible_paths() {
float internal_weight = 0;
vector *one_way_path;
vector *two_way_path;
one_way_paths_found = malloc(sizeof(vector));
two_way_paths_found = malloc(sizeof(vector));
vector_init(one_way_paths_found);
vector_init(two_way_paths_found);
for (int i = 0; i < vertices->count; i++) {
one_way_path = malloc(sizeof(vector));
two_way_path = malloc(sizeof(vector));
vector_init(one_way_path);
vector_init(two_way_path);
find_compressible_path(vector_get(vertices, i), one_way_path, 1);
find_compressible_path(vector_get(vertices, i), two_way_path, 2);
// Check if two-way-path is valid first, since two-way-paths can be false-validated as one-way-paths
if (validate_two_way_path(two_way_path)) {
// Output two_way_path according to type
if (path_exists(two_way_path, two_way_paths_found)) {
printf(RED "Found duplicate two_way_path, ignoring for now...\n" RESET);
vector_free(two_way_path);
vector_init(two_way_path);
// FIXME: Memory leak reallocate memory on path found. Clear vector on duplicate/notfound
continue;
} else {
printf(GREEN "Path found is not duplicate\n" RESET);
vector_add(two_way_paths_found, two_way_path);
}
printf("Debugging valid two_way_path with size %d\n", two_way_path->count);
print_path(two_way_path);
// Calculate and output two_way_path internal weight used for calculation
internal_weight = calculate_internal_edge_weights(two_way_path);
printf("Internal Segments Weight: %f \n", internal_weight);
printf("Each new edge will have: %f \n", internal_weight / 4);
} else if (validate_one_way_path(one_way_path)) {
// Output two_way_path according to type
if (path_exists(one_way_path, one_way_paths_found)) {
printf(RED "Found duplicate one_way_path, ignoring for now...\n" RESET);
vector_free(one_way_path);
vector_init(one_way_path);
// FIXME: Memory leak
continue;
} else {
printf(GREEN "Path found is not duplicate\n" RESET);
vector_add(one_way_paths_found, one_way_path);
}
printf("Debugging valid one_way_path with size %d\n", two_way_path->count);
print_path(one_way_path);
// Calculate and output two_way_path internal weight used for calculation
internal_weight = calculate_internal_edge_weights(one_way_path);
printf("Internal Segments Weight: %f \n", internal_weight);
printf("Each new edge will have: %f \n", internal_weight / 2);
} else {
bool length = false,
head = false,
internals = false,
tail = false;
length = validate_two_way_path_length(two_way_path);
if (length) {
head = validate_two_way_path_head(two_way_path);
}
if (length && head) {
internals = validate_two_way_path_internals(two_way_path);
}
if (length && head && internal_weight) {
tail = validate_two_way_path_tail(two_way_path);
}
printf(RED BOLD UNDERLINE "Path failed validation summary: ");
print_path(two_way_path);
printf(BOLD_OFF UNDERLINE_OFF RED);
printf("|\tLength validation: %d\n", length);
printf("|\tHead validation: %d\n", head);
printf("|\tInternal validation: %d\n", internals);
printf("|\tTail validation: %d\n", tail);
printf(RESET "\n");
}
}
}
/***
* Pre process graph vertices to count how many other vertices point to it
*/
// FIXME: this should already be done in pre_process_origins() since code is now using vectors
void pre_process_origins_count() {
vertex *v1, *v2;
for (int i = 0; i < vertices->count; ++i) {
v1 = vector_get(vertices, i);
for (int j = 0; j < v1->edge_count; ++j) {
v2 = vector_get(vertices, id_to_index(v1->edges[j].destination));
v2->origins_count++;
}
}
for (int i = 0; i < vertices->count; ++i) {
v1 = vector_get(vertices, i);
printf("Vertex %d is destination of %d \n", i + 1, v1->origins_count);
}
}
/***
* Populates list of origins into each vertex
*/
void pre_process_origins() {
vertex *v1, *v2;
// Tracks where is the end of each list
int *index = malloc(sizeof(int) * vertices->count);
int id;
for (int i = 0; i < vertices->count; ++i) {
v1 = vector_get(vertices, i);
index[i] = 0;
v1->origins = malloc(sizeof(vertex *) * v1->origins_count);
}
for (int i = 0; i < vertices->count; ++i) {
v1 = vector_get(vertices, i);
for (int j = 0; j < v1->edge_count; ++j) {
id = v1->edges[j].destination - 1;
v2 = vector_get(vertices, id);
v2->origins[index[id]++] = v1;
}
}
free(index);
}
/**
* Calculates the cost of the path (one side to the other)
*
* @param path
* @param reverse
* @return
*/
float calculate_path_cost(vector *path, bool reverse) {
vertex *v1, *v2;
edge *e;
float cost = 0;
for (int i = 0; i < path->count - 1; ++i) {
v1 = vector_get(path, i);
v2 = vector_get(path, i + 1);
if (reverse) {
e = vertex_has_destination_vert(v2, v1);
} else {
e = vertex_has_destination_vert(v1, v2);
}
if (e != NULL) {
cost += e->weight;
} else {
printf(RED "Could not find an edge to the next vertex, this should not happen in a path!\n" RESET);
}
}
return cost;
}
/**
* Prints compressible paths to screen
*/
void print_compressed_paths() {
for (int i = 0; i < one_way_paths_found->count; ++i) {
print_path(vector_get(one_way_paths_found, i));
printf("Cost: %f\n\n", calculate_path_cost(vector_get(one_way_paths_found, i), false));