-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfptree.cpp
220 lines (219 loc) · 5.55 KB
/
fptree.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
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define PI 3.141592653589793
#define all(x) x.begin(),x.end()
#define iOS ios::sync_with_stdio(false)
using namespace std;
vector<int> transactions;
typedef struct node{
int item;
int freq;
vector<node*> next;
}node;
map<int,int> supcnt;
node* root;
int thresh_fq=200;
int no_items=18;
float confiThreshold = 0.5;
vector <int> freqItemSets[20];
vector <int> tp;
node* createNode(int item)
{
node* nd=new node();
nd->item=item;
nd->freq=0;
nd->next.clear();
for(int i=0;i<no_items;i++)
nd->next.pb(NULL);
return nd;
}
int getFirstItem(int item)
{
for(int i=0;i<no_items;i++)
if((1<<i)&item)
return i;
return -1;
}
void addItem(node* cur_node,int item)
{
//cout<<cur_node->item<<endl;
if(cur_node->item==-1)
{
int id=getFirstItem(item);
if(cur_node->next[id]==NULL)
cur_node->next[id]=createNode(id);
addItem(cur_node->next[id],item);
}
else
{
int id=getFirstItem(item);
cur_node->freq++;
item=item^(1<<id);
if(item==0) return;
id=getFirstItem(item);
if(cur_node->next[id]==NULL)
cur_node->next[id]=createNode(id);
addItem(cur_node->next[id],item);
}
}
pair<node*,int> getTempTree(node* cur_node,int lowid)
{
node* nd=createNode(cur_node->item);
if(nd->item>lowid) return mp(nd,0);
if(nd->item==lowid)
{
nd->freq=cur_node->freq;
return mp(nd,1);
}
int red=0;
for(int i=0;i<no_items;i++)
if(cur_node->next[i]!=NULL)
{
pair<node*,int> temp=getTempTree(cur_node->next[i],lowid);
if(temp.ss==0) continue;
red=1;
node* tmp=temp.ff;
if(nd->item!=-1)
nd->freq+=tmp->freq;
nd->next[i]=tmp;
}
return mp(nd,red);
}
void getCounts(node* cur_node,vector<int>& v)
{
if(cur_node->item!=-1)
v[cur_node->item]+=cur_node->freq;
for(int i=0;i<no_items;i++)
if(cur_node->next[i]!=NULL)
getCounts(cur_node->next[i],v);
}
void printItemset(int msk,int fq)
{
int cnt = __builtin_popcount(msk);
freqItemSets[cnt].pb(msk);
supcnt[msk]=fq;
for(int i=0;i<no_items;i++)
if((1<<i)&msk);//printf("%d ",i);
//printf("%d \n",supcnt[msk]);
}
void FPGrowth(node* root,int msk,int lowid)
{
node* tr=getTempTree(root,lowid).ff;
if(lowid==1e8) tr=root;
vector<int> cnts(no_items,0);
getCounts(tr,cnts);
int id=getFirstItem(msk);
if(id==0) return;
if(id==-1) id=no_items;
for(int i=id-1;i>=0;i--)
if(cnts[i]>=thresh_fq && ((1<<i)&msk)==0)
{
int tmsk=msk|(1<<i);
printItemset(tmsk,cnts[i]);
FPGrowth(tr,tmsk,i);
}
}
void traverse(node* nd)
{
printf("%d %d\n",nd->item,nd->freq);
for(int i=0;i<no_items;i++)
if(nd->next[i]!=NULL)
traverse(nd->next[i]);
}
int getithpos(int j,int msk)
{
int cnt=0;
for(int i=0;i<no_items;i++)
if(msk&(1<<i))
{
if(cnt==j) return i;
cnt++;
}
return -1;
}
vector < pair < pair < vector <int> ,vector <int> > , pair <int, pair <int,float> > > > rules; // second pair ff - left sup , right whole sup , rightmost rule confidence
void genAllRules(int k ,int msk){
int lim = 1<<k;
int supWhole = supcnt[msk];
for(int mask = 0 ; mask < lim ; mask++){
vector <int> l;
vector <int> r;
for(int i = 0 ; i < k ; i++){
if(mask & (1<<i)){
r.pb(i);
}
else{
l.pb(i);
}
}
if(l.size()==0 || r.size()==0)continue;
int supCountOfL=0;
for(int i=0;i<l.size();i++){
supCountOfL+=(1<<getithpos(l[i],msk));
}
supCountOfL=supcnt[supCountOfL];
float conf = ( supWhole * 1.0 ) / ( supCountOfL * 1.0 );
if(conf >= confiThreshold){
for(int i = 0 ; i < l.size() ; i++){
printf("%d ",getithpos(l[i],msk));
}
printf(" ==> ");
for(int i = 0 ; i < r.size() ; i++){
printf("%d ",getithpos(r[i],msk));
}
printf(" Conf = %lf\n",conf);
}
}
}
void generateRules(){
for(int i = 2 ; i <= no_items ; i++){
for(int j = 0 ; j < freqItemSets[i].size() ; j++){
genAllRules(i,freqItemSets[i][j]);
}
}
}
void bruteForceChecker()
{
int tot=1<<no_items;
for(int i=0;i<tot;i++)
{
int cnt=0;
for(int j=0;j<transactions.size();j++)
{
if((i&transactions[j])==i) cnt++;
}
if(cnt>=thresh_fq) printItemset(i,cnt);
}
}
int n,x,y;
int main()
{
freopen("data.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d",&n);
scanf("%d",&x);
for(int i=0;i<n;i++)
{
int msk=0;
for(int j=0;j<x;j++)
{
scanf("%d",&y);
y--;
msk+=(1<<y);
}
transactions.pb(msk);
}
root=createNode(-1);
for(int i=0;i<transactions.size();i++){
addItem(root,transactions[i]);
}
FPGrowth(root,0,1e8);
generateRules();
return 0;
}