?? frequent1set.java
字號:
import java.util.*;
public class frequent1set {
/*
* 連接步:對每一個子集,若前面的元素均相同而最后一個不相同時,進行連接
*/
public static ArrayList<String> connect(ArrayList<String> preFrequentList)
{
ArrayList<String> initCandidate=new ArrayList<String>();//initCandidate為初始候選n項集
StringBuffer buf1=new StringBuffer();
for(int i=0;i<preFrequentList.size();i++) //頻繁n-1項集中的所有元素
{
String[] s1=preFrequentList.get(i).split(",");//l1
String[] s=new String[s1.length+1]; //l1連接l2后產(chǎn)生的候選集放入s中
for(int j=i+1;j<preFrequentList.size();j++)
{
String[] s2=preFrequentList.get(j).toString().split(",");//l2
for(int k1=0;k1<s1.length;k1++)//串內(nèi)部相比較
{
if(!s1[k1].equals(s2[k1]))
if(k1==s1.length-1)//只最后一個不相同時就連接
{
for(int k2=0;k2<s1.length;k2++)
{
s[k2]=s1[k2];
}
s[s.length-1]=s2[s2.length-1];
for(int k2=0;k2<s.length;k2++)
{
buf1.append(",");
buf1.append(s[k2]);
}
initCandidate.add(buf1.substring(1).toString());//將所求的每一個連接產(chǎn)生的候選加入候選n項集initCandidate中
buf1.delete(buf1.indexOf(buf1.toString()),buf1.length());//刪除,以便下一輪的存入
}
else
break;//若前面有元素不相同時,此次連接退出,進入下一次比較
}
}
}
return initCandidate;
}
/*剪枝步:刪除非頻繁的候選
* 即求出初始候選n項集b2中的每個子集
* 若都在頻繁n-1項集beforeFrequent中,則加入真正的候選n項集b3
* 從而得出候選n項集b3*/
public static ArrayList<String> pruning(ArrayList<String> tempList,ArrayList<String> beforeFrequent)
{
ArrayList b3=new ArrayList();
StringBuffer buf1=new StringBuffer();
int n = tempList.get(0).split(",").length;
for(int i=0;i<tempList.size();i++)
{
ArrayList b=new ArrayList();
String[] s1=tempList.get(i).toString().split(",");
for(int j=0;j<s1.length&&j<n-1;j++)//對候選集中的每一個求其子集n-1項集存入b
{
for(int k1=j+1;k1<s1.length;k1++)
{
int m=1;//m用于對候選n項集的每個子集的個數(shù)計數(shù)
buf1.append(",");
buf1.append(s1[j]);
for(int k2=k1;k2<s1.length&&m<n-1;k2++)
{
buf1.append(",");
buf1.append(s1[k2]);
m++;
}
b.add(buf1.substring(1).toString());
buf1.delete(buf1.indexOf(buf1.toString()),buf1.length());
}
}//每一個求子集完畢
int signal=1;
//與頻繁n-1項集firstList進行比較,求出真正的候選n項集b3
for(int k=0;k<b.size();k++)//若候選集的子集都是頻繁項集的元素,則加入b3
if(!beforeFrequent.contains(b.get(k).toString()))
signal=0;
if(signal==1)
{
buf1.append(tempList.get(i).toString());
b3.add(buf1.toString());
buf1.delete(buf1.indexOf(buf1.toString()),buf1.length());
}
//要 System.out.println(b);
}
return b3;
}
//與最小支持度比較,得出頻繁項集:若小于最小支持度,則從候選集中刪除,得到頻繁項集
public static void findFrequent(ArrayList<String> candidate, int[] supportArray, int minSupport)
{
int m = 0;
System.out.println();
for(int i = 0; i < candidate.size(); i ++)
{
if(supportArray[m] < minSupport)
{
candidate.remove(i);
i --;
}
m ++;
}
}
/*
* 支持度計數(shù):掃描事務(wù)集transList,對剪枝后的頻繁項集candidateSet中的每一個子集計數(shù)
*/
public static int[] getSupport(ArrayList<String> candidateSet, ArrayList<String> transList)
{
int[] supportArray = new int[candidateSet.size()];
String strTemp1, strTemp2;
for(int i=0;i<candidateSet.size();i++)//對候選頻繁項集中的每一項
{
supportArray[i]=0;
strTemp1=candidateSet.get(i);
for(int j=0;j<transList.size();j++)//對事務(wù)集transList中的每個事務(wù)
{
strTemp2 = transList.get(j);
if( compare(strTemp1,strTemp2))
{
supportArray[i]++;
}
}
// System.out.println("頻繁xxxxxxxxxx"+b3.size()+"項集:"+b3.get(i).toString()+" "+num1[i]);
}
return supportArray;
}
/*
* 比較,用于計數(shù)
*/
public static boolean compare(String str1, String str2)
{
String[] strtemp1=str1.split(",");
//String[] strn=str2.split(",");
int count =0;
/*若事務(wù)集中的子集str2包含頻繁項集中的子集strtemp1的每一個元素,則count+1;
* 若count與頻繁項集中的子集的元素個數(shù)相同,則返回true
*/
for(int i=0;i<strtemp1.length;i++)
{
if(str2.contains(strtemp1[i]))
count++;
}
if(count==strtemp1.length)
return true;
return false;
}
/*
* 求出所有的成員存入candidateList1
*/
public static void putContent(ArrayList<String> candidateList1,ArrayList<String> transList )
{
String[] strtemp;
for(int i=0; i<transList.size(); i++)
{
strtemp=transList.get(i).split(",");
for(int j=0; j<strtemp.length; j++)
{
if(!candidateList1.contains( strtemp[j].trim()))
candidateList1.add( strtemp[j].trim());
}
}
}
/*
* 排序:升序
*/
public static void sort(ArrayList<String> strList)
{
for(int i=0;i<strList.size()-1;i++)
{
String strTemp1=strList.get(i);
String temp=strTemp1;
for(int j=i+1;j<strList.size();j++)
{
String strTemp2=strList.get(j);
if(strTemp1.compareTo(strTemp2)>0)//s1>s2時需交換位置
{
temp=strTemp1;
strTemp1=strTemp2;
strTemp2=temp;
strList.remove(i);
strList.add(i,strTemp1);
strList.remove(j);
strList.add(j,strTemp2);
}
}
}
}
public static void main(String[] args)
{
ArrayList<String> transList=new ArrayList<String>();//事務(wù)集
ArrayList<String> candidateList1=new ArrayList<String>();
StringBuffer buf1=new StringBuffer();
StringBuffer output = new StringBuffer();
int min_support=2;
transList = Util.getList("d:\\input.txt");
/*
* 求頻繁一項集
*/
putContent(candidateList1, transList);//求候選1項集
sort(candidateList1);//候選1項集排序
int[] supportArray = getSupport(candidateList1,transList);//求一項集的支持度存入supportArray
findFrequent(candidateList1,supportArray,min_support);//與最小支持度進行比較,求出頻繁1項集
supportArray = getSupport(candidateList1,transList);//支持度計數(shù)
//將頻繁1項集的結(jié)果存入output.txt文件中
output.append("頻繁1項集:");
output.append("\n");
output.append("項集 "+"支持度計數(shù)");
output.append("\n");
for(int i = 0; i < candidateList1.size(); i++ )
{
output.append(candidateList1.get(i));
output.append(" "+supportArray[i]);
output.append("\n");
}
ArrayList<String> preFrequentList=new ArrayList<String>();
preFrequentList.addAll(candidateList1);
ArrayList<String> initCandidate = preFrequentList;
/*
* 求頻繁二項集以及其后頻繁項集
*/
while(initCandidate.size() > 0)
{
initCandidate = connect(preFrequentList);//連接步,得到初始候選集
ArrayList<String> candidate=new ArrayList<String>();
candidate = pruning(initCandidate, preFrequentList);//剪枝步,刪除非頻繁的候選,得到真正的候選集
supportArray = getSupport(candidate,transList);//支持度計數(shù)
/*for(int i = 0; i < supportArray.length; i ++)
{
System.out.print(" " + supportArray[i] + " ");
}*/
findFrequent(candidate,supportArray,min_support);//求頻繁項集
if(candidate.size()>0)
{
supportArray = getSupport(candidate,transList);//支持度計數(shù)
//將頻繁n項集的結(jié)果存入output.txt文件中
Integer lm=new Integer(candidate.get(0).split(",").length);
output.append("頻繁");
output.append(lm.toString());
output.append("項集: ");
output.append("\n");
output.append("項集 "+"支持度計數(shù)");
output.append("\n");
for(int i = 0; i < candidate.size(); i++ )
{
output.append(candidate.get(i));
output.append(" "+supportArray[i]);
output.append("\n");
}
// output.append(candidate.toString());
// output.append("\n");
}
initCandidate = candidate;
preFrequentList = initCandidate;
}
Util.writeFile(output.toString(), "d:\\output.txt");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -