i have that same subject this semester...the prof sucks(as do most of them)
we had to make an implimentation of krustuls algo during the last lab class
here it is in C
good luck understanding it...all i know it worked..eureka
Code:
#include<stdio.h>
int allconn(int con[][10],int no)
{
int q,w;
for(q=0;q<no;q++)
for(w=0;w<no;w++)
if(con[q][w]==0)
return 0;
return 1;
}
int moresameweight(int min,int flag[][10],int weight[][10],int no,int *q1,int *w1)
{
int q,w;
for(q=0;q<no;q++)
for(w=0;w<no;w++)
if(min==weight[q][w]&&flag[q][w]==0)
{
*q1=q;*w1=w;
return 1;
}
return 0;
}
int main()
{
int noofno,weight[10][10],flag[10][10],con[10][10];
int next=0,q1,w1,q=1,w=1,e,minfound=100;
printf("Number of nodes:");
scanf("%d",&noofno);
for(q=0;q<noofno;q++)
for(w=0;w<noofno;w++)
{weight[q][w]=50;flag[q][w]=0;con[q][w]=0;con[q][q]=1;}
printf("Enter node1,node2,weight (0 0 0 to end):\n");
while(!(q==0&&w==0))
{
scanf("%d %d %d",&q,&w,&e);
weight[q-1][w-1]=e;weight[w-1][q-1]=e;
}
while(allconn(con,noofno)==0)
{
if(minfound==100&&next==0)
{
next=1;
for(q=0;q<noofno;q++)
for(w=0;w<noofno;w++)
if(minfound>weight[q][w])
{minfound=weight[q][w];q1=q;w1=w;}
flag[q1][w1]=1;flag[w1][q1]=1;con[q1][w1]=1;con[w1][q1]=1;
printf("new con:%d %d\n",q1+1,w1+1);
}
if(minfound>49&&next==1)
{printf("over\n");
exit(0);
}
while(moresameweight(minfound,flag,weight,noofno,&q1,&w1)==0)
{
minfound++;
}
flag[q1][w1]=1;flag[w1][q1]=1;
con[q1][w1]=1;con[w1][q1]=1;
printf("new con:%d %d\n",q1+1,w1+1);
for(q=0;q<noofno;q++)
for(w=0;w<noofno;w++)
for(e=0;e<noofno;e++)
{
if(con[q][w]==1&&con[w][e]==1)
{
con[q][e]=1;con[e][q]=1;
}
}
}
return 0;
}