⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parallelapriori.cpp

📁 关联规则并行算法Count Distribution的C与MPI实现
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		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");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -