You are on page 1of 13

Data Project

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int clusternum = 0;
int count7 = 0;
struct node1
{
int data;
node1*l;
node1*r;

};
struct node2
{//adgacency list
int data;
int weight;
node2*next;
};
struct node3
{//final linklist
int data;
node3*next;
};
struct node4
{///////////queue
int data;
int weight;
node4*next;
node4*previous;
};
class array1
{
public:
node2*head;
node2*tail;
int data;
int degree;
int weight;
array1*next;
array1()
{
data = 0;
head = NULL;
tail = NULL;
weight = 0;
degree = 0;
next = NULL;
}
void insert(int n2)
{
if (n2 == INT_MIN)
{
head = NULL;
return;
}
if (head == NULL)
{
head = new node2;
head->data = n2;
head->weight = 0;
tail = head;
head->next = NULL;
}
else
{
node2*ptr = new node2;
ptr->data = n2;
ptr->weight = 0;
tail->next = ptr;
tail = ptr;
tail->next = NULL;

}
}
void sort(node2 *(y))
{
node2*a;
a = head;

swap(head, head);

};

class queue
{
public:
node4*head;
node4*tail;
queue()
{
head = NULL;
tail = NULL;

}
void enqueue(int n)
{
if (head == NULL)
{
head = new node4;
head->data = n;
head->weight = 0;
head->previous = NULL;
head->next = NULL;
tail = head;
}
else
{
node4*p = new node4;
p->data = n;
p->next = NULL;
p->previous = tail;
tail->next = p;
p->weight = 0;
tail = p;
}
}
int dequeue()
{
node4*p = head;

head = head->next;
return p->data ;
}
bool check()
{
if (head == NULL)
{
return false;
}
}
int check2()
{
if (head != NULL)
{
return head->data;
}
return INT_MIN;
}
void sort()
{

};
class operations
{
array1*ptr;
int size;
node1*root;
queue point;
node3*head1final;
node3*tail1final;
public:
operations()
{
root = NULL;
}
void bst(int number,node1*(&ptr))
{
if (root == NULL)
{
root = new node1;
root->data = number;
root->l = root->r = NULL;
size++;
return;

}
else if (ptr == NULL)
{
ptr = new node1;
ptr->data = number;
ptr->l = ptr->r = NULL;
size++;
return;
}
else if (ptr->data > number)
{
bst(number, ptr->l);
}
else if (ptr->data<number)
{
bst(number, ptr->r);
}
else
{
return;
}
}
void finalnull()
{
node3*p0=head1final;
while (p0 != NULL)
{
if (p0 == tail1final)
{
delete tail1final;

tail1final = NULL;
head1final = NULL;
break;
}
else {
head1final = head1final->next;
delete p0;
p0 = head1final;
}
}
}
void bst(int n)
{
bst(n, root);
}
void print()
{
cout << "Number 0f vertex= " << size << endl;
}
void makearray()
{
ptr = new array1[size];
for (int x = 0; x < size; x++)
{
ptr[x].data = x;
ptr[x].weight = 0;
ptr[x].degree = INT_MIN;
}

}
void assigning(int n1,int n2)
{
ptr[n1].data = n1;
ptr[n1].insert(n2);
}
void printall()
{
node2*p;
for (int x = 0; x < size; x++)
{
if(ptr[x].degree!=INT_MIN){
cout << ptr[x].data << "(" << ptr[x].weight << ")" << "{" <<
ptr[x].degree << "}";
p = ptr[x].head;
while (1)
{
if (p == NULL)
break;
cout << "-->" << p->data << "(" << p->weight << ")";
p = p->next;

cout << endl;


}
}
}
void assigningweight()
{
int count = 0, index = 0;
node2*ptr1;
node2*ptr2;
node2*ptr3;
for (int x = 0; x < size; x++)
{
if (ptr[x].data != INT_MIN)
{
ptr[x].weight = 0;
}
}
for (int x = 0; x < size; x++)
{
ptr1 = ptr[x].head;
while (ptr1 != NULL)
{
ptr2 = ptr[x].head;
while (ptr2 != NULL)
{
index = ptr1->data;
ptr3 = ptr[index].head;
while (ptr3 != NULL)
{
if (ptr3->data != ptr1->data)
{
if (ptr3->data == ptr2->data)
{
count++;
}
ptr3 = ptr3->next;
}
}
ptr2 = ptr2->next;
}

ptr1->weight = count;
ptr[x].weight = ptr[x].weight + count;
count = 0;
ptr1 = ptr1->next;
}
}
////////degree
count=0;
for (int x = 0; x < size; x++)
{
ptr1 = ptr[x].head;
while (ptr1 != NULL)
{
count++;
ptr1 = ptr1->next;
}
ptr[x].degree = count;
count = 0;
}
for (int z = 0; z < size; z++)
{
if (ptr[z].degree == 0)
{
ptr[z].degree = INT_MIN;
}
}
}
void queuee()
{
node4*p1 = point.head;
node4*p2 = point.head;
int weight = 0, index = 0;
while (p1 != NULL)
{
if (p2 == NULL)
break;
while (p2->next != NULL)
{
if (p2->weight < p2->next->weight)
{
weight = p2->weight;
index = p2->data;
p2->weight = p2->next->weight;
p2->data = p2->next->data;
p2->next->weight = weight;
p2->next->data = index;
}
p2 = p2->next;
}
p1 = p1->next;
}

}
int divide(array1*(&ptr), int f, int l)
{
int w, d, d2;
int pivot = ptr[l].weight;
int x = (f - 1);
node2*a = NULL;
for (int y = f; y < l ; y++)
{
if (ptr[y].weight < pivot)
{
x++;

swap(ptr[x], ptr[y]);
}
}

swap(ptr[x+1], ptr[l]);
return (x + 1);
}
void sort(array1 *(&ptr), int low, int high)
{
if (low < high)
{
int pi = divide(ptr, low, high);
sort(ptr, low, pi - 1);
sort(ptr, pi + 1, high);
}
}
void sort1()
{
sort(ptr, 0, size - 1);
for (int x = 0; x < size; x++)
{
if (ptr[x].degree == INT_MIN)
{
ptr[x].weight = INT_MIN;
}
}
}
void sort2()
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size - 1; y++)
{
if (ptr[y].weight < ptr[y + 1].weight)
{
swap(ptr[y], ptr[y + 1]);
}
}
}
}
float Density()
{
float count = 0.0, count1 = 0.0;
node3*p2=head1final;
node2*p3;
node3*p1 = head1final;
p1 = head1final;
//////////
while (p1 != NULL)
{
p3 = ptr[p1->data].head;
p2 = head1final;
while (p2 != NULL)
{
while (p3 != NULL)
{
if (p3->data == ptr[p2->data].data)
{
count++;

}
p3 = p3->next;
}
p2 = p2->next;
p3 = ptr[p1->data].head;
}
p1 = p1->next;
}
////////////
p1 = head1final;
while (p1 != NULL)
{
p1 = p1->next;
count1++;
}

float d = 2 * count;
d = d / (count1*(count1 - 1));
return d;
}
float CP(int y)
{
float count1 = 0.0;

node3*p2 = head1final;
while (p2 != NULL)
{
p2 = p2->next;
count1++;////nc
}
float d;
float count = 0.0;
p2 = head1final;

node2*p3;

p3 = ptr[y].head;
p2 = head1final;
while (p2 != NULL)
{
while (p3 != NULL)
{
if (p3->data == ptr[p2->data].data)
{
count++;//////Ec
}
p3 = p3->next;
}
p2 = p2->next;
p3 = ptr[y].head;
}
d = count / count1;
return d;
}
void fun2(float c1, float d1)
{

int index;
float cp = 0, d = 0;
///////////////////////////////////
if (point.check())
{
index= point.dequeue();
if (ptr[index].weight != INT_MIN)
{
if (head1final == NULL)
{
head1final = new node3;
head1final->data = index;
tail1final = head1final;
head1final->next = NULL;
ptr[index].weight = INT_MIN;
}
else if (head1final->next == NULL)
{
node3*p2 = new node3;
p2->data = index;
p2->next = head1final;
head1final = p2;
ptr[index].weight = INT_MIN;
}
else
{
/////checking density and cp
cp = CP(index);
node3*p2 = new node3;
p2->data = index;
p2->next = head1final;
head1final = p2;
d = Density();
cp = cp / d;
if (cp >= c1)
{

if (d >= d1)
{
ptr[index].weight = INT_MIN;
}
else
{
p2 = head1final;
head1final = head1final->next;
delete p2;

}
else
{
p2 = head1final;
head1final = head1final->next;
delete p2;
}

}
}
if (!point.check())
{

count7 = 0;
node3*p7 = head1final;
if (head1final->next!=NULL)
{
for (int z = 0; z < size; z++)
{
ptr[z].degree = 0;
}
while (p7 != NULL)
{
count7++;
cout << "index=" << p7->data<<" " ;
p7 = p7->next;
}
clusternum++;
finalnull();
cout << count7 << endl;
cout << "CLUSTER count= "<<clusternum << endl;
}
finalnull();
}
///////////////////////////////////

}
int returnindex(int a)
{
for (int x = 0; x < size; x++)
{
if (ptr[x].data == a)
{
return x;
}
}
return INT_MIN;

}
void fun(float c1,float d1)
{///
node2*p;
int check = 0;
int check3 = 0;
int index = 0;

///////////////
for (int x = 0; x < size; )
{
if (check == 0)
{
point.enqueue(x);
ptr[x].degree = INT_MIN;
}
p = ptr[x].head;
while (p != NULL)
{

index = returnindex(p->data);

if(index!=INT_MIN)
{
if ((ptr[index].degree != INT_MIN) &&
(ptr[index].weight != INT_MIN))
{
point.enqueue(index);
ptr[index].degree = INT_MIN;
}
}
p = p->next;

}
if (head1final != NULL)
{
weightofqueue();
queuee();
}
fun2(c1,d1);
index= point.check2();

if (ptr[index].data == 10)
{
}
if (index == INT_MIN)
{
for (int y = 0; y < size;y++)
{
if ((ptr[y].degree != INT_MIN) && (ptr[y].weight !=
INT_MIN))
{
check = 0;
x = y;
break;
}
}
}
else
{
x = index;
check = 1;
continue;
}
check3 = 1;
for (int z = 0; z < size; z++)
{
if (ptr[z].weight == INT_MIN)
{

}
else
{
check3 = -1;
}
}
if (check3 == 1)
{
break;
}
}
///////////////////////////////

}
void weightofqueue()
{

node3*p2 = head1final;
int count = 0;;
node2*p3 ;
node4*p1 = point.head;
////////////////////
while (p1 != NULL)
{
p3 = ptr[p1->data].head;
p2 = head1final;
while (p2!=NULL)
{
while (p3 != NULL)
{
if (p3->data == ptr[p2->data].data)
{
count++;
}
p3 = p3->next;
}
p2 = p2->next;
p3 = ptr[p1->data].head;
}
p1->weight = count;
count = 0;
p1 = p1->next;
}
//////////////////////
}
void nullhead()
{
for (int x = 0; x < size; x++)
{
if (ptr[x].degree == INT_MIN)
{
ptr[x].insert(INT_MIN);
}
}
}

};
int main()
{
operations obj1;
ifstream file;
file.open("ok.txt",ios::app);
int n1,n2;
if(file.is_open())
{
while (!file.eof())
{
file >> n1;
obj1.bst(n1);
}
}
file.close();
obj1.print();
file.open("ok.txt");
obj1.makearray();

if (file.is_open())
{
while (!file.eof())
{
file >> n1;
file >> n2;
if (file.eof())
break;
obj1.assigning(n1,n2);
}
}
cout << "ok1" << endl;
//obj1.printall();
obj1.assigningweight();
cout << endl;
cout << "ok2" << endl;
//obj1.printall();
obj1.sort2();
cout << endl;
cout << "ok3" << endl;
obj1.printall();
cout << "Enter CP::";
float cp;
cin >> cp;
cout << "Enter Density::";
float d;
cin >> d;
cout << endl;
obj1.fun(cp,d);
//cout << endl << clusternum << endl;
system("pause");
}

You might also like