Created
March 12, 2009 10:16
-
-
Save anshumanatri/78003 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream.h> | |
#include<conio.h> | |
#include<stdio.h> | |
int set[10],n,w[10][10],c[10][10],ne=1,mincost; | |
void kruskal() | |
{ | |
int i,j,k,start,end; | |
while(ne<n) | |
{ | |
int min=1234; | |
for(i=0;i<n;i++) | |
for(j=0;j<n;j++) | |
if(c[i][j]<min) | |
{ | |
min=c[i][j];start=i;end=j; | |
} | |
if(set[start]!=set[end]) | |
{ | |
++ne; | |
cout<<"\nedge: ( "<<(1+start)<<" , "<<(end+1)<<" ) \tweight: "<<w[start][end]<<endl; | |
int temp=set[start]<set[end]?set[start]:set[end]; | |
int max=set[start]>set[end]?set[start]:set[end]; | |
for(k=0;k<n;k++) | |
if(set[k]==max) set[k]=temp; | |
mincost+=w[start][end]; | |
} | |
c[start][end]=c[end][start]=1234; | |
} | |
cout<<"\n Minimum cost:\t"<<mincost; | |
} | |
void main() | |
{ | |
clrscr(); | |
int i,j; | |
cout<<"\n Enter the value of n (i.e.), no of vertices:\t";cin>>n; | |
for(i=0;i<n;i++) set[i]=i; | |
cout<<"\nEnter the adjacency matrix:\n"; | |
for(i=0;i<n;i++) | |
for(j=0;j<n;j++) | |
{ | |
scanf("%d",&w[i][j]); | |
if(w[i][j]==0) w[i][j]=1234; | |
c[i][j]=w[i][j]; | |
} | |
kruskal(); | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment