• FPGrowth


    在挖掘关联规则的过程中,无可避免要处理海量的数据,也就是事务数据库如此之大,如果采用Apriori算法来挖掘,每次生成频繁k-项集的时候,可能都需要扫描事务数据库一遍,这是非常耗时的操作。那么,可以想尽办法来减少扫描事务数据库的次数,来改进挖掘频繁关联规则的效率。

    FP-tree是频繁模式树,它是将整个事务数据库压缩到一棵频繁模式树上。而且,在构造整个事务数据库的的FP-tree的过程中,只需要扫描一次事务数据库就能生成。比AproriGen算法生成候选频繁项集要节省很多时间。

    关联规则挖掘FP-growth算法,是通过遍历上面构造的整个事务数据库的频繁模式树来生成频繁项集。FP-growth算法基本思想描述如下:

    第一步:构造整个事务数据库的FP-tree

    关于FP-tree的构造可以参考前面的文章 FP- tree的数据结构及其构造 。这里假设已经能够构造出FP-tree,接着就是在整个事务数据库对应的FP-tree的基础上挖掘频繁项集。在下面的步骤中,需要对FP-tree的结构及其内容熟悉。

    第二步:挖掘条件模式基

    在构造的整个事务数据库的频繁模式树上进行条件模式基的挖掘。

    条件模式基,就是选定一个基于支持计数降序排序的频繁1-项集项目,假设为Item,也就是FP-tree的头表中的频繁1-项集项目(已经知道,头表中频繁1-项集项目是按照降序排列的),此时,称该频繁1-项集项目Item为后缀。

    纵向沿着头表向上,也就是按照头表中频繁1-项集支持计数的升序方向,优先遍历头表;在遍历头表的过程中同时横向遍历每个频繁1-项集对应的链表域。

    通过横向遍历该频繁1-项集项目Item对应的链表域——每个链表中的FPTNode结点都具有一个直接父亲结点的nodeParent指针,纵向向上遍历直到根结点停止,就得到了一个序列(不包含Item对应的横向链表中的结点),这个序列就是条件模式基。

    在遍历的过程中,每个条件模式序列中每个FPTNode结点肯定出现一次;以Item频繁1-项集项目横向遍历得到的序列,都是以Item为后缀的。

    最后,整棵FP-tree遍历完毕,得到全部的条件模式基。

    第三步:根据条件模式基建立局部FP-tree

    对上面得到的条件模式基,对每个头表中的频繁1-项集对应的条件模式,作为数据输入源来构造局部FP-tree,也就是条件模式基的FP- tree。因为每个条件模式基的数据量与整个事务数据库相比,显得非常小,建树不会消耗太多时间;而全部的条件模式基就相当于整个事务数据库,所以大约需要扫描两次事务数据库。

    建立条件模式基的局部FP-tree,分为单个分支和多个分支两种情况,大体过程是这样的:

    (1)对于单个分支:

    扫描每个条件模式基,统计在每个单个分支中FPTNode结点中1-项集的支持计数,如果一个头表中的项目Item对应的条件模式基扫描完成,最终的计数不能满足最小支持计数,需要将该结点删除掉,因为它与其他结点组合以后,每个含有该结点的项集一定不满足最小支持计数。

    根据上面得到的满足最小支持计数的序列来构造局部FP-tree,因为得到的FPtree是单个分支的,遍历该FP-tree能够得到一个Item 的全部满足最小支持计数的序列,从而将该序列中的全部项目进行组合计算,得到全部组合序列,对于每个序列都将当前头表中Item加入到其中,就得到了包含 Item的全部频繁项集。

    (2)对于多个分支:

    如果存在分支的,需要递归挖掘频繁项集。因为对于多个分支,递归到出口一定是对应着单个分支的,可以类似上一种情况,处理单个分支,得到频繁项集。

    第四步:挖掘频繁关联规则

    在上面的步骤中,已经得到了全部的频繁项集,这时挖掘频繁关联规则与Apriori算法的频繁关联规则挖掘的步骤相同。

    通过上面的步骤,就完成了频繁关联规则的挖掘。我认为,该算法的思想还是非常清晰的。在基于FP-tree的关联规则挖掘FP-growth算法中,构造整个事务数据库的FP-tree是一个难点,需要保证在构造的过程中不丢失数据结点;另一个难点就是在处理得到的条件模式基的时候,对于具有多个分支的情况,采用递归的思想挖掘,保证不漏掉任何频繁项集。

    /**

    * FPGrowth算法的主要思想:
    * 1. 构造频繁1项集:遍历初始数据集构造频繁1项集,并作为项头表,建立将指向fpTree节点对应元素的引用
    * 2. 构造FPTree:再次遍历初始数据集,对于每一条事务中的元素,根据频繁1项集中元素的顺序排序,
    * 由此建立FPTree,记录每条事务的节点在同一条路径上出再的节点次数;
    * 3. 逆序遍历在步骤1中构造的项头表,根据其提供的引用指针,找出fpTree中由该节点到根节点的路径,
    * 即生成每个频繁元素的条件模式基
    * 4. 根据每个频繁元素对应的条件模式基,生成其对应的条件fpTree,并删除树中节点记数不满足给定的最小支持度的节点
    * 5. 对于每一颗条件fpTree,生成所有的从根节点到叶子节点的路径,由路径中的集合生成其所有非空子集
    * 所有这些非空子集和每一个频繁1项集中的元素共同构成了原始数据集中的频繁集
    *
    */
    Java代码  收藏代码
    1. package com.ustc.fpGrowth;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. public class Item implements Comparable {  
    7.   
    8.     private String value;  
    9.     private Item preItem; // 前继节点Item  
    10.     private List<Item> nextItem = new ArrayList<Item>(); // 后续节点Item  
    11.   
    12.     private Item sibling; // 关联节点  
    13.   
    14.     private int counter;  
    15.   
    16.     public Item() {  
    17.   
    18.     }  
    19.   
    20.     public Item(String value) {  
    21.         this.value = value;  
    22.     }  
    23.   
    24.     public void addCounter() {  
    25.         this.counter += 1;  
    26.     }  
    27.   
    28.     public Item getSibling() {  
    29.         return sibling;  
    30.     }  
    31.   
    32.     public void setSibling(Item sibling) {  
    33.         this.sibling = sibling;  
    34.     }  
    35.   
    36.     public void addNextItem(Item item) {  
    37.         this.nextItem.add(item);  
    38.     }  
    39.   
    40.     public List<Item> getNextItem() {  
    41.   
    42.         return this.nextItem;  
    43.     }  
    44.   
    45.     public String getValue() {  
    46.         return value;  
    47.     }  
    48.   
    49.     public void setValue(String value) {  
    50.         this.value = value;  
    51.     }  
    52.   
    53.     public Item getPreItem() {  
    54.         return preItem;  
    55.     }  
    56.   
    57.     public void setPreItem(Item preItem) {  
    58.         this.preItem = preItem;  
    59.     }  
    60.   
    61.     public int getCounter() {  
    62.         return counter;  
    63.     }  
    64.   
    65.     public void setCounter(int counter) {  
    66.         this.counter = counter;  
    67.     }  
    68.   
    69.     public int compareTo(Object o) {  
    70.         int value;  
    71.         Item i = (Item) o;  
    72.   
    73.         if (this.counter > i.counter) {  
    74.             value = -1;  
    75.         } else if (this.counter == i.counter) {  
    76.             value = 0;  
    77.         } else {  
    78.             value = 1;  
    79.         }  
    80.         return value;  
    81.     }  
    82.   
    83. }  



    Java代码  收藏代码
    1. package com.ustc.fpGrowth;  
    2.   
    3. import java.io.BufferedReader;  
    4. import java.io.File;  
    5. import java.io.FileNotFoundException;  
    6. import java.io.FileReader;  
    7. import java.io.IOException;  
    8. import java.util.ArrayList;  
    9. import java.util.Arrays;  
    10. import java.util.HashMap;  
    11. import java.util.List;  
    12. import java.util.Map;  
    13. import java.util.Set;  
    14.   
    15. public class FPGrowth {  
    16.   
    17.     private int minSup;  
    18.   
    19.     /** 
    20.      * @param args 
    21.      */  
    22.     public static void main(String[] args) {  
    23.         FPGrowth fpg = new FPGrowth();  
    24.         fpg.setMinSup(1000);  
    25.         List<String> data = fpg.buildData("retail.dat");  
    26.         Item[] f1Items = fpg.buildF1Items(data);  
    27.           
    28.         Map<String, List<String>> condBase;  
    29.         //Item fpTree = fpg.buildFPTree(data, f1Items);  
    30.         fpg.buildFPTree(data, f1Items);  
    31.         // fpg.fpGrowth();  
    32. /* 
    33.         fpg.printFPTree(fpTree); 
    34.         fpg.printF1Items(f1Items);*/  
    35.         condBase = fpg.buildCondBase(f1Items);  
    36.     //  fpg.printCondBase(condBase);  
    37.         Map<String, Item> condFPTree = fpg.buildCondFPTree(condBase);  
    38.     //  fpg.printCondFPTree(condFPTree);  
    39.         //输出频繁子集  
    40.         Map<String, List<List<String>>> fpSetMap = fpg.fpGrowth(condFPTree);  
    41.         fpg.printFPSet(fpSetMap);  
    42.           
    43.            
    44.     }  
    45.     /** 
    46.      * 输出频繁集 
    47.      */  
    48.     public void printFPSet(Map<String, List<List<String>>> fpSetMap){  
    49.         List<List<String>> fpSet;  
    50.           
    51.         Set<String> items = fpSetMap.keySet();  
    52.         for(String item : items){  
    53.             System.out.println(item);  
    54.             fpSet = fpSetMap.get(item);  
    55.             for (int i = 0; i < fpSet.size(); i++) {  
    56.                 for (String str : fpSet.get(i)) {  
    57.                 //  if(str != null && str.length() > 0){  
    58.                         System.out.print(str + ", ");  
    59.                 //  }  
    60.                       
    61.                 }  
    62.                 System.out.println(item);  
    63.             }  
    64.         }  
    65.     }  
    66.       
    67.     // 输出fpTree  
    68.     public void printFPTree(Item root) {  
    69.         System.out.print(root.getValue() + ", " + root.getCounter() + " "  
    70.                 + root.getNextItem().size() + ": ");  
    71.         List<Item> subItems = root.getNextItem();  
    72.         if (subItems.size() != 0) {  
    73.             for (int i = 0; i < subItems.size(); i++) {  
    74.                 printFPTree(subItems.get(i));  
    75.             }  
    76.             System.out.println();  
    77.         }  
    78.   
    79.     }  
    80.   
    81.     // 输出频繁1项集  
    82.     public void printF1Items(Item[] f1Items) {  
    83.         for (Item item : f1Items) {  
    84.   
    85.             while ((item = item.getSibling()) != null) {  
    86.                 System.out.print("item: " + item.getValue() + ": "  
    87.                         + item.getCounter() + " ,");  
    88.                 if (item.getPreItem() != null) {  
    89.                     System.out.println(item.getPreItem().getValue());  
    90.                 }  
    91.             }  
    92.             System.out.println();  
    93.         }  
    94.     }  
    95.   
    96.     // 输出条件模式基  
    97.     public void printCondBase(Map<String, List<String>> condBaseMap) {  
    98.   
    99.         Set<String> items = condBaseMap.keySet();  
    100.         List<String> conBase;  
    101.         for (String item : items) {  
    102.             System.out.print("Item: " + item);  
    103.             conBase = condBaseMap.get(item);  
    104.             System.out.println(", " + conBase.size());  
    105.             for (String str : conBase) {  
    106.                 System.out.println(str + " ");  
    107.             }  
    108.         }  
    109.     }  
    110.   
    111.     // 输出条件fp树  
    112.     public void printCondFPTree(Map<String, Item> condFPTreeMap) {  
    113.         Set<String> items = condFPTreeMap.keySet();  
    114.         for (String item : items) {  
    115.             System.out.println("Item: " + item);  
    116.             this.printFPTree(condFPTreeMap.get(item));  
    117.         }  
    118.     }  
    119.   
    120.     /** 
    121.      * 1.构造数据集 
    122.      */  
    123.     public List<String> buildData(String...fileName) {  
    124.   
    125.         List<String> data = new ArrayList<String>();  
    126.         if(fileName.length !=0){  
    127.             File file = new File(fileName[0]);  
    128.             try {  
    129.                 BufferedReader reader = new BufferedReader(new FileReader(file));  
    130.                 String line;  
    131.                 while( (line = reader.readLine()) != null){  
    132.                     data.add(line);  
    133.                 }  
    134.                   
    135.             } catch (FileNotFoundException e) {  
    136.                   
    137.                 e.printStackTrace();  
    138.             } catch (IOException e) {  
    139.                   
    140.                 e.printStackTrace();  
    141.             }  
    142.         }else{  
    143.               
    144.             data.add("I1 I2 I5");  
    145.             data.add("I2 I4");  
    146.             data.add("I2 I3");  
    147.             data.add("I1 I2 I4");  
    148.             data.add("I1 I3");  
    149.             data.add("I2 I3");  
    150.             data.add("I1 I3");  
    151.             data.add("I1 I2 I3 I5");  
    152.             data.add("I1 I2 I3");  
    153.         }  
    154.         return data;  
    155.     }  
    156.   
    157.     /** 
    158.      * 2.构造频繁1项列表,同时作为树的项头表 
    159.      */  
    160.     public Item[] buildF1Items(List<String> data) {  
    161.         List<Item> itemList = new ArrayList<Item>();  
    162.         Map<String, Item> resultMap = new HashMap<String, Item>();  
    163.         for (String trans : data) {  
    164.   
    165.             String[] items = trans.trim().split(" ");  
    166.             int i;  
    167.             for (String item : items) {  
    168.                   
    169.                 if(resultMap.get(item) == null){  
    170.                     Item newItem = new Item();  
    171.                     newItem.setValue(item);  
    172.                     newItem.setCounter(1);  
    173.                     resultMap.put(item, newItem);  
    174.                 }else{  
    175.                     resultMap.get(item).addCounter();  
    176.                 }  
    177.             }  
    178.         }  
    179.         Set<String> keySet = resultMap.keySet();  
    180.         for(String key : keySet){  
    181.             itemList.add(resultMap.get(key));  
    182.         }  
    183.         List<Item> result = new ArrayList<Item>();  
    184.         // 把支持度小于minSup的项从列表中删除  
    185.         for (int i = 0; i < itemList.size(); i++) {  
    186.             if (itemList.get(i).getCounter() >= this.minSup) {  
    187.                 result.add(itemList.get(i));  
    188.             }  
    189.         }  
    190.   
    191.         // 对列表进行排序  
    192.         Item[] f1Items = result.toArray(new Item[0]);  
    193.         Arrays.sort(f1Items);  
    194.   
    195.         return f1Items;  
    196.     }  
    197.       
    198.     /** 
    199.      * 3. 构造fpTree 
    200.      */  
    201.     public Item buildFPTree(List<String> data, Item[] f1Items) {  
    202.   
    203.         Item fpTree = new Item();  
    204.         List<Item> subItems;  
    205.         // 对每一条事务进行处理  
    206.         for (String trans : data) {  
    207.   
    208.             // 得出每条事件中涉及的元素项  
    209.             String[] items = trans.trim().split(" ");  
    210.             // 对items中的元素按其在频繁1项集中出现次数排序  
    211.             items = sortItem(items, f1Items);  
    212.             // 把items的值加入到fpTree中  
    213.             subItems = fpTree.getNextItem();  
    214.   
    215.             if (subItems.size() == 0) {  
    216.                 this.addLeaf(fpTree, items, f1Items);  
    217.             } else {  
    218.                 Item temp = null;  
    219.   
    220.                 for (int i = 0; i < items.length; i++) {  
    221.                     int j = 0;  
    222.                     int size = subItems.size();  
    223.                     for (; j < subItems.size(); j++) {  
    224.                         if (subItems.get(j).getValue().equals(items[i])) {  
    225.                             temp = subItems.get(j);  
    226.                             temp.addCounter();  
    227.                             subItems = temp.getNextItem();  
    228.                             break;  
    229.                         }  
    230.                     }  
    231.   
    232.                     if (j == size) {  
    233.                         if (temp == null) {  
    234.                             this.addLeaf(fpTree, Arrays.copyOfRange(items, i,  
    235.                                     items.length), f1Items);  
    236.                         } else {  
    237.                             this.addLeaf(temp, Arrays.copyOfRange(items, i,  
    238.                                     items.length), f1Items);  
    239.                         }  
    240.                         break;  
    241.                     }  
    242.                 }  
    243.             }  
    244.   
    245.         }  
    246.         return fpTree;  
    247.     }  
    248.       
    249.     /** 
    250.      * 3.1 对元素数组根据其在f1中出面的频繁进行排序 
    251.      *  
    252.      * @param items 
    253.      * @return 
    254.      */  
    255.     public String[] sortItem(String[] items, Item[] f1Items) {  
    256.           
    257.         String[] temp = new String[f1Items.length];  
    258.         int i;  
    259.         for (String item : items) {  
    260.             for (i = 0; i < f1Items.length; i++) {  
    261.                 if (item.equals(f1Items[i].getValue())) {  
    262.                     temp[i] = item;  
    263.                 }  
    264.             }  
    265.         }  
    266.         List<String> list = new ArrayList<String>();  
    267.         int j = 0;  
    268.         for (i = 0; i < temp.length; i++) {  
    269.             if (temp[i] != null) {  
    270.                 list.add(temp[i]);  
    271.             }  
    272.         }  
    273.           
    274.         return list.toArray(new String[0]);  
    275.     }  
    276.   
    277.     /** 
    278.      * 3.2 给FPTree的节点添加子节点序列 
    279.      *  
    280.      * @param preItem 
    281.      * @param items 
    282.      */  
    283.     public void addLeaf(Item preItem, String[] items, Item[] f1Items) {  
    284.         if (items.length > 0) {  
    285.             Item item = new Item(items[0]);  
    286.             item.setCounter(1);  
    287.             item.setPreItem(preItem);  
    288.             preItem.addNextItem(item);  
    289.   
    290.             for (Item i : f1Items) {  
    291.                 if (i.getValue().equals(items[0])) {  
    292.                     Item temp = i;  
    293.                     while (temp.getSibling() != null) {  
    294.                         temp = temp.getSibling();  
    295.                     }  
    296.                     temp.setSibling(item);  
    297.                     break;  
    298.                 }  
    299.             }  
    300.             if (items.length > 1) {  
    301.                 addLeaf(item, Arrays.copyOfRange(items, 1, items.length),  
    302.                         f1Items);  
    303.             }  
    304.         }  
    305.   
    306.     }  
    307.   
    308.     // 4.生成条件模式基  
    309.     public Map<String, List<String>> buildCondBase(Item[] f1Items) {  
    310.   
    311.         Item item = null// 横向处理时的当前节点  
    312.         Item preItem = null// 横向处理的当前节点对应的纵向节点  
    313.         int counter = 0;  
    314.         StringBuffer data;  
    315.   
    316.         Map<String, List<String>> condBaseMap = new HashMap<String, List<String>>();  
    317.         List<String> conditionBase; // 条件模式基  
    318.         // 逆向遍历频繁1项集(但不需处理其第一项)  
    319.         for (int i = f1Items.length - 1; i > 0; i--) {  
    320.   
    321.             conditionBase = new ArrayList<String>();  
    322.             item = f1Items[i].getSibling();  
    323.             while (item != null) { // 横向处理  
    324.   
    325.                 counter = item.getCounter();  
    326.                 preItem = item.getPreItem();  
    327.                 data = new StringBuffer();  
    328.                 while (preItem.getValue() != null) { // 纵向处理  
    329.                     data.append(preItem.getValue() + " ");  
    330.                     preItem = preItem.getPreItem();  
    331.                 }  
    332.                 for (int j = 0; j < counter; j++) {  
    333.                     if (data.toString().trim() != ""  
    334.                             && data.toString().trim().length() > 0) {  
    335.                         conditionBase.add(data.toString().trim());  
    336.                     }  
    337.                 }  
    338.                 item = item.getSibling();  
    339.             }  
    340.             condBaseMap.put(f1Items[i].getValue(), conditionBase);  
    341.         }  
    342.   
    343.         return condBaseMap;  
    344.     }  
    345.   
    346.     // 5.生成条件FP树  
    347.     public Map<String, Item> buildCondFPTree(  
    348.             Map<String, List<String>> condBaseMap) {  
    349.   
    350.         Map<String, Item> condFPTreeMap = new HashMap<String, Item>();  
    351.         List<String> condBase;  
    352.         Item condFPTree;  
    353.         Set<String> items = condBaseMap.keySet();  
    354.         for (String item : items) {  
    355.             condBase = condBaseMap.get(item);  
    356.             condFPTree = this  
    357.                     .buildFPTree(condBase, this.buildF1Items(condBase));  
    358.             // 删除condFPTree树中节点出现次数少于最小支持度的点  
    359.             this.delLTminSup(condFPTree);  
    360.             condFPTreeMap.put(item, condFPTree);  
    361.         }  
    362.   
    363.         return condFPTreeMap;  
    364.     }  
    365.   
    366.     /** 
    367.      * 5.1  删除树中节点计数小于最小支持度的节点 
    368.      *  
    369.      * @param root 
    370.      */  
    371.     public void delLTminSup(Item root) {  
    372.         List<Item> subItems = root.getNextItem();  
    373.         if (subItems.size() != 0) {  
    374.             for (int i = 0; i < subItems.size(); i++) {  
    375.                 if (subItems.get(i).getCounter() < this.minSup) {  
    376.                     subItems.remove(i);  
    377.                 } else {  
    378.                     delLTminSup(subItems.get(i));  
    379.                 }  
    380.             }  
    381.         }  
    382.     }  
    383.   
    384.     /** 
    385.      * 6.产生频繁模式 根据前面生成的条件FP树,分别产生相应元素相关的频繁模式 
    386.      */  
    387.     public Map<String,List<List<String>>> fpGrowth(Map<String, Item> condFPTreeMap) {  
    388.           
    389.         List<List<String>> result;  
    390.         Map<String, List<List<String>>> resultMap = new HashMap<String, List<List<String>>>();  
    391.         Set<String> items = condFPTreeMap.keySet();  
    392.         Item condFPTree = null;  
    393.         List<String> pathList; // 一个条件fp树中所有的路径  
    394.         List<String> stack = new ArrayList<String>();  
    395.       
    396.         for (String item : items) {  
    397.               
    398.             pathList = new ArrayList<String>();  
    399.             condFPTree = condFPTreeMap.get(item);  
    400.             buildPath(stack, condFPTree, pathList);  
    401.               
    402.             for(String str : pathList){  
    403.                 result = new ArrayList<List<String>>();  
    404.                 if(str.trim().length() != 0){  
    405.                     String[] temp = str.trim().split(" ");  
    406.                     List<String> nodeList = new ArrayList<String>();  
    407.                     for(String t : temp){  
    408.                         nodeList.add(t);  
    409.                     }  
    410.                       
    411.                     buildSubSet(nodeList, result);  
    412.                       
    413.                     if(resultMap.get(item) == null){  
    414.                         resultMap.put(item, result);  
    415.                     }else{  
    416.                         List<List<String>> list = resultMap.get(item);  
    417.                         forint  i = 0; i < result.size(); i++){  
    418.                             list.add(result.get(i));  
    419.                         }  
    420.                         resultMap.put(item, list);  
    421.                     }  
    422.                 }     
    423.             }  
    424.         }  
    425.           
    426.         return resultMap;  
    427.     }  
    428.   
    429.     // 6.1 生成树的每一条路径  
    430.     public void buildPath(List<String> stack, Item root, List<String> pathList) {  
    431.   
    432.         if (root != null) {  
    433.             stack.add(root.getValue());  
    434.             if (root.getNextItem().size() == 0) {  
    435.                 changeToPath(stack, pathList); // 把值栈中的值转化为路径  
    436.             } else {  
    437.                 List<Item> items = root.getNextItem();  
    438.                 for (int i = 0; i < items.size(); i++) {  
    439.                     buildPath(stack, items.get(i), pathList);  
    440.                 }  
    441.             }  
    442.             stack.remove(stack.size() - 1);  
    443.         }  
    444.     }  
    445.   
    446.     /** 
    447.      * 6.1.1 把值栈中的值转化为路径 
    448.      *  
    449.      * @param path 
    450.      * @param pathList 
    451.      */  
    452.     public void changeToPath(List<String> path, List<String> pathList) {  
    453.         StringBuffer sb = new StringBuffer();  
    454.         for (int i = 0; i < path.size(); i++) {  
    455.             if (path.get(i) != null) {  
    456.                 sb.append(path.get(i) + " ");  
    457.             }  
    458.   
    459.         }  
    460.         pathList.add(sb.toString().trim());  
    461.   
    462.     }  
    463.     /** 
    464.      * 6.2 生成子集 
    465.      * @param sourceSet 
    466.      * @param result 
    467.      */  
    468.     public void buildSubSet(List<String> sourceSet, List<List<String>> result) {  
    469.   
    470.         if (sourceSet.size() == 1) {  
    471.             List<String> set = new ArrayList<String>();  
    472.             set.add(sourceSet.get(0));  
    473.             result.add(set);  
    474.         } else if (sourceSet.size() > 1) {  
    475.   
    476.             buildSubSet(sourceSet.subList(0, sourceSet.size() - 1), result);  
    477.             int size = result.size();  
    478.   
    479.             List<String> single = new ArrayList<String>();  
    480.             single.add(sourceSet.get(sourceSet.size() - 1));  
    481.             result.add(single);  
    482.   
    483.             List<String> clone;  
    484.             for (int i = 0; i < size; i++) {  
    485.                 clone = new ArrayList<String>();  
    486.                 for (String str : result.get(i)) {  
    487.                     clone.add(str);  
    488.                 }  
    489.                 clone.add(sourceSet.get(sourceSet.size() - 1));  
    490.   
    491.                 result.add(clone);  
    492.             }  
    493.         }  
    494.     }  
    495.     public void setMinSup(int minSup) {  
    496.         this.minSup = minSup;  
    497.     }  

  • 相关阅读:
    ubuntu下如何安装hg(mercurial)?
    vi启动时报错:YouCompleteMe unavailable: requires Vim 7.4.1578+如何处理?
    linux中如何配置vim的别名为vi?
    linux shell中如何让$就表示为$呢?
    redhat 7.6下如何更新YUM源(仓库)?
    redhat下如何查看red hat版本号?
    javascript快速入门11--正则表达式
    javascript快速入门10--运算符,语句
    javascript快速入门9--引用类型
    javascript快速入门7--ECMAScript语法基础
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205272.html
Copyright © 2020-2023  润新知