-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.cpp
206 lines (171 loc) · 6 KB
/
graph.cpp
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
#include "main.h"
#include "util/graph/graph_bin_input_helper.h"
#include "util/log/log.h"
#define MAXLINE INT_MAX
#define WRITE_BINARY
static int really_read(istream &is, char *buf, size_t global_size) {
char *temp2 = buf;
while (global_size != 0) {
is.read(temp2, global_size);
size_t s = is.gcount();
if (!is)
return -1;
global_size -= s;
temp2 += s;
}
return 0;
}
template<typename VtxType, typename EdgeType>
void writeBinary(char *filename, VtxType nVtx, EdgeType nEdge, VtxType *adj, EdgeType *xadj) {
string str(filename);
string fl = str + ".bin";
FILE *filep = fopen(fl.c_str(), "w");
int vtxt = sizeof(VtxType);
int edget = sizeof(EdgeType);
fwrite(&vtxt, sizeof(int), 1, filep);
fwrite(&edget, sizeof(int), 1, filep);
fwrite(&nVtx, sizeof(VtxType), 1, filep);
fwrite(&nEdge, sizeof(EdgeType), 1, filep);
for (VtxType i = 0; i < nVtx; i++) {
EdgeType sz = xadj[i + 1] - xadj[i];
fwrite(&sz, sizeof(EdgeType), 1, filep);
}
fwrite(adj, sizeof(VtxType), xadj[nVtx], filep);
fclose(filep);
}
template<typename VtxType, typename EdgeType>
void readBinary(char *filename, VtxType *nVtx, EdgeType *nEdge, VtxType **adj, EdgeType **xadj) {
ifstream in(filename);
int vtxsize; //in bytes
int edgesize; //in bytes
//reading header
in.read((char *) &vtxsize, sizeof(int));
in.read((char *) &edgesize, sizeof(int));
if (!in) {
cerr << "IOError" << endl;
return;
}
if (vtxsize != sizeof(VtxType)) {
cerr << "Incompatible VertexSize" << endl;
return;
}
if (edgesize != sizeof(EdgeType)) {
cerr << "Incompatible EdgeSize" << endl;
return;
}
in.read((char *) nVtx, sizeof(VtxType)); // we already write this as +1
in.read((char *) nEdge, sizeof(EdgeType));
(*xadj) = (EdgeType *) malloc(sizeof(EdgeType) * (*nVtx + 1));
(*adj) = (VtxType *) malloc(sizeof(VtxType) * (*nEdge * 2));
(*xadj)[0] = 0;
for (VtxType i = 0; i < *nVtx; i++) {
EdgeType nt = 0;
really_read(in, (char *) &nt, sizeof(EdgeType));
(*xadj)[i + 1] = (*xadj)[i] + nt;
}
really_read(in, (char *) (*adj), sizeof(VtxType) * (*xadj)[*nVtx]);
}
template<typename VtxType, typename EdgeType>
void VV2CRS(vector<vector<VtxType>> &graph, VtxType *nVtx, EdgeType *nEdge, VtxType **adj, EdgeType **xadj) {
EdgeType ei = 0;
*xadj = (EdgeType *) malloc(sizeof(EdgeType) * (*nVtx + 1));
*adj = (VtxType *) malloc(sizeof(VtxType) * (*nEdge * 2));
(*xadj)[0] = 0;
for (VtxType i = 0; i < *nVtx; i++) {
(*xadj)[i + 1] = graph[i].size() + (*xadj)[i];
for (VtxType j = 0; j < graph[i].size(); j++)
(*adj)[ei++] = graph[i][j];
graph[i].clear();
}
graph.clear();
}
template<typename VtxType, typename EdgeType>
void readEdgeList(bool mm, char *filename, VtxType *nVtx, EdgeType *nEdge, VtxType **adj, EdgeType **xadj) {
char *line = (char *) malloc(sizeof(char) * MAXLINE);
FILE *matfp = fopen(filename, "r");
// skip comments
do
fgets(line, MAXLINE, matfp);
while (line[0] == '%' || line[0] == '#');
vector<couple> coords;
VtxType u, v, nv = 0;
stringstream ss(line);
if (mm) {
ss >> *nVtx >> *nEdge;
log_info("|V|: '%d |E|: '%d", *nVtx, *nEdge);
} else {
ss >> u >> v;
nv = max(nv, (max(u, v)));
if (u != v) {
coords.push_back(make_tuple(u, v));
coords.push_back(make_tuple(v, u));
}
}
while (fgets(line, MAXLINE, matfp)) {
stringstream ss(line);
ss >> u >> v;
nv = max(nv, (max(u, v)));
if (u != v) {
coords.push_back(make_tuple(u, v));
coords.push_back(make_tuple(v, u));
}
}
fclose(matfp);
if (mm) {
if (*nVtx != nv + 1) {
printf("nVtx in header (%d) is wrong, must be %d\n", *nVtx, nv + 1);
*nVtx = nv + 1;
}
} else
*nVtx = nv + 1;
sort(coords.begin(), coords.end());
// begin constructing graph
vector<vector<VtxType>> graph(*nVtx);
EdgeType i = 0;
graph[get<0>(coords[i])].push_back(get<1>(coords[i]));
for (i = 1; i < coords.size(); i++)
if (coords[i] != coords[i - 1])
graph[get<0>(coords[i])].push_back(get<1>(coords[i]));
// sort each neighbor list
EdgeType ne = 0;
for (auto v : graph) {
sort(v.begin(), v.end());
ne += v.size();
}
*nEdge = ne / 2;
if (mm) {
if (*nEdge != ne / 2) {
printf("nEdge in header (%d) is wrong, must be %d\n", *nEdge, ne / 2);
*nEdge = ne / 2;
}
} else
*nEdge = ne / 2;
VV2CRS(graph, nVtx, nEdge, adj, xadj);
}
template<typename VtxType, typename EdgeType>
void readGraph(char *filename, VtxType *nVtx, EdgeType *nEdge, VtxType **adj, EdgeType **xadj) {
string st(filename);
string gname = st.substr(st.find_last_of('/') + 1);
int idx = gname.find_last_of('.');
log_info("idx: %d", idx);
if (idx == -1 || ((gname.size() >= (idx + 2)) && (gname[idx + 1] == '5'))) {
log_info("Read Dir Edge List: %s", filename);
// readDirEdgeList(filename, *nVtx, *nEdge, *adj, *xadj);
readDirectory(filename, *nVtx, *nEdge, *adj, *xadj);
} else {
string ext = gname.substr(idx);
if (ext == ".bin")
readBinary<VtxType, EdgeType>(filename, nVtx, nEdge, adj, xadj);
else if (ext == ".mtx")
readEdgeList<VtxType, EdgeType>(true, filename, nVtx, nEdge, adj, xadj);
else if (ext == ".txt" || ext == ".tsv")
readEdgeList<VtxType, EdgeType>(false, filename, nVtx, nEdge, adj, xadj);
#ifdef WRITE_BINARY
if (ext != ".bin") {
writeBinary<VtxType, EdgeType>(filename, *nVtx, *nEdge, *adj, *xadj);
printf("Binary graph is written\n");
}
#endif
}
}
template void readGraph(char *filename, vertex *nVtx, edge *nEdge, vertex **adj, edge **xadj);