Professional Documents
Culture Documents
using
using
using
using
System;
System.Collections.Generic;
System.Text;
System.Collections;
System.IO;
class Program
{
static void Main(string[] args)
{
string file = @"big_test.csv";
string sup = "6";
if (args.Length > 0)
{
file = args[0];
}
if (args.Length == 2)
{
sup = args[1];
}
double support = double.Parse(sup);
CSVReader cr = new CSVReader();
ItemSet data = cr.Read(file);
}
ItemSet finalResult = new ItemSet();
for (int i = 0; i < result.Count; i++)
{
ItemSet con = (ItemSet)result.arr[i];
if (con.ICount >= support)
{
finalResult.Add(con);
}
}
//finalResult.Sort();
return finalResult;
}
private ItemSet apriori(ItemSet data, double support)
{
ItemSet result = new ItemSet();
ItemSet li = new ItemSet();
ItemSet conList = new ItemSet();
ItemSet subConList = new ItemSet();
ItemSet subDataList = new ItemSet();
ItemSet CurList = null;
ItemSet subList = null;
int k = 2;
li.Add(new ItemSet());
li.Add(this.FindOneColSet(data, support));
while (((ItemSet)li.arr[k - 1]).Count != 0)
{
Console.WriteLine(k - 1);
conList = AprioriGenerate((ItemSet)li.arr[k - 1], k - 1, support);
for (int i = 0; i < data.Count; i++)
{
subDataList = SubSet((ItemSet)data.arr[i], k);
for (int j = 0; j < subDataList.Count; j++)
{
subList = (ItemSet)subDataList.arr[j];
for (int n = 0; n < conList.Count; n++)
{
((ItemSet)subDataList.arr[j]).Sort();
((ItemSet)conList.arr[n]).Sort();
CurList = (ItemSet)conList.arr[n];
if (subList.Equals(CurList))
{
((ItemSet)conList.arr[n]).ICount++;
}
}
}
}
li.Add(new ItemSet());
for (int i = 0; i < conList.Count; i++)
{
ItemSet con = (ItemSet)conList.arr[i];
if (con.ICount >= support)
{
((ItemSet)li.arr[k]).Add(con);
}
}
k++;
}
//for (int j = 0; j < li.Count; j++)
//{
//
for (int h = 0; h < li.Count; h++)
//
{
//
if (((ItemSet)li.arr[j]).Equals((ItemSet)li.arr[h]))
//
{
//
li.arr.RemoveAt(j);
//
li.Count = li.arr.Count;
//
}
//
}
//}
for (int i = 0; i < li.Count; i++)
{
result.Add((ItemSet)li.arr[i]);
}
return result;
}
private ItemSet AprioriGenerate(ItemSet li, int k, double support)
{
ItemSet curList = null;
ItemSet durList = null;
ItemSet candi = null;
ItemSet result = new ItemSet();
for (int i = 0; i < li.Count; i++)
{
for (int j = 0; j < li.Count; j++)
{
bool flag = true;
curList = (ItemSet)li.arr[i];
durList = (ItemSet)li.arr[j];
for (int n = 2; n < k; n++)
{
if (((DataItem)curList.arr[n - 2]).Id == ((DataItem)durList.
arr[n - 2]).Id)
{
flag = true;
}
else
{
flag = false;
break;
}
}
if (flag && ((DataItem)curList.arr[k - 1]).Id < ((DataItem)durLi
st.arr[k - 1]).Id)
{
flag = true;
}
else
{
flag = false;
}
if (flag)
{
candi = new ItemSet();
for (int m = 0; m < k; m++)
{
candi.Add((DataItem)durList.arr[m]);
}
candi.Add((DataItem)curList.arr[k - 1]);
}
return true; ;
}
//????
private ItemSet SubSet(ItemSet set)
{
ItemSet subSet = new ItemSet();
ItemSet itemSet = new ItemSet();
//???2n??
int num = 1 << set.Count;
int bit;
int mask = 0; ;
for (int i = 0; i < num; i++)
{
itemSet = new ItemSet();
for (int j = 0; j < set.Count; j++)
{
//mask?i??????????
mask = 1 << j;
bit = i & mask;
if (bit > 0)
{
itemSet.Add((ItemSet)set.arr[j]);
}
}
if (itemSet.Count > 0)
{
subSet.Add(itemSet);
}
}
return subSet;
}
//????
private ItemSet SubSet(ItemSet set, int t)
{
ItemSet subSet = new ItemSet();
ItemSet itemSet = new ItemSet();
//???2n??
int num = 1 << set.Count;
int bit;
int mask = 0; ;
for (int i = 0; i < num; i++)
{
itemSet = new ItemSet();
for (int j = 0; j < set.Count; j++)
{
//mask?i??????????
mask = 1 << j;
bit = i & mask;
if (bit > 0)
{
itemSet.Add((DataItem)set.arr[j]);
}
}
if (itemSet.Count == t)
{
subSet.Add(itemSet);
}
}
return subSet;
}
}
public class DataItem
{
public int Id;
public string ItemName;
public void Add(string item,int id)
{
ItemName=item;
Id=id;
}
}
public class ItemSet
{
public int Count=0;
public int ICount=0;
public ArrayList arr = new ArrayList();
public void Add(ItemSet input)
{
arr.Add(input);
Count++;
}
public void Add(string input)
{
arr.Add(input);
Count++;
}
public void Add(DataItem input)
{
arr.Add(input);
Count++;
}
public void Sort()
{
DataItem temp = null;
for (int i = 0; i < this.Count-1; i++)
{
tcn++;
items = new ItemSet();
while (Line.IndexOf(",") != -1)
{
set = new DataItem();
temp = Line.Substring(0, Line.IndexOf(","));
Line = Line.Substring(Line.IndexOf(",") + 1);
set.Add(temp,id);
items.Add(set);
id++;
start = Line.IndexOf(",");
}
temp = Line;
set.Add(temp,id);
items.Add(set);
id = 0;
temp = "";
start = 0;
General.Add(items);
}
//General.Add(items);
Console.WriteLine(General.Count);
return General;
}
}