Documente Academic
Documente Profesional
Documente Cultură
Contents
1 Main Code 1.1 AprioriImplementation.hpp 1.2 AprioriImplementation.cpp 1.3 database.hpp . . . . . . . . 1.4 database.cpp . . . . . . . . 1.5 sets.hpp . . . . . . . . . . . 1.6 sets.cpp . . . . . . . . . . . 1.7 apriori.hpp . . . . . . . . . 1.8 apriori.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 4 5 6 7 8 10 11
2 Input Generator 13 2.1 AprioriInputGenerator.cpp . . . . . . . . . . . . . . . . . . . 13 3 Sample Input Files 15 3.1 input1.apriori . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.2 input2.apriori . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Apriori Algorithm
Implementation
1
1.1
Main Code
AprioriImplementation.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#if ! d e f i n e d (IOSTREAM) # define IOSTREAM #include<i o s t r e a m > # endif #if ! d e f i n e d (SET) # define SET #include<s e t > # endif #if ! d e f i n e d (MAP) # define MAP #include<map> # endif #if ! d e f i n e d (STRING) # define STRING #include< s t r i n g > # endif #if ! d e f i n e d (TYPEDEFS) # define TYPEDEFS typedef unsigned int uint ; typedef unsigned int DataType ; typedef std : : set<DataType > IntSet ; typedef std : : set<IntSet> SuperSet ; typedef IntSet : : const_iterator IntSetCI ; typedef SuperSet : : const_iterator SuperSetCI ; # endif #if ! d e f i n e d (TYPEDEFS MAPS) # define TYPEDEFS MAPS typedef std : : map<DataType , std : : string> Index ; typedef Index : : const_iterator IndexCI ; # endif #if ! d e f i n e d (SETS) # define SETS # include "sets.hpp" # endif #if ! d e f i n e d (DATABASE) # define DATABASE # include " database .hpp" # endif #if ! d e f i n e d (APRIORI)
by Ashish Jain
Apriori Algorithm
Implementation
40 41 42
by Ashish Jain
Apriori Algorithm
Implementation
1.2
AprioriImplementation.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# include " AprioriImplementation .hpp" void display ( Index index ) ; / * f o r t e s t i n g p u r p o s e s * / int main ( ) { uint min_sup ; uint noItems , noTrans ; std : : cout<<" Reading Data ..." ; std : : cin>>min_sup>>noItems>>noTrans ; Index index ; for ( int i =0; i < noItems ; i++) std : : cin>>index [ i ] ; Matrix db ( noTrans , noItems ) ; db . getData ( ) ; std : : cout<<"\ nProcessing ...\n" ; display ( apriori ( noItems , db , min_sup ) , index ) ; return 0 ; } void display ( Index index ) { std : : cout<<"Map :-" ; for ( IndexCI i=index . begin ( ) ; i != index . end ( ) ;++i ) std : : cout<<std : : endl<<i>first<<' \t '<<i>second ; }
by Ashish Jain
Apriori Algorithm
Implementation
1.3
database.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#if ! d e f i n e d (IOSTREAM) # define IOSTREAM #include<i o s t r e a m > # endif #if ! d e f i n e d (SET) # define SET #include<s e t > # endif #if ! d e f i n e d (TYPEDEFS) # define TYPEDEFS typedef unsigned int uint ; typedef unsigned int DataType ; typedef std : : set<DataType > IntSet ; typedef std : : set<IntSet> SuperSet ; typedef IntSet : : const_iterator IntSetCI ; typedef SuperSet : : const_iterator SuperSetCI ; # endif class Matrix { bool ** mat ; uint r , c ; public : Matrix ( uint r , uint c ) ; Matrix ( ) ; void getData ( ) ; void display ( ) ; uint count ( const IntSet& c ) const ; bool candidateInTransaction ( const uint& row , IntSet c ) const ; };
by Ashish Jain
Apriori Algorithm
Implementation
1.4
database.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# include " database .hpp" Matrix : : Matrix ( uint r , uint c ) { this>r=r ; this>c=c ; mat=new bool * [ r ] ; for ( int i =0; i < r ; i++) mat [ i ]= new bool [ c ] ; } Matrix : : Matrix ( ) { for ( int i =0; i < r ; i++) delete [ ] mat [ i ] ; delete [ ] mat ; } void Matrix : : getData ( ) { for ( int i =0; i < r ; i++) for ( int j =0; j < c ; j++) std : : cin>>mat [ i ] [ j ] ; } void Matrix : : display ( ) { for ( int i =0; i < r ; i++){ std : : cout<<std : : endl ; for ( int j =0; j< c ; j++) std : : cout<<mat [ i ] [ j ]<< ' ' ; } } bool Matrix : : candidateInTransaction ( const uint& row , IntSet c ) const { for ( IntSetCI i=c . begin ( ) ; i != c . end ( ) ;++i ) if ( ! mat [ row ] [ * i ] ) return false ; return true ; } uint Matrix : : count ( const IntSet& c ) const { int i , count =0; for ( i =0; i < r ; i++) if ( candidateInTransaction ( i , c ) ) count ++; return count ; }
by Ashish Jain
Apriori Algorithm
Implementation
1.5
sets.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#if ! d e f i n e d (IOSTREAM) # define IOSTREAM #include<i o s t r e a m > # endif #if ! d e f i n e d (SET) # define SET #include<s e t > # endif #if ! d e f i n e d (MAP) # define MAP #include<map> # endif #if ! d e f i n e d (STRING) # define STRING #include< s t r i n g > # endif #if ! d e f i n e d (TYPEDEFS) # define TYPEDEFS typedef unsigned int uint ; typedef unsigned int DataType ; typedef std : : set<DataType > IntSet ; typedef std : : set<IntSet> SuperSet ; typedef IntSet : : const_iterator IntSetCI ; typedef SuperSet : : const_iterator SuperSetCI ; # endif #if ! d e f i n e d (TYPEDEFS MAPS) # define TYPEDEFS MAPS typedef std : : map<DataType , std : : string> Index ; typedef Index : : const_iterator IndexCI ; # endif void display ( IntSet s ) ; void display ( SuperSet ss ) ; void display ( IntSet s , Index index ) ; void display ( SuperSet ss , Index index ) ; IntSet allButLast ( IntSet s ) ; IntSet last ( IntSet s ) ; SuperSet genSubsets ( IntSet s ) ;
by Ashish Jain
Apriori Algorithm
Implementation
1.6
sets.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# include "sets.hpp" void display ( IntSet s ) { std : : cout<<' { ' ; if ( s . size ( ) ) { IntSetCI i=s . begin ( ) ; std : : cout <<*( i++) ; for ( ; i != s . end ( ) ;++i ) std : : cout<<' , '<<*i ; } std : : cout<<' } ' ; } void display ( SuperSet ss ) { std : : cout<<' { ' ; if ( ss . size ( ) ) { SuperSetCI i=ss . begin ( ) ; display ( * ( i++)) ; for ( ; i != ss . end ( ) ;++i ) { std : : cout<<' , ' ; display ( * i ) ; } } std : : cout<<' } ' ; } void display ( IntSet s , Index index ) { std : : cout<<' { ' ; if ( s . size ( ) ) { IntSetCI i=s . begin ( ) ; std : : cout<<index [ * ( i++) ] ; for ( ; i != s . end ( ) ;++i ) std : : cout<<' , '<<index [ * i ] ; } std : : cout<<' } ' ; } void display ( SuperSet ss , Index index ) { std : : cout<<' { ' ; if ( ss . size ( ) ) { SuperSet : : iterator i=ss . begin ( ) ; display ( * ( i++) , index ) ; for ( ; i != ss . end ( ) ;++i ) { std : : cout<<' , ' ; display ( * i , index ) ;
by Ashish Jain
Apriori Algorithm
Implementation
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
} } std : : cout<<' } ' ; } SuperSet genSubsets ( IntSet s ) { SuperSet ss ; if ( s . size ( ) < 2 ) return ss ; IntSetCI front , back ; IntSet temp ; front=s . begin ( ) ; back=s . end ( ) ; back ; temp . insert ( front , back ) ; ss . insert ( temp ) ; temp . clear ( ) ; ++front ;++ back ; temp . insert ( front , back ) ; ss . insert ( temp ) ; temp . clear ( ) ; if ( s . size ( ) == 2 ) return ss ; back=++(s . begin ( ) );++ front ; while ( front != s . end ( ) ) { temp . insert ( front , s . end ( ) ) ; temp . insert ( s . begin ( ) , back ) ; ss . insert ( temp ) ; temp . clear ( ) ; front++;++back ; } return ss ; } IntSet allButLast ( IntSet s ) { IntSet temp ; if ( s . size ( ) ) temp . insert ( s . begin ( ) , ( s . end ( ) ) ) ; return temp ; } IntSet last ( IntSet s ) { IntSet temp ; if ( s . size ( ) ) temp . insert (( s . end ( ) ) , s . end ( ) ) ; return temp ; }
by Ashish Jain
Apriori Algorithm
Implementation
1.7
apriori.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#if ! d e f i n e d (IOSTREAM) # define IOSTREAM #include<i o s t r e a m > # endif #if ! d e f i n e d (SET) # define SET #include<s e t > # endif #if ! d e f i n e d (TYPEDEFS) # define TYPEDEFS typedef unsigned int uint ; typedef unsigned int DataType ; typedef std : : set<DataType > IntSet ; typedef std : : set<IntSet> SuperSet ; typedef IntSet : : const_iterator IntSetCI ; typedef SuperSet : : const_iterator SuperSetCI ; # endif #if ! d e f i n e d (DATABASE) # define DATABASE # include " database .hpp" # endif #if ! d e f i n e d (SETS) # define SETS # include "sets.hpp" # endif bool has_infrequent_subset ( IntSet c , SuperSet l1 ) ; SuperSet apriori_gen ( SuperSet l1 ) ; SuperSet scanDB ( SuperSet cs , Matrix &db , const uint& min_sup ); SuperSet makeL1 ( const uint& noItems , const Matrix& db , const uint& min_sup ) ; SuperSet apriori ( const uint& noItems , const Matrix& db , const uint& min_sup ) ;
10
by Ashish Jain
Apriori Algorithm
Implementation
1.8
apriori.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# include " apriori .hpp" bool has_infrequent_subset ( IntSet c , SuperSet l1 ) { // Assuming c . s i z e ( ) > 1 SuperSet subsetsC=genSubsets ( c ) ; for ( SuperSetCI i=subsetsC . begin ( ) ; i != subsetsC . end ( ) ;++ i) if ( ! l1 . count ( * i ) ) return true ; return false ; } SuperSet apriori_gen ( SuperSet l1 ) { SuperSet ck ; if ( l1 . size ( ) < 2 ) return ck ; SuperSetCI rearmost=(l1 . end ( ) ) , i , j , tempI ; for ( i=l1 . begin ( ) ; i != rearmost ;++i ) { tempI=i ; for ( j=++tempI ; j != l1 . end ( ) ;++j ) { if ( allButLast ( * i ) == allButLast ( * j ) && last ( * i ) != last ( * j ) ) { IntSet temp ( i>begin ( ) , i>end ( ) ) ; temp . insert (( j>end ( ) ) , j>end ( ) ) ; / * Assuming i t e m s e t . s i z e ( ) >= 1 * / if ( has_infrequent_subset ( temp , l1 ) ) ; // i g n o r e ; else ck . insert ( temp ) ; } } } return ck ; } SuperSet scanDB ( SuperSet cs , const Matrix &db , const uint & min_sup ) { SuperSet l2 ; for ( SuperSetCI i=cs . begin ( ) ; i != cs . end ( ) ;++i ) if ( db . count ( * i ) >= min_sup ) l2 . insert ( * i ) ; return l2 ; } SuperSet makeL1 ( const uint& noItems , const Matrix& db , const
11
by Ashish Jain
Apriori Algorithm
Implementation
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
uint& min_sup ) { IntSet temp ; SuperSet c1 ; for ( int i =0; i < noItems ; i++){ temp . insert ( i ) ; c1 . insert ( temp ) ; / * By Value * / temp . clear ( ) ; } return scanDB ( c1 , db , min_sup ) ; } SuperSet apriori ( const uint& noItems , const Matrix& db , const uint& min_sup ) { SuperSet l1 , prev ; l1=makeL1 ( noItems , db , min_sup ) ; while ( l1 . size ( ) ) { prev=l1 ; l1=scanDB ( apriori_gen ( l1 ) , db , min_sup ) ; } return prev ; }
12
by Ashish Jain
Apriori Algorithm
Implementation
2
2.1
Input Generator
AprioriInputGenerator.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include<i o s t r e a m > #include<f s t r e a m > #include< s t r i n g > using namespace std ; typedef unsigned int uint ; void error ( const char * p , const char * p2 = "" ) { cerr<<p<<' '<<p2<<' \n ' ; exit ( 1 ) ; } int main ( int argc , char ** argv ) { ofstream out ; if ( argc < 2 ) { out . open ( " default . apriori " ) ; if ( ! out ) { error ( " cannot openout putfile " , " default . apriori " ) ; return 1; } } else { out . open ( argv [ 1 ] ) ; if ( ! out ) { error ( " cannot open output file " , argv [ 2 ] ) ; return 1; } } uint var , i , j , noItems , noTrans ; cout<<" min_sup ? " ; cin>>var ; out<<var<<endl ; cout<<" Number of items ? " ; cin>>noItems ; out<<noItems<<' ' ; cout<<" Number of transactions ? " ; cin>>noTrans ; out<< noTrans<<endl ; string * itemNames ; itemNames=new string [ noItems ] ; cout<<" Enter the names of the items :-\n" ; for ( i =0; i < noItems ; i++){ cout<<"Item "<<i<<"? " ; cin>>itemNames [ i ] ; out<<itemNames [ i ]<< endl ; }
13
by Ashish Jain
Apriori Algorithm
Implementation
38 39 40 41 42 43 44 45 46 47 48 49 50
cout<<" Enter Transactions :-\n" ; for ( i =0; i < noTrans ; i++){ for ( j =0; j < noItems ; j++){ cout<<' [ '<<i+1<<"]["<<itemNames [ j ]<< "]? " ; cin>>var ; out <<(var != 0 )<<' ' ; } out<<endl ; } delete [ ] itemNames ; out . close ( ) ; return 0 ; }
14
by Ashish Jain
Apriori Algorithm
Implementation
3
3.1
2 5 1 2 3 4 5 1 0 0 1 1 0 1 1 1
1 1 1 1 0 1 0 1 1
0 0 1 0 1 1 1 1 1
0 1 0 1 0 0 0 0 0
1 0 0 0 0 0 0 1 0
15
by Ashish Jain
Apriori Algorithm
Implementation
3.2
input2.apriori
2 5 1 2 3 4 5 1 0 1 0
0 1 1 1
1 1 1 0
1 0 0 0
0 1 1 1
16
by Ashish Jain