#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define INFNITY 999
int a[10][10],key[10][2],n,p[10];
voidprintArray(){
inti,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
voiduAdjacencyMatrix(){
inti,j,c=1,w;
for(i = 0;i < n; i++)
{
for(j = 0;j < i; j++)
{
a[i][j]=a[j][i] = INFNITY;
}
a[i][i] = INFNITY;
}
while(c!=-1)
{
printf ("\ni , j , w :");
scanf("%d%d%d",&i,&j,&w);
a[i][j]=w;
a[j][i]=w;
//printf
("c? :");
scanf("%d",&c);
}
printArray();
getch();
}
voidsetParent()
{
int i;
for (i=0;i<n;i++)
{
p[i]=-1;
key[i][0]=999;
key[i][1]=0;
}
}
voidexMin(int *aa)
{
inti,w=999;
for (i=0;i<n;i++)
{
if( key[i][0]<w && key[i][1]==0)
{
w=key[i][0];
*aa=i;
}
}
}
int main()
{
printf("enter the number of vertices : ");
scanf("%d",&n);
uAdjacencyMatrix();
setParent();
key[0][0]=0;
intcount,u=0,v=0,w=0,j,i;
count=n;
while(count>0)
{
exMin(&i);
//printf("\n i : %d",i);
key[i][1]=1;
if(p[i]!=-1)
printf("\n%d-->%d", p[i],i);
if( p[i]!=-1)
w+=a[p[i]][i];
for( j=0; j<n; j++)
{
if(
a[i][j] != INFNITY && key[j][1]==0 && a[i][j]< key[j][0] )
{
p[j]=i;
key[j][0]= a[i][j];
}
}
count--;
}
printf( "\nTree Weight : %d", w);
getch();
return(0);
}
No comments:
Post a Comment