#include<iostream.h> #include<conio.h> #define Infinity 9999 #define Max_Vertex 10 void main(){ int cost[Max_Vertex][Max_Vertex],n,edg; int parent[Max_Vertex]={0}; clrscr(); cout<<"ENTER THE NUMBER OF VERTICES\n"; cin>>n; cout<<"ENTER THE NUMBER OF EDGES"; cin>>edg; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cost[i][j]=Infinity; } } cout<<"ENTER THE Edge (u,v) and COST\n"; int cst,j; for(int z=1;z<=edg;z++){ cin>>i>>j>>cst; cost[i][j]=cost[j][i]=cst; } int totEdgeCount=1; int u,v,tempU,tempV,mc=0; while(totEdgeCount<n){ for(int min=Infinity,i=1;i<=n;i++){ for(j=1;j<=n;j++){ if(cost[i][j]<min){ min=cost[i][j]; tempU=u=i; tempV=v=j; } } } while(parent[tempU]) tempU=parent[tempU]; while(parent[tempV]) tempV=parent[tempV]; if(tempV!=tempU){ cout<<totEdgeCount++<<" edge ("<<u<<","<<v<<") ="<<min<<"\n"; mc=mc+min; parent[tempV]=u; } cost[u][v]=cost[v][u]=Infinity; } cout<<"\nMINIMUM COST OF SPANNING TREE IS "<<mc<<"\n"; getch(); }