-
Notifications
You must be signed in to change notification settings - Fork 8
/
DIC.cpp
497 lines (463 loc) · 12.6 KB
/
DIC.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
Implementation of Dynamic Itemset Counting(DIC) Algorithm
**/
/**
Author: Sudheesh Singanamalla
B.Tech CSE(2012-16), National Institute of Technology, Warangal
**/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<climits>
#include<vector>
#include<map>
#include<iterator>
#include<set>
#include<bitset>
#include<ctime>
#include<iomanip>
#include<fstream>
#include<string>
#include<stdlib.h>
//#include "itoa.h"
using namespace std;
struct node
{
int data;
node* next;
};
struct tid_set
{
vector<int> tid;
};
struct itemset
{
string id;
node* i_set;
int counter;
int size_set;
int start;
};
struct cmp_str
{
bool operator()(char const *a, char const *b)
{
return std::strcmp(a, b) < 0;
}
};
vector< tid_set *> temp_occurences(1), final_scan(1);
vector<itemset*> SS(1), SC(1), DS(1), DC(1), temp_DC(1), temp_DS(1);
vector<itemset*> :: iterator it;
int size_SS,size_SC, size_DS, size_DC,size_temp_DS, size_temp_DC;
int trans[1000][1000];
int t;
int items;
int min_sup = 10;// minimum support in percent.
int min_count;
int m = 3;
int start, stop;
map< string, bool> SS_DS, taken;
void reverse(char s[])
{
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoa(int n, char s[], int base)
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void add_ll(node*& h,int k)
{
if(h == NULL)
{
h = new node();
h->data = k;
h->next = NULL;
}
else
{
node *p,*l = h;
while(l->next != NULL)
{
l = l->next;
}
p = new node();
p->data = k;
p->next = NULL;
l->next = p;
}
}
void print_ll(node* h)
{
while(h != NULL)
{
cout<<h->data<<" ";
h = h->next;
}
cout<<",";
}
string itemset_to_string(node* h)
{
char temp[1000000], integ[10000];
temp[0] = '\0';
while(h != NULL)
{
itoa(h->data, integ, 10); //converts itemno. to a char array..
strcat(temp, integ);
strcat(temp, " ");
h=h->next;
}
string s(temp);
return s;
}
vector<int> temp_vec,temp1;
void tid_intersection(node* l, int k)
{
temp_vec.clear();
vector<int> present_temp(1),pres(1);
pres.resize(t+1);
present_temp.resize(t+1);
//pres.resize(items);
for(int i=1; i<=t; i++)
{
present_temp[i] = 1;
}
vector<int> :: iterator it1,it2;
//int i=1;
for(int i=1; i<=k; i++)
{
temp1.clear();
temp1 = (temp_occurences[l->data])->tid;
for(int i=1; i<=t; i++)
{
pres[i] = 0;
}
it1 = temp1.begin();
while(it1!=temp1.end())
{
pres[*it1] = 1;
it1++;
}
for(int j=1; j<=t; j++)
{
present_temp[j] = present_temp[j]&pres[j];
}
l = l->next;
}
//cout<<"( ";
for(int j=1; j<=t; j++)
{
if(present_temp[j])
{
cout<<j<<" , ";
temp_vec.push_back(j);
}
}
//cout<<")\n";
}
bool check_subsets(node* q, int k)
{
int flag = 0;
int i=1;
node *t = NULL,*h = q;
while(i <= k)
{
t = NULL;
h = q;
for(int j=1; j<i; j++)
{
add_ll(t,h->data);
h = h->next;
}
h = h->next;
for(int j=i+1; j<=k; j++)
{
add_ll(t,h->data);
h = h->next;
}//we have k-1 subset here..
string c = itemset_to_string(t);
if(SS_DS[c] == false)//if itemset is not solid or dashed square
{
print_ll(q);
cout<<" rejected because "<<c<<"does not belong to squared-itemsets\n";
return false;
}
i++;
}//cout<<"hello2";
return true;//all subsets are solid/dashed squares..
}
vector<int> superset(1),temp2(1),temp3(1);
vector<bool> p(1);
void construct_check_superset(node* l, int k)
{
cout<<"checking supersets of ";
print_ll(l);
cout<<"\n";
temp2.resize(items+1);
p.clear();
p.resize(items+1);
temp2[0] = 0;
int temp_size=0;
while(temp_size != k)
{
temp2[++temp_size] = l->data;
p[l->data] = true;
l = l->next;
}
for(int i =1; i<=items; i++)
{
if(p[i] == false)
{
temp3.clear();
temp3 = temp2;
temp3.resize(items+1);
temp3[++temp_size] = i;
sort(temp3.begin(), temp3.begin() + temp_size + 1);
node *q = NULL;
for(int j=1; j<=temp_size; j++)
{
add_ll(q, temp3[j]);
}
if(check_subsets(q, k+1)==true && taken[itemset_to_string(q)]!=true)
//if all immediate subsets are solid/dashed squares
{
itemset *temp = new itemset();
temp->i_set = q;
temp->id = itemset_to_string(q);
temp->counter = 0;
temp->size_set = k+1;
temp->start = -5;//dummy value..
temp_DC[++size_temp_DC] = temp;
taken[itemset_to_string(q)] = true;
print_ll(q);
//the superset has been added to dashed circle..
}
temp_size--;
}
}
}
int main()
{
cout<<"----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n";
ifstream fin("trans6.txt");
ofstream fout;
fin>>items>>t;
int i=1;
temp_occurences.resize(items+1);
while(i<=t)
{
for(int j=1; j<=items; j++)
{
fin>>trans[i][j];
}
i++;
}
//min_count = (int)((ceil)(((double)(min_sup * t))/100));
min_count = 2;
DS.resize(100000);
SS.resize(100000);
DC.resize(100000);
SC.resize(100000);
temp_DS.resize(100000);//used for transferring itemsets from DC to DS..
temp_DC.resize(100000);//used for transferring new itemsets to DC..
size_DS = 0;
size_SS = 0;
size_DC = 0;
size_SC = 0;
temp_occurences.resize(items+1);
for(int i=1; i<=items; i++)
{
temp_occurences[i] = new tid_set();
itemset *temp = new itemset();
node *t = NULL;
add_ll(t ,i);
temp->i_set = t;
cout<<"adding \t";
print_ll(temp->i_set);
cout<<" to DC\n";
temp->id = itemset_to_string(t);
temp->counter = 0;
temp->size_set = 1;
temp->start = 1;
DC[++size_DC] = temp;
}//all 1-itemsets have been marked as dashed-circles..
stop = 0;
int read = 1;
//start reading the m transaction at a time
while(size_DS > 0 || size_DC > 0)
{
cout<<"\nreadno."<<read<<endl;
read++;
start = (stop+1);
if(start > t)
start = 1;
stop = (start + m - 1);
if(stop > t)
stop = t;
cout<<"\nstart,stop = "<<start<<" "<<stop<<endl;
size_temp_DC = 0;
size_temp_DS = 0;
for(int i=1; i<=items; i++)
{
(temp_occurences[i]->tid).clear();
}
for(int i=start; i<=stop; i++)
{
for(int j=1; j<=items; j++)
{
if(trans[i][j] == 1)
{
(temp_occurences[j]->tid).push_back(i);
}
}
}//constructed a tid-list for all items in the m transactions.
cout<<"\nDC\n";
for(int i=1; i<=size_DC; i++) //count the occurences of dashed-circles
{
tid_intersection(DC[i]->i_set, DC[i]->size_set);
DC[i]->counter += temp_vec.size();
if(DC[i]->counter >= min_count)//if the dashed temset is frequent
{
//remove the itemset from DC and add it to temp_DS..
print_ll( DC[i]->i_set);
cout<<" has been removed from DC to DS\n";
temp_DS[++size_temp_DS] = DC[i];//itemset added to temp_DS..
SS_DS[DC[i]->id] = true;
int w=i;
while(w<size_DC)//shift the itemsets by one to the left
{
DC[w] = DC[w+1];
w++;
}
i--;
size_DC -= 1;//removed the itemset from DC
}
}
cout<<"\nDS\n";
for(int i=1; i<=size_DS; i++) //count the occurences of dashed-squares
{
tid_intersection(DS[i]->i_set, DS[i]->size_set);
DS[i]->counter += temp_vec.size();
}
//transferring those itemsets which have covered all transactions, from DC to SC..
for(int i=1; i<=size_DC; i++)
{
if(stop == ((DC[i]->start) - 1) || (stop == t && DC[i]->start == 1))//the itemset has been counted through all transactions..
{
itemset *temp = new itemset();
temp->i_set = DC[i]->i_set;
temp->id = DC[i]->id;
temp->counter = 0;
temp->size_set = DC[i]->size_set;
//temp->start = 1;
SC[++size_SC] = temp;//the itemset has been added to SC..
print_ll(temp->i_set);
cout<<" has been removed from DC to SC\n";
int w=i;
while(w<size_DC)//shift the itemsets by one to the left
{
DC[w] = DC[w+1];
w++;
}
i--;
size_DC -= 1;//removed the itemset from DC
}
}
//checking supersets of those itemsets which are in temp_DS..
for(int i=1; i<=size_temp_DS; i++)
{
construct_check_superset(temp_DS[i]->i_set, temp_DS[i]->size_set);//check all its supersets and add them to temp_DC if necessary..
}
//transferring new itemsets from temp_DS to DS
for(int i=1; i<=size_temp_DS; i++)
{
DS[++size_DS] = temp_DS[i];
SS_DS[temp_DS[i]->id] = true;
}
//transferring those itemsets which have covered all transactions, from DS to SS..
for(int i=1; i<=size_DS; i++)
{
if(stop == ((DS[i]->start) - 1) || (stop == t && DS[i]->start == 1))//the itemset has been counted through all transactions..
{
itemset *temp = new itemset();
temp->i_set = DS[i]->i_set;
temp->id = DS[i]->id;
temp->counter = DS[i]->counter;
temp->size_set = DS[i]->size_set;
//temp->start = 1;
SS[++size_SS] = temp;//the itemset has been added to SS..
SS_DS[temp->id] = true;
int w=i;
while(w<size_DS)//shift the itemsets by one to the left
{
DS[w] = DS[w+1];
w++;
}
i--;
size_DS -= 1;//removed the itemset from DS
}
}
//transferring new itemsets from temp_DC to DC
for(int i=1; i<=size_temp_DC; i++)
{
DC[++size_DC] = temp_DC[i];
DC[size_DC]->start = stop+1;
if(stop+1 > t)
DC[size_DC]->start = 1;
//the new itemset will be counted from the next reading of transaction, hence the new start value..
}
cout<<"itemsets in DC are:\n";
for(int i=1; i<=size_DC; i++)
{
print_ll(DC[i]->i_set);
}
cout<<"\nitemsets in DS are:\n";
for(int i=1; i<=size_DS; i++)
{
print_ll(DS[i]->i_set);
}
cout<<"\n";
}
for(int j=1;j<=items;j++)
{
temp_occurences[j]->tid.clear();
}
//preparing the final temp_occurences..
for(int i=1;i<=t;i++)
{
for(int j=1;j<=items;j++)
{
if(trans[i][j])
temp_occurences[j]->tid.push_back(i);
}
}
//the frequent itemsets are..
cout<<"The following are the frequent itemsets:\n";
for(int i=1; i<=size_SS; i++)
{
cout<<SS[i]->id;
cout<<" is present in transactions : ";
tid_intersection(SS[i]->i_set, SS[i]->size_set);
cout<<"\n";
}
return 0;
}