| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 711 人关注过本帖
标题:kruskal算法的实现
只看楼主 加入收藏
xiaobai1593
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-2-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
kruskal算法的实现
看算法实现,有些贴出来的代码都有问题,自己做了一个,大家共同进步
编译环境为vc6
在下新人,望多指教
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;


#define MAXNUM_VERTEX 20    //最多有 20个顶点
#define MAXNUM_EDGE 21

typedef struct
{
    int v,w;     
    int weight;
}Edge;

typedef struct
{
    int vertex[MAXNUM_VERTEX];     
    Edge edge[MAXNUM_EDGE];
    int num_vertex, num_edge;
}Graph;

Graph graph;    //声明为全局变量

bool search_vertex(int ver)
{
    for(int i=0; i<graph.num_vertex; i++)
        if( ver == graph.vertex[i] )
            return 1;
    printf("the vertex %d you input is not existed! \n", ver);
        return 0;
}

void create_graph()
{
    //输入顶点信息
    printf("input the number of vertex in the graph\n");
    scanf(" %d", &graph.num_vertex);

    printf("input the vertex of graph, like 1,2\n");
    for(int i=0; i<graph.num_vertex; i++)
        scanf(" %d", &graph.vertex[i]);
    //输入边信息
    printf("input the number of edge in the graph\n");
    scanf(" %d", &graph.num_edge);
    printf("intput the edge of graph and the weight of line, like 1-2 5\n");
    for(int j=0; j<graph.num_edge; j++)
    {
p1:        int a,c,d;
        char b;
        scanf(" %d %c %d %d", &a, &b, &c, &d);
        if( search_vertex(a) && search_vertex(c) )
        {
            graph.edge[j].v=a;
            graph.edge[j].w=c;
            graph.edge[j].weight=d;
        }
        else
            goto p1;
    }
}

void sort_by_weight()
{
    for(int i=1; i<graph.num_edge; i++)
    {
        Edge temp=graph.edge[i];
        for(int j=i-1; j>=0 && graph.edge[j].weight>temp.weight; j--)
            graph.edge[j+1]=graph.edge[j];
        graph.edge[j+1]=temp;
    }
}

/*不相交集处理函数*/

inline void makeset(vector<int> & array)
{
    for(int i=0; i<array.size(); i++)
        array[i]=i;
}

int findset(vector<int> & parent, int i)
{
    if(i != parent[i])
        i=parent[i];
    return i;
}

inline void merge(vector<int> & parent, int i, int j)
{
    parent[i]=j;
}

/*不相交集处理函数*/

void kruskal()
{
    int num_ver=graph.num_vertex;
    int count=0;
    vector<int> parent_ver;
    parent_ver.resize(num_ver);
    /*核心部分是用不相交集合成树*/
    makeset(parent_ver);
    printf("the edge of min tree as follow: \n");
    for( int i=0; i<num_ver && count<num_ver-1; i++ )    //count表示合并(merge)的次数num_ver-1次
    {
        int m=graph.edge[i].v;
        int n=graph.edge[i].w;
        int w=graph.edge[i].weight;
        if( findset(parent_ver,m) != findset(parent_ver,n))     //当属于不同的集合时,则将该边添加进树中
        {
            printf("(%d %d) %d\n", m,n,w);
            merge(parent_ver,m,n);
            count++;
        }
    }
}

void print_edge()
{
    printf("the graph you input as follow: \n");
    for(int i=0; i<graph.num_edge; i++)
        printf("%d-%d the weight is: %d\n", graph.edge[i].v, graph.edge[i].w, graph.edge[i].weight);
}

void main()
{
    create_graph();
    sort_by_weight();
    print_edge();
    kruskal();
}



  

[ 本帖最后由 xiaobai1593 于 2011-3-2 16:47 编辑 ]
搜索更多相关主题的帖子: 算法 color 
2011-03-02 16:41
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:20 
学习

   唯实惟新 至诚致志
2011-03-02 16:44
xiaobai1593
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-2-25
收藏
得分:0 
本来想把实现的截图也发上去的,可是不知道怎么插  
2011-03-02 18:26
arklau
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2011-5-11
收藏
得分:0 
学习
2011-05-11 21:56
快速回复:kruskal算法的实现
数据加载中...
 
   



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

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