Exemplo n.º 1
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);
        }