| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4861 人关注过本帖
标题:克鲁斯卡尔算法求最小生成树
取消只看楼主 加入收藏
yangzhks
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1135
专家分:13
注 册:2006-10-27
结帖率:28.57%
收藏
 问题点数:0 回复次数:3 
克鲁斯卡尔算法求最小生成树
我想知道克鲁斯卡尔算法求最小生成树,怎么做,谢谢
搜索更多相关主题的帖子: 成树 克鲁斯卡尔 算法 小生 
2006-12-30 12:41
yangzhks
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1135
专家分:13
注 册:2006-10-27
收藏
得分:0 
yes 想看看代码来着 算法会的
2007-01-04 10:24
yangzhks
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1135
专家分:13
注 册:2006-10-27
收藏
得分:0 

#include <stdio.h>
#define MAX_VERTEX_NUM 10
#define INFINITY 1000

typedef struct Edge
{
int weight;
}Edge, EdgeMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct MGraph
{
EdgeMatrix edges;
int vexnum;
int edgenum;
}MGraph;

typedef struct
{
int vertex;
int lowcost;
}Closedge;

void InitializeMG(MGraph &G)
{
G.edgenum = G.vexnum = 0;
for(int i = 0; i < MAX_VERTEX_NUM; ++i)
for(int j = 0; j < MAX_VERTEX_NUM; ++j)
G.edges[i][j].weight = INFINITY;
}

void CreateGraph(MGraph &G)
{
int i, j;
G.vexnum = 6;
G.edgenum = 10;

G.edges[0][1].weight = G.edges[1][0].weight = 6;
G.edges[0][2].weight = G.edges[2][0].weight = 1;
G.edges[0][3].weight = G.edges[3][0].weight = 5;
G.edges[1][2].weight = G.edges[2][1].weight = 5;
G.edges[1][4].weight = G.edges[4][1].weight = 3;
G.edges[2][3].weight = G.edges[3][2].weight = 5;
G.edges[2][4].weight = G.edges[4][2].weight = 6;
G.edges[2][5].weight = G.edges[5][2].weight = 4;
G.edges[3][5].weight = G.edges[5][3].weight = 2;
G.edges[4][5].weight = G.edges[5][4].weight = 6;

printf("\tv1\tv2\tv3\tv4\tv5\tv6\n");
for(i = 0; i < G.vexnum; ++i)
{
printf("\n\nv%d\t", i+1);
for(j = 0; j < G.vexnum; ++j)
printf("%d\t", G.edges[i][j].weight);
}
}

int GetMinIndex(MGraph G, Closedge closedge[])
{
int i, min_index = -1, min = INFINITY;
for(i = 1; i < G.vexnum; ++i)
if(closedge[i].lowcost && closedge[i].lowcost < min)
{
min = closedge[i].lowcost;
min_index = i;
}

return min_index;
}

bool MiniSpanTree_Prim(MGraph G, Closedge closedge[], int v0)
{
int i, j, k;
for(i = 0; i < G.vexnum; ++i)
{
closedge[i].vertex = v0;
closedge[i].lowcost = G.edges[v0][i].weight;
}
closedge[v0].lowcost = 0;

printf("\n\nThe Minimal-Span-Tree is composed of following edges:\nedge\t\tWeight\n");
for(i = 1; i < G.vexnum; ++i)
{
k = GetMinIndex(G, closedge);
if(k == -1)
return false;
printf("<%d, %d>\t\t%d\n",
closedge[k].vertex, k, closedge[k].lowcost);
closedge[k].lowcost = 0;
for(j = 0; j < G.vexnum; ++j)
if(G.edges[k][j].weight < closedge[j].lowcost)
{
closedge[j].vertex = k;
closedge[j].lowcost = G.edges[k][j].weight;
}
}
return true;
}

void main()
{
MGraph G;
Closedge closedge[6];

2007-01-06 13:36
yangzhks
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1135
专家分:13
注 册:2006-10-27
收藏
得分:0 
别人给我的 给大家看看
2007-01-06 13:37
快速回复:克鲁斯卡尔算法求最小生成树
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019754 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved