Brute Force Algorithm for Mining Association Rule > Java Program
Brute Force Algorithm for Mining Association Rule > Java Program
Data Warehousing and Mining
Program:
import java.util.*;
class transaction {
int tid;
int item[];
transaction(int a, int b) {
tid = a;
item = new int[b];
}
}
class brute {
static int n = 5;
static int p = 1;
static int flag = 0;
static int found = 0;
static int s = 0;
static void check(int a[], int l, transaction t[]) {
int c1 = 0, c2 = 0;
loop1:
for (int i = 0; i < 4; ++i) {
c1 = 0;
loop2:
for (int j = 0; j < l; ++j) {
loop3:
for (int k = 0; k < t[i].item.length; ++k) {
if (t[i].item[k] == a[j]) {
++c1;
}
}
}
if (c1 == l) {
++c2;
}
}
if (c2 >= s) {
for (int i = 0; i < l; ++i) {
System.out.print(" " + a[i]);
}
System.out.print("\t" + c2);
System.out.println();
++found;
}
}
public static void main(String args[]) {
Scanner scr = new Scanner(System.in);
System.out.println("\nBrute force Algorithm\n");
System.out.println("You have 4 transactions:- T0,T1,T2,T3.\n\n");
System.out.println("You have 5 items with id:- 0,1,2,3,4.\n");
System.out.println("Enter items in each transaction");
transaction t[] = new transaction[4];
int count;
for (int i = 0; i < 4; ++i) {
count = 0;
System.out.print("Enter no. of items in transaction T" + i + " ");
count = scr.nextInt();
t[i] = new transaction(i, count);
System.out.print("Enter items in transaction T" + i + " ");
for (int k = 0; k < count; ++k) {
t[i].item[k] = scr.nextInt();
}
}
System.out.println("Enter support");
s = scr.nextInt();
System.out.println("\nTid\tItems\n");
for (int i = 0; i < 4; ++i) {
System.out.print("T" + i + "\t");
for (int k = 0; k < t[i].item.length; ++k) {
System.out.print(" " + t[i].item[k]);
}
System.out.println();
}
int temp[] = {-1, -1, -1, -1, -1};
do {
++flag;
found = 0;
int f = 0;
for (int i = 0; i <= n - p; ++i) {
if (flag == 1) {
if (f == 0) {
System.out.println("\n1-frequent set");
System.out.println("itemset\tsup");
}
f = 1;
temp[0] = i;
check(temp, 1, t);
} else {
for (int j = i + 1; j <= n - (p - 1); ++j) {
if (flag == 2) {
if (f == 0) {
System.out.println("\n2-frequent set");
System.out.println("itemset\tsup");
}
f = 1;
temp[0] = i;
temp[1] = j;
check(temp, 2, t);
} else {
for (int k = j + 1; k <= n - (p - 2); ++k) {
if (flag == 3) {
if (f == 0) {
System.out.println("\n3-frequent set");
System.out.println("itemset\tsup");
}
f = 1;
temp[0] = i;
temp[1] = j;
temp[2] = k;
check(temp, 3, t);
} else {
for (int l = k + 1; l <= n - (p - 3); ++l) {
if (flag == 4) {
if (f == 0) {
System.out.println("\n4-frequent set");
System.out.println("itemset\tsup");
}
f = 1;
temp[0] = i;
temp[1] = j;
temp[2] = k;
temp[3] = l;
check(temp, 4, t);
} else {
for (int m = l + 1; m <= n - (p - 4); ++m) {
if (f == 0) {
System.out.println("\n5-frequent set");
System.out.println("itemset\tsup");
}
f = 1;
temp[0] = i;
temp[1] = j;
temp[2] = k;
temp[3] = l;
temp[4] = m;
check(temp, 5, t);
}
}
}
}
}
}
}
}
}
++p;
if (found == 0) {
System.out.println("not found.");
}
}//end of do
while (found > 0);
System.out.println("Association rules will be calculated from " + (flag - 1) + "-frequent set.");
}
}
/*OUTPUT
C:\Users\Aditya\Desktop\aditya>javac brute.java
C:\Users\Aditya\Desktop\aditya>java brute
Brute force Algorithm
You have 4 transactions:- T0,T1,T2,T3.
You have 5 items with id:- 0,1,2,3,4.
Enter items in each transaction
Enter no. of items in transaction T0 3
Enter items in transaction T0 0 2 3
Enter no. of items in transaction T1 3
Enter items in transaction T1 1 2 4
Enter no. of items in transaction T2 4
Enter items in transaction T2 0 1 2 4
Enter no. of items in transaction T3 2
Enter items in transaction T3 1 4
Enter support
2
Tid Items
T0 0 2 3
T1 1 2 4
T2 0 1 2 4
T3 1 4
1-frequent set
itemset sup
0 2
1 3
2 3
4 3
2-frequent set
itemset sup
0 2 2
1 2 2
1 4 3
2 4 2
3-frequent set
itemset sup
1 2 4 2
4-frequent set
itemset sup
not found.
Association rules will be calculated from 3-frequent set.
*/
Comments
Post a Comment