Java program to implement Apriori Algorithm
Simple Java program to implement Apriori Algorithm
Code:
package apriori;import com.sun.xml.internal.ws.util.StringUtils;
import java.util.*;
import java.lang.Object;
public class Apriori {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number Of Transactions:");
int n = sc.nextInt();
System.out.println("Enter Minumum Limit:");
int min = sc.nextInt();
sc.nextLine();
ArrayList<Set<String>> aray = new ArrayList<>();
Set<String> list = new HashSet<>();
Set<String> list1= new HashSet<>();
Set<String> list2,list3,list4;
System.out.println("Enter Item id saperated by space per transaction");
for(int i=0;i<n;i++){
System.out.println("Entere Item Id included in transaction-"+(i+1));
String tempp =sc.nextLine();
Set<String> templist= new HashSet<>();
templist.addAll(Arrays.asList(tempp.split(" ")));
aray.add(i,templist);
list.addAll(templist);
}//outer for loop
for(int i=0;i<list.size();i++)
{//for loop start
String comperer =list.toArray()[i].toString();
int count=0;
for(int j=0;j<n;j++)
{//startforloop
if(aray.get(j).contains(comperer)){count=count+1;}
}//endforloop
if(count >= min){list1.add(comperer.trim());}
}//for loop end
System.out.println("After First Prune, Items which will move Ahead: "+list1.toString());
if(list1.size()>=2){//ifstart
list2 = new HashSet<>();
for(int i=0;i<list1.size()-1;i++)
{//outerforstart
for(int j=i+1;j<list1.size();j++)
{//startinnerfor
int count=0;
for(int k=0;k<n;k++)
{//startinnerinnerfor
if(aray.get(k).contains(list1.toArray()[i].toString()) && aray.get(k).contains(list1.toArray()[j].toString()))
{//ifstart
count=count+1;
}//ifend
}//endinnerinnerfor
if(count>=min){list2.add(list1.toArray()[i].toString()+list1.toArray()[j].toString());}
}//endinnerfor
}//outerforend
System.out.println("After Second Prune, Items which will move Ahead: "+list2.toString());
}//ifend
else{System.out.println("Nothing to show in secnd list");return;}
}//void main
}//class
Comments
Post a Comment