最小生成树之克鲁卡斯尔算法

代码示意:重点有连通集的设计,选择排序,克鲁卡斯尔算法的用法
#include
#include
#include
typedef struct{
int start;
int end;
int weight;
}EdgeType;
typedef int FatherType;
typedef struct{
int ne,nv;
FatherType father[30];
EdgeType edge[30];
}Graph;

//创建一个图,其中边的权值为随机函数随机生成。
void Create(Graph &g)
{
    srand(time(0));
    printf("Please input the number of edge and vertex:\n");
    scanf ("%d%d",&g.ne,&g.nv);
    for(int i=0;ig.edge[j].weight){
                min=g.edge[j].weight;
                k=j;
            }
        }
        if(k!=i){
            temp=g.edge[k];
            g.edge[k]=g.edge[i];
            g.edge[i]=temp;
        }
    }
}

 //利用克鲁卡斯尔算法构造最小生成树
void MST(Graph g)
{
    int min=0,num=0;
    for(int i=0;i\n",g.edge[i].start,g.edge[i].end,g.edge[i].weight);
        min+=g.edge[i].weight;
    }
    printf("the min spanning tree's cost is %d\n",min);
}
void Print(Graph g)
{
    for(int i=0;i

你可能感兴趣的:(最小生成树之克鲁卡斯尔算法)