-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmygraph.c
642 lines (609 loc) · 14.9 KB
/
mygraph.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
/*
* mygraph.c
*
* Copyright 2013 Ashok shankar Das <ashok.s.das@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
////////////////////////////////////////////////////////////////
// adjcency list implementation of graph //
// copied and modified some portions from internet source. //
// released as free collected by ashok.s.das@gmail.com //
// some adition modification and adoption is done to the //
// original code floating in internet sources //
// can be covered under GPL-v2, LGPL, or similar free licences//
////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
///////////////////////////////////////////////
// GRAPH implementation
///////////////////////////////////////////////
// A structure to represent an adjacency list node
typedef struct adj_list_node
{
int dest; // destination node
float weight; // weight of the edge
struct adj_list_node* next;
}adj_list_node;
// A structure to represent an adjacency liat
typedef struct adjlist
{
adj_list_node *head; // pointer to head node of list
}adj_list;
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
typedef struct graph
{
int num_vertex; // number of vertices
adj_list* array; // array of adjecency lists
int *degree_array; // array of degree of nodes
float *bw_array;
}graph;
// A utility function to create a new adjacency list node
adj_list_node* new_adj_list_node(int dest)
{
struct adj_list_node* new_node = (struct adj_list_node*) malloc(sizeof(struct adj_list_node));
new_node->dest = dest;
new_node->next = NULL;
new_node->weight =0.0;
return new_node;
}
// A utility function that creates a graph of V vertices and returns a pointer to it
graph* create_graph(int num_nodes)
{
graph* g = (graph*) malloc(sizeof(graph));
g->num_vertex = num_nodes;
// Create an array of adjacency lists. Size of array will be num_nodes
g->array = (adj_list*) malloc(num_nodes * sizeof(adj_list));
int i;
g->degree_array = (int *)malloc(num_nodes * sizeof(int));
g->bw_array = (float *)malloc(num_nodes * sizeof(float));
for (i = 0; i < num_nodes; ++i)
{
g->array[i].head = NULL; // Initialize each adjacency list as empty by making head as NULL
g->degree_array[i] = 0; // initialize degree of each node as 0
g->bw_array[i] = 0.0;
}
return g;
}
void free_graph(graph *g)
{
if(g != NULL)
{
free(g->degree_array);
int i;
for (i = 0; i < g->num_vertex; ++i)
{
adj_list_node *temp,*itr = g->array[i].head;
while(itr)
{
temp = itr;
itr = itr->next;
free(temp);
}
}
}
}
// Adds an edge 'src to dst' of weight 'w' to graph 'g'
void add_edge(graph *g, int src, int dest, float w)
{
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
adj_list_node* newNode = new_adj_list_node(dest);
newNode->weight = w;
newNode->next = g->array[src].head;
g->array[src].head = newNode;
g->degree_array[src]++; // increase the degree of source by one
g->degree_array[dest]++; // increase the degree of dest by one
g->bw_array[src] += w;
g->bw_array[dest] += w;
/* // uncomment if undirected graph is needed
// Since graph is undirected, add an edge from dest to src also
newNode = new_adj_list_node(src);
newNode->next = g->array[dest].head;
g->array[dest].head = newNode;
*/
}
// A utility function to print the adjacenncy list representation of graph
void print_graph(graph* g)
{
int v;
printf("The Graph represented by adjecent list is\n");
for (v = 0; v < g->num_vertex; ++v)
{
adj_list_node* itr = g->array[v].head;
printf("Vertex [%d]{Deg:%d} ", v+1,g->degree_array[v]);
while (itr)
{
printf("-> %d( %f)", itr->dest+1,itr->weight);
itr = itr->next;
}
printf("\n");
}
printf("=========================================\n");
}
void write_graph_to_file(graph *g, char *fname)
{
FILE *fp = fopen(fname, "w");
if(fp == NULL)
{
printf("Error: Unable to create file %s to write graph\n",fname);
return;
}
char fromstr[14000]="", tostr[14000]="", wtstr[14000]="", temp[40]="";
int numedges =0;
fprintf(fp,"###The Graph represented by adjecent list is\n");
int v;
fprintf(fp,"cores = %d\n",g->num_vertex);
for(v = 0; v<g->num_vertex; v++)
{
adj_list_node* itr = g->array[v].head;
while(itr!=NULL)
{
sprintf(temp,"%d,",v);
strcat(fromstr,temp);
sprintf(temp,"%d,",itr->dest);
strcat(tostr,temp);
sprintf(temp,"%f,",itr->weight);
strcat(wtstr,temp);
numedges += 1;
itr = itr->next;
}
}
fromstr[strlen(fromstr)-1]='\0';
tostr[strlen(tostr)-1]='\0';
wtstr[strlen(wtstr)-1]='\0';
fprintf(fp,"edges = %d\n",numedges);
fprintf(fp,"froms = [%s]\n",fromstr);
fprintf(fp,"tos = [%s]\n",tostr);
fprintf(fp,"weights= [%s]\n",wtstr);
fclose(fp);
}
// convert a graph 'g' to adjecency matrix 'adjmat'
void graph2adjmat(graph *g, float adjmat[g->num_vertex][g->num_vertex])
{
int i,j;
i = g->num_vertex;
float w;
bzero(adjmat, i * i * sizeof(float)); // clear out the region
for(i=0; i<g->num_vertex; i++)
{
adj_list_node *itr = g->array[i].head;
while(itr != NULL)
{
j= itr->dest;
w = itr->weight;
adjmat[i][j] = w;
itr = itr->next;
}
}
//return adjmat;
}
// converts an adjecent matrix 'mat' to a graph and returns a pointer to graph
graph *adjmat2graph(int rows,float mat[][rows])
{
graph *g;
int i,j;
g = create_graph(rows);
if(g == NULL)
{
printf("Error: Insufficient memory to allocate for graph\n");
return(NULL);
}
for(i = 0; i<rows; i++)
{
for(j = 0; j<rows; j++)
{
if(mat[i][j] != 0.0)
add_edge(g,i,j,mat[i][j]);
}
}
return g;
}
#if 0
// NO MORE NEEDED AS WE INBUILT THE DEGREE ARRAY INTO THE STRUCTURE AND UPDATE IT EVERY TIME WE ADD AN EDGE
// creates and returns an array containing degree of all nodes in a graph 'g'
int *degree_of_nodes_array(graph *g)
{
int i;
int *arr = (int*)malloc(g->num_vertex * sizeof(int));
if(arr == NULL)
{
printf("Error: Insufficient memory\n");
return NULL;
}
bzero(arr,g->num_vertex * sizeof(int));
for(i = 0; i < g->num_vertex; i++)
{
adj_list_node *itr = g->array[i].head;
while(itr)
{
arr[i]++;
arr[itr->dest]++;
itr = itr->next;
}
}
return (arr);
}
#endif
// utility routine to print adjcent matrix
void print_adjmatrix(int dim,float mat[dim][dim])
{
int i,j;
printf("===========================Bandwidth matrix=============================\n\n");
for(i = 0; i<dim; i++)
{
for(j = 0; j<dim; j++)
{
printf("%3.3f ",mat[i][j]);
}
printf("\n");
}
}
// returns degree of node 'node' in graph 'g'
int get_degree(graph *g, int node)
{
if(node<0 || node>g->num_vertex)
return -1;
return(g->degree_array[node]);
}
// returns weight of edge from src to dest in graph 'g'
float get_weight(int src, int dest, graph *g)
{
adj_list_node* pCrawl = g->array[src].head;
while (pCrawl)
{
if(pCrawl->dest == dest)
return (pCrawl->weight);
pCrawl = pCrawl->next;
}
return 0.0;
}
int *get_neighbours(int core, graph *g)
{
int *neighbours = (int *)malloc(sizeof(int)*get_degree(g,core));
int subscript = 0;
if(neighbours == NULL)
{
return NULL;
}
adj_list_node *itr = g->array[core].head;
while(itr)
{
neighbours[subscript] = itr->dest;
subscript++;
itr = itr->next;
}
int i;
for(i = 0; i<g->num_vertex; i++)
{
itr = g->array[i].head;
while(itr)
{
if(itr->dest == core)
{
neighbours[subscript] = i;
subscript++;
}
itr = itr->next;
}
}
return neighbours;
}
//////////////////////////////// GRAPH implementation ends //////////////////////////////////////
////////////////////////////////////////////////////////
///////////// MERGGE SORT //////////////////////////////
////////////////////////////////////////////////////////
// merge-sort to sort the adcency list
/* function prototypes */
adj_list_node* SortedMerge(adj_list_node* a, adj_list_node* b);
void FrontBackSplit(adj_list_node* source, adj_list_node** frontRef, adj_list_node** backRef);
/* sorts the linked list by changing next pointers (not data) */
void MergeSort(adj_list_node** headRef)
{
adj_list_node* head = *headRef;
adj_list_node* a;
adj_list_node* b;
/* Base case -- length 0 or 1 */
if ((head == NULL) || (head->next == NULL))
{
return;
}
/* Split head into 'a' and 'b' sublists */
FrontBackSplit(head, &a, &b);
/* Recursively sort the sublists */
MergeSort(&a);
MergeSort(&b);
/* answer = merge the two sorted lists together */
*headRef = SortedMerge(a, b);
}
/* See http://geeksforgeeks.org/?p=3622 for details of this function */
adj_list_node* SortedMerge(adj_list_node* a, adj_list_node* b)
{
adj_list_node* result = NULL;
/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
/* Pick either a or b, and recur */
if (a->weight >= b->weight)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}
/* Split the nodes of the given list into front and back halves,
and return the two lists using the reference parameters.
If the length is odd, the extra node should go in the front list.
Uses the fast/slow pointer strategy. */
void FrontBackSplit(adj_list_node* source,
adj_list_node** frontRef, adj_list_node** backRef)
{
adj_list_node* fast;
adj_list_node* slow;
if (source==NULL || source->next==NULL)
{
/* length < 2 cases */
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->next;
/* Advance 'fast' two nodes, and advance 'slow' one node */
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
/* 'slow' is before the midpoint in the list, so split it in two at that point. */
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
}
////////////////// MergeSort ends ///////////////////////////
void sort_graph_by_weight(graph *g)
{
int i;
for(i = 0; i<g->num_vertex; i++)
{
MergeSort(&g->array[i].head);
}
}
////////////////////////////////////////////////////////
///////////////// ARRAY INDEXER ////////////////////////
////////////////////////////////////////////////////////
#define ASCENDING 1
#define DESCENDING 0
// my_index takes an integer array, sizeof array, and order ASCENDING or DESCENDING
// returns the index array
int *my_index(int *array, int size, int order)
{
int i,j,ctr=0;
int *b = (int*)malloc(sizeof(int) * size);
for(i = 0; i < size; i++ )
{
for(j=0;j<size;j++)
{
if(array[i]>array[j])
ctr++;
}
for(j=0;j<i;j++)
{
if(array[i]==array[j] && i!=j)
ctr++;
}
if(order == DESCENDING)
b[size-ctr-1]=i;
else
b[ctr]=i;
ctr=0;
}
return(b);
}
/////////////////// INDEXER ends ///////////////////////
typedef struct edge
{
int vs,ve;
float weight;
struct edge *next;
}edge;
typedef struct egraph
{
int num_edges;
edge *head;
}egraph;
egraph *init_egraph()
{
egraph *eg = (egraph *)malloc(sizeof(egraph));
if(eg == NULL)
{
printf("Error: unable to allocate memory for egarph structure in %s at %d\n",__FILE__,__LINE__);
return NULL;
}
eg->num_edges = 0;// currently no edges
eg->head = NULL; // -do-
return (eg);
}
void free_egraph(egraph *eg)
{
edge *temp,*temp1;
temp = eg->head;
while(temp)
{
temp1 = temp;
temp = temp->next;
free(temp1);
}
eg->num_edges = 0;
eg->head = NULL;
free(eg);
}
void add_egedge(egraph *eg,int vs, int ve, float w)
{
edge *e = (edge *)malloc(sizeof(edge));
if(e == NULL)
{
printf("Error: unable to allocate memory for edge in %s at %d\n",__FILE__,__LINE__);
return;
}
e->vs = vs;
e->ve = ve;
e->weight = w;
e->next = eg->head;
eg->head = e;
eg->num_edges++;
}
void display_egraph(egraph *eg)
{
if(eg->num_edges <= 0)
return;
printf("Edge graph : ");
printf("Number of edges = %d\n",eg->num_edges);
edge *temp = eg->head;
printf("Vs Ve wt\n");
printf("======================\n");
while(temp)
{
printf("[%3d %3d %9.4f]\n",temp->vs+1,temp->ve+1,temp->weight);
temp = temp->next;
}
printf("=========================================\n");
}
edge *get_edge_from_egraph(int num,egraph *eg)
{
edge *temp = eg->head;
while(num)
{
temp = temp->next;
num--;
}
return(temp);
}
void display_edge(edge *e)
{
printf("[%3d %3d %9.4f]\n",e->vs+1,e->ve+1,e->weight);
}
egraph *graph_to_egraph(graph *g)
{
egraph *eg;
eg = init_egraph();
int v;
for (v = 0; v < g->num_vertex; ++v)
{
adj_list_node* itr = g->array[v].head;
while (itr)
{
add_egedge(eg,v,itr->dest,itr->weight);
itr = itr->next;
}
}
return(eg);
}
edge *_move( edge *x )
{
edge *n, *p, *ret;
p = x;
n = x->next;
ret = n;
while( n != NULL && x->weight < n->weight ) {
p = n;
n = n->next;
}
/* we now move the top item between p and n */
p->next = x;
x->next = n;
return ret;
}
edge* _sort(edge *start)
{
if( start == NULL ) return NULL;
start->next = _sort(start->next);
if( start->next != NULL && start->weight < start->next->weight )
{
start = _move( start );
}
return start;
}
void sort_egraph(egraph *eg)
{
eg->head = _sort(eg->head);
}
#ifdef TEST_SYSTEM
// main function to test the routines
int main()
{
// create the graph given in above fugure
int i,j;
int a[20];
// for test purpose pip data is used
graph* g = create_graph(8);
add_edge(g, 0, 4,64.0);
add_edge(g, 1, 0,128.0);
add_edge(g, 1, 2,64.0);
add_edge(g, 2, 3,64.0);
add_edge(g, 3, 6,64.0);
add_edge(g, 4, 5,64.0);
add_edge(g, 5, 6,64.0);
add_edge(g, 6, 7,64.0);
//add_edge(g, 4, 2,17.0);
// print the adjacency list representation of the above graph
printf("The unsorted graph : \n");
print_graph(g);
float b[g->num_vertex][g->num_vertex]; // the adjecency matrix
graph2adjmat(g,b);
print_adjmatrix(g->num_vertex,b);
graph *g1 = adjmat2graph(g->num_vertex, b);
print_graph(g1);
for(i=0 ; i<g1->num_vertex; i++)
{
printf("%d-> %d\n",i,get_degree(g1,i));
int *neighbours = get_neighbours(i,g1);
for(j = 0; j<get_degree(g1,i); j++)
{
printf(" %d ",neighbours[j]);
}
free (neighbours);
printf("\n");
}
sort_graph_by_weight(g1);
print_graph(g1);
printf("\n");
egraph *eg = graph_to_egraph(g1);
display_egraph(eg);
sort_egraph(eg);
display_egraph(eg);
//write_graph_to_file(g,"gtof.txt");
free_egraph(eg);
return 0;
}
#endif