-
Notifications
You must be signed in to change notification settings - Fork 64
/
graph.c
1940 lines (1606 loc) · 61.3 KB
/
graph.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
/*!
* \file
*
* \brief Various routines with dealing with sparse graphs
*
* \author George Karypis
* \version\verbatim $Id: graph.c 22415 2019-09-05 16:55:00Z karypis $ \endverbatim
*/
#include <GKlib.h>
#define OMPMINOPS 50000
/*************************************************************************/
/*! Allocate memory for a graph and initializes it
\returns the allocated graph. The various fields are set to NULL.
*/
/**************************************************************************/
gk_graph_t *gk_graph_Create()
{
gk_graph_t *graph;
graph = (gk_graph_t *)gk_malloc(sizeof(gk_graph_t), "gk_graph_Create: graph");
gk_graph_Init(graph);
return graph;
}
/*************************************************************************/
/*! Initializes the graph.
\param graph is the graph to be initialized.
*/
/*************************************************************************/
void gk_graph_Init(gk_graph_t *graph)
{
memset(graph, 0, sizeof(gk_graph_t));
graph->nvtxs = -1;
}
/*************************************************************************/
/*! Frees all the memory allocated for a graph.
\param graph is the graph to be freed.
*/
/*************************************************************************/
void gk_graph_Free(gk_graph_t **graph)
{
if (*graph == NULL)
return;
gk_graph_FreeContents(*graph);
gk_free((void **)graph, LTERM);
}
/*************************************************************************/
/*! Frees only the memory allocated for the graph's different fields and
sets them to NULL.
\param graph is the graph whose contents will be freed.
*/
/*************************************************************************/
void gk_graph_FreeContents(gk_graph_t *graph)
{
gk_free((void *)&graph->xadj, &graph->adjncy,
&graph->iadjwgt, &graph->fadjwgt,
&graph->ivwgts, &graph->fvwgts,
&graph->ivsizes, &graph->fvsizes,
&graph->vlabels,
LTERM);
}
/**************************************************************************/
/*! Reads a sparse graph from the supplied file
\param filename is the file that stores the data.
\param format is the graph format. The supported values are:
GK_GRAPH_FMT_METIS, GK_GRAPH_FMT_IJV.
\param hasvals is 1 if the input file has values
\param numbering is 1 if the input file numbering starts from one
\param isfewgts is 1 if the edge-weights should be read as floats
\param isfvwgts is 1 if the vertex-weights should be read as floats
\param isfvsizes is 1 if the vertex-sizes should be read as floats
\returns the graph that was read.
*/
/**************************************************************************/
gk_graph_t *gk_graph_Read(char *filename, int format, int hasvals,
int numbering, int isfewgts, int isfvwgts, int isfvsizes)
{
ssize_t i, k, l;
size_t nfields, nvtxs, nedges, fmt, ncon, lnlen;
ssize_t *xadj;
int32_t ival, *iinds=NULL, *jinds=NULL, *ivals=NULL, *adjncy, *iadjwgt;
float fval, *fvals=NULL, *fadjwgt;
int readsizes=0, readwgts=0, readvals=0;
char *line=NULL, *head, *tail, fmtstr[256];
FILE *fpin=NULL;
gk_graph_t *graph=NULL;
if (!gk_fexists(filename))
gk_errexit(SIGERR, "File %s does not exist!\n", filename);
switch (format) {
case GK_GRAPH_FMT_METIS:
fpin = gk_fopen(filename, "r", "gk_graph_Read: fpin");
do {
if (gk_getline(&line, &lnlen, fpin) <= 0)
gk_errexit(SIGERR, "Premature end of input file: file:%s\n", filename);
} while (line[0] == '%');
fmt = ncon = 0;
nfields = sscanf(line, "%zu %zu %zu %zu", &nvtxs, &nedges, &fmt, &ncon);
if (nfields < 2)
gk_errexit(SIGERR, "Header line must contain at least 2 integers (#vtxs and #edges).\n");
nedges *= 2;
if (fmt > 111)
gk_errexit(SIGERR, "Cannot read this type of file format [fmt=%zu]!\n", fmt);
sprintf(fmtstr, "%03zu", fmt%1000);
readsizes = (fmtstr[0] == '1');
readwgts = (fmtstr[1] == '1');
readvals = (fmtstr[2] == '1');
numbering = 1;
ncon = (ncon == 0 ? 1 : ncon);
graph = gk_graph_Create();
graph->nvtxs = nvtxs;
graph->xadj = gk_zmalloc(nvtxs+1, "gk_graph_Read: xadj");
graph->adjncy = gk_i32malloc(nedges, "gk_graph_Read: adjncy");
if (readvals) {
if (isfewgts)
graph->fadjwgt = gk_fmalloc(nedges, "gk_graph_Read: fadjwgt");
else
graph->iadjwgt = gk_i32malloc(nedges, "gk_graph_Read: iadjwgt");
}
if (readsizes) {
if (isfvsizes)
graph->fvsizes = gk_fmalloc(nvtxs, "gk_graph_Read: fvsizes");
else
graph->ivsizes = gk_i32malloc(nvtxs, "gk_graph_Read: ivsizes");
}
if (readwgts) {
if (isfvwgts)
graph->fvwgts = gk_fmalloc(nvtxs*ncon, "gk_graph_Read: fvwgts");
else
graph->ivwgts = gk_i32malloc(nvtxs*ncon, "gk_graph_Read: ivwgts");
}
/*----------------------------------------------------------------------
* Read the sparse graph file
*---------------------------------------------------------------------*/
numbering = (numbering ? - 1 : 0);
for (graph->xadj[0]=0, k=0, i=0; i<nvtxs; i++) {
do {
if (gk_getline(&line, &lnlen, fpin) == -1)
gk_errexit(SIGERR, "Pregraphure end of input file: file while reading row %d\n", i);
} while (line[0] == '%');
head = line;
tail = NULL;
/* Read vertex sizes */
if (readsizes) {
if (isfvsizes) {
#ifdef __MSC__
graph->fvsizes[i] = (float)strtod(head, &tail);
#else
graph->fvsizes[i] = strtof(head, &tail);
#endif
if (tail == head)
gk_errexit(SIGERR, "The line for vertex %zd does not have size information\n", i+1);
if (graph->fvsizes[i] < 0)
gk_errexit(SIGERR, "The size for vertex %zd must be >= 0\n", i+1);
}
else {
graph->ivsizes[i] = strtol(head, &tail, 0);
if (tail == head)
gk_errexit(SIGERR, "The line for vertex %zd does not have size information\n", i+1);
if (graph->ivsizes[i] < 0)
gk_errexit(SIGERR, "The size for vertex %zd must be >= 0\n", i+1);
}
head = tail;
}
/* Read vertex weights */
if (readwgts) {
for (l=0; l<ncon; l++) {
if (isfvwgts) {
#ifdef __MSC__
graph->fvwgts[i*ncon+l] = (float)strtod(head, &tail);
#else
graph->fvwgts[i*ncon+l] = strtof(head, &tail);
#endif
if (tail == head)
gk_errexit(SIGERR, "The line for vertex %zd does not have enough weights "
"for the %d constraints.\n", i+1, ncon);
if (graph->fvwgts[i*ncon+l] < 0)
gk_errexit(SIGERR, "The weight vertex %zd and constraint %zd must be >= 0\n", i+1, l);
}
else {
graph->ivwgts[i*ncon+l] = strtol(head, &tail, 0);
if (tail == head)
gk_errexit(SIGERR, "The line for vertex %zd does not have enough weights "
"for the %d constraints.\n", i+1, ncon);
if (graph->ivwgts[i*ncon+l] < 0)
gk_errexit(SIGERR, "The weight vertex %zd and constraint %zd must be >= 0\n", i+1, l);
}
head = tail;
}
}
/* Read the rest of the row */
while (1) {
ival = (int)strtol(head, &tail, 0);
if (tail == head)
break;
head = tail;
if ((graph->adjncy[k] = ival + numbering) < 0)
gk_errexit(SIGERR, "Error: Invalid column number %d at row %zd.\n", ival, i);
if (readvals) {
if (isfewgts) {
#ifdef __MSC__
fval = (float)strtod(head, &tail);
#else
fval = strtof(head, &tail);
#endif
if (tail == head)
gk_errexit(SIGERR, "Value could not be found for edge! Vertex:%zd, NNZ:%zd\n", i, k);
graph->fadjwgt[k] = fval;
}
else {
ival = strtol(head, &tail, 0);
if (tail == head)
gk_errexit(SIGERR, "Value could not be found for edge! Vertex:%zd, NNZ:%zd\n", i, k);
graph->iadjwgt[k] = ival;
}
head = tail;
}
k++;
}
graph->xadj[i+1] = k;
}
if (k != nedges)
gk_errexit(SIGERR, "gk_graph_Read: Something wrong with the number of edges in "
"the input file. nedges=%zd, Actualnedges=%zd.\n", nedges, k);
gk_fclose(fpin);
gk_free((void **)&line, LTERM);
break;
case GK_GRAPH_FMT_IJV:
case GK_GRAPH_FMT_HIJV:
gk_getfilestats(filename, &nvtxs, &nedges, NULL, NULL);
if (format == GK_GRAPH_FMT_HIJV) { /* remove the #rows/#cols values and row */
nedges -= 2;
nvtxs -= 1;
}
if (hasvals == 1 && 3*nvtxs != nedges)
gk_errexit(SIGERR, "Error: The number of numbers (%zd %d) in the input file is not a multiple of 3.\n", nedges, hasvals);
if (hasvals == 0 && 2*nvtxs != nedges)
gk_errexit(SIGERR, "Error: The number of numbers (%zd %d) in the input file is not a multiple of 2.\n", nedges, hasvals);
nedges = nvtxs;
numbering = (numbering ? -1 : 0);
/* read the data into three arrays */
iinds = gk_i32malloc(nedges, "iinds");
jinds = gk_i32malloc(nedges, "jinds");
if (hasvals) {
if (isfewgts)
fvals = gk_fmalloc(nedges, "fvals");
else
ivals = gk_i32malloc(nedges, "ivals");
}
fpin = gk_fopen(filename, "r", "gk_graph_Read: fpin");
if (format == GK_GRAPH_FMT_HIJV) { /* read and ignore the #rows/#cols values */
if (fscanf(fpin, "%zd %zd", &i, &i) != 2)
gk_errexit(SIGERR, "Error: Failed to read the header line.\n");
}
for (nvtxs=0, i=0; i<nedges; i++) {
if (hasvals) {
if (isfewgts) {
if (fscanf(fpin, "%"PRId32" %"PRId32" %f", &iinds[i], &jinds[i], &fvals[i]) != 3)
gk_errexit(SIGERR, "Error: Failed to read (i, j, val) for nedge: %zd.\n", i);
}
else {
if (fscanf(fpin, "%"PRId32" %"PRId32" %"PRId32, &iinds[i], &jinds[i], &ivals[i]) != 3)
gk_errexit(SIGERR, "Error: Failed to read (i, j, val) for nedge: %zd.\n", i);
}
}
else {
if (fscanf(fpin, "%"PRId32" %"PRId32, &iinds[i], &jinds[i]) != 2)
gk_errexit(SIGERR, "Error: Failed to read (i, j) value for nedge: %zd.\n", i);
}
iinds[i] += numbering;
jinds[i] += numbering;
if (nvtxs < iinds[i])
nvtxs = iinds[i];
if (nvtxs < jinds[i])
nvtxs = jinds[i];
}
gk_fclose(fpin);
/* convert (i, j, v) into a graph format */
graph = gk_graph_Create();
graph->nvtxs = ++nvtxs;
xadj = graph->xadj = gk_zsmalloc(nvtxs+1, 0, "xadj");
adjncy = graph->adjncy = gk_i32malloc(nedges, "adjncy");
if (hasvals) {
if (isfewgts)
fadjwgt = graph->fadjwgt = gk_fmalloc(nedges, "fadjwgt");
else
iadjwgt = graph->iadjwgt = gk_i32malloc(nedges, "iadjwgt");
}
for (i=0; i<nedges; i++)
xadj[iinds[i]]++;
MAKECSR(i, nvtxs, xadj);
for (i=0; i<nedges; i++) {
adjncy[xadj[iinds[i]]] = jinds[i];
if (hasvals) {
if (isfewgts)
fadjwgt[xadj[iinds[i]]] = fvals[i];
else
iadjwgt[xadj[iinds[i]]] = ivals[i];
}
xadj[iinds[i]]++;
}
SHIFTCSR(i, nvtxs, xadj);
gk_free((void **)&iinds, &jinds, &fvals, &ivals, LTERM);
break;
default:
gk_errexit(SIGERR, "Unrecognized format: %d\n", format);
}
return graph;
}
/**************************************************************************/
/*! Writes a graph into a file.
\param graph is the graph to be written,
\param filename is the name of the output file.
\param format specifies the format of the output file.
\param numbering is either 0 or 1, indicating if the first vertex
will be numbered 0 or 1. Some formats ignore this.
*/
/**************************************************************************/
void gk_graph_Write(gk_graph_t *graph, char *filename, int format, int numbering)
{
int32_t i;
ssize_t j;
int hasvwgts, hasvsizes, hasewgts;
FILE *fpout;
if (filename)
fpout = gk_fopen(filename, "w", "gk_graph_Write: fpout");
else
fpout = stdout;
hasewgts = (graph->iadjwgt || graph->fadjwgt);
hasvwgts = (graph->ivwgts || graph->fvwgts);
hasvsizes = (graph->ivsizes || graph->fvsizes);
switch (format) {
case GK_GRAPH_FMT_METIS:
/* write the header line */
fprintf(fpout, "%d %zd", graph->nvtxs, graph->xadj[graph->nvtxs]/2);
if (hasvwgts || hasvsizes || hasewgts)
fprintf(fpout, " %d%d%d", hasvsizes, hasvwgts, hasewgts);
fprintf(fpout, "\n");
for (i=0; i<graph->nvtxs; i++) {
if (hasvsizes) {
if (graph->ivsizes)
fprintf(fpout, " %d", graph->ivsizes[i]);
else
fprintf(fpout, " %f", graph->fvsizes[i]);
}
if (hasvwgts) {
if (graph->ivwgts)
fprintf(fpout, " %d", graph->ivwgts[i]);
else
fprintf(fpout, " %f", graph->fvwgts[i]);
}
for (j=graph->xadj[i]; j<graph->xadj[i+1]; j++) {
fprintf(fpout, " %d", graph->adjncy[j]+1);
if (hasewgts) {
if (graph->iadjwgt)
fprintf(fpout, " %d", graph->iadjwgt[j]);
else
fprintf(fpout, " %f", graph->fadjwgt[j]);
}
}
fprintf(fpout, "\n");
}
break;
case GK_GRAPH_FMT_IJV:
for (i=0; i<graph->nvtxs; i++) {
for (j=graph->xadj[i]; j<graph->xadj[i+1]; j++) {
fprintf(fpout, "%d %d ", i+numbering, graph->adjncy[j]+numbering);
if (hasewgts) {
if (graph->iadjwgt)
fprintf(fpout, " %d\n", graph->iadjwgt[j]);
else
fprintf(fpout, " %f\n", graph->fadjwgt[j]);
}
else {
fprintf(fpout, " 1\n");
}
}
}
break;
default:
gk_errexit(SIGERR, "Unknown file format. %d\n", format);
}
if (filename)
gk_fclose(fpout);
}
/*************************************************************************/
/*! Returns a copy of a graph.
\param graph is the graph to be duplicated.
\returns the newly created copy of the graph.
*/
/**************************************************************************/
gk_graph_t *gk_graph_Dup(gk_graph_t *graph)
{
gk_graph_t *ngraph;
ngraph = gk_graph_Create();
ngraph->nvtxs = graph->nvtxs;
/* copy the adjacency structure */
if (graph->xadj)
ngraph->xadj = gk_zcopy(graph->nvtxs+1, graph->xadj,
gk_zmalloc(graph->nvtxs+1, "gk_graph_Dup: xadj"));
if (graph->ivwgts)
ngraph->ivwgts = gk_i32copy(graph->nvtxs, graph->ivwgts,
gk_i32malloc(graph->nvtxs, "gk_graph_Dup: ivwgts"));
if (graph->ivsizes)
ngraph->ivsizes = gk_i32copy(graph->nvtxs, graph->ivsizes,
gk_i32malloc(graph->nvtxs, "gk_graph_Dup: ivsizes"));
if (graph->vlabels)
ngraph->vlabels = gk_i32copy(graph->nvtxs, graph->vlabels,
gk_i32malloc(graph->nvtxs, "gk_graph_Dup: ivlabels"));
if (graph->fvwgts)
ngraph->fvwgts = gk_fcopy(graph->nvtxs, graph->fvwgts,
gk_fmalloc(graph->nvtxs, "gk_graph_Dup: fvwgts"));
if (graph->fvsizes)
ngraph->fvsizes = gk_fcopy(graph->nvtxs, graph->fvsizes,
gk_fmalloc(graph->nvtxs, "gk_graph_Dup: fvsizes"));
if (graph->adjncy)
ngraph->adjncy = gk_i32copy(graph->xadj[graph->nvtxs], graph->adjncy,
gk_i32malloc(graph->xadj[graph->nvtxs], "gk_graph_Dup: adjncy"));
if (graph->iadjwgt)
ngraph->iadjwgt = gk_i32copy(graph->xadj[graph->nvtxs], graph->iadjwgt,
gk_i32malloc(graph->xadj[graph->nvtxs], "gk_graph_Dup: iadjwgt"));
if (graph->fadjwgt)
ngraph->fadjwgt = gk_fcopy(graph->xadj[graph->nvtxs], graph->fadjwgt,
gk_fmalloc(graph->xadj[graph->nvtxs], "gk_graph_Dup: fadjwgt"));
return ngraph;
}
/*************************************************************************/
/*! Returns the transpose of a graph.
\param graph is the graph to be transposed.
\returns the newly created copy of the graph.
*/
/**************************************************************************/
gk_graph_t *gk_graph_Transpose(gk_graph_t *graph)
{
int32_t vi, vj;
ssize_t ei;
gk_graph_t *ngraph;
ngraph = gk_graph_Create();
ngraph->nvtxs = graph->nvtxs;
ngraph->xadj = gk_zsmalloc(graph->nvtxs+1, 0, "gk_graph_Transpose: xadj");
ngraph->adjncy = gk_i32malloc(graph->xadj[graph->nvtxs], "gk_graph_Transpose: adjncy");
if (graph->iadjwgt)
ngraph->iadjwgt = gk_i32malloc(graph->xadj[graph->nvtxs], "gk_graph_Transpose: iadjwgt");
if (graph->fadjwgt)
ngraph->fadjwgt = gk_fmalloc(graph->xadj[graph->nvtxs], "gk_graph_Transpose: fadjwgt");
for (vi=0; vi<graph->nvtxs; vi++) {
for (ei=graph->xadj[vi]; ei<graph->xadj[vi+1]; ei++)
ngraph->xadj[graph->adjncy[ei]]++;
}
MAKECSR(vi, ngraph->nvtxs, ngraph->xadj);
for (vi=0; vi<graph->nvtxs; vi++) {
for (ei=graph->xadj[vi]; ei<graph->xadj[vi+1]; ei++) {
vj = graph->adjncy[ei];
ngraph->adjncy[ngraph->xadj[vj]] = vi;
if (ngraph->iadjwgt)
ngraph->iadjwgt[ngraph->xadj[vj]] = graph->iadjwgt[ei];
if (ngraph->fadjwgt)
ngraph->fadjwgt[ngraph->xadj[vj]] = graph->fadjwgt[ei];
ngraph->xadj[vj]++;
}
}
SHIFTCSR(vi, ngraph->nvtxs, ngraph->xadj);
/* copy vertex attributes */
if (graph->ivwgts)
ngraph->ivwgts = gk_i32copy(graph->nvtxs, graph->ivwgts,
gk_i32malloc(graph->nvtxs, "gk_graph_Transpose: ivwgts"));
if (graph->ivsizes)
ngraph->ivsizes = gk_i32copy(graph->nvtxs, graph->ivsizes,
gk_i32malloc(graph->nvtxs, "gk_graph_Transpose: ivsizes"));
if (graph->vlabels)
ngraph->vlabels = gk_i32copy(graph->nvtxs, graph->vlabels,
gk_i32malloc(graph->nvtxs, "gk_graph_Transpose: ivlabels"));
if (graph->fvwgts)
ngraph->fvwgts = gk_fcopy(graph->nvtxs, graph->fvwgts,
gk_fmalloc(graph->nvtxs, "gk_graph_Transpose: fvwgts"));
if (graph->fvsizes)
ngraph->fvsizes = gk_fcopy(graph->nvtxs, graph->fvsizes,
gk_fmalloc(graph->nvtxs, "gk_graph_Transpose: fvsizes"));
return ngraph;
}
/*************************************************************************/
/*! Returns a subgraph containing a set of consecutive vertices.
\param graph is the original graph.
\param vstart is the starting vertex.
\param nvtxs is the number of vertices from vstart to extract.
\returns the newly created subgraph.
*/
/**************************************************************************/
gk_graph_t *gk_graph_ExtractSubgraph(gk_graph_t *graph, int vstart, int nvtxs)
{
ssize_t i;
gk_graph_t *ngraph;
if (vstart+nvtxs > graph->nvtxs)
return NULL;
ngraph = gk_graph_Create();
ngraph->nvtxs = nvtxs;
/* copy the adjancy structure */
if (graph->xadj)
ngraph->xadj = gk_zcopy(nvtxs+1, graph->xadj+vstart,
gk_zmalloc(nvtxs+1, "gk_graph_ExtractSubgraph: xadj"));
for (i=nvtxs; i>=0; i--)
ngraph->xadj[i] -= ngraph->xadj[0];
ASSERT(ngraph->xadj[0] == 0);
if (graph->ivwgts)
ngraph->ivwgts = gk_i32copy(nvtxs, graph->ivwgts+vstart,
gk_i32malloc(nvtxs, "gk_graph_ExtractSubgraph: ivwgts"));
if (graph->ivsizes)
ngraph->ivsizes = gk_i32copy(nvtxs, graph->ivsizes+vstart,
gk_i32malloc(nvtxs, "gk_graph_ExtractSubgraph: ivsizes"));
if (graph->vlabels)
ngraph->vlabels = gk_i32copy(nvtxs, graph->vlabels+vstart,
gk_i32malloc(nvtxs, "gk_graph_ExtractSubgraph: vlabels"));
if (graph->fvwgts)
ngraph->fvwgts = gk_fcopy(nvtxs, graph->fvwgts+vstart,
gk_fmalloc(nvtxs, "gk_graph_ExtractSubgraph: fvwgts"));
if (graph->fvsizes)
ngraph->fvsizes = gk_fcopy(nvtxs, graph->fvsizes+vstart,
gk_fmalloc(nvtxs, "gk_graph_ExtractSubgraph: fvsizes"));
ASSERT(ngraph->xadj[nvtxs] == graph->xadj[vstart+nvtxs]-graph->xadj[vstart]);
if (graph->adjncy)
ngraph->adjncy = gk_i32copy(graph->xadj[vstart+nvtxs]-graph->xadj[vstart],
graph->adjncy+graph->xadj[vstart],
gk_i32malloc(graph->xadj[vstart+nvtxs]-graph->xadj[vstart],
"gk_graph_ExtractSubgraph: adjncy"));
if (graph->iadjwgt)
ngraph->iadjwgt = gk_i32copy(graph->xadj[vstart+nvtxs]-graph->xadj[vstart],
graph->iadjwgt+graph->xadj[vstart],
gk_i32malloc(graph->xadj[vstart+nvtxs]-graph->xadj[vstart],
"gk_graph_ExtractSubgraph: iadjwgt"));
if (graph->fadjwgt)
ngraph->fadjwgt = gk_fcopy(graph->xadj[vstart+nvtxs]-graph->xadj[vstart],
graph->fadjwgt+graph->xadj[vstart],
gk_fmalloc(graph->xadj[vstart+nvtxs]-graph->xadj[vstart],
"gk_graph_ExtractSubgraph: fadjwgt"));
return ngraph;
}
/*************************************************************************/
/*! Returns a graph that has been reordered according to the permutation.
\param[IN] graph is the graph to be re-ordered.
\param[IN] perm is the new ordering of the graph's vertices
\param[IN] iperm is the original ordering of the re-ordered graph's vertices
\returns the newly created copy of the graph.
\note Either perm or iperm can be NULL but not both.
*/
/**************************************************************************/
gk_graph_t *gk_graph_Reorder(gk_graph_t *graph, int32_t *perm, int32_t *iperm)
{
ssize_t j, jj, *xadj;
int i, k, u, v, nvtxs;
int freeperm=0, freeiperm=0;
int32_t *adjncy;
gk_graph_t *ngraph;
if (perm == NULL && iperm == NULL)
return NULL;
ngraph = gk_graph_Create();
ngraph->nvtxs = nvtxs = graph->nvtxs;
xadj = graph->xadj;
adjncy = graph->adjncy;
/* allocate memory for the different structures that are present in graph */
if (graph->xadj)
ngraph->xadj = gk_zmalloc(nvtxs+1, "gk_graph_Reorder: xadj");
if (graph->ivwgts)
ngraph->ivwgts = gk_i32malloc(nvtxs, "gk_graph_Reorder: ivwgts");
if (graph->ivsizes)
ngraph->ivsizes = gk_i32malloc(nvtxs, "gk_graph_Reorder: ivsizes");
if (graph->vlabels)
ngraph->vlabels = gk_i32malloc(nvtxs, "gk_graph_Reorder: ivlabels");
if (graph->fvwgts)
ngraph->fvwgts = gk_fmalloc(nvtxs, "gk_graph_Reorder: fvwgts");
if (graph->fvsizes)
ngraph->fvsizes = gk_fmalloc(nvtxs, "gk_graph_Reorder: fvsizes");
if (graph->adjncy)
ngraph->adjncy = gk_i32malloc(graph->xadj[nvtxs], "gk_graph_Reorder: adjncy");
if (graph->iadjwgt)
ngraph->iadjwgt = gk_i32malloc(graph->xadj[nvtxs], "gk_graph_Reorder: iadjwgt");
if (graph->fadjwgt)
ngraph->fadjwgt = gk_fmalloc(graph->xadj[nvtxs], "gk_graph_Reorder: fadjwgt");
/* create perm/iperm if not provided */
if (perm == NULL) {
freeperm = 1;
perm = gk_i32malloc(nvtxs, "gk_graph_Reorder: perm");
for (i=0; i<nvtxs; i++)
perm[iperm[i]] = i;
}
if (iperm == NULL) {
freeiperm = 1;
iperm = gk_i32malloc(nvtxs, "gk_graph_Reorder: iperm");
for (i=0; i<nvtxs; i++)
iperm[perm[i]] = i;
}
/* fill-in the information of the re-ordered graph */
ngraph->xadj[0] = jj = 0;
for (v=0; v<nvtxs; v++) {
u = iperm[v];
for (j=xadj[u]; j<xadj[u+1]; j++, jj++) {
ngraph->adjncy[jj] = perm[adjncy[j]];
if (graph->iadjwgt)
ngraph->iadjwgt[jj] = graph->iadjwgt[j];
if (graph->fadjwgt)
ngraph->fadjwgt[jj] = graph->fadjwgt[j];
}
if (graph->ivwgts)
ngraph->ivwgts[v] = graph->ivwgts[u];
if (graph->fvwgts)
ngraph->fvwgts[v] = graph->fvwgts[u];
if (graph->ivsizes)
ngraph->ivsizes[v] = graph->ivsizes[u];
if (graph->fvsizes)
ngraph->fvsizes[v] = graph->fvsizes[u];
if (graph->vlabels)
ngraph->vlabels[v] = graph->vlabels[u];
ngraph->xadj[v+1] = jj;
}
/* free memory */
if (freeperm)
gk_free((void **)&perm, LTERM);
if (freeiperm)
gk_free((void **)&iperm, LTERM);
return ngraph;
}
/*************************************************************************/
/*! This function finds the connected components in a graph.
\param graph is the graph structure
\param cptr is the ptr structure of the CSR representation of the
components. The length of this vector must be graph->nvtxs+1.
\param cind is the indices structure of the CSR representation of
the components. The length of this vector must be graph->nvtxs.
\returns the number of components that it found.
\note The cptr and cind parameters can be NULL, in which case only the
number of connected components is returned.
*/
/*************************************************************************/
int gk_graph_FindComponents(gk_graph_t *graph, int32_t *cptr, int32_t *cind)
{
ssize_t i, ii, j, jj, k, nvtxs, first, last, ntodo, ncmps;
ssize_t *xadj;
int32_t *adjncy, *pos, *todo;
int32_t mustfree_ccsr=0;
nvtxs = graph->nvtxs;
xadj = graph->xadj;
adjncy = graph->adjncy;
/* Deal with NULL supplied cptr/cind vectors */
if (cptr == NULL) {
cptr = gk_i32malloc(nvtxs+1, "gk_graph_FindComponents: cptr");
cind = gk_i32malloc(nvtxs, "gk_graph_FindComponents: cind");
mustfree_ccsr = 1;
}
/* The list of vertices that have not been touched yet.
The valid entries are from [0..ntodo). */
todo = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs, "gk_graph_FindComponents: todo"));
/* For a vertex that has not been visited, pos[i] is the position in the
todo list that this vertex is stored.
If a vertex has been visited, pos[i] = -1. */
pos = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs, "gk_graph_FindComponents: pos"));
/* Find the connected componends */
ncmps = -1;
ntodo = nvtxs; /* All vertices have not been visited */
first = last = 0; /* Point to the first and last vertices that have been touched
but not explored.
These vertices are stored in cind[first]...cind[last-1]. */
while (1) {
if (first == last) { /* Find another starting vertex */
cptr[++ncmps] = first; /* Mark the end of the current CC */
if (ntodo > 0) {
/* put the first vertex in the todo list as the start of the new CC */
GKASSERT(pos[todo[0]] != -1);
cind[last++] = todo[0];
pos[todo[0]] = -1;
todo[0] = todo[--ntodo];
pos[todo[0]] = 0;
}
else {
break;
}
}
i = cind[first++]; /* Get the first visited but unexplored vertex */
for (j=xadj[i]; j<xadj[i+1]; j++) {
k = adjncy[j];
if (pos[k] != -1) {
cind[last++] = k;
/* Remove k from the todo list and put the last item in the todo
list at the position that k was so that the todo list will be
consequtive. The pos[] array is updated accordingly to keep track
the location of the vertices in the todo[] list. */
todo[pos[k]] = todo[--ntodo];
pos[todo[pos[k]]] = pos[k];
pos[k] = -1;
}
}
}
GKASSERT(first == nvtxs);
if (mustfree_ccsr)
gk_free((void **)&cptr, &cind, LTERM);
gk_free((void **)&pos, &todo, LTERM);
return (int) ncmps;
}
/*************************************************************************/
/*! This function computes a permutation of the vertices based on a
breadth-first-traversal. It can be used for re-ordering the graph
to reduce its bandwidth for better cache locality.
The algorithm used is a simplified version of the method used to find
the connected components.
\param[IN] graph is the graph structure
\param[IN] v is the starting vertex of the BFS
\param[OUT] perm[i] stores the ID of vertex i in the re-ordered graph.
\param[OUT] iperm[i] stores the ID of the vertex that corresponds to
the ith vertex in the re-ordered graph.
\note The perm or iperm (but not both) can be NULL, at which point,
the corresponding arrays are not returned. Though the program
works fine when both are NULL, doing that is not smart.
The returned arrays should be freed with gk_free().
*/
/*************************************************************************/
void gk_graph_ComputeBFSOrdering(gk_graph_t *graph, int v, int32_t **r_perm,
int32_t **r_iperm)
{
ssize_t j, *xadj;
int i, k, nvtxs, first, last;
int32_t *adjncy, *cot, *pos;
if (graph->nvtxs <= 0)
return;
nvtxs = graph->nvtxs;
xadj = graph->xadj;
adjncy = graph->adjncy;
/* This array will function like pos + touched of the CC method */
pos = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs, "gk_graph_ComputeBFSOrdering: pos"));
/* This array ([C]losed[O]pen[T]odo => cot) serves three purposes.
Positions from [0...first) is the current iperm[] vector of the explored vertices;
Positions from [first...last) is the OPEN list (i.e., visited vertices);
Positions from [last...nvtxs) is the todo list. */
cot = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs, "gk_graph_ComputeBFSOrdering: cot"));
/* put v at the front of the todo list */
pos[0] = cot[0] = v;
pos[v] = cot[v] = 0;
/* compute a BFS ordering from the seed vertex */
first = last = 0;
while (first < nvtxs) {
if (first == last) { /* Find another starting vertex */
k = cot[last];
ASSERT(pos[k] != -1);
pos[k] = -1; /* mark node as being visited */
last++;
}
i = cot[first++]; /* the ++ advances the explored vertices */
for (j=xadj[i]; j<xadj[i+1]; j++) {
k = adjncy[j];
/* if a node has already been visited, its pos[] will be -1 */
if (pos[k] != -1) {
/* pos[k] is the location within cot[] where k resides (it is in the 'todo' part);
It is placed in that location cot[last] (end of OPEN list) that we
are about to overwrite and update pos[cot[last]] to reflect that. */
cot[pos[k]] = cot[last]; /* put the head of the todo list to
where k was in the todo list */
pos[cot[last]] = pos[k]; /* update perm to reflect the move */
cot[last++] = k; /* put node at the end of the OPEN list */
pos[k] = -1; /* mark node as being visited */
}
}
}
/* time to decide what to return */
if (r_perm != NULL) {
/* use the 'pos' array to build the perm array */
for (i=0; i<nvtxs; i++)
pos[cot[i]] = i;
*r_perm = pos;
pos = NULL;
}
if (r_iperm != NULL) {
*r_iperm = cot;
cot = NULL;
}
/* cleanup memory */
gk_free((void **)&pos, &cot, LTERM);
}
/*************************************************************************/
/*! This function computes a permutation of the vertices based on a
best-first-traversal. It can be used for re-ordering the graph
to reduce its bandwidth for better cache locality.
\param[IN] graph is the graph structure.
\param[IN] v is the starting vertex of the best-first traversal.
\param[IN] type indicates the criteria to use to measure the 'bestness'
of a vertex.
\param[OUT] perm[i] stores the ID of vertex i in the re-ordered graph.
\param[OUT] iperm[i] stores the ID of the vertex that corresponds to
the ith vertex in the re-ordered graph.
\note The perm or iperm (but not both) can be NULL, at which point,
the corresponding arrays are not returned. Though the program
works fine when both are NULL, doing that is not smart.
The returned arrays should be freed with gk_free().
*/
/*************************************************************************/
void gk_graph_ComputeBestFOrdering0(gk_graph_t *graph, int v, int type,
int32_t **r_perm, int32_t **r_iperm)
{
ssize_t j, jj, *xadj;
int i, k, u, nvtxs;
int32_t *adjncy, *perm, *degrees, *minIDs, *open;
gk_i32pq_t *queue;
if (graph->nvtxs <= 0)
return;
nvtxs = graph->nvtxs;
xadj = graph->xadj;
adjncy = graph->adjncy;
/* the degree of the vertices in the closed list */
degrees = gk_i32smalloc(nvtxs, 0, "gk_graph_ComputeBestFOrdering: degrees");
/* the minimum vertex ID of an open vertex to the closed list */
minIDs = gk_i32smalloc(nvtxs, nvtxs+1, "gk_graph_ComputeBestFOrdering: minIDs");
/* the open list */
open = gk_i32malloc(nvtxs, "gk_graph_ComputeBestFOrdering: open");
/* if perm[i] >= 0, then perm[i] is the order of vertex i;
otherwise perm[i] == -1.
*/
perm = gk_i32smalloc(nvtxs, -1, "gk_graph_ComputeBestFOrdering: perm");
/* create the queue and put everything in it */
queue = gk_i32pqCreate(nvtxs);
for (i=0; i<nvtxs; i++)
gk_i32pqInsert(queue, i, 0);
gk_i32pqUpdate(queue, v, 1);
open[0] = v;
/* start processing the nodes */
for (i=0; i<nvtxs; i++) {
if ((v = gk_i32pqGetTop(queue)) == -1)
gk_errexit(SIGERR, "The priority queue got empty ahead of time [i=%d].\n", i);
if (perm[v] != -1)
gk_errexit(SIGERR, "The perm[%d] has already been set.\n", v);
perm[v] = i;
for (j=xadj[v]; j<xadj[v+1]; j++) {
u = adjncy[j];