-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterative_peeling_naive_weighted.cpp
180 lines (144 loc) · 4.6 KB
/
iterative_peeling_naive_weighted.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
// Saurabh Sawlani
// Find maximum subgraph density for weighted graphs
// Uses BBST to store intermediate degrees
// 6-10 x slower than using linked lists, but simpler to understand
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include<cassert>
#include <vector>
#include <queue>
#include<list>
#include <set>
#include<cstring>
#include<ctime>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////
// Helper for fast input
inline char GET_CHAR(){
const int maxn = 131072;
static char buf[maxn],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,maxn,stdin),p1==p2)?EOF:*p1++;
}
inline int getInt() {
int res(0);
char c = GET_CHAR();
while(c < '0') c = GET_CHAR();
while(c >= '0') {
res = res * 10 + (c - '0');
c = GET_CHAR();
}
return res;
}
////////////////////////////////////////////////////////////////////////////////////////
vector<int> deg;
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return deg[lhs]<deg[rhs] || (deg[lhs]==deg[rhs] && lhs<rhs);}
};
set<int,classcomp> deg_sorted; //BBST storing degrees
//////////////////////////////////////////////////////////////////////////////////////////////////////
// MAIN
//////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv) {
cout << "Finding maximum subgraph density (naive version using BBST)..." << endl;
auto startio = chrono::steady_clock::now();
int iters = atoi(argv[1]);
int m, n;
//cin >> n >> m;
n = getInt(); m = getInt();
int * init_deg = new int[n];
memset(init_deg, 0, sizeof(int) * n);
int * w = new int[n];
memset(w, 0, sizeof(int) * n);
deg.reserve(n);
set<pair<int, int>> * nbrs = new set<pair<int, int>>[n];
int sum_wts = 0;
for (int i = 0; i < m; i++) {
int p, q, wt;
p = getInt(); q = getInt(), wt = getInt();
//cin >> p >> q >> wt;
p -= 1;
q -= 1;
nbrs[p].insert(make_pair(q,wt));
nbrs[q].insert(make_pair(p,wt));
init_deg[p]+=wt;
init_deg[q]+=wt;
sum_wts += wt;
}
double mm_density = 0;
vector<bool> exists(n);
vector<bool> iterans(n);
vector<bool> ans(n);
auto endio = chrono::steady_clock::now();
int init_time = chrono::duration_cast<chrono::milliseconds>(endio - startio).count();
cout << "Time for reading input and initialization: " << init_time << " ms" << endl;
int sum_iter_times = 0;
for (int tt = 0; tt < iters; tt++) {
auto startiter = chrono::steady_clock::now();
deg_sorted.clear();
for (int i = 0; i < n; i++) {
deg[i] = w[i] + init_deg[i]; //degree for this iteration is "vertex weight" + actual degree
deg_sorted.insert(i);
}
double iter_max_density = (double) sum_wts / n;
int cur_sum_wts = sum_wts, cur_n = n;
fill(exists.begin(), exists.end(), true);
iterans = exists;
while (cur_n > 0) {
cur_n--;
int k = *(deg_sorted.begin()); //k = min degree vertex
w[k] = deg[k]; //increment vertex weight for the next iteration (self loops)
deg_sorted.erase(k); //delete k
for (pair<int, int> j : nbrs[k]) { //decrement degrees of k's neighbors
int nbr = j.first;
int nbrwt = j.second;
if (exists[nbr]) {
deg_sorted.erase(nbr);
deg[nbr] -= nbrwt;
cur_sum_wts -= nbrwt;
deg_sorted.insert(nbr);
}
exists[k] = false;
}
if (iter_max_density < (double) cur_sum_wts / cur_n) {
iter_max_density = (double) cur_sum_wts / cur_n;
iterans = exists;
}
}
if(iter_max_density > mm_density) {
mm_density = iter_max_density;
cout << "mm changing " << mm_density << endl;
ans = iterans;
}
auto enditer = chrono::steady_clock::now();
int elapsed = chrono::duration_cast<chrono::milliseconds>(enditer - startiter).count();
sum_iter_times += elapsed;
cout << "Max density AT iteration " << tt+1 <<": " << iter_max_density << endl;
cout << "Max density until iteration " << tt+1 <<": " << mm_density << endl;
cout << "Avg time per iteration: " << sum_iter_times/(tt+1) << " ms" << endl;
cout << "Total time: " << sum_iter_times + init_time << " ms" << endl;
}
string output_file;
ofstream outfile;
if (argc >= 3)
{
output_file = argv[2];
}
else
{
output_file = "soln.tmp";
}
outfile.open(output_file.c_str());
for (int i=0;i<n;i++)
{
if (ans[i]) outfile << i+1 << endl;
}
return 0;
}