普里姆算法
邻接矩阵:
①adjvex数组存储相关顶点的下标,初始化时全部为0
②lowcost数组存储相关顶点的边的权值,初始化时为一个点的一维数组
③然后开始构建最小生成树,从下标0开始
a.从当前顶点开始,便来其一维数组,找出最小权值对应的下标值,然后就找到对应的最小值边的权值,
b.把lowcost【k】设置为0,代表顶点k已加入最小生成树
c.替换最小值对应的下标的那个一维数组的权值,和当前lowcost的权值进行比较,把更小的设置为其lowcost的值
d.把最小权值对应的下标值存入adjve数组中
空格代表无穷大
0 | 10 | | | | 11 | | | |
10 | 0 | 18 | | | | 16 | | 12 |
| | 0 | 22 | | | | | 8 |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
11 | | | | 26 | 0 | 17 | | |
| 16 | | | | 17 | 0 | 19 | |
| | | 16 | 7 | | 19 | 0 | |
| 12 | 8 | 21 | | | | | 0 |
数组第一排,除0外 10就是最小,找到10对应的下标,再找到对应下标的那排一维数组,然后和这一排对比,替换更小的值,下面红色代表替换的值
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
| | 0 | 22 | | | | | 8 |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
11 | | | | 26 | 0 | 17 | | |
| 16 | | | | 17 | 0 | 19 | |
| | | 16 | 7 | | 19 | 0 | |
| 12 | 8 | 21 | | | | | 0 |
在这一排中,11最小,找到第六排,替换值
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
| | 0 | 22 | | | | | 8 |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
0 | 0 | 18 | 26 | 0 | 16 | | 12 | |
| 16 | | | | 17 | 0 | 19 | |
| | | 16 | 7 | | 19 | 0 | |
| 12 | 8 | 21 | | | | | 0 |
12为最小值,跑到最后一排,进行替换
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
| | 0 | 22 | | | | | 8 |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
0 | 0 | 18 | 26 | 0 | 16 | | 12 | |
| 16 | | | | 17 | 0 | 19 | |
| | | 16 | 7 | | 19 | 0 | |
0 | 0 | 8 | 21 | 26 | 0 | 16 | 0 |
8为最小值,去第三排
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
0 | 0 | 0 | 21 | 26 | 0 | 16 | 0 | |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
0 | 0 | 18 | 26 | 0 | 16 | | 12 | |
| 16 | | | | 17 | 0 | 19 | |
| | | 16 | 7 | | 19 | 0 | |
0 | 0 | 8 | 21 | 26 | 0 | 16 | 0 |
16为最小值,倒数第三排
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
0 | 0 | 0 | 21 | 26 | 0 | 16 | 0 | |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
0 | 0 | 18 | 26 | 0 | 16 | | 12 | |
0 | 0 | 0 | 21 | 26 | 0 | 0 | 19 | 0 |
| | | 16 | 7 | | 19 | 0 | |
0 | 0 | 8 | 21 | 26 | 0 | 16 | 0 |
19为最小值,倒数第二排
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
0 | 0 | 0 | 21 | 26 | 0 | 16 | 0 | |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
0 | 0 | 18 | 26 | 0 | 16 | | 12 | |
0 | 0 | 0 | 21 | 26 | 0 | 0 | 19 | 0 |
0 | 0 | 0 | 16 | 7 | 0 | 0 | 0 | 0 |
0 | 0 | 8 | 21 | 26 | 0 | 16 | 0 |
最小值为7,第六排
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
0 | 0 | 0 | 21 | 26 | 0 | 16 | 0 | |
| | 22 | 0 | 20 | | | 16 | 21 |
0 | 0 | 0 | 16 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 18 | 26 | 0 | 16 | 12 | ||
0 | 0 | 0 | 21 | 26 | 0 | 0 | 19 | 0 |
0 | 0 | 0 | 16 | 7 | 0 | 0 | 0 | 0 |
0 | 0 | 8 | 21 | 26 | 0 | 16 | 0 |
最小值为16,第四排
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
0 | 0 | 0 | 21 | 26 | 0 | 16 | 0 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 16 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 18 | 26 | 0 | 16 | | 12 | |
0 | 0 | 0 | 21 | 26 | 0 | 0 | 19 | 0 |
0 | 0 | 0 | 16 | 7 | 0 | 0 | 0 | 0 |
0 | 0 | 8 | 21 | 26 | 0 | 16 | 0 |
全部替换为0,则全部顶点已经在最小生成树里,
每次找出的最小值就是这个图的最小生成树
代码如下,摘自大话数据结构:
void MiniSpanTree_Prim(MGraph G)
{
int min, i, j, k;
int adjvex[MAXVEX]; /* 保存相关顶点下标 */
int lowcost[MAXVEX]; /* 保存相关顶点间边的权值 */
lowcost[0] = 0;/* 初始化第一个权值为0,即v0加入生成树 */
/* lowcost的值为0,在这里就是此下标的顶点已经加入生成树 */
adjvex[0] = 0; /* 初始化第一个顶点下标为0 */
for(i = 1; i < G.numVertexes; i++) /* 循环除下标为0外的全部顶点 */
{
lowcost[i] = G.arc[0][i]; /* 将v0顶点与之有边的权值存入数组 */
adjvex[i] = 0; /* 初始化都为v0的下标 */
}
for(i = 1; i < G.numVertexes; i++)
{
min = INFINITY; /* 初始化最小权值为∞, */
/* 通常设置为不可能的大数字如32767、65535等 */
j = 1;k = 0;
while(j < G.numVertexes) /* 循环全部顶点 */
{
if(lowcost[j]!=0 && lowcost[j] < min)/* 如果权值不为0且权值小于min */
{
min = lowcost[j]; /* 则让当前权值成为最小值 */
k = j; /* 将当前最小值的下标存入k */
}
j++;
}
printf("(%d, %d)\n", adjvex[k], k);/* 打印当前顶点边中权值最小的边 */
lowcost[k] = 0;/* 将当前顶点的权值设置为0,表示此顶点已经完成任务 */
for(j = 1; j < G.numVertexes; j++) /* 循环所有顶点 */
{
if(lowcost[j]!=0 && G.arc[k][j] < lowcost[j])
{/* 如果下标为k顶点各边权值小于此前这些顶点未被加入生成树权值 */
lowcost[j] = G.arc[k][j];/* 将较小的权值存入lowcost相应位置 */
adjvex[j] = k; /* 将下标为k的顶点存入adjvex */
}
}
}
}
0 | 10 | | | | 11 | | | |
0 | 0 | 18 | | | 11 | 16 | | 12 |
| | 0 | 22 | | | | | 8 |
| | 22 | 0 | 20 | | | 16 | 21 |
| | | 20 | 0 | 26 | | 7 | |
11 | | | | 26 | 0 | 17 | | |
| 16 | | | | 17 | 0 | 19 | |
| | | 16 | 7 | | 19 | 0 | |
| 12 | 8 | 21 | | | | | 0 |