Source Code : C # implementation Apriori algorithm (source code)

C # implementation Apriori algorithm (source code)

  1. /*Program.cs*/
  2. using System; 
  3. using System.Collections.Generic; 
  4. using System.Text; 
  5. using System.Data; 
  6. using System.Data.SqlClient; 
  7. using System.Text.RegularExpressions; 
  8. using System.Collections; 
  9. namespace testApriori1 
  10. { 
  11.  publicclass TestApriori 
  12.  { 
  13.  staticvoid Main(string[] args) 
  14.  { 
  15.  Console.WriteLine(DateTime.Now); 
  16.  ArrayList D = GetEventsFromDB();//
  17.  ArrayList I = GetItems1FromDB();//
  18.  float s = 0.01f;//
  19.  List L = new List();//
  20.  L = Apriori(D, I, s); 
  21.  for (int i = 0; i < L.Count; i++) 
  22.  { 
  23.  Console.WriteLine(L[i].Items); 
  24.  Console.WriteLine(L[i].Sup); 
  25.  } 
  26.  Console.WriteLine(DateTime.Now); 
  27.  Console.Read(); 
  28.  } 
  29.  #region-----Apriori-----
  30.  /// 
  31.  /// Apriori
  32.  /// 
  33.  /// 
  34.  /// 
  35.  /// 
  36.  /// 
  37.  static List Apriori(ArrayList D,ArrayList I,float sup) 
  38.  { 
  39.  List L = new List();//
  40.  if (I.Count == 0) return L; 
  41.  else
  42.  { 
  43.  int[] Icount = newint[I.Count];//,0
  44.  ArrayList Ifrequent = new ArrayList();//
  45.  //?
  46.  Regex r=new Regex(","); 
  47.  for (int i = 0; i < D.Count; i++) 
  48.  { 
  49.  string[] subD=r.Split(D[i].ToString()); 
  50.  for (int j = 0; j < I.Count;j++ ) 
  51.  { 
  52.  string[] subI = r.Split(I[j].ToString()); 
  53.  bool subIInsubD=true; 
  54.  for(int m=0;m
  55.  { 
  56.  bool subImInsubD=false; 
  57.  for(int n=0;n
  58.  if (subI[m] == subD[n]) 
  59.  { 
  60.  subImInsubD = true; 
  61.  continue; 
  62.  } 
  63.  if(subImInsubD==false) subIInsubD=false; 
  64.  } 
  65.  if(subIInsubD==true) Icount[j]++; 
  66.  } 
  67.  } 
  68.  //L
  69.  for (int i = 0; i < Icount.Length;i++ ) 
  70.  { 
  71.  if (Icount[i] >= sup * D.Count) 
  72.  { 
  73.  Ifrequent.Add(I[i]); 
  74.  ItemSet iSet = new ItemSet(); 
  75.  iSet.Items=I[i].ToString(); 
  76.  iSet.Sup = Icount[i]; 
  77.  L.Add(iSet); 
  78.  } 
  79.  } 
  80.  I.Clear(); 
  81.  I = AprioriGen(Ifrequent); 
  82.  L.AddRange(Apriori(D, I, sup)); 
  83.  return L; 
  84.  } 
  85.  } 
  86.  #endregion-----Apriori-----
  87.  #region-------Apriori-gen---------
  88.  /// 
  89.  /// Apriori-gen
  90.  /// 
  91.  /// 
  92.  /// 
  93.  static ArrayList AprioriGen(ArrayList L) 
  94.  { 
  95.  ArrayList Lk=new ArrayList(); 
  96.  Regex r=new Regex(","); 
  97.  for(int i=0;i
  98.  { 
  99.  string[] subL1 = r.Split(L[i].ToString()); 
  100.  for(int j=i+1;j
  101.  { 
  102.  string[] subL2 = r.Split(L[j].ToString()); 
  103.  //Ltemp
  104.  string temp = L[j].ToString();//
  105.  for(int m=0;m
  106.  { 
  107.  bool subL1mInsubL2=false; 
  108.  for(int n=0;n
  109.  { 
  110.  if (subL1[m] == subL2[n]) subL1mInsubL2 = true; 
  111.  } 
  112.  if (subL1mInsubL2 == false) temp = temp + "," + subL1[m]; 
  113.  } 
  114.  //temp?L?+1temp
  115.  string[] subTemp = r.Split(temp); 
  116.  if (subTemp.Length == subL1.Length + 1) 
  117.  { 
  118.  bool isExists = false; 
  119.  for (int m = 0; m < Lk.Count; m++) 
  120.  { 
  121.  bool isContained = true; 
  122.  for (int n = 0; n < subTemp.Length; n++) 
  123.  { 
  124.  if (!Lk[m].ToString().Contains(subTemp[n])) isContained=false; 
  125.  } 
  126.  if (isContained == true) isExists = true; 
  127.  } 
  128.  if(isExists==false) Lk.Add(temp); 
  129.  } 
  130.  } 
  131.  } 
  132.  return Lk; 
  133.  } 
  134.  #endregion-------Apriori-gen---------
  135.  #region----------------
  136.  /// 
  137.  /// 
  138.  /// 
  139.  /// 
  140.  static ArrayList GetItems1FromDB() 
  141.  { 
  142.  string commandString = "select distinct Model from dbo.vAssocSeqLineItems"; 
  143.  DataSet ds = ExcuteDataSetByCommandString(commandString); 
  144.  int countItems1 = 0; 
  145.  countItems1 = ds.Tables[0].Rows.Count; 
  146.  ArrayList Items1 = new ArrayList(); 
  147.  for (int i = 0; i < countItems1; i++) 
  148.  Items1.Add(ds.Tables[0].Rows[i]["Model"].ToString()); 
  149.  return Items1; 
  150.  } 
  151.  #endregion----------------
  152.  #region----------------
  153.  /// 
  154.  /// 
  155.  /// 
  156.  /// 
  157.  static ArrayList GetEventsFromDB() 
  158.  { 
  159.  string commandString="select count(OrderNumber) from vAssocSeqOrders"; 
  160.  DataSet ds=ExcuteDataSetByCommandString(commandString); 
  161.  int countEvent = Convert.ToInt32(ds.Tables[0].Rows[0][0]);//
  162.  ArrayList events=new ArrayList(); 
  163.  string temp=null; 
  164.  string orderNumber=null;//OrderNumber,Order
  165.  //
  166.  commandString = "select OrderNumber,Model from vAssocSeqLineItems"; 
  167.  ds = ExcuteDataSetByCommandString(commandString); 
  168.  for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
  169.  { 
  170.  if (orderNumber == null)//
  171.  { 
  172.  orderNumber = ds.Tables[0].Rows[i]["OrderNumber"].ToString(); 
  173.  temp = ds.Tables[0].Rows[i]["Model"].ToString(); 
  174.  } 
  175.  elseif (orderNumber == ds.Tables[0].Rows[i]["OrderNumber"].ToString())//Order
  176.  { 
  177.  temp = temp +","+ ds.Tables[0].Rows[i]["Model"]; 
  178.  } 
  179.  else//Order
  180.  { 
  181.  events.Add(temp); 
  182.  orderNumber = ds.Tables[0].Rows[i]["OrderNumber"].ToString(); 
  183.  temp=ds.Tables[0].Rows[i]["Model"].ToString(); 
  184.  } 
  185.  if (i == ds.Tables[0].Rows.Count - 1) events.Add(temp); 
  186.  } 
  187.  return events; 
  188.  } 
  189.  #endregion----------------
  190.  #region-----commandString------
  191.  /// 
  192.  /// commandString
  193.  /// 
  194.  /// 
  195.  /// 
  196.  static DataSet ExcuteDataSetByCommandString(string commandString) 
  197.  { 
  198.  string connectionString = "server=.;database=AdventureWorksDW;trusted_connection=true"; 
  199.  SqlConnection sqlCon = new SqlConnection(connectionString); 
  200.  sqlCon.Open(); 
  201.  SqlCommand sqlCmd = new SqlCommand(); 
  202.  sqlCmd.Connection = sqlCon; 
  203.  sqlCmd.CommandText = commandString; 
  204.  DataSet ds = new DataSet(); 
  205.  SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd); 
  206.  sqlAdapter.Fill(ds); 
  207.  sqlCon.Close(); 
  208.  return ds; 
  209.  } 
  210.  #endregion-----commandString------
  211.  } 
  212. }