-
Notifications
You must be signed in to change notification settings - Fork 1
/
FPTree-V2.cpp
545 lines (504 loc) · 12.1 KB
/
FPTree-V2.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <list>
using namespace std;
const int MAX_LENTH = 1000;
const int MAX_ITEMS = 5 + 1e7;
const char* SPLITCHAR = " ";
/*
Utility function
*/
list<string> splitStr(string str, string TEMP)
{
string::size_type pos;
list<string> result;
//去掉'\n'字符
int size = str.size() - 1;
for(int i = 0;i < size;i ++)
{
pos = str.find(TEMP, i);
if(pos < size)
{
string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + TEMP.size() - 1;
}
else {
string s = str.substr(i, size - i);
result.push_back(s);
break;
}
}
return result;
}
list< pair<int,list<string> > > getTransRecordsFormFile(const char* fileName)
{
list< pair<int,list<string> > > res;
FILE* f = fopen(fileName,"r");
if(f == NULL) {
cout << "load input file failed!" << endl;
exit(0);
}
else {
char *buffer;
while(fgets(buffer, MAX_LENTH, f) != NULL) {
list<string> v = splitStr(buffer, SPLITCHAR);
res.push_back(make_pair(1,v));
}
}
fclose(f);
return res;
}
void printRecords(list< pair<int,list<string> > > R)
{
list< pair<int,list<string> > >::iterator itI;
list<string>:: iterator itJ;
for(itI = R.begin();itI != R.end();itI ++)
{
cout << "(" << (*itI).first << ") ";
for(itJ = ((*itI).second).begin();itJ != ((*itI).second).end();itJ ++) {
cout << (*itJ) <<" ";
}
cout<<endl;
}
}
/*
TreeNode class
*/
class TreeNode
{
public:
TreeNode()
{
parent = NULL;
nextHomonym = NULL;
children = NULL;
count = 0;
}
TreeNode(string name)
{
parent = NULL;
nextHomonym = NULL;
children = NULL;
count = 0;
this->name = name;
}
void setName(string name)
{
this->name = name;
}
void setCount(int count)
{
this->count = count;
}
void setParent(TreeNode* parent)
{
this->parent = parent;
}
void setChildren(list<TreeNode*> *children)
{
this->children = children;
}
void addChild(TreeNode* child)
{
if(children == NULL) {
children = new list<TreeNode*>;
}
(*children).push_back(child);
}
void setNextHomonym(TreeNode* nextHomonym)
{
this->nextHomonym = nextHomonym;
}
void addCountBy(int n)
{
this->count += n;
}
int getCount()
{
return this->count;
}
string getName()
{
return this->name;
}
TreeNode* getParent()
{
return this->parent;
}
TreeNode* getNextHomonym()
{
return this->nextHomonym;
}
list<TreeNode*>* getChildren()
{
return this->children;
}
TreeNode* findChild(string name)
{
if(children == NULL) return NULL;
TreeNode* res = NULL;
list<TreeNode*>::iterator it;
for(it = (*children).begin();it != (*children).end();it ++) {
if( (*it)->name == name) {
res = (*it);
break;
}
}
return res;
}
private:
string name;
int count;
TreeNode* parent;
TreeNode* nextHomonym;
list<TreeNode*> *children;
};
/*
sort function
*/
bool cmp(TreeNode* a, TreeNode* b)
{
return a->getCount() > b->getCount();
}
bool cmpRecord(pair<string,int> a, pair<string,int> b)
{
return a.second < b.second;
}
bool cmpMode(pair<int, list<string> > a, pair<int, list<string> > b)
{
if(a.second.size() == b.second.size()) {
return a.first > b.first;
}
return a.second.size() < b.second.size();
}
bool cmpKMode(pair<int, list<string> > a, pair<int, list<string> > b)
{
if(a.first == b.first) {
return a.second.size() > b.second.size();
}
return a.first > b.first;
}
/*
FPTree class
*/
class FPTree
{
public:
int Vcnt;
pair<int, list<string> > resMode[MAX_ITEMS];
FPTree()
{
//default min support
minSupport = 3;
Vcnt = 0;
modeSize = 0;
DEBUG = 0;
}
void setMinSupport(int newMinSupport)
{
minSupport = newMinSupport;
}
void setDebug(bool DEBUG)
{
this->DEBUG = DEBUG;
}
int getMinSupport()
{
return minSupport;
}
void clearMap()
{
mapStrInt.clear();
}
void setMap(string str,int cnt)
{
mapStrInt[str] = cnt;
}
void addMapBy(string str, int inc)
{
mapStrInt[str] += inc;
}
void FPGrowth(list< pair<int,list<string> > > records, list<string> pattern)
{
if(records.empty()) return ;
Vcnt ++;
list<TreeNode*> HeaderTable = buildHeaderTable(records);
TreeNode* root = buildFPTree(records, HeaderTable);
if(DEBUG) {
cout << "--------------------------" << endl;
cout<< "Now the depth is: " << Vcnt << endl;
cout<< "Now the records is: " << endl;
printRecords(records);
cout<< "Now the pattern is: " << endl;
printPattern(pattern);
cout<< "Now the F1 is: " << endl;
printHeaderTable(HeaderTable);
cout<< "Now the FPTree is: " << endl;
printTree(root);
}
if(root->getChildren() == NULL) {
return ;
}
if(DEBUG) cout << "Now the result is : " << endl;
list<TreeNode*>::iterator _it;
for(_it = HeaderTable.begin();_it != HeaderTable.end();_it ++) {
TreeNode* node = (*_it);
list<string> items = pattern;
items.push_front(node->getName());
if(DEBUG) {
cout << node->getCount() << " : ";
for(list<string>::iterator I = items.begin();I != items.end();I ++) {
cout << (*I) << " ";
}
cout << endl;
}
resMode[modeSize ++] = make_pair(node->getCount(), items);
}
list<TreeNode*>::iterator it;
for(it = HeaderTable.begin();it != HeaderTable.end();it ++) {
TreeNode* header = (*it);
list<string> newPattern = pattern;
list< pair<int,list<string> > > newRecords;
//构造新的模式串
newPattern.push_front(header->getName());
TreeNode* nextNode = header->getNextHomonym();
while(nextNode != NULL) {
list<string> record;
int cnt = nextNode->getCount();
TreeNode* preNode = nextNode;
while( ((preNode = preNode->getParent())->getName()) != "") {
record.push_back(preNode->getName());
}
if(!record.empty()) {
newRecords.push_back(make_pair(cnt,record));
}
record.clear();
nextNode = nextNode->getNextHomonym();
}
delete header;
FPGrowth(newRecords, newPattern);
}
HeaderTable.clear();
}
//构造频繁1项集
list<TreeNode*> buildHeaderTable(list< pair<int,list<string> > > records)
{
list<TreeNode*> F1;
map<string,TreeNode*> mpTree;
mpTree.clear();
F1.clear();
clearMap();
list< pair<int,list<string> > >::iterator itI;
list<string>:: iterator itJ;
for(itI = records.begin();itI != records.end();itI ++) {
int cnt = (*itI).first ;
for(itJ = ((*itI).second).begin();itJ != ((*itI).second).end();itJ ++) {
string item = (*itJ);
if(mpTree.find(item) == mpTree.end()) {
TreeNode* node = new TreeNode(item);
node->setCount(cnt);
mpTree[item] = node;
setMap(item, cnt);
}
else {
(mpTree[item])->addCountBy(cnt);
addMapBy(item, cnt);
}
}
}
for(map<string,TreeNode*>:: iterator it = mpTree.begin();it != mpTree.end();it ++) {
if(( it->second )->getCount() >= minSupport) {
F1.push_back( (it->second) );
}
}
F1 = sortF1(F1);
return F1;
}
//构造对应的FP-Tree
TreeNode* buildFPTree(list< pair<int,list<string> > > records, list<TreeNode*> F1)
{
TreeNode* root = new TreeNode();
list< pair<int,list<string> > >::iterator itI;
for(itI = records.begin();itI != records.end();itI ++) {
int cnt = (*itI).first;
list<string> sortedRecord = sortByMap((*itI).second);
TreeNode* parent = root;
TreeNode* node;
bool ok = true;//标记是否有新的分支
while(!sortedRecord.empty()) {
string item = sortedRecord.back();
if(ok && (node = (parent->findChild(item))) != NULL) {
node->addCountBy(cnt);
parent = node;
}
else {
ok = false;
TreeNode* newNode = new TreeNode(item);
newNode->setCount(cnt);
newNode->setParent(parent);
parent->addChild(newNode);
parent = newNode;
//链接同名节点
list<TreeNode*>::iterator it;
for(it = F1.begin();it != F1.end();it ++) {
TreeNode* f = (*it);
if(f->getName() == item) {
while( f->getNextHomonym() != NULL ) {
f = f->getNextHomonym();
}
f->setNextHomonym(newNode);
break;
}
}
}
sortedRecord.pop_back();
}
sortedRecord.clear();
}
return root;
}
list<string> sortByMap(list<string> record)
{
vector< pair<string,int> > tmp;
list<string> sortedRecord;
list<string>::iterator it;
for(it = record.begin();it != record.end();it ++) {
string now = (*it);
if(mapStrInt[now] >= minSupport) {
tmp.push_back(make_pair(now, mapStrInt[now]));
}
}
sort(tmp.begin(), tmp.end(), cmpRecord);
for(int i = 0;i < tmp.size();i ++) {
sortedRecord.push_back(tmp[i].first);
}
tmp.clear();
return sortedRecord;
}
list<TreeNode*> sortF1(list<TreeNode*> F1)
{
vector<TreeNode*> tmp;
list<string> sortedRecord;
tmp.clear();
list<TreeNode*>::iterator it;
for(it = F1.begin();it != F1.end();it ++) {
tmp.push_back((*it));
}
F1.clear();
sort(tmp.begin(), tmp.end(), cmp);
for(int i = 0;i < tmp.size();i ++) {
F1.push_back(tmp[i]);
}
tmp.clear();
return F1;
}
// function for looking process
void printHeaderTable(list<TreeNode*> HeaderTable)
{
list<TreeNode*>::iterator it;
for(it = HeaderTable.begin();it != HeaderTable.end();it ++) {
cout << (*it)->getName() << " : ";
cout << (*it)->getCount() << endl;
}
}
void printPattern(list<string> pattern)
{
list<string>::iterator it;
for(it = pattern.begin();it != pattern.end();it ++) {
cout << (*it) << " ";
}
cout << endl;
}
void printTree(TreeNode* rt)
{
if( rt->getChildren() == NULL) return ;
list<TreeNode*>::iterator it;
list<TreeNode*> *child = rt->getChildren();
for(it = (*child).begin();it != (*child).end();it ++) {
cout << (rt->getName()) << "(" << (rt->getCount()) << ") -> " << (*it)->getName() <<"(" << (*it)->getCount() << ")" << endl;
printTree(*it);
}
}
void printAllSortedMode()
{
sort(resMode, resMode + modeSize, cmpMode);
for(int i = 0;i < modeSize;i ++) {
int sz = resMode[i].second.size();
if(i == 0 || sz != resMode[i-1].second.size()) {
cout << "The " << sz << " frequent items set :" <<endl;
}
list<string>::iterator it;
cout << "{";
for(it = (resMode[i].second).begin();it != (resMode[i].second).end();it ++) {
cout << (*it) << " ";
}
cout << "} [" << (resMode[i].first) << "]" << "\n";
}
cout << "--------------------------" << "\n";
}
void printTopKMode(int k)
{
sort(resMode, resMode + modeSize, cmpKMode);
k = min(k, modeSize);
cout << "The top " << k << " frequent items set is:" << endl;
for(int i = 0;i < k;i ++) {
list<string>::iterator it;
cout << "{";
for(it = (resMode[i].second).begin();it != (resMode[i].second).end();it ++) {
cout << (*it) << " ";
}
cout << "} [" << (resMode[i].first) << "]" << "\n";
}
}
private:
int minSupport;
int modeSize;
//select DEBUG mode
bool DEBUG;
//用来记录字符串出现的次数
map<string,int> mapStrInt;
};
int main()
{
cout << "Input fileName:" << endl;
char filename[MAX_LENTH];
cin >> filename;
cout << endl;
list< pair<int,list<string> > > R = getTransRecordsFormFile(filename);
list<string> v;
int n, mode, DEBUG;
cout << "Input the min support:" << endl;
cin >> n;
cout << "Input the mode of processing:" << endl;
cout << "0: Results will be print in screen" << endl;
cout << "1: Results will be print in file" << endl;
cin >> mode;
cout << "Need DEBUG(will print all information)?" << endl;
cin >> DEBUG;
if(mode == 1) freopen("out.txt", "w", stdout);
FPTree* fptree = new FPTree();
fptree->setDebug(DEBUG != 0);
fptree->setMinSupport(n);
cout<<"The minSupport is: "<< fptree->getMinSupport() <<endl;
clock_t start=clock();
fptree->FPGrowth(R, v);
clock_t finish=clock();
cout << "The depth is : " << fptree->Vcnt <<endl;
cout << "(FP-Tree Growth)Time: " << (double)(finish - start) / CLOCKS_PER_SEC << "s." << endl;
cout << "--------------------------" << endl;
//print result after sorting all data
start = clock();
fptree->printAllSortedMode();
finish = clock();
cout << "(print All frequent item set)Time: " << (double)(finish - start) / CLOCKS_PER_SEC << "s." << endl;
cout << "--------------------------" << endl;
fptree->printTopKMode(10);
exit(0);
}