Algorithm

Saturday, January 17, 2009

Algorithm Kruskale(E,cost,n,t) //E is an array E[1....n][1....3],such that E[1][1]&E[1][2] denote the two //adjacent vertex & E[i][3]denote the edge value of this two adjacent vertex. //G has n vertex.cost(i,j) is the cost of vertex[ i ,j ]. //t is the set of edge in minimum cost spanning tree.The final cost is returned. { Sorting the edge in increasing order according to edge value . for(i=1;i<=n;i++)//Consider each vertex is in a different set. tree[i]=i; op=n+1; for(i=1;i<=edge;i++) {//Find edge for t among the given edge. m=tree[E[i][1]];//E[i][1]&E[i][2] are two adjacent vertex. n=tree[E[i][2]]; if(tree[E[i][1]]!=tree[E[i][2]])//check two vertex are the element of different set. { for(j=1;j<=n;j++) if((m==tree[j])||(n==tree[j])) //keep each element of two different vertex in a new set. tree[j]=op; //E[i][3] is the edge value betwwen E[i][1]&E[i][2]. mincost=mincost+E[i][3]; t[k][1]=E[i][1]; t[k][2]=E[i][2]; k++; op++; } } return mincost; }

0 comments:

Visitors

PlugIn.ws - Free Hit Counter, Web Site Statistics, Traffic Analysis

  © Blogger template Leaving by Ourblogtemplates.com 2008

Back to TOP