-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprpack_preprocessed_gs_graph.cpp
81 lines (75 loc) · 2.39 KB
/
prpack_preprocessed_gs_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
#include "prpack_preprocessed_gs_graph.h"
#include <algorithm>
using namespace prpack;
using namespace std;
void prpack_preprocessed_gs_graph::initialize() {
heads = NULL;
tails = NULL;
vals = NULL;
ii = NULL;
d = NULL;
num_outlinks = NULL;
}
void prpack_preprocessed_gs_graph::initialize_weighted(const prpack_base_graph* bg) {
vals = new double[num_es];
d = new double[num_vs];
fill(d, d + num_vs, 1);
for (int tails_i = 0, heads_i = 0; tails_i < num_vs; ++tails_i) {
tails[tails_i] = heads_i;
ii[tails_i] = 0;
const int start_j = bg->tails[tails_i];
const int end_j = (tails_i + 1 != num_vs) ? bg->tails[tails_i + 1]: bg->num_es;
for (int j = start_j; j < end_j; ++j) {
if (tails_i == bg->heads[j])
ii[tails_i] += bg->vals[j];
else {
heads[heads_i] = bg->heads[j];
vals[heads_i] = bg->vals[j];
++heads_i;
}
d[bg->heads[j]] -= bg->vals[j];
}
}
}
void prpack_preprocessed_gs_graph::initialize_unweighted(const prpack_base_graph* bg) {
num_outlinks = new double[num_vs];
fill(num_outlinks, num_outlinks + num_vs, 0);
for (int tails_i = 0, heads_i = 0; tails_i < num_vs; ++tails_i) {
tails[tails_i] = heads_i;
ii[tails_i] = 0;
const int start_j = bg->tails[tails_i];
const int end_j = (tails_i + 1 != num_vs) ? bg->tails[tails_i + 1]: bg->num_es;
for (int j = start_j; j < end_j; ++j) {
if (tails_i == bg->heads[j])
++ii[tails_i];
else
heads[heads_i++] = bg->heads[j];
++num_outlinks[bg->heads[j]];
}
}
for (int i = 0; i < num_vs; ++i) {
if (num_outlinks[i] == 0)
num_outlinks[i] = -1;
ii[i] /= num_outlinks[i];
}
}
prpack_preprocessed_gs_graph::prpack_preprocessed_gs_graph(const prpack_base_graph* bg) {
initialize();
num_vs = bg->num_vs;
num_es = bg->num_es - bg->num_self_es;
heads = new int[num_es];
tails = new int[num_vs];
ii = new double[num_vs];
if (bg->vals != NULL)
initialize_weighted(bg);
else
initialize_unweighted(bg);
}
prpack_preprocessed_gs_graph::~prpack_preprocessed_gs_graph() {
delete[] heads;
delete[] tails;
delete[] vals;
delete[] ii;
delete[] d;
delete[] num_outlinks;
}