FP-growth挖掘算法

步骤一

扫描数据库,扫描数据库一次,得到频繁1-项集,把项按支持度递减排序,再一次扫描数据库,建立FP-tree

步骤二

对每个项,生成它的 条件模式库

步骤三

用条件模式库构造对应的条件FP-tree,递归构造条件 FP-trees 同时增长其包含的频繁集,如果条件FP-tree直包含一个路径,则直接生成所包含的频繁集

C++源码

 #include<bits/stdc++.h>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<sstream>
#define suport 0.001//最小支持度
using namespace std;
struct FPtreeNode{//孩子兄弟链表法存储FP-tree
int data;
int count;
FPtreeNode *father;
FPtreeNode *children;
FPtreeNode *brother;
FPtreeNode *next;
FPtreeNode(){
data=;
count=;
father=NULL;
children=NULL;
brother=NULL;
next=NULL;
}
};
struct resultNode{//结果
string data;
int count;
resultNode(){
count=;
}
};
struct listNode{//头表
int data;
int count;
FPtreeNode *next;
};
struct fptreeNode{//递归过程中的子FP-tree
int data;
int count;
fptreeNode(){
data=;
count=;
}
};
int line=;
vector<string> Getfile(){
vector<string> file;
ifstream ifile("D://retail.txt");
if(!ifile){
cout<<"open file error"<<endl;
}
else{
string temp;
while(getline(ifile,temp)){
line++;
file.insert(file.end(),temp);
}
}
ifile.close();
return file;
}
bool cmp( listNode &a,listNode &b){
return a.count>b.count;
} vector<listNode> Getheadlist(vector<string> &file){
vector<listNode>L1;
map<string,int>r1;
string temp;
string lk;
for(int f=;f<file.size();f++){//第一次扫描数据库
temp=file[f];
int i;
for( i=;i<temp.size();i++){
while(temp[i]!=' '&&temp[i]!='\n'&&i!=temp.size()){
lk+=temp[i];
i++;
}
if(r1.find(lk)!=r1.end())
r1[lk]++;
else
r1[lk]=;
lk.clear();//
}
}
temp.clear();
map<string,int>::iterator it;
for(it=r1.begin();it!=r1.end();it++){//待删除 if(it->second>=ceil(suport*line)){
string s=it->first;
int x=atoi(s.c_str());//转换成整数
listNode xx;
xx.data=x;
xx.count=it->second;
xx.next=NULL;
L1.insert(L1.end(),xx);
}
}
sort(L1.begin(),L1.end(),cmp);
return L1;
}
bool Isin(string temp,string str){
int j;
string s;
for( j=;j<temp.size();j++){//遍历temp
while(temp[j]!=' '&&j<temp.size()&&temp[j]!='\n'){
s.insert(s.end(),temp[j]);
j++;
}
if(s==str)
break;
s.clear();
}
if(j>=temp.size())
return ;
else
return ; }
vector<vector<int> > Get_FPfile(vector<string> &file,vector<listNode> &L1){//第二次扫描数据库 删除非频繁一项
string temp;
vector<vector<int> > rfile;
vector<int >ri;
for(int f=;f<file.size();f++){
temp=file[f];
for(int k=;k<L1.size();k++){
string s;
int n=L1[k].data; stringstream ss;
ss<<n;
ss>>s;
if(Isin(temp,s)){
ri.push_back(n);
}
}
if(ri.size()>){
rfile.push_back(ri);
ri.clear();
}
temp.clear();
}
file.clear();
return rfile;
}
int c=;
void Linknext(FPtreeNode *newNode,vector<listNode> &L1){
for(int m=;m<L1.size();m++){
if(L1[m].data==newNode->data){
if(L1[m].next==NULL){
L1[m].next=newNode;
}
else{
FPtreeNode *t1=L1[m].next;
FPtreeNode *t2=L1[m].next;
while(t1){
t2=t1;
t1=t1->next;
}
t2->next=newNode;
}
break;
}
} }
FPtreeNode* Buildtree( vector<vector<int> > &rfile,vector<listNode> &L1){
FPtreeNode *head=new FPtreeNode;
FPtreeNode *p=head;
FPtreeNode *q=head;
int flag=;
for(int i=;i<rfile.size();i++){
p=head;
int j=;
while(j<rfile[i].size()){
flag=;
if(i==){//第一条
FPtreeNode *newNode=new FPtreeNode;
c++;
newNode->count=;
newNode->data=rfile[i][j];
newNode->father=p;
p->children=newNode;
p=p->children;
j++;
for(int m=;m<L1.size();m++){
if(L1[m].data==newNode->data){
L1[m].next=newNode;
break;
}
}
}
else{
p=p->children;
while(p&&j<rfile[i].size()){
if(p->data==rfile[i][j]){
p->count++;
q=p;//q->chilren=p;
p=p->children;
j++;
flag=;
}
else{//
q=p;//q->brother=p;
p=p->brother;
flag=;
} }
if(flag==){
while(j<rfile[i].size()){
FPtreeNode *newNode=new FPtreeNode;
c++;
newNode->count=;
newNode->father=q;
q->children=newNode;
newNode->data=rfile[i][j];
q=q->children;
j++;
//Linknext();
Linknext(newNode,L1);
} }
else if(flag==){
FPtreeNode *newNode=new FPtreeNode;c++;
newNode->count=;
newNode->data=rfile[i][j];
q->brother=newNode;
newNode->father=q->father;
q=q->brother;
j++;
Linknext(newNode,L1);
while(j<rfile[i].size()){
FPtreeNode *newNode=new FPtreeNode;
c++;
newNode->count=;
newNode->father=q;
q->children=newNode;
newNode->data=rfile[i][j];
q=q->children;
j++;
//Linknext();
Linknext(newNode,L1);
} }
}
}
}
return head;
}
vector<string> GetFrequentItems(listNode &lk,FPtreeNode* &head){//生成条件模式基 rfile
FPtreeNode *p=lk.next;
vector<string> rfile;
while(p){
FPtreeNode* q=p;
vector<string> temp;
while(q->father!=head){
q=q->father;
stringstream ss;
string x;
int n;
n=q->data; ss<<n;
ss>>x; temp.push_back(x+" ");
}
reverse(temp.begin(),temp.end());
string s; for(int j=;j<temp.size();j++){
s+=temp[j];
}
for(int i=;i<p->count;i++){
if(s.size()>)
rfile.push_back(s);
}
s.clear();
p=p->next;
}
return rfile;
}
vector<resultNode> result;
void Getresult(vector<listNode> &headlist,FPtreeNode* &head,string &base,vector<resultNode> &result){
if(headlist.empty()){
return;
}
for(auto p = headlist.rbegin(); p != headlist.rend(); p++){
resultNode temp;
int n;
n=p->data;//int to string
stringstream ss;
string x;
ss<<n;
ss>>x;
string xx=base+" "+x;
temp.data=xx;
temp.count=p->count;
result.push_back(temp);
/*****递归******/
//产生条件模式基
vector<string> file1=GetFrequentItems(*p,head); vector<listNode>headlist1= Getheadlist(file1);//getL1 vector<vector<int> > rfile1=Get_FPfile(file1,headlist1); //建树
FPtreeNode* Tree=Buildtree(rfile1,headlist1); string s=base+" "+x;
//产生结果
Getresult(headlist1,Tree,s,result);
} }
void Print(){
for (auto p =result.cbegin(); p != result.cend(); p++)
{
cout << p->data << " "<<"("<<p->count<<")"<< endl;
}
}
int main(){
vector<string> file=Getfile();
vector<listNode> headlist=Getheadlist(file); vector<vector<int> >rfile=Get_FPfile(file,headlist); FPtreeNode* head=Buildtree(rfile,headlist);
string base=" "; Getresult(headlist,head,base,result);
Print();
cout<<result.size();
return ;
}

数据挖掘 FP-tree算法C++实现及源码的更多相关文章

  1. FP Tree算法原理总结

    在Apriori算法原理总结中,我们对Apriori算法的原理做了总结.作为一个挖掘频繁项集的算法,Apriori算法需要多次扫描数据,I/O是很大的瓶颈.为了解决这个问题,FP Tree算法(也称F ...

  2. FP Tree算法原理总结(转载)

    FP Tree算法原理总结 在Apriori算法原理总结中,我们对Apriori算法的原理做了总结.作为一个挖掘频繁项集的算法,Apriori算法需要多次扫描数据,I/O是很大的瓶颈.为了解决这个问题 ...

  3. 数据挖掘:关联规则的apriori算法在weka的源码分析

    相对于机器学习,关联规则的apriori算法更偏向于数据挖掘. 1) 测试文档中调用weka的关联规则apriori算法,如下 try { File file = new File("F:\ ...

  4. 中国象棋程序的设计与实现(六)--N皇后问题的算法设计与实现(源码+注释+截图)

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题. 该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列 ...

  5. 常用限流算法与Guava RateLimiter源码解析

    在分布式系统中,应对高并发访问时,缓存.限流.降级是保护系统正常运行的常用方法.当请求量突发暴涨时,如果不加以限制访问,则可能导致整个系统崩溃,服务不可用.同时有一些业务场景,比如短信验证码,或者其它 ...

  6. 最快速的“高斯”模糊算法(附Android源码)

      这是一个外国人的算法,本人是搬运工.参考:http://blog.ivank.net/fastest-gaussian-blur.html   1:高斯模糊算法(参考:http://www.rua ...

  7. A*算法(附c源码)

    关于A*算法网上介绍的有很多,我只是看了之后对这个算法用c写了一下,并测试无误后上传以分享一下,欢迎指正!下面是我找的一个介绍,并主要根据这个实现的. 寻路算法不止 A* 这一种, 还有递归, 非递归 ...

  8. SIFT算法的教程及源码

    1.ubc:DAVID LOWE---SIFT算法的创始人,两篇巨经典经典的文章http://www.cs.ubc.ca/~lowe/[1] 2.cmu:YanKe---PCASIFT,总结的SIFT ...

  9. 常见算法合集[java源码+持续更新中...]

    一.引子 本文搜集从各种资源上搜集高频面试算法,慢慢填充...每个算法都亲测可运行,原理有注释.Talk is cheap,show me the code! 走你~ 二.常见算法 2.1 判断单向链 ...

随机推荐

  1. windows下Mysql5.7表名不区分大小写问题

    前言 Windwos文件系统本身是不区分大小写的,但是Linux文件系统是支持大小写的.于是安装在Linux下的Mysql导出到windows下可能因为大小写问题导致错误,因此要开启window下My ...

  2. vue.js实现用户评论、登录、注册、及修改信息功能

    vue.js实现用户评论.登录.注册.及修改信息功能:https://www.jb51.net/article/113799.htm

  3. pyhon if分支

    在python中,最常用的就是if判断,if判断可以分为单次判断和多次判断 单次判断 if   条件 : (条件成立执行我) else:(else也可以没有,最近的else对应最近的if语句) (条件 ...

  4. 编译Android源代码

    硬盘空间需要在50G以上,最好100g 系统:ubuntu14.04 交叉工具链:arm-linux-gcc-4.5.1-v6-vfp-20120301 安装 Java 开发环境 $ sudo apt ...

  5. JS window对象 返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL。

    返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL. 如果倒退之后,再想回到倒退之前浏览的页面,则可以使用forward()方法,代码如下: window.hi ...

  6. Thunar 右键菜单等自定义

    Thunar 右键菜单等自定义 可以使用图形界面或者直接编辑配置文件,二者是等价的. 图形界面: 以给"zip,rar,7z"等文件添加"在此位置使用unar解压缩&qu ...

  7. case ...esac判断 function方法 循环loop,while do done,until do done

    就类似于其他语言中的case语句 用法 要点 第一 开始结束  case  esac 正好相反 第二  每段程序段需要用  两个:号结束. 例: } in "hello") ech ...

  8. ubuntu:beyond compare 4 This license key has been revoked 解决办法

    错误如图所示: 解决办法: (1)先用find命令找到bcompare所在位置:sudo find /home/ -name '*bcompare' ()进入 /home/whf/.config,删除 ...

  9. JS判断浏览器类型的方法总结(IE firefox chrome opera safari)

    JS判断浏览器类型的方法总结,可判别当前客户端所使用的浏览器是ie,firefox,safari,chrome或者是opera,另外js可以精确判断到ie浏览器的版本,依然直接上代码,需要的朋友可按照 ...

  10. 转载 如何理解API,API 是如何工作的

    本文转载于https://blog.csdn.net/cumtdeyurenjie/article/details/80211896 感谢作者 仁杰兄 大家可能最近经常听到 API 这个概念,那什么是 ...