/** * Mine an FP-Tree having more than one path. * * @param tree the FP-tree * @param prefix the current prefix, named "alpha" * @param mapSupport the frequency of items in the FP-Tree * @throws IOException exception if error writing the output file */ private void fpgrowth( FPTree tree, int[] prefix, int prefixLength, int prefixSupport, Map<Integer, Integer> mapSupport) throws IOException { //// ======= DEBUG ======== // System.out.print("###### Prefix: "); // for(int k=0; k< prefixLength; k++) { // System.out.print(prefix[k] + " "); // } // System.out.println("\n"); //// ========== END DEBUG ======= // System.out.println(tree); // We will check if the FPtree contains a single path boolean singlePath = true; // We will use a variable to keep the support of the single path if there is one int singlePathSupport = 0; // This variable is used to count the number of items in the single path // if there is one int position = 0; // if the root has more than one child, than it is not a single path if (tree.root.childs.size() > 1) { singlePath = false; } else { // Otherwise, // if the root has exactly one child, we need to recursively check childs // of the child to see if they also have one child FPNode currentNode = tree.root.childs.get(0); while (true) { // if the current child has more than one child, it is not a single path! if (currentNode.childs.size() > 1) { singlePath = false; break; } // otherwise, we copy the current item in the buffer and move to the child // the buffer will be used to store all items in the path itemsetTempBuffer[position] = currentNode.itemID; // we keep the support of the path singlePathSupport = currentNode.counter; position++; // if this node has no child, that means that this is the end of this path // and it is a single path, so we break if (currentNode.childs.size() == 0) { break; } currentNode = currentNode.childs.get(0); } } // Case 1: the FPtree contains a single path if (singlePath && singlePathSupport >= minSupportRelative) { // We save the path, because it is a maximal itemset saveAllCombinationsOfPrefixPath( itemsetTempBuffer, position, prefix, prefixLength, singlePathSupport); } else { // For each frequent item in the header table list of the tree in reverse order. for (int i = tree.headerList.size() - 1; i >= 0; i--) { // get the item Integer item = tree.headerList.get(i); // get the item support int support = mapSupport.get(item); // Create Beta by concatening prefix Alpha by adding the current item to alpha prefix[prefixLength] = item; // calculate the support of the new prefix beta int betaSupport = (prefixSupport < support) ? prefixSupport : support; // save beta to the output file saveItemset(prefix, prefixLength + 1, betaSupport); // === (A) Construct beta's conditional pattern base === // It is a subdatabase which consists of the set of prefix paths // in the FP-tree co-occuring with the prefix pattern. List<List<FPNode>> prefixPaths = new ArrayList<List<FPNode>>(); FPNode path = tree.mapItemNodes.get(item); // Map to count the support of items in the conditional prefix tree // Key: item Value: support Map<Integer, Integer> mapSupportBeta = new HashMap<Integer, Integer>(); while (path != null) { // if the path is not just the root node if (path.parent.itemID != -1) { // create the prefixpath List<FPNode> prefixPath = new ArrayList<FPNode>(); // add this node. prefixPath.add(path); // NOTE: we add it just to keep its support, // actually it should not be part of the prefixPath // #### int pathCount = path.counter; // Recursively add all the parents of this node. FPNode parent = path.parent; while (parent.itemID != -1) { prefixPath.add(parent); // FOR EACH PATTERN WE ALSO UPDATE THE ITEM SUPPORT AT THE SAME TIME // if the first time we see that node id if (mapSupportBeta.get(parent.itemID) == null) { // just add the path count mapSupportBeta.put(parent.itemID, pathCount); } else { // otherwise, make the sum with the value already stored mapSupportBeta.put(parent.itemID, mapSupportBeta.get(parent.itemID) + pathCount); } parent = parent.parent; } // add the path to the list of prefixpaths prefixPaths.add(prefixPath); } // We will look for the next prefixpath path = path.nodeLink; } // (B) Construct beta's conditional FP-Tree // Create the tree. FPTree treeBeta = new FPTree(); // Add each prefixpath in the FP-tree. for (List<FPNode> prefixPath : prefixPaths) { treeBeta.addPrefixPath(prefixPath, mapSupportBeta, minSupportRelative); } // Mine recursively the Beta tree if the root has child(s) if (treeBeta.root.childs.size() > 0) { // Create the header list. treeBeta.createHeaderList(mapSupportBeta); // recursive call fpgrowth(treeBeta, prefix, prefixLength + 1, betaSupport, mapSupportBeta); } } } }