最小生成树算法C语言代码实例
在贪婪算法这一章提到了最小生成树的一些算法,首先是Kruskal算法,实现如下:
MST.h
#ifndef H_MST
#define H_MST
#define NODE node *
#define G graph *
#define MST edge **
/* the undirect graph start */
typedef struct _node {
char data;
int flag;
struct _node *parent;
} node;
typedef struct _edge {
node *A;
node *B;
int w;
} edge;
typedef struct _graph {
node **nodelist;
int nodeLen;
edge **edgelist;
int edgeLen;
} graph;
/* the undirect graph end */
int kruskal(G , edge *[]);
int makeset(NODE);
int find(NODE , NODE);
int merge(NODE , NODE);
int comp(const void *, const void *);
#endif
MST.c
#include "mst.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
/* Construct the undirect connected graph */
graph g;
g.nodeLen = 6;
g.edgeLen = 10;
node node_a, node_b, node_c, node_d, node_e, node_f;
edge edge_1, edge_2, edge_3, edge_4, edge_5, edge_6, edge_7, edge_8, edge_9, edge_10;
node_a.data = 'a';
node_a.flag = 0;
node_a.parent = (node *)malloc(sizeof(node));
node_b.data = 'b';
node_b.flag = 0;
node_b.parent = (node *)malloc(sizeof(node));
node_c.data = 'c';
node_c.flag = 0;
node_c.parent = (node *)malloc(sizeof(node));
node_d.data = 'd';
node_d.flag = 0;
node_d.parent = (node *)malloc(sizeof(node));
node_e.data = 'e';
node_e.flag = 0;
node_e.parent = (node *)malloc(sizeof(node));
node_f.data = 'f';
node_f.flag = 0;
node_f.parent = (node *)malloc(sizeof(node));
edge_1.A = &node_a;
edge_1.B = &node_b;
edge_1.w = 5;
edge_2.A = &node_a;
edge_2.B = &node_c;
edge_2.w = 6;
edge_3.A = &node_a;
edge_3.B = &node_d;
edge_3.w = 4;
edge_4.A = &node_b;
edge_4.B = &node_c;
edge_4.w = 1;
edge_5.A = &node_b;
edge_5.B = &node_d;
edge_5.w = 2;
edge_6.A = &node_c;
edge_6.B = &node_d;
edge_6.w = 2;
edge_7.A = &node_c;
edge_7.B = &node_e;
edge_7.w = 5;
edge_8.A = &node_c;
edge_8.B = &node_f;
edge_8.w = 3;
edge_9.A = &node_d;
edge_9.B = &node_f;
edge_9.w = 4;
edge_10.A = &node_e;
edge_10.B = &node_f;
edge_10.w = 4;
node **nodelist;
nodelist = (node **)malloc(sizeof(node *) * g.nodeLen);
edge **edgelist;
edgelist = (edge **)malloc(sizeof(edge *) * g.edgeLen);
nodelist[0] = &node_a;
nodelist[1] = &node_b;
nodelist[2] = &node_c;
nodelist[3] = &node_d;
nodelist[4] = &node_e;
nodelist[5] = &node_f;
edgelist[0] = &edge_1;
edgelist[1] = &edge_2;
edgelist[2] = &edge_3;
edgelist[3] = &edge_4;
edgelist[4] = &edge_5;
edgelist[5] = &edge_6;
edgelist[6] = &edge_7;
edgelist[7] = &edge_8;
edgelist[8] = &edge_9;
edgelist[9] = &edge_10;
g.nodelist = nodelist;
g.edgelist = edgelist;
edge *X[g.nodeLen-1];
int e = 0;
while (e < g.edgeLen)
{
printf("%c-%c %d\n", g.edgelist[e]->A->data, g.edgelist[e]->B->data, g.edgelist[e]->w);
e++;
}
printf("------------------------------------------------------\n");
kruskal(&g, X);
e = 0;
while (e < (g.nodeLen-1))
{
printf("%c-%c %d\n", X[e]->A->data, X[e]->B->data, X[e]->w);
e++;
}
}
int kruskal(G g, edge *pX[])
{
int i, j;
/* Initially every disjoint set have one node */
for (i = 0; i < g->nodeLen; i++)
makeset(g->nodelist[i]);
/* sort the edgelist */
qsort(g->edgelist, g->edgeLen, sizeof(edge *), comp);
int e = 0;
while (e < g->edgeLen)
{
printf("%c-%c %d\n", g->edgelist[e]->A->data, g->edgelist[e]->B->data, g->edgelist[e]->w);
e++;
}
printf("------------------------------------------------------\n");
node da, db;
da.parent = (node *)malloc(sizeof(node));
db.parent = (node *)malloc(sizeof(node));
for (j = 0; j < g->edgeLen; j++)
{
find(g->edgelist[j]->A, &da);
find(g->edgelist[j]->B, &db);
if (da.data != db.data)
{
merge(g->edgelist[j]->A, g->edgelist[j]->B);
*pX++ = g->edgelist[j];
}
}
}
int makeset(NODE n)
{
n->parent = n;
}
int find(NODE n, NODE ds)
{
if (n->parent == n)
{
ds->data = n->data;
ds->flag = 1;
ds->parent = n->parent;
}
if (n->parent != n)
find(n->parent, ds);
}
int merge(NODE da, NODE db)
{
if (da->flag)
db->parent = da;
else
da->parent = db;
}
int comp(const void *ea, const void *eb)
{
if ((*(edge **)ea)->w > (*(edge **)eb)->w) return 1;
else if ((*(edge **)ea)->w == (*(edge **)eb)->w ) return 0;
else return -1;
}
在实现这个算法的时候,真正体会到了测试的重要性。程序能成功编译只是完成了一小部分,必须经过反复的测试才能发布。
上一篇:深入理解C语言 static、extern与指针函数
栏 目:C语言
下一篇:C语言顺序表实现代码排错
本文标题:最小生成树算法C语言代码实例
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/3866.html
您可能感兴趣的文章
- 01-10深入二叉树两个结点的最低共同父结点的详解
- 01-10深入理解二叉树的非递归遍历
- 01-10深入遍历二叉树的各种操作详解(非递归遍历)
- 01-10生成随机数rand函数的用法详解
- 01-10判断整数序列是否为二元查找树的后序遍历结果的解决方法
- 01-10如何用C语言生成简单格式的xml
- 01-10探讨:C++实现链式二叉树(用非递归方式先序,中序,后序遍历二叉树
- 01-10如何在二叉树中找出和为某一值的所有路径
- 01-10深入linux下遍历目录树的方法总结分析
- 01-10解析四则表达式的编译过程及生成汇编代码
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结
- 08-05DEDE织梦data目录下的sessions文件夹有什