-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·246 lines (224 loc) · 8.02 KB
/
main.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
#include <iostream>
#include <algorithm>
#include <chrono>
#include <fstream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <omp.h>
using namespace std;
class Transactions {
public:
vector<vector<int>> transactions;
map<int, int> items; // support count per items
map<vector<int>, int> getC1();
int getRowSupport(vector<int> row);
map<vector<int>, int> generateL(map<vector<int>, int> C, int minSupport);
map<vector<int>, int> generateC(map<vector<int>, int> prevC);
bool subsetOfTransaction(vector<int> row, vector<int> transaction);
private:
map<vector<int>, int> joinPhase(map<vector<int>, int> L);
map<vector<int>, int> prunePhase(map<vector<int>, int> C, map<vector<int>, int> L);
};
map<vector<int>, int> Transactions::getC1() {
map<vector<int>, int> elements;
for (auto&item:items) {
vector<int> z = {item.first};
elements[z] = item.second;
}
return elements;
}
int Transactions::getRowSupport(vector<int> row) {
int support = 0;
for (auto item : transactions) {
if (subsetOfTransaction(row, item)) {
support++;
}
}
return support;
}
bool Transactions::subsetOfTransaction(vector<int> row, vector<int> transaction) {
int j = 0;
for (int i : row) {
while(j < transaction.size() && transaction[j] != i) {
j++;
}
if (j >= transaction.size()) {
return false;
}
}
return true;
}
/**
* Find the frequent itemset from a candidate set C.
* For every candidate item i in C, this function counts the number of transaction that contains i (support).
* If the support of i is above the minimum support, then this item is added to the frequent itemset.
* @param C - the candidate set
* @param minSupport - the min support
* @return the frequent itemset.
*/
map<vector<int>, int> Transactions::generateL(map<vector<int>, int> C, int minSupport) {
map<vector<int>, int> L;
#pragma omp parallel
{
map<vector<int>, int> localL;
#pragma omp for
for (int i = 0; i< C.size(); i++) {
auto it = C.begin();
advance(it, i);
// for every row in C, check if the support is > minSupport
int supp = getRowSupport(it->first);
if (supp>minSupport) {
localL[it->first] = supp;
}
}
#pragma omp critical
{
for (auto item:localL) {
L[item.first] = item.second;
}
}
}
return L;
}
/**
* Generate the C set (Candidate set) from the previous frequent itemset.
* This is done in 2 phases:
* 1. Join Phase: merge together 2 item from L. This will generate, starting from items with length N, items with length N+1.
* 2. Prune Phase: remove from the set generated before items that does not have all k-subset to be frequent
* @param L - the frequent itemset with item length N-1.
* @return the Candidate set with item length N.
*/
map<vector<int>, int> Transactions::generateC(map<vector<int>, int> L) {
map<vector<int>, int> C;
return prunePhase(joinPhase(L), L);
}
map<vector<int>, int> Transactions::joinPhase(map<vector<int>, int> L) {
map<vector<int>, int> joinedC;
#pragma omp parallel
{
map<vector<int>, int> localC;
#pragma omp for
for (int i = 0; i < L.size(); i++) {
auto it = L.begin();
advance(it, i);
vector<int> row = it->first;
auto internalIt = it;
internalIt++;
while( internalIt != L.end()) {
for (auto item:internalIt->first) {
if (find(row.begin(), row.end(), item) == row.end()) { // not found
vector<int> joinItems = row;
joinItems.push_back(item);
sort(joinItems.begin(), joinItems.end());
localC[joinItems] ++;
}
}
internalIt++;
}
}
#pragma omp critical
{
for (auto item:localC) {
joinedC[item.first] = item.second;
}
}
}
return joinedC;
}
map<vector<int>, int> Transactions::prunePhase(map<vector<int>, int> C, map<vector<int>, int> L) {
map<vector<int>, int> prunedC;
#pragma omp parallel
{
map<vector<int>, int> localC;
#pragma omp for
for (int i = 0; i < C.size(); i++) {
auto row = C.begin();
advance(row, i);
int i;
for(i=0; i<row->first.size();i++){
vector<int> rowSubset = row->first;
rowSubset.erase(rowSubset.begin()+i);
if (!L[rowSubset]) {
break;
}
}
if(i==row->first.size()){
localC[row->first]++;
}
}
#pragma omp critical
{
for (auto item:localC) {
prunedC[item.first]++;
}
}
}
return prunedC;
}
void parseFile(string filePath, Transactions* transactions) {
string rawTransaction;
ifstream transactionFile(filePath);
if (transactionFile.is_open())
{
while ( getline (transactionFile,rawTransaction) )
{
std::stringstream ss(rawTransaction);
int item;
vector<int> transaction;
while (ss >> item) {
transactions->items[item] += 1;
transaction.push_back(item);
if (ss.peek() == ' ')
ss.ignore();
}
sort(transaction.begin(), transaction.end());
transactions->transactions.push_back(transaction);
}
}
transactionFile.close();
}
void apriori(Transactions *transactions, int minSupport) {
auto begin = std::chrono::high_resolution_clock::now();
map<vector<int>, int> C = transactions->getC1();
int step = 1;
while(!C.empty()) {
cout << "STEP " << step << endl;
auto begin = std::chrono::high_resolution_clock::now();
map<vector<int>, int> L = transactions->generateL(C, minSupport);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
cout << "generate L total time: " << elapsed.count() << "ms." << endl;
cout << "# Frequent itemset of size " << step << ": " << L.size() << endl;
auto beginC = std::chrono::high_resolution_clock::now();
C = transactions->generateC(L);
end = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
cout << "generate C total time: " << elapsed.count() << "ms." << endl;
step++;
end = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
cout << "apriori step " << step << " total time: " << elapsed.count() << "ms." << endl << endl;
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
cout << "apriori parallel total time: " << elapsed.count() << "ms." << endl;
}
int main(int argc, char **argv) {
if (argc < 3) {
cout << "Usage: {executableName} {filePath} {minSupport}" << endl;
exit(1);
}
Transactions transactions; // every transaction is a set of int (items)
cout << "Parse file " << argv[1] << "..." << endl;
parseFile(argv[1], &transactions);
cout << "Finding frequent itemset with min support of " << argv[2] << endl;
apriori(&transactions, stoi(argv[2]));
cout << "Done!" << endl;
// we parse the file in Transaction (every row is a transaction)
// a transaction has: ID, list of integers
// itemset: collection of one or more items
// goal: find itemset whose support (fraction of transactions that contain an itemset) is greater than or eqaul to a minsup threshold (s(I)>=minsup).
return 0;
}