-
Notifications
You must be signed in to change notification settings - Fork 1
/
DHCS.cpp
398 lines (356 loc) · 11.5 KB
/
DHCS.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
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
#include <iostream>
#include <vector>
#include <set>
#include <cassert>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int csl(vector<int> &a, vector<int> &t, vector<int> &cnt, vector<int> &dsp, int size)
{
int n = a.size();
for(int i = 0; i < n - 2; i++)
{
int ai = a[i];
for(int j = i + 1; j < n - 1; j++)
{
int aj = a[j];
int m = cnt[ai * size + aj];
int l = dsp[ai * size + aj];
m += l;
for(int k = j + 1; k < n; k++)
{
int ak = a[k];
bool found = false;
while(l < m)
{
if(ak == t[l++])
{
found = true;
break;
}
}
if(!found) return 0;
}
}
}
return 1;
}
bool compare(vector<int> a, vector<int> b)
{
return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
}
bool equal_vector(vector<int> a, vector<int> b)
{
if(a.size() != b.size()) return false;
for(int i = 0; i < a.size(); i++) if(a[i] != b[i]) return false;
return true;
}
double scale(int i)
{
return (3.0 + sqrt(1.0 + 8.0 * (double)i))/2.0;
}
bool tally(vector<int> &e, vector<int> &t, int m)
{
static bool seed = true;
int tsize = t.size(), esize = e.size();
for(int i = 0; i < tsize; i++) t[i] = 0;
for(int i = 0; i < esize; i++) t[e[i]]++;
if(seed)
{
for(int i = 0; i < tsize; i++)
{
if(t[i] < (m - 1)*(m - 2)/2) t[i] = 0;
seed = false;
}
}
int n = 0;
int min = esize;
for(int i = 0; i < tsize; i++)
{
int t0 = t[i];
if(t0 != 0)
{
if(min > t0) min = t0;
n++;
}
}
int max = (n - 1)*(n - 2)/2;
cout << "min: " << scale(min) << " max: " << scale(max) <<endl;
if(min == max) return false;
for(int i = 0; i < tsize; i++) if(t[i] == min) {t[i] = 0; break;}
int j = 0;
for(int i = 0; i < esize; i+=3)
{
int e0 = e[i+0], e1 = e[i+1], e2 = e[i+2];
if(t[e0] != 0 && t[e1] != 0 && t[e2] != 0)
{
e[j+0] = e0; e[j+1] = e1; e[j+2] = e2;
j+=3;
}
}
e.resize(j);
return true;
}
int main(int argc, char *argv[])
{
//STEP 1: READ IN TRIPLES
// Result: One dimensional array of size nt starting from 1
unsigned long int nt=0;
int *triples;
int *doubles;
const char* file;
char ac[256];
int Needed = 1;
long int sample = 100000;
long int numoftrips;
int done=0;
vector< vector<int> > se, re;
vector<int> A, B, C, D;
vector< vector<int> > Triples; // Declare two dimensional vector of vectors
vector< vector<int> >::iterator iter_ii;
vector<int>::iterator iter_jj;
vector<int> cnt, dsp;
vector<int> ele;
size_t arraysize = 1024000000;
unsigned int n,count,j,i,k,num,entered;
j=i=k=count=0;
triples = (int*) calloc (arraysize, sizeof(int));
//doubles = (int*) calloc (arraysize, sizeof(int));
//if(triples==NULL || doubles==NULL) exit(1);
//(below) Read t#xx.txt into Triples-----------------------------------------------------------------------
if (argc == 0){
fprintf(stderr,"Usage: ./dhcs file\n");
exit(1);
}
//int max_clique = 300; //TODO
ifstream is(argv[1]);
if(!is.good()) {
cout << "Error: file could not be opened" << endl;
exit(1);
}
ifstream indata;
indata.open(argv[1]);
if(!indata) {
cerr << "Error: file could not be opened" << endl;
exit(1);
}
// indata >> num; // read in t1xx.txt
i=0;
while(!indata.eof()){
char line[255];
indata.getline(line, 255);
string sline(line);
if(sline.empty()) continue;
istringstream iss(sline);
iss >> triples[i+0];
iss >> triples[i+1];
iss >> triples[i+2];
i+=3;
}
indata.close();
nt=i;numoftrips=nt/3;
cout << nt << " " << numoftrips << " Triples Have Been Read" <<endl;
Triples.clear();
Triples.reserve(nt);
for(i=0;i<nt;i+=3){
D.clear();
D.push_back(triples[i]);
D.push_back(triples[i+1]);
D.push_back(triples[i+2]);
Triples.push_back(D);
}
cout << D[0] << " " << D[1] << " " << D[2] << endl;
//UNCOMMENT BELOW TO PRINT Triples
// cout << endl << "Triples:";
// for(iter_ii=Triples.begin(); iter_ii!=Triples.end(); iter_ii++)
// {
// printf("\n");
// for(iter_jj=(*iter_ii).begin(); iter_jj!=(*iter_ii).end(); iter_jj++)
// {
// printf("%d\t",*iter_jj);
// }
// }
// cout << endl;
//UNCOMMENT ABOVE TO PRINT Triples
//(above) Input t1#.txt into Triples-----------------------------------------------------------------------
#if 1
double ta, tb;
int edges = nt;
int m = Needed;
vector<int> e(edges);
for(int i = 0; i < edges; i++) e[i] = triples[i] - 1;
int nodes = *max_element(e.begin(), e.end()) + 1;
vector<int> v(nodes);
vector<int> t(nodes);
ta = clock();
while(tally(e, t, m));// cout << "Edges: " << e.size() << " Nodes: " << v.size() << endl;
tb = clock();
int l = 0;
for(int i = 0; i < nodes; i++) if(t[i] != 0) v[l++] = i;
v.resize(l);
cout << endl;
cout << "Clique: " << v.size() << " | ";
for(int i = 0; i < v.size(); i++) cout << v[i] + 1 << " ";
cout << endl;
cout << "Time: " << (tb - ta)/CLOCKS_PER_SEC << endl;
return 0;
#else
//STEP 2: DO THE WHILE LOOP IN Hypergrpah.nb
//
// Note 1 : The outer union in "Union[Array[Union @@ RandomSample[t, l] &, m]]"
// is not implemented since I don't know how to union two vector of vectors
//
// Note 2 : In my RandomSample equivalent we are not assured that we are not getting
// repeats!
//
int TMAX = *max_element(D.begin(), D.end()) + 1;//Idependent of base index 0 or 1!
cout << "Table: " << TMAX - 1 << endl;
cnt.resize(TMAX * TMAX, 0);
dsp.resize(TMAX * TMAX, 0);
for(int i = 0; i < Triples.size(); i++) cnt[Triples[i][0] * TMAX + Triples[i][1]]++;
for(int t = 0, i = 0; i < TMAX * TMAX; i++)
{
dsp[i] = t;
t += cnt[i];
}
ele.resize(Triples.size());
for(int i = 0; i < Triples.size(); i++) ele[i] = Triples[i][2];
vector<int> wgh;
wgh.resize(TMAX, 0);
for(int i = 0; i < Triples.size(); i++) wgh[Triples[i][0]]++;
for(int i = 0; i < Triples.size(); i++) wgh[Triples[i][1]]++;
for(int i = 0; i < Triples.size(); i++) wgh[Triples[i][2]]++;
for(iter_jj=wgh.begin(); iter_jj!=wgh.end(); iter_jj++) cout << *iter_jj << " ";
cout << endl;
double timea, timeb;
timea = clock();
srand ( time(NULL) );//initialize random seed
int MMAX,NMAX,max,csize,r1,r2,cs;
se.reserve(sample);
se.assign(Triples.begin(), Triples.end());
re.reserve(sample);
re.clear();
while(done==0 && se.size()>1){
MMAX = Triples.size();
NMAX = se.size();
max=0;
for(j=0;j<sample;j++){
C.clear();
r1=rand() % NMAX;
//r2=rand() % NMAX;
//set_union(se[r1].begin(), se[r1].end(), se[r2].begin(), se[r2].end(), back_inserter(C));
r2=rand() % MMAX;
set_union(se[r1].begin(), se[r1].end(), Triples[r2].begin(), Triples[r2].end(), back_inserter(C));
cs = csl(C, ele, cnt, dsp, TMAX);
/*
cout << endl << "C:";
for(iter_jj=C.begin(); iter_jj!=C.end(); iter_jj++) cout << *iter_jj << " ";
cout << endl << cs <<endl;
*/
if(cs==1){
//if(!binary_search(se.begin(), se.end(), C))
re.push_back(C);
csize=C.size();
if(csize>max)max=csize;
if(csize>=Needed){
cout << "!!!!!!!!! Solution found of length " << C.size() << " !!!!!!!!!" << endl;
for(iter_jj=C.begin(); iter_jj!=C.end(); iter_jj++) cout << *iter_jj << "(" << wgh[*iter_jj] << ") ";
cout << endl;
done =1;
break;}
}//end if
}//end for loop
#if 1
/*
vector<bool> rf(re.size());
for(int i = 0; i < re.size(); i++)
{
bool rb = true;
for(int j = 0; j < re.size(); j++)
{
C.clear();
set_intersection(re[i].begin(), re[i].end(), re[j].begin(), re[j].end(), back_inserter(C));
if((re[i] != re[j]) && (C.size() == re[i].size())) {rb = false; break;}
}
rf[i] = rb;
}
int l = 0;
for(int i = 0; i < re.size(); i++)
{
if(rf[i] == true)
{
re[l] = re[i]; l++;
}
}
re.resize(l);
*/
//set< vector<int> > rs(re.begin(), re.end());
//rs.insert(re.begin(), re.end());
//se.assign(rs.begin(), rs.end());
//rs.clear();
se.assign(re.begin(), re.end());
re.clear();
#else
vector<bool> rf(re.size());
for(int i = 0; i < re.size(); i++)
{
bool rb = true;
for(int j = 0; j < re.size(); j++)
{
C.clear();
set_intersection(re[i].begin(), re[i].end(), re[j].begin(), re[j].end(), back_inserter(C));
if((re[i] != re[j]) && (C.size() == re[i].size())) {rb = false; break;}
}
rf[i] = rb;
}
int l = 0;
for(int i = 0; i < re.size(); i++)
{
if(rf[i] == true)
{
re[l] = re[i]; l++;
}
}
re.resize(l);
set< vector<int> > rs(re.begin(), re.end());
re.assign(rs.begin(), rs.end());
vector<bool> sf(se.size());
for(int i = 0; i < se.size(); i++)
{
bool sb = true;
for(int j = 0; j < re.size(); j++)
{
C.clear();
set_intersection(se[i].begin(), se[i].end(), re[j].begin(), re[j].end(), back_inserter(C));
if(C.size() == se[i].size()) {sb = false; break;}
}
sf[i] = sb;
}
int k = 0;
for(int i = 0; i < se.size(); i++)
{
if(sf[i] == true)
{
se[k] = se[i]; k++;
}
}
se.resize(k);
for(int i = 0; i < re.size(); i++)
{
se.push_back(re[i]);
}
#endif
cout << "done=" << done << " se.size=" << se.size() << " max=" << max << endl;
}//end while loop
timeb = clock();
cout << "Time: " << (timeb - timea)/CLOCKS_PER_SEC << endl;
return 0;
#endif
}//end of main