-
Notifications
You must be signed in to change notification settings - Fork 4
/
GraphVec.h
136 lines (113 loc) · 3.96 KB
/
GraphVec.h
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
#ifndef __GRAPH_VEC_H__
#define __GRAPH_VEC_H__
#include "Meta.h"
#include "TimeMeasurer.h"
#include "EdgeBatch.h"
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
class GraphVec{
public:
GraphVec(const std::string &filename, bool directed = true){
this->directed = directed;
this->filename = filename;
ConstructGraph();
}
GraphVec(){}
~GraphVec(){}
virtual void ConstructGraph(){
// edge file
// format: a b (a and b is vertex id)
TimeMeasurer timer;
timer.StartTimer();
// initialize file
FILE* file_ptr = fopen(filename.c_str(), "rb");
assert(file_ptr != NULL);
fseek(file_ptr, 0L, SEEK_END);
size_t file_size = ftell(file_ptr);
rewind(file_ptr);
// first int indicate vertex_count
size_t res_size;
size_t file_pos = 0;
res_size = fread(&vertex_count, sizeof(IndexType), 1, file_ptr);
assert(res_size == 1);
file_pos += sizeof(IndexType);
std::cout << "vertex_count=" << vertex_count << std::endl;
col_ind.resize(vertex_count);
in_col_ind.resize(vertex_count);
deg.resize(vertex_count);
IndexType v1, v2;
edge_count = 0;
while (file_pos < file_size){
res_size += fread(&v1, sizeof(IndexType), 1, file_ptr);
res_size += fread(&v2, sizeof(IndexType), 1, file_ptr);
assert(0 <= v1 && v1 < vertex_count);
assert(0 <= v2 && v2 < vertex_count);
col_ind[v1].push_back(v2);
in_col_ind[v2].push_back(v1);
edge_count++;
if (!directed){
col_ind[v2].push_back(v1);
in_col_ind[v1].push_back(v2);
edge_count++;
}
file_pos += sizeof(IndexType) * 2;
}
IndexType edge_stream_count = directed ? edge_count : edge_count / 2;
assert(res_size / 2 == edge_stream_count);
std::cout << "edge_stream_count=" << edge_stream_count << std::endl;
std::cout << "edge_count=" << edge_count << std::endl;
for (IndexType i = 0; i < vertex_count; ++i) deg[i] = col_ind[i].size();
fclose(file_ptr);
timer.EndTimer();
std::cout << "read file elapsed time=" << timer.GetElapsedMicroSeconds() * 1.0 / 1000 << "ms" << std::endl;
}
void SortCSRColumns(){
for (IndexType i = 0; i < vertex_count; ++i){
std::sort(col_ind[i].begin(), col_ind[i].end());
}
for (IndexType i = 0; i < vertex_count; ++i){
std::sort(in_col_ind[i].begin(), in_col_ind[i].end());
}
}
virtual void SerializeEdgeStream(EdgeBatch *edge_stream){
FILE* file_ptr = fopen(filename.c_str(), "rb");
assert(file_ptr != NULL);
fseek(file_ptr, 0L, SEEK_END);
size_t file_size = ftell(file_ptr);
rewind(file_ptr);
size_t res_size, file_pos;
IndexType vcount;
res_size = fread(&vcount, sizeof(IndexType), 1, file_ptr);
assert(res_size == 1);
file_pos = sizeof(IndexType);
IndexType v1, v2;
edge_stream->length = 0;
while (file_pos < file_size){
res_size += fread(&v1, sizeof(IndexType), 1, file_ptr);
res_size += fread(&v2, sizeof(IndexType), 1, file_ptr);
edge_stream->edge1[edge_stream->length] = v1;
edge_stream->edge2[edge_stream->length] = v2;
edge_stream->length++;
file_pos += sizeof(IndexType) * 2;
}
fclose(file_ptr);
}
virtual bool StreamUpdates(const size_t stream_count){
return true;
}
public:
std::string filename;
bool directed;
IndexType vertex_count;
IndexType edge_count;
std::vector<IndexType> deg;
std::vector<std::vector<IndexType> > col_ind;
std::vector<std::vector<IndexType> > in_col_ind;
};
#endif