-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathmain.cc
More file actions
414 lines (347 loc) · 17.2 KB
/
main.cc
File metadata and controls
414 lines (347 loc) · 17.2 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <algorithm>
#include "graph/temporal_network.h"
#include "util/parameters_helper.h"
using namespace std;
/**
*void Print( const set < shared_ptr < string >, cmp_str_ptr >& seed )
*
* Simple helper function. Prints the members of a set on the same line (with following newline).
*
*@param seed Vertices to print
*/
void Print(const set<shared_ptr<string>, cmp_str_ptr> &seed) {
set<shared_ptr<string> >::const_iterator it_s = seed.begin();
while (it_s != seed.end()) {
cout << **it_s << "|";
++it_s;
}
cout << endl;
}
/**
*double CalcDensity( const int& size, const double& Win, const double& Wout, const double& lambda )
*
* Function to calculate the density of a given set of vertices
*
*@param size Number of vertices in the set
*@param Win Total weight of edges between two vertices in the set
*@param Wout Total weight of edges with only 1 vertex in the set
*@param lambda Weight value between the two parts of the density measure
*
*@return Density of set of vertices
*/
double CalcDensity(const int &size, const double &Win, const double &Wout, const double &lambda) {
if (size < 1) return numeric_limits<double>::min();
double partA = ((1 - lambda) * (Win / (Win + Wout)));
double partB = (lambda * ((2 * Win) / (size * (size - 1))));
if (size == 1) partB = lambda;
return partA + partB;
}
/**
*map < double, set < shared_ptr < string >, cmp_str_ptr > > Components ( set < shared_ptr < string >, cmp_str_ptr > seed, shared_ptr < Network > G, double lambda )
*
* Splits a set of vertices into the connected components that it is made up of. Associates each component
* with its density value.
*
*@param seed Set of vertices to split
*@param G Network structure
*@param lambda Value for density calculation (see CalcDensity)
*
*@return A map associating density values with the components
*/
map<double, set<shared_ptr<string>, cmp_str_ptr> >
Components(set<shared_ptr<string>, cmp_str_ptr> seed, shared_ptr<network> G, double lambda) {
map<double, set<shared_ptr<string>, cmp_str_ptr> > result;
set<shared_ptr<string>, cmp_str_ptr>::iterator it_s, it_u, it_v;
set<shared_ptr<string>, cmp_str_ptr> seen;
double win = 0, wout = 0;
for (it_s = seed.begin(); it_s != seed.end(); it_s++) { //Go through each vertex in set
if (seen.find(*it_s) != seen.end()) continue; //If already assigned to partition, skip
win = 0;
wout = 0;
set<shared_ptr<string>, cmp_str_ptr> component, to_check;
to_check.insert(*it_s); //Initialize stats for new component
//Keep expanding the component based off the neighborhoods of members until no new members can be added
while (to_check.size() != 0) {
//Add the first vertex to component
it_u = to_check.begin();
component.insert(*it_u);
seen.insert(*it_u);
map<shared_ptr<string>, double, cmp_str_ptr> N = G->GetNeighborhood(*it_u);
to_check.erase(it_u);
//Go through vertex neighborhodd to find vertices that are also in the component while tracking density measures
map<shared_ptr<string>, double, cmp_str_ptr>::iterator it_n;
for (it_n = N.begin(); it_n != N.end(); it_n++) {
if ((seen.find(it_n->first) == seen.end()) && (seed.find(it_n->first) != seed.end())) {
to_check.insert(it_n->first);
win += it_n->second;
} else {
wout += it_n->second;
}
}
}
//document results
result.insert(
pair<double, set<shared_ptr<string>, cmp_str_ptr> >(CalcDensity(component.size(), win, wout, lambda),
component));
}
//Return full results
return result;
}
/**
*void ExpandSeed ( set < shared_ptr < string >, cmp_str_ptr >& seed, shared_ptr < Network > G, double lambda )
*
* Main portion of CIS. Takes a seed and iteratively adds neighbors/removes members in order to maximize the
* CalcDensity() value of the set of vertices. Only rule is that the component must remain connected. If it
* becomes disconnected, the component that has the highest independant density is taken.
*
*@param seed Set of vertices to start from
*@param G Network structure
*@param lambda Value for density calculation
*/
void ExpandSeed(set<shared_ptr<string>, cmp_str_ptr> &seed, shared_ptr<network> G, double lambda) {
cout << *(*seed.begin()) << ",,,";
map<shared_ptr<string>, pair<double, double>, cmp_str_ptr> members, neighbors;
set<shared_ptr<string>, cmp_str_ptr> fringe;
set<shared_ptr<string>, cmp_str_ptr>::iterator it_s;
map<shared_ptr<string>, double, cmp_str_ptr>::iterator it_n;
map<shared_ptr<string>, pair<double, double>, cmp_str_ptr>::iterator it_d, it_d2;
double seed_win = 0, seed_wout = 0;
for (it_s = seed.begin(); it_s != seed.end(); it_s++) { //Tally members of the seed, calculating individual
// Win and Wout measures and noting neighborhood
map<shared_ptr<string>, double, cmp_str_ptr> N = G->GetNeighborhood(*it_s);
double Win = 0, Wout = 0;
for (it_n = N.begin(); it_n != N.end(); it_n++) {
if (seed.find(it_n->first) != seed.end()) { //If the neighbor is also in the seed, increase weight_in
Win += it_n->second;
seed_win += it_n->second;
} else { //Else increase weight out
Wout += it_n->second;
seed_wout += it_n->second;
fringe.insert(it_n->first);
}
}
members.insert(pair<shared_ptr<string>, pair<double, double> >(shared_ptr<string>(*it_s),
pair<double, double>(Win, Wout)));
//cout << **it_s << " : " << Win << " " << Wout << endl;
}
seed_win /= 2.0; //Internal edges were counted twice (assumed undirected)
//cout << "Fringe: " << endl;
for (it_s = fringe.begin(); it_s != fringe.end(); it_s++) { //Tally same information for neighborhood
map<shared_ptr<string>, double, cmp_str_ptr> N = G->GetNeighborhood(*it_s);
double Win = 0, Wout = 0;
for (it_n = N.begin(); it_n != N.end(); it_n++) {
if (seed.find(it_n->first) != seed.end()) {
Win += it_n->second;
} else {
Wout += it_n->second;
}
}
neighbors.insert(pair<shared_ptr<string>, pair<double, double> >(shared_ptr<string>(*it_s),
pair<double, double>(Win, Wout)));
//cout << **it_s << " : " << Win << " " << Wout << endl;
}
bool changed = true;
srand(time(NULL));
//cout << endl << " NEW SEED " << endl << endl;
//While the seed is changing, add new members and remove poor members
while (changed) {
changed = false;
vector<shared_ptr<string> > to_check;
vector<set<shared_ptr<string>, cmp_str_ptr> > order_by_degree;
set<shared_ptr<string>, cmp_str_ptr>::iterator it_deg;
for (it_d = neighbors.begin(); it_d != neighbors.end(); it_d++) {
int deg = G->Degree(it_d->first);
if (order_by_degree.size() < deg + 1) order_by_degree.resize(deg + 1);
order_by_degree[deg].insert(it_d->first);
}
for (int k = 0; k < order_by_degree.size(); k++) {
for (it_deg = order_by_degree[k].begin(); it_deg != order_by_degree[k].end(); it_deg++) {
to_check.push_back(*it_deg);
}
}
/*for ( it_d = neighbors.begin(); it_d != neighbors.end(); it_d++ ){
to_check.push_back(it_d->first);
}
random_shuffle ( to_check.begin(), to_check.end() ); ///Get the neighborhood in a random order*/
for (unsigned int i = 0; i < to_check.size(); i++) { // Go through all the neighbors
it_d = neighbors.find(to_check[i]);
//cout << *to_check[i] << " to be checked for addition : " << seed_win << " " << seed_wout << " " << it_d->second.first << " " << it_d->second.second << " " << CalcDensity(seed.size(), seed_win, seed_wout, lambda) << " " << CalcDensity(seed.size() + 1, seed_win + it_d->second.first, seed_wout + it_d->second.second - it_d->second.first, lambda) << endl;
if (CalcDensity(seed.size(), seed_win, seed_wout, lambda) <
CalcDensity(seed.size() + 1, seed_win + it_d->second.first,
seed_wout + it_d->second.second - it_d->second.first, lambda)) {
//If the density would increase by includeing the vertex - do it
//cout << "...Added" << endl;
changed = true; //Mark the change in seed
seed_win += it_d->second.first;
seed_wout = seed_wout - it_d->second.first + it_d->second.second;
seed.insert(it_d->first); //Update seed
members.insert(pair<shared_ptr<string>, pair<double, double> >(it_d->first, it_d->second));
neighbors.erase(to_check[i]); //Update local trackers
//UPDATE MEMBER AND NEIGHBOR LISTS
// The Win and Wout values of vertices connected to the added vertex have changed...
map<shared_ptr<string>, double, cmp_str_ptr> N = G->GetNeighborhood(to_check[i]);
for (it_n = N.begin(); it_n != N.end(); it_n++) {
if ((it_d2 = members.find(it_n->first)) != members.end()) { //Update member
it_d2->second.first += it_n->second;
it_d2->second.second -= it_n->second;
} else if ((it_d2 = neighbors.find(it_n->first)) != neighbors.end()) { //Update current neighbor
it_d2->second.first += it_n->second;
it_d2->second.second -= it_n->second;
} else { //Add new neighbor
map<shared_ptr<string>, double, cmp_str_ptr> N2 = G->GetNeighborhood(it_n->first);
map<shared_ptr<string>, double, cmp_str_ptr>::iterator it_d3;
double newWin = 0, newWout = 0;
for (it_d3 = N2.begin(); it_d3 != N2.end(); it_d3++) {
if (members.find(it_d3->first) != members.end()) newWin += it_d3->second;
else newWout += it_d3->second;
}
neighbors.insert(pair<shared_ptr<string>, pair<double, double> >(it_n->first,
pair<double, double>(newWin,
newWout)));
}
}
}
/*Print ( seed );
for ( it_d2 = members.begin(); it_d2 != members.end(); it_d2++ ){
cout << *(it_d2->first) << "|";
}
cout << endl;*/
}
//REPEAT FOR MEMBERS (reversing mathematical signs where necessary, of course)
to_check.clear();
order_by_degree.clear();
for (it_d = members.begin(); it_d != members.end(); it_d++) {
int deg = G->Degree(it_d->first);
if (order_by_degree.size() < deg + 1) order_by_degree.resize(deg + 1);
order_by_degree[deg].insert(it_d->first);
}
for (int k = 0; k < order_by_degree.size(); k++) {
for (it_deg = order_by_degree[k].begin(); it_deg != order_by_degree[k].end(); it_deg++) {
to_check.push_back(*it_deg);
}
}
/* for ( it_d = members.begin(); it_d != members.end(); it_d++ ){
to_check.push_back(it_d->first);
}
random_shuffle ( to_check.begin(), to_check.end() );*/
for (unsigned int i = 0; i < to_check.size(); i++) {
it_d = members.find(to_check[i]);
//cout << *to_check[i] << " to be checked for removal : " << seed_win << " " << seed_wout << " " << it_d->second.first << " " << it_d->second.second << " " << CalcDensity(seed.size(), seed_win, seed_wout, lambda) << " " << CalcDensity(seed.size() + 1, seed_win + it_d->second.first, seed_wout + it_d->second.second - it_d->second.first, lambda) << endl;
if (CalcDensity(seed.size(), seed_win, seed_wout, lambda) <
CalcDensity(seed.size() - 1, seed_win - it_d->second.first,
seed_wout - it_d->second.second + it_d->second.first, lambda)) {
//cout << "...Removed" << endl;
changed = true;
seed_win -= it_d->second.first;
seed_wout = seed_wout + it_d->second.first - it_d->second.second;
seed.erase(it_d->first);
neighbors.insert(pair<shared_ptr<string>, pair<double, double> >(it_d->first, it_d->second));
members.erase(to_check[i]);
//UPDATE MEMBER AND NEIGHBOR LISTS
map<shared_ptr<string>, double, cmp_str_ptr> N = G->GetNeighborhood(to_check[i]);
for (it_n = N.begin(); it_n != N.end(); it_n++) {
if ((it_d2 = members.find(it_n->first)) != members.end()) { //Update member
it_d2->second.first -= it_n->second;
it_d2->second.second += it_n->second;
} else if ((it_d2 = neighbors.find(it_n->first)) != neighbors.end()) { //Update current neighbor
it_d2->second.first -= it_n->second;
it_d2->second.second += it_n->second;
} //No new neighbors can be added to consider when removing members
}
}
}
//Get best component to move forward with
//map < double, set < shared_ptr < string >, cmp_str_ptr > > comps = Components(seed, G, lambda);
//seed = (comps.begin())->second;
//Print ( seed );
}
}
/**
*void Print( const set < shared_ptr < string >, cmp_str_ptr >& seed )
*
* Simple helper function. Prints the members of a set on the same line (with following newline).
*
*@param seed Vertices to print
*/
void Print(const set<shared_ptr<string>, cmp_str_ptr> &seed, ofstream &fout, const string delim) {
set<shared_ptr<string> >::const_iterator it_s = seed.begin();
fout << **it_s;
++it_s;
while (it_s != seed.end()) {
fout << delim << **it_s;
++it_s;
}
fout << endl;
}
/**
*Insertion point for CIS
*/
int main(int argc, char **argv) {
//Get command line params
parameters_helper P;
P.Read(argc, argv);
string inputfile, outputfile, delimiters, seed_file, seed_delim, output_delim;
bool directed, given_seeds;
double lambda;
P.set<string>(&inputfile, "i", "network.dat");
P.set<string>(&outputfile, "o", "clusters.dat");
P.set<string>(&delimiters, "dl", "|");
P.set<string>(&seed_file, "s", "seeds.dat");
P.set<string>(&seed_delim, "sdl", "|");
P.set<double>(&lambda, "l", 0);
P.set<string>(&output_delim, "odl", "|");
P.boolset(&given_seeds, "s");
P.boolset(&directed, "dir");
if (argc < 2) {
cout << "Usage:\n\t./cis -i network -o output -dl delimiter -s seed file -l lambda value" << endl;
return 0;
}
//Load the network
temporal_network T;
shared_ptr<network> G(T.AddNetwork(inputfile, delimiters, directed));
set<set<shared_ptr<string>, cmp_str_ptr>, cmp_set_str> results;
// Either read seeds from file or go through each vertex as seed.
// Expand each seed and record the result
if (given_seeds) {
ifstream fin;
openFileHarsh(&fin, seed_file);
vector<string> fields;
while (fline_tr(&fin, &fields, seed_delim)) {
set<shared_ptr<string>, cmp_str_ptr> seed;
for (unsigned int i = 0; i < fields.size(); i++) {
seed.insert(shared_ptr<string>(new string(fields[i])));
}
ExpandSeed(seed, G, lambda);
results.insert(seed);
}
} else {
set<string>::const_iterator it_v = T.getFirstVertex();
while (it_v != T.getLastVertex()) {
set<shared_ptr<string>, cmp_str_ptr> seed;
seed.insert(shared_ptr<string>(new string(*it_v)));
ExpandSeed(seed, G, lambda);
cout << "!!!" << seed.size() << "\t:";
for (auto str:seed) {
cout << *str << ",";
}
cout << endl;
results.insert(seed);
//cout << seed.size() << endl;
++it_v;
}
}
for (auto iter_tmp = results.begin(); iter_tmp != results.end(); ++iter_tmp) {
for (auto iter_tmp2 = (*iter_tmp).begin(); iter_tmp2 != (*iter_tmp).end(); ++iter_tmp2) {
cout << *(*iter_tmp2) << ",";
}
cout << endl;
}
//Print resulting communities
set<set<shared_ptr<string>, cmp_str_ptr>, cmp_set_str>::iterator it_ss;
ofstream fout(outputfile.c_str());
for (it_ss = results.begin(); it_ss != results.end(); it_ss++) {
Print(*it_ss, fout, output_delim);
}
fout.close();
return 0;
}