Kruskal算法生成最小生成树

来源:互联网 发布:淘宝五星好评在哪里 编辑:程序博客网 时间:2024/05/16 11:20
//先按权值把边的权值升序,begin和end存入边集数组中 // Kruskal算法生成最小生成树int Find(int *parent, int f) {while(parent[f] > 0) {f = parent[f];}return f;} void MiniSpanTree_Kruskal(MGraph G) {int i, n, m;Edge edges[MAXEDGE];// 定义边集数组 int parent[MAXVEX];// 定义parent数组用来判断边与边是否形成环路for( i=0; i<G.numVertexes; i++ ){parent[i] = 0;} for( i=0; i<G.numEdges; i++ ){n = Find(parent, edges[i].begin);m = Find(parent, edges[i].end);if(n != m) // 如果n==m,则形成环路,不满足{parent[n] = m; //将此边的结尾顶点放入下标为起点的parent数组中,表示此顶点已经在生成树的集合中printf("(%d, %d) %d ", edges[i].begin, edges[i].end, edges[i].weight); }// @endif } // @endfor}

0 0
原创粉丝点击