/// <summary> /// Create a new object with the passed value /// </summary> /// <param name="Item">the passed value</param> public FPNode(int Item) { _item = Item; _count = default(int); _parent = default(FPNode); _childs = default(FPNode[]); }
/// <summary> /// A method that uses a binary search based on custom ComparatorByItem /// that return the index of the searched Node if founded, otherwise a negative number /// </summary> /// <param name="node">The FPNode looking index for</param> /// <returns>the index of the founded node</returns> public int GetIndex(FPNode node) { HTrecord test = new HTrecord(); test.Item = node.Item; return(Array.BinarySearch(HTarray, test, new ComparatorByItem())); }
public FPNode() { _item = _count = default(int); _parent = default(FPNode); _childs = default(FPNode[]); _next = default(FPNode); }
public FPTree() { MinSup = default(int); Root = new FPNode(); Root.Item = default(int); Root.Parent = default(FPNode); _singleFrequent = new List <ItemSet>(); _hasSinglePath = true; _depth = 0; }
/// <summary> /// Method that build a new FPTree starting from i-th header table entry /// </summary> /// <param name="i">index of header table entry (head of Conditional Pattern Base)</param> /// <returns>the created FPTree</returns> public FPTree CreateFPtree(int i) { FPNode ResultRootNode = new FPNode(); FPTree Result = new FPTree(); Result.Root = ResultRootNode; Result.MinSup = _minSup; // Counting the frequency of single items Dictionary <int, int> FrequencyItemCounter2 = new Dictionary <int, int>(); Result.FrequencyItemCounter = FrequencyItemCounter2; FPNode node; //Obtain a reference to head of the list of the i.th htrecord node = ht.GetHead(i); while (node != default(FPNode)) { int support; _travel = node; support = _travel.Count; _travel = _travel.Parent; if (_travel.Parent != default(FPNode)) { while (_travel.Parent != default(FPNode)) { if (FrequencyItemCounter2.ContainsKey(_travel.Item)) { FrequencyItemCounter2[_travel.Item] += support; } else { FrequencyItemCounter2.Add(_travel.Item, support); } _travel = _travel.Parent; } } node = node.Next; } //Evaluate header table dimension int HTsize = 0; foreach (int item in FrequencyItemCounter2.Keys) { if (FrequencyItemCounter2[item] >= _minSup) { HTsize++; } } HeaderTable newht = new HeaderTable(HTsize); Result.ht = newht; //Insertion of frequent items in header table foreach (KeyValuePair <int, int> coppia in FrequencyItemCounter2) { if (coppia.Value >= _minSup) { newht.addRecord(coppia.Key, coppia.Value); } } // Removal of non frequent items, sorting and final insertion in the FPTreee node = ht.GetHead(i); while (node != default(FPNode)) { _travel = node; ItemSet path = new ItemSet(); path.ItemsSupport = _travel.Count; _travel = _travel.Parent; if (_travel.Parent != default(FPNode)) { while (_travel.Parent != default(FPNode)) { path.Add(_travel.Item); _travel = _travel.Parent; } ItemSet SortedList = new ItemSet(); SortedList.ItemsSupport = path.ItemsSupport; foreach (int item in path.Items) { if (FrequencyItemCounter2[item] >= _minSup) { SortedList.Add(item); } } if (SortedList.Items.Count > 0) { SortedList.Items.Sort(new ItemSortingStrategy(FrequencyItemCounter2)); if (Result._depth < SortedList.ItemsNumber) { Result._depth = SortedList.ItemsNumber; } Result.AddItemSetTree(SortedList, ResultRootNode); } } node = node.Next; } return(Result); }
/// <summary> /// Add an itemset to an FPTree /// </summary> /// <param name="SortedList">itemset to be added (already sorted)</param> /// <param name="me">node where to start the insertion</param> private void AddItemSetTree(ItemSet SortedList, FPNode me) { FPNode found; //looping on each item and searching if it's present or not in the FPTree for (int i = 0; i < SortedList.ItemsNumber; i++) { //if the me node has got no childs let's create one new if (me.Childs == default(FPNode[])) { me.Childs = new FPNode[0]; } int j = 0; found = default(FPNode); // j represented a sliding index that point to the position where i want to // add or insert the current item while ((j < me.Childs.Length) && (me.Childs[j].Item != SortedList.Items[i])) { j++; } if (j < me.Childs.Length) { found = me.Childs[j]; } // If founded node is present in the tree, increase it's support and // move on to the next item updating me reference if (found != default(FPNode)) { found.Count += SortedList.ItemsSupport; me = found; } // Create a new FPNode and set header table head and tail corresponding list else { if (me.Childs.Length > 0) { _hasSinglePath = false; } FPNode newnode = new FPNode(SortedList.Items[i]); int index = ht.GetIndex(newnode); if (ht.GetTail(index) == default(FPNode)) { ht.SetHead(newnode, index); ht.SetTail(newnode, index); } else { ht.GetTail(index).Next = newnode; ht.SetTail(newnode, index); } newnode.Count = SortedList.ItemsSupport; // Temporary Array where to store the new list of child nodes FPNode[] tmpArray = new FPNode[me.Childs.Length + 1]; for (int k = 0; k < me.Childs.Length; k++) { tmpArray[k] = me.Childs[k]; } tmpArray[me.Childs.Length] = newnode; me.Childs = tmpArray; newnode.Parent = me; // Me reference set to the new node me = newnode; } } }
/// <summary> /// Set the ending pointer of the specified header table list to first node /// </summary> /// <param name="last">the last node of the header table list given by index</param> /// <param name="index">the index of the list of the header table</param> internal void SetTail(FPNode last, int index) { HTarray[index].Tail = last; }
/// <summary> /// Set the initial pointer of the specified header table list to first node /// </summary> /// <param name="first">the first node of the header table list given by index</param> /// <param name="index">the index of the list of the header table</param> public void SetHead(FPNode first, int index) { HTarray[index].Head = first; }