Kruskal's algorithm

SOURCE CODE:
#include<stdio.h>
typedef struct{
int u,v,cost;
}edge;
edge E[20],e;
int n,near,parent[20],t[20][3];
void adjust(int i){
int j=2*i;
edge temp=E[i];
while(j<=near){
if((j<near) && (E[j].cost > E[j+1].cost))
j++;
if(temp.cost < E[j].cost)
break;
E[j/2]=E[j];
j*=2;
}
E[j/2]=temp;
}
void Union(int i, int j){
parent[i]=j;
}
int find(int i){
while(parent[i]>=0)
i=parent[i];
return i;
}
void heapify(){
for(int i=near/2;i>=1;i--)
adjust(i);
}
edge deletemin()
{
edge temp=E[1];
E[1]=E[near];
near--;
adjust(1);
return temp;
}
int main(){
int i,j,mincost,k;
printf("Enter # os vertices and edges :\n");
scanf("%d%d",&n,&near);
printf("Enter edge details:\nFIRST_EDGE LAST_EDGE COST\n");
for(i=1;i<=near;i++){
scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].cost);
parent[i]=-1;}
heapify();
i=0;mincost=0;
while((i<(n-1))&&(near>0))
{
e=deletemin();
j=find(e.u);k=find(e.v);
if(j!=k){
i++;
mincost+=e.cost;
printf("Edge %d : %d ---> %d = %d\n",i,e.u,e.v,e.cost);
Union(j,k);
}
}
if(i!=(n-1))
printf("NO SPANNING TREE\n");
else
printf("Minimum cost is : %d \n",mincost);
}
OUTPUT:
Enter # of vertices and edges :
7
9
Enter edge details:
FIRST_EDGE LAST_EDGE COST
1 2 28
1 6 10
2 3 16
2 7 14
3 4 12
4 5 22
4 7 18
5 6 25
5 7 24

Edge 1 : 1 ---> 6
Edge 2 : 3 ---> 4
Edge 3 : 2 ---> 7
Edge 4 : 2 ---> 3
Edge 5 : 4 ---> 5
Edge 6 : 5 ---> 6
Minimum cost is : 99

Comments