-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainbm.cpp
359 lines (343 loc) · 15.1 KB
/
mainbm.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "truss.h"
using namespace std;
int main(int argc, char *argv[]) {
string progName(argv[0]);
progName = progName.substr(2);
assert(argc >= 3);
string dataset(argv[1]);
string resInfo(argv[2]);
int argi = 4;
kInput = (argc >= argi) ? atoi(argv[argi - 1]) : 10;
++argi;
params.emplace_back(kInput);
int numRounds = (argc >= argi) ? atoi(argv[argi - 1]) : 10;
++argi;
params.emplace_back(numRounds);
int numV1Check = (argc >= argi) ? atoi(argv[argi - 1]) : 100;
++argi;
params.emplace_back(numV1Check);
int numV2Check = (argc >= argi) ? atoi(argv[argi - 1]) : 50;
++argi;
params.emplace_back(numV2Check);
int numPairsCheck = (argc >= argi) ? atoi(argv[argi - 1]) : 100;
++argi;
params.emplace_back(numPairsCheck);
infile = "datasets_txt/" + dataset + ".txt";
outfile = "res_txt/" + resInfo + ".txt";
fout.open(outfile.c_str(), ios_base::app);
// print the basic information of the experimental settings
cout << progName << " ";
fout << progName << " ";
cout << dataset << " ";
fout << dataset << " ";
params.resize(6); // filled with 0
for (auto param_ : params) {
cout << param_ << " ";
fout << param_ << " ";
}
cout << endl;
fout << endl;
auto bIOPs = numPairsCheck >> 1, bIIPs = numPairsCheck >> 1; // initially the budget is equally distributed
bool firstRoundIIPsBetter = false;
for (int i = 0; i <= numRounds; ++i) {
cout << "round " << i << endl;
fout << "round " << i << endl;
if (numPairsCheck == 1) {
if (i == 0) {
bIOPs = 1;
bIIPs = 1;
} else if (firstRoundIIPsBetter) {
bIOPs = 0;
bIIPs = 1;
} else {
bIOPs = 1;
bIIPs = 0;
}
}
auto startTimeZ = chrono::system_clock::now(); // time of truss update, etc.
if (i == 0) {
readOrderedSimpleGraph();
countTriangles(supp);
suppOrig = supp; // supp backup
} else {
varsReInit();
}
binSort(supp);
trussDecomp();
if (i == 0) {
startTime = chrono::system_clock::now();
}
fillInNodesEdges();
printNodeEdgeInfo();
printNodeEdgeInfoFile();
if (i == numRounds) { // last round
cout << "final k-truss size = " << mTk << endl;
fout << mTk << " ";
break;
}
auto endTimeZ = chrono::system_clock::now();
chrono::duration<double> elapsedSecondsZ = endTimeZ - startTimeZ;
sprintf(timeStr, "%.6f", elapsedSecondsZ.count());
cout << "time of truss update: " << timeStr << endl;
fout << "time of truss update: " << timeStr << endl;
auto startTimeA = chrono::system_clock::now();
computeNbrsInTkm1();
computeNbrsInTk();
findBestOutsideNodes(); // max-set-based pruning
int chosenV1, chosenV2;
int bestRes = 0, bestResIOPs = 0, bestResIIPs = 0;
VI &v1Pruned = Tkm1Nodes;
int n1 = (int)v1Pruned.size();
sort(v1Pruned.begin(), v1Pruned.end(), compMoreIncidentPotential);
VI &v2Pruned = bestOutsideNodes;
int n2 = (int)v2Pruned.size();
sort(v2Pruned.begin(), v2Pruned.end(), compMoreNbrsInTkm1);
numV1Check = (numV1Check == -1) ? n1 : min(numV1Check, n1);
numV2Check = (numV2Check == -1) ? n2 : min(numV2Check, n2);
auto endTimeA = chrono::system_clock::now();
chrono::duration<double> elapsedSecondsA = endTimeA - startTimeA;
sprintf(timeStr, "%.6f", elapsedSecondsA.count());
cout << "time of preparation: " << timeStr << endl;
fout << "time of preparation: " << timeStr << endl;
// IOPs
auto numIOPs = numV1Check * numV2Check;
tqdm bar;
int curr = 0;
if (bIOPs > 0) {
auto startTimeB = chrono::system_clock::now(); // time of IOPs-finding pairs
VE bestPairsIOPs(bIOPs, TEdge{-1, -1});
vector<tuple<size_t, size_t>> bestScoresIOPs(bIOPs, {0, 0});
vector<VI> bestNewNbrsList(bIOPs);
for (auto i_ = 0; i_ < numV1Check; ++i_) {
int v1_ = v1Pruned[i_];
vector<bool> bestN1(n, false), bestNN1(n, false);
VI baseNewNbrs; // incident prospects
baseNewNbrs.reserve(nbrsInTkm1[v1_].size());
for (auto &[x_, _] : suppTkm1[v1_]) {
bestNN1[x_] = true;
}
for (auto &x_ : nbrsInTkm1[v1_]) {
if (!bestNN1[x_]) {
baseNewNbrs.emplace_back(x_);
}
bestN1[x_] = true;
}
unordered_map<int, VI> n2HSE;
n2HSE.reserve(n);
unordered_set<int> baseHSE;
baseHSE.reserve(v2ShNbrs[v1_].size());
for (auto &n1_ : v2ShNbrs[v1_]) {
for (auto &[n2_, _] : suppTkm1[n1_]) {
if (!bestN1[n2_]) {
n2HSE[n2_].emplace_back(n1_);
}
}
}
for (auto nn_ : baseNewNbrs) {
baseHSE.insert(n2HSE[nn_].begin(), n2HSE[nn_].end());
}
for (auto j_ = 0; j_ < numV2Check; ++j_) {
bar.progress(curr++, numIOPs);
auto v2_ = v2Pruned[j_];
VI &n2_ = nbrsInTkm1[v2_];
VI newNbrsV1V2 = baseNewNbrs;
newNbrsV1V2.reserve(n2_.size() + baseNewNbrs.size());
vector<bool> newNbrs(n, false);
bool e12 = false; // edge between v1 and v2 exist?
unordered_set<int> HSEv1v2Incident = baseHSE;
HSEv1v2Incident.reserve(v2ShNbrs[v1_].size());
for (auto x_ : n2_) {
if (x_ == v1_) {
e12 = true;
continue;
}
if (!bestN1[x_]) {
newNbrs[x_] = true;
newNbrsV1V2.emplace_back(x_);
HSEv1v2Incident.insert(n2HSE[x_].begin(), n2HSE[x_].end());
}
}
auto numOfPHSEsV1V2Incident = HSEv1v2Incident.size();
auto numNewNbrsV1V2 = newNbrsV1V2.size();
if (e12) {
--numNewNbrsV1V2;
}
// shell edges
size_t numOfPHSEsV1V2 = 0;
for (auto &[x_, y_] : Skm1Edges) {
if (x_ == v1_ || y_ == v1_) {
continue;
}
bool xNN = bestNN1[x_];
bool yNN = bestNN1[y_];
bool xN = bestN1[x_];
bool yN = bestN1[y_];
bool xNew = newNbrs[x_];
bool yNew = newNbrs[y_];
if (xNN && yNN) {
continue;
}
if ((xN || xNew) && (yN || yNew)) {
++numOfPHSEsV1V2;
}
}
numOfPHSEsV1V2 += numOfPHSEsV1V2Incident;
tuple<size_t, size_t> scoreV1V2 = make_tuple(numOfPHSEsV1V2, numNewNbrsV1V2);
auto minScorePos = min_element(bestScoresIOPs.begin(), bestScoresIOPs.end());
auto minScore = *minScorePos;
auto minScoreIndex = minScorePos - bestScoresIOPs.begin();
if (scoreV1V2 > minScore) {
bestPairsIOPs[minScoreIndex] = TEdge{v1_, v2_};
bestScoresIOPs[minScoreIndex] = scoreV1V2;
bestNewNbrsList[minScoreIndex] = newNbrsV1V2;
}
}
}
bar.finish();
auto endTimeB = chrono::system_clock::now();
chrono::duration<double> elapsedSecondsB = endTimeB - startTimeB;
sprintf(timeStr, "%.6f", elapsedSecondsB.count());
cout << "time of IOPs-finding pairs: " << timeStr << endl;
fout << "time of IOPs-finding pairs: " << timeStr << endl;
auto startTimeC = chrono::system_clock::now(); // time of IOPs-checking pairs
if (numPairsCheck == 1 && i > 0) {
auto &[bestV1, bestV2] = bestPairsIOPs[0];
chosenV1 = bestV1;
chosenV2 = bestV2;
} else {
for (auto ip = 0; ip < bIOPs; ++ip) {
auto &[bestV1, bestV2] = bestPairsIOPs[ip];
if (bestV1 == -1 || bestV2 == -1) {
continue;
}
int resV1V2 = checkMergerResultIOPInputNewNbrs(bestV1, bestV2, bestNewNbrsList[ip]);
if (resV1V2 > bestResIOPs) {
bestResIOPs = resV1V2;
chosenV1 = bestV1;
chosenV2 = bestV2;
}
}
bestRes = bestResIOPs;
}
auto endTimeC = chrono::system_clock::now();
chrono::duration<double> elapsedSecondsC = endTimeC - startTimeC;
sprintf(timeStr, "%.6f", elapsedSecondsC.count());
cout << "time of IOPs-checking pairs: " << timeStr << endl;
fout << "time of IOPs-checking pairs: " << timeStr << endl;
}
// IIPs
if (bIIPs > 0) {
VE bestPairsIIPs(bIIPs, TEdge{-1, -1});
VI bestScoresIIPs(bIIPs, -mTotal);
auto numIIPs = numV1Check * (numV1Check - 1) / 2;
auto startTimeB = chrono::system_clock::now(); // time of IIPs-finding pairs
curr = 0;
for (auto i_ = 0; i_ < numV1Check - 1; ++i_) {
int v1_ = v1Pruned[i_];
VI &n1_ = nbrsInTkm1[v1_];
vector<bool> membershipCnt1(n, false);
vector<bool> membershipCntK1(n, false);
for (auto &x_ : n1_) {
membershipCnt1[x_] = true;
}
for (auto &x_ : nbrsInTk[v1_]) {
membershipCntK1[x_] = true;
}
for (auto j_ = i_ + 1; j_ < numV1Check; ++j_) {
bar.progress(curr++, numIIPs);
auto v2_ = v1Pruned[j_];
VI &n2_ = nbrsInTkm1[v2_];
vector<bool> membershipCnt2(n, false);
int scoreV1V2 = 0;
for (auto &x_ : n2_) {
membershipCnt2[x_] = true;
}
for (auto &x_ : nbrsInTk[v2_]) {
if (x_ == v1_ || membershipCntK1[x_]) --scoreV1V2;
}
for (auto &[x_, y_] : Skm1Edges) {
if (x_ == v1_ || x_ == v2_ || y_ == v1_ || y_ == v2_) { // only checking non-incident edges
continue;
}
bool x1_ = membershipCnt1[x_], x2_ = membershipCnt2[x_];
bool y1_ = membershipCnt1[y_], y2_ = membershipCnt2[y_];
if ((!x1_ && !x2_) || (!y1_ && !y2_)) {
continue;
}
if ((!x1_ && !y2_) || (!x2_ && !y1_)) {
++scoreV1V2;
} else if (x1_ && x2_ && y1_ && y2_) {
--scoreV1V2;
}
}
auto minScorePos = min_element(bestScoresIIPs.begin(), bestScoresIIPs.end());
auto minScore = *minScorePos;
auto minScoreIndex = minScorePos - bestScoresIIPs.begin();
if (scoreV1V2 > minScore) {
bestPairsIIPs[minScoreIndex] = TEdge{v1_, v2_};
bestScoresIIPs[minScoreIndex] = scoreV1V2;
}
}
}
bar.finish();
auto endTimeB = chrono::system_clock::now();
chrono::duration<double> elapsedSecondsB = endTimeB - startTimeB;
sprintf(timeStr, "%.6f", elapsedSecondsB.count());
cout << "time of IIPs-finding pairs: " << timeStr << endl;
fout << "time of IIPs-finding pairs: " << timeStr << endl;
auto startTimeC = chrono::system_clock::now(); // IIPs-checking pairs
if (numPairsCheck == 1 && i > 0) {
auto &[bpu, bpv] = bestPairsIIPs[0];
chosenV1 = bpu;
chosenV2 = bpv;
} else {
for (auto &[bpu, bpv]: bestPairsIIPs) {
if (bpu == -1 || bpv == -1)
continue;
int resAfterMerger = checkMergerResultIIP(bpu, bpv);
if (resAfterMerger > bestRes) {
bestRes = resAfterMerger;
chosenV1 = bpu;
chosenV2 = bpv;
}
}
}
auto endTimeC = chrono::system_clock::now();
chrono::duration<double> elapsedSecondsC = endTimeC - startTimeC;
sprintf(timeStr, "%.6f", elapsedSecondsC.count());
cout << "time of IIPs-checking pairs: " << timeStr << endl;
fout << "time of IIPs-checking pairs: " << timeStr << endl;
}
// budget adjustment
auto startTimeD = chrono::system_clock::now(); // time of update the graph
auto minBudget = max(2 * numPairsCheck / numRounds, 1);
if (bestRes > bestResIOPs) { // IIPs give better performance
bIIPs = min(numPairsCheck - minBudget, bIIPs + minBudget);
bIOPs = max(minBudget, bIOPs - minBudget);
if (numPairsCheck == 1 && i == 0) {
firstRoundIIPsBetter = true;
}
} else {
bIIPs = max(minBudget, bIIPs - minBudget);
bIOPs = min(numPairsCheck - minBudget, bIOPs + minBudget);
}
if (i == numRounds - 1) { // all pairs have been decided
endTime = chrono::system_clock::now();
}
updateGraph(chosenV1, chosenV2);
cout << "merging " << chosenV1 << " and " << chosenV2 << endl;
fout << "merging " << chosenV1 << " and " << chosenV2 << endl;
supp = suppOrig;
auto endTimeD = chrono::system_clock::now();
chrono::duration<double> elapsedSecondsD = endTimeD - startTimeD;
sprintf(timeStr, "%.6f", elapsedSecondsD.count());
cout << "time of updating the graph: " << timeStr << endl;
fout << "time of updating the graph: " << timeStr << endl;
}
chrono::duration<double> elapsedSeconds = endTime - startTime;
sprintf(timeStr, "%.6f", elapsedSeconds.count());
cout << "total runtime = " << timeStr << " seconds" << endl;
fout << timeStr << endl;
fout.close();
return 0;
}