最小生成树
Prim算法
Prim算法 主要是从结点出发,需要用到2个数组
path数组保存结点从哪个结点过来
lowcost保存起始结点到其他结点的代价,可以直接读邻接矩阵的内容
path = {-1, 1, 1, 0, 0}
lowcost = {0, 10, 5, ∞, ∞}
显然我们要遍历n-1轮,每1轮确定1个结点
第1轮遍历 找到lowcost中最小不为0的值,数组下标代表顶点
确定顶点后,看下跟他相连的结点的代价,跟原来的代价,谁大谁小,小的替换大的,修改代价后把path对应的点改成当前顶点
知道n-1轮结束
此时path 路径确定
lowcost 都是最小值
Kruscal算法
Kruscal算法 从边开始
typedef struct Edge{
int begin;
int end;
int weight;
}Edge;
把所有的边按权值排序
用到一个数组parent,用来判断是否产生回路
依次选用边,parent[3] = 4 表示顶点3 和 顶点4 已经在生成树集合中,每次都是占用新的位置(通过Find函数找到新位置和新的顶点)
当所有顶点都在集合中时,任意2个点通过Find函数找到的值都相同,此时说明再连已经成环形
直到所有边遍历完
Prim和Kruscal最小生成树代码
//
// seq_depth_graph.cpp
// Snake2021
//
// Created by Charming on 2021/10/10.
//
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define MAX_NUM 20 //顶点的最大个数
typedef struct {
int Vex[MAX_NUM];
int Edge[MAX_NUM][MAX_NUM];
int vexnum;
int edgenum;//边
}Graph;
void MiniSpanTreePrim(Graph g){
int path[6];//从哪个点过来的
int lowcost[6];//到每个点的最低花费
lowcost[1] = 0;
path[1] = -1;
cout << "min vertex :" << 1 << " path:" << path[1] << endl;
for (int i = 2; i<=g.vexnum; i++) {
lowcost[i] = g.Edge[1][i];//目标顶点1 到其他顶点的权值
path[i] = 1;
}
for (int i = 2; i<=g.vexnum; i++) {
int min = 999;
int j = 2, k = 0;
while (j<=g.vexnum) {
if (lowcost[j]!=0 && lowcost[j]<min) {
min = lowcost[j];
k = j;
}
j++;
}
lowcost[k] = 0;//标记已访问
cout << "vertex :" << k << " path:" << path[k] << endl;
for (j = 2; j<=g.vexnum; j++) {
// cout << "lowcost" << lowcost[j] << " " << g.Edge[k][j] <<endl;
if (lowcost[j]>g.Edge[k][j]) {
lowcost[j] = g.Edge[k][j];
path[j] = k;
}
}
}
}
typedef struct Edge{
int begin;
int end;
int weight;
}Edge;
int Find(int *parent, int f);
void MiniSpanTreeKruskal(Graph g){
Edge edges[10];
int k = 1;
for (int i = 1; i<10; i++) {
edges[i].begin = 0;
edges[i].end = 0;
edges[i].weight = 0;
}
for (int i = 1; i<=g.vexnum; i++) {
int j = i + 1;//无向图 只看上三角
while (j<=g.vexnum) {
if (g.Edge[i][j]!=0 && g.Edge[i][j]<999) {
edges[k].begin = i;
edges[k].end = j;
edges[k].weight = g.Edge[i][j];
k++;
}
j++;
}
}
int begin,end,weight;
for (int i = 1; i<g.edgenum; i++) {
for (int j= i+1; j<=g.edgenum; j++) {
if (edges[i].weight>edges[j].weight) {
begin = edges[i].begin;
end = edges[i].end;
weight = edges[i].weight;
edges[i].begin = edges[j].begin;
edges[i].end = edges[j].end;
edges[i].weight = edges[j].weight;
edges[j].begin = begin;
edges[j].end = end;
edges[j].weight = weight;
}
}
}
// cout << "sort after:" << endl;
// for (int i = 1; i<=g.vexnum; i++) {
// cout << "start-" << edges[i].begin << ";end-" << edges[i].end << ";weight-" << edges[i].weight << endl;
//
// }
cout << "result:" << endl;
int parent[10];
for (int i = 1; i<10; i++) {
parent[i] = 0;
}
int n,m;
for (int i = 1; i<=g.edgenum; i++) {
//找出头和尾分别权值最小的顶点
n = Find(parent, edges[i].begin);
m = Find(parent, edges[i].end);
if (n!=m) {
parent[n] = m;
cout << "start-" << edges[i].begin << ";end-" << edges[i].end << ";weight-" << edges[i].weight << endl;
}
}
}
// 找到没有被占用的位置
int Find(int *parent, int f){
while (parent[f]>0) {
f = parent[f];
}
return f;
}
int main(){
std::cout << "welcome, to my world!" << std::endl;
Graph graph;
for (int i=1; i<=5; i++) {
graph.Vex[i] = i;
}
for (int i=1; i<=5; i++) {
for (int j = 1; j<=5; j++) {
if (i==j) {
graph.Edge[i][j] = 999;//很大的数,表示不可到达
}else{
graph.Edge[i][j] = 999;
}
}
}
graph.Edge[1][2] = 10;
graph.Edge[1][3] = 5;
graph.Edge[2][4] = 2;
graph.Edge[2][5] = 3;
graph.Edge[4][3] = 1;
graph.Edge[2][1] = 10;
graph.Edge[3][1] = 5;
graph.Edge[4][2] = 2;
graph.Edge[5][2] = 3;
graph.Edge[3][4] = 1;
graph.vexnum = 5;
graph.edgenum = 5;
cout << "size of:" << sizeof(graph) <<endl;
cout << "MiniSpanTreePrim:" << endl;
MiniSpanTreePrim(graph);
cout << "MiniSpanTreeKruskal:" << endl;
MiniSpanTreeKruskal(graph);
return 0;
}
输出
welcome, to my world!
size of:1688
MiniSpanTreePrim:
min vertex :1 path:-1
vertex :3 path:1
vertex :4 path:3
vertex :2 path:4
vertex :5 path:2
MiniSpanTreeKruskal:
result:
start-3;end-4;weight-1
start-2;end-4;weight-2
start-2;end-5;weight-3
start-1;end-3;weight-5
Program ended with exit code: 0