-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
121 lines (95 loc) · 3.06 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
#ifndef __GRAPH_H__
#define __GRAPH_H__
#include "mylib.h"
#include "config.h"
class Graph {
public:
//两个向量以起点和终点两种方式来存储边
vector<vector<int>> g;
vector<vector<int>> gr;
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;
Graph(string data_folder) {
INFO("sub constructor");
this->data_folder = data_folder;
this->alpha = ALPHA_DEFAULT;
if(config.action == GEN_SS_QUERY && !config.query_high_degree)
init_nm();
else
init_graph();
cout << "init graph n: " << this->n << " m: " << this->m << endl;
}
//读取属性文件中的n和m的值
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_graph() {
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);
FILE *fin = fopen(graph_file.c_str(), "r");
int t1, t2;
//dblp2010 and orkut are undirected graph
if (data_folder.find("dblp2010")!=string::npos||data_folder.find("orkut")!=string::npos){
while (fscanf(fin, "%d%d", &t1, &t2) != EOF) {
assert(t1 < n);
assert(t2 < n);
if(t1 == t2) continue;
g[t1].push_back(t2);
gr[t2].push_back(t1);
g[t2].push_back(t1);
gr[t1].push_back(t2);
}
m*=2;
} else {
while (fscanf(fin, "%d%d", &t1, &t2) != EOF) {
assert(t1 < n);
assert(t2 < n);
if(t1 == t2) continue;
g[t1].push_back(t2);
gr[t2].push_back(t1);
}
}
}
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