forked from iRitwik/algo11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment02.c
More file actions
516 lines (467 loc) · 11.2 KB
/
Copy pathassignment02.c
File metadata and controls
516 lines (467 loc) · 11.2 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
/*
Name- Ritwik Yadav
Roll Number- 10CS30034
Assignment 2- Graphs, trees and heaps!
*/
//importing necessary header files
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
//defines the node of a binary tree which stores the binomial tree in the first child next sibling representation.
typedef struct _bintreenode
{
int d;
int nodenum;
struct _bintreenode *left, *right;
}bnode;
//defines the heap node which stores the binomial heap
typedef struct _heapnode
{
int val;
struct _heapnode *next;
bnode* bintree;
}hnode;
//insertion sort function to sort the array of degree sequences in decreasing order
void sort_ins(int* d, int n)
{
int i,j,r;
int k=0; //stores the index till where the array has been sorted.
for(i=k+1; i<n; i++)
{
int ele=d[i];
for(j=0; j<=k; j++)
{
if(ele>d[j])
break;
}
for(r=k; r>=j; r--)
{
d[r+1]=d[r];
}
d[j]=ele;
k++;
}
}
//This function joins two binary trees
bnode* joinTree(bnode *a, bnode *b)
{
bnode *c;
if (a==NULL) return b;
else if (b==NULL) return a;
else
{
if ((a->d)>(b->d))
{
if (a->left!=NULL)
b->right=a->left;
a->left=b;
c=a;
}
else
{
if (b->left!=NULL)
a->right=b->left;
b->left=a;
c=b;
}
return c;
}
}
//this function merges two binomial heaps into one single binomial heap
hnode* mergeheap(hnode* a, hnode* b)
{
int flag=-1; //flag is used to keep track of which heap is shorter
int preval=-5; //this variable keeps track of the previous order of the tree encountered
hnode *i,*j; bnode *c; //i and j are traversal variables. c stores the carry over
if ((a->next==NULL) && (b->next==NULL)) return (hnode*)malloc(sizeof(hnode));
//if both heaps are empty, we return an empty heap
else
{
if(a->next!=NULL)
{
i=a->next;
}
else flag=0; //setting flag to 0 if heap1 is empty
if(b->next!=NULL)
{
j=b->next;
}
else flag=1; //setting flag to 1 if heap2 is empty
}
c=NULL;
hnode *res, *result; //stores the resultant heap
res=(result=(hnode*)malloc(sizeof(hnode)));
while(flag==-1)
{
//this loop runs so long as either one or both heaps gets empty
if(((i->val)>(preval+1)) && ((j->val)>(preval+1)) && (c!=NULL))
{ //appropriately placing carryover in its correct position
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=preval+1;
res->bintree=c;
c=NULL;
}
if((i->val)==(j->val))
{
if(c!=NULL)
{
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=i->val;
res->bintree=c; //placing carry resulting from previous addition
c=joinTree((i->bintree), (j->bintree)); //updating carry
preval=i->val; //updating preval
i=i->next; //incrementing pointer
j=j->next; //incrementing pointer
}
else
{
c=joinTree((i->bintree), (j->bintree)); //updating carry
preval=i->val; //updating preval
i=i->next; //incrementing pointer
j=j->next; //incrementing pointer
}
}
else if((i->val)<(j->val))
{
if(c==NULL)
{
//if there was no carry from previous additions, creating a new node
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=i->val;
res->bintree=i->bintree;
preval=i->val; //updating preval
i=i->next; //incrementing pointer
}
else
{
//otherwise the carry needs to be updated
c=joinTree(c,i->bintree); //updating carry
preval=i->val; //updating preval
i=i->next; //incrementing pointer
}
}
else
{
if(c==NULL)
{
//if there was no carry from previous additions, creating a new node
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=j->val;
res->bintree=j->bintree;
preval=j->val; //updating preval
j=j->next; //incrementing pointer
}
else
{
//otherwise the carry needs to be updated
c=joinTree(c,j->bintree);
preval=j->val; //updating preval
j=j->next; //updating pointer
}
}
//setting value of flag according to which heap is shorter
if(i==NULL) flag=0;
else if (j==NULL) flag=1;
}
if (flag==0)
{
//flag=0 implies heap1 is shorter
while(j!=NULL)
{
//adding the rest of the nodes in heap2 to the result
if((j->val)==(preval+1))
{
if(c==NULL)
{
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=j->val;
res->bintree=j->bintree;
preval=j->val;
j=j->next;
}
else
{
c=joinTree(c,j->bintree);
preval=j->val;
j=j->next;
}
}
else if(c!=NULL)
{
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=preval+1;
res->bintree=c;
preval++;
c=NULL;
}
else preval++; //incrementing preval
}
if(c!=NULL)
{
//if a carry over is still left, a new node is added to the end
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=preval+1;
res->bintree=c;
}
}
if (flag==1)
{
//flag=1 implies heap2 is shorter
while(i!=NULL)
{ //adding rest of the nodes of heap1 to the result
if((i->val)==(preval+1))
{
if(c==NULL)
{
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=i->val;
res->bintree=i->bintree;
preval=i->val;
i=i->next;
}
else
{
c=joinTree(c,i->bintree);
preval=i->val;
i=i->next;
}
}
else if(c!=NULL)
{
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=preval+1;
res->bintree=c;
preval++;
c=NULL;
}
else preval++;
}
if(c!=NULL)
{
//adding a new node to end if the carryover still exists
res->next=(hnode*)malloc(sizeof(hnode));
res=res->next;
res->val=preval+1;
res->bintree=c;
}
}
res->next=NULL; //ending the result
return result;
}
hnode* makeheap(int *d_arr, int i, int j)
{
//divide and conquer algorithm to create a heap from the degree sequences given
int k;
if(i==j)
{
bnode *p=(bnode*)malloc(sizeof(bnode)); //creating a single node
p->d=d_arr[i];
p->nodenum=i;
p->left=NULL;
p->right=NULL;
hnode *rr, *rrr;
//creating an heap with just one node
rrr=(rr=(hnode*)malloc(sizeof(hnode)));
rr->next=(hnode*)malloc(sizeof(hnode));
rr=rr->next;
rr->val=0;
rr->bintree=p;
rr->next=NULL;
return rrr;
}
k=(i+j)/2;
hnode *h1, *h2;
h1=makeheap(d_arr, i, k);
h2=makeheap(d_arr, k+1, j);
return mergeheap(h1,h2); //merging the two heaps with half nodes each to a single heap
}
//function to insert a node into a heap
hnode* insert(bnode *p, hnode *j)
{
//creates an empty heap with one node
hnode *rr, *rrr;
rr=(rrr=(hnode*)malloc(sizeof(hnode)));
rr->next=(hnode*)malloc(sizeof(hnode));
rr=rr->next;
rr->val=0;
rr->bintree=p;
rr->next=NULL;
return mergeheap(rrr,j); //merging it with the already existent heap
}
//this function returns the pointer to the heap node whose next node has the node with greatest degree
hnode* findmax(hnode *heap)
{
int maxdeg=-1; //since degree is positive, we initially set degree to a negative value
hnode *p=heap;
if (heap->next==NULL) return heap; //if heap is empty, empty heap is returned
heap=heap->next;
while (heap->next!=NULL)
{
//finding the node with the greatest degree
if ((heap->next->bintree->d)>maxdeg)
{
p=heap;
maxdeg=p->next->bintree->d;
}
heap=heap->next;
}
return p;
}
//this function deletes the node with the maximum degree
hnode* delmax(hnode *heap)
{
hnode *h=findmax(heap); //finding the address of the maximum degree node in the heap
if (h->next==NULL) return heap; //if the heap is empty, there is nothing to delete
hnode *h1=(hnode*)malloc(sizeof(hnode));
hnode *h2=h1;
int heval=h->next->val; //storing the degree of the tree
bnode *b, *bd;
b=(bd=h->next->bintree->left);
int i,j;
for(i=0; i<heval; i++)
{
//storing the children of the root in another heap
h1->next=(hnode*)malloc(sizeof(hnode));
h1=h1->next;
h1->val=i;
for(j=1; j<=(heval-i-2); j++)
b=b->right;
if(b->right!=NULL)
h1->bintree=b->right;
else h1->bintree=b;
b->right=NULL;
b=bd;
}
h1->next=NULL;
h->next=h->next->next; //deleting the heapnode which has the root node with maximum degree
return mergeheap(heap, h2); //merging the heap of children with the original heap and max. heapnode deleted
}
int main()
{
int n, sum; //n is the number of vertices in the graph
int *deg; //array which sotres the degree sequence
int i,j,flag; //i, j are loop variables
//flag is used to keep track if the degree sequence is graphic using Erdos Gallai criterion
printf("Enter the number of vertices=");
scanf("%d",&n);
deg=(int*)malloc(n*sizeof(int));
srand((unsigned int) time(NULL)); //seeding the random number generator with a time function
for(i=0; i<n; i++)
{
/* //uncomment this region and comment the line which follows to manually enter the degree sequence
printf("Enter Degree=");
scanf("%d", °[i]);
printf("\n");
*/
deg[i]=(int)rand()%n;
sum+=deg[i];
}
sort_ins(deg, n); //sorting the degree sequence in descending order
printf("\n+++ Sequence Generated:\n");
for(i=0; i<n; i++)
{
//printing the generated degree sequence
if(i!=(n-1))
printf("%d,",deg[i]);
else
printf("%d\n",deg[i]);
}
flag=0;
//Applying the Erdos-Gallai criterion to check if the degree sequence is graphic
if((sum%2)!=0)
{
flag=1;
}
for(i=0; i<n; i++)
{
int lsum=0;
for(j=0; j<=i; j++)
{
lsum+=deg[j];
}
int rsum=i*(i+1);
for(j=i+1; j<n; j++)
{
rsum+=(deg[j]<(i+1))?deg[j]:(i+1);
}
if (lsum>rsum)
{
flag=1;
break;
}
}
if (flag==1)
{
printf("\n*** Error: The sequence is not graphic...\n");
exit(0); //exiting if the degree sequence is graphic
}
else printf("\n+++ Degree sequence is graphic...");
//The Havel Hakimi algorithm is implemented here
//initialising an empty graph
int **graph=(int**)malloc(n*sizeof(int*));
for(i=0; i<n; i++)
{
graph[i]=(int*)malloc(n*sizeof(int));
for(j=0; j<n; j++)
graph[i][j]=0;
}
hnode *h=makeheap(deg, 0, n-1); //creating the heap from the given degree sequence
printf("\n+++ Degree sequence stored in heap...");
hnode *dum;
while(h->next!=NULL)
{
dum=findmax(h); ///finding the node with maximum degree.
int d=dum->next->bintree->d;
int u=dum->next->bintree->nodenum;
h=delmax(h); //deleting node with maximum degree
hnode *h1=(hnode*)malloc(sizeof(hnode));
for(i=1; i<=d; i++)
{ // making d connections with other nodes
hnode *maxh=findmax(h);
int r=maxh->next->bintree->d;
int v=maxh->next->bintree->nodenum;
h=delmax(h);
//updating the graph
graph[u][v]=1;
graph[v][u]=1;
if(r>1)
{
bnode *bn=(bnode*)malloc(sizeof(bnode));
bn->d=r-1;
bn->nodenum=v;
bn->left=NULL;
bn->right=NULL;
h1=insert(bn,h1);
}
}
h=mergeheap(h,h1); //recreating the heap from which d elements were deleted to create the necessary connections in the graph
}
printf("\n+++ Graph generated...");
flag=0; //now the flag serves as a tool to check correctness of the graph
//checking validity of the graph generated
for(i=0; i<n; i++)
{
int rowsum=0;
for(j=0; j<n; j++)
{
rowsum+=graph[i][j];
}
if (rowsum!=deg[i]) flag-=1; //each rowsum should match the corresponding degree of each node
}
if(flag!=0)
{
printf("+++ Graph generated is invalid...");
printf("flag=%d",flag);
}
else printf("\n+++ Correctness verified...\n");
}