/** * 对频繁项集做剪枝步骤,必须保证新的频繁项集的子项集也必须是频繁项集 */ private List <FrequentItem> cutItem(List <String[]> resultIds) { String[] temp; // 忽略的索引位置,以此构建子集 int igNoreIndex = 0; FrequentItem tempItem; // 剪枝生成新的频繁项集 List <FrequentItem> newItem = new List <FrequentItem>(); // 不符合要求的id List <String[]> deleteIdArray = new List <String[]>(); // 子项集是否也为频繁子项集 bool isContain = true; foreach (String[] array in resultIds) { // 列举出其中的一个个的子项集,判断存在于频繁项集列表中 temp = new String[array.Length - 1]; for (igNoreIndex = 0; igNoreIndex < array.Length; igNoreIndex++) { isContain = true; for (int j = 0, k = 0; j < array.Length; j++) { if (j != igNoreIndex) { temp[k] = array[j]; k++; } } if (!isIDArrayContains(resultItemID, temp)) { isContain = false; break; } } if (!isContain) { deleteIdArray.Add(array); } } // 移除不符合条件的ID组合 resultIds.RemoveAll(s => deleteIdArray.Contains(s)); // 移除支持度计数不够的id集合 int tempCount = 0; bool isSatisfied = false; foreach (String[] array in resultIds) { isSatisfied = judgeFItem(array); // 如果此频繁项集满足多支持度阈值限制条件和支持度差别限制条件,则添加入结果集中 if (isSatisfied) { tempItem = new FrequentItem(array, tempCount); newItem.Add(tempItem); resultItemID.Add(array); resultItem.Add(tempItem); } } return(newItem); }
/** * 项集进行连接运算 */ private void computeLink() { // 连接计算的终止数,k项集必须算到k-1子项集为止 int endNum = 0; // 当前已经进行连接运算到几项集,开始时就是1项集 int currentNum = 1; // 商品,1频繁项集映射图 //Hashtable<String, FrequentItem> itemMap = new Hashtable<String, FrequentItem>(); Hashtable itemMap = new Hashtable(); FrequentItem tempItem; // 初始列表 List <FrequentItem> list = new List <FrequentItem>(); // 经过连接运算后产生的结果项集 resultItem = new List <FrequentItem>(); resultItemID = new List <String[]>(); // 商品ID的种类 List <String> idType = new List <String>(); foreach (String[] a in totalGoodsIDs) { foreach (String s in a) { if (!idType.Contains(s)) { tempItem = new FrequentItem(new String[] { s }, 1); idType.Add(s); resultItemID.Add(new String[] { s }); } else { // 支持度计数加1 tempItem = (FrequentItem)itemMap[s]; tempItem.setCount(tempItem.getCount() + 1); } if (itemMap.ContainsKey(s)) { //itemMap.Remove(s); //itemMap.Add(s, tempItem); } else { itemMap.Add(s, tempItem); } } } // 将初始频繁项集转入到列表中,以便继续做连接运算 foreach (DictionaryEntry entry in itemMap) { tempItem = (FrequentItem)entry.Value; // 判断1频繁项集是否满足支持度阈值的条件 if (judgeFItem(tempItem.getIdArray())) { list.Add(tempItem); } } // 按照商品ID进行排序,否则连接计算结果将会不一致,将会减少 list.Sort(); resultItem.AddRange(list); String[] array1; String[] array2; String[] resultArray; List <String> tempIds; List <String[]> resultContainer; // 总共要算到endNum项集 endNum = list.Count() - 1; initFItemNum = list.Count() - 1; while (currentNum < endNum) { resultContainer = new List <string[]>(); for (int i = 0; i < list.Count() - 1; i++) { tempItem = list[i]; array1 = tempItem.getIdArray(); for (int j = i + 1; j < list.Count(); j++) { tempIds = new List <string>(); array2 = list[j].getIdArray(); for (int k = 0; k < array1.Length; k++) { // 如果对应位置上的值相等的时候,只取其中一个值,做了一个连接删除操作 if (array1[k].Equals(array2[k])) { tempIds.Add(array1[k]); } else { tempIds.Add(array1[k]); tempIds.Add(array2[k]); } } //resultArray = new String[tempIds.Count()]; resultArray = tempIds.ToArray(); //tempIds.AddRange(resultArray); bool isContain = false; // 过滤不符合条件的的ID数组,包括重复的和长度不符合要求的 if (resultArray.Length == (array1.Length + 1)) { isContain = isIDArrayContains(resultContainer, resultArray); if (!isContain) { resultContainer.Add(resultArray); } } } } // 做频繁项集的剪枝处理,必须保证新的频繁项集的子项集也必须是频繁项集 list = cutItem(resultContainer); currentNum++; } }