@@ -16,18 +16,23 @@ class FPTree
16
16
17
17
private FPNode $ root ;
18
18
19
+ private int $ maxLength = 0 ;
20
+
21
+ private int $ depth = 0 ;
22
+
19
23
/**
20
24
* Initialize the tree.
21
25
* @param array $transactions
22
26
* @param int $threshold
23
27
* @param $rootValue
24
28
* @param int $rootCount
25
29
*/
26
- public function __construct (array $ transactions , int $ threshold , $ rootValue , int $ rootCount )
30
+ public function __construct (array $ transactions , int $ threshold , $ rootValue , int $ rootCount, $ maxLength = 0 )
27
31
{
28
32
$ this ->frequent = $ this ->findFrequentItems ($ transactions , $ threshold );
29
33
$ this ->headers = $ this ->buildHeaderTable ();
30
34
$ this ->root = $ this ->buildFPTree ($ transactions , $ rootValue , $ rootCount , $ this ->frequent );
35
+ $ this ->maxLength = $ maxLength ;
31
36
}
32
37
33
38
/**
@@ -168,6 +173,8 @@ public function minePatterns(int $threshold): array
168
173
{
169
174
if ($ this ->treeHasSinglePath ($ this ->root )) {
170
175
return $ this ->generatePatternList ();
176
+ } elseif ($ this ->maxLength && $ this ->maxLength <= $ this ->getDepth ()) {
177
+ return [];
171
178
}
172
179
173
180
return $ this ->zipPatterns ($ this ->mineSubTrees ($ threshold ));
@@ -211,7 +218,13 @@ protected function generatePatternList(): array
211
218
$ patterns [$ this ->root ->value ] = $ this ->root ->count ;
212
219
}
213
220
214
- for ($ i = 1 ; $ i <= count ($ items ); $ i ++) {
221
+ // limit length of combinations to remaining length
222
+ $ count = count ($ items );
223
+ if ($ this ->maxLength ) {
224
+ $ count = min ($ count , $ this ->maxLength - $ this ->getDepth ());
225
+ }
226
+
227
+ for ($ i = 1 ; $ i <= $ count ; $ i ++) {
215
228
$ combinations = new Combinations ($ items ,$ i );
216
229
foreach ($ combinations ->generator () as $ subset ) {
217
230
$ pattern = $ this ->root ->value !== null ? array_merge ($ subset , [$ this ->root ->value ]) : $ subset ;
@@ -270,7 +283,8 @@ protected function mineSubTrees(int $threshold): array
270
283
}
271
284
272
285
// Now we have the input for a subtree, so construct it and grab the patterns.
273
- $ subtree = new FPTree ($ conditionalTreeInput , $ threshold , $ item , $ this ->frequent [$ item ]);
286
+ $ subtree = new FPTree ($ conditionalTreeInput , $ threshold , $ item , $ this ->frequent [$ item ], $ this ->maxLength );
287
+ $ subtree ->depth = $ this ->depth + 1 ;
274
288
$ subtreePatterns = $ subtree ->minePatterns ($ threshold );
275
289
276
290
// Insert subtree patterns into main patterns dictionary.
@@ -285,4 +299,9 @@ protected function mineSubTrees(int $threshold): array
285
299
286
300
return $ patterns ;
287
301
}
302
+
303
+ private function getDepth (): int
304
+ {
305
+ return $ this ->depth ;
306
+ }
288
307
}
0 commit comments