This repository has been archived by the owner on Sep 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_centrality.cpp
176 lines (157 loc) · 6.28 KB
/
flow_centrality.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
#include "network.h"
int main(int argc, char* argv[]) {
char q[512];
MYSQL_ROW row;
MYSQL_RES* res;
int err = init_network();
if (err) {
return err;
}
for (int year = 1990; year <= 2011; year++) {
std::cout << "Querying " << year << "\n";
snprintf(q, 512, "select count(value) from entries where year=%d and inserted>(select updated from visualizations where name='Flow Centrality' and year=%d)", year, year);
if (mysql_query(mysql, q)) {
std::cerr << mysql_error(mysql) << "\n";
return -2;
}
res = mysql_use_result(mysql);
if((row = mysql_fetch_row(res)) == 0 || atoi(row[0]) == 0) {
mysql_free_result(res);
continue;
}
mysql_free_result(res);
err = read_network(year);
if (err) {
return err;
}
std::cout << "Calculating " << year << "\n";
err = disconnect();
if (err) {
return err;
}
Basetype* betweenness = create_array(0);
Basetype* in_flow = create_array(0);
Basetype* out_flow = create_array(0);
for (int v = 0; v < network_size; v++) {
for (int w = 0; w < network_size; w++) {
in_flow[v] += flows[w][v];
out_flow[v] += flows[v][w];
}
}
// Algorithm according to "A space-efficient parallel algorithm for computing betweenness centrality in distributed memory", p. 2
int s;
#pragma omp parallel default(none) shared(betweenness, flows, network_size, in_flow, std::cerr)
{
#pragma omp for schedule(guided) nowait
for (s = 0; s < network_size; s++) {
std::vector<int> S;
std::queue<int> PQ;
BasetypeInt* sigma;
Basetype* delta;
Basetype* dist;
BasetypeInt** P;
BasetypeInt* P_size;
Basetype* pipe;
sigma = create_array_int(0);
sigma[s] = 1;
delta = create_array(0);
P = create_double_int_array(0);
P_size = create_array_int(0);
dist = create_array(-1);
dist[s] = 0;
pipe = create_array(0);
pipe[s] = -1;
PQ.push(s);
while (!PQ.empty()) {
int v = PQ.front();
PQ.pop();
for (std::vector<int>::iterator it = S.begin(); it != S.end(); it++) {
if (*it == v) {
S.erase(it);
break;
}
}
S.push_back(v);
for (int w = 0; w < network_size; w++) {
if (w != v && w != s && flows[v][w] > 0) {
Basetype c_v_w = flows[v][w];
Basetype new_pipe;
if (pipe[v] < 0) {
new_pipe = c_v_w;
} else {
new_pipe = std::min(pipe[v], c_v_w);
}
if (pipe[w] < new_pipe || (pipe[w] == new_pipe && dist[w] > dist[v] + 1)) { // Better best path via v
PQ.push(w);
pipe[w] = new_pipe;
dist[w] = dist[v] + 1;
sigma[w] = 0;
P_size[w] = 0;
}
if (pipe[w] == new_pipe && dist[w] == dist[v] + 1 && !in_array(v,P[w],P_size[w])) { // Some best path via v
sigma[w] += sigma[v];
P[w][P_size[w]] = v;
P_size[w]++;
}
}
}
}
while (!S.empty()) {
int w = S.back();
S.pop_back();
for (int v_index = 0; v_index < P_size[w]; v_index++) {
int v = P[w][v_index];
delta[v] += ((1 + delta[w]) * (Basetype) sigma[v]) / (Basetype) sigma[w];
}
if (w != s) {
#pragma omp atomic
betweenness[w] += delta[w];
}
}
delete[] delta;
delete[] sigma;
delete[] dist;
delete[] P_size;
free_double_int_array(P);
delete[] pipe;
}
}
err = connect();
if (err) {
return err;
}
snprintf(q, 512, "delete from visualization_data where visualization in (select id from visualizations where name='Flow Centrality' and year=%d)", year);
if (mysql_query(mysql, q)) {
std::cerr << mysql_error(mysql) << "\n";
return -2;
}
snprintf(q, 512, "select id from visualizations where name='Flow Centrality' and year=%d", year);
if (mysql_query(mysql, q)) {
std::cerr << mysql_error(mysql) << "\n";
return -2;
}
res = mysql_use_result(mysql);
row = mysql_fetch_row(res);
int id = atoi(row[0]);
mysql_free_result(res);
std::stringstream query("insert into visualization_data (visualization, sector_from, region_from, value) values ", std::ios_base::app | std::ios_base::out);
for (int js = 0; js < network_size; js++) {
if (js > 0) {
query << ",";
}
query << "(" << id << ",'" << sectors[get_sector(js)] << "','" << regions[get_region(js)] << "'," << betweenness[js] << ")";
}
if (mysql_query(mysql, query.str().c_str())) {
std::cerr << mysql_error(mysql) << "\n";
return -2;
}
snprintf(q, 512, "update visualizations set updated=now() where name='Flow Centrality' and year=%d", year);
if (mysql_query(mysql, q)) {
std::cerr << mysql_error(mysql) << "\n";
return -2;
}
delete[] betweenness;
free_double_array(flows);
}
return 0;
}