00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "Apriori.hpp"
00010 #include <iostream>
00011 #include <vector>
00012 #include <set>
00013 #include <cmath>
00014
00015 using namespace std;
00016
00021 void Apriori::support( const itemtype& candidate_size )
00022 {
00023 vector<itemtype> basket, basket_v;
00024 if (candidate_size == 2)
00025 {
00026 while( input_output_manager.read_in_a_line( basket ) )
00027 {
00028 input_output_manager.basket_recode( basket, basket_v );
00029 if (basket_v.size()>1) reduced_baskets[basket_v]++;
00030 }
00031 }
00032 for (map<vector<itemtype>,unsigned long>::iterator it =
00033 reduced_baskets.begin(); it!=reduced_baskets.end();it++)
00034 apriori_trie->find_candidate(it->first,candidate_size,it->second);
00035
00036 }
00042 Apriori::Apriori( ifstream& basket_file, const char* output_file_name ):
00043 input_output_manager(basket_file, output_file_name )
00044 {
00045 }
00046
00050 void Apriori::APRIORI_alg( const unsigned long min_supp )
00051 {
00052 unsigned long basket_number;
00053 itemtype candidate_size=1;
00054 vector<unsigned long> support_of_items;
00055 basket_number = input_output_manager.find_frequent_items(
00056 min_supp, support_of_items );
00057 input_output_manager<< " ("<< basket_number << ')' << endl;
00058 set<itemtype> temp_set;
00059 for(vector<unsigned long>::size_type index = 0;
00060 index < support_of_items.size(); index++)
00061 {
00062 temp_set.insert(index);
00063 input_output_manager.write_out_basket_and_counter( temp_set, support_of_items[index] );
00064 temp_set.erase(temp_set.begin());
00065 }
00066 apriori_trie = new Apriori_Trie( basket_number );
00067 apriori_trie->insert_frequent_items( support_of_items );
00068
00069 candidate_size++;
00070 apriori_trie->candidate_generation(candidate_size-1, input_output_manager);
00071 while( apriori_trie->is_there_any_candidate() )
00072 {
00073 input_output_manager.rewind();
00074 support( candidate_size );
00075 apriori_trie->delete_infrequent(min_supp, candidate_size);
00076 candidate_size++;
00077 apriori_trie->candidate_generation(candidate_size-1,
00078 input_output_manager);
00079 }
00080 apriori_trie->statistics();
00081 }
00082
00083 Apriori::~Apriori()
00084 {
00085 delete apriori_trie;
00086 }