-
Notifications
You must be signed in to change notification settings - Fork 18
/
UndirectedGraph.c
1611 lines (1245 loc) · 48.5 KB
/
UndirectedGraph.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/UndirectedGraph.h"
#include "../../Tables/Headers/HashMap.h"
#include "../../Tables/Headers/HashSet.h"
#include "../../Stacks/Headers/Stack.h"
#include "../../Queues/Headers/Queue.h"
#include "../../Queues/Headers/PriorityQueue.h"
#include "../../Lists/Headers/ArrayList.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 UDGraphNode
* @brief This structure implements a basic directed graph node.
* @var UDGraphNode::value
* Member 'value' is a pointer to the node value.
* @var UDGraphNode::adjacentNodes
* Member 'adjacentNodes' is a pointer to the adjacent nodes array list.
* @var UDGraphNode::freeFun
* Member 'freeFun' is a pointer to the free function, that will free the graph values.
* @var UDGraphNode::compFun
* Member 'compFun' is a pointer to the comparator function, that will compare the graph values.
*/
typedef struct UDGraphNode {
void *value;
ArrayList *adjacentNodes;
void (*freeFun)(void *);
int (*compFun)(const void *, const void *);
} UDGraphNode;
typedef struct UDGraphEdge {
UDGraphNode *node;
int weight;
} UDGraphEdge;
/** This struct will be useful in getting shortest distance and minimum spanning. */
typedef struct DistanceHolder {
UDGraphNode *toNode;
UDGraphNode *fromNode;
int distance;
} DistanceHolder;
HashMap *UDGraphGenerateDistancesMap(UDGraphNode *startNode);
int UDGraphNodePartOfCycleR(UDGraphNode *prevNode, UDGraphNode *currentNode, HashSet *visitedNodes);
/** This function take a node as it's parameter,
* then it will free the node without freeing it's value.
*
* @param node the node address
*/
void UDGraphNodeFreeFun(void *node) {
UDGraphNode *nodeToFree = (UDGraphNode *) node;
destroyArrayList(nodeToFree->adjacentNodes);
free(node);
}
/** This function will take an edge as it's parameter,
* then it will free the edge without freeing it's node.
*
* @param edge the edge pointer
*/
void UDGraphEdgeFreeFun(void *edge) {
UDGraphEdge *edgeToFree = (UDGraphEdge *) edge;
free(edgeToFree);
}
/** This function will take two edges then it will compare there nodes values.
*
* Note: if the function returned zero, then the two edges are equal.
*
* @param e1 the first edge pointer
* @param e2 the second edge pointer
* @return it will return zero if the two nodes are equal, minus if the second bigger, and positive if the first bigger
*/
int UDGraphEdgeComp(const void *e1, const void *e2) {
UDGraphEdge *edge1 = (UDGraphEdge *) e1;
UDGraphEdge *edge2 = (UDGraphEdge *) e2;
return edge1->node->compFun(edge1->node->value, edge2->node->value);
}
/** This function will take two nodes then it will compare them by there value.
*
* @param n1 the first node pointer
* @param n2 the second node pointer
* @return it will return zero if the two nodes are equal
*/
int UDGraphNodeComp(const void *n1, const void *n2) {
UDGraphNode *node1 = (UDGraphNode *) n1;
UDGraphNode *node2 = (UDGraphNode *) n2;
int returnValue = node1->compFun(node1->value, node2->value);
return returnValue;
}
/** This function will initialize a new undirected graph in the memory,
* then return it.
*
* Note: because the graph is implemented by hash map, you need to pass the hashing function.
*
* @param freeFun the values freeing function pointer
* @param compFun the values comparing function pointer
* @param hashFun the hashing function that will return a unique integer representing the hash map key
* @return it will return the new initialized graph pointer
*/
UndirectedGraph *undirectedGraphInitialization(void (*freeFun)(void *), int (*compFun)(const void *, const void *),
int (*hashFun)(const void *)) {
if (freeFun == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "freeing function pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (compFun == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "comparing function pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (compFun == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "hash function pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UndirectedGraph *graph = (UndirectedGraph *) malloc(sizeof(UndirectedGraph));
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = FAILED_ALLOCATION;
return NULL;
#else
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "graph", "undirected graph data structure");
exit(FAILED_ALLOCATION);
#endif
}
graph->nodes = hashMapInitialization(freeFun, UDGraphNodeFreeFun, compFun, hashFun);
graph->freeFun = freeFun;
graph->compFun = compFun;
return graph;
}
/** This function will add the provided value in a new node in the graph.
*
* Note: if the node is already exist in the graph, then the function will free the passed value.
*
* @param graph the graph pointer
* @param value the new value pointer
*/
void udGraphAddNode(UndirectedGraph *graph, void *value) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected 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", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
if (hashMapContains(graph->nodes, value)) {
graph->freeFun(value);
return;
}
UDGraphNode *newNode = (UDGraphNode *) malloc(sizeof(UDGraphNode));
if (newNode == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = FAILED_ALLOCATION;
return;
#else
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "new node", "undirected graph data structure");
exit(FAILED_ALLOCATION);
#endif
}
newNode->value = value;
newNode->freeFun = graph->freeFun;
newNode->compFun = graph->compFun;
newNode->adjacentNodes = arrayListInitialization(5, UDGraphEdgeFreeFun, UDGraphEdgeComp);
hashMapInsert(graph->nodes, value, newNode);
}
/** This function will take two nodes and then it will add a new edge between them.
*
* Note: it there was already an edge between the two values then the function will update the weight.
*
* Note: the function will note free the passed values.
*
* @param graph the graph pointer
* @param fValue the first value pointer
* @param sValue the second value pointer
* @param edgeWeight the weight of the new edge
*/
void udGraphAddEdge(UndirectedGraph *graph, void *fValue, void *sValue, int edgeWeight) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
} else if (fValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "first value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (sValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "second value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (edgeWeight < 0) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "edge weight", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode *fNode = hashMapGet(graph->nodes, fValue);
UDGraphNode *sNode = hashMapGet(graph->nodes, sValue);
if (fNode == NULL || sNode == NULL)
return;
UDGraphEdge *fNewEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
UDGraphEdge *sNewEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
if (fNewEdge == NULL || sNewEdge == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = FAILED_ALLOCATION;
return;
#else
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "new edge", "undirected graph data structure");
exit(FAILED_ALLOCATION);
#endif
}
fNewEdge->node = sNode;
fNewEdge->weight = edgeWeight;
sNewEdge->node = fNode;
sNewEdge->weight = edgeWeight;
int fEdgeIndex = arrayListGetIndex(fNode->adjacentNodes, fNewEdge);
int sEdgeIndex = arrayListGetIndex(sNode->adjacentNodes, sNewEdge);
if (fEdgeIndex >= 0 && sEdgeIndex >= 0) {
UDGraphEdge *fTempEdge = arrayListGet(fNode->adjacentNodes, fEdgeIndex);
UDGraphEdge *sTempEdge = arrayListGet(sNode->adjacentNodes, sEdgeIndex);
fTempEdge->weight = sTempEdge->weight = edgeWeight;
free(fNewEdge);
free(sNewEdge);
return;
}
arrayListAdd(fNode->adjacentNodes, fNewEdge);
arrayListAdd(sNode->adjacentNodes, sNewEdge);
}
/** This function will take a node value,
* then it will remove and free the node with it's value from the graph.
*
* Note: the function will not free the passed value.
*
* @param graph the graph pointer
* @param value the value pointer
*/
void udGraphRemoveNode(UndirectedGraph *graph, void *value) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected 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", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode *node = hashMapGet(graph->nodes, value);
if (node == NULL)
return;
UDGraphEdge *tempNodeEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
tempNodeEdge->node = node;
for (int i = 0; i < arrayListGetLength(node->adjacentNodes); i++) {
UDGraphEdge *tempEdge = (UDGraphEdge *) arrayListGet(node->adjacentNodes, i);
arrayListRemoveAtIndex(tempEdge->node->adjacentNodes,
arrayListGetIndex(tempEdge->node->adjacentNodes, tempNodeEdge));
}
free(tempNodeEdge);
hashMapDelete(graph->nodes, value);
}
/** This function will take a node value,
* then it will remove and free the node from the graph without freeing the node value.
*
* Note: the function will not free the passed value.
*
* @param graph the graph pointer
* @param value the value pointer
*/
void *udGraphRemoveNodeWtoFr(UndirectedGraph *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", "undirected 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", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode *node = hashMapGet(graph->nodes, value);
if (node == NULL)
return NULL;
UDGraphEdge *tempNodeEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
tempNodeEdge->node = node;
for (int i = 0; i < arrayListGetLength(node->adjacentNodes); i++) {
UDGraphEdge *tempEdge = (UDGraphEdge *) arrayListGet(node->adjacentNodes, i);
arrayListRemoveAtIndex(tempEdge->node->adjacentNodes,
arrayListGetIndex(tempEdge->node->adjacentNodes, tempNodeEdge));
}
free(tempNodeEdge);
hashMapDeleteWtoFrAll(graph->nodes, value);
void *returnValue = node->value;
UDGraphNodeFreeFun(node);
return returnValue;
}
/** This function will take two value,
* then it will remove the edge between them it found.
*
* Note: the function will not free the passed values.
*
* @param graph the graph pointer
* @param fValue the fist value pointer
* @param sValue the second value pointer
*/
void udGraphRemoveEdge(UndirectedGraph *graph, void *fValue, void *sValue) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
} else if (fValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "fist value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (sValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "second value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode *fNode = hashMapGet(graph->nodes, fValue);
UDGraphNode *sNode = hashMapGet(graph->nodes, sValue);
if (fNode == NULL || sNode == NULL)
return;
UDGraphEdge *fNewEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
UDGraphEdge *sNewEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
if (fNewEdge == NULL || sNewEdge == NULL) {
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "graph nodes", "undirected graph data structure");
exit(FAILED_ALLOCATION);
}
fNewEdge->node = sNode;
sNewEdge->node = fNode;
int fEdgeIndex = arrayListGetIndex(fNode->adjacentNodes, fNewEdge);
int sEdgeIndex = arrayListGetIndex(sNode->adjacentNodes, sNewEdge);
if (fEdgeIndex >= 0 && sEdgeIndex >= 0) {
arrayListRemoveAtIndex(fNode->adjacentNodes, fEdgeIndex);
arrayListRemoveAtIndex(sNode->adjacentNodes, sEdgeIndex);
}
free(fNewEdge);
free(sNewEdge);
}
/** This function will check if the passed value exist in the graph or not.
* The function will return one (1) if the value exists, other wise it will return zero (0).
*
* Note: the function will not free the passed value.
*
* @param graph the graph pointer
* @param value the value pointer
* @return it will return one if the value exists in the graph, other wise it will return zero
*/
int udGraphContainsNode(UndirectedGraph *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", "undirected 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", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
return hashMapContains(graph->nodes, value);
}
/** This function will take two value then it will check if there is an edge between them,
* if there was then the function will return one (1), other wise it will return zero (0).
*
* Note: the function will not free the passed values.
*
* @param graph the graph pointer
* @param fValue the first value pointer
* @param sValue the second value pointer
* @return it will return one if the edge exist, other wise it will return zero
*/
int udGraphContainsEdge(UndirectedGraph *graph, void *fValue, void *sValue) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
} else if (fValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "first value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (sValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "second value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode *fNode = hashMapGet(graph->nodes, fValue);
UDGraphNode *sNode = hashMapGet(graph->nodes, sValue);
if (fNode == NULL || sNode == NULL)
return 0;
UDGraphEdge *fNewEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
UDGraphEdge *sNewEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
if (fNewEdge == NULL || sNewEdge == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = FAILED_ALLOCATION;
return -1;
#else
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "new edge", "undirected graph data structure");
exit(FAILED_ALLOCATION);
#endif
}
fNewEdge->node = fNode;
sNewEdge->node = sNode;
int fEdgeIndex = arrayListGetIndex(fNode->adjacentNodes, sNewEdge);
int sEdgeIndex = arrayListGetIndex(sNode->adjacentNodes, fNewEdge);
free(fNewEdge);
free(sNewEdge);
return fEdgeIndex >= 0 && sEdgeIndex >= 0;
}
/** This function will take two value then it will check if there is an edge between them,
* if there was then the function will return the weight of the edge, other wise it will return minus one (-1).
*
* Note: the function will not free the passed values.
*
* @param graph the graph pointer
* @param fValue the first value pointer
* @param sValue the second value pointer
* @return it will return the edge weight, other wise it will return -1
*/
int udGraphGetEdgeWeight(UndirectedGraph *graph, void *fValue, void *sValue) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
} else if (fValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "first value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (sValue == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "second value pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode *fNode = hashMapGet(graph->nodes, fValue);
UDGraphNode *sNode = hashMapGet(graph->nodes, sValue);
if (fNode == NULL || sNode == NULL)
return -1;
UDGraphEdge *tempEdge = (UDGraphEdge *) malloc(sizeof(UDGraphEdge));
tempEdge->node = sNode;
UDGraphEdge *targetEdge = (UDGraphEdge *) arrayListGet(fNode->adjacentNodes,
arrayListGetIndex(fNode->adjacentNodes, tempEdge));
UDGraphEdgeFreeFun(tempEdge);
return targetEdge != NULL ? targetEdge->weight : -1;
}
/** This function will print the graph nodes as following:
*
* <<
*
* The node: //the node name
*
* Is connected to:
*
* // the connected nodes names --weight--> (the edge weight)
*
* .....
*
* >>
*
* @param graph the graph pointer
* @param printFun the values printing function pointer
*/
void printUDGraph(UndirectedGraph *graph, void (*printFun)(void *)) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
}
if (printFun == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "print function pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode **nodesArray = (UDGraphNode **) hashMapToArray(graph->nodes);
for (int i = 0; i < hashMapGetLength(graph->nodes); i++) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
printf("The node: ");
#endif
printFun(nodesArray[i]->value);
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
printf("\nIs connected to:\n");
#endif
for (int j = 0; j < arrayListGetLength(nodesArray[i]->adjacentNodes); j++) {
UDGraphEdge *tempEdge = (UDGraphEdge *) arrayListGet(nodesArray[i]->adjacentNodes, j);
printFun(tempEdge->node->value);
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
fprintf(stdout, " --weight--> %d\n", tempEdge->weight);
#endif
}
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
#else
printf("\n\n");
#endif
}
}
/** This function will return the number of nodes in the graph.
*
* @param graph the graph pointer
* @return it will return the number of nodes in the graph
*/
int udGraphGetSize(UndirectedGraph *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
}
return hashMapGetLength(graph->nodes);
}
/** This function will check if the graph is empty,
* and if it was the function will return one (1), other wise it will return zero (0).
*
* @param graph the graph pointer
* @return it will return one if the graph was empty, other wise it will return zero
*/
int udGraphIsEmpty(UndirectedGraph *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
}
return hashMapIsEmpty(graph->nodes);
}
/** This function will clear and free the graph nodes and value,
* without freeing the graph.
*
* @param graph the graph pointer
*/
void clearUDGraph(UndirectedGraph *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
}
clearHashMap(graph->nodes);
}
/** This function will destroy and free the graph, with all it's nodes and values.
*
* @param graph the graph pointer
*/
void destroyUDGraph(void *graph) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected graph data structure");
exit(NULL_POINTER);
#endif
}
destroyHashMap(((UndirectedGraph *) graph)->nodes);
free(graph);
}
/** This function will take an uintptr_t pointer as a parameter,
* then it will free it.
*
* This function will be useful in traversal functions.
*
* @param uintptr_tPointer the uintptr_t address
*/
void UDGFreeUintptr_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 UDGCompUintptr_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_tHashFunUDirG(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 UDGraphDepthFirstTraversal(UndirectedGraph *graph, void *startVal, void (*printFun)(void *)) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected 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", "undirected graph data structure");
exit(INVALID_ARG);
#endif
} else if (printFun == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "print function pointer", "undirected graph data structure");
exit(INVALID_ARG);
#endif
}
UDGraphNode *startNode = hashMapGet(graph->nodes, startVal);
Stack *nodesStack = stackInitialization(UDGraphNodeFreeFun);
HashSet *visitedNodes = hashSetInitialization(UDGFreeUintptr_t, UDGCompUintptr_t, uintptr_tHashFunUDirG);
stackPush(nodesStack, startNode);
uintptr_t *startNodeValAddress = (uintptr_t *) malloc(sizeof(uintptr_t));
*startNodeValAddress = (uintptr_t) startNode->value;
hashSetInsert(visitedNodes, startNodeValAddress);
while (!stackIsEmpty(nodesStack)) {
UDGraphNode *tempNode = stackPop(nodesStack);
printFun(tempNode->value);
for (int i = 0; i < arrayListGetLength(tempNode->adjacentNodes); i++) {
UDGraphNode *adjNode = ((UDGraphEdge *) arrayListGet(tempNode->adjacentNodes, i))->node;
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);
}
}
destroyStack(nodesStack);
destroyHashSet(visitedNodes);
}
/** 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 UDGraphBreadthFirstTraversal(UndirectedGraph *graph, void *startVal, void (*printFun)(void *)) {
if (graph == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "graph", "undirected 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", "undirected graph data structure");
exit(INVALID_ARG);