-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
1885 lines (1657 loc) · 74.4 KB
/
main.c
File metadata and controls
1885 lines (1657 loc) · 74.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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 <stdbool.h>
#include <time.h>
#include <sys/times.h>
#include <string.h>
#include "cubic.h"
#include "nausparse.h"
/* Nauty worksize */
#define WORKSIZE 50 * MAXM
/* Variables for nauty */
int lab[MAXN], ptn[MAXN], orbits[MAXN];
static DEFAULTOPTIONS_SPARSEGRAPH(options);
statsblk stats;
setword workspace[WORKSIZE];
//For the timing
#define time_factor sysconf(_SC_CLK_TCK)
sparsegraph sg; /* Sparse graph datastructure for nauty */
sparsegraph sg_canon; /* Sparse graph datastructure for nauty */
int cubicIndex = 0;
Graph **cubicGraphs = NULL;
// Nauty and Sparse Graph Functions
void init_nauty_options() {
//options.getcanon = TRUE;
//options.userautomproc = save_generators;
/* Init the nauty datastructures */
SG_INIT(sg);
SG_ALLOC(sg, MAX_VERTICES, 3 * MAX_VERTICES, "malloc");
//sg.v and sg.d only have to be set once
int i;
for (i = 0; i < MAX_VERTICES; i++) {
sg.v[i] = i * MAX_DEGREE;
sg.d[i] = 3;
}
SG_INIT(sg_canon);
SG_ALLOC(sg_canon, MAX_VERTICES, 3 * MAX_VERTICES, "malloc");
}
/**
* Copy a graph represented by an AdjList to a sparsegraph struct for nauty representation.
*
* @param graph The input graph represented by an AdjList.
*/
void copy_sparse_graph(Graph *graph) {
sg.nv = graph->current_nodes;
sg.nde = 3 * graph->current_nodes;
int i, j;
for (i = 0; i < graph->V; i++) {
//These values were already set in init_nauty_options()
//sg.v[i] = i * REG;
//sg.d[i] = 3;
struct AdjListNode *adjListNode = graph->nodes[i].head;
for (j = 0; j < 3 && adjListNode != NULL; j++) {
sg.e[i * 3 + j] = adjListNode->label;
adjListNode = adjListNode->next;
}
}
}
/**
* Print a sparse graph in the nauty format.
*
* @param sparse_graph The sparsegraph struct representing the graph to be printed.
*/
void print_sparse_graph_nauty(sparsegraph sparse_graph) {
int i, j;
fprintf(stderr, "Printing sparse graph nauty:\n");
for (i = 0; i < sparse_graph.nv; i++) {
fprintf(stderr, "%d :", i);
for (j = 0; j < sparse_graph.d[i]; j++) {
//fprintf(stderr, " %d", sparse_graph.e[i * REG + j]);
fprintf(stderr, " %d", sparse_graph.e[sparse_graph.v[i] + j]);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "Number of directed edges: %lu\n", (unsigned long) sparse_graph.nde);
}
/* Basic Graph Operations */
/**
* Counts the degree of a specific vertex in a cubic graph.
*
* This function calculates the degree (number of adjacent vertices) of a given vertex in a cubic graph.
* A cubic graph is a graph in which every vertex has exactly three neighbors.
*
* @param graph A pointer to the Graph structure representing the cubic graph.
* @param label The label of the vertex whose degree needs to be counted.
* @return An integer representing the degree of the specified vertex. If the vertex does not exist in the graph,
* or if the graph parameter is NULL, the function returns 0.
*/
int countVertexDegree(Graph *graph, int label) {
int count = 0;
struct AdjListNode *nodeDest = graph->nodes[label].head;
while (nodeDest != NULL) {
count++;
nodeDest = nodeDest->next;
}
return count;
}
/**
* Removes a specific node from the adjacency list of a vertex in a cubic graph, if present.
*
* This function removes a specified node with the label 'dest' from the adjacency list of the vertex 'src'
* in a cubic graph. If the node is found and removed, its memory is freed. The function assumes that both
* 'src' and 'dest' are valid vertices in the graph.
*
* @param graph A pointer to the Graph structure representing the cubic graph.
* @param src The label of the source vertex from which to remove the node.
* @param dest The label of the node to be removed from the adjacency list.
*/
void removeNodeFromAdjList(Graph *graph, int src, int dest) {
struct AdjListNode *nodeDest = graph->nodes[src].head;
struct AdjListNode *placeHolder = NULL;
if (nodeDest->label == dest) {
placeHolder = nodeDest;
graph->nodes[src].head = nodeDest->next;
free(placeHolder);
return; //It wasn't there before
}
while (nodeDest->next != NULL) {
if (nodeDest->next->label == dest) {
placeHolder = nodeDest->next;
nodeDest->next = nodeDest->next->next;
free(placeHolder);
return; //There was a break before
}
nodeDest = nodeDest->next;
}
}
/**
* Adds an undirected edge between two vertices in a cubic graph, if possible.
*
* This function attempts to add an undirected edge between two vertices, 'src' and 'dest', in a cubic graph.
* It checks if the graph has not reached its maximum degree constraint (MAX_DEGREE) for either 'src' or 'dest'.
* If either vertex has reached the maximum degree or if the edge already exists, the addition is skipped.
*
* @param graph A pointer to the Graph structure representing the cubic graph.
* @param src The label of the source vertex.
* @param dest The label of the destination vertex.
*/
void addEdge(Graph *graph, int src, int dest) {
// Check
if (countVertexDegree(graph, src) >= MAX_DEGREE || countVertexDegree(graph, dest) >= MAX_DEGREE)
return;
if (graph->nodes[src].head == NULL)
graph->current_nodes++;
if (graph->nodes[dest].head == NULL)
graph->current_nodes++;
//Add Edge to AdjList
struct AdjListNode *nodeDest = graph->nodes[src].head;
while (nodeDest) {
if (nodeDest->label == dest)
return;
nodeDest = nodeDest->next;
}
struct AdjListNode *newNode = newAdjListNode(dest);
newNode->next = graph->nodes[src].head;
graph->nodes[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = graph->nodes[dest].head;
graph->nodes[dest].head = newNode;
//Add Edge to EdgeList
EdgeListNode *edge = newEdgeListNode(src, dest);
edge->next = graph->edges->head;
graph->edges->head = edge;
//Update total edges
graph->current_edges++;
}
/**
* Removes an undirected edge between two vertices in a cubic graph, if present.
*
* This function removes an undirected edge between 'src' and 'dest' vertices in a cubic graph, if the edge exists.
* It removes both occurrences of the edge from the adjacency lists of both vertices and the edge list.
* The function assumes that 'src' and 'dest' are valid vertices in the graph.
*
* @param graph A pointer to the Graph structure representing the cubic graph.
* @param src The label of one endpoint of the edge.
* @param dest The label of the other endpoint of the edge.
*/
void removeEdge(Graph *graph, int src, int dest) {
//Remove Edge from AdjList
removeNodeFromAdjList(graph, src, dest);
removeNodeFromAdjList(graph, dest, src);
//Remove Edge from edgeList
removeEdgeFromEdgeList(graph->edges, src, dest);
removeEdgeFromEdgeList(graph->edges, dest, src);
//Update current edges
graph->current_edges--;
}
/**
* Removes an edge node from the edge list, if present.
*
* This function removes an edge node with the given source 'src' and destination 'dest' from the edge list
* represented by 'edgeList', if the edge node exists in the list. The memory occupied by the removed edge node
* is freed. The function assumes that the 'edgeList' is a valid EdgeList structure.
*
* @param edgeList A pointer to the EdgeList structure representing the list of edges.
* @param src The label of the source vertex of the edge.
* @param dest The label of the destination vertex of the edge.
*/
void removeEdgeFromEdgeList(EdgeList *edgeList, int src, int dest) {
EdgeListNode *edgeNode = edgeList->head;
EdgeListNode *placeHolder = NULL;
if (edgeNode->edge->src == src && edgeNode->edge->dest == dest) {
placeHolder = edgeNode;
edgeList->head = edgeNode->next;
//free( placeHolder->edge);
free(placeHolder);
return; //It wasn't there before
}
while (edgeNode->next != NULL) {
if (edgeNode->next->edge->src == src && edgeNode->next->edge->dest == dest) {
EdgeListNode *placeHolder2 = edgeNode->next;
edgeNode->next = edgeNode->next->next;
//free(placeHolder2->edge);
free(placeHolder2);
return; // There was a break before
}
edgeNode = edgeNode->next;
}
}
/**
* Inserts a new edge into the edge list if it does not already exist.
*
* This function inserts a new edge, with the source 'src' and destination 'dest', into the edge list
* represented by 'edgeList', but only if the edge does not already exist in the list. The function first
* verifies the non-existence of the edge using the 'verifyEdgeExistence' function. If the edge is not found,
* a new edge node is created and added to the front of the edge list.
*
* @param edgeList A pointer to the EdgeList structure representing the list of edges.
* @param src The label of the source vertex of the new edge.
* @param dest The label of the destination vertex of the new edge.
*/
void insertInEdgeList(EdgeList *edgeList, int src, int dest) {
if (!verifyEdgeExistence(src, dest, edgeList)) {
EdgeListNode *edge = newEdgeListNode(src, dest);
edge->next = edgeList->head;
edgeList->head = edge;
}
}
/**
* Removes the first edge node from the edge list, if the list is not empty.
*
* This function removes the first edge node from the edge list represented by 'edgeList',
* if the list is not empty. The memory occupied by the removed edge node is freed.
* The function assumes that 'edgeList' is a valid EdgeList structure.
*
* @param edgeList A pointer to the EdgeList structure representing the list of edges.
*/
void popFromEdgeList(EdgeList *edgeList) {
EdgeListNode *headEdgeListNode = edgeList->head;
if (headEdgeListNode != NULL) {
edgeList->head = headEdgeListNode->next;
//free(headEdgeListNode->edge);
free(headEdgeListNode);
return;
}
return;
}
/**
* Compares two edges for equality.
*
* This function compares two edges 'e1' and 'e2' to check if they are equal or represent the same edge,
* regardless of the directionality. It returns 1 if the edges are equal or represent the same edge,
* and 0 otherwise.
*
* @param e1 A pointer to the first Edge structure to be compared.
* @param e2 A pointer to the second Edge structure to be compared.
* @return 1 if the edges are equal or represent the same edge, 0 otherwise.
*/
int compareEdge(Edge *e1, Edge *e2) {
return ((e1->src == e2->src && e1->dest == e2->dest) || (e1->src == e2->dest && e1->dest == e2->src));
}
/**
* Verify if an edge exists in the given edge list.
*
* This function checks if an edge with the given source 'src' and destination 'dest' exists in the specified edge list.
* It traverses the edge list and compares each edge's source and destination nodes with the provided values.
*
* @param src The source node of the edge to be checked.
* @param dest The destination node of the edge to be checked.
* @param edgeList A pointer to the EdgeList where the edge existence will be verified.
* @return 1 if the edge (src, dest) or (dest, src) is found in the edge list, 0 otherwise.
*/
int verifyEdgeExistence(int src, int dest, EdgeList *edgeList) {
struct EdgeListNode *edgeNode = edgeList->head;
while (edgeNode != NULL) {
if ((edgeNode->edge->src == src && edgeNode->edge->dest == dest) ||
(edgeNode->edge->src == dest && edgeNode->edge->dest == src))
return 1;
edgeNode = edgeNode->next;
}
return 0;
}
/* Graph Manipulation */
/**
* Generates a new edge and two nodes to connect to existing vertices in the graph.
*
* This function generates a new edge and two new nodes to define it in the graph 'graph'.
* It also connects one of the new nodes to vertices 'src1' and 'dest1', and the other to vertices 'src2' and 'dest2'.
* Before connecting the new nodes, it removes the edges (src1, dest1) and (src2, dest2) from the graph.
* The new nodes are created using spare vertices from the graph, which must have at least 2 spare vertices available.
* If there are not enough spare vertices (less than 2), the function returns 0 to indicate the operation failed.
* Otherwise, it successfully adds the new edge and nodes and returns 1.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param src1 The source vertex of the first edge to be removed.
* @param dest1 The destination vertex of the first edge to be removed.
* @param src2 The source vertex of the second edge to be removed.
* @param dest2 The destination vertex of the second edge to be removed.
* @return 1 if the operation is successful, 0 if there are not enough spare vertices in the graph.
*/
int edgeInsertion(Graph *graph, int src1, int dest1, int src2, int dest2) {
int size = 2;
int spareVertices[size];
int retrVertices = getSpareVertices(graph, spareVertices, size);
if (retrVertices < 2) {
return 0;
}
removeEdge(graph, src1, dest1);
removeEdge(graph, src2, dest2);
addEdge(graph, spareVertices[0], spareVertices[1]);
addEdge(graph, spareVertices[0], src1);
addEdge(graph, spareVertices[0], dest1);
addEdge(graph, spareVertices[1], src2);
addEdge(graph, spareVertices[1], dest2);
return 1;
}
/**
* Generates a K4- and connects it to existing vertices in the graph.
*
* This function generates a K4- (K4 with one edge removed) using six spare vertices from the graph 'graph'.
* It then connects the two degree-2 nodes of the K4- structure to two new nodes.
* The new nodes are connected to vertices 'src1' and 'dest1', and 'src2' and 'dest2' of the given graph.
* Before connecting the new nodes, it removes the edges (src1, dest1) and (src2, dest2) from the graph.
* The operation requires at least six spare vertices in the graph.
* If there are not enough spare vertices, the function returns 0 to indicate the operation failed.
* Otherwise, it successfully adds the K4- structure and connects it to the specified vertices, and returns 1.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param src1 The source vertex of the first edge to be removed.
* @param dest1 The destination vertex of the first edge to be removed.
* @param src2 The source vertex of the second edge to be removed.
* @param dest2 The destination vertex of the second edge to be removed.
* @return 1 if the operation is successful, 0 if there are not enough spare vertices in the graph.
*/
int nonAdjDiamondInsertion(Graph *graph, int src1, int dest1, int src2, int dest2) {
int size = 6;
int spareVertices[size];
int retrVertices = getSpareVertices(graph, spareVertices, size);
if (retrVertices < size) {
return 0;
}
// Create K4-
addEdge(graph, spareVertices[0], spareVertices[1]);
addEdge(graph, spareVertices[1], spareVertices[2]);
addEdge(graph, spareVertices[2], spareVertices[3]);
addEdge(graph, spareVertices[3], spareVertices[0]);
addEdge(graph, spareVertices[0], spareVertices[2]);
addEdge(graph, spareVertices[1], spareVertices[4]);
addEdge(graph, spareVertices[3], spareVertices[5]);
removeEdge(graph, src1, dest1);
removeEdge(graph, src2, dest2);
addEdge(graph, src1, spareVertices[4]);
addEdge(graph, spareVertices[4], dest1);
addEdge(graph, src2, spareVertices[5]);
addEdge(graph, spareVertices[5], dest2);
return 1;
}
/**
* Generates a K4- and connects it to existing vertices in the graph.
*
* This function generates a K4- (K4 with one edge removed) using four spare vertices from the graph 'graph'.
* It then connects the two degree-2 nodes of the K4- structure to the nodes 'src' and 'dest' of the given graph.
* Before connecting the new nodes, it removes the edge (src, dest) from the graph.
* The operation requires at least four spare vertices in the graph.
* If there are not enough spare vertices, the function returns 0 to indicate the operation failed.
* Otherwise, it successfully adds the K4- structure and connects it to the specified vertices, and returns 1.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param src The source vertex of the edge to be removed.
* @param dest The destination vertex of the edge to be removed.
* @return 1 if the operation is successful, 0 if there are not enough spare vertices in the graph.
*/
int adjDiamondInsertion(Graph *graph, int src, int dest) {
int size = 4;
int spareVertices[size];
int retrVertices = getSpareVertices(graph, spareVertices, size);
if (retrVertices < size) {
return 0;
}
// Create K4-
addEdge(graph, spareVertices[0], spareVertices[1]);
addEdge(graph, spareVertices[1], spareVertices[2]);
addEdge(graph, spareVertices[2], spareVertices[3]);
addEdge(graph, spareVertices[3], spareVertices[0]);
addEdge(graph, spareVertices[0], spareVertices[2]);
removeEdge(graph, src, dest);
addEdge(graph, src, spareVertices[3]);
addEdge(graph, spareVertices[1], dest);
return 1;
}
/**
* Generates a K4+ and connects it to existing vertices in the graph.
*
* This function generates a K4+ (K4 with one edge removed and a new node connected to the endpoints of the removed edge)
* using six spare vertices from the graph 'graph'.
* It then connects the degree-2 node of the K4+ structure to a new node, which is then connected to vertices 'src' and 'dest'
* of the given graph.
* Before connecting the new nodes, it removes the edge (src, dest) from the graph.
* The operation requires at least six spare vertices in the graph.
* If there are not enough spare vertices, the function returns 0 to indicate the operation failed.
* Otherwise, it successfully adds the K4+ structure and connects it to the specified vertices, and returns 1.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param src The source vertex of the edge to be removed.
* @param dest The destination vertex of the edge to be removed.
* @return 1 if the operation is successful, 0 if there are not enough spare vertices in the graph.
*/
int lollipopInsertion(Graph *graph, int src, int dest) {
int size = 6;
int spareVertices[size];
int retrVertices = getSpareVertices(graph, spareVertices, size);
if (retrVertices < size) {
return 0;
}
// Create K4+
addEdge(graph, spareVertices[0], spareVertices[1]);
addEdge(graph, spareVertices[1], spareVertices[2]);
addEdge(graph, spareVertices[3], spareVertices[0]);
addEdge(graph, spareVertices[0], spareVertices[2]);
addEdge(graph, spareVertices[3], spareVertices[1]);
addEdge(graph, spareVertices[3], spareVertices[4]);
addEdge(graph, spareVertices[2], spareVertices[4]);
addEdge(graph, spareVertices[4], spareVertices[5]);
removeEdge(graph, src, dest);
addEdge(graph, src, spareVertices[5]);
addEdge(graph, spareVertices[5], dest);
return 1;
}
/* Irreducibility Checks */
/**
* Performs Depth-First Search (DFS) traversal for finding bridges in a graph.
*
* This function performs a Depth-First Search (DFS) traversal starting from the vertex 'start' in the given graph 'graph'.
* It uses an array 'visited' to mark visited vertices during the traversal.
* The DFS traversal explores the edges of the graph to detect bridges.
* Bridges are the edges whose removal would increase the number of connected components in the graph.
* During the DFS, if an unvisited edge leads to an unvisited vertex, it recursively calls itself for that vertex.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param start The label of the starting vertex for DFS traversal.
* @param visited An array to mark visited vertices during DFS traversal.
*/
void dfsForBridge(Graph *graph, int start, int *visited) {
visited[start] = 1;
EdgeListNode *edgeListNode = graph->edges->head;
while (edgeListNode) {
if (edgeListNode->edge->src == start && !visited[edgeListNode->edge->dest])
dfsForBridge(graph, edgeListNode->edge->dest, visited);
if (edgeListNode->edge->dest == start && !visited[edgeListNode->edge->src])
dfsForBridge(graph, edgeListNode->edge->src, visited);
edgeListNode = edgeListNode->next;
}
/*struct AdjListNode* nodeDest = graph->nodes[start].head;
while(nodeDest != NULL){
if(!visited[nodeDest->label])
dfsForBridge(graph, nodeDest->label, visited);
nodeDest = nodeDest->next;
}*/
}
/**
* Checks if an Edge is a bridge in the graph.
*
* This function checks if the given Edge 'edge' is a bridge in the graph 'graph'.
* It temporarily removes the edge from the graph, performs Depth-First Search (DFS) traversal using 'dfsForBridge' function
* to find bridges, and then restores the edge in the graph.
* A bridge is an edge whose removal would increase the number of connected components in the graph.
* If the destination vertex of the edge is not visited after the temporary removal, the function returns 1,
* indicating that the edge is a bridge. Otherwise, it returns 0, indicating that it is not a bridge.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param edge A pointer to the Edge structure to be checked for being a bridge.
* @return 1 if 'edge' is a bridge in the graph, 0 otherwise.
*/
int isABridge(Graph *graph, Edge *edge) {
removeEdge(graph, edge->src, edge->dest);
int visited[graph->V];
for (int i = 0; i < (graph->V); i++) {
visited[i] = 0;
}
dfsForBridge(graph, edge->src, visited);
addEdge(graph, edge->src, edge->dest);
return !visited[edge->dest];
}
// Unused function
int isABridgeList(Graph *graph) {
struct EdgeListNode *edgeNode = graph->edges->head;
while (edgeNode != NULL) {
if (!isABridge(graph, edgeNode->edge))
return 0;
edgeNode = edgeNode->next;
}
return 1;
}
/**
* Checks if one of the edge endpoints is part of a triangle, and the edge isn't.
*
* This function checks if one of the endpoints of the given 'edge' is part of a triangle in the graph,
* while the 'edge' itself is not part of any triangle. It returns 1 if the condition is met,
* indicating that the edge is reducible, and 0 otherwise, indicating that the edge is irreducible.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param edge A pointer to the Edge structure representing the edge to be checked.
* @return 1 if the edge is reducible, 0 if the edge is irreducible.
*/
int irreducibilityCondition2(Graph *graph, Edge *edge) {
TriangleList *triangleList = graph->triangles;
TriangleListNode *triangleNode = triangleList->head;
int src = edge->src;
int dest = edge->dest;
while (triangleNode) {
if (!compareEdge(edge, triangleNode->triangle->e1) && !compareEdge(edge, triangleNode->triangle->e2) &&
!compareEdge(edge, triangleNode->triangle->e3))
if (src == triangleNode->triangle->e1->src || src == triangleNode->triangle->e1->dest ||
src == triangleNode->triangle->e2->src || src == triangleNode->triangle->e2->dest ||
src == triangleNode->triangle->e3->src || src == triangleNode->triangle->e3->dest ||
dest == triangleNode->triangle->e1->src || dest == triangleNode->triangle->e1->dest ||
dest == triangleNode->triangle->e2->src || dest == triangleNode->triangle->e2->dest ||
dest == triangleNode->triangle->e3->src || dest == triangleNode->triangle->e3->dest)
return 1;
triangleNode = triangleNode->next;
}
return 0;
}
/**
* Checks if both endpoints of the edge are part of the same 4-gon, and the edge isn't.
*
* This function checks if both endpoints of the given 'edge' are part of the same 4-gon (tetragon) in the graph,
* while the 'edge' itself is not part of the 4-gon. It returns 1 if the condition is met,
* indicating that the edge is reducible, and 0 otherwise, indicating that the edge is irreducible.
*
* @param graph A pointer to the Graph structure representing the graph.
* @param edge A pointer to the Edge structure representing the edge to be checked.
* @return 1 if the edge is reducible, 0 if the edge is irreducible.
*/
int irreducibilityCondition3(Graph *graph, Edge *edge) {
TetraList *tList = graph->tetragons;
int src = edge->src;
int dest = edge->dest;
TetraListNode *tNode = tList->head;
while (tNode) {
if (!compareEdge(edge, tNode->tetragon->e1) && !compareEdge(edge, tNode->tetragon->e2) &&
!compareEdge(edge, tNode->tetragon->e3) && !compareEdge(edge, tNode->tetragon->e4))
if ((src == tNode->tetragon->e1->src || src == tNode->tetragon->e1->dest ||
src == tNode->tetragon->e2->src || src == tNode->tetragon->e2->dest ||
src == tNode->tetragon->e3->src || src == tNode->tetragon->e3->dest ||
src == tNode->tetragon->e4->src || src == tNode->tetragon->e4->dest) &&
(dest == tNode->tetragon->e1->src || dest == tNode->tetragon->e1->dest ||
dest == tNode->tetragon->e2->src || dest == tNode->tetragon->e2->dest ||
dest == tNode->tetragon->e3->src || dest == tNode->tetragon->e3->dest ||
dest == tNode->tetragon->e4->src || dest == tNode->tetragon->e4->dest))
return 1;
tNode = tNode->next;
}
return 0;
}
/**
* Checks if the given graph is an irreducible graph.
*
* This function checks if the given graph 'g' is an irreducible graph by analyzing its edges and triangle/tetragon lists.
* It checks the three irreducibility conditions: whether any edge is a bridge ('isABridge'), whether there exist edges that satisfy
* 'irreducibilityCondition2', and whether there exist edges that satisfy 'irreducibilityCondition3'.
*
* @param g A pointer to the Graph structure representing the graph to be checked.
* @return 1 if the graph is irreducible, 0 if the graph is reducible.
*/
int isAIrreducibleGraph(Graph *g) {
TriangleList *trianglelist = (TriangleList *) malloc(sizeof(TriangleList));
trianglelist->head = NULL;
findTriangles(g, 0, trianglelist);
insertTriangles(g, trianglelist);
TetraList *tetralist = (TetraList *) malloc(sizeof(TetraList));
tetralist->head = NULL;
findTetragons(g, 0, tetralist);
insertTetragons(g, tetralist);
EdgeListNode *edgeListNode = g->edges->head;
EdgeListNode *edgeListNodeSucc = NULL;
while (edgeListNode) {
edgeListNodeSucc = edgeListNode->next;
Edge *edge = edgeListNode->edge;
int b = isABridge(g, edge);
//edge = edgeListNode->edge;
int irr2 = irreducibilityCondition2(g, edge);
int irr3 = irreducibilityCondition3(g, edge);
if (!(b || irr2 || irr3))
return 0;
edgeListNode = edgeListNodeSucc;
}
return 1;
}
/**
* Initializes the given graph as an irreducible graph.
*
* This function initializes the given graph 'graph' as an irreducible graph by creating a K4 structure using spare vertices.
*
* @param graph A pointer to the Graph structure to be initialized as an irreducible graph.
*/
void initIrreducibleGraph(Graph *graph) {
int size = 4;
int spareVertices[size];
int retrVertices = getSpareVertices(graph, spareVertices, size);
if (retrVertices < size) {
return;
}
addEdge(graph, spareVertices[0], spareVertices[1]);
addEdge(graph, spareVertices[1], spareVertices[2]);
addEdge(graph, spareVertices[2], spareVertices[3]);
addEdge(graph, spareVertices[3], spareVertices[0]);
addEdge(graph, spareVertices[0], spareVertices[2]);
addEdge(graph, spareVertices[1], spareVertices[3]);
}
/* Triangle and Tetragon Operations */
/**
* Perform Depth-First Search (DFS) to find cycles of length 'n' starting from the specified vertex 'start' in the graph 'g'.
*
* @param g The Graph on which the DFS is performed.
* @param trianglelist The TriangleList to store found triangles.
* @param tetraList The TetraList to store found tetragons.
* @param pathEdgeList The current path (sequence of edges) being explored during the DFS.
* @param visited An array that keeps track of visited vertices during the DFS.
* @param n The desired length of the cycle to be found.
* @param vert The current vertex being explored.
* @param start The starting vertex of the cycle.
* @param count A pointer to an integer that keeps track of the number of cycles found.
* @param triangles A flag (1 or 0) indicating whether to look for triangles (1) or tetragons (0) in the graph.
*/
void
DFSforCycle(Graph *g, TriangleList *trianglelist, TetraList *tetraList, EdgeList *pathEdgeList, int *visited, int n,
int vert, int start, int *count, int triangles) {
// Mark the current vertex as visited
visited[vert] = 1;
// If the path of length (n-1) is found, check if it forms a cycle and save it if necessary
if (n == 0) {
// Mark vert as unvisited to make it usable again.
visited[vert] = 0;
// Check if the current path is a cycle by verifying that the last edge connects back to the starting vertex
if (verifyEdgeExistence(vert, start, g->edges)) {
EdgeListNode *edgeNode = newEdgeListNode(vert, start);
edgeNode->next = pathEdgeList->head;
pathEdgeList->head = edgeNode;
*count = *count + 1;
// Add the cycle (path) to the respective lists (trianglelist or tetraList)
if (triangles) {
EdgeListNode *edgeNode1 = pathEdgeList->head;
TriangleListNode *node = newTriangleListNode(edgeNode1->edge, edgeNode1->next->edge,
edgeNode1->next->next->edge);
/*node->next = trianglelist->head;
trianglelist->head = node;*/
insertNodeInTriangleList(node, trianglelist);
popFromEdgeList(pathEdgeList);
/*EdgeListNode * ptr = pathEdgeList->head;
pathEdgeList->head =pathEdgeList->head->next;
free(ptr);*/
} else {
EdgeListNode *edgeNode1 = pathEdgeList->head;
TetraListNode *node = newTetraListNode(edgeNode1->edge, edgeNode1->next->edge,
edgeNode1->next->next->edge, edgeNode1->next->next->next->edge);
/*node->next = tetraList->head;
tetraList->head = node;*/
insertNodeInTetraList(tetraList, node);
/*
EdgeListNode * ptr = pathEdgeList->head;
pathEdgeList->head =pathEdgeList->head->next;
free(ptr);*/
popFromEdgeList(pathEdgeList);
/*EdgeListNode * ptr = pathEdgeList->head;
ptr = pathEdgeList->head;
pathEdgeList->head =pathEdgeList->head->next;
free(ptr);*/
}
return;
} else
return;
}
// For searching every possible path of length (n-1)
for (int i = 0; i < g->current_nodes; i++)
if (!visited[i] && verifyEdgeExistence(vert, i, g->edges)) {
/* EdgeListNode* edgeNode = newEdgeListNode(i, vert);
edgeNode->next = pathEdgeList->head;
pathEdgeList->head= edgeNode;*/
// Insert the current edge into the pathEdgeList and continue the DFS
insertInEdgeList(pathEdgeList, vert, i);
// Recursive call decreasing length by 1
DFSforCycle(g, trianglelist, tetraList, pathEdgeList, visited, n - 1, i, start, count, triangles);
// Backtrack by removing the last inserted edge from the pathEdgeList
popFromEdgeList(pathEdgeList);
}
// Mark vert as unvisited to make it usable again.
visited[vert] = 0;
}
/**
* Find and store triangles in the graph 'g'.
*
* @param g The Graph in which triangles are to be found.
* @param update A flag (1 or 0) indicating whether to update the 'trianglelist' or not.
* @param trianglelist The TriangleList to store the found triangles.
*/
void findTriangles(Graph *g, int update, TriangleList *trianglelist) {
if (!update) {
EdgeList *edgeList = (EdgeList *) malloc(sizeof(EdgeList));
int visited[g->V];
int n = 3, count = 0, triangles = 1;
for (int i = 0; i < g->V; i++)
visited[i] = 0;
// Start DFS from each vertex in the graph to find triangles
for (int i = 0; i < g->current_nodes - (n - 1); i++) {
DFSforCycle(g, trianglelist, NULL, edgeList, visited, n - 1, i, i, &count, triangles);
visited[i] = 1;
}
free(edgeList);
} else {
// If 'update' is 1, it means that 'trianglelist' is already updated, so we do nothing and return.
return;
}
}
/**
* Find and enumerate tetragons in the graph 'g'.
* The tetragons found are added to the 'tetraList'.
*
* @param g The Graph in which to find tetragons.
* @param update A flag to indicate whether to update the 'tetraList' or not.
* If update=1, it means that the 'tetraList' already contains some tetragons,
* and we don't need to find new ones, so the function will return without doing anything.
* @param tetraList The TetraList to which the found tetragons are added.
*/
void findTetragons(Graph *g, int update, TetraList *tetraList) {
if (!update) {
EdgeList *edgeList = (EdgeList *) malloc(sizeof(EdgeList));
int visited[g->V];
int n = 4, count = 0, tetra = 0;
for (int i = 0; i < g->V; i++)
visited[i] = 0;
// Perform DFS for each vertex to find cycles of length (n-1)
for (int i = 0; i < g->current_nodes - (n - 1); i++) {
DFSforCycle(g, NULL, tetraList, edgeList, visited, n - 1, i, i, &count, tetra);
visited[i] = 1;
}
free(edgeList);
} else {
// If the 'tetraList' is already updated, return without doing anything
return;
}
}
/**
* Compares two triangles for equality.
*
* This function compares two triangles represented by 't1' and 't2' for equality. It checks if any of the four possible
* combinations of edges between the triangles are equal. If any combination is found to be equal, the function returns 1,
* indicating that the triangles are equal. Otherwise, it returns 0, indicating inequality.
*
* @param t1 A pointer to the first Triangle structure to be compared.
* @param t2 A pointer to the second Triangle structure to be compared.
* @return 1 if the triangles are equal (regardless of the order of edges), 0 otherwise.
*/
int compareTriangle(Triangle *t1, Triangle *t2) {
return compareEdge(t1->e1, t2->e1) && compareEdge(t1->e2, t2->e2) && compareEdge(t1->e3, t2->e3) ||
compareEdge(t1->e1, t2->e2) && compareEdge(t1->e2, t2->e1) && compareEdge(t1->e3, t2->e3) ||
compareEdge(t1->e1, t2->e1) && compareEdge(t1->e2, t2->e3) && compareEdge(t1->e3, t2->e2) ||
compareEdge(t1->e1, t2->e3) && compareEdge(t1->e2, t2->e2) && compareEdge(t1->e3, t2->e1);
}
/**
* Compares two tetragons for equality.
*
* This function compares two tetragons represented by 't1' and 't2' for equality.
* It checks if any of the seven possible combinations of edges between the tetragons are equal.
* If any combination is found to be equal, the function returns 1, indicating that the tetragons are equal.
* Otherwise, it returns 0, indicating inequality.
*
* @param t1 A pointer to the first Tetragon structure to be compared.
* @param t2 A pointer to the second Tetragon structure to be compared.
* @return 1 if the tetragons are equal (regardless of the order of edges), 0 otherwise.
*/
int compareTetragon(Tetragon *t1, Tetragon *t2) {
return compareEdge(t1->e1, t2->e1) && compareEdge(t1->e2, t2->e2) && compareEdge(t1->e3, t2->e3) &&
compareEdge(t1->e4, t2->e4) ||
compareEdge(t1->e1, t2->e2) && compareEdge(t1->e2, t2->e1) && compareEdge(t1->e3, t2->e3) &&
compareEdge(t1->e4, t2->e4) ||
compareEdge(t1->e1, t2->e1) && compareEdge(t1->e2, t2->e3) && compareEdge(t1->e3, t2->e2) &&
compareEdge(t1->e4, t2->e4) ||
compareEdge(t1->e1, t2->e3) && compareEdge(t1->e2, t2->e2) && compareEdge(t1->e3, t2->e1) &&
compareEdge(t1->e4, t2->e4) ||
compareEdge(t1->e1, t2->e4) && compareEdge(t1->e2, t2->e2) && compareEdge(t1->e3, t2->e3) &&
compareEdge(t1->e4, t2->e1) ||
compareEdge(t1->e1, t2->e4) && compareEdge(t1->e2, t2->e3) && compareEdge(t1->e3, t2->e2) &&
compareEdge(t1->e4, t2->e1) ||
compareEdge(t1->e1, t2->e1) && compareEdge(t1->e2, t2->e4) && compareEdge(t1->e3, t2->e3) &&
compareEdge(t1->e4, t2->e2);
}
/**
* Checks if a Triangle is valid.
*
* This function checks if the given Triangle 't' is a valid triangle, i.e., it consists of three distinct vertices.
* The function creates a boolean array 'v' to mark the occurrence of vertices in the triangle's edges.
* If exactly three vertices are marked as present in the triangle, the function returns 1, indicating that it is a valid triangle.
* Otherwise, it returns 0, indicating that it is not a valid triangle.
*
* @param t A pointer to the Triangle structure to be checked for validity.
* @return 1 if 't' is a valid triangle, 0 otherwise.
*/
int isATriangle(Triangle *t) {
int v[MAX_VERTICES];
for (int i = 0; i < MAX_VERTICES; i++)
v[i] = 0;
v[t->e1->src] = 1;
v[t->e1->dest] = 1;
v[t->e2->src] = 1;
v[t->e2->dest] = 1;
v[t->e3->src] = 1;
v[t->e3->dest] = 1;
int count = 0;
for (int i = 0; i < MAX_VERTICES; i++)
count += v[i];
if (count == 3)
return 1;
return 0;
}
/**
* Checks if a Tetragon is valid.
*
* This function checks if the given Tetragon 't' is a valid tetragon, i.e., it consists of four distinct vertices.
* The function creates a boolean array 'v' to mark the occurrence of vertices in the tetragon's edges.
* If exactly four vertices are marked as present in the tetragon, the function returns 1, indicating that it is a valid tetragon.
* Otherwise, it returns 0, indicating that it is not a valid tetragon.
*
* @param t A pointer to the Tetragon structure to be checked for validity.
* @return 1 if 't' is a valid tetragon, 0 otherwise.
*/
int isATetragon(Tetragon *t) {
int v[MAX_VERTICES];
for (int i = 0; i < MAX_VERTICES; i++)
v[i] = 0;
v[t->e1->src] = 1;
v[t->e1->dest] = 1;
v[t->e2->src] = 1;
v[t->e2->dest] = 1;
v[t->e3->src] = 1;
v[t->e3->dest] = 1;
v[t->e4->src] = 1;
v[t->e4->dest] = 1;
int count = 0;
for (int i = 0; i < MAX_VERTICES; i++)
count += v[i];
if (count == 4)
return 1;
return 0;
}
/**
* Verifies if a Triangle exists in the TriangleList.
*
* This function checks if the given Triangle 'triangle' exists in the TriangleList represented by 'triangleList'.
* It iterates through each Triangle in the list and compares 'triangle' for equality using 'isATriangle' and 'compareTriangle' functions.
* If an equal Triangle is found, the function returns 1, indicating existence. Otherwise, it returns 0, indicating non-existence.
*
* @param triangle A pointer to the Triangle structure to be verified.
* @param triangleList A pointer to the TriangleList structure representing the list of Triangles.
* @return 1 if 'triangle' exists in the list, 0 otherwise.
*/
int verifyTriangleExistence(Triangle *triangle, TriangleList *triangleList) {
TriangleListNode *triangleListNode = triangleList->head;
while (triangleListNode != NULL) {
if (isATriangle(triangle))
if (compareTriangle(triangle, triangleListNode->triangle))
return 1;
triangleListNode = triangleListNode->next;
}
return 0;
}
/**
* Verifies if a Tetragon exists in the TetraList.
*
* This function checks if the given Tetragon 'tetra' exists in the TetraList represented by 'tetraList'.
* It iterates through each Tetragon in the list and compares 'tetra' for validity using 'isATetragon' function
* and for equality using 'compareTetragon' function. If a valid and equal Tetragon is found, the function returns 1,
* indicating existence. Otherwise, it returns 0, indicating non-existence.
*
* @param tetraList A pointer to the TetraList structure representing the list of Tetragons.
* @param tetra A pointer to the Tetragon structure to be verified.
* @return 1 if 'tetra' exists in the list, 0 otherwise.
*/
int verifyTetragonExistence(TetraList *tetraList, Tetragon *tetra) {
TetraListNode *tetraListNode = tetraList->head;
while (tetraListNode != NULL) {
if (isATetragon(tetra))
if (compareTetragon(tetra, tetraListNode->tetragon))
return 1;
tetraListNode = tetraListNode->next;
}
return 0;
}