最小生成树—普里姆与克鲁斯卡尔
时间: 2024-05-27 15:07:34 浏览: 24
最小生成树是指在一张有权无向图中,生成一棵树,并使得这棵树上所有边的权值之和最小。普里姆和克鲁斯卡尔是两种常用的最小生成树算法。
普里姆算法的思想是从一个点开始,每次选择一个距离该点最近的未选择的点,将这个点与已选择的点形成一条边加入到最小生成树中。具体实现可以用堆来维护未选择的点集合。
克鲁斯卡尔算法的思想是先将所有边按照权值从小到大排序,然后依次将每条边加入到生成树中,但要保证加入后不会形成环,可以用并查集来维护已选择的点集合。
相关问题
无向图最小生成树 普里姆算法和克鲁斯卡尔算法
无向图最小生成树(Minimum Spanning Tree, MST)是指在一个无向图中,选取一些边,使得这些边连接的所有顶点构成一棵树,并且这棵树覆盖所有顶点,同时边的总权重尽可能小。在图论中有两种主要的算法用于求解最小生成树:普里姆算法(Prim's Algorithm)和克鲁斯卡尔算法(Kruskal's Algorithm)。
1. **普里姆算法**:
- 这是一种贪心算法,从任意一个顶点开始,每次添加一条与当前生成树中所有顶点相连且权重最小的新边,直到所有顶点都被包含。
- 操作过程是逐步构建树,始终保持边的选择是当前未加入树的顶点中距离最近的。
- 直接操作是邻接矩阵或邻接表,方便查找最短边。
2. **克鲁斯卡尔算法**:
- 这也是一种贪心策略,但它不是从一个顶点出发,而是从所有的边开始,每次选择一条能形成一棵树且权重最小的新边,直到树包含了所有顶点。
- 克鲁斯卡尔算法通常用并查集数据结构来辅助,因为需要频繁地合并集合。
- 这种算法适合边的数量远大于顶点的数量的情况,因为它不需要维护一个已访问过的集合。
最小生成树的普里姆算法和克鲁斯卡尔算法c语言
以下是最小生成树的普里姆算法和克鲁斯卡尔算法的C语言实现:
1. 普里姆算法
```c
#include <stdio.h>
#include <limits.h>
#define V 5
int minKey(int key[], bool mstSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
void printMST(int parent[], int graph[V][V])
{
printf("Edge \tWeight\n");
for (int i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
void primMST(int graph[V][V])
{
int parent[V];
int key[V];
bool mstSet[V];
for (int i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
printMST(parent, graph);
}
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
}
```
2. 克鲁斯卡尔算法
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define V 5
#define E 9
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
struct Edge* edge;
};
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*)malloc(graph->E * sizeof(struct Edge));
return graph;
}
struct subset {
int parent;
int rank;
};
int find(struct subset subsets[], int i)
{
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void Union(struct subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
int myComp(const void* a, const void* b)
{
struct Edge* a1 = (struct Edge*)a;
struct Edge* b1 = (struct Edge*)b;
return a1->weight > b1->weight;
}
void KruskalMST(struct Graph* graph)
{
int V = graph->V;
struct Edge result[V];
int e = 0;
int i = 0;
qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp);
struct subset* subsets = (struct subset*)malloc(V * sizeof(struct subset));
for (int v = 0; v < V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
while (e < V - 1 && i < graph->E) {
struct Edge next_edge = graph->edge[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
printf("Following are the edges in the constructed MST\n");
for (i = 0; i < e; ++i)
printf("%d -- %d == %d\n", result[i].src, result[i].dest,
result[i].weight);
return;
}
int main()
{
int V = 5;
int E = 9;
struct Graph* graph = createGraph(V, E);
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 2;
graph->edge[1].src = 0;
graph->edge[1].dest = 3;
graph->edge[1].weight = 6;
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 3;
graph->edge[3].src = 1;
graph->edge[3].dest = 4;
graph->edge[3].weight = 5;
graph->edge[4].src = 2;
graph->edge[4].dest = 4;
graph->edge[4].weight = 7;
graph->edge[5].src = 3;
graph->edge[5].dest = 4;
graph->edge[5].weight = 9;
KruskalMST(graph);
return 0;
}
```