Apr 16, 2014 at 9:53pm UTC
Hi I am not able to access the class variable noof_vertex in the function merge , the error is the variable is private
below is the code
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int parent[200];
class kruskal
{
public:kruskal()
{
int count1, count2, u, v, noof_vertex, noof_edge, x, y;
int min_wt, wt[200][200], min;
int mst_wt[200][200] ;
noof_edge = 1;
min_wt = 0;
noof_vertex = 0;
}
void initialize();
void mst(int wt[][200], int noof_vertex);
void display(int wt[][200],int noof_vertex);
void merge(kruskal k1, kruskal k2, int final_vertex);
};
void kruskal::initialize()
{
srand(time(0));
noof_vertex = 4;
cout<<"\nNumber of vertices in the graph is "<<noof_vertex;
cout<<"\nThe adjacency matrix is:\n";
for (count1 = 1; count1<=noof_vertex; count1++)
for (count2 = count1; count2<=noof_vertex; count2++)
{
if(count1 == count2)
wt[count1][count2]= 999;
else
{
wt[count1][count2] = wt[count2][count1] = 1+(rand() % 4);
}
}
display(wt, noof_vertex);
mst(wt,noof_vertex);
}
void kruskal::mst(int wt[][200], int noof_vertex)
{
for (count1 = 1; count1<=noof_vertex; count1++)
for (count2 = 1; count2<=noof_vertex; count2++)
{
mst_wt[count1][count2] = 0;
if(wt[count1][count2] == 0)
wt[count1][count2] = 999;
}
cout<<"\nThe minimum spanning tree consists of following edges\n";
while(noof_edge<noof_vertex)
{
min = 999;
for (count1 = 1; count1<=noof_vertex; count1++)
for (count2 = 1; count2<=noof_vertex; count2++)
if(wt[count1][count2]<min)
{
min = wt[count1][count2];
x=u=count1;
y=v=count2;
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
noof_edge++;
//cout<<"\nedge("<<x<<"->"<<y<<")="<<min;
mst_wt[x][y] = min;
mst_wt[y][x] = min;
min_wt = min_wt + min;
parent[v]=u;
}
wt[x][y] = wt[y][x] = 999;
}
cout<<"\nMinimum weight="<<min_wt<<"\n";
display(mst_wt, noof_vertex);
}
void kruskal::display(int wt[][200],int noof_vertex)
{
for (count1 = 1; count1<=noof_vertex; count1++)
{
for (count2 = 1; count2<=noof_vertex; count2++)
{
if(wt[count1][count2] == 999)
wt[count1][count2] = 0;
cout<<wt[count1][count2]<<" ";
wt[count2][count1] = wt[count1][count2];
}
cout<<"\n";
}
}
void kruskal::merge(kruskal k1, kruskal k2, int final_vertex)
{
int merge_wt[200][200];
for (r=1;r<=k1.noof_vertex;r++)
for(s=r;s<=k1.noof_vertex;s++)
merge_wt[r][s] = k1.wt[r][s];
for (r=(k1.noof_vertex+1);r<=(final_vertex);r++)
for(s=(k1.noof_vertex+1);s<=(final_vertex);s++)
merge_wt[r][s] = k2.wt[r-k1.noof_vertex+1][s-k1.noof_vertex+1];
r = 1;
for(s=(k1.noof_vertex+1);s<=(final_vertex);s++)
merge_wt[r][s] = k2.wt[r][s-k1.noof_vertex+1];
for (r=2;r<=k1.noof_vertex;r++)
for(s=(k1.noof_vertex+1);s<=(final_vertex);s++)
merge_wt[r][s] = 0;
k1.display(merge_wt,final_vertex);
k1.mst(merge_wt,final_vertex);
}
int main()
{
int noof_graph, final_vertex, common_vertex, r,s;
noof_graph = 0;
cout <<"\nEnter the number of graphs in the system : ";
cin >> noof_graph;
srand(time(0));
common_vertex = 1+(rand()%1);
cout<<"\nNoof common vertices between the 2 graph is :"<<common_vertex;
switch(noof_graph)
{
case 2:
{
kruskal k1;
kruskal k2;
kruskal k3;
k1.initialize();
k2.initialize();
final_vertex = k1.noof_vertex + k2.noof_vertex - common_vertex;
k3.merge(k1,k2,final_vertex);
}
break;
/*case 3:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
break;
case 4:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
kruskal k4;
k4.initialize();
break;
case 5:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
kruskal k4;
k4.initialize();
kruskal k5;
k5.initialize();
break;
case 6:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
kruskal k4;
k4.initialize();
kruskal k5;
k5.initialize();
kruskal k6;
k6.initialize();
break;
case 7:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
kruskal k4;
k4.initialize();
kruskal k5;
k5.initialize();
kruskal k6;
k6.initialize();
kruskal k7;
k7.initialize();
break;
case 8:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
kruskal k4;
k4.initialize();
kruskal k5;
k5.initialize();
kruskal k6;
k6.initialize();
kruskal k7;
k7.initialize();
kruskal k8;
k8.initialize();
break;
case 9:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
kruskal k4;
k4.initialize();
kruskal k5;
k5.initialize();
kruskal k6;
k6.initialize();
kruskal k7;
k7.initialize();
kruskal k8;
k8.initialize();
kruskal k9;
k9.initialize();
break;
case 10:
kruskal k1;
k1.initialize();
kruskal k2;
k2.initialize();
kruskal k3;
k3.initialize();
kruskal k4;
k4.initialize();
kruskal k5;
k5.initialize();
kruskal k6;
k6.initialize();
kruskal k7;
k7.initialize();
kruskal k8;
k8.initialize();
kruskal k9;
k9.initialize();
kruskal k10;
k10.initialize();
break;*/
default :
cout<<"\nNo.of graphs should be greater than 1 and less than 10";
}
return 0;
}
Apr 16, 2014 at 10:45pm UTC
You accidentally declared all the variables in the body of your constructor instead of in the body of your class, and you also accidentally created an initialize function instead of using your constructor.