-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnucesGraph.h
More file actions
603 lines (463 loc) · 10.3 KB
/
Copy pathnucesGraph.h
File metadata and controls
603 lines (463 loc) · 10.3 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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
struct nGraph max;
struct vertex
{
int label;
struct vertex *next;
struct vertex *prev;
};
struct edge
{
int weight;
int head;
int tail;
struct edge *next;
struct edge *prev;
};
struct verList
{
int count;
struct vertex *head;
struct vertex *tail;
};
struct edgList
{
int count;
struct edge *head;
struct edge *tail;
};
struct nGraph
{
char label;
struct verList *V;
struct edgList *E;
};
void nGraphFree(struct nGraph *G)
{
}
void nGraphInit(struct nGraph *G, char t)
{
G->V = malloc(sizeof(struct verList));
G->V->count = 0;
G->E = malloc(sizeof(struct edgList));
G->E->count = 0;
G->label = t;
}
int searchVertex(struct nGraph *G, int search)
{
struct vertex *tmp = G->V->head;
while (tmp != NULL) {
if (tmp->label == search) {
return 1;
}
tmp = tmp->next;
}
return 0;
}
int getVertex(struct nGraph *G, int search)
{
struct vertex *tmp = G->V->head;
int count = 0;
while (tmp != NULL) {
if (tmp->label == search) {
return count;
}
tmp = tmp->next;
count++;
}
return 0;
}
int edgeExists(struct nGraph *G, int head, int tail)
{
struct edge *tmp = G->E->head;
while (tmp != NULL) {
if (((tmp->head == head && tmp->tail == tail) ||
(tmp->head == head && tmp->tail == head)) ||
((tmp->head == tail && tmp->tail == tail) ||
(tmp->head == tail && tmp->tail == head))) {
return 0;
}
tmp = tmp->next;
}
return 1;
}
void addRandomEdge(struct nGraph *G, int weight)
{
struct edge *tmp = malloc(sizeof(struct edge));
tmp->weight = weight;
int randomHead = rand() % G->V->count;
int randomTail = rand() % G->V->count;
while (randomTail == randomHead) {
randomTail = rand() % G->V->count;
}
tmp->head = randomHead;
tmp->tail = randomTail;
if (edgeExists(G, tmp->head, tmp->tail)) {
if (G->E->count == 0) {
G->E->head = tmp;
G->E->tail = tmp;
G->E->count++;
}
else {
G->E->tail->next = tmp;
G->E->tail = tmp;
G->E->count++;
}
}
}
void addEdge(struct nGraph *G, int head, int tail, int weight)
{
if (head != tail) {
if (!searchVertex(G, head)) {
printf("invalid vertex %d in edge %d->%d\n", head, head, tail);
return;
}
if (!searchVertex(G, tail)) {
printf("invalid vertex %d in edge %d->%d\n", tail, head, tail);
return;
}
struct edge *tmp = malloc(sizeof(struct edge));
tmp->weight = weight;
tmp->head = head;
tmp->tail = tail;
if (G->E->count == 0) {
G->E->head = tmp;
G->E->tail = tmp;
G->E->count++;
}
else {
G->E->tail->next = tmp;
G->E->tail = tmp;
G->E->count++;
}
}
}
int vertexSize(struct nGraph *G)
{
struct vertex *tmp = G->V->head;
int count = 0;
while(tmp != NULL) {
if (count == (G->V->count-1)) {
//printf("%d ", tmp->label);
}
else {
//printf("%d, ", tmp->label);
}
tmp = tmp->next;
count++;
}
return count;
}
void addVertex(struct nGraph *G, int c)
{
struct vertex *tmp = malloc(sizeof(struct vertex));
tmp->label = c;
if (G->V->count == 0) {
G->V->head = tmp;
G->V->tail = tmp;
G->V->count++;
}
else {
G->V->tail->next = tmp;
G->V->tail = tmp;
G->V->count++;
}
}
void makeCompleteGraph(struct nGraph *G, int size)
{
int i, j;
for (i = 0; i < size; i++)
addVertex(G, i);
for (i = 0; i < size; i++) {
for (j = i; j < size; j++) {
addEdge(G, i, j, 0);
}
}
}
void listVertices(struct nGraph *G)
{
struct vertex *tmp = G->V->head;
printf("%c.V = { ", G->label);
int count = 0;
while(tmp != NULL) {
if (count == (G->V->count-1)) {
printf("%d ", tmp->label);
}
else {
printf("%d, ", tmp->label);
}
tmp = tmp->next;
count++;
}
printf("}\n");
}
void listEdges(struct nGraph *G)
{
struct edge *tmp = G->E->head;
printf("%c.E = { ", G->label);
int count = 0;
while (tmp != NULL) {
if (count == (G->E->count-1)) {
printf("%d->%d ", tmp->head, tmp->tail);
}
else {
printf("%d->%d, ", tmp->head, tmp->tail);
}
tmp = tmp->next;
count++;
}
printf("}\n");
}
/********************************************************
Below Here is the Additional Assignment Code
*********************************************************/
struct nGraph unionGrpah(struct nGraph *G1,struct nGraph *G2)
{
struct edge *tmp1 = G1->E->head;
struct edge *tmp2 = G2->E->head;
struct vertex *tmp3 = G1->V->head;
struct vertex *tmp4 = G2->V->head;
//Make New Graph
struct nGraph G4 ;
nGraphInit(&G4, 'C');
//Copying Vertex of G1 to new Graph G4
while(G1->V->head!=NULL){
addVertex(&G4,G1->V->head->label);
G1->V->head=G1->V->head->next;
}
G1->V->head=tmp3;
//Copying Edge of G1 to new Graph G4
while(tmp1!=NULL){
addEdge(&G4, tmp1->head, tmp1->tail, 0);
tmp1=tmp1->next;
}
//Union of G1 and G2 Vertex
while (G2->V->head != NULL)
{
if (!searchVertex(&G4,G2->V->head->label))
{
addVertex(&G4, G2->V->head->label);
}
G2->V->head=G2->V->head->next;
}
//Union of G1 and G2 Edges
while (tmp2 != NULL)
{
if (edgeExists(&G4, tmp2->head, tmp2->tail))
{
//printf("%d->%d, ", tmp2->head, tmp2->tail);
addEdge(&G4, tmp2->head, tmp2->tail, 0);
}
tmp2=tmp2->next;
}
//Returning G1 and G2 to their head pointers
G1->V->head=tmp3;
G2->V->head=tmp4;
return G4;
}
struct nGraph intersectGrpah(struct nGraph *G1,struct nGraph *G2)
{
struct edge *tmp1 = G1->E->head;
struct edge *tmp2 = G2->E->head;
struct vertex *tmp3 = G1->V->head;
struct vertex *tmp4 = G2->V->head;
//Make New Graph
struct nGraph G9 ;
nGraphInit(&G9, 'D');
//Intersection oof G1 and G2 Vertex
while (G2->V->head != NULL)
{
if (searchVertex(G1,G2->V->head->label))
{
//printf("%d, ", G2->V->head->label);
addVertex(&G9, G2->V->head->label);
}
G2->V->head=G2->V->head->next;
}
G2->E->head=tmp2;
//Intersection of G1 and G2 Edges
while (tmp2 != NULL)
{
if (!edgeExists(G1, tmp2->head, tmp2->tail))
{
//printf("%d->%d, ", tmp2->head, tmp2->tail);
addEdge(&G9, tmp2->head, tmp2->tail, 0);
}
tmp2=tmp2->next;
}
//Returning G1 and G2 to their head pointers
G1->V->head=tmp3;
G2->V->head=tmp4;
return G9;
}
struct nGraph ringSum(struct nGraph *G1,struct nGraph *G2)
{
struct edge *tmp1 = G1->E->head;
struct edge *tmp2 = G2->E->head;
struct vertex *tmp3 = G1->V->head;
struct vertex *tmp4 = G2->V->head;
struct edge *tmpA=malloc(sizeof(G1->E->head)) ;
struct edge *tmpB=malloc(sizeof(G2->E->head)) ;
struct edge *headA=tmpA;
struct edge *headB=tmpB;
//Make New Graph
struct nGraph G10 ;
nGraphInit(&G10, 'E');
//Copying Vertex of G1 to new Graph G10
while(G1->V->head!=NULL){
addVertex(&G10,G1->V->head->label);
G1->V->head=G1->V->head->next;
}
G1->V->head=tmp3;
//Union of G1 and G2 Vertex
while (G2->V->head != NULL)
{
if (!searchVertex(&G10,G2->V->head->label))
{
addVertex(&G10, G2->V->head->label);
}
G2->V->head=G2->V->head->next;
}
//Doing (G2-G1)Union(G1-G2) for Edges using 2 while loops
while (tmp1 != NULL)
{
if (edgeExists(G2, tmp1->head, tmp1->tail))
{
//printf("%d->%d, ", tmp1->head, tmp1->tail);
//printf("%s\n","--");
tmpA->head= tmp1->head;
tmpA->tail= tmp1->tail;
addEdge(&G10, tmp1->head, tmp1->tail, 0);
}
tmp1=tmp1->next;
}
while (tmp2 != NULL)
{
if (edgeExists(G1, tmp2->head, tmp2->tail))
{
//printf("%d->%d, ", tmp2->head, tmp2->tail);
//printf("%s\n","--");
tmpB->head= tmp2->head;
tmpB->tail= tmp2->tail;
addEdge(&G10, tmp2->head, tmp2->tail, 0);
}
tmp2=tmp2->next;
}
tmpA=headA;
tmpB=headB;
/*
while(tmpA!=NULL){
addEdge(&G10, tmpA->head, tmpA->tail, 0);
tmpA=tmpA->next;
}
while(tmpB!=NULL){
addEdge(&G10, tmpB->head, tmpB->tail, 0);
tmpB=tmpB->next;
}*/
//listEdges(&G10);
G1->V->head=tmp3;
G2->V->head=tmp4;
return G10;
}
struct nGraph neighbours(struct nGraph *G1,int c)
{
struct edge *tmp1 = G1->E->head;
struct vertex *tmp3 = G1->V->head;
//make new Graph
struct nGraph G15 ;
nGraphInit(&G15, 'F');
G1->V->head=tmp3;
//Finding Neighbours of a given vertex in Graph G1
if (searchVertex(G1,c)){
while (tmp1!=NULL){
if(tmp1->head==c){
//printf("%s %d %s %d\n", "Neighbour of ",c," :",tmp1->tail );
addVertex(&G15,tmp1->tail);
}
else if(tmp1->tail==c){
//printf("%s %d %s %d\n", "Neighbour of ",c," :",tmp1->head );
addVertex(&G15,tmp1->head);
}
else{
}
tmp1=tmp1->next;
}
}
else{
printf("%s\n","Vertex not present in this graph" );
}
return G15;
}
void BK(struct nGraph PP,struct nGraph P, struct nGraph R, struct nGraph X){
struct vertex *temporary ;
struct nGraph GV;
nGraphInit(&GV, 'G');
struct nGraph UnionOfPX;
struct nGraph NeighboursOfP;
struct nGraph tempP;//P
struct nGraph tempR;//R
struct nGraph tempX;//X
nGraphInit(&UnionOfPX, 'G');
UnionOfPX=unionGrpah(&P,&X); //PUX
int sizeOfVertexUnion=vertexSize(&UnionOfPX);
if(sizeOfVertexUnion==0){
printf("%s\n", "------------------------------------------");
if(max.V->count < R.V->count ){
nGraphInit(&max, 'M');
max=R;
}
printf("%s", "Maximal Clique : ");
listVertices(&R);
printf("%s\n", "------------------------------------------");
return ;
}
while (P.V->head != NULL)
{
sizeOfVertexUnion=vertexSize(&R);
nGraphInit(&GV, 'G');
addVertex(&GV,P.V->head->label);
nGraphInit(&NeighboursOfP, 'G');
NeighboursOfP=neighbours(&PP,P.V->head->label);
nGraphInit(&tempP, 'P');
tempP=intersectGrpah(&P,&NeighboursOfP); //P N N(v)
nGraphInit(&tempR, 'R');
tempR=unionGrpah(&R,&GV); //R U (v)
nGraphInit(&tempX, 'X');
tempX=intersectGrpah(&X,&NeighboursOfP); //X N N(v)
BK(PP,tempP,tempR,tempX);
temporary = P.V->head;
P.V->head=P.V->head->next;
free(temporary);
X=unionGrpah(&X,&GV);
}
}
struct nGraph FindingCliques(struct nGraph G1)
{
struct vertex *tmp1 = G1.V->head;
struct nGraph P ;
nGraphInit(&P, 'P');
struct nGraph X ;
nGraphInit(&X, 'X');
struct nGraph R;
nGraphInit(&R, 'R');
nGraphInit(&max, 'M');
while (G1.V->head != NULL)
{
addVertex(&P, G1.V->head->label);
G1.V->head=G1.V->head->next;
}
G1.V->head=tmp1;
clock_t tic = clock();
BK(G1,P,R,X);
clock_t toc = clock();
printf("%s\n", "==================");
//printf("%d\n", G1.E->count);
printf("%s","Maximum: ");
listVertices(&max);
printf("%s\n", "==================");
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
return G1;
}