Thursday, March 13, 2014

minimum spanning tree in C++

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int i,k,j,l,mtix[7][7],v[7],flag[7][7],visit[7],vi[7],dist[7],path[6],addr,a,b,w,n,mini=0,count;
char ch='y';
cout<<"enter no. of vertex: ";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
mtix[i][j]=0; flag[i][j]=0;
}

for(i=0;i<n;i++)
{
dist[i]=99;
visit[i]=0;
vi[i]=-1;
}
while(ch=='y')
{
cout<<"for edge 1st vertex,weight, 2nd vertex ";
cin>>a>>w>>b;
mtix[a-1][b-1]=w;
mtix[b-1][a-1]=w;
cout<<"enter choice::";
cin>>ch;
}

cout<<"vertex from minimum spanning tree start";
cin>>a;
count=0;
v[count++]=a-1;

for(i=1;i<=n;i++)
{
mini=0;
for(j=0;j<i;j++)
{
for(k=0;k<n;k++)
for(l=0;l<n;l++)
 if(mtix[k][l]<mini && flag[k][l]!=1)
 {
  mini=mtix[k][l];
  a=k; b=l;
 }
}
flag[a][b]=1;

}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<mtix[i][j]<<" ";
cout<<endl;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<flag[i][j]<<" ";
cout<<endl;
}

return 0;
}

No comments:

Post a Comment

Contributors

Translate