-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscl_graph.c
1709 lines (1366 loc) · 53.9 KB
/
scl_graph.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
/**
* @file scl_graph.c
* @author Mihai Negru (determinant289@gmail.com)
* @version 1.0.0
* @date 2022-06-21
*
* @copyright Copyright (C) 2022-2023 Mihai Negru <determinant289@gmail.com>
* This file is part of C-language-Data-Structures.
*
* C-language-Data-Structures is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* C-language-Data-Structures is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with C-language-Data-Structures. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "./include/scl_graph.h"
#include "./include/scl_queue.h"
#include "./include/scl_stack.h"
#include "./include/scl_priority_queue.h"
/**
* @brief Create a new graph object. Function may fail if the
* number of vertices is zero, or not enough heap memory is
* left to allocate all vertices.
*
* @param number_of_vertices initial number of vertices to form a graph
* @return graph_t* a new allocated graph object or `NULL` if function fails
*/
graph_t* create_graph(size_t number_of_vertices) {
/* Check if input data is valid */
if (0 == number_of_vertices) {
errno = EINVAL;
perror("Number of vertexes of the graph is zero at creation");
return NULL;
}
/* Allocate a new graph object */
graph_t *new_graph = malloc(sizeof(*new_graph));
/* Check if graph object was allocated successfully */
if (NULL != new_graph) {
/* Set default values of the graph object */
new_graph->size = number_of_vertices;
/* Allocate visit array of the current graph object */
new_graph->visit = malloc(sizeof(*new_graph->visit) * number_of_vertices);
/* Check if visit array was allocated */
if (NULL != new_graph->visit) {
/* Allocate vertices array of the graph object */
new_graph->vertices = malloc(sizeof(*new_graph->vertices) * number_of_vertices);
/* Check if vertices array was allocated */
if (NULL != new_graph->vertices) {
/* Set every vertex to its default value */
for (size_t iter = 0; iter < new_graph->size; ++iter) {
/* Allocate a vertex node */
new_graph->vertices[iter] = malloc(sizeof(*new_graph->vertices[iter]));
/* Check if vertex was allocated successfully */
if (NULL != new_graph->vertices[iter]) {
/* Set default vertex values */
new_graph->vertices[iter]->link = NULL;
new_graph->vertices[iter]->in_deg = 0;
new_graph->vertices[iter]->out_deg = 0;
} else {
/* Allocation of one vertex failed so free all memory */
for (size_t del_iter = 0; del_iter <= iter; ++del_iter) {
free(new_graph->vertices[del_iter]);
new_graph->vertices[del_iter] = NULL;
}
free(new_graph->vertices);
new_graph->vertices = NULL;
free(new_graph->visit);
new_graph->visit = NULL;
free(new_graph);
new_graph = NULL;
errno = ENOMEM;
perror("Not enought memory to allocate graph links");
}
}
} else {
/* Allocation of the vertices array failed */
free(new_graph->visit);
new_graph->visit = NULL;
free(new_graph);
new_graph = NULL;
errno = ENOMEM;
perror("Not enough memory to allocate vertices of the graph");
}
} else {
/* Allocation of the visit array failed */
free(new_graph);
new_graph = NULL;
errno = ENOMEM;
perror("Not enough memory to allocate visit vector for graph");
}
} else {
/* Allocation of the graph failed */
errno = ENOMEM;
perror("Not enough memory to allocate new graph object");
}
/* Return a new allocated graph object or `NULL` */
return new_graph;
}
/**
* @brief Subroutine function of free_graph, to
* free all edges of one vertex from heap memory.
* No graph object is needed just pointer to
* linked list representing the edges.
*
* @param link pointer to a linked list representing the edges array
*/
static void free_graph_link(graph_link_t *link) {
/* Free memory occupied by edges of one vertex */
while (NULL != link) {
graph_link_t *delete_node = link;
link = link->next;
free(delete_node);
delete_node = NULL;
}
}
/**
* @brief Function to free all memory allocated for a graph
* object from heap memory. Function may fail if graph object
* is `NULL`, however it will not break the program.
*
* @param gr pointer to an allocated graph object
* @return scl_error_t enum object for handling errors
*/
scl_error_t free_graph(graph_t * const __restrict__ gr) {
/* Check if graph pointer is valid */
if (NULL != gr) {
/* Check if vertices array is allocated */
if (NULL != gr->vertices) {
/* Free memory of all vertices */
for (size_t iter = 0; iter < gr->size; ++iter) {
if (NULL != gr->vertices[iter]) {
/* Free memory for one vertex */
free_graph_link(gr->vertices[iter]->link);
free(gr->vertices[iter]);
gr->vertices[iter] = NULL;
}
}
/* Free vertices array */
free(gr->vertices);
gr->vertices = NULL;
}
/* Free visit array */
if (NULL != gr->visit) {
free(gr->visit);
gr->visit = NULL;
}
/* Free graph object */
free(gr);
/* All good */
return SCL_OK;
}
/* `NULL` graph sent to free */
return SCL_NULL_GRAPH;
}
/**
* @brief Create a graph edge node for linked list.
* Function may fail if vertex value is `SIZE_MAX`
* or edge length is `__LBDL_MAX__` or no heap memory is
* left for memory allocation.
*
* @param vertex number of the vertex to link the edge
* @param edge_len the length of the current edge node
* @return graph_link_t* a new allocated edge node object or `NULL`
* if function fails
*/
static graph_link_t* create_graph_link(size_t vertex, long double edge_len) {
/* Check if input data is valid */
if ((SIZE_MAX == vertex) || (__LDBL_MAX__ == edge_len)) {
errno = EINVAL;
perror("Data input for link creation is invalid");
return NULL;
}
/* Allocate a new edge node */
graph_link_t *new_link = malloc(sizeof(*new_link));
/* Check if edge was allocated */
if (NULL != new_link) {
/* Set edge object data */
new_link->vertex = vertex;
new_link->edge_len = edge_len;
new_link->next = NULL;
} else {
/* Edge allocation failed */
errno = ENOMEM;
perror("Not enough memory to allocate graph vertex");
}
/* Return a new allocated edge(link) or `NULL` */
return new_link;
}
/**
* @brief Function to insert one edge into the graph object or to link
* two vertices with one edge. Function may fail if input graph object is
* or input data is not valid.
*
* @param gr pointer to an allocated graph object
* @param start_vertex number of vertex that edge starts from
* @param end_vertex number of vertex that edge ends to
* @param edge_len the length of the edge that links two vertices
* @return scl_error_t enum object for handling errors
*/
scl_error_t graph_insert_edge(const graph_t * const __restrict__ gr, size_t start_vertex, size_t end_vertex, long double edge_len) {
/* Check if graph object is valid */
if (NULL == gr) {
return SCL_NULL_GRAPH;
}
/* Check if vertices array is valid */
if (NULL == gr->vertices) {
return SCL_NULL_GRAPH_VERTICES;
}
/* Check if two vertices are in the current graph object */
if ((gr->size <= start_vertex) || (gr->size <= end_vertex)) {
return SCL_VERTEX_OUT_OF_BOUND;
}
/* Check if selected vertices are allocated */
if ((NULL == gr->vertices[start_vertex]) || (NULL == gr->vertices[end_vertex])) {
return SCL_NULL_GRAPH_VERTEX;
}
/* Check if edge length is valid */
if (__LDBL_MAX__ == edge_len) {
return SCL_INVALID_EDGE_LENGTH;
}
/* Create a new link between two vertices */
graph_link_t *new_vertex = create_graph_link(end_vertex, edge_len);
/* Check if allocation went successfully */
if (NULL == new_vertex) {
return SCL_NOT_ENOUGHT_MEM_FOR_NODE;
}
/* Link edge to start_vertex linked list */
new_vertex->next = gr->vertices[start_vertex]->link;
gr->vertices[start_vertex]->link = new_vertex;
/* Update in and out degree of the vertices */
++(gr->vertices[start_vertex]->out_deg);
++(gr->vertices[end_vertex]->in_deg);
/* All good */
return SCL_OK;
}
/**
* @brief Function to add more vertices in the current graph object.
* Function may fail if no heap memory is for the new vertices.
*
* @param gr a pointer to an allocated graph object
* @param new_vertices number of vertices to add in graph object
* @return scl_error_t enum object for handling errors
*/
scl_error_t graph_insert_vertices(graph_t * const __restrict__ gr, size_t new_vertices) {
/* Check if graph pointer is valid */
if (NULL == gr) {
return SCL_NULL_GRAPH;
}
/* Check if vertices array is allocated */
if (NULL == gr->vertices) {
return SCL_NULL_GRAPH_VERTICES;
}
/* Check if new_vertices will add nodes on graph */
if (0 == new_vertices) {
return SCL_GRAPH_INVALID_NEW_VERTICES;
}
/* Try to realloc visit array of the graph */
uint8_t *try_realloc_visit = realloc(gr->visit, sizeof(*gr->visit) * (gr->size + new_vertices));
if (NULL == try_realloc_visit) {
return SCL_REALLOC_GRAPH_VERTICES_FAIL;
}
gr->visit = try_realloc_visit;
/* Try to realloc vertices array of the graph */
graph_vertex_t **try_realloc = realloc(gr->vertices, sizeof(*try_realloc) * (gr->size + new_vertices));
if (NULL == try_realloc) {
return SCL_REALLOC_GRAPH_VERTICES_FAIL;
}
/* Allocate the new created vertices and set default values */
for (size_t iter = gr->size; iter < gr->size + new_vertices; ++iter) {
try_realloc[iter] = malloc(sizeof(*try_realloc[iter]));
if (NULL != try_realloc[iter]) {
try_realloc[iter]->link = NULL;
try_realloc[iter]->in_deg = 0;
try_realloc[iter]->out_deg = 0;
} else {
for (size_t del_iter = gr->size; del_iter <= iter; ++del_iter) {
free(try_realloc[del_iter]);
try_realloc[del_iter] = NULL;
}
return SCL_REALLOC_GRAPH_VERTICES_FAIL;
}
}
/* If adding new vertices went successfully update size */
gr->size += new_vertices;
gr->vertices = try_realloc;
/* All good */
return SCL_OK;
}
/**
* @brief Create the transpose graph of the selected graph. The object
* graph sent as input has to be a valid object. Function may fail if no memory
* is left on heap to allcoate the new transposed graph.
*
* @param gr a pointer to an allocated graph object
* @return graph_t* allocated transposed graph of selected object or `NULL`
*/
graph_t* create_transpose_graph(const graph_t * const __restrict__ gr) {
/* Check if the graph object is valid */
if ((NULL == gr) || (0 == gr->size) || (NULL == gr->vertices)) {
errno = EINVAL;
perror("Graph sent as input in transpose function is not valid");
return NULL;
}
/* Create a new graph */
graph_t *transpose_gr = create_graph(gr->size);
/* Check if transposed graph was created */
if (NULL == transpose_gr) {
return NULL;
}
scl_error_t err = SCL_OK;
/* Invert every edges from original graph into transposed graph */
for (size_t iter = 0; iter < gr->size; ++iter) {
if (NULL == gr->vertices[iter]) {
return NULL;
}
graph_link_t *link = gr->vertices[iter]->link;
while (NULL != link) {
err = graph_insert_edge(transpose_gr, link->vertex, iter, link->edge_len);
if (SCL_OK != err) {
return NULL;
}
link = link->next;
}
}
/* Return an allocated transposed graph */
return transpose_gr;
}
/**
* @brief Function to print the vertices of the graph and their edges with
* other vertices. Function may fail if one vertex os not allcoated or graph
* object is not valid.
*
* @param gr a pointer to an allocated graph object.
* @param data_arr array of words to print the mapping of the vertices can be `NULL`
* @return scl_error_t enum object for handling errors
*/
scl_error_t graph_print(const graph_t * const __restrict__ gr, const uint8_t ** const data_arr) {
/* Check if graph pointer is valid */
if (NULL == gr) {
return SCL_NULL_GRAPH;
}
/* Check if vertices array is allocated */
if (NULL == gr->vertices) {
return SCL_NULL_GRAPH_VERTICES;
}
/* Print vertices of the graph and their edges */
if (0 == gr->size) {
printf("[ ]\n");
} else {
for (size_t iter = 0; iter < gr->size; ++iter) {
if (NULL == gr->vertices[iter]) {
return SCL_NULL_GRAPH_VERTEX;
}
if (NULL != data_arr) {
printf("(%s) ", data_arr[iter]);
} else {
printf("(%ld) ", iter);
}
graph_link_t *link = gr->vertices[iter]->link;
while (NULL != link) {
if (NULL != data_arr) {
printf("-> [%s] ", data_arr[link->vertex]);
} else {
printf("-> (%ld) ", link->vertex);
}
link = link->next;
}
printf(" -> (~)\n");
}
}
/* All good */
return SCL_OK;
}
/**
* @brief Function to delete one edge that links two vertices.
* Function may fail if the graph is not valid or selected
* edge is not in the current graph.
*
* @param gr a pointer to an allocated graph object
* @param first_vertex first vertex that edge starts from
* @param second_vertex second vertex that edge end to
* @return scl_error_t enum object for handling errors
*/
scl_error_t graph_delete_edge(const graph_t * const __restrict__ gr, size_t first_vertex, size_t second_vertex) {
/* Check if graph pointer is valid */
if (NULL == gr) {
return SCL_NULL_GRAPH;
}
/* Check if vertices array is allocated */
if (NULL == gr->vertices) {
return SCL_NULL_GRAPH_VERTICES;
}
/* Check if start vertex is in the current graph */
if (first_vertex >= gr->size) {
return SCL_VERTEX_OUT_OF_BOUND;
}
/* Check if start vertex is allocated */
if (NULL == gr->vertices[first_vertex]) {
return SCL_NULL_GRAPH_VERTEX;
}
graph_link_t *delete_link = gr->vertices[first_vertex]->link;
graph_link_t *parent_delete_link = NULL;
/* Find selected edge from graph */
while ((NULL != delete_link) & (second_vertex != delete_link->vertex)) {
parent_delete_link = delete_link;
delete_link = delete_link->next;
}
/* Edge was not found in the graph */
if (NULL == delete_link) {
return SCL_EDGE_NOT_FOUND;
}
/* Update the linked list */
if (NULL == parent_delete_link) {
gr->vertices[first_vertex]->link = delete_link->next;
} else {
parent_delete_link->next = delete_link->next;
}
/* Delete edge from memory */
delete_link->next = NULL;
free(delete_link);
delete_link = NULL;
/* Update the out degree of the start vertex */
--(gr->vertices[first_vertex]->out_deg);
/* All good */
return SCL_OK;
}
/**
* @brief Function to delete all edges that link the same two nodes.
* This function is a bigger brother of the graph_delete_edge, however
* an oriented graph can have more than two edges that link the same
* two vertices so the function will delete all these edges.
*
* @param gr a pointer to an allocated graph object
* @param first_vertex first vertex that edge starts from
* @param second_vertex second vertex that edge end to
* @return scl_error_t enum object for handling errors
*/
scl_error_t graph_delete_all_edges(const graph_t * const __restrict__ gr, size_t first_vertex, size_t second_vertex) {
/* Check if graph pointer is valid */
if (NULL == gr) {
return SCL_NULL_GRAPH;
}
/* Check if vertices array is allocated */
if (NULL == gr->vertices) {
return SCL_NULL_GRAPH_VERTICES;
}
/* Check if start vertex is in the current graph */
if (first_vertex >= gr->size) {
return SCL_VERTEX_OUT_OF_BOUND;
}
/* Check if start vertex is allocated */
if (NULL == gr->vertices[first_vertex]) {
return SCL_NULL_GRAPH_VERTEX;
}
graph_link_t *delete_link = gr->vertices[first_vertex]->link;
graph_link_t *parent_delete_link = NULL;
/* Find every occurence of the selected edge and remove them from graph */
while (NULL != delete_link) {
if (second_vertex == delete_link->vertex) {
if (NULL == parent_delete_link) {
gr->vertices[first_vertex]->link = delete_link->next;
delete_link->next = NULL;
free(delete_link);
delete_link = gr->vertices[first_vertex]->link;
} else {
parent_delete_link->next = delete_link->next;
delete_link->next = NULL;
free(delete_link);
delete_link = parent_delete_link->next;
}
--(gr->vertices[first_vertex]->out_deg);
} else {
parent_delete_link = delete_link;
delete_link = delete_link->next;
}
}
/* All good */
return SCL_OK;
}
/**
* @brief Function to delete a selected vertex from the current
* graph object. Vertices greater than selected vertex will
* deacrease their number by one so vertex `3` maps into the
* vertex `2` and vertex `2` becomes `1` if vertex 1 was deleted.
* When vertex is deleted all of its in and out edges will be removed
* from the current working graph object.
*
* @param gr a pointer to an allocated graph object
* @param vertex selected vertex number to delet from graph
* @return scl_error_t enum object for handling errors
*/
scl_error_t graph_delete_vertex(graph_t * const __restrict__ gr, size_t vertex) {
/* Check if graph pointer is valid */
if (NULL == gr) {
return SCL_NULL_GRAPH;
}
/* Check if vertices array is allocated */
if (NULL == gr->vertices) {
return SCL_NULL_GRAPH_VERTICES;
}
/* Check if selected vertex is from current graph */
if (vertex >= gr->size) {
return SCL_VERTEX_OUT_OF_BOUND;
}
/* Check if selected vertex is allocated and can be removed */
if (NULL == gr->vertices[vertex]) {
return SCL_NULL_GRAPH_VERTEX;
}
scl_error_t err = SCL_OK;
/* Delete IN edges from the graph */
for (size_t iter = 0; iter < gr->size; ++iter) {
if (iter != vertex) {
err = graph_delete_all_edges(gr, iter, vertex);
if (SCL_OK != err) {
return err;
}
}
}
/* Delete OUT edges of the selected vertex */
free_graph_link(gr->vertices[vertex]->link);
gr->vertices[vertex]->in_deg = gr->vertices[vertex]->out_deg = 0;
/* Free memory allocated for vertex */
free(gr->vertices[vertex]);
gr->vertices[vertex] = NULL;
--(gr->size);
/* Try to realloc memory to reduce memory space */
for (size_t iter = vertex; iter < gr->size; ++iter) {
gr->vertices[iter] = gr->vertices[iter + 1];
}
uint8_t *try_realloc_visit = realloc(gr->visit, sizeof(*gr->visit) * gr->size);
if (NULL == try_realloc_visit) {
return SCL_REALLOC_GRAPH_VERTICES_FAIL;
}
gr->visit = try_realloc_visit;
graph_vertex_t **try_realloc = realloc(gr->vertices, sizeof(*try_realloc) * gr->size);
if (NULL == try_realloc) {
return SCL_REALLOC_GRAPH_VERTICES_FAIL;
}
gr->vertices = try_realloc;
/* If reallocation went successfully update the vertices number */
for (size_t iter = 0; iter < gr->size; ++iter) {
if (NULL == gr->vertices[iter]) {
return SCL_NULL_GRAPH_VERTEX;
}
graph_link_t *link = gr->vertices[iter]->link;
while (NULL != link) {
if (link->vertex > vertex) {
--(link->vertex);
}
link = link->next;
}
}
/* All good */
return SCL_OK;
}
/**
* @brief Function to get the number of vertices from the
* selected graph. If pointer to graph object is NULL than
* SIZE_MAX will be returned
*
* @param gr a pointer to an allocated graph object
* @return size_t SIZE_MAX or the actual size of the graph
*/
size_t get_graph_size(const graph_t * const __restrict__ gr) {
if (NULL == gr) {
return SIZE_MAX;
}
return gr->size;
}
/**
* @brief Function to traverse the vertices of the graph object by
* breath-first-search method. Function will get as input an allocated
* array to save the path of bfs, if the array is `NULL` then the path
* will not be saved however function will return the exact size of the
* traversed vertices in this bfs call.
*
* @param gr a pointer to an allocated graph object
* @param start_vertex a vertex to start the bfs
* @param vertex_path an array to save the path of bfs traversal can be `NULL`
* @return size_t size of the traversed vertices or 0 if function failed
*/
size_t graph_bfs_traverse(const graph_t * const __restrict__ gr, size_t start_vertex, size_t * __restrict__ vertex_path) {
/* Check if input data is valids */
if ((NULL == gr) || (NULL == gr->vertices) || (start_vertex >= gr->size)) {
return 0;
}
/* No vertex was traversed */
size_t traversed_vex = 0;
/* Set all vertices as unvisited */
for (size_t iter = 0; iter < gr->size; ++iter) {
gr->visit[iter] = 0;
}
/* Create the bfs traversal queue */
queue_t *bfs_queue = create_queue(NULL, sizeof(*vertex_path));
if (NULL != bfs_queue) {
/* Start the breath-first-search from the start vertex */
gr->visit[start_vertex] = 1;
if (SCL_OK != queue_push(bfs_queue, &start_vertex)) {
free_queue(bfs_queue);
return 0;
}
while (!is_queue_empty(bfs_queue)) {
const size_t *front_vertex = queue_front(bfs_queue);
if (NULL != vertex_path) {
/* Insted of adding in path variable you can perform an action */
vertex_path[traversed_vex] = *front_vertex;
}
/* Increase traversed vertices size */
++traversed_vex;
if (SCL_OK != queue_pop(bfs_queue)) {
free_queue(bfs_queue);
return 0;
}
graph_link_t *link = gr->vertices[*front_vertex]->link;
while (NULL != link) {
if (0 == gr->visit[link->vertex]) {
gr->visit[link->vertex] = 1;
if (SCL_OK != queue_push(bfs_queue, &link->vertex)) {
free_queue(bfs_queue);
return 0;
}
}
link = link->next;
}
}
free_queue(bfs_queue);
}
/* Return the traversed vertices size */
return traversed_vex;
}
/**
* @brief Subroutine function for graph_dfs_traversal to
* calculate recursively the depth-first-search vertices.
*
* @param gr a pointer to an allocated graph object
* @param start_vertex number of vertex to start the dfs traversal
* @param vertex_path an array to save dfs path can be `NULL`
* @param traversed_vex size of the traversed vertices in the dfs call
*/
static void graph_dfs_traverse_helper(const graph_t * const __restrict__ gr, size_t start_vertex, size_t * __restrict__ vertex_path, size_t * __restrict__ traversed_vex) {
/* Visit the current vertex */
gr->visit[start_vertex] = 1;
/* Insert vertex into the path */
if (NULL != vertex_path) {
vertex_path[*traversed_vex] = start_vertex;
}
/* Increase traversed size */
++(*traversed_vex);
graph_link_t *link = gr->vertices[start_vertex]->link;
/* Visit the neighbours vertices */
while (NULL != link) {
if (0 == gr->visit[link->vertex]) {
graph_dfs_traverse_helper(gr, link->vertex, vertex_path, traversed_vex);
}
link = link->next;
}
}
/**
* @brief Function to traverse the vertices of the graph object by
* depth-first-search method. Function will get as input an allocated
* array to save the path of dfs, if the array is `NULL` then the path
* will not be saved however function will return the exact size of the
* traversed vertices in this dfs call.
*
* @param gr a pointer to an allocated graph object
* @param start_vertex a vertex to start the dfs
* @param vertex_path an array to save the path of dfs traversal can be `NULL`
* @return size_t size of the traversed vertices or 0 if function failed
*/
size_t graph_dfs_traverse(const graph_t * const __restrict__ gr, size_t start_vertex, size_t * __restrict__ vertex_path) {
/* Check if input data is valid */
if ((NULL == gr) || (NULL == gr->vertices) || (start_vertex >= gr->size)) {
return 0;
}
/* No vertex was traversed */
size_t traversed_vex = 0;
/* Set every vertex as unvisited */
for (size_t iter = 0; iter < gr->size; ++iter) {
gr->visit[iter] = 0;
}
/* Call helper function the real dfs call */
graph_dfs_traverse_helper(gr, start_vertex, vertex_path, &traversed_vex);
/* Return the size of the traversed vertices */
return traversed_vex;
}
/**
* @brief Subroutine function of the graph_has_cycle to perform
* a special dfs traversal of the graph and to find if graph has or
* not a cycle.
*
* @param gr a pointer to an allocated graph object
* @param start_vertex first vertex to check if it forms a cycle
* @param current_vertex current working vertex of the dfs traversal
* @return uint8_t 1 if graph has a cycle starting from start_vertex or 0 otherwise
*/
static uint8_t graph_has_cycle_helper(const graph_t * const __restrict__ gr, size_t start_vertex, size_t current_vertex) {
/* All vertices were visited and start vertex is the last visited vertex */
if ((1 == gr->visit[start_vertex]) && (start_vertex == current_vertex)) {
return 1;
}
/* All vertices were visited and no cycle was found */
if (1 == gr->visit[current_vertex]) {
return 0;
}
graph_link_t *link = gr->vertices[current_vertex]->link;
int has_cycle = 0;
/* Perform the dfs call */
while ((NULL != link) && (0 == has_cycle)) {
has_cycle = graph_has_cycle_helper(gr, start_vertex, link->vertex);
link = link->next;
}
/* Return if graph has or not a cycle */
return has_cycle;
}
/**
* @brief Function to check if a graph object has or has not a cycle.
* Function may fail if graph object is not valid in that case false will
* be returned because an invalid graph has no cycle.
*
* @param gr a pointer to an allocated graph object
* @return uint8_t if graph has a cycle or 0 otherwise
*/
uint8_t graph_has_cycle(const graph_t * const __restrict__ gr) {
/* Check if graph object is valid */
if ((NULL == gr) || (NULL == gr->visit) || (NULL == gr->vertices)) {
return 0;
}
uint8_t has_cycle = 0;
/* Traverse every vertex by dfs method and check if it has a cycle */
for (size_t iter = 0; (iter < gr->size) && (0 == has_cycle); ++iter) {
/* Reset the visit array */
for (size_t make_zero = 0; make_zero < gr->size; ++make_zero) {
gr->visit[make_zero] = 0;
}
/* Check if graph has a cycle */
has_cycle = graph_has_cycle_helper(gr, iter, iter);
}
/* 1 if graph has a cycle or 0 otherwise */
return has_cycle;
}
/**
* @brief Function to get all vertices that are resulting
* from the start_vertex in other words there exists a path
* from node X to node Y, in this case node Y will be part of
* past nodes.
*
* @param gr a pointer to an allocated graph object
* @param start_vertex a vertex to get its past vertices
* @param vertex_path an allocated array to save all past vertices
* @return size_t size of the past vertices or 0 if function failed
*/
size_t graph_vertex_past_vertices(const graph_t * const __restrict__ gr, size_t start_vertex, size_t * __restrict__ vertex_path) {
/* Check if graph object is valid */
if ((NULL == gr) || (NULL == gr->vertices) || (start_vertex >= gr->size) || (NULL == vertex_path)) {
return 0;
}
size_t traversed_vex = 0;
/* Reset the visit array */
for (size_t iter = 0; iter < gr->size; ++iter) {
gr->visit[iter] = 0;
}
/* Create the bfs queue to find past nodes */
queue_t *bfs_queue = create_queue(NULL, sizeof(*vertex_path));
if (NULL != bfs_queue) {
gr->visit[start_vertex] = 1;
if (SCL_OK != queue_push(bfs_queue, &start_vertex)) {
free_queue(bfs_queue);
return 0;
}
/* Start the bfs traversal */
while (!is_queue_empty(bfs_queue)) {
const size_t *front_vertex = queue_front(bfs_queue);
/* Add node to past nodes */
if (start_vertex != *front_vertex) {
vertex_path[traversed_vex++] = *front_vertex;
}
if (SCL_OK != queue_pop(bfs_queue)) {
free_queue(bfs_queue);
return 0;
}
graph_link_t *link = gr->vertices[*front_vertex]->link;
while (NULL != link) {
if (0 == gr->visit[link->vertex]) {
gr->visit[link->vertex] = 1;
if (SCL_OK != queue_push(bfs_queue, &link->vertex)) {
free_queue(bfs_queue);
return 0;
}
}
link = link->next;
}
}
free_queue(bfs_queue);
}
/* Return number of the past vertices */
return traversed_vex;
}