#include
main()
{
int a[6][6];int s[6];int p[6];
int i,j,k,t=0,ri,rmin,par;
/* User Inputs */
for(i=0;i<6;i++)
for(j=0;j<6;j++)
cin>>a[i][j];
/* Set parent value -1 for all*/
for(i=0;i<6;i++)
p[i]=-1;
s[0]=0;
p[0]=0;
while(t<6){
for(i=0;i<=t;i++)
{
k=s[i];
for(j=0;j<6;j++)
{
rmin=9999;
if(a[k][j]>0)
{
if(a[k][j]
{
ri=j;
rmin=a[k][j];
par=k;
}
}
}
}
a[par][ri]=0;a[ri][par]=0;
p[ri]=par;
t++;
s[t]=ri;
}
p[0]=-1;
/* Display parent list for each node*/
for(i=0;i<6;i++)
cout<
}
you can change its size from 6 to n, using dynamic array, for this you have to first declare pointer with memory int* a=(int*)malloc(n*n*sizeof(int));
and replace a[i][j] with *(a+i*n+j);
declare single dimensial array as int* p=new int[n];
thanks for the algorithm.This was just what I was looking for
ReplyDelete