-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListingSampling.cpp
213 lines (172 loc) · 7.67 KB
/
ListingSampling.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "ListingSampling.hpp"
bool cmp(const Butterfly &b1, const Butterfly &b2) { return b1.weight > b2.weight; }
void ListingSampling(Model &model) {
ComputePriority(model);
vector<Butterfly> candidate_butterfly_set;
clock_t start = clock();
cout << "Start Listing\n";
for (int &u: model.node) {
unordered_map<int, vector<Angle>> angle;
for (auto &edge1: model.neighbor[u]) {
int v = edge1.v;
if (model.priority[v] >= model.priority[u]) continue;
for (auto &edge2: model.neighbor[v]) {
int w = edge2.v;
if (model.priority[w] >= model.priority[u]) continue;
Angle new_angle = Model::EdgeToAngle(edge1, edge2);
angle[w].push_back(new_angle);
}
}
for (auto &[w, angles]: angle) {
for (int i = 0; i < angles.size(); i++) {
for (int j = i + 1; j < angles.size(); j++) {
Butterfly butterfly = Model::AngleToButterfly(angles[i], angles[j]);
candidate_butterfly_set.push_back(butterfly);
}
}
}
}
cout << "Finish Listing\n";
cout << "size = " << candidate_butterfly_set.size() << "\n";
sort(candidate_butterfly_set.begin(), candidate_butterfly_set.end(), cmp);
cout << "finish sort\n";
clock_t end = clock();
cout << "Listing Time : " << (double) (end - start) / CLOCKS_PER_SEC << " s " << endl;
start = clock();
// ProbabilityEstimation(model, candidate_butterfly_set);
ProbabilityEstimationKarpLuby(model, candidate_butterfly_set);
end = clock();
cout << "Listing Time : " << (double) (end - start) / CLOCKS_PER_SEC << " s " << endl;
}
bool EdgeExist(int u, int v, double pr, unordered_map<int, unordered_map<int, int>> &edge_exist) {
if (edge_exist[u][v] == 0) {
if (rand() % 100 <= pr * 100) {
edge_exist[u][v] = 1;
} else {
edge_exist[u][v] = 2;
}
}
if (edge_exist[u][v] == 1) {
return true;
} else {
return false;
}
}
void ProbabilityEstimation(Model &model, vector<Butterfly> &candidate_butterfly_set) {
// unordered_map<Butterfly, double, Butterfly::HashFunction> result;
for (int i = 0; i < model.monte_carlo_iteration; i++) {
double w_max = 0;
vector<Butterfly> max_butterfly_set;
// 0: not sampled 1: exists 2: not exists
unordered_map<int, unordered_map<int, int>> edge_exist;
for (auto &butterfly: candidate_butterfly_set) {
if (butterfly.weight < w_max) break;
if (!EdgeExist(butterfly.u1, butterfly.v1, butterfly.pr_u1_v1, edge_exist)) continue;
if (!EdgeExist(butterfly.u1, butterfly.v2, butterfly.pr_u1_v2, edge_exist)) continue;
if (!EdgeExist(butterfly.u2, butterfly.v1, butterfly.pr_u2_v1, edge_exist)) continue;
if (!EdgeExist(butterfly.u2, butterfly.v2, butterfly.pr_u2_v2, edge_exist)) continue;
max_butterfly_set.push_back(butterfly);
w_max = butterfly.weight;
}
for (auto &butterfly: max_butterfly_set) {
model.result[butterfly] += 1;
// result[butterfly] += 1;
}
}
model.PrintResult(model.monte_carlo_iteration);
}
bool Check(int &u, int &v, Butterfly &b) {
if ((u == b.u1 || u == b.u2) && (v == b.v1 || v == b.v2)) {
return false;
} else {
return true;
}
}
double Except(Butterfly &b1, Butterfly &b2) {
double p = 1;
if (Check(b1.u1, b1.v1, b2)) p *= b1.pr_u1_v1;
if (Check(b1.u1, b1.v2, b2)) p *= b1.pr_u1_v2;
if (Check(b1.u2, b1.v1, b2)) p *= b1.pr_u2_v1;
if (Check(b1.u2, b1.v2, b2)) p *= b1.pr_u2_v2;
return p;
}
void Add(Butterfly &b1, Butterfly &b2, unordered_map<int, unordered_map<int, int>> &edge_exist) {
if (Check(b1.u1, b1.v1, b2)) edge_exist[b1.u1][b1.v1] = 1;
if (Check(b1.u1, b1.v2, b2)) edge_exist[b1.u1][b1.v2] = 1;
if (Check(b1.u2, b1.v1, b2)) edge_exist[b1.u2][b1.v1] = 1;
if (Check(b1.u2, b1.v2, b2)) edge_exist[b1.u2][b1.v2] = 1;
}
bool Exist(Butterfly &b1, Butterfly &b2, unordered_map<int, unordered_map<int, int>> &edge_exist) {
// if (Check(b1.u1, b1.v1, b2)) if (!edge_exist[b1.u1][b1.v1]) return false;
// if (Check(b1.u1, b1.v2, b2)) if (!edge_exist[b1.u1][b1.v2]) return false;
// if (Check(b1.u2, b1.v1, b2)) if (!edge_exist[b1.u2][b1.v1]) return false;
// if (Check(b1.u2, b1.v2, b2)) if (!edge_exist[b1.u2][b1.v2]) return false;
if (Check(b1.u1, b1.v1, b2)) if (!EdgeExist(b1.u1, b1.v1, b1.pr_u1_v1, edge_exist)) return false;
if (Check(b1.u1, b1.v2, b2)) if (!EdgeExist(b1.u1, b1.v2, b1.pr_u1_v2, edge_exist)) return false;
if (Check(b1.u2, b1.v1, b2)) if (!EdgeExist(b1.u2, b1.v1, b1.pr_u2_v1, edge_exist)) return false;
if (Check(b1.u2, b1.v2, b2)) if (!EdgeExist(b1.u2, b1.v2, b1.pr_u2_v2, edge_exist)) return false;
return true;
}
void ProbabilityEstimationKarpLuby(Model &model, vector<Butterfly> &candidate_butterfly_set) {
// int check = false;
// for (int i = 0;i < min(100, (int)candidate_butterfly_set.size()); i++) {
for (int i = 0; i < candidate_butterfly_set.size(); i++) {
// srand(i);
int cnt = 0;
int L = 0;
for (int j = 0; j <= i; j++) {
if (candidate_butterfly_set[j].weight == candidate_butterfly_set[i].weight) {
L = j - 1;
break;
}
}
double S = 0;
vector<double> pr;
for (int j = 0; j <= L; j++) {
double p = Except(candidate_butterfly_set[j], candidate_butterfly_set[i]);
S += p;
pr.push_back(p);
}
for (auto &p: pr) {
p /= S;
}
double ratio = candidate_butterfly_set[i].pr * S * (candidate_butterfly_set[i].pr / 0.1 - 1);
ratio = max(ratio, candidate_butterfly_set[i].pr * S);
int r_limit = (int) (model.monte_carlo_iteration * ratio);
r_limit = max(r_limit, 1);
for (int r = 0; r < r_limit; r++) {
if (pr.size() == 0) break;
double p = 1.0 * (rand() % 100) / 100;
int j = 0;
while (p - pr[j] - 1e-9 > 0) {
p -= pr[j];
j++;
}
// 0: not sampled 1: exists 2: not exists
unordered_map<int, unordered_map<int, int>> edge_exist;
Add(candidate_butterfly_set[j], candidate_butterfly_set[i], edge_exist);
// for (auto &neighbor : model.neighbor) {
// for (auto &edge : neighbor) {
// EdgeExist(edge.u, edge.v, edge.pr, edge_exist);
// }
// }
cnt++;
for (int k = 0; k < j; k++) {
if (Exist(candidate_butterfly_set[k], candidate_butterfly_set[i], edge_exist)) {
cnt--;
break;
}
}
if (candidate_butterfly_set[i].u1 == 2 && candidate_butterfly_set[i].u2 == 3 &&
candidate_butterfly_set[i].v1 == 22552 && candidate_butterfly_set[i].v2 == 22553) {
if ((r + 1) % (r_limit / 100) == 0) {
double pp = (1 - 1.0 * cnt / (r + 1) * S) * candidate_butterfly_set[i].pr;
cout << r << " " << pp << endl;
}
}
}
double pp = (1 - 1.0 * cnt / (r_limit) * S) * candidate_butterfly_set[i].pr;
model.result[candidate_butterfly_set[i]] = pp;
}
model.PrintResult(1);
}