-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphio.c
419 lines (346 loc) · 10.7 KB
/
graphio.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
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "mmio.h"
#include "graphio.h"
//#define DEBUG
typedef struct {
vtype i;
vtype j;
ewtype w;
} Triple;
int cmp(const void *a, const void *b){
const vtype *ia = (const vtype *)a;
const vtype *ib = (const vtype *)b;
return *ia - *ib;
}
int tricmp(const void *t1, const void *t2){
Triple *tr1 = (Triple *)t1;
Triple *tr2 = (Triple *)t2;
if(tr1->i == tr2->i) {
return (int)(tr1->j - tr2->j);
}
return (int)(tr1->i - tr2->i);
}
int ends_with(const char *str, const char *suffix) {
if (!str || !suffix) return 0;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr) return 0;
return (strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0);
}
int read_chaco(FILE* fp, etype **pxadj, vtype **padj,
ewtype **pewghts, vwtype **pvwghts,
vtype* pnov, int loop) {
int state = 0, fmt = 0, ncon = 1, i;
vtype numVertices = -1, vcount = 0, jv;
etype numEdges = -1, ecount = 0;
char *temp, *graphLine = (char *) malloc(sizeof(char)*10000000+1);
while(fgets(graphLine, 10000000, fp) != NULL) {
for(i = 0; i < (int) strlen(graphLine); i++) {
char c = graphLine[i];
if(c != ' ' && c != '\t' && c != '\n') {
break;
}
}
/* read the line wrt fmt and mw */
if(graphLine[0] == '%') {
continue;
} else if(state == 0) {
temp = strtok(graphLine, " \t\n");
numVertices = atoi(temp);
temp = strtok(NULL, " \t\n");
numEdges = atoi(temp);
temp = strtok(NULL, " \t\n");
if(temp != NULL) {
fmt = atoi(temp);
temp = strtok(NULL, " \t\n");
if(temp != NULL) {ncon = atoi(temp);}
}
*pnov = numVertices;
(*pxadj) = (etype*)malloc(sizeof(etype) * (numVertices+1));
(*pxadj)[0] = 0;
(*pvwghts) = (vwtype*)malloc(sizeof(vwtype) * numVertices);
(*padj) = (vtype*)malloc(sizeof(vtype) * 2 * numEdges);
(*pewghts) = (ewtype*)malloc(sizeof(ewtype) * 2 * numEdges);
state = 1;
} else { /* consume a line */
if(vcount == numVertices) {
printf("Error: file contains more than %ld lines\n", (long int)numVertices);
return -1;
}
/* read the line wrt fmt and mw */
temp = strtok(graphLine, " \t\n");
/* ignore vertex size if exists */
if (fmt >= 100) {
temp = strtok(NULL, " \t\n");
}
/* take only the first vertex weight if exists. ignore the rest
* if no vertex weight exists then unit weight */
if (fmt % 100 >= 10) {
(*pvwghts)[vcount] = atoi(temp);
for (i = 1; i < ncon; i++) {
temp = strtok(NULL, " \t\n");
}
} else {
(*pvwghts)[vcount] = 1;
}
/* read edges and edge weights if exists */
while(temp != NULL) {
if(ecount == 2 * numEdges) {
printf("Error: file contains more than %ld edges\n", (long int)numEdges);
return -1;
}
(*padj)[ecount] = atoi(temp) - 1; /* ids start from 1 in the graph */
if((*padj)[ecount] == vcount && !loop) {
continue;
}
temp = strtok(NULL, " \t\n");
if(fmt % 10 == 1) {
(*pewghts)[ecount] = atoi(temp);
temp = strtok(NULL, " \t\n");
} else {
(*pewghts)[ecount] = 1;
}
if((*pewghts)[ecount] < 0) {
printf("negative edge weight %lf between %ld-%ld.\n", (*pewghts)[ecount], (long int)vcount, (long int)((*padj)[ecount]));
return -1;
}
ecount++;
}
vcount++;
(*pxadj)[vcount] = ecount;
}
}
if(vcount != numVertices) {
printf("number of vertices do not match %ld %ld\n", (long int)numVertices, (long int)vcount);
return -1;
}
if(ecount != 2 * numEdges) {
printf("number of edges do not match %ld %ld: realloc memory appropriately\n", (long int)ecount, (long int)(2 * numEdges));
(*padj) = (vtype*)realloc((*padj), sizeof(vtype) * ecount);
(*pewghts) = (ewtype*)realloc((*pewghts) , sizeof(ewtype) * ecount);
}
for(jv = 0; jv < vcount; jv++) {
qsort((*padj) + (*pxadj)[jv], (*pxadj)[jv+1] - (*pxadj)[jv], sizeof(vtype), cmp);
}
return 1;
}
int read_mtx(FILE* fp, etype **pxadj, vtype **padj,
ewtype **pewghts, vwtype **pvwghts,
vtype* pnov, int loop, int zerobased) {
Triple *T;
vtype i, j, M, N, lower, upper, wi;
ewtype w;
etype k, nz, ecnt, cnt;
MM_typecode matcode;
if (mm_read_banner(fp, &matcode) != 0) {
printf("Could not process Matrix Market banner.\n");
return -1;
}
// find out size of sparse matrix ....
if (mm_read_mtx_crd_size(fp, &M, &N, &nz) != 0) {
printf("ret code is wrong\n");
return -1;
}
#ifdef DEBUG
printf("There are %ld rows, %ld columns, %ld nonzeros\n", (long int)M, (long int)N, (long int)nz);
#endif
if(M != N) {
printf("not a graph, rectangular matrix???\n");
return -1;
}
lower = 1 - zerobased;
upper = N - zerobased;
T = (Triple *)malloc(2 * nz * sizeof(Triple));
cnt = 0;
if(mm_is_pattern(matcode)) {
for (ecnt = 0; ecnt < nz; ecnt++) {
fscanf(fp, "%d %d\n", &i, &j);
if(i < lower || j < lower || i > upper || j > upper) {
printf("read coordinate %ld %ld -- lower and upper is %ld and %ld\n", (long int)i, (long int)j, (long int)lower, (long int)upper);
return -1;
}
if(loop || i != j) {
T[cnt].i = i;
T[cnt].j = j;
T[cnt].w = 1;
cnt++;
if(mm_is_symmetric(matcode) && i != j) {
T[cnt].i = T[cnt-1].j; /* insert the symmetric edge */
T[cnt].j = T[cnt-1].i;
T[cnt].w = 1;
cnt++;
}
}
}
} else {
for (ecnt = 0; ecnt < nz; ecnt++) {
if(mm_is_real(matcode)) {
fscanf(fp, "%d %d %lf\n", &i, &j, &w);
} else if(mm_is_integer(matcode)) {
fscanf(fp, "%d %d %d\n", &i, &j, &wi);
}
w = (double)wi;
if(i < lower || j < lower || i > upper || j > upper) {
printf("read coordinate %ld %ld -- lower and upper is %ld and %ld\n", (long int)i, (long int)j, (long int)lower, (long int)upper);
return -1;
}
if(w != 0 && (loop || i != j)) {
T[cnt].i = i;
T[cnt].j = j;
T[cnt].w = fabs(w);
cnt++;
if(mm_is_symmetric(matcode) && i != j) { /* insert the symmetric edge */
T[cnt].i = T[cnt-1].j;
T[cnt].j = T[cnt-1].i;
T[cnt].w = T[cnt-1].w;
cnt++;
}
}
}
}
//#ifdef DEBUG
// printf("the triplet array is filled with %ld edges\n", (long int)cnt); fflush(0);
//#endif
/* eliminate the duplicates, and writes them to the arrays */
qsort(T, cnt, sizeof(Triple), tricmp);
/* create xadj array */
(*pxadj) = (etype*)malloc(sizeof(etype) * (N+1));
memset((*pxadj), 0, sizeof(etype) * (N+1));
k = 0;
(*pxadj)[T[0].i + zerobased]++;
for(ecnt = 1; ecnt < cnt; ecnt++) {
i = T[ecnt].i;
if(i != T[ecnt-1].i || T[ecnt].j != T[ecnt-1].j) { /* if this edge is not the same as previous one */
(*pxadj)[i + zerobased]++;
k = i; /* the first edge entry */
} else { /* add the weight to the original */
T[k].w += T[i].w; /* add the weight to the representative */
}
}
for(i = 2; i <= N; i++) (*pxadj)[i] += (*pxadj)[i-1];
(*padj) = (vtype*)malloc(sizeof(vtype) * (*pxadj)[N]);
(*pewghts) = (ewtype*)malloc(sizeof(ewtype) * (*pxadj)[N]);
(*padj)[0] = T[0].j - 1 + zerobased; (*pewghts) [0] = T[0].w; k = 1;
for(ecnt = 1; ecnt < cnt; ecnt++) {
i = T[ecnt].i;
if(i != T[ecnt-1].i || T[ecnt].j != T[ecnt-1].j) { /* if this edge is not the same as previous one */
(*padj)[k] = T[ecnt].j - 1 + zerobased; /* adjust from 1-based to 0-based */
(*pewghts)[k++] = T[ecnt].w;
}
}
(*pvwghts) = (vwtype*)malloc(sizeof(vwtype) * N);
for(i = 0; i < N; i++) (*pvwghts) [i] = 1;
*pnov = N; // the number of vertices
//#ifdef DEBUG
//printf("file is read m %ld n %ld edges %ld =? %ld, real edges\n", (long int)M, (long int)N, (long int)k, (long int)((*pxadj)[N]));
//#endif
free(T);
return 1;
}
int readBinaryGraph(FILE* bp, etype **pxadj, vtype **padj,
ewtype **pewghts, vwtype **pvwghts,
vtype* pnov) {
fread(pnov, sizeof(vtype), 1, bp);
(*pxadj) = (etype*)malloc(sizeof(etype) * (*pnov + 1));
fread(*pxadj, sizeof(etype), (size_t)(*pnov + 1), bp);
(*padj) = (vtype*)malloc(sizeof(vtype) * (*pxadj)[*pnov]);
fread(*padj, sizeof(vtype), (size_t)(*pxadj)[*pnov], bp);
(*pewghts) = (ewtype*)malloc(sizeof(ewtype) * (*pxadj)[*pnov]);
fread(*pewghts, sizeof(ewtype), (size_t)(*pxadj)[*pnov], bp);
(*pvwghts) = (vwtype*)malloc(sizeof(vwtype) * (*pnov));
fread(*pvwghts, sizeof(vwtype), *pnov, bp);
return 1;
}
int writeBinaryGraph(FILE* bp, etype *xadj, vtype *adj,
ewtype *ewghts, vwtype *vwghts,
vtype nov) {
fwrite(&nov, sizeof(vtype), (size_t)1, bp);
fwrite(xadj, sizeof(etype), (size_t)(nov + 1), bp);
fwrite(adj, sizeof(vtype), (size_t)(xadj[nov]), bp);
fwrite(ewghts, sizeof(ewtype), (size_t)(xadj[nov]), bp);
fwrite(vwghts, sizeof(vwtype), (size_t)(nov), bp);
return 1;
}
int read_graph(char* gfile, etype **xadj, vtype **adj,
ewtype **ewghts, vwtype **vwghts,
vtype* nov, int loop) {
char bfile[2048];
FILE *bp, *fp;
/*
Write the binaryfile in the directory of the executable.
*/
int i=0;
int dirindex = 0;
while(gfile[i] != '\0' && i < 2048 ){
if(gfile[i] == '/')
{
dirindex = i;
}
i++;
}
if(i==2048)
i = 0;
char tbfile[2048];
tbfile[0] = '.';
char c = ' ';
i = 0;
while(c!= '\0'){
c = gfile[dirindex+i];
tbfile[i+1] = c;
i++;
}
/* check if binary file exists */
sprintf(bfile, "%s.bin", tbfile);
//printf("bin:%s\n", bfile);
bp = fopen(bfile, "rb");
if(bp != NULL) { /* read from binary */
if(readBinaryGraph(bp, xadj, adj, ewghts, vwghts, nov) == -1) {
printf("error reading the graph in binary format\n");
fclose(bp);
return -1;
}
fclose(bp);
} else { /* read from text, write to binary */
fp = fopen(gfile, "r");
if(fp == NULL) {
printf("%s: file does not exist\n", gfile);
return -1;
} else {
if(ends_with(gfile, ".mtx")) {
if(read_mtx(fp, xadj, adj, ewghts, vwghts, nov, loop, 0) == -1) {
printf("problem with mtx file\n");
fclose(fp);
return -1;
}
} else if(ends_with(gfile, ".txt")) {
if(read_mtx(fp, xadj, adj, ewghts, vwghts, nov, loop, 1) == -1) {
printf("problem with txt file\n");
fclose(fp);
return -1;
}
} else if(ends_with(gfile, ".graph")) {
if(read_chaco(fp, xadj, adj, ewghts, vwghts, nov, loop) == -1) {
printf("problem with mtx file\n");
fclose(fp);
return -1;
}
}
bp = fopen(bfile, "wb");
if(bp != NULL) {
if(writeBinaryGraph(bp, *xadj, *adj, *ewghts, *vwghts, *nov) == -1) {
printf("error writing to graph in binary format\n");
fclose(bp);
return -1;
}
fclose(bp);
}
fclose(fp);
}
}
return 1;
}