-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
2324 lines (2042 loc) · 91.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <stdint.h>
#define PRINTTOCMD
// #define dbg
#define MAXIMUM_OUTPUT_COUNT 1
typedef intptr_t ssize_t;
ssize_t getline(char **lineptr, size_t *n, FILE *stream)
{
size_t pos;
int c;
if (lineptr == NULL || stream == NULL || n == NULL)
{
errno = EINVAL;
return -1;
}
c = getc(stream);
if (c == EOF)
{
return -1;
}
if (*lineptr == NULL)
{
*lineptr = malloc(128);
if (*lineptr == NULL)
{
return -1;
}
*n = 128;
}
pos = 0;
while (c != EOF)
{
if (pos + 1 >= *n)
{
size_t new_size = *n + (*n >> 2);
if (new_size < 128)
{
new_size = 128;
}
char *new_ptr = realloc(*lineptr, new_size);
if (new_ptr == NULL)
{
return -1;
}
*n = new_size;
*lineptr = new_ptr;
}
((unsigned char *)(*lineptr))[pos++] = c;
if (c == '\n')
{
break;
}
c = getc(stream);
}
(*lineptr)[pos] = '\0';
return pos;
}
struct Node
{
int vertex;
int weight;
struct Node *nextNode;
} Node;
struct Graph
{
int noOfVertices;
int *adjacencyMatrix;
char *description;
} Graph;
struct Vector
{
void *data;
int size;
int currentNumberOfElements;
} Vector;
int createVector_Int(struct Vector *newVector, int size)
{
/*
Accepts a pointer to already allocated memory for struct Vector
*/
if (NULL == newVector)
return -1;
newVector->data = malloc(size * sizeof(int));
if (NULL == newVector->data)
{
printf("Error allocating memory for Int Vector");
exit(EXIT_FAILURE);
}
newVector->currentNumberOfElements = 0;
newVector->size = size;
return 1;
}
int createVector_Graph(struct Vector *newVector, int size)
{
/*
Accepts a pointer to already allocated memory for struct Vector
*/
if (NULL == newVector)
return -1;
newVector->data = malloc(size * sizeof(struct Graph));
if (NULL == newVector->data)
{
printf("Error allocating memory for Graph Vector");
exit(EXIT_FAILURE);
}
newVector->currentNumberOfElements = 0;
newVector->size = size;
return 1;
}
int createVector_Vector(struct Vector *newVector, int size)
{
/*
Accepts a pointer to already allocated memory for struct Vector
*/
if (NULL == newVector)
return -1;
newVector->data = malloc(size * sizeof(struct Vector));
if (NULL == newVector->data)
{
printf("Error allocating memory for Vector Vector");
exit(EXIT_FAILURE);
}
newVector->currentNumberOfElements = 0;
newVector->size = size;
return 1;
}
int pushBackVector_Int(struct Vector *vector, int value)
{
if (NULL == vector || NULL == vector->data)
return -1;
if (vector->currentNumberOfElements == vector->size)
{
int *newArray = realloc(vector->data, 2 * sizeof(int) * vector->size);
if (NULL == newArray)
{
printf("Error allocating memory when adding new value to int Vector");
exit(EXIT_FAILURE);
}
vector->data = newArray;
vector->size = 2 * vector->size;
}
if (NULL != vector->data)
{
*((int *)vector->data + vector->currentNumberOfElements) = value;
vector->currentNumberOfElements = vector->currentNumberOfElements + 1;
}
return 1;
}
int pushBackVector_Vector(struct Vector *vector, struct Vector value)
{
if (NULL == vector || NULL == vector->data)
return -1;
if (vector->currentNumberOfElements == vector->size)
{
struct Vector *newArray = realloc(vector->data, sizeof(struct Vector) * 2 * vector->size);
if (NULL == newArray)
{
printf("Error allocating memory when adding new value to Vector Vector");
exit(EXIT_FAILURE);
}
vector->data = newArray;
vector->size = 2 * vector->size;
}
if (NULL != vector->data)
{
*((struct Vector *)vector->data + vector->currentNumberOfElements) = value;
vector->currentNumberOfElements = vector->currentNumberOfElements + 1;
}
return 1;
}
int pushBackVector_Graph(struct Vector *vector, struct Graph value)
{
if (NULL == vector || NULL == vector->data)
return -1;
if (vector->currentNumberOfElements == vector->size)
{
struct Graph *newArray = realloc(vector->data, sizeof(struct Graph) * 2 * vector->size);
if (NULL == newArray)
{
printf("Error allocating memory when adding new value to Graph Vector");
exit(EXIT_FAILURE);
}
vector->data = newArray;
vector->size = 2 * vector->size;
}
if (NULL != vector->data)
{
*((struct Graph *)vector->data + vector->currentNumberOfElements) = value;
vector->currentNumberOfElements = vector->currentNumberOfElements + 1;
}
return 1;
}
int removeElementVector_Int(struct Vector *vector, int value)
{
if (NULL == vector || NULL == vector->data)
return -1;
for (int i = 0; i < vector->currentNumberOfElements; i++)
{
if (*((int *)vector->data + i) == value)
{
for (int j = i; j < vector->currentNumberOfElements - 1; j++)
{
*((int *)vector->data + j) = *((int *)vector->data + j + 1);
}
vector->currentNumberOfElements = vector->currentNumberOfElements - 1;
return 1;
}
}
return -1;
}
int findElementVector_Int(struct Vector *vector, int value)
{
/*
-1 if no element found
index if element found
*/
if (NULL == vector || NULL == vector->data)
return -1;
for (int i = 0; i < vector->currentNumberOfElements; i++)
{
if (*((int *)vector->data + i) == value)
return i;
}
return -1;
}
struct Vector popVector_Vector(struct Vector *vector)
{
struct Vector pop = *((struct Vector *)(vector->data) + vector->currentNumberOfElements - 1);
vector->currentNumberOfElements = vector->currentNumberOfElements - 1;
return pop;
}
void printVector_Int(struct Vector vector)
{
if (NULL == vector.data)
return;
printf("[ ");
for (int i = 0; i < vector.currentNumberOfElements; i++)
{
printf("%d ", *((int *)vector.data + i));
}
printf("]\n");
}
void printVector_Vector(struct Vector vector)
{
if (NULL == vector.data)
return;
for (int i = 0; i < vector.currentNumberOfElements; i++)
{
printf("[ ");
for (int j = 0; j < ((struct Vector *)(vector.data) + i)->currentNumberOfElements; j++)
{
printf("%d ", *(((int *)((struct Vector *)(vector.data) + i)->data) + j));
}
printf("]\n");
}
}
void printVector_Vector_Max(struct Vector vector)
{
if (NULL == vector.data)
return;
int max = 0;
for (int i = 0; i < vector.currentNumberOfElements; i++)
{
if (max < ((struct Vector *)(vector.data) + i)->currentNumberOfElements)
max = ((struct Vector *)(vector.data) + i)->currentNumberOfElements;
}
for (int i = 0; i < vector.currentNumberOfElements; i++)
{
if (max != ((struct Vector *)(vector.data) + i)->currentNumberOfElements)
continue;
printf("[ ");
for (int j = 0; j < ((struct Vector *)(vector.data) + i)->currentNumberOfElements; j++)
{
printf("%d ", *(((int *)((struct Vector *)(vector.data) + i)->data) + j));
}
printf("]\n");
}
}
int saveToFileVector_Vector(struct Vector vector, FILE *fptr)
{
if (NULL == vector.data || NULL == fptr)
return -1;
for (int i = 0; i < vector.currentNumberOfElements; i++)
{
fprintf(fptr, "[ ");
for (int j = 0; j < ((struct Vector *)(vector.data) + i)->currentNumberOfElements; j++)
{
fprintf(fptr, "%d ", *(((int *)((struct Vector *)(vector.data) + i)->data) + j));
}
fprintf(fptr, "]\n");
}
return 1;
}
int saveToFileVector_Vector_Max(struct Vector vector, FILE *fptr)
{
if (NULL == vector.data || NULL == fptr)
return -1;
int max = 0;
for (int i = 0; i < vector.currentNumberOfElements; i++)
{
if (max < ((struct Vector *)(vector.data) + i)->currentNumberOfElements)
max = ((struct Vector *)(vector.data) + i)->currentNumberOfElements;
}
for (int i = 0; i < vector.currentNumberOfElements; i++)
{
if (max != ((struct Vector *)(vector.data) + i)->currentNumberOfElements)
continue;
fprintf(fptr, "[ ");
for (int j = 0; j < ((struct Vector *)(vector.data) + i)->currentNumberOfElements; j++)
{
fprintf(fptr, "%d ", *(((int *)((struct Vector *)(vector.data) + i)->data) + j));
}
fprintf(fptr, "]\n");
}
return 1;
}
void printGraph(struct Graph graph)
{
printf("Amount of vertices: %d\n", graph.noOfVertices);
for (int i = 0; i < graph.noOfVertices; i++)
{
for (int j = 0; j < graph.noOfVertices; j++)
{
printf("%d ", graph.adjacencyMatrix[i * graph.noOfVertices + j]);
}
printf("\n");
}
printf("Additional information: %s\n", graph.description);
printf("-------------------------------------------------\n\n");
}
void printGraphs(struct Graph *graphs, int noOfGraphs)
{
for (int i = 0; i < noOfGraphs; i++)
{
printf("------------------Graph %d----------------------\n", i);
printGraph(graphs[i]);
}
}
int countGraphEdges(struct Graph *graph)
{
int noOfEdges = 0;
for (int i = 0; i < graph->noOfVertices; i++)
{
for (int j = 0; j < graph->noOfVertices; j++)
{
if (graph->adjacencyMatrix[i * graph->noOfVertices + j] != 0)
noOfEdges = noOfEdges + graph->adjacencyMatrix[i * graph->noOfVertices + j];
}
}
return noOfEdges;
}
void printGraphs_Max(struct Graph *graphs, int noOfGraphs, int limit)
{
if (0 == limit)
limit = noOfGraphs;
int max = graphs[0].noOfVertices + countGraphEdges(graphs);
for (int i = 1; i < noOfGraphs; i++)
{
if (max < graphs[i].noOfVertices + countGraphEdges(graphs + i))
max = graphs[i].noOfVertices + countGraphEdges(graphs + i);
}
int count = 0;
for (int i = 0; i < noOfGraphs; i++)
{
if (0 != limit && count == limit)
break;
if (graphs[i].noOfVertices + countGraphEdges(graphs + i) == max)
{
if (limit > 1)
printf("------------------Graph %d----------------------\n", i);
int noOfEdges = 0;
noOfEdges = countGraphEdges(graphs + i);
printf("Graph is of size %d with %d vertices and %d edges\n", noOfEdges + graphs[i].noOfVertices, graphs[i].noOfVertices, noOfEdges);
printf("Adjacency matrix:\n");
printGraph(graphs[i]);
count++;
}
}
}
void saveToFileGraph(struct Graph *graph, FILE *output_file)
{
for (int i = 0; i < graph->noOfVertices; i++)
{
for (int j = 0; j < graph->noOfVertices; j++)
{
fprintf(output_file, "%d ", graph->adjacencyMatrix[i * graph->noOfVertices + j]);
}
fprintf(output_file, "\n");
}
fprintf(output_file, "Additional information: %s\n", graph->description);
fprintf(output_file, "-------------------------------------------------\n\n");
}
void saveToFileGraphs_Max(struct Graph *graphs, FILE *outputFile, int noOfGraphs, int limit)
{
if (0 == limit)
limit = noOfGraphs;
int max = graphs[0].noOfVertices + countGraphEdges(graphs);
for (int i = 1; i < noOfGraphs; i++)
{
int edges = +countGraphEdges(graphs + i);
if (max < graphs[i].noOfVertices + edges)
max = graphs[i].noOfVertices + edges;
}
int count = 0;
for (int i = 0; i < noOfGraphs; i++)
{
if (0 != limit && count == limit)
break;
int noOfEdges = countGraphEdges(graphs + i);
if (graphs[i].noOfVertices + noOfEdges == max)
{
if (limit > 1)
fprintf(outputFile, "------------------Graph %d----------------------\n", i);
fprintf(outputFile, "Graph is of size %d with %d vertices and %d edges\n", noOfEdges + graphs[i].noOfVertices, graphs[i].noOfVertices, noOfEdges);
fprintf(outputFile, "Adjacency matrix:\n");
saveToFileGraph(graphs + i, outputFile);
count++;
}
}
}
void freeGraph(struct Graph *graph)
{
if (NULL == graph)
return;
graph->noOfVertices = 0;
free(graph->adjacencyMatrix);
free(graph->description);
}
void readGraphsFromFile(FILE *filePtr, int *noOfGraphs, struct Vector *graphsVector)
{
size_t lineLength = 0;
char *line = NULL;
int bytesRead = 0;
int noOfVertices = 0;
int noOfGraphsInFile = 0;
// Read number of graphs in a file
bytesRead = getline(&line, &lineLength, filePtr);
if (0 < bytesRead)
{
noOfGraphsInFile = strtol(line, NULL, 10);
#ifdef dbg
printf("file: %s\n", line);
printf("Graphs: %d\n", noOfGraphsInFile);
#endif // dbg
}
for (int i = 0; i < noOfGraphsInFile; i++)
{
bytesRead = getline(&line, &lineLength, filePtr);
if (0 >= bytesRead)
continue;
struct Graph newGraph;
noOfVertices = strtol(line, NULL, 10);
if (0 == noOfVertices)
{
bytesRead = getline(&line, &lineLength, filePtr);
if (0 >= bytesRead)
{
printf("Input file is not in a accepted format\n");
exit(EXIT_FAILURE);
}
noOfVertices = strtol(line, NULL, 10);
}
newGraph.noOfVertices = noOfVertices;
#ifdef dbg
printf("file: %s\n", line);
printf("Vertices in graph %d: %d \n", i, newGraph.noOfVertices);
#endif // dbg
newGraph.adjacencyMatrix = calloc(noOfVertices * noOfVertices, sizeof(int));
if (NULL == newGraph.adjacencyMatrix)
{
printf("Error: Couldn't allocate memory for new adjacency lists\n");
exit(EXIT_FAILURE);
}
#ifdef dbg
printf("New adj list[0]: %d\n", newGraph.adjacencyLists[0]);
#endif // dbg
for (int j = 0; j < noOfVertices; j++)
{
bytesRead = getline(&line, &lineLength, filePtr);
if (0 < bytesRead)
{
char *ptr = strtok(line, " ");
int k = 0;
while (NULL != ptr)
{
int weight = strtol(ptr, NULL, 10);
if (weight > 0)
{
newGraph.adjacencyMatrix[j * noOfVertices + k] = weight;
}
#ifdef dbg
printf("file: %s\n", ptr);
printf("new vertex in adj list: %d\n", newGraph.adjacencyLists[j * newGraph.noOfVertices + k]);
#endif // dbg
ptr = strtok(NULL, " ");
k++;
}
}
}
int totalDescriptionSize = 0;
bytesRead = getline(&line, &lineLength, filePtr);
if (0 < bytesRead && line[0] != '\r' && line[0] != '\n')
{
newGraph.description = malloc(sizeof(char) * (bytesRead + 1));
totalDescriptionSize = bytesRead;
if (NULL == newGraph.description)
{
printf("Error: Couldn't allocate memory for graph description\n");
exit(EXIT_FAILURE);
}
strncpy(newGraph.description, line, bytesRead);
bytesRead = getline(&line, &lineLength, filePtr);
while (bytesRead > 0 && line[0] != '\r' && line[0] != '\n')
{
char *newDescription = malloc(sizeof(char) * (totalDescriptionSize + bytesRead + 1));
for (int l = 0; l < totalDescriptionSize; l++)
{
newDescription[l] = newGraph.description[l];
}
for (int l = 0; l < bytesRead; l++)
{
newDescription[l + totalDescriptionSize] = line[l];
}
totalDescriptionSize = totalDescriptionSize + bytesRead;
free(newGraph.description);
newGraph.description = newDescription;
bytesRead = getline(&line, &lineLength, filePtr);
}
// strncpy(newGraph.description, line, bytesRead);
newGraph.description[totalDescriptionSize] = '\0';
if (newGraph.description[totalDescriptionSize - 1] == '\n')
newGraph.description[totalDescriptionSize - 1] = '\0';
#ifdef dbg
printf("Additional graph information: %s\n", newGraph.description);
#endif // dbg
}
else
{
newGraph.description = malloc(sizeof(char));
if (NULL == newGraph.description)
{
printf("Error: Couldn't allocate memory for graph description\n");
exit(EXIT_FAILURE);
}
newGraph.description[0] = '\0';
}
pushBackVector_Graph(graphsVector, newGraph);
}
*noOfGraphs = *noOfGraphs + noOfGraphsInFile;
free(line);
}
int isVertexInsideList(struct Node *iterator, int desiredVertex)
{
while (NULL != iterator)
{
if (iterator->vertex == desiredVertex)
return 1;
iterator = iterator->nextNode;
}
return 0;
}
struct Graph *modularProduct(struct Graph *G, struct Graph *H)
{
/*
DONE
1. Iterate over matrix G, over it's adjacencyLists
2. For each AdjacencyList of graph G:
1. Iterate over AdjacencyLists from graph H
2. Take element from Adjacency list from graph G, for each element from adjacency list from graph H it will have an edge, Push it to graph's GxH adjacency list
3. So far we would have all multiplied elements with condition 1. "u is adjacent with u' and v is adjacent with v'"
4. The above is DONE, what's left is: Need to figure out a way to add edges that satisfy the second condition: 'u is not adjacent with u' and v is not adjacent with v''
1. Keeping in mind that: Any two vertices (u, v) and (u' , v' ) are adjacent in the modular product of G and H if and only if u is distinct from u', v is distinct from v'
2. The way I would do this is to:
1. Iterate over all vertices of GH, by creating two for loops iterating over vertices of G and H
2. If vertex from GH (indexed by the two new for loops) corresponding to given graph is not present in that graphs's adjacency list and same for the other graph then add that vertex to adjacency list as long as it satisfies the distinct condition (4.1)
5. DONE: Only potentially: Efficiency perhaps could be improved. When we check if a vertex is not in given adjacency list then also maybe one could check if it's in both instead of doing it separately.
6. DONE: Handle directed graphs. Edges should be in both directions.
*/
if (NULL == G || NULL == H || NULL == G->adjacencyMatrix || NULL == H->adjacencyMatrix)
{
printf("Error: Couldn't multiply matrices, given matrices don't contain enough information\n");
return NULL;
}
struct Graph *GH = malloc(sizeof(struct Graph));
if (NULL == GH)
{
printf("Error: Couldn't allocate memory for graph product\n");
exit(EXIT_FAILURE);
}
GH->description = malloc(sizeof(char));
if (NULL == GH->description)
{
printf("Error: Couldn't allocate memory for graph product description\n");
exit(EXIT_FAILURE);
}
GH->description[0] = '\0';
GH->noOfVertices = G->noOfVertices * H->noOfVertices;
GH->adjacencyMatrix = calloc(GH->noOfVertices * GH->noOfVertices, sizeof(int));
if (NULL == GH->adjacencyMatrix)
{
printf("Error: Couldn't allocate memory for graph product adjacency lists\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < G->noOfVertices; i++) // 1. 2.
{
for (int j = 0; j < H->noOfVertices; j++) // 2.1
{
for (int k = 0; k < G->noOfVertices; k++) // 4.1.2
{
if (k != i && G->adjacencyMatrix[i * G->noOfVertices + k] == 0 && G->adjacencyMatrix[k * G->noOfVertices + i] == 0)
{
for (int l = 0; l < H->noOfVertices; l++)
{
if (j != l && H->adjacencyMatrix[j * H->noOfVertices + l] == 0 && H->adjacencyMatrix[l * H->noOfVertices + j] == 0)
{
GH->adjacencyMatrix[(i * H->noOfVertices + j) * GH->noOfVertices + k * H->noOfVertices + l] = 1;
}
}
}
else if (k != i && G->adjacencyMatrix[i * G->noOfVertices + k] != 0 && G->adjacencyMatrix[k * G->noOfVertices + i] != 0)
{
for (int l = 0; l < H->noOfVertices; l++)
{
if (j != l && H->adjacencyMatrix[j * H->noOfVertices + l] != 0 && H->adjacencyMatrix[l * H->noOfVertices + j] != 0)
{
GH->adjacencyMatrix[(i * H->noOfVertices + j) * GH->noOfVertices + k * H->noOfVertices + l] = H->adjacencyMatrix[j * H->noOfVertices + l] * H->adjacencyMatrix[l * H->noOfVertices + j];
}
}
}
}
}
}
return GH;
};
void toUndirectedGraph(struct Graph G)
{
/*
DONE
1. DONE: Write a function to remove a node from a list.
2. Iterate over all adjacency lists.
1. For each adjacency list, for each node, check if there is an edge in the other direction.
1. If not remove the node.
3. Return the resulting new graph.
*/
for (int i = 0; i < G.noOfVertices; i++)
{
for (int j = 0; j < G.noOfVertices; j++)
{
if ((i == j && G.adjacencyMatrix[i * G.noOfVertices + j] != 0) || (G.adjacencyMatrix[i * G.noOfVertices + j] != 0 && G.adjacencyMatrix[j * G.noOfVertices + i] == 0))
{
G.adjacencyMatrix[i * G.noOfVertices + j] = 0;
}
}
}
}
int bronKerbosch(struct Vector R, struct Vector P, struct Vector X, struct Graph *graph, struct Vector *bronResult)
{
/*
1. DONE: Write a function to transform a directed graph to undirected graph (just remove the single edges)
2. DONE: Write needed datastructures to hold vertices, I guess we could use a List to dynamically store a list of vertices, or we could implement a vector.
3. DONE: Write the Bron-Kerbosch
4. DONE: Test it.
5. DONE: Free memory, use valgrind :)
*/
#ifdef dbg
printVector_Int(R);
printVector_Int(P);
printVector_Int(X);
printf("\n");
#endif
if (0 == P.currentNumberOfElements && 0 == X.currentNumberOfElements)
{
// printf("Maximal Clique: ");
// printVector_Int(R);
pushBackVector_Vector(bronResult, R);
free(P.data);
free(X.data);
return 1;
}
struct Node *iterator = NULL;
int pivot = -1;
if (0 < P.currentNumberOfElements)
pivot = *((int *)P.data);
else if (0 < X.currentNumberOfElements)
pivot = *((int *)X.data);
struct Vector toIterateOver;
createVector_Int(&toIterateOver, P.currentNumberOfElements);
for (int i = 0; i < P.currentNumberOfElements; i++)
{
pushBackVector_Int(&toIterateOver, *((int *)P.data + i));
}
if (-1 != pivot)
{
for (int i = 0; i < graph->noOfVertices; i++)
{
if (graph->adjacencyMatrix[pivot * graph->noOfVertices + i] != 0)
{
removeElementVector_Int(&toIterateOver, i);
}
}
while (NULL != iterator)
{
iterator = iterator->nextNode;
}
}
int i = 0;
while (0 < toIterateOver.currentNumberOfElements)
{
// R ⋃ {v}
struct Vector rPlusV;
createVector_Int(&rPlusV, R.currentNumberOfElements + 1);
for (int j = 0; j < R.currentNumberOfElements; j++)
{
pushBackVector_Int(&rPlusV, *((int *)R.data + j));
}
pushBackVector_Int(&rPlusV, *((int *)toIterateOver.data + i));
#ifdef dbg
printVector_Int(R);
printVector_Int(P);
printVector_Int(X);
printf("%d\n", *((int *)P.data + i));
#endif
// P ⋂ N(v)
struct Vector pAndVEdges;
createVector_Int(&pAndVEdges, P.size);
for (int j = 0; j < P.currentNumberOfElements; j++)
{
if (graph->adjacencyMatrix[*((int *)toIterateOver.data + i) * graph->noOfVertices + *((int *)P.data + j)] != 0)
{
pushBackVector_Int(&pAndVEdges, *((int *)P.data + j));
}
}
// X ⋂ N(v)
struct Vector xAndVEdges;
createVector_Int(&xAndVEdges, X.size);
for (int j = 0; j < X.currentNumberOfElements; j++)
{
if (graph->adjacencyMatrix[*((int *)toIterateOver.data + i) * graph->noOfVertices + *((int *)X.data + j)] != 0)
{
pushBackVector_Int(&xAndVEdges, *((int *)X.data + j));
}
}
#ifdef dbg
printVector_Int(rPlusV);
printVector_Int(pAndVEdges);
printVector_Int(xAndVEdges);
printf("\n");
#endif
// BronKerbosch2(R ⋃ {v}, P ⋂ N(v), X ⋂ N(v))
bronKerbosch(rPlusV, pAndVEdges, xAndVEdges, graph, bronResult);
// X := X ⋃ {v}
pushBackVector_Int(&X, *((int *)toIterateOver.data + i));
#ifdef dbg
printf("Removed: %d\n", *((int *)P.data + i));
#endif
// P := P \ {v}
removeElementVector_Int(&P, *((int *)toIterateOver.data + i));
removeElementVector_Int(&toIterateOver, *((int *)toIterateOver.data + i));
#ifdef dbg
printVector_Int(P);
printf("\n");
#endif
// free(pAndVEdges.data);
// if (!result)
// free(rPlusV.data);
// free(xAndVEdges.data);
}
free(P.data);
free(R.data);
free(X.data);
free(toIterateOver.data);
return 0;
}
int iterBronKerbosch(struct Vector R, struct Vector P, struct Vector X, struct Graph *graph, struct Vector *bronResult)
{
/*
1. DONE: Test it.
*/
struct Vector stack;
createVector_Vector(&stack, 3);
pushBackVector_Vector(&stack, R);
pushBackVector_Vector(&stack, P);
pushBackVector_Vector(&stack, X);
while (0 < stack.currentNumberOfElements)
{
struct Vector currentX = popVector_Vector(&stack);
struct Vector currentP = popVector_Vector(&stack);
struct Vector currentR = popVector_Vector(&stack);
/*
printVector_Int(currentR);
printVector_Int(currentP);
printVector_Int(currentX);
printf("\n");
*/
if (0 == currentP.currentNumberOfElements && 0 == currentX.currentNumberOfElements)
{
// printf("Maximal Clique: ");
// printVector_Int(R);
// free(currentR.data);
free(currentX.data);
free(currentP.data);
pushBackVector_Vector(bronResult, currentR);
}
else
{
if (0 < currentP.currentNumberOfElements)
{
struct Vector pNotV;
createVector_Int(&pNotV, currentP.currentNumberOfElements - 1);
for (int j = 1; j < currentP.currentNumberOfElements; j++)
{
pushBackVector_Int(&pNotV, *((int *)currentP.data + j));
}
struct Vector xAndV;
createVector_Int(&xAndV, currentX.currentNumberOfElements + 1);
for (int j = 0; j < currentX.currentNumberOfElements; j++)
{
pushBackVector_Int(&xAndV, *((int *)currentX.data + j));
}
pushBackVector_Int(&xAndV, *((int *)currentP.data));
struct Vector rPlusV;
createVector_Int(&rPlusV, currentR.currentNumberOfElements + 1);
for (int j = 0; j < currentR.currentNumberOfElements; j++)
{
pushBackVector_Int(&rPlusV, *((int *)currentR.data + j));
}
pushBackVector_Int(&rPlusV, *((int *)currentP.data));
struct Vector pAndVEdges;
createVector_Int(&pAndVEdges, currentP.size);
for (int j = 0; j < currentP.currentNumberOfElements; j++)
{
if (graph->adjacencyMatrix[*((int *)currentP.data) * graph->noOfVertices + *((int *)currentP.data + j)] != 0)
{
pushBackVector_Int(&pAndVEdges, *((int *)currentP.data + j));
}
}
struct Vector xAndVEdges;
createVector_Int(&xAndVEdges, currentX.size);
for (int j = 0; j < currentX.currentNumberOfElements; j++)
{
if (graph->adjacencyMatrix[*((int *)currentP.data) * graph->noOfVertices + *((int *)currentX.data + j)] != 0)
{
pushBackVector_Int(&xAndVEdges, *((int *)currentX.data + j));
}
}
// copy R!!
struct Vector newR;
createVector_Int(&newR, currentR.currentNumberOfElements);
for (int i = 0; i < currentR.currentNumberOfElements; i++)
{
pushBackVector_Int(&newR, *((int *)currentR.data + i));
}
pushBackVector_Vector(&stack, newR);
pushBackVector_Vector(&stack, pNotV);
pushBackVector_Vector(&stack, xAndV);
/*
printf("pushed:\n");
printVector_Int(newR);printVector_Int(pNotV);
printVector_Int(xAndV);
printf("\n");
*/
pushBackVector_Vector(&stack, rPlusV);
pushBackVector_Vector(&stack, pAndVEdges);
pushBackVector_Vector(&stack, xAndVEdges);
/*
printf("pushed:\n");
printVector_Int(rPlusV);
printVector_Int(pAndVEdges);
printVector_Int(xAndVEdges);
printf("\n");
*/
}
free(currentR.data);
free(currentX.data);
free(currentP.data);
}
}
free(stack.data);
return 0;
}
int iterPivotBronKerbosch(struct Vector R, struct Vector P, struct Vector X, struct Graph *graph, struct Vector *bronResult)