KRUSHKAL - return mincost void main int...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<stdio.h> #include<conio.h> #define i100 #define max 5 int kruskal(int weight[][max]) { int k,nodecount,mincost=0,parent[max]={0,0,0,0,0}; struct edge { int v1,v2,wt; }e[20]; int i,j,size = 0; for(i=0;i<max-1;i++) { for(j=i+1;j<max;j++) { if(weight[i][j]!=i) { e[size].v1=i; e[size].v2=j; e[size].wt=weight[i][j]; size++; } } } for(i=0;i<size-1;i++) { for(j=i+1;j<size;j++) { if(e[j].wt<e[i].wt) { struct edge temp=e[i]; e[i]=e[j]; e[j]=temp; } } } nodecount=1; for(k=0;nodecount<max;k++) { i=e[k].v1; j=e[k].v2; while(parent[i]>0)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
i=parent[i]; while(parent[j]>0) j=parent[j]; if(i!=j) { parent[j]=i; printf("%d-%d\t%d\n",e[k].v1,e[k].v2,e[k+1].wt); mincost=mincost+e[k+1].wt; nodecount++;
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } } return mincost; } void main() { int i,j,z,weight[max][max]; clrscr(); printf("\n enter the weight matrix "); for(i=0;i<max;i++) { for(j=0;j<max;j++) scanf("%d",&weight[i][j]); } z=kruskal(weight); printf("\n thus the minmum cost is = %d",z); getch(); } /* **************OUTPUT****************** enter the weight matrix 0 800 410 612 0 800 0 2985 300 200 410 2985 0 1421 0 612 300 1421 0 400 0 200 0 400 0 2-5 200 2-4 300 1-3 410 1-4 612 thus the minmum cost is = 1522 */...
View Full Document

This note was uploaded on 10/25/2010 for the course CMPN SEM4 taught by Professor Thakur during the Spring '07 term at University of Mumbai.

Page1 / 2

KRUSHKAL - return mincost void main int...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online