import java.util.ArrayList; public class TransactionAnalysis { private Transaction[] transactions; private Item[] items; public TransactionAnalysis(Transaction[] transactions, Item[] items) { this.transactions = transactions; this.items = items; } public String aPrioriAlgorithm(int threshold) { String result = ""; ArrayList itemSets = createItemsetsOfSize1(); int itemsetNumber=1; //the current itemset being looked at while(itemSets.size()>0) { itemSets = calculateFrequencysItemSets(itemSets, threshold); if(!itemSets.isEmpty()) { result += "- Found " + itemSets.size()+ " frequent itemsets of size " + itemsetNumber + ":\n"; result += printItemSets(itemSets); itemSets = createItemSetsFromPreviousOnes(itemSets); } itemsetNumber++; } return result; } private ArrayList createItemsetsOfSize1() { ArrayList itemSets = new ArrayList(); for(int i=0; i createItemSetsFromPreviousOnes(ArrayList previousItemSets) { // by construction, all existing itemsets have the same size int previousSizeOfItemsets = previousItemSets.get(0).length; ArrayList newItemSets = new ArrayList(); //temporary candidates // compare each pair of itemsets of size n-1 for(int i=0; i calculateFrequencysItemSets(ArrayList itemSets, int tresHold) { ArrayList result = new ArrayList(); int[] frequencies = new int[itemSets.size()]; int i = 0; for(Item[] productSet: itemSets) { frequencies[i] = frequencyItemSet(productSet); i++; } for(int j=0; j < frequencies.length; j++) if(frequencies[j] >= tresHold) result.add(itemSets.get(j)); return result; } public String printItemSets(ArrayList itemSets){ String result = "{"; for(Item[] items: itemSets) { result += "("; for(Item item: items) result += item.getItemName() + ", "; result = result.substring(0, result.length()-2); result += ");"; } result = result.substring(0, result.length()-1); return result + "}\n"; } }