时间:2021-05-19
在贪婪算法这一章提到了最小生成树的一些算法,首先是Kruskal算法,实现如下:
MST.h
复制代码 代码如下:
#ifndef H_MST
#define H_MST
#define NODE node *
#define G graph *
#define MST edge **
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;
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[])
{
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;
for (i = 0; i < g->nodeLen; i++)
makeset(g->nodelist[i]);
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;
}
在实现这个算法的时候,真正体会到了测试的重要性。程序能成功编译只是完成了一小部分,必须经过反复的测试才能发布。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文介绍了最小生成树的定义,Prim算法的实现步骤,通过简单举例实现了C语言编程。1.什么是最小生成树算法?简言之,就是给定一个具有n个顶点的加权的无相连通图,
之前都是看书,大部分也是c++的实现,但是搞前端不能忘了JS啊,所以JS实现一遍这两个经典的最小生成树算法。一、权重图和最小生成树权重图:图的边带权重最小生成树
最小生成树最小生成树(minimumspanningtree)是由n个顶点,n-1条边,将一个连通图连接起来,且使权值最小的结构。最小生成树可以用Prim(普里
次小生成树的定义设G=(V,E,w)是连通的无向图,T是图G的一个最小生成树。如果有另一棵树T1,满足不存在树T',ω(T')
以数据结构为例,最小生成树和最短路径的区别是最小生成树能够保证整个拓扑图的所有路径之和最小,但不能保证任意两点之间是最短路径。最短路径是从一点出发,到达目的地的