Find the answer to your Linux question:
Results 1 to 4 of 4
I have this code for parallel implement an Apriori algorithm. This code is written in VC++. I want to change it to C code. Could you please please someone help ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    1

    MPI code urgent


    I have this code for parallel implement an Apriori algorithm. This code is written in VC++. I want to change it to C code. Could you please please someone help me with this. I tried many times, and I am frustrated.

    Code:
    #include "stdio.h"
    #include <fstream>
    #include <iostream>
    #include "stdlib.h"
    #include "string.h"
    #include <iomanip>
    #include <time.h>
    #include "mpi.h"
    
    struct tItem
    {
    	int tag;
    	char itemset[15];
    	int count;
    	struct tItem *next;
    };
    
    #define LEN sizeof(struct tItem);
    
    int GetColNum(char fileName1[]);  //读取源数据的列数
    int GetRecordNum(char fileName2[]);  //读取源数据的行数
    int Index(char *List, char c, int ListLength);
    void GetSourceFile(char fileName[],int recordNum,int colNum);  //获取源数据
    void FindFrequence1itemsets(char **fullSet1,double sup);
    void Gen_association_rules(tItem *kf, tItem *p, double minCon);  //生成关联规则
    void SetminSup();  //设置最小值支持度
    void SetminCon();  //设置最小置信度
    struct tItem *Find_candidate_1itemsets(char **fullSet2, double sup2,int recordNum,int colNum);  //生成频繁1项集
    struct tItem *apriori_gen(tItem *freq);  //生成候选项集
    struct tItem *Gen_candidate_itemset(tItem *cp,tItem *cp1,tItem *cp2);  //生成后选项
    struct tItem *Gen_frequence(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet);  //生成频繁项集
    struct tItem *Gen_frequence1(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet);
    struct tItem *Compute_candidateItem_Num(tItem *c,int recordNum, int colNum, char **fullSet);  //计算候选项集中每一项的最小支持数
    struct tItem *Gen_kFrequence_subsets(tItem *f);  //生成频繁k项集每项的子集的集合
    struct tItem *SubSet(char *List, int m, char *Buffer, int flag, tItem *fis, int ListLength);  //生成频繁集中每一项的子集
    struct tItem *AddToSubset(char *Buffer, int flag, tItem *fis);  //将一个子集加入链表
    struct tItem *Combine_candidate_itemsets(tItem *lc, char rItemset, int rCount);  //合并局部候选1项集成为总的候选1项集
    struct tItem *Combine_candidate_k_itemsets(tItem *lc,tItem *r);
    bool Has_infrequence_subset(tItem *pp, tItem *f);  //判断候选k项集的子集是否在频繁k-1项集中
    bool Is_before_equal(tItem *pp1, tItem *pp2);  //判断两个项目集的前k-1项是否相同
    static void masterprocess(int rankNum);
    static void slaveprocess(int rankNum);
    
    char **fullSet;
    int colNum=0;
    int recordNum=0;
    double minSup=0.37;
    double minCon=0.50;
    
    int main(int argc,char *argv[])
    {
    	int rank,size;
    
    	MPI_Init(&argc,&argv);
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	MPI_Comm_size(MPI_COMM_WORLD,&size);
    
    	if(rank==0)
    	{
    		//printf("#%d#\n",size);
    		masterprocess(size);
    	}
    	else
    	{
    		slaveprocess(size);
    	}
    
    	MPI_Finalize();
    	return(0);
    }
    
    void SetminSup()
    {
    	cout<<"Please input the minSup: ";
    	cin>>minSup;
    }
    
    void SetminCon()
    {
    	cout<<"Please input the minCon: ";
    	cin>>minCon;
    }
    
    int GetColNum(char fileName1[])  //读取源数据的列数
    {
    	ifstream file(fileName1);
    
    	char ch;
    	//int colNum=0;
    	while(ch!='\n')
    	{
    		if(ch==',')
    		{
    			colNum++;
    			ch=file.get();
    		}
    		ch=file.get();
    	}
    	//colNum++;
    	file.close();
    	return colNum;
    }
    
    
    int GetRecordNum(char fileName2[])  //读取源数据的行数
    {
    	ifstream file(fileName2);
    
    	char ch=NULL;
    	//int recordNum=0;
    
    	while(!file.eof())
    	{
    		if(ch=='\n')
    		{
    			recordNum++;
    			ch=file.get();
    		}
    		ch=file.get();
    	}
    
    	file.close();
    	return recordNum;
    }
    
    void GetSourceFile(char fileName[],int rNum,int cNum)  //获取源数据读入数组fullSet[][]中
    {
    	ifstream file(fileName);
    
    	char ch;
    	//char **fullSet;
    	int item=0;
    	int i=0,j=0;
    	int temp1='A';
    	int temp2='a';
    
    	/*b=(int**)malloc(row*sizeof(int*));
    	for(i=0;i<row;i++)
    	{
    		b[i]=(int*)malloc(col*sizeof(int));
    	}*/
    
    	fullSet=(char**)malloc(rNum*sizeof(char*));
    	for(int m=0;m<rNum;m++)
    	{
    		fullSet[m]=(char*)malloc(cNum*sizeof(char));
    	}
    
    	while(i<rNum)
    	{
    		while(j<colNum)
    		{
    			if(ch==',')
    			{
    				item++;
    				ch=file.get();
    				if(ch=='T')
    				{
    					fullSet[i][j]=(char)(item+temp1);
    					//printf("%c",fullSet[i][j]);
    				}
    				if(ch=='F')
    				{
    					fullSet[i][j]=(char)(item+temp2);
    					//printf("%c",fullSet[i][j]);
    				}
    				j++;
    			}
    			ch=file.get();
    		}
    		//printf("\n");
    		j=0;
    		ch=file.get();
    		item=0;
    		i++;
    	}
    	file.close();
    }
    
    static void masterprocess(int rankNum)
    {
    
    	char inputFileName[]="c:\\shujuku.txt";
    	char *buffer;
    	int colNum,recordNum,blockNum,bound,i,j,m,k;
    	int rank,destRank,tag;
        double starttime,endtime;
    
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
        starttime=MPI_Wtime();
    
    	//SetminSup();
    	//SetminCon();
    
    	//printf("#%d#\n",rankNum);
    
    	colNum=GetColNum(inputFileName);
    	recordNum=GetRecordNum(inputFileName);
    
    	//system("pause");
    	printf("recordNum= %d", recordNum);
    	printf("\n");
    	printf("colNum= %d",colNum);
    	printf("\n");
    
    	GetSourceFile(inputFileName,recordNum,colNum);
    
    	//system("pause");
    
    	/*for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf(" %c ",fullSet[i][j]);
    		}
    		printf("\n");
    	}*/
    
    	blockNum=recordNum/(rankNum-1);
    	bound=blockNum*(rankNum-1);
    	//printf("%d\n",blockNum);
    
    	m=0;
    	k=1;
    	destRank=1;
    	tag=1;
    	buffer=(char*)malloc(colNum*sizeof(char*));
    	while(m!=recordNum)
    	{
    		if(m==(k*blockNum-1)&&(m!=bound-1))
    		{
    			MPI_Send(&colNum,1,MPI_INT,destRank,100,MPI_COMM_WORLD);
    			MPI_Send(&recordNum,1,MPI_INT,destRank,101,MPI_COMM_WORLD);
    			MPI_Send(&blockNum,1,MPI_INT,destRank,tag,MPI_COMM_WORLD);
    			//printf("%d\n",blockNum);
    			for(i=((k-1)*blockNum);i<k*blockNum;i++)
    			{
    				for(j=0;j<colNum;j++)
    				{
    					buffer[j]=fullSet[i][j];
    					printf("%c ",buffer[j]);
    				}
    				MPI_Send(buffer,colNum,MPI_CHAR,destRank,tag,MPI_COMM_WORLD);
    				printf("\n");
    			}
    			printf("\n");
    			destRank++;
    			tag++;
    			k++;
    		}
    		else if(m==bound-1)
    		{
    			blockNum=recordNum-m-1+blockNum;
    			MPI_Send(&colNum,1,MPI_INT,destRank,100,MPI_COMM_WORLD);
    			MPI_Send(&recordNum,1,MPI_INT,destRank,101,MPI_COMM_WORLD);
    			MPI_Send(&blockNum,1,MPI_INT,destRank,tag,MPI_COMM_WORLD);
    			//MPI_Send(&flag,1,MPI_INT,destRank,102,MPI_COMM_WORLD);
    			//printf("%d\n",blockNum);
    			for(i=bound-blockNum;i<recordNum;i++)
    			{
    				for(j=0;j<colNum;j++)
    				{
    					buffer[j]=fullSet[i][j];
    					printf("%c ",buffer[j]);
    				}
    				MPI_Send(buffer,colNum,MPI_CHAR,destRank,tag,MPI_COMM_WORLD);
    				printf("\n");
    			}
    			printf("\n");
    		}
    
    		m++;
    	}
    
    
    	/*int s,rItemNum;
    	tItem *r;
    	r=(struct tItem*)malloc(sizeof(struct tItem));
    
    	s=0;
    	MPI_Recv(&rItemNum,1,MPI_INT,1,1000,MPI_COMM_WORLD,&status);
    	while(s<rItemNum)
    	{
    		for(j=0;j<=5;j++)
    		{
    			MPI_Recv(&r->itemset[j],1,MPI_CHAR,1,1001,MPI_COMM_WORLD,&status);
    		}
    		MPI_Recv(&r->count,1,MPI_INT,1,1002,MPI_COMM_WORLD,&status);
    		MPI_Recv(&r->tag,1,MPI_INT,0,1003,MPI_COMM_WORLD,&status);
    		s++;
    	}*/
    
    	endtime=MPI_Wtime();
    	printf("The master process tooks %f seconds.\n",endtime-starttime);
    	printf("\n");
    }
    
    static void slaveprocess(int rankNum)
    {
    
    	int rowNum,recordNum,colNum,tag,stag,rank,destRank,m,i,j;
    	char **fSet,*buffer;
    	double minSup=0.45;
        double minCon=0.50;
    	double starttime,endtime;
    
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	starttime=MPI_Wtime();
    
    	MPI_Recv(&colNum,1,MPI_INT,0,100,MPI_COMM_WORLD,&status);
    	MPI_Recv(&recordNum,1,MPI_INT,0,101,MPI_COMM_WORLD,&status);
    	//printf("%d\n",recordNum);
    	//printf("%d\n",colNum);
    
    	tag=rank;
    	MPI_Recv(&rowNum,1,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
    	printf("\n");
    	printf("\n");
        printf("The block in rank %d has %d records.\n",rank,rowNum);
        //MPI_Recv(&flag,1,MPI_INT,0,102,MPI_COMM_WORLD,&status);
    
    	buffer=(char*)malloc(colNum*sizeof(char*));
    	fSet=(char**)malloc(rowNum*sizeof(char*));
    	for(m=0;m<rowNum;m++)
    	{
    		fSet[m]=(char*)malloc(colNum*sizeof(char));
    	}
    
    	//接收进程0发送来的初始矩阵的一块
    	for(i=0;i<rowNum;i++)
    	{
    		MPI_Recv(buffer,colNum,MPI_CHAR,0,tag,MPI_COMM_WORLD,&status);
    		for(j=0;j<colNum;j++)
    		{
    			fSet[i][j]=buffer[j];
    		}
    	}
    
    	printf("\n");
    	printf("Rank %d receives part of the matrix is as follow:\n",rank);
    	printf("\n");
    	for(i=0;i<rowNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf("%c ",fSet[i][j]);
    		}
    		printf("\n");
    	}
    	printf("\n");
    
    	tItem *c;  //定义局部候选项集
    	tItem *cr;  //接收其他进程发送来的局部候选集
    	tItem *p;
    	int count=0,s,rCount,rTag;
    	char rItemset;
    
    
    	c=(struct tItem*)malloc(sizeof(struct tItem));
    	c=Find_candidate_1itemsets(fSet,minSup,rowNum,colNum);
    
    	destRank=1;
    	stag=1;
    
    	p=c;
    	while(p!=NULL)
    	{
    		count++;
    		p=p->next;
    	}
    
    	for(i=1;i<=rankNum-1;i++)  //向其他进程发送局部候选1项集
    	{
    		p=c;
    		if(i!=rank)
    		{
    			MPI_Send(&count,1,MPI_INT,destRank,14,MPI_COMM_WORLD);
    			while(p!=NULL)
    			{
    				MPI_Send(&p->itemset[p->tag],1,MPI_CHAR,destRank,15,MPI_COMM_WORLD);
    				MPI_Send(&p->count,1,MPI_INT,destRank,16,MPI_COMM_WORLD);
    				MPI_Send(&p->tag,1,MPI_INT,destRank,17,MPI_COMM_WORLD);
    				p=p->next;
    			}
    		}
    		destRank++;
    		stag++;
    	}
    
    	int reRank=1,reTag=1,itemNum;
    	for(j=1;j<=rankNum-1;j++)  //接收其他进程发送来的局部候选1项集
    	{
    		if(j!=rank)
    		{
    			s=0;
    			MPI_Recv(&itemNum,1,MPI_INT,reRank,14,MPI_COMM_WORLD,&status);
    			while(s<itemNum)
    			{
    				MPI_Recv(&rItemset,1,MPI_CHAR,reRank,15,MPI_COMM_WORLD,&status);
    				MPI_Recv(&rCount,1,MPI_INT,reRank,16,MPI_COMM_WORLD,&status);
    				MPI_Recv(&rTag,1,MPI_INT,reRank,17,MPI_COMM_WORLD,&status);
    
    				c=Combine_candidate_itemsets(c,rItemset,rCount);
    
    				//printf("  %c",rItemset);
    				//printf("  %d",rCount);
    				//printf("  %d",rTag);
    				//printf("\n");
    				s++;
    			}
    			printf("\n");
    		}
    		reRank++;
    		reTag++;
    	}
    
    	//输出合并后得到的全部候选1项集
    	p=c;
    	printf("Now every rank get the whole candidate itemsets as follow:\n\n");
    	while(p!=NULL)
    	{
    		printf("  %c",p->itemset[p->tag]);
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    
    	//由候选1项集生成频繁1项集
    	tItem *pp,*fHead,*fp,*temp;
    	int minNum;
    
    	fp=fHead=(struct tItem*)malloc(sizeof(struct tItem));
    	minNum=(int)((recordNum*minSup)+1);
    	//printf("%d",minNum);
    	pp=c;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			fHead->count=pp->count;
    			fHead->tag=pp->tag;
    			fHead->itemset[fHead->tag]=pp->itemset[pp->tag];
    			fHead->next=NULL;
    			break;
    		}
    		pp=pp->next;
    	}
    
    	fp=fHead;
    	pp=pp->next;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			temp=(struct tItem*)malloc(sizeof(struct tItem));
    			temp->count=pp->count;
    			temp->next=NULL;
    			temp->tag=pp->tag;
    			temp->itemset[temp->tag]=pp->itemset[pp->tag];
    			fp->next=temp;
    			fp=temp;
    			//printf("%c",pp->itemset[pp->tag]);
    		}
    		pp=pp->next;
    	}
    
    	/*printf("\n");
    	printf("--------The local frequence 1_itemsets--------\n");
    	fp=fHead;
    	while(fp!=NULL)
    	{
    		printf("  %c",fp->itemset[fp->tag]);
    		printf("  %d",fp->count);
    		printf("  %d",fp->tag);
    		printf("\n");
    		fp=fp->next;
    	}*/
    
    	tItem *f1,*kFrequence,*kf,*fis,*kfTemp;
    
    	//kFrequence=(struct tItem*)malloc(sizeof(struct tItem));
    	f1=(struct tItem*)malloc(sizeof(struct tItem));
    	//f1=Find_frequence_1itemsets(fullSet,minSup);
    
    	f1=fHead;
    
    	//system("pause");
    	//printf(" %d",rowNum);
    	//printf(" %d*&*&*&\n",colNum);
    	kFrequence=Gen_frequence(f1,rowNum,colNum,minNum,fSet);//*过连接和剪枝生成最终的频繁k项集
    
    	i=2;
    	while(i<=5)
    	{
    		kfTemp=Gen_frequence(kFrequence,rowNum,colNum,minNum,fSet);
    		if(kfTemp==NULL)
    		{
    			break;
    		}
    		kFrequence=kfTemp;
    		i++;
    	}
    	//MPI_Barrier(MPI_COMM_WORLD);
    	/*printf("^^^^^^^^^^^^^^^\n");
    	kf=kFrequence;
    	while(kf!=NULL)
    	{
    		for(j=0;j<=kf->tag;j++)
    		{
    			printf("  %c",kf->itemset[j]);
    		}
    		printf("  %d",kf->count);
    		printf("  %d",kf->tag);
    		printf("\n");
    		kf=kf->next;
    	}
    	printf("^^^^^^^^^^^^^^^\n");*/
    
    
    	printf("\n");
    	kf=kFrequence;
    	printf("--------The frequence %d_itemsets--------\n",kf->tag+1);
    	while(kf!=NULL)
    	{
    		for(j=0;j<=kf->tag;j++)
    		{
    			printf("  %c",kf->itemset[j]);
    		}
    		printf("  %d",kf->count);
    		printf("  %d",kf->tag);
    		printf("\n");
    		kf=kf->next;
    	}
    	printf("\n");
    
    	/*kf=kFrequence;
    	int kItemNum=0;
    	while(kf!=NULL)
    	{
    		kItemNum++;
    		kf=kf->next;
    	}
    
    	kf=kFrequence;
    	if(rank==1)
    	{
    		MPI_Send(&kItemNum,1,MPI_INT,0,1000,MPI_COMM_WORLD);
    		while(kf!=NULL)
    		{
    			for(j=0;j<=kf->tag;j++)
    			{
    				MPI_Send(&kf->itemset[j],1,MPI_CHAR,0,1001,MPI_COMM_WORLD);
    			}
    			MPI_Send(&kf->count,1,MPI_INT,0,1002,MPI_COMM_WORLD);
    			MPI_Send(&kf->tag,1,MPI_INT,0,1003,MPI_COMM_WORLD);
    			kf=kf->next;
    		}
    	}*/
    
    	kf=kFrequence;
    	printf("\n\nThe subset of the frequence is:\n\n");
    	while(kf!=NULL)
    	{
    
    		fis=Gen_kFrequence_subsets(kf);  //生成频繁k项集每项的子集的集合
    		fis=Compute_candidateItem_Num(fis,rowNum,colNum,fSet); //计算每一个子集的支持度
    
    		/*p=fis;
    		p=p->next;
    		while(p!=NULL)
    		{
    			for(i=0;i<=p->tag;i++)
    			{
    				printf("  %c",p->itemset[i]);
    			}
    			printf("  %d",p->count);
    			printf("  %d",p->tag);
    			printf("\n");
    			p=p->next;
    		}*/
    
    		printf("\n\nNow we get the association rules:\n\n");
    		p=fis;
    		p=p->next;
    		while(p!=NULL)
    		{
    			//printf("\n");
    			Gen_association_rules(kf,p,minCon);
    			printf("\n");
    			p=p->next;
    		}
    
    		kf=kf->next;
    	}
        endtime=MPI_Wtime();
    	printf("The slave process %d tooks %f seconds.\n",rank,endtime-starttime);
    	printf("\n");
    
    }
    
    struct tItem *Gen_frequence(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet)  //生成频繁项集
    {
    
    	tItem *candidate,*cp,*fp,*fpb,*p,*p1;
    	int i,j,k,m,count=0,rank,rankNum,destRank,stag;
    	//int i,m,n;
    	//char *buffer;
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	MPI_Comm_size(MPI_COMM_WORLD,&rankNum);
    
    	/*printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    	printf("##### %d\n",recordNum);
    	printf("##### %d\n",colNum);
    	printf("##### %d\n",minNum);
    	for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf(" %c",fullSet[i][j]);
    		}
    		printf("\n");
    	}
    	p=(struct tItem*)malloc(sizeof(struct tItem));
    	p=frequence;
    	while(p!=NULL)
    	{
    		printf("  %c",p->itemset[p->tag]);
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    	printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");*/
    
    
    	//for(k=2;frequence!=NULL;k++)
    	//while(candidate!=NULL)
    	//{
    		//printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    
    		candidate=apriori_gen(frequence);
    
    		if(candidate!=NULL)
    		{
    			candidate=Compute_candidateItem_Num(candidate,recordNum,colNum,fullSet);
    
    			destRank=1;
    			stag=1;
    
    			p=candidate;
    			p1=candidate;
    			while(p!=NULL)
    			{
    				count++;
    				p=p->next;
    			}
    			//printf(" #%d#\n",count);
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			for(i=1;i<=rankNum-1;i++)  //向其他进程发送局部候选k项集
    			{
    				p=candidate;
    				if(i!=rank)
    				{
    					MPI_Send(&count,1,MPI_INT,destRank,24,MPI_COMM_WORLD);
    					while(p!=NULL)
    					{
    						for(j=0;j<=p->tag;j++)
    						{
    							MPI_Send(&p->itemset[j],1,MPI_CHAR,destRank,25,MPI_COMM_WORLD);
    						}
    
    						MPI_Send(&p->count,1,MPI_INT,destRank,26,MPI_COMM_WORLD);
    						MPI_Send(&p->tag,1,MPI_INT,destRank,27,MPI_COMM_WORLD);
    						p=p->next;
    					}
    				}
    				destRank++;
    				stag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			int reRank=1,reTag=1,itemNum;
    			int rCount,rTag,s;
    			char rItemset;
    			tItem *r;
    			r=(struct tItem*)malloc(sizeof(struct tItem));
    			for(j=1;j<=rankNum-1;j++)  //接收其他进程发送来的局部候选k项集
    			{
    				if(j!=rank)
    				{
    					s=0;
    					MPI_Recv(&itemNum,1,MPI_INT,reRank,24,MPI_COMM_WORLD,&status);
    					while(s<itemNum)
    					{
    						for(m=0;m<=p1->tag;m++)
    						{
    							MPI_Recv(&r->itemset[m],1,MPI_CHAR,reRank,25,MPI_COMM_WORLD,&status);
    							//printf(" %c",r->itemset[m]);
    						}
    
    						MPI_Recv(&r->count,1,MPI_INT,reRank,26,MPI_COMM_WORLD,&status);
    						MPI_Recv(&r->tag,1,MPI_INT,reRank,27,MPI_COMM_WORLD,&status);
    
    
    						candidate=Combine_candidate_k_itemsets(candidate,r);
    
    						//printf(" %c",rItemset);
    						//printf("  %d",r->count);
    						//printf("  %d",r->tag);
    						//printf("\n");
    						s++;
    					}
    					//printf("\n");
    					//printf("&&&&&&&&&&&&&&&&&&&&&\n");
    				}
    				reRank++;
    				reTag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			/*cp=candidate;
    			count=0;
    			printf("--------The candidate %d_itemsets--------\n",cp->tag+1);
    			while(cp!=NULL)
    			{
    				for(j=0;j<=cp->tag;j++)
    				{
    					printf("  %c",cp->itemset[j]);
    				}
    				printf("  %d",cp->count);
    				printf("  %d",cp->tag);
    				printf("\n");
    				count++;
    				cp=cp->next;
    			}
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");*/
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			frequence=candidate;
    			fpb=(struct tItem*)malloc(sizeof(struct tItem));
    			fp=(struct tItem*)malloc(sizeof(struct tItem));
    			fpb=frequence;
    			fp=fpb->next;
    			//int minNum=(int)((recordNum*minSup)+1);
    			//printf("##########  %d\n",minNum);
    
    			while(fp!=NULL)  //删除后选项集中支持度小于最小支持度的项目,生成频繁项集
    			{
    
    				if(fpb->count<minNum)
    				{
    					frequence=fp;
    					fpb=fp;
    					fp=fp->next;
    				}
    
    
    				if(fp->count<minNum)
    				{
    					fp=fp->next;
    					fpb->next=fp;
    				}
    				else
    				{
    					fp=fp->next;
    					fpb=fpb->next;
    
    				}
    			}
    
    			/*fp=frequence;
    			count=0;
    			printf("--------The frequence %d_itemsets--------\n",fp->tag+1);
    			while(fp!=NULL)
    			{
    				//MPI_Barrier(MPI_COMM_WORLD);
    				for(j=0;j<=fp->tag;j++)
    				{
    					printf("  %c",fp->itemset[j]);
    				}
    				printf("  %d",fp->count);
    				printf("  %d",fp->tag);
    				printf("\n");
    				count++;
    				fp=fp->next;
    			}
    
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");*/
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    		}
    		else
    		{
    			//return (NULL);
    			//break;
    			frequence=NULL;
    		}
    		//printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    		//MPI_Barrier(MPI_COMM_WORLD);
    	//}
    
    	return (frequence);
    
    }
    
    struct tItem *Gen_frequence1(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet)  //生成频繁项集
    {
    
    	tItem *candidate,*cp,*fp,*fpb,*p,*p1;
    	int i,j,k,m,count=0,rank,rankNum,destRank,stag;
    	//int i,m,n;
    	//char *buffer;
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	MPI_Comm_size(MPI_COMM_WORLD,&rankNum);
    
    	//printf("*******************************************\n");
    
    	/*printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    	printf("##### %d\n",recordNum);
    	printf("##### %d\n",colNum);
    	printf("##### %d\n",minNum);
    	printf("##### %d\n",rank);
    	for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf(" %c",fullSet[i][j]);
    		}
    		printf("\n");
    	}
    	p=(struct tItem*)malloc(sizeof(struct tItem));
    	p=frequence;
    	while(p!=NULL)
    	{
    		for(j=0;j<=p->tag;j++)
    		{
    			printf("  %c",p->itemset[j]);
    		}
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    	printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");*/
    
    	//for(k=2;frequence!=NULL;k++)
    	//while(candidate!=NULL)
    	//{
    		printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    
    		candidate=apriori_gen(frequence);
    
    		/*p=candidate;
    		int t=0;
    		printf("测试结果\n");
    		while(p!=NULL)
    		{
    			for(j=0;j<=p->tag;j++)
    			{
    				printf("  %c",p->itemset[j]);
    			}
    			printf("  %d",p->count);
    			printf("  %d",p->tag);
    			printf("\n");
    			t++;
    			p=p->next;
    		}
    		printf("一共有 %d\n",t);
    		printf("结束测试\n");*/
    
    
    		/*if(rank==2)
    		{
    			printf("!!!!!!\n");
    			p=candidate;
    			while(p!=NULL)
    			{
    				for(j=0;j<=p->tag;j++)
    				{
    					printf("  %c",p->itemset[j]);
    				}
    				printf("  %d",p->count);
    				printf("  %d",p->tag);
    				printf("\n");
    				p=p->next;
    			}
    		}
    
    		printf("!!!!!!\n");*/
    
    
    		if(candidate!=NULL)
    		{
    			//printf("!!!!!!\n");
    			candidate=Compute_candidateItem_Num(candidate,recordNum,colNum,fullSet);
    
    			destRank=1;
    			stag=1;
    
    			p=candidate;
    			p1=candidate;
    			while(p!=NULL)
    			{
    				count++;
    				p=p->next;
    			}
    			//printf(" #%d#\n",count);
    			//printf("进行到这里\n");
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			for(i=1;i<=rankNum-1;i++)  //向其他进程发送局部候选k项集
    			{
    				p=candidate;
    				if(i!=rank)
    				{
    					MPI_Send(&count,1,MPI_INT,destRank,54,MPI_COMM_WORLD);
    					//printf("### %d ###\n",count);
    					while(p!=NULL)
    					{
    						for(j=0;j<=p->tag;j++)
    						{
    							MPI_Send(&p->itemset[j],1,MPI_CHAR,destRank,55,MPI_COMM_WORLD);
    							//printf(" %c",p->itemset[j]);
    						}
    
    						MPI_Send(&p->count,1,MPI_INT,destRank,56,MPI_COMM_WORLD);
    						MPI_Send(&p->tag,1,MPI_INT,destRank,57,MPI_COMM_WORLD);
    						//printf("  %d",p->count);
    						//printf("  %d",p->tag);
    						//printf("\n");
    						p=p->next;
    					}
    				}
    				destRank++;
    				stag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			int reRank=1,reTag=1,itemNum;
    			int rCount,rTag,s;
    			char rItemset;
    			tItem *r;
    			r=(struct tItem*)malloc(sizeof(struct tItem));
    			for(j=1;j<=rankNum-1;j++)  //接收其他进程发送来的局部候选k项集
    			{
    				if(j!=rank)
    				{
    					s=0;
    					MPI_Recv(&itemNum,1,MPI_INT,reRank,54,MPI_COMM_WORLD,&status);
    					while(s<itemNum)
    					{
    						for(m=0;m<=p1->tag;m++)
    						{
    							MPI_Recv(&r->itemset[m],1,MPI_CHAR,reRank,55,MPI_COMM_WORLD,&status);
    							//printf(" %c",r->itemset[m]);
    						}
    
    						MPI_Recv(&r->count,1,MPI_INT,reRank,56,MPI_COMM_WORLD,&status);
    						MPI_Recv(&r->tag,1,MPI_INT,reRank,57,MPI_COMM_WORLD,&status);
    
    
    						candidate=Combine_candidate_k_itemsets(candidate,r);
    
    						//printf(" %c",rItemset);
    						//printf("  %d",r->count);
    						//printf("  %d",r->tag);
    						//printf("\n");
    						s++;
    					}
    					//printf("\n");
    					printf("&&&&&&&&&&&&&&&&&&&&&\n");
    				}
    				reRank++;
    				reTag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			cp=candidate;
    			count=0;
    			printf("--------The candidate %d_itemsets--------\n",cp->tag+1);
    			while(cp!=NULL)
    			{
    				for(j=0;j<=cp->tag;j++)
    				{
    					printf("  %c",cp->itemset[j]);
    				}
    				printf("  %d",cp->count);
    				printf("  %d",cp->tag);
    				printf("\n");
    				count++;
    				cp=cp->next;
    			}
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			frequence=candidate;
    			fpb=(struct tItem*)malloc(sizeof(struct tItem));
    			fp=(struct tItem*)malloc(sizeof(struct tItem));
    			fpb=frequence;
    			fp=fpb->next;
    			//int minNum=(int)((recordNum*minSup)+1);
    			//printf("##########  %d\n",minNum);
    
    			while(fp!=NULL)  //删除后选项集中支持度小于最小支持度的项目,生成频繁项集
    			{
    
    				if(fpb->count<minNum)
    				{
    					frequence=fp;
    					fpb=fp;
    					fp=fp->next;
    				}
    
    
    				if(fp->count<minNum)
    				{
    					fp=fp->next;
    					fpb->next=fp;
    				}
    				else
    				{
    					fp=fp->next;
    					fpb=fpb->next;
    
    				}
    			}
    
    			fp=frequence;
    			count=0;
    			printf("--------The frequence %d_itemsets--------\n",fp->tag+1);
    			while(fp!=NULL)
    			{
    				//MPI_Barrier(MPI_COMM_WORLD);
    				for(j=0;j<=fp->tag;j++)
    				{
    					printf("  %c",fp->itemset[j]);
    				}
    				printf("  %d",fp->count);
    				printf("  %d",fp->tag);
    				printf("\n");
    				count++;
    				fp=fp->next;
    			}
    
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    		}
    		else
    		{
    			//return (NULL);
    			//break;
    			//frequence=NULL;
    		}
    		//printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    		//MPI_Barrier(MPI_COMM_WORLD);
    	//}
    
    	return (frequence);
    
    }
    
    bool Has_infrequence_subset(tItem *pp, tItem *f)  //判断候选k项集的子集是否在频繁k-1项集中
    {
    	tItem *p,*fp;  //储存*pp中的k-1项子集
    	int i=0,j,m=0;
    	bool flag;
    
    	while(i<=pp->tag)
    	{
    
    		p=(struct tItem*)malloc(sizeof(struct tItem));
    
    		for(j=0;j<pp->tag;j++)
    		{
    			if(j==i)
    			{
    				j++;
    			}
    			else
    			{
    				p->itemset[j]=pp->itemset[j];
    			}
    		}
    
    		fp=f;
    		while(fp!=NULL)
    		{
    			while(m<=fp->tag)
    			{
    				if(p->itemset[m]==fp->itemset[m])
    				{
    					m++;
    				}
    				else
    				{
    					flag=false;
    					m++;
    				}
    			}
    
    			flag=true;
    			fp=fp->next;
    		}
    
    		i++;
    	}
    
    
    	return (flag);
    }
    
    struct tItem *Gen_candidate_itemset(tItem *cp,tItem *cp1,tItem *cp2)  //生成后选项
    {
    	int i=0;
    
    	while(i<=cp1->tag)
    	{
    		cp->itemset[i]=cp1->itemset[i];
    		//cp->tag=cp1->tag+1;
    		i++;
    	}
    
    	cp->tag=cp1->tag+1;
    	cp->itemset[i]=cp2->itemset[i-1];
    	cp->count=0;
    	cp->next=NULL;
    
    	return (cp);
    }
    
    bool Is_before_equal(tItem *pp1, tItem *pp2)  //判断两个项目集的前k-1项是否相同
    {
    	int i=0;
    	bool flag;
    
    	while(i<pp1->tag)
    	{
    		if(pp1->itemset[i]==pp2->itemset[i])
    		{
    			flag=true;
    		}
    		else
    		{
    			flag=false;
    			break;
    		}
    		i++;
    	}
    
    	return (flag);
    }
    
    struct tItem *Compute_candidateItem_Num(tItem *c,int recordNum, int colNum, char **fullSet)  //计算候选项集中每一项的最小支持数
    {
    	tItem *cp;
    	int i,j,m,n;
    	char *buffer;
    
        cp=c;
    
    	while(cp!=NULL) //统计每个候选项集的计数
    	{
    		for(i=0;i<recordNum;i++)
    		{
    
    			bool flag=true;
    			buffer=(char*)malloc(colNum*sizeof(char*));
    
    			for(j=0;j<colNum;j++)
    			{
    				buffer[j]=fullSet[i][j];
    			}
    
    			m=0;
    			while(m<=cp->tag)
    			{
    				bool flag1=false;
    				for(n=0;n<colNum;n++)
    				{
    					if(cp->itemset[m]==buffer[n])
    					{
    						flag1=true;
    						break;
    					}
    
    					flag1=false;
    				}
    
    				if(flag1==false)
    				{
    					flag=false;
    				}
    				m++;
    			}
    
    			if(flag==true)
    			{
    				cp->count++;
    			}
    		}
    
    		cp=cp->next;
    	}
    
    	return (c);
    }
    
    struct tItem *apriori_gen(tItem *freq)  //生成候选项集
    {
    	tItem *p1,*p2,*head,*cHead,*p,*pTail;
    	int tag,i=0;
    	//bool flag;
    
    	if(freq->next!=NULL)
    	{
    
    		head=freq;
    		p1=head;
    		p2=head;
    		p2=p2->next;
    
    		p=(struct tItem*)malloc(sizeof(struct tItem));
    		tag=p1->tag+1;
    
    		if(tag==1)//生成候选2项集
    		{
    			//printf("############\n");
    			while(i<=tag)
    			{
    				if(i<=tag-1)
    				{
    					//p->count=p1->count;
    					p->itemset[i]=p1->itemset[i];
    					p->tag=tag;
    				}
    				else
    				{
    					//p->count=p2->count;
    					p->itemset[i]=p2->itemset[i-1];
    					p->tag=tag;
    				}
    				i++;
    			}
    			p->count=0;
    			p->next=NULL;
    
    			/*printf("  %c",p->itemset[0]);
    			printf("  %c",p->itemset[1]);
    			printf("  %d",p->tag);*/
    
    			pTail=p;
    			cHead=p;
    			p2=p2->next;
    			//printf("#%c#",p1->itemset[p1->tag]);
    
    			while(p1!=NULL)
    			{
    				while(p2!=NULL)
    				{
    					p=(struct tItem*)malloc(sizeof(struct tItem));
    					tag=p1->tag+1;
    					i=0;
    
    					while(i<=tag)
    					{
    						if(i<=tag-1)
    						{
    							//p->count=p1->count;
    							p->itemset[i]=p1->itemset[i];
    							//printf("%c",p->itemset[i]);
    							p->tag=tag;
    						}
    						else
    						{
    							//p->count=p2->count;
    							p->itemset[i]=p2->itemset[i-1];
    							//printf("%c",p->itemset[i]);
    							p->tag=tag;
    						}
    						i++;
    					}
    					p->count=0;
    					p->next=NULL;
    
    					//flag=Has_infrequence_subset(cHead,freq);
    
    					//if(flag==true)
    					//{
    						pTail->next=p;
    						pTail=p;
    					//}
    					p2=p2->next;
    				}
    
    				p1=p1->next;
    				p2=p1->next;
    
    				if(p2==NULL)
    				{
    					break;
    				}
    			}
    		}
    		/*else if(p1->tag==3)
    		{
    			if(Is_before_equal(p1,p2))
    			{
    				//printf("进行到这里\n");
    				//p=(struct tItem*)malloc(sizeof(struct tItem));
    				//cHead=(struct tItem*)malloc(sizeof(struct tItem));
    				//pTail=(struct tItem*)malloc(sizeof(struct tItem));
    
    				p=Gen_candidate_itemset(p,p1,p2);
    				pTail=p;
    				cHead=p;
    				p2=p2->next;
    
    
    				while((p1!=NULL)&&(p2!=NULL))
    				{
    					//while(Is_before_equal(p1,p2))
    					while(p2!=NULL)
    					{
    						if(Is_before_equal(p1,p2))
    						{
    							p=(struct tItem*)malloc(sizeof(struct tItem));
    							tag=p1->tag+1;
    
    							p=Gen_candidate_itemset(p,p1,p2);
    
    							if(Has_infrequence_subset(p, freq))
    							{
    								pTail->next=p;
    								pTail=p;
    							}
    						}
    						//printf("进行到这里\n");
    						p2=p2->next;
    
    					}
    
    					p1=p1->next;
    					p2=p1->next;
    				}
    			}
    			//printf("进行到这里\n");
    		}*/
    		else//生成侯选2项集以上的候选集(标记cHead时候有问题)
    		{
    
    			/*while(p2!=NULL)
    			{
    				if(Is_before_equal(p1,p2))
    				{
    					break;
    				}
    				p2=p2->next;
    			}*/
    
    			if(Is_before_equal(p1,p2))
    			{
    				//p=(struct tItem*)malloc(sizeof(struct tItem));
    				//cHead=(struct tItem*)malloc(sizeof(struct tItem));
    				//pTail=(struct tItem*)malloc(sizeof(struct tItem));
    
    				p=Gen_candidate_itemset(p,p1,p2);
    				pTail=p;
    				cHead=p;
    				p2=p2->next;
    
    
    				while((p1!=NULL)&&(p2!=NULL))
    				{
    					//while(Is_before_equal(p1,p2))
    					while(p2!=NULL)
    					{
    						if(Is_before_equal(p1,p2))
    						{
    							p=(struct tItem*)malloc(sizeof(struct tItem));
    							tag=p1->tag+1;
    
    							p=Gen_candidate_itemset(p,p1,p2);
    
    							if(Has_infrequence_subset(p, freq))
    							{
    								pTail->next=p;
    								pTail=p;
    							}
    						}
    
    						p2=p2->next;
    
    					}
    
    					p1=p1->next;
    					p2=p1->next;
    				}
    			}
    			else
    			{
    				p1=p1->next;
    				p2=p2->next;
    				p=Gen_candidate_itemset(p,p1,p2);
    				pTail=p;
    				cHead=p;
    				p2=p2->next;
    
    
    				while((p1!=NULL)&&(p2!=NULL))
    				{
    					while(Is_before_equal(p1,p2))
    					{
    						p=(struct tItem*)malloc(sizeof(struct tItem));
    						tag=p1->tag+1;
    
    						p=Gen_candidate_itemset(p,p1,p2);
    
    						if(Has_infrequence_subset(p, freq))
    						{
    							pTail->next=p;
    							pTail=p;
    						}
    
    						p2=p2->next;
    
    					}
    
    					p1=p1->next;
    					p2=p1->next;
    				}
    				//cHead=NULL;
    			}
    		}
    	}
    	else
    	{
    		cHead=NULL;
    	}
    	/*tItem *pp;
    	int j,count=0;
    
    	pp=cHead;
    	printf("--------The candidate %d_itemsets--------\n",pp->tag+1);
    	while(pp!=NULL)
    	{
    		for(j=0;j<=tag;j++)
    		{
    			printf("  %c",pp->itemset[j]);
    		}
    		printf("  %d",pp->count);
    		printf("  %d",pp->tag);
    		printf("\n");
    		count++;
    		pp=pp->next;
    	}
    	printf("\n");
    	printf("The total number is: %d\n",count);
    	printf("\n");*/
    
    	return (cHead);
    }
    
    struct tItem *Combine_candidate_k_itemsets(tItem *lc,tItem *r)
    {
    	tItem *p;
    	int i,j;
    	bool flag,flagj;
    
    	//printf("**************************\n");
    	p=lc;
    	while(p!=NULL)
    	{
    		//flag=true;
    		for(i=0;i<=r->tag;i++)
    		{
    			flagj=false;
    			for(j=0;j<=p->tag;j++)
    			{
    				if(p->itemset[j]==r->itemset[i])
    				{
    					flagj=true;
    					break;
    				}
    			}
    			if(flagj==false)
    			{
    				flag=false;
    				break;
    			}
    			else
    			{
    				flag=true;
    			}
    		}
    		if(flag==true)
    		{
    			p->count=p->count+r->count;
    			break;
    		}
    		p=p->next;
    	}
    
    	return (lc);
    }
    
    struct tItem *Combine_candidate_itemsets(tItem *lc, char rItemset, int rCount)
    {
    	tItem *p;
    
    	p=lc;
    	while(p!=NULL)
    	{
    		if(p->itemset[p->tag]==rItemset)
    		{
    			p->count=p->count+rCount;
    		}
    		p=p->next;
    	}
    
    	return (lc);
    }
    
    struct tItem *Find_candidate_1itemsets(char **fullSet2, double sup2,int recordNum,int colNum)  //生成频繁1项集
    {
    	tItem *cHead,*fHead;
    	tItem *p1,*pHead,*pTail,*p;
    	tItem *fp,*temp,*pp;
    	fp=fHead=(struct tItem*)malloc(sizeof(struct tItem));
    
    	int i,j,count=0;
    	bool flag;
    
    	p=pHead=pTail=(struct tItem*)malloc(sizeof(struct tItem));
    	cHead=NULL;
    	pHead->tag=0;
    	pHead->itemset[pHead->tag]=fullSet2[0][0];
    	pHead->count=0;
    	pHead->next=NULL;
    
    
    	int minNum=(int)((recordNum*sup2)+1);
    	//printf("%d",minNum);
    
    	cHead=pHead;
    	for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			p=cHead;
    			while(p!=NULL)
    			{
    				if(p->itemset[p->tag]==fullSet2[i][j])
    				{
    					p->count++;
    					flag=false;
    					break;
    				}
    				else
    				{
    					pTail=p;
    					p=p->next;
    					flag=true;
    				}
    			}
    			if(flag==true)
    			{
    				p1=(struct tItem*)malloc(sizeof(struct tItem));
    				p1->tag=0;
    				p1->itemset[p1->tag]=fullSet2[i][j];
    				p1->count=1;
    				pTail->next=p1;
    				p1->next=NULL;
    			}
    		}
    	}
    
    	printf("\n");
    	printf("--------The local candidate 1_itemsets--------\n");
    	p=cHead;
    	while(p!=NULL)
    	{
    		count++;
    		printf("  %c",p->itemset[p->tag]);
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    	printf("\n");
    	printf("In all, there are %d candidate 1_itemsets.\n",count);
    
    	/*pp=cHead;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			fHead->count=cHead->count;
    			fHead->tag=cHead->tag;
    			fHead->itemset[fHead->tag]=cHead->itemset[cHead->tag];
    			fHead->next=NULL;
    			break;
    		}
    		pp=pp->next;
    	}
    
    	fp=fHead;
    	pp=pp->next;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			temp=(struct tItem*)malloc(sizeof(struct tItem));
    			temp->count=pp->count;
    			temp->next=NULL;
    			temp->tag=pp->tag;
    			temp->itemset[temp->tag]=pp->itemset[pp->tag];
    			fp->next=temp;
    			fp=temp;
    			//printf("%c",pp->itemset[pp->tag]);
    		}
    		pp=pp->next;
    	}
    
    	printf("\n");
    	printf("--------The frequence 1_itemsets--------\n");
    	fp=fHead;
    	while(fp!=NULL)
    	{
    		printf("  %c",fp->itemset[fp->tag]);
    		printf("  %d",fp->count);
    		printf("  %d",fp->tag);
    		printf("\n");
    		fp=fp->next;
    	}
    
    	return (fHead);*/
    	return (cHead);
    }
    
    struct tItem *Gen_kFrequence_subsets(tItem *f)  //生成频繁k项集每项的子集的集合
    {
    	tItem *FreItemSubset;
    	//tItem *p;
    	int ListLength=f->tag+1;
    	int i;
    	//buffer=(int*)malloc(col*sizeof(int*))
    	char *List,*Buffer;
    
    	List=(char*)malloc(ListLength*sizeof(char*));
    	Buffer=(char*)malloc(ListLength*sizeof(char*));
    
    	for(i=0;i<f->tag+1;i++)
    	{
    		List[i]=f->itemset[i];
    		//printf(" %c",List[i]);
    	}
    
    	FreItemSubset=(struct tItem*)malloc(sizeof(struct tItem));
    	FreItemSubset->next=NULL;
    	FreItemSubset=SubSet(List,0,Buffer,0,FreItemSubset,ListLength);
    
    	/*p=FreItemSubset;
    	p=p->next;
    	while(p!=NULL)
    	{
    		for(i=0;i<=p->tag;i++)
    		{
    			printf("  %c",p->itemset[i]);
    		}
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}*/
    
    	return (FreItemSubset);
    }
    
    int Index(char *List, char c, int ListLength)
    {
         for(int i=0; i<=ListLength-1; i++)
         {
                  if(c==List[i])
                  {
                        return i;
                        break;
                  }
         }
         return -1;
    }
    
    struct tItem *SubSet(char *List, int m, char *Buffer, int flag, tItem *fis,int ListLength)
    {
    	 //fis->next=NULL;
    	 //printf("###\n");
         if(m <= ListLength-1)
         {
              /*if(m==0)
              {
                      Buffer[0]=List[0];
              }*/
              //Buffer[flag]=List[m];
              /*if(flag==0)
              {
                    Buffer[flag]=List[m];
              }*/
    
              for(int i=(flag==0) ? 0 : Index(List,Buffer[flag-1],ListLength)+1; i<=ListLength-1; i++)
              //当flag==0时,Buffer中没有任何元素,此时i=[0...ListLength-1]
              //当flag>0时,找到Buffer中的最后一个元素在集合List中的位置i,把[i....ListLength-1]
              //处的元素,加到Buffer元素的最后面
              {
    
                    Buffer[flag]=List[i];
                    fis=AddToSubset(Buffer,flag,fis);
                    //Output(Buffer,flag);
                    SubSet(List, m+1, Buffer,flag+1,fis,ListLength);
              }
         }
    
    	 /*tItem *p;
    	 p=fis;
    	 while(p!=NULL)
    	 {
    	 	 for(int j=0;j<=p->tag;j++)
    		 {
    		 	 printf("  %c",p->itemset[j]);
    		 }
    		 printf("  %d",p->count);
    		 printf("  %d",p->tag);
    		 printf("\n");
    		 p=p->next;
    	 }*/
    
         return (fis);
    }
    
    struct tItem *AddToSubset(char *Buffer, int flag, tItem *fis)  //将子集添加到链表中
    {
    
    	tItem *p;
    	int i=0;
    
    	p=(struct tItem*)malloc(sizeof(struct tItem));
    
    	p->count=0;
    	p->tag=flag;
    
    	while(i<=flag)
    	{
    		p->itemset[i]=Buffer[i];
    		i++;
    	}
    
    	p->next=fis->next;
    	fis->next=p;
    
    	return (fis);
    }
    
    void Gen_association_rules(tItem *kf, tItem *p, double minCon)  //生成关联规则
    {
    	int i=0;
    	int j=0;
    	double m;
    	bool flag;
    
    	//printf("@@@@ %f\n",minCon);
    
    	m=(double)(kf->count)/(double)(p->count);
    
    	if(m>=minCon)
    	{
    		printf("\n");
    		while(i<=p->tag)
    		{
    			if(i!=p->tag)
    			{
    				printf("%c ",p->itemset[i]);
    			}
    			else
    			{
    				printf("%c ",p->itemset[i]);
    			}
    			i++;
    		}
    
    		printf("=====> ");
    
    		while(j<=kf->tag)
    		{
    			if(j!=kf->tag)
    			{
    				for(i=0;i<=p->tag;i++)
    				{
    					if(kf->itemset[j]==p->itemset[i])
    					{
    						flag=false;
    						break;
    					}
    					else
    					{
    						flag=true;
    					}
    				}
    				if(flag==true)
    				{
    					printf("%c ",kf->itemset[j]);
    				}
    
    			}
    			else
    			{
    				for(i=0;i<=p->tag;i++)
    				{
    					if(kf->itemset[j]==p->itemset[i])
    					{
    						flag=false;
    						break;
    					}
    					else
    					{
    						flag=true;
    					}
    				}
    				if(flag==true)
    				{
    					printf("%c ",kf->itemset[j]);
    				}
    
    			}
    			j++;
    		}
    
    		//cout << setprecision<< m <<endl;
    		m=(double)(p->count)/(double)(kf->count);
    		printf("    Confidence : %f",m);
    
    		printf("\n");
    	}
    }
    Last edited by Cabhan; 12-01-2011 at 06:48 AM. Reason: Enclosing in [code] tags

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,539
    Please enclose your code inside code blocks, like this:

    Code:
    #include "stdio.h"
    #include <fstream>
    #include <iostream>
    #include "stdlib.h"
    #include "string.h"
    #include <iomanip>
    #include <time.h>
    #include "mpi.h"
    
    struct tItem
    {
    	int tag;
    	char itemset[15];
    	int count;
    	struct tItem *next;
    };
    
    #define LEN sizeof(struct tItem);
    
    int GetColNum(char fileName1[]);  //读取源数据的列数
    int GetRecordNum(char fileName2[]);  //读取源数据的行数
    int Index(char *List, char c, int ListLength);
    void GetSourceFile(char fileName[],int recordNum,int colNum);  //获取源数据
    void FindFrequence1itemsets(char **fullSet1,double sup);
    void Gen_association_rules(tItem *kf, tItem *p, double minCon);  //生成关联规则
    void SetminSup();  //设置最小值支持度
    void SetminCon();  //设置最小置信度
    struct tItem *Find_candidate_1itemsets(char **fullSet2, double sup2,int recordNum,int colNum);  //生成频繁1项集
    struct tItem *apriori_gen(tItem *freq);  //生成候选项集
    struct tItem *Gen_candidate_itemset(tItem *cp,tItem *cp1,tItem *cp2);  //生成后选项
    struct tItem *Gen_frequence(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet);  //生成频繁项集
    struct tItem *Gen_frequence1(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet);
    struct tItem *Compute_candidateItem_Num(tItem *c,int recordNum, int colNum, char **fullSet);  //计算候选项集中每一项的最小支持数
    struct tItem *Gen_kFrequence_subsets(tItem *f);  //生成频繁k项集每项的子集的集合
    struct tItem *SubSet(char *List, int m, char *Buffer, int flag, tItem *fis, int ListLength);  //生成频繁集中每一项的子集
    struct tItem *AddToSubset(char *Buffer, int flag, tItem *fis);  //将一个子集加入链表
    struct tItem *Combine_candidate_itemsets(tItem *lc, char rItemset, int rCount);  //合并局部候选1项集成为总的候选1项集
    struct tItem *Combine_candidate_k_itemsets(tItem *lc,tItem *r);
    bool Has_infrequence_subset(tItem *pp, tItem *f);  //判断候选k项集的子集是否在频繁k-1项集中
    bool Is_before_equal(tItem *pp1, tItem *pp2);  //判断两个项目集的前k-1项是否相同
    static void masterprocess(int rankNum);
    static void slaveprocess(int rankNum);
    
    char **fullSet;
    int colNum=0;
    int recordNum=0;
    double minSup=0.37;
    double minCon=0.50;
    
    int main(int argc,char *argv[])
    {
    	int rank,size;
    
    	MPI_Init(&argc,&argv);
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	MPI_Comm_size(MPI_COMM_WORLD,&size);
    
    	if(rank==0)
    	{
    		//printf("#%d#\n",size);
    		masterprocess(size);
    	}
    	else
    	{
    		slaveprocess(size);
    	}
    
    	MPI_Finalize();
    	return(0);
    }
    
    void SetminSup()
    {
    	cout<<"Please input the minSup: ";
    	cin>>minSup;
    }
    
    void SetminCon()
    {
    	cout<<"Please input the minCon: ";
    	cin>>minCon;
    }
    
    int GetColNum(char fileName1[])  //读取源数据的列数
    {
    	ifstream file(fileName1);
    
    	char ch;
    	//int colNum=0;
    	while(ch!='\n')
    	{
    		if(ch==',')
    		{
    			colNum++;
    			ch=file.get();
    		}
    		ch=file.get();
    	}
    	//colNum++;
    	file.close();
    	return colNum;
    }
    
    
    int GetRecordNum(char fileName2[])  //读取源数据的行数
    {
    	ifstream file(fileName2);
    
    	char ch=NULL;
    	//int recordNum=0;
    
    	while(!file.eof())
    	{
    		if(ch=='\n')
    		{
    			recordNum++;
    			ch=file.get();
    		}
    		ch=file.get();
    	}
    
    	file.close();
    	return recordNum;
    }
    
    void GetSourceFile(char fileName[],int rNum,int cNum)  //获取源数据读入数组fullSet[][]中
    {
    	ifstream file(fileName);
    
    	char ch;
    	//char **fullSet;
    	int item=0;
    	int i=0,j=0;
    	int temp1='A';
    	int temp2='a';
    
    	/*b=(int**)malloc(row*sizeof(int*));
    	for(i=0;i<row;i++)
    	{
    		b[i]=(int*)malloc(col*sizeof(int));
    	}*/
    
    	fullSet=(char**)malloc(rNum*sizeof(char*));
    	for(int m=0;m<rNum;m++)
    	{
    		fullSet[m]=(char*)malloc(cNum*sizeof(char));
    	}
    
    	while(i<rNum)
    	{
    		while(j<colNum)
    		{
    			if(ch==',')
    			{
    				item++;
    				ch=file.get();
    				if(ch=='T')
    				{
    					fullSet[i][j]=(char)(item+temp1);
    					//printf("%c",fullSet[i][j]);
    				}
    				if(ch=='F')
    				{
    					fullSet[i][j]=(char)(item+temp2);
    					//printf("%c",fullSet[i][j]);
    				}
    				j++;
    			}
    			ch=file.get();
    		}
    		//printf("\n");
    		j=0;
    		ch=file.get();
    		item=0;
    		i++;
    	}
    	file.close();
    }
    
    static void masterprocess(int rankNum)
    {
    
    	char inputFileName[]="c:\\shujuku.txt";
    	char *buffer;
    	int colNum,recordNum,blockNum,bound,i,j,m,k;
    	int rank,destRank,tag;
        double starttime,endtime;
    
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
        starttime=MPI_Wtime();
    
    	//SetminSup();
    	//SetminCon();
    
    	//printf("#%d#\n",rankNum);
    
    	colNum=GetColNum(inputFileName);
    	recordNum=GetRecordNum(inputFileName);
    
    	//system("pause");
    	printf("recordNum= %d", recordNum);
    	printf("\n");
    	printf("colNum= %d",colNum);
    	printf("\n");
    
    	GetSourceFile(inputFileName,recordNum,colNum);
    
    	//system("pause");
    
    	/*for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf(" %c ",fullSet[i][j]);
    		}
    		printf("\n");
    	}*/
    
    	blockNum=recordNum/(rankNum-1);
    	bound=blockNum*(rankNum-1);
    	//printf("%d\n",blockNum);
    
    	m=0;
    	k=1;
    	destRank=1;
    	tag=1;
    	buffer=(char*)malloc(colNum*sizeof(char*));
    	while(m!=recordNum)
    	{
    		if(m==(k*blockNum-1)&&(m!=bound-1))
    		{
    			MPI_Send(&colNum,1,MPI_INT,destRank,100,MPI_COMM_WORLD);
    			MPI_Send(&recordNum,1,MPI_INT,destRank,101,MPI_COMM_WORLD);
    			MPI_Send(&blockNum,1,MPI_INT,destRank,tag,MPI_COMM_WORLD);
    			//printf("%d\n",blockNum);
    			for(i=((k-1)*blockNum);i<k*blockNum;i++)
    			{
    				for(j=0;j<colNum;j++)
    				{
    					buffer[j]=fullSet[i][j];
    					printf("%c ",buffer[j]);
    				}
    				MPI_Send(buffer,colNum,MPI_CHAR,destRank,tag,MPI_COMM_WORLD);
    				printf("\n");
    			}
    			printf("\n");
    			destRank++;
    			tag++;
    			k++;
    		}
    		else if(m==bound-1)
    		{
    			blockNum=recordNum-m-1+blockNum;
    			MPI_Send(&colNum,1,MPI_INT,destRank,100,MPI_COMM_WORLD);
    			MPI_Send(&recordNum,1,MPI_INT,destRank,101,MPI_COMM_WORLD);
    			MPI_Send(&blockNum,1,MPI_INT,destRank,tag,MPI_COMM_WORLD);
    			//MPI_Send(&flag,1,MPI_INT,destRank,102,MPI_COMM_WORLD);
    			//printf("%d\n",blockNum);
    			for(i=bound-blockNum;i<recordNum;i++)
    			{
    				for(j=0;j<colNum;j++)
    				{
    					buffer[j]=fullSet[i][j];
    					printf("%c ",buffer[j]);
    				}
    				MPI_Send(buffer,colNum,MPI_CHAR,destRank,tag,MPI_COMM_WORLD);
    				printf("\n");
    			}
    			printf("\n");
    		}
    
    		m++;
    	}
    
    
    	/*int s,rItemNum;
    	tItem *r;
    	r=(struct tItem*)malloc(sizeof(struct tItem));
    
    	s=0;
    	MPI_Recv(&rItemNum,1,MPI_INT,1,1000,MPI_COMM_WORLD,&status);
    	while(s<rItemNum)
    	{
    		for(j=0;j<=5;j++)
    		{
    			MPI_Recv(&r->itemset[j],1,MPI_CHAR,1,1001,MPI_COMM_WORLD,&status);
    		}
    		MPI_Recv(&r->count,1,MPI_INT,1,1002,MPI_COMM_WORLD,&status);
    		MPI_Recv(&r->tag,1,MPI_INT,0,1003,MPI_COMM_WORLD,&status);
    		s++;
    	}*/
    
    	endtime=MPI_Wtime();
    	printf("The master process tooks %f seconds.\n",endtime-starttime);
    	printf("\n");
    }
    
    static void slaveprocess(int rankNum)
    {
    
    	int rowNum,recordNum,colNum,tag,stag,rank,destRank,m,i,j;
    	char **fSet,*buffer;
    	double minSup=0.45;
        double minCon=0.50;
    	double starttime,endtime;
    
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	starttime=MPI_Wtime();
    
    	MPI_Recv(&colNum,1,MPI_INT,0,100,MPI_COMM_WORLD,&status);
    	MPI_Recv(&recordNum,1,MPI_INT,0,101,MPI_COMM_WORLD,&status);
    	//printf("%d\n",recordNum);
    	//printf("%d\n",colNum);
    
    	tag=rank;
    	MPI_Recv(&rowNum,1,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
    	printf("\n");
    	printf("\n");
        printf("The block in rank %d has %d records.\n",rank,rowNum);
        //MPI_Recv(&flag,1,MPI_INT,0,102,MPI_COMM_WORLD,&status);
    
    	buffer=(char*)malloc(colNum*sizeof(char*));
    	fSet=(char**)malloc(rowNum*sizeof(char*));
    	for(m=0;m<rowNum;m++)
    	{
    		fSet[m]=(char*)malloc(colNum*sizeof(char));
    	}
    
    	//接收进程0发送来的初始矩阵的一块
    	for(i=0;i<rowNum;i++)
    	{
    		MPI_Recv(buffer,colNum,MPI_CHAR,0,tag,MPI_COMM_WORLD,&status);
    		for(j=0;j<colNum;j++)
    		{
    			fSet[i][j]=buffer[j];
    		}
    	}
    
    	printf("\n");
    	printf("Rank %d receives part of the matrix is as follow:\n",rank);
    	printf("\n");
    	for(i=0;i<rowNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf("%c ",fSet[i][j]);
    		}
    		printf("\n");
    	}
    	printf("\n");
    
    	tItem *c;  //定义局部候选项集
    	tItem *cr;  //接收其他进程发送来的局部候选集
    	tItem *p;
    	int count=0,s,rCount,rTag;
    	char rItemset;
    
    
    	c=(struct tItem*)malloc(sizeof(struct tItem));
    	c=Find_candidate_1itemsets(fSet,minSup,rowNum,colNum);
    
    	destRank=1;
    	stag=1;
    
    	p=c;
    	while(p!=NULL)
    	{
    		count++;
    		p=p->next;
    	}
    
    	for(i=1;i<=rankNum-1;i++)  //向其他进程发送局部候选1项集
    	{
    		p=c;
    		if(i!=rank)
    		{
    			MPI_Send(&count,1,MPI_INT,destRank,14,MPI_COMM_WORLD);
    			while(p!=NULL)
    			{
    				MPI_Send(&p->itemset[p->tag],1,MPI_CHAR,destRank,15,MPI_COMM_WORLD);
    				MPI_Send(&p->count,1,MPI_INT,destRank,16,MPI_COMM_WORLD);
    				MPI_Send(&p->tag,1,MPI_INT,destRank,17,MPI_COMM_WORLD);
    				p=p->next;
    			}
    		}
    		destRank++;
    		stag++;
    	}
    
    	int reRank=1,reTag=1,itemNum;
    	for(j=1;j<=rankNum-1;j++)  //接收其他进程发送来的局部候选1项集
    	{
    		if(j!=rank)
    		{
    			s=0;
    			MPI_Recv(&itemNum,1,MPI_INT,reRank,14,MPI_COMM_WORLD,&status);
    			while(s<itemNum)
    			{
    				MPI_Recv(&rItemset,1,MPI_CHAR,reRank,15,MPI_COMM_WORLD,&status);
    				MPI_Recv(&rCount,1,MPI_INT,reRank,16,MPI_COMM_WORLD,&status);
    				MPI_Recv(&rTag,1,MPI_INT,reRank,17,MPI_COMM_WORLD,&status);
    
    				c=Combine_candidate_itemsets(c,rItemset,rCount);
    
    				//printf("  %c",rItemset);
    				//printf("  %d",rCount);
    				//printf("  %d",rTag);
    				//printf("\n");
    				s++;
    			}
    			printf("\n");
    		}
    		reRank++;
    		reTag++;
    	}
    
    	//输出合并后得到的全部候选1项集
    	p=c;
    	printf("Now every rank get the whole candidate itemsets as follow:\n\n");
    	while(p!=NULL)
    	{
    		printf("  %c",p->itemset[p->tag]);
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    
    	//由候选1项集生成频繁1项集
    	tItem *pp,*fHead,*fp,*temp;
    	int minNum;
    
    	fp=fHead=(struct tItem*)malloc(sizeof(struct tItem));
    	minNum=(int)((recordNum*minSup)+1);
    	//printf("%d",minNum);
    	pp=c;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			fHead->count=pp->count;
    			fHead->tag=pp->tag;
    			fHead->itemset[fHead->tag]=pp->itemset[pp->tag];
    			fHead->next=NULL;
    			break;
    		}
    		pp=pp->next;
    	}
    
    	fp=fHead;
    	pp=pp->next;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			temp=(struct tItem*)malloc(sizeof(struct tItem));
    			temp->count=pp->count;
    			temp->next=NULL;
    			temp->tag=pp->tag;
    			temp->itemset[temp->tag]=pp->itemset[pp->tag];
    			fp->next=temp;
    			fp=temp;
    			//printf("%c",pp->itemset[pp->tag]);
    		}
    		pp=pp->next;
    	}
    
    	/*printf("\n");
    	printf("--------The local frequence 1_itemsets--------\n");
    	fp=fHead;
    	while(fp!=NULL)
    	{
    		printf("  %c",fp->itemset[fp->tag]);
    		printf("  %d",fp->count);
    		printf("  %d",fp->tag);
    		printf("\n");
    		fp=fp->next;
    	}*/
    
    	tItem *f1,*kFrequence,*kf,*fis,*kfTemp;
    
    	//kFrequence=(struct tItem*)malloc(sizeof(struct tItem));
    	f1=(struct tItem*)malloc(sizeof(struct tItem));
    	//f1=Find_frequence_1itemsets(fullSet,minSup);
    
    	f1=fHead;
    
    	//system("pause");
    	//printf(" %d",rowNum);
    	//printf(" %d*&*&*&\n",colNum);
    	kFrequence=Gen_frequence(f1,rowNum,colNum,minNum,fSet);//*过连接和剪枝生成最终的频繁k项集
    
    	i=2;
    	while(i<=5)
    	{
    		kfTemp=Gen_frequence(kFrequence,rowNum,colNum,minNum,fSet);
    		if(kfTemp==NULL)
    		{
    			break;
    		}
    		kFrequence=kfTemp;
    		i++;
    	}
    	//MPI_Barrier(MPI_COMM_WORLD);
    	/*printf("^^^^^^^^^^^^^^^\n");
    	kf=kFrequence;
    	while(kf!=NULL)
    	{
    		for(j=0;j<=kf->tag;j++)
    		{
    			printf("  %c",kf->itemset[j]);
    		}
    		printf("  %d",kf->count);
    		printf("  %d",kf->tag);
    		printf("\n");
    		kf=kf->next;
    	}
    	printf("^^^^^^^^^^^^^^^\n");*/
    
    
    	printf("\n");
    	kf=kFrequence;
    	printf("--------The frequence %d_itemsets--------\n",kf->tag+1);
    	while(kf!=NULL)
    	{
    		for(j=0;j<=kf->tag;j++)
    		{
    			printf("  %c",kf->itemset[j]);
    		}
    		printf("  %d",kf->count);
    		printf("  %d",kf->tag);
    		printf("\n");
    		kf=kf->next;
    	}
    	printf("\n");
    
    	/*kf=kFrequence;
    	int kItemNum=0;
    	while(kf!=NULL)
    	{
    		kItemNum++;
    		kf=kf->next;
    	}
    
    	kf=kFrequence;
    	if(rank==1)
    	{
    		MPI_Send(&kItemNum,1,MPI_INT,0,1000,MPI_COMM_WORLD);
    		while(kf!=NULL)
    		{
    			for(j=0;j<=kf->tag;j++)
    			{
    				MPI_Send(&kf->itemset[j],1,MPI_CHAR,0,1001,MPI_COMM_WORLD);
    			}
    			MPI_Send(&kf->count,1,MPI_INT,0,1002,MPI_COMM_WORLD);
    			MPI_Send(&kf->tag,1,MPI_INT,0,1003,MPI_COMM_WORLD);
    			kf=kf->next;
    		}
    	}*/
    
    	kf=kFrequence;
    	printf("\n\nThe subset of the frequence is:\n\n");
    	while(kf!=NULL)
    	{
    
    		fis=Gen_kFrequence_subsets(kf);  //生成频繁k项集每项的子集的集合
    		fis=Compute_candidateItem_Num(fis,rowNum,colNum,fSet); //计算每一个子集的支持度
    
    		/*p=fis;
    		p=p->next;
    		while(p!=NULL)
    		{
    			for(i=0;i<=p->tag;i++)
    			{
    				printf("  %c",p->itemset[i]);
    			}
    			printf("  %d",p->count);
    			printf("  %d",p->tag);
    			printf("\n");
    			p=p->next;
    		}*/
    
    		printf("\n\nNow we get the association rules:\n\n");
    		p=fis;
    		p=p->next;
    		while(p!=NULL)
    		{
    			//printf("\n");
    			Gen_association_rules(kf,p,minCon);
    			printf("\n");
    			p=p->next;
    		}
    
    		kf=kf->next;
    	}
        endtime=MPI_Wtime();
    	printf("The slave process %d tooks %f seconds.\n",rank,endtime-starttime);
    	printf("\n");
    
    }
    
    struct tItem *Gen_frequence(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet)  //生成频繁项集
    {
    
    	tItem *candidate,*cp,*fp,*fpb,*p,*p1;
    	int i,j,k,m,count=0,rank,rankNum,destRank,stag;
    	//int i,m,n;
    	//char *buffer;
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	MPI_Comm_size(MPI_COMM_WORLD,&rankNum);
    
    	/*printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    	printf("##### %d\n",recordNum);
    	printf("##### %d\n",colNum);
    	printf("##### %d\n",minNum);
    	for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf(" %c",fullSet[i][j]);
    		}
    		printf("\n");
    	}
    	p=(struct tItem*)malloc(sizeof(struct tItem));
    	p=frequence;
    	while(p!=NULL)
    	{
    		printf("  %c",p->itemset[p->tag]);
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    	printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");*/
    
    
    	//for(k=2;frequence!=NULL;k++)
    	//while(candidate!=NULL)
    	//{
    		//printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    
    		candidate=apriori_gen(frequence);
    
    		if(candidate!=NULL)
    		{
    			candidate=Compute_candidateItem_Num(candidate,recordNum,colNum,fullSet);
    
    			destRank=1;
    			stag=1;
    
    			p=candidate;
    			p1=candidate;
    			while(p!=NULL)
    			{
    				count++;
    				p=p->next;
    			}
    			//printf(" #%d#\n",count);
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			for(i=1;i<=rankNum-1;i++)  //向其他进程发送局部候选k项集
    			{
    				p=candidate;
    				if(i!=rank)
    				{
    					MPI_Send(&count,1,MPI_INT,destRank,24,MPI_COMM_WORLD);
    					while(p!=NULL)
    					{
    						for(j=0;j<=p->tag;j++)
    						{
    							MPI_Send(&p->itemset[j],1,MPI_CHAR,destRank,25,MPI_COMM_WORLD);
    						}
    
    						MPI_Send(&p->count,1,MPI_INT,destRank,26,MPI_COMM_WORLD);
    						MPI_Send(&p->tag,1,MPI_INT,destRank,27,MPI_COMM_WORLD);
    						p=p->next;
    					}
    				}
    				destRank++;
    				stag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			int reRank=1,reTag=1,itemNum;
    			int rCount,rTag,s;
    			char rItemset;
    			tItem *r;
    			r=(struct tItem*)malloc(sizeof(struct tItem));
    			for(j=1;j<=rankNum-1;j++)  //接收其他进程发送来的局部候选k项集
    			{
    				if(j!=rank)
    				{
    					s=0;
    					MPI_Recv(&itemNum,1,MPI_INT,reRank,24,MPI_COMM_WORLD,&status);
    					while(s<itemNum)
    					{
    						for(m=0;m<=p1->tag;m++)
    						{
    							MPI_Recv(&r->itemset[m],1,MPI_CHAR,reRank,25,MPI_COMM_WORLD,&status);
    							//printf(" %c",r->itemset[m]);
    						}
    
    						MPI_Recv(&r->count,1,MPI_INT,reRank,26,MPI_COMM_WORLD,&status);
    						MPI_Recv(&r->tag,1,MPI_INT,reRank,27,MPI_COMM_WORLD,&status);
    
    
    						candidate=Combine_candidate_k_itemsets(candidate,r);
    
    						//printf(" %c",rItemset);
    						//printf("  %d",r->count);
    						//printf("  %d",r->tag);
    						//printf("\n");
    						s++;
    					}
    					//printf("\n");
    					//printf("&&&&&&&&&&&&&&&&&&&&&\n");
    				}
    				reRank++;
    				reTag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			/*cp=candidate;
    			count=0;
    			printf("--------The candidate %d_itemsets--------\n",cp->tag+1);
    			while(cp!=NULL)
    			{
    				for(j=0;j<=cp->tag;j++)
    				{
    					printf("  %c",cp->itemset[j]);
    				}
    				printf("  %d",cp->count);
    				printf("  %d",cp->tag);
    				printf("\n");
    				count++;
    				cp=cp->next;
    			}
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");*/
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			frequence=candidate;
    			fpb=(struct tItem*)malloc(sizeof(struct tItem));
    			fp=(struct tItem*)malloc(sizeof(struct tItem));
    			fpb=frequence;
    			fp=fpb->next;
    			//int minNum=(int)((recordNum*minSup)+1);
    			//printf("##########  %d\n",minNum);
    
    			while(fp!=NULL)  //删除后选项集中支持度小于最小支持度的项目,生成频繁项集
    			{
    
    				if(fpb->count<minNum)
    				{
    					frequence=fp;
    					fpb=fp;
    					fp=fp->next;
    				}
    
    
    				if(fp->count<minNum)
    				{
    					fp=fp->next;
    					fpb->next=fp;
    				}
    				else
    				{
    					fp=fp->next;
    					fpb=fpb->next;
    
    				}
    			}
    
    			/*fp=frequence;
    			count=0;
    			printf("--------The frequence %d_itemsets--------\n",fp->tag+1);
    			while(fp!=NULL)
    			{
    				//MPI_Barrier(MPI_COMM_WORLD);
    				for(j=0;j<=fp->tag;j++)
    				{
    					printf("  %c",fp->itemset[j]);
    				}
    				printf("  %d",fp->count);
    				printf("  %d",fp->tag);
    				printf("\n");
    				count++;
    				fp=fp->next;
    			}
    
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");*/
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    		}
    		else
    		{
    			//return (NULL);
    			//break;
    			frequence=NULL;
    		}
    		//printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    		//MPI_Barrier(MPI_COMM_WORLD);
    	//}
    
    	return (frequence);
    
    }
    
    struct tItem *Gen_frequence1(tItem *frequence, int recordNum, int colNum, int minNum, char **fullSet)  //生成频繁项集
    {
    
    	tItem *candidate,*cp,*fp,*fpb,*p,*p1;
    	int i,j,k,m,count=0,rank,rankNum,destRank,stag;
    	//int i,m,n;
    	//char *buffer;
    	MPI_Status status;
    	MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    	MPI_Comm_size(MPI_COMM_WORLD,&rankNum);
    
    	//printf("*******************************************\n");
    
    	/*printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    	printf("##### %d\n",recordNum);
    	printf("##### %d\n",colNum);
    	printf("##### %d\n",minNum);
    	printf("##### %d\n",rank);
    	for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			printf(" %c",fullSet[i][j]);
    		}
    		printf("\n");
    	}
    	p=(struct tItem*)malloc(sizeof(struct tItem));
    	p=frequence;
    	while(p!=NULL)
    	{
    		for(j=0;j<=p->tag;j++)
    		{
    			printf("  %c",p->itemset[j]);
    		}
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    	printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");*/
    
    	//for(k=2;frequence!=NULL;k++)
    	//while(candidate!=NULL)
    	//{
    		printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    
    		candidate=apriori_gen(frequence);
    
    		/*p=candidate;
    		int t=0;
    		printf("测试结果\n");
    		while(p!=NULL)
    		{
    			for(j=0;j<=p->tag;j++)
    			{
    				printf("  %c",p->itemset[j]);
    			}
    			printf("  %d",p->count);
    			printf("  %d",p->tag);
    			printf("\n");
    			t++;
    			p=p->next;
    		}
    		printf("一共有 %d\n",t);
    		printf("结束测试\n");*/
    
    
    		/*if(rank==2)
    		{
    			printf("!!!!!!\n");
    			p=candidate;
    			while(p!=NULL)
    			{
    				for(j=0;j<=p->tag;j++)
    				{
    					printf("  %c",p->itemset[j]);
    				}
    				printf("  %d",p->count);
    				printf("  %d",p->tag);
    				printf("\n");
    				p=p->next;
    			}
    		}
    
    		printf("!!!!!!\n");*/
    
    
    		if(candidate!=NULL)
    		{
    			//printf("!!!!!!\n");
    			candidate=Compute_candidateItem_Num(candidate,recordNum,colNum,fullSet);
    
    			destRank=1;
    			stag=1;
    
    			p=candidate;
    			p1=candidate;
    			while(p!=NULL)
    			{
    				count++;
    				p=p->next;
    			}
    			//printf(" #%d#\n",count);
    			//printf("进行到这里\n");
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			for(i=1;i<=rankNum-1;i++)  //向其他进程发送局部候选k项集
    			{
    				p=candidate;
    				if(i!=rank)
    				{
    					MPI_Send(&count,1,MPI_INT,destRank,54,MPI_COMM_WORLD);
    					//printf("### %d ###\n",count);
    					while(p!=NULL)
    					{
    						for(j=0;j<=p->tag;j++)
    						{
    							MPI_Send(&p->itemset[j],1,MPI_CHAR,destRank,55,MPI_COMM_WORLD);
    							//printf(" %c",p->itemset[j]);
    						}
    
    						MPI_Send(&p->count,1,MPI_INT,destRank,56,MPI_COMM_WORLD);
    						MPI_Send(&p->tag,1,MPI_INT,destRank,57,MPI_COMM_WORLD);
    						//printf("  %d",p->count);
    						//printf("  %d",p->tag);
    						//printf("\n");
    						p=p->next;
    					}
    				}
    				destRank++;
    				stag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			int reRank=1,reTag=1,itemNum;
    			int rCount,rTag,s;
    			char rItemset;
    			tItem *r;
    			r=(struct tItem*)malloc(sizeof(struct tItem));
    			for(j=1;j<=rankNum-1;j++)  //接收其他进程发送来的局部候选k项集
    			{
    				if(j!=rank)
    				{
    					s=0;
    					MPI_Recv(&itemNum,1,MPI_INT,reRank,54,MPI_COMM_WORLD,&status);
    					while(s<itemNum)
    					{
    						for(m=0;m<=p1->tag;m++)
    						{
    							MPI_Recv(&r->itemset[m],1,MPI_CHAR,reRank,55,MPI_COMM_WORLD,&status);
    							//printf(" %c",r->itemset[m]);
    						}
    
    						MPI_Recv(&r->count,1,MPI_INT,reRank,56,MPI_COMM_WORLD,&status);
    						MPI_Recv(&r->tag,1,MPI_INT,reRank,57,MPI_COMM_WORLD,&status);
    
    
    						candidate=Combine_candidate_k_itemsets(candidate,r);
    
    						//printf(" %c",rItemset);
    						//printf("  %d",r->count);
    						//printf("  %d",r->tag);
    						//printf("\n");
    						s++;
    					}
    					//printf("\n");
    					printf("&&&&&&&&&&&&&&&&&&&&&\n");
    				}
    				reRank++;
    				reTag++;
    			}
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			cp=candidate;
    			count=0;
    			printf("--------The candidate %d_itemsets--------\n",cp->tag+1);
    			while(cp!=NULL)
    			{
    				for(j=0;j<=cp->tag;j++)
    				{
    					printf("  %c",cp->itemset[j]);
    				}
    				printf("  %d",cp->count);
    				printf("  %d",cp->tag);
    				printf("\n");
    				count++;
    				cp=cp->next;
    			}
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    
    			frequence=candidate;
    			fpb=(struct tItem*)malloc(sizeof(struct tItem));
    			fp=(struct tItem*)malloc(sizeof(struct tItem));
    			fpb=frequence;
    			fp=fpb->next;
    			//int minNum=(int)((recordNum*minSup)+1);
    			//printf("##########  %d\n",minNum);
    
    			while(fp!=NULL)  //删除后选项集中支持度小于最小支持度的项目,生成频繁项集
    			{
    
    				if(fpb->count<minNum)
    				{
    					frequence=fp;
    					fpb=fp;
    					fp=fp->next;
    				}
    
    
    				if(fp->count<minNum)
    				{
    					fp=fp->next;
    					fpb->next=fp;
    				}
    				else
    				{
    					fp=fp->next;
    					fpb=fpb->next;
    
    				}
    			}
    
    			fp=frequence;
    			count=0;
    			printf("--------The frequence %d_itemsets--------\n",fp->tag+1);
    			while(fp!=NULL)
    			{
    				//MPI_Barrier(MPI_COMM_WORLD);
    				for(j=0;j<=fp->tag;j++)
    				{
    					printf("  %c",fp->itemset[j]);
    				}
    				printf("  %d",fp->count);
    				printf("  %d",fp->tag);
    				printf("\n");
    				count++;
    				fp=fp->next;
    			}
    
    			printf("\n");
    			printf("The total number is: %d\n",count);
    			printf("\n");
    
    			//MPI_Barrier(MPI_COMM_WORLD);
    		}
    		else
    		{
    			//return (NULL);
    			//break;
    			//frequence=NULL;
    		}
    		//printf("++++++++++++++++++++++++++++++++++++++++++++\n");
    		//MPI_Barrier(MPI_COMM_WORLD);
    	//}
    
    	return (frequence);
    
    }
    
    bool Has_infrequence_subset(tItem *pp, tItem *f)  //判断候选k项集的子集是否在频繁k-1项集中
    {
    	tItem *p,*fp;  //储存*pp中的k-1项子集
    	int i=0,j,m=0;
    	bool flag;
    
    	while(i<=pp->tag)
    	{
    
    		p=(struct tItem*)malloc(sizeof(struct tItem));
    
    		for(j=0;j<pp->tag;j++)
    		{
    			if(j==i)
    			{
    				j++;
    			}
    			else
    			{
    				p->itemset[j]=pp->itemset[j];
    			}
    		}
    
    		fp=f;
    		while(fp!=NULL)
    		{
    			while(m<=fp->tag)
    			{
    				if(p->itemset[m]==fp->itemset[m])
    				{
    					m++;
    				}
    				else
    				{
    					flag=false;
    					m++;
    				}
    			}
    
    			flag=true;
    			fp=fp->next;
    		}
    
    		i++;
    	}
    
    
    	return (flag);
    }
    
    struct tItem *Gen_candidate_itemset(tItem *cp,tItem *cp1,tItem *cp2)  //生成后选项
    {
    	int i=0;
    
    	while(i<=cp1->tag)
    	{
    		cp->itemset[i]=cp1->itemset[i];
    		//cp->tag=cp1->tag+1;
    		i++;
    	}
    
    	cp->tag=cp1->tag+1;
    	cp->itemset[i]=cp2->itemset[i-1];
    	cp->count=0;
    	cp->next=NULL;
    
    	return (cp);
    }
    
    bool Is_before_equal(tItem *pp1, tItem *pp2)  //判断两个项目集的前k-1项是否相同
    {
    	int i=0;
    	bool flag;
    
    	while(i<pp1->tag)
    	{
    		if(pp1->itemset[i]==pp2->itemset[i])
    		{
    			flag=true;
    		}
    		else
    		{
    			flag=false;
    			break;
    		}
    		i++;
    	}
    
    	return (flag);
    }
    
    struct tItem *Compute_candidateItem_Num(tItem *c,int recordNum, int colNum, char **fullSet)  //计算候选项集中每一项的最小支持数
    {
    	tItem *cp;
    	int i,j,m,n;
    	char *buffer;
    
        cp=c;
    
    	while(cp!=NULL) //统计每个候选项集的计数
    	{
    		for(i=0;i<recordNum;i++)
    		{
    
    			bool flag=true;
    			buffer=(char*)malloc(colNum*sizeof(char*));
    
    			for(j=0;j<colNum;j++)
    			{
    				buffer[j]=fullSet[i][j];
    			}
    
    			m=0;
    			while(m<=cp->tag)
    			{
    				bool flag1=false;
    				for(n=0;n<colNum;n++)
    				{
    					if(cp->itemset[m]==buffer[n])
    					{
    						flag1=true;
    						break;
    					}
    
    					flag1=false;
    				}
    
    				if(flag1==false)
    				{
    					flag=false;
    				}
    				m++;
    			}
    
    			if(flag==true)
    			{
    				cp->count++;
    			}
    		}
    
    		cp=cp->next;
    	}
    
    	return (c);
    }
    
    struct tItem *apriori_gen(tItem *freq)  //生成候选项集
    {
    	tItem *p1,*p2,*head,*cHead,*p,*pTail;
    	int tag,i=0;
    	//bool flag;
    
    	if(freq->next!=NULL)
    	{
    
    		head=freq;
    		p1=head;
    		p2=head;
    		p2=p2->next;
    
    		p=(struct tItem*)malloc(sizeof(struct tItem));
    		tag=p1->tag+1;
    
    		if(tag==1)//生成候选2项集
    		{
    			//printf("############\n");
    			while(i<=tag)
    			{
    				if(i<=tag-1)
    				{
    					//p->count=p1->count;
    					p->itemset[i]=p1->itemset[i];
    					p->tag=tag;
    				}
    				else
    				{
    					//p->count=p2->count;
    					p->itemset[i]=p2->itemset[i-1];
    					p->tag=tag;
    				}
    				i++;
    			}
    			p->count=0;
    			p->next=NULL;
    
    			/*printf("  %c",p->itemset[0]);
    			printf("  %c",p->itemset[1]);
    			printf("  %d",p->tag);*/
    
    			pTail=p;
    			cHead=p;
    			p2=p2->next;
    			//printf("#%c#",p1->itemset[p1->tag]);
    
    			while(p1!=NULL)
    			{
    				while(p2!=NULL)
    				{
    					p=(struct tItem*)malloc(sizeof(struct tItem));
    					tag=p1->tag+1;
    					i=0;
    
    					while(i<=tag)
    					{
    						if(i<=tag-1)
    						{
    							//p->count=p1->count;
    							p->itemset[i]=p1->itemset[i];
    							//printf("%c",p->itemset[i]);
    							p->tag=tag;
    						}
    						else
    						{
    							//p->count=p2->count;
    							p->itemset[i]=p2->itemset[i-1];
    							//printf("%c",p->itemset[i]);
    							p->tag=tag;
    						}
    						i++;
    					}
    					p->count=0;
    					p->next=NULL;
    
    					//flag=Has_infrequence_subset(cHead,freq);
    
    					//if(flag==true)
    					//{
    						pTail->next=p;
    						pTail=p;
    					//}
    					p2=p2->next;
    				}
    
    				p1=p1->next;
    				p2=p1->next;
    
    				if(p2==NULL)
    				{
    					break;
    				}
    			}
    		}
    		/*else if(p1->tag==3)
    		{
    			if(Is_before_equal(p1,p2))
    			{
    				//printf("进行到这里\n");
    				//p=(struct tItem*)malloc(sizeof(struct tItem));
    				//cHead=(struct tItem*)malloc(sizeof(struct tItem));
    				//pTail=(struct tItem*)malloc(sizeof(struct tItem));
    
    				p=Gen_candidate_itemset(p,p1,p2);
    				pTail=p;
    				cHead=p;
    				p2=p2->next;
    
    
    				while((p1!=NULL)&&(p2!=NULL))
    				{
    					//while(Is_before_equal(p1,p2))
    					while(p2!=NULL)
    					{
    						if(Is_before_equal(p1,p2))
    						{
    							p=(struct tItem*)malloc(sizeof(struct tItem));
    							tag=p1->tag+1;
    
    							p=Gen_candidate_itemset(p,p1,p2);
    
    							if(Has_infrequence_subset(p, freq))
    							{
    								pTail->next=p;
    								pTail=p;
    							}
    						}
    						//printf("进行到这里\n");
    						p2=p2->next;
    
    					}
    
    					p1=p1->next;
    					p2=p1->next;
    				}
    			}
    			//printf("进行到这里\n");
    		}*/
    		else//生成侯选2项集以上的候选集(标记cHead时候有问题)
    		{
    
    			/*while(p2!=NULL)
    			{
    				if(Is_before_equal(p1,p2))
    				{
    					break;
    				}
    				p2=p2->next;
    			}*/
    
    			if(Is_before_equal(p1,p2))
    			{
    				//p=(struct tItem*)malloc(sizeof(struct tItem));
    				//cHead=(struct tItem*)malloc(sizeof(struct tItem));
    				//pTail=(struct tItem*)malloc(sizeof(struct tItem));
    
    				p=Gen_candidate_itemset(p,p1,p2);
    				pTail=p;
    				cHead=p;
    				p2=p2->next;
    
    
    				while((p1!=NULL)&&(p2!=NULL))
    				{
    					//while(Is_before_equal(p1,p2))
    					while(p2!=NULL)
    					{
    						if(Is_before_equal(p1,p2))
    						{
    							p=(struct tItem*)malloc(sizeof(struct tItem));
    							tag=p1->tag+1;
    
    							p=Gen_candidate_itemset(p,p1,p2);
    
    							if(Has_infrequence_subset(p, freq))
    							{
    								pTail->next=p;
    								pTail=p;
    							}
    						}
    
    						p2=p2->next;
    
    					}
    
    					p1=p1->next;
    					p2=p1->next;
    				}
    			}
    			else
    			{
    				p1=p1->next;
    				p2=p2->next;
    				p=Gen_candidate_itemset(p,p1,p2);
    				pTail=p;
    				cHead=p;
    				p2=p2->next;
    
    
    				while((p1!=NULL)&&(p2!=NULL))
    				{
    					while(Is_before_equal(p1,p2))
    					{
    						p=(struct tItem*)malloc(sizeof(struct tItem));
    						tag=p1->tag+1;
    
    						p=Gen_candidate_itemset(p,p1,p2);
    
    						if(Has_infrequence_subset(p, freq))
    						{
    							pTail->next=p;
    							pTail=p;
    						}
    
    						p2=p2->next;
    
    					}
    
    					p1=p1->next;
    					p2=p1->next;
    				}
    				//cHead=NULL;
    			}
    		}
    	}
    	else
    	{
    		cHead=NULL;
    	}
    	/*tItem *pp;
    	int j,count=0;
    
    	pp=cHead;
    	printf("--------The candidate %d_itemsets--------\n",pp->tag+1);
    	while(pp!=NULL)
    	{
    		for(j=0;j<=tag;j++)
    		{
    			printf("  %c",pp->itemset[j]);
    		}
    		printf("  %d",pp->count);
    		printf("  %d",pp->tag);
    		printf("\n");
    		count++;
    		pp=pp->next;
    	}
    	printf("\n");
    	printf("The total number is: %d\n",count);
    	printf("\n");*/
    
    	return (cHead);
    }
    
    struct tItem *Combine_candidate_k_itemsets(tItem *lc,tItem *r)
    {
    	tItem *p;
    	int i,j;
    	bool flag,flagj;
    
    	//printf("**************************\n");
    	p=lc;
    	while(p!=NULL)
    	{
    		//flag=true;
    		for(i=0;i<=r->tag;i++)
    		{
    			flagj=false;
    			for(j=0;j<=p->tag;j++)
    			{
    				if(p->itemset[j]==r->itemset[i])
    				{
    					flagj=true;
    					break;
    				}
    			}
    			if(flagj==false)
    			{
    				flag=false;
    				break;
    			}
    			else
    			{
    				flag=true;
    			}
    		}
    		if(flag==true)
    		{
    			p->count=p->count+r->count;
    			break;
    		}
    		p=p->next;
    	}
    
    	return (lc);
    }
    
    struct tItem *Combine_candidate_itemsets(tItem *lc, char rItemset, int rCount)
    {
    	tItem *p;
    
    	p=lc;
    	while(p!=NULL)
    	{
    		if(p->itemset[p->tag]==rItemset)
    		{
    			p->count=p->count+rCount;
    		}
    		p=p->next;
    	}
    
    	return (lc);
    }
    
    struct tItem *Find_candidate_1itemsets(char **fullSet2, double sup2,int recordNum,int colNum)  //生成频繁1项集
    {
    	tItem *cHead,*fHead;
    	tItem *p1,*pHead,*pTail,*p;
    	tItem *fp,*temp,*pp;
    	fp=fHead=(struct tItem*)malloc(sizeof(struct tItem));
    
    	int i,j,count=0;
    	bool flag;
    
    	p=pHead=pTail=(struct tItem*)malloc(sizeof(struct tItem));
    	cHead=NULL;
    	pHead->tag=0;
    	pHead->itemset[pHead->tag]=fullSet2[0][0];
    	pHead->count=0;
    	pHead->next=NULL;
    
    
    	int minNum=(int)((recordNum*sup2)+1);
    	//printf("%d",minNum);
    
    	cHead=pHead;
    	for(i=0;i<recordNum;i++)
    	{
    		for(j=0;j<colNum;j++)
    		{
    			p=cHead;
    			while(p!=NULL)
    			{
    				if(p->itemset[p->tag]==fullSet2[i][j])
    				{
    					p->count++;
    					flag=false;
    					break;
    				}
    				else
    				{
    					pTail=p;
    					p=p->next;
    					flag=true;
    				}
    			}
    			if(flag==true)
    			{
    				p1=(struct tItem*)malloc(sizeof(struct tItem));
    				p1->tag=0;
    				p1->itemset[p1->tag]=fullSet2[i][j];
    				p1->count=1;
    				pTail->next=p1;
    				p1->next=NULL;
    			}
    		}
    	}
    
    	printf("\n");
    	printf("--------The local candidate 1_itemsets--------\n");
    	p=cHead;
    	while(p!=NULL)
    	{
    		count++;
    		printf("  %c",p->itemset[p->tag]);
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}
    	printf("\n");
    	printf("In all, there are %d candidate 1_itemsets.\n",count);
    
    	/*pp=cHead;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			fHead->count=cHead->count;
    			fHead->tag=cHead->tag;
    			fHead->itemset[fHead->tag]=cHead->itemset[cHead->tag];
    			fHead->next=NULL;
    			break;
    		}
    		pp=pp->next;
    	}
    
    	fp=fHead;
    	pp=pp->next;
    
    	while(pp!=NULL)
    	{
    		if(pp->count>minNum)
    		{
    			temp=(struct tItem*)malloc(sizeof(struct tItem));
    			temp->count=pp->count;
    			temp->next=NULL;
    			temp->tag=pp->tag;
    			temp->itemset[temp->tag]=pp->itemset[pp->tag];
    			fp->next=temp;
    			fp=temp;
    			//printf("%c",pp->itemset[pp->tag]);
    		}
    		pp=pp->next;
    	}
    
    	printf("\n");
    	printf("--------The frequence 1_itemsets--------\n");
    	fp=fHead;
    	while(fp!=NULL)
    	{
    		printf("  %c",fp->itemset[fp->tag]);
    		printf("  %d",fp->count);
    		printf("  %d",fp->tag);
    		printf("\n");
    		fp=fp->next;
    	}
    
    	return (fHead);*/
    	return (cHead);
    }
    
    struct tItem *Gen_kFrequence_subsets(tItem *f)  //生成频繁k项集每项的子集的集合
    {
    	tItem *FreItemSubset;
    	//tItem *p;
    	int ListLength=f->tag+1;
    	int i;
    	//buffer=(int*)malloc(col*sizeof(int*))
    	char *List,*Buffer;
    
    	List=(char*)malloc(ListLength*sizeof(char*));
    	Buffer=(char*)malloc(ListLength*sizeof(char*));
    
    	for(i=0;i<f->tag+1;i++)
    	{
    		List[i]=f->itemset[i];
    		//printf(" %c",List[i]);
    	}
    
    	FreItemSubset=(struct tItem*)malloc(sizeof(struct tItem));
    	FreItemSubset->next=NULL;
    	FreItemSubset=SubSet(List,0,Buffer,0,FreItemSubset,ListLength);
    
    	/*p=FreItemSubset;
    	p=p->next;
    	while(p!=NULL)
    	{
    		for(i=0;i<=p->tag;i++)
    		{
    			printf("  %c",p->itemset[i]);
    		}
    		printf("  %d",p->count);
    		printf("  %d",p->tag);
    		printf("\n");
    		p=p->next;
    	}*/
    
    	return (FreItemSubset);
    }
    
    int Index(char *List, char c, int ListLength)
    {
         for(int i=0; i<=ListLength-1; i++)
         {
                  if(c==List[i])
                  {
                        return i;
                        break;
                  }
         }
         return -1;
    }
    
    struct tItem *SubSet(char *List, int m, char *Buffer, int flag, tItem *fis,int ListLength)
    {
    	 //fis->next=NULL;
    	 //printf("###\n");
         if(m <= ListLength-1)
         {
              /*if(m==0)
              {
                      Buffer[0]=List[0];
              }*/
              //Buffer[flag]=List[m];
              /*if(flag==0)
              {
                    Buffer[flag]=List[m];
              }*/
    
              for(int i=(flag==0) ? 0 : Index(List,Buffer[flag-1],ListLength)+1; i<=ListLength-1; i++)
              //当flag==0时,Buffer中没有任何元素,此时i=[0...ListLength-1]
              //当flag>0时,找到Buffer中的最后一个元素在集合List中的位置i,把[i....ListLength-1]
              //处的元素,加到Buffer元素的最后面
              {
    
                    Buffer[flag]=List[i];
                    fis=AddToSubset(Buffer,flag,fis);
                    //Output(Buffer,flag);
                    SubSet(List, m+1, Buffer,flag+1,fis,ListLength);
              }
         }
    
    	 /*tItem *p;
    	 p=fis;
    	 while(p!=NULL)
    	 {
    	 	 for(int j=0;j<=p->tag;j++)
    		 {
    		 	 printf("  %c",p->itemset[j]);
    		 }
    		 printf("  %d",p->count);
    		 printf("  %d",p->tag);
    		 printf("\n");
    		 p=p->next;
    	 }*/
    
         return (fis);
    }
    
    struct tItem *AddToSubset(char *Buffer, int flag, tItem *fis)  //将子集添加到链表中
    {
    
    	tItem *p;
    	int i=0;
    
    	p=(struct tItem*)malloc(sizeof(struct tItem));
    
    	p->count=0;
    	p->tag=flag;
    
    	while(i<=flag)
    	{
    		p->itemset[i]=Buffer[i];
    		i++;
    	}
    
    	p->next=fis->next;
    	fis->next=p;
    
    	return (fis);
    }
    
    void Gen_association_rules(tItem *kf, tItem *p, double minCon)  //生成关联规则
    {
    	int i=0;
    	int j=0;
    	double m;
    	bool flag;
    
    	//printf("@@@@ %f\n",minCon);
    
    	m=(double)(kf->count)/(double)(p->count);
    
    	if(m>=minCon)
    	{
    		printf("\n");
    		while(i<=p->tag)
    		{
    			if(i!=p->tag)
    			{
    				printf("%c ",p->itemset[i]);
    			}
    			else
    			{
    				printf("%c ",p->itemset[i]);
    			}
    			i++;
    		}
    
    		printf("=====> ");
    
    		while(j<=kf->tag)
    		{
    			if(j!=kf->tag)
    			{
    				for(i=0;i<=p->tag;i++)
    				{
    					if(kf->itemset[j]==p->itemset[i])
    					{
    						flag=false;
    						break;
    					}
    					else
    					{
    						flag=true;
    					}
    				}
    				if(flag==true)
    				{
    					printf("%c ",kf->itemset[j]);
    				}
    
    			}
    			else
    			{
    				for(i=0;i<=p->tag;i++)
    				{
    					if(kf->itemset[j]==p->itemset[i])
    					{
    						flag=false;
    						break;
    					}
    					else
    					{
    						flag=true;
    					}
    				}
    				if(flag==true)
    				{
    					printf("%c ",kf->itemset[j]);
    				}
    
    			}
    			j++;
    		}
    
    		//cout << setprecision<< m <<endl;
    		m=(double)(p->count)/(double)(kf->count);
    		printf("    Confidence : %f",m);
    
    		printf("\n");
    	}
    }
    That will preserve the indentations and keep from interpreting character sequences such as smileys.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I've enclosed your code in [code] tags for our sanity .

    You're asking a huge question here. Perhaps you could tell us what you're trying to do that isn't working. I'm not going to read through your code and do translation, but if you tell us exactly what concept or line you're having difficulty with, I'm sure someone can help.

  4. #4
    Just Joined!
    Join Date
    Dec 2011
    Posts
    1
    Can you please attach the shujuku.txt?? So that i can understand what you did??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •