📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Apriori
{
//事务
struct trans
{
public string tID;
public ArrayList items;
}
//项集和支持度计数
struct itemAndSup
{
public ArrayList items;
public int sup;
}
public partial class Form1 : Form
{
private ArrayList tData = new ArrayList(); //事务数据
private int minSup = 2; //最小支持度计数阀值
private ArrayList C0 = new ArrayList(); //L的超集
private ArrayList L0 = new ArrayList(); //频繁k项集
private int step; //已完成步骤数
private bool finish; //算法是否完成
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Reset();
//test1();
}
private void Reset()
{
tData.Clear();
C0.Clear();
L0.Clear();
this.TDataView.Items.Clear();
this.CResultView.Items.Clear();
this.LResultView.Items.Clear();
this.TDataView.Items.Add("TID\t商品ID的列表\n");
this.MinSupTextBox.Text = minSup.ToString();
step = 0;
finish = false;
}
private void AddItem_Click(object sender, EventArgs e)
{
Trans addTrans = new Trans();
if (addTrans.ShowDialog() == DialogResult.OK)
{
trans t = new trans();
t.tID = addTrans.GetTID();
t.items = addTrans.GetItemList();
AddItemToDataView(t);
tData.Add(t);
}
}
private void DeleteItem_Click(object sender, EventArgs e)
{
if (this.TDataView.SelectedIndex == 0)
return;
tData.RemoveAt(this.TDataView.SelectedIndex - 1);
this.TDataView.Items.RemoveAt(this.TDataView.SelectedIndex);
}
private void Next_Click(object sender, EventArgs e)
{
if (finish == true)
{
this.Next.Text = "计算下一步";
Reset();
return;
}
ArrayList OldL = new ArrayList(L0);
//增加步骤计数,用来决定计算C或者是L。
step++;
//计算L
if (step % 2 == 1)
{
//找出频繁1项集L1
if (step == 1)
{
for (int i = 0; i < tData.Count; i++)
{
trans t = (trans)tData[i];
for (int j = 0; j < t.items.Count; j++)
{
bool flag = true;
for (int k = 0; k < L0.Count; k++)
{
if (((itemAndSup)L0[k]).items[0] == t.items[j])
{
flag = false;
break;
}
}
if (flag == false)
continue;
ArrayList items = new ArrayList();
items.Add(t.items[j]);
int sup = FindItemSup(items);
if (sup >= minSup)
{
itemAndSup temp = new itemAndSup();
temp.sup = sup;
temp.items = items;
L0.Add(temp);
}
}
}
}
//通过Ck来确定Lk
else
{
L0.Clear();
for (int i = 0; i < C0.Count; i++)
{
itemAndSup temp = (itemAndSup)C0[i];
if (temp.sup >= minSup)
L0.Add(temp);
}
}
//对L0排序
{ }
//更新L的视图
{
if (L0.Count != 0)
{
this.LResultView.Items.Clear();
this.LResultView.Items.Add("项集\t支持度计数\n");
for (int i = 0; i < L0.Count; i++)
{
ArrayList items = ((itemAndSup)L0[i]).items;
int sup = ((itemAndSup)L0[i]).sup;
string LResultLine = "";
for (int j = 0; j < items.Count; j++)
{
LResultLine = LResultLine + items[j].ToString() + ",";
}
LResultLine = LResultLine + "\t" + sup + "\n";
this.LResultView.Items.Add(LResultLine);
}
}
else
{
this.LResultView.Items.Clear();
this.LResultView.Items.Add("项集\t支持度计数\n");
for (int i = 0; i < OldL.Count; i++)
{
ArrayList items = ((itemAndSup)OldL[i]).items;
int sup = ((itemAndSup)OldL[i]).sup;
string LResultLine = "";
for (int j = 0; j < items.Count; j++)
{
LResultLine = LResultLine + items[j].ToString() + ",";
}
LResultLine = LResultLine + "\t" + sup + "\n";
this.LResultView.Items.Add(LResultLine);
}
}
}
//更新说明
{
if (L0.Count != 0)
this.Msg.Text = "比较候选支持度计数与最小支持度计数";
else
{
this.Msg.Text = "由于L为空,算法终止";
this.Next.Text = "完成(重新开始)";
finish = true;
}
}
}
//计算C
else
{
//通过将Lk-1与自身连接产生Ck
C0.Clear();
for (int i = 0; i < L0.Count; i++)
{
ArrayList items0 = ((itemAndSup)L0[i]).items;
ArrayList addItem = new ArrayList();
for (int j = 0; j < L0.Count; j++)
{
if (j == i)
continue;
ArrayList items1 = ((itemAndSup)L0[j]).items;
for (int k = 0; k < items1.Count; k++)
{
/*
if (items0.Contains(items1[k]))
continue;
*/
//改进
if (((string)items1[k]).CompareTo((string)items0[items0.Count - 1]) <= 0)
continue;
if (addItem.Contains(items1[k]))
continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -