简介
求最小生成树的目的:
最常见的便是,在多个途经点中计划出一条最快捷的路线。
求最小生成树的两种算法:
- 普利姆算法 Prim
- 克鲁斯卡尔算法 Kruskal
二者主要区别在于:
Prim通过遍历图的顶点来寻找最小生成树;Kruskal通过遍历边来寻找。
因此Prim的优势在于遍历稠密图,即点少边多的情况;Kruskal的优势在于遍历稀疏图,即边少点多的情况。
提示:下附算法代码,亲测可用
1. Prim算法
Prim基于贪心算法,遍历各顶点。
代码如下:
#include "iostream"
using namespace std;
#define MAX 1000
#define N 10
typedef struct {
int vnum;
int anum;
int vertex[N];
int edge[N][N];
}MGraph;
void CreateGraph(MGraph& G, int vnum, int v[6], int a[6][6])
{
G.vnum = vnum;
G.anum = vnum * vnum;
for (int i = 0; i < vnum; i++)
{
G.vertex[i] = v[i];
}
for (int i = 0; i < vnum; i++)
{
for (int j = 0; j < vnum; j++)
{
G.edge[i][j] = a[i][j];
}
}
return;
}
void prim(MGraph& G)
{
/// 1. 初始化
int min, i, j, k;
int adjvex[N];
int lowcost[N];
lowcost[0] = 0;
adjvex[0] = 0;
for (i = 1; i < G.vnum; i++)
{
lowcost[i] = G.edge[0][i];
adjvex[i] = 0;
}
/// 2. mini Tree
for (i = 1; i < G.vnum; i++)
{
min = MAX;
j = 1;
k = 0;
// 2.1 min weight in each loop
while (j < G.vnum)
{
if (lowcost[j] != 0 and lowcost[j] < min)
{
min = lowcost[j];
k = j;
adjvex[j] = min;
}
j++;
}
cout << "adjvex[k]: " << adjvex[k] << " k: " << k << endl;
lowcost[k] = 0;
// 2.2 get new array with low weight for next loop
for (j = 1; j < G.vnum; j++)
{
if (lowcost[j] != 0 and G.edge[k][j] < lowcost[j])
{
lowcost[j] = G.edge[k][j];
adjvex[j] = k;
}
}
}
}
int main()
{
MGraph G;
int vnum = 6;
int v[6] = {0, 1, 2, 3, 4, 5};
int a[6][6] = {0, 1, 3, 7, 9, 2,
1, 0, 4, 8, 3, 5,
3, 4, 0, 6, 11, 10,
7, 8, 6, 0, 17, 16,
9, 3, 11, 17, 0, 21,
2, 5, 10, 16, 21, 0};
CreateGraph(G, vnum, v, a);
prim(G);
return 0;
}
若输入邻接矩阵如下:
0 1 3 7 9 2
1 0 4 8 3 5
3 4 0 6 11 10
7 8 6 0 17 16
9 3 11 17 0 21
2 5 10 16 21 0
则运行结果如下:
adjvex[k]: 1 k: 1
adjvex[k]: 2 k: 5
adjvex[k]: 3 k: 2
adjvex[k]: 3 k: 4
adjvex[k]: 6 k: 3
每次循环的lowcost数组为:
0 1 3 7 9 2
0 0 3 7 3 2
0 0 3 7 3 0
0 0 0 6 3 0
0 0 0 6 0 0
2. Kruskal算法
代码如下:
#include <iostream>
#define N 100
typedef struct
{
int v1;
int v2;
int weight;
}Edge;
typedef struct
{
int vnum;
int anum;
Edge edge[100];
int node[100];
}MGraph;
bool CreateALGraph(MGraph& G, int vnum, int anum, int n[N], int a[N][3])
{
G.vnum = vnum;
G.anum = anum;
for (int i = 0; i < vnum; i++)
{
G.node[i] = n[i];
}
for (int i = 0; i < anum; i++)
{
G.edge[i].v1 = a[i][0];
G.edge[i].v2 = a[i][1];
G.edge[i].weight = a[i][2];
}
return true;
}
int parent[N];
int Find(int i)
{
while (parent[i] > 0)
i = parent[i];
return i;
}
int MiniTree(MGraph& G)
{
for (int i = 0; i < G.vnum; i++)
parent[i] = 0;
for (int i = 0; i < G.anum; i++)
{
int m = Find(G.edge[i].v1);
int n = Find(G.edge[i].v2);
if ( n != m)
{
parent[m] = n;
std::cout << "v1: " << G.edge[i].v1 << " v2: " << G.edge[i].v2 << " weight: " << G.edge[i].weight << std::endl;
}
}
return 0;
}
int main()
{
MGraph G;
int n[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int a[N][3] = {4, 7, 7,
2, 8, 8,
0, 1, 10,
0, 5, 11,
1, 8, 12,
3, 7, 16,
1, 6, 16,
5, 6, 17,
1, 2, 18,
6, 7, 19,
3, 4, 20,
3, 8, 21,
2, 3, 22,
3, 6, 24,
4, 5, 26};
CreateALGraph(G, 6, 15, n, a);
MiniTree(G);
return 0;
}
运行结果如下:
v1: 4 v2: 7 weight: 7
v1: 2 v2: 8 weight: 8
v1: 0 v2: 1 weight: 10
v1: 0 v2: 5 weight: 11
v1: 1 v2: 8 weight: 12
v1: 3 v2: 7 weight: 16
v1: 1 v2: 6 weight: 16
v1: 6 v2: 7 weight: 19