费力整出的Kruskal算法,使用小根堆和并查集,请大神指点
程序代码:
Graph* Kruskal(Graph *T, Graph *E) { int n = E->vexnum;//记录图E的顶点数量 MinHeap temp; int startroot, endroot; int i; for(i = 0; i < E->arcnum; ++i) //初始化并查集 parent[i] = -1; inHeap(E->arcnum, E); //将所有边存入堆中 if(E->arcnum <= n - 1) printf("No spanning tree.\n"); while(!IsEdgeEmpty(E)){ temp = removeHeapMin(); DeleteEdge(E, temp.start, temp.end); startroot = collaspingFind(temp.start); endroot = collaspingFind(temp.end); if(startroot != endroot){ InsertEdge(T, temp.start, temp.end, temp.weight); weightedUnion(startroot, endroot); } } return T; }