diff --git a/applications/fptree.h b/applications/fptree.h deleted file mode 100644 index 5cd6aa5..0000000 --- a/applications/fptree.h +++ /dev/null @@ -1,313 +0,0 @@ -#ifndef _fpgrowth_header_only_h_ -#define _fpgrowth_header_only_h_ - -#include -#include -#include -#include -#include -#include - -using namespace std; - -template -using Transaction = vector; - -/*! - * \struct Node - * \details The Node representation - */ -template -struct Node -{ - T _itemValue; - - int _order{0}; - int _freq{0}; - Node *_parent{nullptr}; - list _children; - list _links; - - explicit Node(T const& p_value, int p_order = 0):_itemValue(p_value), _order(p_order) - { - ++_freq; - //cout << " + " << _itemValue << " (" << _order << ")" << endl; - } - - bool operator ==(Node const& p_node) const - { - return _itemValue == p_node._itemValue; - } -}; - - -/*! - * \struct Comparator - * \details A helper to allow comparison of 2 nodes - */ -template -struct Comparator -{ - bool operator()(Node const& p_left, Node const& p_right) - { - return (p_left._freq > p_right._freq) || - (p_left._freq == p_right._freq && p_left._order < p_right._order); - } -}; - - -/*! - * \typedef Itemset - * \details To define an itemset type - */ -template -using ItemSet = vector>; - - -/*! - * \typedef OrderedItems - * \details To define an ordered unique items - */ -template -using OrderedItems = std::set, Comparator>; - - -/*! - * \struct ItemSupport - * \details The tree representation - */ -template -struct ItemSupport -{ - - explicit ItemSupport(int p_minSup){ItemSupport::_minSup = p_minSup;} - - ItemSupport& operator<< (T const& p_itemValue) - { - static int order = 0; - auto inode = find_if(_itemList.begin(), _itemList.end(), [&p_itemValue](Node const& p_node) - { - return p_node._itemValue == p_itemValue; - }); - - if(inode == _itemList.end()) - { - Node node(p_itemValue, order); - _itemList.push_back(node); - cout << p_itemValue << " : order " << order << endl; - ++order; - } - else - { - auto& node = (*inode); - ++node._freq; - } - - return *this; - } - - friend ostream& operator<<( ostream& p_os, ItemSupport const& p_itemSupport) - { - OrderedItems itemset = p_itemSupport.getFrequentItems(); - - for(Node node: itemset) - { - p_os << node._itemValue << ": support " << node._freq << ", order " << node._order << endl; - } - return p_os; - } - - Node* getItem(T const& p_itemValue) - { - auto inode = find_if(_itemList.begin(), _itemList.end(), [&p_itemValue](Node const& p_node) - { - return p_node._itemValue == p_itemValue; - }); - if(inode != _itemList.end()) - { - Node* node = const_cast*>(&(*inode)); - return node; - } - - return nullptr; - } - - - static int getMinSup() - { - return _minSup; - } - - const ItemSet& getItemList() const - { - return _itemList; - } - - static int _minSup; -private: - - ItemSet _itemList; - - OrderedItems getFrequentItems() const - { - OrderedItems ordered; - for(Node const& node: _itemList) - { - if(node._freq >= ItemSupport::_minSup) - ordered.insert(node); - } - - return ordered; - } - - OrderedItems getUnfrequentItems() const - { - OrderedItems ordered; - for(Node const& node: _itemList) - { - if(node._freq <= ItemSupport::_minSup) - ordered.insert(node); - } - - return ordered; - } -}; - -template -int ItemSupport::_minSup = 0; - -template -struct FP_Tree -{ - explicit FP_Tree(ItemSupport& p_itemSupport, const T& p_rootValue = T()):_headItemSupport(p_itemSupport) - { - _root = new Node(p_rootValue); - } - - void construct( Transaction const& p_itemValues) - { - // A. Order items into transaction - OrderedItems ordered; - cout << "(transaction) "; - for(T const& itemValue: p_itemValues) - { - cout << itemValue << " "; - Node *pNode = _headItemSupport.getItem(itemValue); - if(pNode && pNode->_freq >= ItemSupport::getMinSup()) - { - ordered.insert((*pNode)); - } - } - cout << endl; - - cout << "(ordered) "; - for(Node const& node: ordered) - { - cout << node._itemValue << " "; - } - cout << endl; - - // B. Create FP_TREE - Node* actualNode = _root; - bool here = true; - string tab; - for(Node const& node: ordered) - { - tab += "\t-"; - - auto it = actualNode->_children.begin(); - if(here) - { - it = find_if(actualNode->_children.begin(), - actualNode->_children.end(), [&node](const Node* nodeTmp){ - return node == (*nodeTmp); - }); - - here &= it != actualNode->_children.end(); - } - - if(here) - { - actualNode = *it; - ++actualNode->_freq; - } - else - { - Node* pNode = new Node(node._itemValue); - actualNode->_children.push_back(pNode); - - pNode->_parent = actualNode; - Node *pNodeHead = _headItemSupport.getItem(node._itemValue); - pNodeHead->_links.push_back(pNode); - - actualNode = pNode; - } - - //cout << tab << actualNode->_itemValue << "(" << actualNode->_freq << ")" << endl; - } - cout << endl; - } - - void printFPTree() - { - cout << endl << "--------- FP-Tree ---------------" << endl; - - function *, int)> fctPrint; - fctPrint = [&fctPrint](Node *p_actualNode, int p_level) - { - string tab; - for(int l=0; l_itemValue << " (freq " << p_actualNode->_freq << ")" << endl; - if(p_actualNode && !p_actualNode->_children.empty()) - { - ++p_level; - for(Node* node : p_actualNode->_children) - { - fctPrint(node, p_level); - } - } - }; - - fctPrint(_root, 0); - } - - void printConditionalPattern() - { - cout << endl << "--------- Conditional Pattern (p-Conditional) ---------------" << endl; - - for(Node const& node: _headItemSupport.getItemList()) - { - if(ItemSupport::_minSup) - cout << endl << node._itemValue << ": "; - - for(Node* link: node._links) - { - Node* parent = link->_parent; - while(parent && parent != _root) - { - cout << parent->_itemValue; - parent = parent->_parent; - } - cout << ":" << link->_freq << " "; - } - } - } - - void print() - { - printFPTree(); - - printConditionalPattern(); - } - -private: - ItemSupport& _headItemSupport; - Node* _root; -}; - -#endif - diff --git a/applications/fptree_headeronly.h b/applications/fptree_headeronly.h deleted file mode 100644 index 166d910..0000000 --- a/applications/fptree_headeronly.h +++ /dev/null @@ -1,257 +0,0 @@ -#ifndef _fpgrowth_header_only_h_ -#define _fpgrowth_header_only_h_ - -#include -#include -#include -#include -#include -#include - -using namespace std; - -template -using Transaction = vector; - -/*! - * \struct Node - * \details The Node representation - */ -template -struct Node -{ - T _itemValue; - - int _order{0}; - int _freq{0}; - Node *_parent{nullptr}; - list _children; - list _links; - - explicit Node(T const& p_value, int p_order = 0):_itemValue(p_value), _order(p_order) - { - ++_freq; - //cout << " + " << _itemValue << " (" << _order << ")" << endl; - } - - bool operator ==(Node const& p_node) const - { - return _itemValue == p_node._itemValue; - } -}; - - -/*! - * \struct Comparator - * \details A helper to allow comparison of 2 nodes - */ -template -struct Comparator -{ - bool operator()(Node const& p_left, Node const& p_right) - { - return (p_left._freq > p_right._freq) || - (p_left._freq == p_right._freq && p_left._order < p_right._order); - } -}; - - -/*! - * \typedef Itemset - * \details To define an itemset type - */ -template -using ItemSet = vector>; - - -/*! - * \typedef OrderedItems - * \details To define an ordered unique items - */ -template -using OrderedItems = std::set, Comparator>; - - -/*! - * \struct ItemSupport - * \details The tree representation - */ -template -struct ItemSupport -{ - - explicit ItemSupport(int p_minSup){ItemSupport::_minSup = p_minSup;} - - ItemSupport& operator<< (T const& p_itemValue) - { - static int order = 0; - auto inode = find_if(_itemList.begin(), _itemList.end(), [&p_itemValue](Node const& p_node) - { - return p_node._itemValue == p_itemValue; - }); - - if(inode == _itemList.end()) - { - Node node(p_itemValue, order); - _itemList.push_back(node); - ++order; - } - else - { - auto& node = (*inode); - ++node._freq; - } - - return *this; - } - - friend ostream& operator<<( ostream& p_os, ItemSupport const& p_itemSupport) - { - OrderedItems itemset = p_itemSupport.getFrequentItems(); - - for(Node node: itemset) - { - p_os << node._itemValue << ": support " << node._freq << ", order " << node._order << endl; - } - return p_os; - } - - Node* getItem(T const& p_itemValue) - { - auto inode = find_if(_itemList.begin(), _itemList.end(), [&p_itemValue](Node const& p_node) - { - return p_node._itemValue == p_itemValue; - }); - if(inode != _itemList.end()) - { - Node* node = const_cast*>(&(*inode)); - return node; - } - - return nullptr; - } - - - static int getMinSup() - { - return _minSup; - } - - const ItemSet& getItemList() const - { - return _itemList; - } - - static int _minSup; -private: - - ItemSet _itemList; - - OrderedItems getFrequentItems() const - { - OrderedItems ordered; - for(Node const& node: _itemList) - { - if(node._freq >= ItemSupport::_minSup) - ordered.insert(node); - } - - return ordered; - } - - OrderedItems getUnfrequentItems() const - { - OrderedItems ordered; - for(Node const& node: _itemList) - { - if(node._freq <= ItemSupport::_minSup) - ordered.insert(node); - } - - return ordered; - } -}; - -template -int ItemSupport::_minSup = 0; - -template -struct FP_Tree -{ - explicit FP_Tree(ItemSupport& p_itemSupport, const T& p_rootValue = T()):_headItemSupport(p_itemSupport) - { - _root = new Node(p_rootValue); - } - - void construct( Transaction const& p_itemValues) - { - // A. Order items into transaction - OrderedItems ordered; - for(T const& itemValue: p_itemValues) - { - Node *pNode = _headItemSupport.getItem(itemValue); - if(pNode && pNode->_freq >= ItemSupport::getMinSup()) - { - ordered.insert((*pNode)); - } - } - - // B. Create FP_TREE - Node* actualNode = _root; - bool here = true; - string tab; - for(Node const& node: ordered) - { - tab += "\t-"; - - auto it = actualNode->_children.begin(); - if(here) - { - it = find_if(actualNode->_children.begin(), - actualNode->_children.end(), [&node](const Node* nodeTmp){ - return node == (*nodeTmp); - }); - - here &= it != actualNode->_children.end(); - } - - if(here) - { - actualNode = *it; - ++actualNode->_freq; - } - else - { - Node* pNode = new Node(node._itemValue); - actualNode->_children.push_back(pNode); - - pNode->_parent = actualNode; - Node *pNodeHead = _headItemSupport.getItem(node._itemValue); - pNodeHead->_links.push_back(pNode); - - actualNode = pNode; - } - - //cout << tab << actualNode->_itemValue << "(" << actualNode->_freq << ")" << endl; - } - cout << endl; - } - - ItemSupport &headItemSupport() const - { - return _headItemSupport; - } - - -public: - Node *root() const - { - return _root; - } - -private: - ItemSupport& _headItemSupport; - Node* _root; -}; - -#endif