You are on page 1of 18

#include<fstream.

h>

#include<iostream.h>

#define MAX 7

#include<string.h>

struct node{

int *a;

int count;

node *link;

}*first[MAX];

struct hashtable{

int count;

node* link;

}hash[13];

int thresh;

node* prune(node* x,int l)

int **point;

node* temp1=x;

node* prev=temp1;

point=new int*[l];

int i;

node* temp2=first[l-1];

int j=l-1,check,check1,check2;

while(temp1!=NULL)
{

for(i=0;i<l;i++)

point[i]=&(temp1->a[i]);

j=l-1;

check2=1;

while(j>-2&&check2)

check=1;

temp2=first[l-1];

while(temp2!=NULL && check)

check1=1;

for(i=0;i<l && check1 ;i++)

if(temp2->a[i]!=*point[i])

check1=0;

if(!check1)

temp2=temp2->link;

else

check=0;

if(temp2==NULL)

check2=0;

}
else

if(j>=0)

point[j]=point[j]+1;

j--;

if(check2)

prev=temp1;

temp1=temp1->link;

else

if(temp1==x)

prev=x=temp1->link;

temp1->link=NULL;

delete []temp1->a;

delete temp1;

temp1=prev;

else

{
prev->link=temp1->link;

temp1->link=NULL;

delete []temp1->a;

delete temp1;

temp1=prev->link;

return x;

node* candidategen(node* t,int lev)

node *n,*temp1;

// generation of candidates at first level

if(lev==0)

int i=0;

while(i<MAX)

n=new node;

n->a=new int;

*n->a=i;

n->count=0;

n->link=NULL;
if(t==NULL)

t=n;

temp1=n;

else

temp1->link=n;

temp1=temp1->link;

i++;

//generation at second level

else if(lev==1)

node *temp2,*temp3;

temp2=first[lev-1];

temp3=temp2->link;

while(temp3!=NULL)

n=new node;

n->a=new int[lev+1];

n->a[0]=*temp2->a;

n->a[1]=*temp3->a;
n->count=0;

n->link=NULL;

if(t==NULL)

t=n;

temp1=n;

else

temp1->link=n;

temp1=temp1->link;

temp3=temp3->link;

if(temp3==NULL)

temp2=temp2->link;

temp3=temp2->link;

//candidate generation for level>2......

else

node *temp2,*temp3;

temp2=first[lev-1];
temp3=temp2->link;

int i,j,check;

while(temp3!=NULL)

check=1;

for(i=0;i<lev-1;i++)

if(temp2->a[i]!=temp3->a[i])

check=0;

if(!check)

temp2=temp2->link;

temp3=temp2->link;

else

n=new node;

n->a=new int[lev+1];

for(i=0;i<lev;i++)

n->a[i]=temp2->a[i];

j=i;

n->a[j]=temp3->a[--i];

n->count=0;

n->link=NULL;

if(t==NULL)

{
t=n;

temp1=n;

else

temp1->link=n;

temp1=temp1->link;

temp3=temp3->link;

if(temp3==NULL)

temp2=temp2->link;

temp3=temp2->link;

if(t!=NULL)

t=prune(t,lev);

cout<<"after pruning for level"<<lev+1<<endl;

temp1=t;

while(temp1!=NULL)

for(int i=0;i<=lev;i++)
cout<<temp1->a[i];

cout<<" ";

temp1=temp1->link;

cout<<endl;

if(lev)

temp1=t;

node* prev=temp1;

int i,hashval;

while(temp1!=NULL)

hashval=0;

cout<<"bucket count for";

for(i=0;i<=lev;i++)

hashval=37*hashval+temp1->a[i];

cout<<temp1->a[i];

hashval=hashval%13;

cout<<"is"<<hash[hashval].count<<endl;

if(hashval<0)

hashval=hashval+13;
if(hash[hashval].count>=thresh)

// cout<<"hashval for level"<<lev+1<<":"<<hashval<<endl;

prev=temp1;

temp1=temp1->link;

else

if(temp1==t)

prev=t=temp1->link;

temp1->link=NULL;

delete []temp1->a;

delete temp1;

temp1=prev;

else

prev->link=temp1->link;

temp1->link=NULL;

delete []temp1->a;

delete temp1;

temp1=prev->link;

}
}

cout<<"after hashing for level"<<lev+1<<endl;

temp1=t;

while(temp1!=NULL)

for(i=0;i<=lev;i++)

cout<<temp1->a[i];

cout<<" ";

temp1=temp1->link;

cout<<endl;

for(i=0;i<13;i++)

hash[i].count=0;

prev=temp1=hash[i].link;

hash[i].link=NULL;

while(temp1!=NULL)

temp1=temp1->link;

prev->link=NULL;

delete []prev->a;

delete prev;

prev=temp1;

}
}

return t;

int main()

ifstream fin("ke2.txt");

cout<<"Enter the threshold value"<<endl;

cin>>thresh;

int level=0,check=1,j,check1,i;

char buf[MAX*2];

int len=2*MAX;char ch;

int read,arr[MAX];

for(i=0;i<MAX;i++)

first[i]=NULL;

int firstime=0,flag=0,hashval;

for(i=0;i<13;i++)

hash[i].count=0;

hash[i].link=NULL;

node *temp,*h;

while(level<MAX && check)

first[level]=candidategen(first[level],level);

temp=first[level];
while(temp!=NULL)

for(i=temp->a[level]+1;i<MAX;i++)

node* n=new node;

n->a=new int[level+2];

for(j=0;j<level+1;j++)

n->a[j]=temp->a[j];

n->a[j]=i;

n->count=0;

n->link=NULL;

hashval=0;

for(j=0;j<=level+1;j++)

hashval=37*hashval+n->a[j];

hashval=hashval%13;

if(hashval<0)

hashval=hashval+13;

if(hash[hashval].link==NULL)

hash[hashval].link=n;

else

h=hash[hashval].link;

while(h->link!=NULL)

h=h->link;

h->link=n;
}

temp=temp->link;

if(first[level]!=NULL)

fin.getline(buf,len);

firstime=0;

while(strlen(buf))

for(i=0,j=0;i<MAX;i++,j=j+2)

arr[i]=buf[j]-'0';

//cout<<arr[i];

temp=first[level];

while(temp!=NULL)

check1=1;

for(i=0;i<=level && check1;i++)

if(!arr[temp->a[i]])

check1=0;
if(check1)

temp->count=temp->count+1;

for(i=temp->a[level]+1;i<MAX;i++)

flag=1;

if(arr[i])

hashval=0;

for(j=0;j<=level;j++)

hashval=37*hashval+temp->a[j];

hashval=37*hashval+i;

hashval=hashval%13;

if(hashval<0)

hashval=hashval+13;

hash[hashval].count++;

temp=temp->link;

fin.getline(buf,len);

}
node *prev,*prev1;

prev=prev1=temp=first[level];

//cout<<"counts"<<endl;

while(temp!=NULL)

if(temp->count<thresh)

if(temp==first[level])

prev=first[level]=temp->link;

temp->link=NULL;

delete []temp->a;

delete temp;

temp=prev;

else

prev->link=temp->link;

temp->link=NULL;

delete []temp->a;

delete temp;

temp=prev->link;

}
else

prev=temp;

temp=temp->link;

level++;

fin.close();

fin.open("ke2.txt");

else

check=0;

int k;

for(j=0;j<level;j++)

temp=first[j];

while(temp!=NULL)

for(k=0;k<=j;k++)

cout<<temp->a[k];

temp=temp->link;

cout<<" ";
}

cout<<"are popular"<<endl;

return 0;

You might also like