-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.h
executable file
·197 lines (158 loc) · 4.84 KB
/
graph.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
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
#ifndef __GRAPH_H__
#define __GRAPH_H__
#include "mylib.h"
#include "config.h"
class Graph {
public:
vector<vector<int>> g;
vector<vector<int>> gr;
vector<int> outdegrees;
string data_folder;
//vector<double> global_ppr;
// node rank[100] = 0, means node 100 has first rank
//vector<int> node_rank;
// node_score[0]
//vector<double> node_score;
//node order 0 = [100, 34.5], most important node is 100 with score 34.5
//vector<pair<int, double>> node_order;
//vector<int> loc;
// the tele ratio for random walk
//double alpha;
static bool cmp(const pair<int, double> &t1, const pair<int, double> &t2) {
return t1.second > t2.second;
}
int n;
long long m;
long long maxid;
Graph(string data_folder) {
INFO("sub constructor");
this->data_folder = data_folder;
//this->alpha = ALPHA_DEFAULT;
if(config.action != GEN_SS_QUERY)
init_graph();
else
init_nm();
init_outdegree();
cout << "init graph n: " << this->n << " m: " << this->m << endl;
}
void init_nm() {
string attribute_file = data_folder + FILESEP + "attribute.txt";
assert_file_exist("attribute file", attribute_file);
ifstream attr(attribute_file);
string line1, line2;
char c;
while (true) {
attr >> c;
if (c == '=') break;
}
attr >> n;
while (true) {
attr >> c;
if (c == '=') break;
}
attr >> m;
}
void init_outdegree() {
outdegrees.resize(n);
for(int i = 0; i < g.size(); i++){
outdegrees[i] = g[i].size();
}
}
void init_nm_convert(){
string attribute_file = data_folder + FILESEP + "attribute.txt";
assert_file_exist("attribute file", attribute_file);
ifstream attr(attribute_file);
string line1, line2;
char c;
while (true) {
attr >> c;
if (c == '=') break;
}
attr >> n;
while (true) {
attr >> c;
if (c == '=') break;
}
attr >> m;
while(true){
attr >> c;
if (c == '=') break;
}
attr >> maxid;
}
void init_graph() {
if(config.remap)
init_nm_convert();
else
init_nm();
g = vector<vector<int>>(n, vector<int>());
gr = vector<vector<int>>(n, vector<int>());
string graph_file = data_folder + FILESEP + "graph.txt";
assert_file_exist("graph file", graph_file);
if(config.remap){
int* idmap = new int[maxid];
int* degree = new int[n];
for(long long i =0; i<maxid; i++){
idmap[i] = -1;
}
for(int i=0; i<n; i++){
degree[i] = 0;
}
int startid = 0;
long long t1, t2;
FILE *fin = fopen(graph_file.c_str(), "r");
while (fscanf(fin, "%d%d", &t1, &t2) != EOF) {
if(idmap[t1] <0){
idmap[t1] = startid;
degree[startid]++;
startid++;
}else{
degree[idmap[t1]]++;
}
if(idmap[t2] < 0){
idmap[t2] = startid;
startid++;
}
if(t1 == t2) continue;
}
fclose(fin);
for(int i=0; i< n; i++){
g[i].reserve(degree[i]);
}
FILE *fin2 = fopen(graph_file.c_str(), "r");
while (fscanf(fin2, "%d%d", &t1, &t2) != EOF) {
assert(idmap[t1] < n && idmap[t1] >= 0);
assert(idmap[t2]< n && idmap[t2] >= 0);
if(t1 == t2) continue;
g[idmap[t1]].push_back(idmap[t2]);
//gr[idmap[t2]].push_back(idmap[t1]);
}
fclose(fin2);
delete [] idmap;
delete [] degree;
}else{
FILE *fin = fopen(graph_file.c_str(), "r");
int t1, t2;
while (fscanf(fin, "%d%d", &t1, &t2) != EOF) {
assert(t1 < n);
assert(t2 < n);
if(t1 == t2) continue;
g[t1].push_back(t2);
g[t2].push_back(t1);
gr[t2].push_back(t1);
gr[t1].push_back(t2);
}
}
}
double get_avg_degree() const {
return double(m) / double(n);
}
};
static void init_parameter(Config &config, const Graph &graph) {
// init the bwd delta, fwd delta etc
INFO("init parameters", graph.n);
config.delta = 1.0 / graph.n;
config.pfail = 1.0 / graph.n;
config.dbar = double(graph.m) / double(graph.n); // average degree
}
#endif