Program.cs
上传用户:zijide88
上传日期:2022-07-10
资源大小:77k
文件大小:9k
源码类别:

数据库系统

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5.  
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string file = @"c:test.csv";
  11.             string sup = "2";
  12.             if (args.Length > 0) {
  13.                 file = args[0];
  14.                 
  15.             }
  16.             if (args.Length== 2)
  17.             {
  18.                 sup= args[1];
  19.             }
  20.             double support = double.Parse(sup);
  21.             CSVReader cr = new CSVReader();
  22.             ItemSet data = cr.Read(file);
  23.             
  24.             Program p = new Program();
  25.             ItemSet a= p.apriori( data, support);
  26.             for (int i = 0; i < a.Count;i++ )
  27.             {
  28.                 ItemSet cur = (ItemSet)a[i];
  29.                 for (int j = 0; j < cur.Count; j++) {
  30.                     ItemSet now = (ItemSet)cur[j];
  31.                     foreach (DataItem item in now)
  32.                     {
  33.                         Console.Write("编号" + item.Id + ":" + item.ItemName+"  ");
  34.                         
  35.                     }
  36.                     Console.WriteLine("  支持度:"+now.ICount);
  37.                 }
  38.             }
  39.             Console.Read();
  40.         }
  41.         private ItemSet FindOneColSet(ItemSet data, double support)
  42.         {
  43.             ItemSet cur=null;
  44.             ItemSet result = new ItemSet();
  45.             ItemSet set=null;
  46.             ItemSet newset=null;
  47.             DataItem cd=null;
  48.              DataItem td=null;
  49.              bool flag = true;
  50.             for (int i = 0; i < data.Count; i++) {
  51.                 cur = (ItemSet)data[i];
  52.                 for (int j = 0; j < cur.Count; j++) {
  53.                     cd = (DataItem)cur[j];
  54.                     
  55.                     for (int n = 0; n < result.Count; n++) {
  56.                         set = (ItemSet)result[n];
  57.                         td= (DataItem)set[0];
  58.                         if (cd.Id == td.Id)
  59.                         {
  60.                             set.ICount++;
  61.                             flag = false;
  62.                             break;
  63.                                                               
  64.                         }
  65.                         flag=true;
  66.                     }
  67.                     if (flag) {
  68.                         newset = new ItemSet();
  69.                         newset.Add(cd);
  70.                         result.Add(newset);
  71.                         newset.ICount = 1;
  72.                     
  73.                     
  74.                     }
  75.                    
  76.                     
  77.                 }
  78.                 
  79.             }
  80.             ItemSet finalResult = new ItemSet();
  81.             for (int i = 0; i < result.Count; i++)
  82.             {
  83.                 ItemSet con = (ItemSet)result[i];
  84.                 if (con.ICount >= support)
  85.                 {
  86.                     finalResult.Add(con);
  87.                 }
  88.             }
  89.             //finalResult.Sort();
  90.             return finalResult;
  91.   
  92.         
  93.         }
  94.         private ItemSet apriori(  ItemSet data, double support)
  95.         {
  96.             ItemSet result = new ItemSet();
  97.             ItemSet li = new ItemSet();
  98.             ItemSet conList = new ItemSet();
  99.             ItemSet subConList = new ItemSet();
  100.             ItemSet subDataList = new ItemSet();
  101.             int k = 2;
  102.             li.Add( new ItemSet());
  103.             li.Add(this.FindOneColSet(data,support));
  104.             
  105.             while (((ItemSet)li[k-1]).Count != 0)
  106.             {
  107.                 conList = AprioriGenerate((ItemSet)li[k - 1],k-1, support);
  108.                 for (int i = 0; i < data.Count; i++)
  109.                 {
  110.                     subDataList = SubSet((ItemSet)data[i], k);
  111.                     for (int j = 0; j < subDataList.Count; j++)
  112.                     {
  113.                         for (int n = 0; n < conList.Count; n++)
  114.                         {
  115.                             ((ItemSet)subDataList[j]).Sort();
  116.                             ((ItemSet)conList[n]).Sort();
  117.                             if (((ItemSet)subDataList[j]).Equals(conList[n]))
  118.                             {
  119.                                 ((ItemSet)conList[n]).ICount++;
  120.                             }
  121.                         }
  122.                       
  123.                     }
  124.                 }
  125.                 li.Add(new ItemSet());
  126.                 for (int i = 0; i < conList.Count; i++)
  127.                 {
  128.                     ItemSet con = (ItemSet)conList[i];
  129.                     if (con.ICount >= support)
  130.                     {
  131.                          
  132.                         ((ItemSet)li[k]).Add(con);
  133.                     }
  134.                 }
  135.               
  136.                 k++;
  137.             }
  138.             for (int i = 0; i < li.Count; i++)
  139.             {
  140.                 
  141.                 result.Add(li[i]);
  142.                   
  143.             }
  144.             return result;
  145.         }
  146.         private ItemSet AprioriGenerate(ItemSet li,int k, double support)
  147.         {
  148.             ItemSet curList = null;
  149.             ItemSet durList = null;
  150.             ItemSet candi = null;
  151.             ItemSet result = new ItemSet();
  152.             for (int i = 0; i < li.Count; i++)
  153.             {
  154.                 for (int j = 0; j < li.Count; j++)
  155.                 {
  156.                     bool flag = true ;
  157.                     curList = (ItemSet)li[i];
  158.                     durList = (ItemSet)li[j];
  159.                     for (int n = 2; n < k; n++)
  160.                     {
  161.                         if (((DataItem)curList[n - 2]).Id == ((DataItem)durList[n - 2]).Id)
  162.                         {
  163.                             flag = true;
  164.                         }
  165.                         else {
  166.                             break;
  167.                             flag = false;
  168.                            
  169.                            
  170.                         }
  171.                       
  172.                     }
  173.                     if (flag && ((DataItem)curList[k - 1] ).Id< ((DataItem)durList[k - 1]).Id)
  174.                     {
  175.                         flag = true;
  176.                     }
  177.                     else {
  178.                         flag = false;
  179.                     }
  180.                     if (flag)
  181.                     {
  182.                         candi = new ItemSet();
  183.                         
  184.                         for(int m=0;m<k;m++){
  185.                             candi.Add(durList[m]);
  186.                         
  187.                         }
  188.                         candi.Add(curList[k-1]);
  189.                         if (HasInFrequentSubset(candi, li,k))
  190.                         {
  191.                             candi.Clear();
  192.                         }
  193.                         else
  194.                         {
  195.                             result.Add(candi);
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.             return result;
  201.         }
  202.         
  203.         private bool HasInFrequentSubset(ItemSet candidate, ItemSet li,int k)
  204.         {
  205.             ItemSet subSet = SubSet(candidate,k);
  206.             ItemSet curList = null;
  207.             ItemSet liCurList = null;
  208.         
  209.             for (int i = 0; i < subSet.Count; i++)
  210.             {
  211.                 curList = (ItemSet)subSet[i];
  212.                 for (int j = 0; j < li.Count; j++)
  213.                 {
  214.                     liCurList = (ItemSet)li[j];
  215.                     if (liCurList.Equals(curList))
  216.                     {
  217.                        return false;
  218.                     }
  219.                    
  220.                 }
  221.             }
  222.             return true;;
  223.         }
  224.         //划分子集
  225.         private ItemSet SubSet(ItemSet set)
  226.         {
  227.             ItemSet subSet = new ItemSet();
  228.             ItemSet itemSet = new ItemSet();
  229.             //移位求2n次访
  230.             int num = 1 << set.Count;
  231.             int bit;
  232.             int mask = 0; ;
  233.             for (int i = 0; i < num; i++)
  234.             {
  235.                 itemSet = new ItemSet();
  236.                 for (int j = 0; j < set.Count; j++)
  237.                 {
  238.                     //mask与i可以得出某位是否为零
  239.                     mask = 1 << j;
  240.                     bit = i & mask;
  241.                     if (bit > 0)
  242.                     {
  243.                         itemSet.Add(set[j]);
  244.                     }
  245.                 }
  246.                 if (itemSet.Count > 0)
  247.                 {
  248.                     subSet.Add(itemSet);
  249.                 }
  250.             }
  251.             return subSet;
  252.         }
  253.         //划分子集
  254.         private ItemSet SubSet(ItemSet set, int t)
  255.         {
  256.             ItemSet subSet = new ItemSet();
  257.             ItemSet itemSet = new ItemSet();
  258.             //移位求2n次访
  259.             int num = 1 << set.Count;
  260.             int bit;
  261.             int mask = 0; ;
  262.             for (int i = 0; i < num; i++)
  263.             {
  264.                 itemSet = new ItemSet();
  265.                 for (int j = 0; j < set.Count; j++)
  266.                 {
  267.                     //mask与i可以得出某位是否为零
  268.                     mask = 1 << j;
  269.                     bit = i & mask;
  270.                     if (bit > 0)
  271.                     {
  272.                         itemSet.Add(set[j]);
  273.                     }
  274.                 }
  275.                 if (itemSet.Count == t)
  276.                 {
  277.                     subSet.Add(itemSet);
  278.                 }
  279.             }
  280.             return subSet;
  281.         }
  282.     }
  283.