-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapriori.cpp
254 lines (208 loc) · 6.08 KB
/
apriori.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
#include<bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define pb(x) push_back(x)
#define si set<int>
#define all(x) x.begin(), x.end()
#define LIM 30000
#define MAX 120
#define MIN_SUP_THRES 50
using namespace std;
int MIN_SUP_COUNT_THRES;
vvi database;
si items;
int numberOfTrans, numberOfItem;
vector<vvi>L;
set<vi>ase;
int MAXI = 0;
void printDatabase() {
cout << "DATABASE: " << endl;
for(int i=0; i<database.size(); i++) {
cout << "T" << i << " : " ;
for(auto it : database[i]) cout << it << ' ';
cout << endl;
}
cout << endl;
}
struct info {
int IDS;
int id[LIM];
int mark[LIM];
int root;
// int nxt[LIM][MAX];
vvi nxt;
int numberofCandidates;
info() {
numberofCandidates = 0;
root = 1;
IDS = 1;
nxt.clear();
vi temp;
for(int i=0; i<=MAX; i++) temp.pb(0);
nxt.pb(temp);
nxt.pb(temp);
// memset(nxt, 0, sizeof nxt);
memset(id, 0, sizeof id);
memset(mark, 0, sizeof mark);
}
clear() {
numberofCandidates = 0;
root = 1;
IDS = 1;
nxt.clear();
vi temp;
for(int i=0; i<=MAX; i++) temp.pb(0);
nxt.pb(temp);
nxt.pb(temp);
// memset(nxt, 0, sizeof nxt);
memset(id, 0, sizeof id);
memset(mark, 0, sizeof mark);
}
void add(vi candidate, int indx) {
int cur = root;
for(auto item : candidate) {
// assert(IDS < LIM-5);
// if(nxt.size() <= cur)
if(!nxt[cur][item]) {
nxt[cur][item] = ++IDS;
vi temp;
for(int i=0; i<=MAX; i++) temp.pb(0);
nxt.pb(temp);
}
cur = nxt[cur][item];
}
numberofCandidates++;
mark[cur] = 1;
id[cur] = indx;
// cout << "CURRENT MAX " << IDS << endl;
MAXI = max(MAXI, IDS);
}
vi datasetTest(vvi &database) {
vi occurance;
for(int i=0; i<numberofCandidates; i++) occurance.pb(0);
int in = 0;
for(auto line : database) {
dfs(root, 0, line, occurance);
}
return occurance;
}
void dfs(int u, int in, vi &line, vi &occurance, int con = 0) {
if(mark[u] && !con) occurance[ id[u] ]++;
if(in == line.size()) return;
int vval = line[in];
if(nxt[u][vval]) dfs(nxt[u][vval], in+1, line, occurance, 0);
dfs(u, in+1, line, occurance, 1);
}
};
info trie;
info makeTrie(vvi candidates) {
trie.clear();
for(int i=0; i<candidates.size(); i++) {
vi can = candidates[i];
trie.add(can, i);
}
return trie;
}
bool asecheck(vi temp) {
for(int i=0; i<temp.size(); i++) {
vi cur;
for(int j=0; j<temp.size(); j++) if(i != j) cur.pb(temp[j]);
if(ase.find(cur) == ase.end()) return false;
}
return true;
}
vvi prune(vvi candidates) {
vvi ans;
for(int i=0; i<candidates.size(); i++) {
if(asecheck(candidates[i])) ans.pb(candidates[i]);
}
return ans;
}
vvi join(vvi last) {
vvi ans;
for(int i=0; i<last.size(); i++) {
sort(last[i].begin(), last[i].end());
}
for(int i=0; i<last.size(); i++) {
for(int j=i+1; j<last.size(); j++) {
assert(last[i].size() == last[j].size());
bool ok = true;
for(int k=0; k<last[i].size()-1; k++) {
if(last[i][k] != last[j][k]) ok = false;
}
if(ok) {
vi temp = last[i];
temp.pb(last[j].back());
// if(asecheck(temp))
ans.pb(temp);
}
}
}
return ans;
}
int main() {
char fl[110] = "mushroom.txt";
cout << "Please enter filename of the dataset: ";
scanf("%s", fl);
freopen(fl, "r", stdin);
// freopen("out.txt", "w", stdout);
// cout << "PAISE " << endl;
string line;
while(getline(cin, line)) {
vi data;
int item;
stringstream ss;
ss << line;
while(ss >> item) {
if(item != -1) {
data.pb(item);
items.insert(item);
}
}
sort(all(data));
database.pb(data);
}
// cout << "BUG" << endl;
// printDatabase();
numberOfItem = items.size();
numberOfTrans = database.size();
MIN_SUP_COUNT_THRES = ceil(numberOfTrans * MIN_SUP_THRES / 100.0);
// scanf("%d", &MIN_SUP_);
// MIN_SUP_COUNT_THRES = ceil(numberOfTrans * MIN_SUP_THRES / 100.0);
cout << "Number of Trans: " << numberOfTrans << " Number Of Items: " << items.size() << " Minimum Support Count: " << MIN_SUP_COUNT_THRES << endl;
int ans = 0;
for(int level = 0; ;level++) {
vvi candidates;
vvi pruned;
if(level == 0) {
for(auto it : items) {
vi temp;
temp.pb(it);
candidates.pb(temp);
pruned = candidates;
}
}
else {
candidates = join(L[level-1]);
pruned = prune(candidates);
}
// candidates = pruned;
// pruned = candidates;
trie = makeTrie(pruned);
vi occurance = trie.datasetTest(database);
assert(occurance.size() == pruned.size());
vvi li;
for(int i=0; i<pruned.size(); i++) {
if(occurance[i] >= MIN_SUP_COUNT_THRES) li.pb(pruned[i]);
}
L.pb(li);
ase.clear();
if(li.size() == 0) break;
for(auto it : li) ase.insert(it);
cout << "Candidates: " << candidates.size() << " Pruned: " << pruned.size() << " " << "L" << level+1 << ": " << li.size() << endl;
ans += li.size();
// cout << li.size() << endl;
}
cout << "TOTAL: " << ans << endl;
// cout << "SD " << MAXI << endl;
}