public FPNode() { counter = 1; parent = null; nodeLink = null; children = new List <FPNode>(); }
}//end scanDatabaseToDetermineFrequencyOfSingleItems private void print(FPNode node, string indention) { Console.WriteLine("{0}NODE : {1} COUNTER {2}", indention, node.itemID, node.counter); foreach (FPNode child in node.children) { print(child, string.Concat(indention, '\t')); } }//end print
public void addPrefixPath(List <FPNode> prefixPath, Dictionary <string, int> mapSupportBeta, int relativeMinSupport) { int pathCount = prefixPath.ElementAt(0).counter; FPNode currentNode = root; for (int i = prefixPath.Count - 1; i >= 1; i--) { FPNode pathItem = prefixPath[i]; if (mapSupportBeta[pathItem.itemID] < relativeMinSupport) { continue; } FPNode child = currentNode.getChildWithID(pathItem.itemID); if (child == null) { FPNode newNode = new FPNode(); newNode.itemID = pathItem.itemID; newNode.parent = currentNode; newNode.counter = pathCount; currentNode.children.Add(newNode); if (!hasMoreThanOnePath && currentNode.children.Count > 1) { hasMoreThanOnePath = true; } currentNode = newNode; FPNode header = null; if (mapItemNodes.ContainsKey(pathItem.itemID)) { header = mapItemNodes[pathItem.itemID]; } if (header == null) { mapItemNodes.Add(pathItem.itemID, newNode); } else { while (header.nodeLink != null) { header = header.nodeLink; } header.nodeLink = newNode; } } else { child.counter += pathCount; currentNode = child; } } }
}//end fpgrowth private void addAllCombinationsForPathAndPrefix(FPNode node, string[] prefix) { string[] itemset = new string[prefix.Length + 1]; prefix.CopyTo(itemset, 0); itemset[prefix.Length] = node.itemID; //write output to screen writeItemsetToOutput(itemset, node.counter); if (node.children.Count != 0) { addAllCombinationsForPathAndPrefix(node.children.ElementAt(0), itemset); addAllCombinationsForPathAndPrefix(node.children.ElementAt(0), prefix); } }//end addAllCombinationsForPathAndPrefix
public void addTransaction(List <string> transaction) { FPNode currentNode = root; foreach (string item in transaction) { FPNode child = currentNode.getChildWithID(item); if (child == null) { FPNode newNode = new FPNode(); newNode.itemID = item; newNode.parent = currentNode; currentNode.children.Add(newNode); if (!hasMoreThanOnePath && currentNode.children.Count > 1) { hasMoreThanOnePath = true; } currentNode = newNode; if (!mapItemNodes.ContainsKey(item)) { mapItemNodes.Add(item, newNode); } else { FPNode header = mapItemNodes[item]; while (header.nodeLink != null) { header = header.nodeLink; } header.nodeLink = newNode; } } else { child.counter++; currentNode = child; } } }
}//end Process private void fpgrowthMoreThanOnePath(FPTree tree, string[] prefixAlpha, int prefixSupport, Dictionary <string, int> mapSupport) { //process the frequent items in reverse for (int i = tree.headerList.Count - 1; i >= 0; i--) { string item = tree.headerList.ElementAt(i); int support = mapSupport[item]; // if the item is not frequent, we skip it if (support < _relativeMinSupport) { continue; } //Create the BETA by using concatentation of the Alpha with the current item string[] beta = new string[prefixAlpha.Length + 1]; prefixAlpha.CopyTo(beta, 0); beta[prefixAlpha.Length] = item; int betaSupport = (prefixSupport < support) ? prefixSupport : support; //Write out to file writeItemsetToOutput(beta, betaSupport); List <List <FPNode> > prefixPaths = new List <List <FPNode> >(); FPNode path = null; if (tree.mapItemNodes.ContainsKey(item)) { path = tree.mapItemNodes[item]; } while (path != null) { // if the path is not just the root node if (path.parent.itemID != null) { //build prefixpath List <FPNode> prefixPath = new List <FPNode>(); // add this node. prefixPath.Add(path); //Recursively add all the parents of this node. FPNode parent = path.parent; while (parent.itemID != null) { prefixPath.Add(parent); parent = parent.parent; } // add the path to the list of prefixpaths prefixPaths.Add(prefixPath); } // We will look for the next prefixpath path = path.nodeLink; } // (A) Calculate the frequency of each item in the prefixpath Dictionary <string, int> mapSupportBeta = new Dictionary <string, int>(); // for each prefixpath foreach (List <FPNode> prefixPath in prefixPaths) { // the support of the prefixpath is the support of its first node. int pathCount = prefixPath.ElementAt(0).counter; // for each node in the prefixpath, // except the first one, we count the frequency for (int j = 1; j < prefixPath.Count; j++) { FPNode node = prefixPath.ElementAt(j); // if the first time we see that node id if (!mapSupportBeta.ContainsKey(node.itemID)) { // just add the path count mapSupportBeta.Add(node.itemID, pathCount); } else { // otherwise, make the sum with the value already stored mapSupportBeta[node.itemID] += pathCount; } } } // (B) Construct beta's conditional FP-Tree FPTree treeBeta = new FPTree(); // add each prefixpath in the FP-tree foreach (List <FPNode> prefixPath in prefixPaths) { treeBeta.addPrefixPath(prefixPath, mapSupportBeta, _relativeMinSupport); } // Create the header list. treeBeta.createHeaderList(mapSupportBeta); // Mine recursively the Beta tree if the root as child(s) if (treeBeta.root.children.Count > 0) { // recursive call fpgrowth(treeBeta, beta, betaSupport, mapSupportBeta); } } }//end fpgrowthMoreThanOnePath
public FPNode() { parent = null; nodeLink = null; children = new List <FPNode>(); }