-
Notifications
You must be signed in to change notification settings - Fork 1
/
all-maximal-sets-lexicographic.cc
416 lines (385 loc) · 13.8 KB
/
all-maximal-sets-lexicographic.cc
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
// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ---
// An algorithm for finding all maximal sets based on the
// lexicographic property.
// ---
// Author: Roberto Bayardo
#include "all-maximal-sets-lexicographic.h"
#include <assert.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>
#include "data-source-iterator.h"
#include "set-properties.h"
namespace google_extremal_sets {
namespace {
// Perform a binary search to find the first non-NULL candidate in the
// range such that comp(current_item, candidate[depth]) no longer holds.
template<class Compare>
AllMaximalSetsLexicographic::CandidateList::iterator find_new_it(
AllMaximalSetsLexicographic::CandidateList::iterator first,
AllMaximalSetsLexicographic::CandidateList::iterator last,
uint32_t current_item,
unsigned int depth,
Compare comp) {
while (first != last && !*first)
++first;
int len = last - first;
int half;
AllMaximalSetsLexicographic::CandidateList::iterator current;
while (len > 0) {
half = len >> 1;
current = first + half;
while (current < last && !*current)
++current;
if (current == last) {
len = half;
} else if (comp(current_item, (**current)[depth])) {
// Not far enough along yet!
first += half + 1;
len = len - half - 1;
while (first < last && !*first) {
++first;
--len;
}
if (first == last)
return last;
} else {
// We may be too far along.
len = half;
}
}
assert(*first);
return first;
}
} // namespace
bool AllMaximalSetsLexicographic::FindAllMaximalSets(DataSourceIterator* data, uint32_t) {
Init();
// Vars set by the data source iterator.
int result;
uint32_t set_id;
ItemSet current_set;
// This outer loop supports multiple passes over the data in the
// case where the dataset exceeds the bound on max_items_in_ram_. As
// long as resume_offset == 0, we will continue retaining itemsets
// in RAM.
off_t start_offset = 0;
off_t resume_offset = 0;
do { // while (resume_offset != 0)
if (!PrepareForDataScan(data, resume_offset))
return false; // IO error
start_offset = resume_offset;
if (!ReadNextChunk(data, &resume_offset))
return false; // IO error
DeleteTriviallySubsumedCandidates();
BuildIndex();
std::cerr << "; Potential maximal sets: " << candidates_.size() << '\n'
<< "; Beginning subsumption checking scan." << std::endl;
for (unsigned int i = 0; i < candidates_.size() - 1; ++i) {
if (candidates_[i]) // check to make sure not already deleted.
DeleteSubsumedCandidates(i);
}
if (start_offset != 0) {
if (!PrepareForDataScan(data, 0))
return false; // IO error
while (data->Tell() < start_offset &&
(result = data->Next(&set_id, ¤t_set)) > 0) {
DeleteSubsumedCandidates(current_set);
}
if (result < 0) // IO error
return false;
}
std::cerr << "; Dumping maximal sets." << std::endl;
DumpMaximalSets();
} while (resume_offset != 0);
return true;
}
void AllMaximalSetsLexicographic::Init() {
maximal_sets_count_ = input_sets_count_ = canidate_seek_count_ = 0;
std::cerr << "; Finding all maximal itemsets.\n"
<< "; Limit on number of items in main memory: "
<< max_items_in_ram_ << std::endl;
}
bool AllMaximalSetsLexicographic::PrepareForDataScan(
DataSourceIterator* data, off_t resume_offset) {
std::cerr << "; Starting new dataset scan at offset: "
<< resume_offset << std::endl;
return data->Seek(resume_offset);
}
bool AllMaximalSetsLexicographic::ReadNextChunk(
DataSourceIterator* data, off_t* resume_offset) {
*resume_offset = 0;
items_in_ram_ = 0;
ItemSet current_set;
uint32_t set_id;
bool result;
while ((result = data->Next(&set_id, ¤t_set)) > 0) {
candidates_.push_back(SetProperties::Create(set_id, current_set));
items_in_ram_ += current_set.size();
++input_sets_count_;
// Check if we've exceeded the RAM limit and if so stop
// retaining any further itemsets in memory until the next
// scan.
if (items_in_ram_ >= max_items_in_ram_) {
*resume_offset = data->Tell();
std::cerr << "; Halted scan at input set number "
<< input_sets_count_ << " with id " << set_id << std::endl;
return true;
}
} // while ((result = data->Next() ...
return result == 0;
}
void AllMaximalSetsLexicographic::DeleteTriviallySubsumedCandidates() {
// Now iterate over the current chunk backwards and delete
// itemsets that are tivially subsumed based on prefix comparison.
std::cerr << "; Deleting trivially subsumed itemsets..." << std::endl;
assert(candidates_.size());
SetProperties* not_a_prefix_itemset = candidates_.back();
for (int i = candidates_.size() - 2; i >= 0; --i) {
SetProperties* candidate = candidates_[i];
bool subsumed = false;
if (candidate->size < not_a_prefix_itemset->size) {
subsumed = true;
for (unsigned int j = 0; j < candidate->size; ++j) {
if (candidate->item[j] != not_a_prefix_itemset->item[j]) {
subsumed = false;
break;
}
}
}
if (subsumed) {
items_in_ram_ -= candidate->size;
SetProperties::Delete(candidate);
candidates_[i] = 0;
} else {
not_a_prefix_itemset = candidate;
}
}
}
void AllMaximalSetsLexicographic::BuildIndex() {
// Finally, we compress out the blanks, identify blocks of candidates that
// start with the same item id, and build the index.
std::cerr << "; Building index..." << std::endl;
int blanks = 0;
index_.resize(candidates_.back()->item[0] + 1);
int begin_candidate_index = -1;
SetProperties* begin_candidate = 0; // candidate at the beginning of a block.
uint32_t previous_item = 0;
for (size_t i = 0; i < candidates_.size(); ++i) {
SetProperties* candidate = candidates_[i];
if (!candidate) {
blanks++;
} else {
candidates_[i - blanks] = candidate;
if (!begin_candidate) {
begin_candidate = candidate;
begin_candidate_index = i - blanks;
} else if (candidate->item[0] != begin_candidate->item[0]) {
// We've started a new block. Update the index with
// the stats from the previous block.
for (uint32_t item = previous_item + 1; item <= begin_candidate->item[0]; ++item) {
index_[item] = begin_candidate_index;
}
previous_item = begin_candidate->item[0];
begin_candidate = candidate;
begin_candidate_index = i - blanks;
}
}
} // for()
// Finish processing the final block.
for (uint32_t item = previous_item + 1; item <= begin_candidate->item[0]; ++item) {
index_[item] = begin_candidate_index;
}
candidates_.resize(candidates_.size() - blanks);
}
void AllMaximalSetsLexicographic::DeleteSubsumedCandidates(unsigned int current_set_index) {
current_set_ = candidates_[current_set_index];
assert(current_set_);
if (current_set_->size <= 1)
return;
const uint32_t* current_set_it = current_set_->begin();
// The first candidate_set we consider is the first set following
// current_set in the ordering, if one exists.
CandidateList::iterator begin_range_it =
candidates_.begin() + current_set_index + 1;
DeleteSubsumedFromRange(begin_range_it, candidates_.end(), current_set_it, 0);
}
void AllMaximalSetsLexicographic::DeleteSubsumedCandidates(const ItemSet& itemset) {
if (itemset.size() <= 1)
return;
current_set_ = SetProperties::Create(0, itemset);
const uint32_t* current_set_it = current_set_->begin();
DeleteSubsumedFromRange(
candidates_.begin(), candidates_.end(), current_set_it, 0);
SetProperties::Delete(current_set_);
}
// Helper function that advances begin_range_it over all subsumed &
// already deleted candidate sets, and deletes all subsumed itemsets
// encountered.
inline void AllMaximalSetsLexicographic::DeleteSubsumedSets(
CandidateList::iterator* begin_range_it,
CandidateList::iterator end_range_it,
unsigned int depth) {
// If current_set_->size() == depth, then the current set
// cannot *properly* subsume any candidates.
if (current_set_->size > depth) {
while (*begin_range_it != end_range_it &&
(!**begin_range_it || (**begin_range_it)->size == depth)) {
if (**begin_range_it) {
// Subsumed!
items_in_ram_ -= (**begin_range_it)->size;
SetProperties::Delete(**begin_range_it);
**begin_range_it = 0;
}
++(*begin_range_it);
}
} else {
// Otherwise just skip over already-deleted itemsets.
while (*begin_range_it != end_range_it && !**begin_range_it) {
++(*begin_range_it);
}
}
}
inline
AllMaximalSetsLexicographic::CandidateList::iterator AllMaximalSetsLexicographic::GetNewBeginRangeIt(
CandidateList::iterator begin_range_it,
CandidateList::iterator end_range_it,
uint32_t current_item,
unsigned int depth) {
++canidate_seek_count_;
if (depth == 0) {
// At depth 0 we can use the index rather than binary search.
if (current_item >= index_.size())
return end_range_it;
if (candidates_.begin() + index_[current_item] > begin_range_it) {
begin_range_it = candidates_.begin() + index_[current_item];
}
while (begin_range_it != end_range_it && !*begin_range_it)
++begin_range_it;
} else {
begin_range_it = find_new_it(
begin_range_it,
end_range_it,
current_item,
depth,
std::greater<uint32_t>());
}
return begin_range_it;
}
inline
AllMaximalSetsLexicographic::CandidateList::iterator AllMaximalSetsLexicographic::GetNewEndRangeIt(
CandidateList::iterator begin_range_it,
CandidateList::iterator end_range_it,
uint32_t current_item,
unsigned int depth) {
++canidate_seek_count_;
CandidateList::iterator new_end_range_it;
if (depth == 0) {
// At depth 0 we can use the index rather than binary search.
if (current_item + 1 < index_.size()) {
new_end_range_it = candidates_.begin() + index_[current_item + 1];
assert(new_end_range_it <= end_range_it);
} else {
new_end_range_it = end_range_it;
}
} else {
new_end_range_it = find_new_it(
begin_range_it,
end_range_it,
current_item,
depth,
std::equal_to<uint32_t>());
}
return new_end_range_it;
}
// This function has 2 important preconditions:
// (1) all candidates between begin_range_it and end_range_it have
// the same length-d prefix where d is the value of "depth"
// (2) *current_set_it <= candidate[d+1] for any candidate with more
// than d elements.
void AllMaximalSetsLexicographic::DeleteSubsumedFromRange(
CandidateList::iterator begin_range_it,
CandidateList::iterator end_range_it,
const uint32_t* current_set_it,
unsigned int depth) {
assert(begin_range_it != end_range_it);
DeleteSubsumedSets(&begin_range_it, end_range_it, depth);
if (begin_range_it == end_range_it || current_set_it == current_set_->end())
return;
do { // while (begin_range_it != end_range_it)
// First thing we do is find the next item in the current_set
// that, if added to our prefix, could potentially subsume some
// candidate within the remaining range.
uint32_t candidate_item = (**begin_range_it)[depth];
assert(current_set_it != current_set_->end());
if (*current_set_it < candidate_item) {
current_set_it = std::lower_bound(
current_set_it, current_set_->end(), candidate_item);
}
if (current_set_it == current_set_->end())
return;
assert(*current_set_it >= candidate_item);
if (*current_set_it == candidate_item) {
// The item we found matches the next candidate set item, which
// means we can extend the prefix. Before we recurse, we must
// compute an end range for the extended prefix.
CandidateList::iterator new_end_range_it = GetNewEndRangeIt(
begin_range_it, end_range_it, candidate_item, depth);
assert(new_end_range_it >= begin_range_it);
if (begin_range_it != new_end_range_it) {
DeleteSubsumedFromRange(
begin_range_it, new_end_range_it, current_set_it + 1, depth + 1);
}
begin_range_it = new_end_range_it;
while (begin_range_it != end_range_it && !*begin_range_it)
++begin_range_it;
} else {
// Advance the begin_range until we reach potentially subsumable candidates.
begin_range_it = GetNewBeginRangeIt(
begin_range_it, end_range_it, *current_set_it, depth);
}
} while (begin_range_it != end_range_it);
}
void AllMaximalSetsLexicographic::DumpMaximalSets() {
for (unsigned int i = 0; i < candidates_.size(); ++i) {
SetProperties* maximal_set = candidates_[i];
if (maximal_set) {
FoundMaximalSet(*maximal_set);
SetProperties::Delete(maximal_set);
}
}
candidates_.clear();
std::cout << std::flush;
}
void AllMaximalSetsLexicographic::FoundMaximalSet(const SetProperties& maximal_set) {
++maximal_sets_count_;
switch (output_mode_) {
case COUNT_ONLY:
break;
case ID:
std::cout << maximal_set.set_id << '\n';
break;
case ID_AND_ITEMS:
std::cout << maximal_set << '\n';
break;
default:
std::cout << "Huh?\n";
assert(0);
break;
}
}
} // google_extremal_sets