示例#1
0
        public FPTree CreateTree()
        {
            FPTree tree = new FPTree();

            tree.root      = new Node();
            tree.arrayNode = new List <Node>(); //new Node[999999];
            tree.countNode = 0;
            return(tree);
        }
示例#2
0
        private static FPTree CreateTree(ItemsetCollection T)
        {
            FPTree tree = new FPTree();

            List[] list_frequencyItems_TID;

            int i = 0;

            list_frequencyItems_TID = ToList(T);
            for (i = 0; i < T.Count; i++)
            {
                List list = new List();
                list = list_frequencyItems_TID[i];
                tree = tree.InsertNode(tree, list);
            }
            return(tree);
        }
示例#3
0
        public FPTree InsertNode(FPTree tree, List list)
        {
            int  i    = 0;
            Node root = new Node();

            root = tree.root;
            Node node = new Node();

            node = list.pHead;
            while (node != null)
            {
                bool flag = false;
                // tìm rtrong root có itemName nào trùng với node.itemName không?
                for (i = 0; i < root.nodeChildrens.Length; i++)
                {
                    if (root.nodeChildrens[i] != null)
                    {
                        if (root.nodeChildrens[i].itemName == node.itemName)
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                // nếu tìm thấy thì tăng cout của itemName đó lên 1
                if (flag == true)
                {
                    root.nodeChildrens[i].count += 1;
                    if (node.nodeChild != null && node.nodeChild.nodeParent != null)
                    {
                        node.nodeChild.nodeParent = root.nodeChildrens[i];
                    }
                }
                // ngược lại tạo node mới
                else
                {
                    node.nodeParent = root;
                    node.count     += 1;
                    tree.countNode += 1;
                    // gán node là con của root
                    for (i = 0; i < root.nodeChildrens.Length; i++)
                    {
                        if (root.nodeChildrens[i] == null)
                        {
                            root.nodeChildrens[i] = node;
                            break;
                        }
                    }
                    tree.arrayNode.Add(node);
                    // thêm node vào arrayNode
                    //for (i = 0; i < tree.countNode; i++)
                    //{
                    //    if (tree.arrayNode[i] == null)
                    //    {
                    //        tree.arrayNode[i] = node;
                    //        break;
                    //    }
                    //}
                    // link đến node có itemName giống node
                    //for (i = tree.countNode - 2; i > 0; i--)
                    //{
                    //    if (tree.arrayNode[i] != null)
                    //    {
                    //        if (tree.arrayNode[i].itemName == node.itemName)
                    //        {
                    //            node.nodeLink = arrayNode[i];
                    //            break;
                    //        }
                    //    }
                    //}
                }
                if (node.nodeChild == null)
                {
                    root = node;
                }
                else
                {
                    root = node.nodeChild.nodeParent;
                }
                node = node.nodeChild;
            }
            return(tree);
        }
示例#4
0
        public static ItemsetCollection DoFPGrowthParallel(ItemsetCollection db, double supportThreshold)
        {
            ItemsetCollection        Fre       = new ItemsetCollection(); //List of Items > support
            ItemsetCollection        T         = new ItemsetCollection(); //frequent items
            List <ItemsetCollection> P         = new List <ItemsetCollection>();
            ItemsetCollection        FPTreeCon = new ItemsetCollection(); //Conditional FP-Tree

            //item > support
            //Itemset I = db.GetUniqueItems();
            //foreach (string item in I)
            //{
            //    Itemset itemset = new Itemset() { item };
            //    itemset.Support = db.FindSupport(itemset);
            //    if (itemset.Support >= supportThreshold)
            //    {
            //        Fre.Add(itemset);
            //    }
            //}
            //// sort item
            //Fre.Sort(delegate (Itemset x, Itemset y)
            //{
            //    if (x.Support > y.Support) return -1;
            //    else if (x.Support < y.Support)
            //        return 1;
            //    else return 0;
            //});
            ////Fre = ParallelFre(db, supportThreshold);
            ////frequent items
            //foreach (Itemset itemdata in db)
            //{
            //    Itemset itemset = new Itemset();
            //    foreach (Itemset itemFre in Fre)
            //    {
            //        if (itemdata.Contains(itemFre[0]))
            //        {
            //            itemset.Add(itemFre[0]);
            //        }
            //    }
            //    T.Add(itemset);
            //}

            T = ParallelFPGrowth(db, supportThreshold, ref Fre);
            // create FP Tree
            FPTree tree = CreateTree(T);

            // Conditional Patern Bases
            foreach (Itemset itemset in Fre)
            {
                ItemsetCollection itemsetCollection = new ItemsetCollection();
                for (int j = 0; j < tree.countNode; j++)
                {
                    var node = tree.arrayNode[j];
                    if (node.itemName.Equals(itemset[0]) && !node.visited)
                    {
                        node.visited = true;
                        var nodeparent = node.nodeParent;
                        while (nodeparent.itemName > -1)
                        {
                            int index = itemsetCollection.FindIndex(nodeparent.itemName);
                            if (index > -1)
                            {
                                itemsetCollection[index].Support += node.count;
                            }
                            else
                            {
                                Itemset item = new Itemset();
                                item.Support = node.count;
                                item.Add(nodeparent.itemName);
                                itemsetCollection.Add(item);
                            }
                            nodeparent = nodeparent.nodeParent;
                        }
                    }
                }
                P.Add(itemsetCollection);
            }
            // Conditional FP-Tree
            for (int i = 0; i < P.Count; i++)
            {
                ItemsetCollection itemsetCollection = new ItemsetCollection();
                //itemsetCollection.Add(Fre[i]); //hoán vị sau
                foreach (var item in P[i])
                {
                    if (db.Support(item) >= supportThreshold)
                    {
                        itemsetCollection.Add(item);
                    }
                }
                ItemsetCollection subsets = Bit.FindSubsets(itemsetCollection.GetUniqueItems(), 0); //get all subsets
                foreach (Itemset items in subsets)
                {
                    items.Add(Fre[i][0]);
                    items.Support = db.FindSupport(items);
                    if (items.Support >= supportThreshold)
                    {
                        FPTreeCon.Add(items);
                    }
                }
            }
            return(FPTreeCon);
        }