-
Notifications
You must be signed in to change notification settings - Fork 18
/
DirectedGraph.c
1069 lines (798 loc) · 29.8 KB
/
DirectedGraph.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 "../Headers/DirectedGraph.h"
#include "../../Lists/Headers/ArrayList.h"
#include "../../Tables/Headers/HashMap.h"
#include "../../Tables/Headers/HashSet.h"
#include "../../Stacks/Headers/Stack.h"
#include "../../Queues/Headers/Queue.h"
#include "../../../System/Utils.h"
#include "../../../Unit Test/CuTest/CuTest.h"
#if defined(__BORLANDC__)
typedef unsigned char uint8_t;
typedef __int64 int64_t;
typedef unsigned long uintptr_t;
#elif defined(_MSC_VER)
typedef unsigned char uint8_t;
typedef __int64 int64_t;
#else
#include <stdint.h>
#endif
/** @struct DirGraphNode
* @brief This structure implements a basic directed graph node.
* @var DirGraphNode::value
* Member 'value' is a pointer to the node value.
* @var DirGraphNode::adjacentNodes
* Member 'adjacentNodes' is a pointer to the adjacent nodes array list.
* @var DirGraphNode::valueCompFun
* Member 'valueCompFun' is a pointer to the comparator function, that will compare the graph values.
*/
typedef struct DirGraphNode {
void *value;
struct ArrayList *adjacentNodes;
int (*valueCompFun)(const void *, const void *);
} DirGraphNode;
void dirGraphTopologicalSortR(DirGraphNode *node, HashSet *visitedNodes, Stack *sortStack);
int dirGraphNodeIsPartOfCycleR(DirGraphNode *node, HashSet *visitedNodes);
/** This function will be useful to free the graph nodes.
*
* @param node the node address
*/
void freeDGraphNode(void *node) {
DirGraphNode *nodeToFree = (DirGraphNode *) node;
if (nodeToFree->adjacentNodes != NULL) {
for (int i = arrayListGetLength(nodeToFree->adjacentNodes) - 1; i >= 0; i--)
arrayListRemoveWtFr(nodeToFree->adjacentNodes);
destroyArrayList(nodeToFree->adjacentNodes);
}
free(nodeToFree);
}
/** Comparing two nodes values function.
*
* @param n1 the first node address
* @param n2 the second node address
* @return it will return zero if the two nodes are equal
*/
int dGraphNodeComp(const void *n1, const void *n2) {
DirGraphNode *node1 = (DirGraphNode *) n1;
DirGraphNode *node2 = (DirGraphNode *) n2;
return node1->valueCompFun(node1->value, node2->value);
}
/** This function will initialize the directed graph then it will return a pointer to the graph address.
*
* Note: because the graph is implemented by hash map, you need to pass the hashing function.
*
* @param freeValue the freeing value function address
* @param valueComp the comparing values function address
* @param hashFun the hashing function that will return a unique integer representing the hash map key
* @return it will return the initialized graph address
*/
DirectedGraph *directedGraphInitialization(
void (*freeValue)(void *),
int (*valueComp)(const void *, const void *),
int (*hashFun)(const void *)
) {
if (freeValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "free function pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
} else if (valueComp == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "comparator function pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
} else if (hashFun == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "hash function pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirectedGraph *directedGraph = (DirectedGraph *) malloc(sizeof(DirectedGraph));
if (directedGraph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = FAILED_ALLOCATION;
return NULL;
#else
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "directed graph", "directed graph data structure");
exit(FAILED_ALLOCATION);
#endif
}
directedGraph->valueComp = valueComp;
directedGraph->freeValue = freeValue;
directedGraph->nodes = hashMapInitialization(freeValue, freeDGraphNode, valueComp, hashFun);
return directedGraph;
}
/** This function will take a value and insert it in the graph.
*
* Note: if the value is already in the graph, then the graph will free the passed value immediately.
*
* @param graph the graph address
* @param value the new value address
*/
void dirGraphAddNode(DirectedGraph *graph, void *value) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (value == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
if (hashMapContains(graph->nodes, value)) {
graph->freeValue(value);
return;
}
DirGraphNode *newNode = (DirGraphNode *) malloc(sizeof(DirGraphNode));
newNode->value = value;
newNode->valueCompFun = graph->valueComp;
newNode->adjacentNodes = arrayListInitialization(5, freeDGraphNode, dGraphNodeComp);
hashMapInsert(graph->nodes, value, newNode);
}
/** This function will take a value as a parameter, then it will check if the value is in the graph,
* then it will remove and free the value that in the graph if found.
*
* Note: the function will not free the passed value.
*
* @param graph the graph address
* @param value the value address
*/
void dirGraphRemoveNode(DirectedGraph *graph, void *value) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (value == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *nodeToDelete = (DirGraphNode *) hashMapGet(graph->nodes, value);
if (nodeToDelete == NULL)
return;
DirGraphNode **hashMapNodes = (DirGraphNode **) hashMapToArray(graph->nodes);
for (int i = 0; i < hashMapGetLength(graph->nodes); i++) {
int index = arrayListGetIndex(hashMapNodes[i]->adjacentNodes, nodeToDelete);
if (index != -1)
arrayListRemoveAtIndexWtFr(hashMapNodes[i]->adjacentNodes, index);
}
free(hashMapNodes);
hashMapDelete(graph->nodes, value);
}
/** This function will take a value as a parameter, then it will check if the value is in the graph,
* then it will remove the value that in the graph without freeing it if found.
*
* Note: the function will not free the passed value.
*
* @param graph the graph address
* @param value the value address
* @return it will return the deleted value if found, other wise it will return NULL
*/
void *dirGraphRemoveNodeWtoFr(DirectedGraph *graph, void *value) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return NULL;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (value == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *nodeToDelete = (DirGraphNode *) hashMapGet(graph->nodes, value);
if (nodeToDelete == NULL)
return NULL;
DirGraphNode **hashMapNodes = (DirGraphNode **) hashMapToArray(graph->nodes);
for (int i = 0; i < hashMapGetLength(graph->nodes); i++) {
int index = arrayListGetIndex(hashMapNodes[i]->adjacentNodes, nodeToDelete);
if (index != -1)
arrayListRemoveAtIndexWtFr(hashMapNodes[i]->adjacentNodes, index);
}
free(hashMapNodes);
return hashMapDeleteWtoFrAll(graph->nodes, value)->key;
}
/** This function will take two nodes values, and then it will create an edge between them,
* if they are found in the graph.
*
* Note: The function will not free the two passed values.
*
* @param graph the graph address
* @param fromVal the first value address
* @param toVal the second value address
*/
void dirGraphAddEdge(DirectedGraph *graph, void *fromVal, void *toVal) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (fromVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "from value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
} else if (toVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "to value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *fromNode = hashMapGet(graph->nodes, fromVal);
DirGraphNode *toNode = hashMapGet(graph->nodes, toVal);
if (fromNode == NULL || toNode == NULL)
return;
if (!arrayListContains(fromNode->adjacentNodes, toNode))
arrayListAdd(fromNode->adjacentNodes, toNode);
}
/** This function will take a two nodes value and then it will remove the edge between them if found.
*
* Note: The function will not free the passed values.
*
* @param graph the graph address
* @param fromVal the first value address
* @param toVal the second value address
*/
void dirGraphRemoveEdge(DirectedGraph *graph, void *fromVal, void *toVal) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (fromVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "from value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
} else if (toVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "to value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *fromNode = hashMapGet(graph->nodes, fromVal);
DirGraphNode *toNode = hashMapGet(graph->nodes, toVal);
if (fromNode == NULL || toNode == NULL)
return;
int toNodeIndex = arrayListGetIndex(fromNode->adjacentNodes, toNode);
if (toNodeIndex != -1)
arrayListRemoveAtIndexWtFr(fromNode->adjacentNodes, toNodeIndex);
}
/** This function will check if the passed value is in the graph or not,
* and if it was then the function will return one (1), other wise it will return zero (0).
*
* Note: the function will not free the passed value.
*
* @param graph the graph address
* @param value the value address
* @return it will return one if the value found, other wise in will return zero
*/
int dirGraphContainsNode(DirectedGraph *graph, void *value) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (value == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
return hashMapContains(graph->nodes, value);
}
/** This function will take two nodes value then it will check if there is an edge between them,
* and if there was the function will return one, other wise it will return zero.
*
* Note: the function will not free the passed two values.
*
* @param graph the graph address
* @param fromVal the first value address
* @param toVal the second value address
* @return it will return one it there was an edge between the two values, other wise it will return zero
*/
int dirGraphContainsEdge(DirectedGraph *graph, void *fromVal, void *toVal) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (fromVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "from value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
} else if (toVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "to value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *fromNode = hashMapGet(graph->nodes, fromVal);
DirGraphNode *toNode = hashMapGet(graph->nodes, toVal);
if (fromNode == NULL || toNode == NULL)
return 0;
return arrayListContains(fromNode->adjacentNodes, toNode);
}
/** This function will return the number of nodes in the graph.
*
* @param graph the graph address
* @return it will return the number of nodes in the graph
*/
int dirGraphGetSize(DirectedGraph *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
}
return hashMapGetLength(graph->nodes);
}
/** This function will check if the graph is empty,
* and return one (1) if it was, other wise in will return zero (0).
*
* @param graph the graph address
* @return it will return one if the graph was empty, other wise in will return zero
*/
int dirGraphIsEmpty(DirectedGraph *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
}
return hashMapGetLength(graph->nodes) == 0;
}
/** This function will clear and free the graph nodes without freeing the graph itself.
*
* @param graph the graph address
*/
void clearDirGraph(DirectedGraph *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
}
clearHashMap(graph->nodes);
}
/** This function will destroy and free the graph and all it's nodes.
*
* @param graph the graph address
*/
void destroyDirGraph(void *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
}
destroyHashMap(((DirectedGraph *) graph)->nodes);
free(graph);
}
/** This function will print all the graph values in this from:
*
* <<
*
* The node: //the node name
*
* Is connected to:
*
* // the connected nodes names
*
* .....
*
* >>
*
* @param graph the graph address
* @param printVal the printing function address, that will be called to print the values
*/
void dirGraphPrint(DirectedGraph *graph, void (*printVal)(void *)) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (printVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "print function pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode **arr = (DirGraphNode **) hashMapToArray(graph->nodes);
for (int i = 0; i < hashMapGetLength(graph->nodes); i++) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
printf("The node: ");
#endif
printVal(arr[i]->value);
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
printf("\nIs connected to:\n");
#endif
for (int j = 0; j < arrayListGetLength(arr[i]->adjacentNodes); j++) {
printVal(((DirGraphNode *) arrayListGet(arr[i]->adjacentNodes, j))->value);
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
printf("\n");
#endif
}
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
printf("\n\n");
#endif
}
}
/** This function will take an uintptr_tPointer pointer as a parameter,
* then it will free it.
*
* This function will be useful in traversal functions.
*
* @param uintptr_tPointer the uintptr_tPointer address
*/
void freeUintptr_t(void *uintptr_tPointer) {
free(uintptr_tPointer);
}
/** This function will take two uintptr_t pointer as a parameters,
* then it will compare the two uintptr_t.
*
* This function will be useful in traversal functions.
*
* @param uintptr_tPointer1 the first uintptr_t address
* @param uintptr_tPointer2 the second uintptr_t address
* @return it will return zero if the two uintptr_t are equal.
*/
int compUintptr_t(const void *uintptr_tPointer1, const void *uintptr_tPointer2) {
return (int) (*(uintptr_t *) uintptr_tPointer1 - *(uintptr_t *) uintptr_tPointer2);
}
/** This function will take an uintptr_t pointer as a parameter,
* then it will return the value of the uintptr_t.
*
* Note: this function will be useful to use in the hash map and hash set data structures.
*
* @param item the uintptr_t pointer
* @return it will return the value of the uintptr_t as the unique hash key
*/
int uintptr_tHashFunDirG(const void *item) {
return (int) *(uintptr_t *) item;
}
/** This function will depth first traverse the graph.
*
* Note: actually you can do more than printing values, the printing function will take the value as a parameter.
*
* @param graph the graph address
* @param startVal the start node value address
* @param printVal the printing function address, that will be called to print the value
*/
void dirGraphDepthFirstTraversal(DirectedGraph *graph, void *startVal, void (*printVal)(void *)) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (startVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "start value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
} else if (printVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "print function pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *startNode = hashMapGet(graph->nodes, startVal);
if (startNode == NULL)
return;
HashSet *visitedNodes = hashSetInitialization(freeUintptr_t, compUintptr_t, uintptr_tHashFunDirG);
Stack *nodesStack = stackInitialization(freeDGraphNode);
stackPush(nodesStack, startNode);
uintptr_t *startNodeValAddress = (uintptr_t *) malloc(sizeof(uintptr_t));
*startNodeValAddress = (uintptr_t) startNode->value;
hashSetInsert(visitedNodes, startNodeValAddress);
DirGraphNode *currentNode;
while (!stackIsEmpty(nodesStack)) {
currentNode = stackPop(nodesStack);
printVal(currentNode->value);
for (int i = 0; i < arrayListGetLength(currentNode->adjacentNodes); i++) {
DirGraphNode *adjNode = (DirGraphNode *) arrayListGet(currentNode->adjacentNodes, i);
uintptr_t *adjNodeValAddress = (uintptr_t *) malloc(sizeof(uintptr_t));
*adjNodeValAddress = (uintptr_t) adjNode->value;
if (!hashSetContains(visitedNodes, adjNodeValAddress)) {
stackPush(nodesStack, adjNode);
hashSetInsert(visitedNodes, adjNodeValAddress);
} else
free(adjNodeValAddress);
}
}
destroyHashSet(visitedNodes);
destroyStack(nodesStack);
}
/** This function will breadth first traverse the graph.
*
* Note: actually you can do more than printing values, the printing function will take the value as a parameter.
*
* @param graph the graph address
* @param startVal the start node value address
* @param printVal the printing function address, that will be called to print the value
*/
void dirGraphBreadthFirstTraversal(DirectedGraph *graph, void *startVal, void (*printVal)(void *)) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (startVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "start value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
} else if (printVal == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "print function pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *startNode = hashMapGet(graph->nodes, startVal);
if (startNode == NULL)
return;
HashSet *visitedNodes = hashSetInitialization(freeUintptr_t, compUintptr_t, uintptr_tHashFunDirG);
Queue *nodesQueue = queueInitialization(freeDGraphNode);
queueEnqueue(nodesQueue, startNode);
uintptr_t *startNodeValAddress = (uintptr_t *) malloc(sizeof(uintptr_t));
*startNodeValAddress = (uintptr_t) startNode->value;
hashSetInsert(visitedNodes, startNodeValAddress);
DirGraphNode *currentNode;
while (!queueIsEmpty(nodesQueue)) {
currentNode = queueDequeue(nodesQueue);
printVal(currentNode->value);
for (int i = 0; i < arrayListGetLength(currentNode->adjacentNodes); i++) {
DirGraphNode *adjNode = (DirGraphNode *) arrayListGet(currentNode->adjacentNodes, i);
uintptr_t *adjNodeValAddress = (uintptr_t *) malloc(sizeof(uintptr_t));
*adjNodeValAddress = (uintptr_t) adjNode->value;
if (!hashSetContains(visitedNodes, adjNodeValAddress)) {
queueEnqueue(nodesQueue, adjNode);
hashSetInsert(visitedNodes, adjNodeValAddress);
} else
free(adjNodeValAddress);
}
}
destroyHashSet(visitedNodes);
destroyQueue(nodesQueue);
}
/** This function will sort the graph nodes topologically, then it will return an array list that contains the values.
*
* @param graph the graph address
* @return it will return an array list pointer that contains the values sorted
*/
ArrayList *dirGraphTopologicalSort(DirectedGraph *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return NULL;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
}
DirGraphNode **hashMapNodes = (DirGraphNode **) hashMapToArray(graph->nodes);
ArrayList *sortedArrayList = arrayListInitialization(hashMapGetLength(graph->nodes), graph->freeValue,
graph->valueComp);
HashSet *visitedNodes = hashSetInitialization(freeUintptr_t, compUintptr_t, uintptr_tHashFunDirG);
Stack *sortStack = stackInitialization(freeDGraphNode);
for (int i = 0; i < hashMapGetLength(graph->nodes); i++)
dirGraphTopologicalSortR(hashMapNodes[i], visitedNodes, sortStack);
while (!stackIsEmpty(sortStack)) {
DirGraphNode *tempNode = stackPop(sortStack);
arrayListAdd(sortedArrayList, tempNode->value);
}
destroyStack(sortStack);
destroyHashSet(visitedNodes);
free(hashMapNodes);
return sortedArrayList;
}
/** This function will sort the graph nodes topologically recursively, and it will fill the stack with the values.
*
* @param node the current node address
* @param visitedNodes the visited nodes hash set address
* @param sortStack the nodes stack address
*/
void dirGraphTopologicalSortR(DirGraphNode *node, HashSet *visitedNodes, Stack *sortStack) {
uintptr_t *nodeValueAddress = (uintptr_t *) malloc(sizeof(uintptr_t));
*nodeValueAddress = (uintptr_t) node->value;
if (hashSetContains(visitedNodes, nodeValueAddress)) {
free(nodeValueAddress);
return;
}
hashSetInsert(visitedNodes, nodeValueAddress);
for (int i = 0; i < arrayListGetLength(node->adjacentNodes); i++)
dirGraphTopologicalSortR(arrayListGet(node->adjacentNodes, i), visitedNodes, sortStack);
stackPush(sortStack, node);
}
/** This function will check if the passed node value is a part of a cycle,
* and if it was the function will return one (1) other wise it will return zero (0).
*
* Note: the passed item will not be freed in the end of the function.
*
* @param graph the directed graph address
* @param startValue the start value address
* @return it will return one if the node is a part of a cycle, other wise it will return zero
*/
int dirGraphNodeIsPartOfCycle(DirectedGraph *graph, void *startValue) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "directed graph data structure");
exit(NULL_POINTER);
#endif
} else if (startValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "start value pointer", "directed graph data structure");
exit(INVALID_ARG);
#endif
}
DirGraphNode *startNode = hashMapGet(graph->nodes, startValue);
if (startNode == NULL)
return 0;
HashSet *visitedNodes = hashSetInitialization(freeUintptr_t, compUintptr_t, uintptr_tHashFunDirG);
int cycleFlag = dirGraphNodeIsPartOfCycleR(startNode, visitedNodes);
destroyHashSet(visitedNodes);
return cycleFlag;
}
/** This function will check if there is a cycle recursively,
* then it will return one if there was a cycle, other wise it will return zero.
*
* @param node the node address
* @param visitedNodes the visited nodes hash set address
* @return it will return one if there was a cycle, other wise it will return zero
*/