C语言用普里姆算法求最小生成树
时间: 2024-01-13 18:18:57 浏览: 85
普里姆算法是一种用于求解连通网的最小生成树的贪心算法。下面是用C语言实现普里姆算法求最小生成树的示例代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define MAX_VERTICES 100
#define INF 9999
int graph[MAX_VERTICES][MAX_VERTICES];
int parent[MAX_VERTICES];
int key[MAX_VERTICES];
bool mstSet[MAX_VERTICES];
int minKey(int key[], bool mstSet[], int vertices) {
int min = INF, min_index;
for (int v = 0; v < vertices; v++) {
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
}
return min_index;
}
void printMST(int parent[], int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {
printf("Edge \tWeight\n");
for (int i = 1; i < vertices; i++) {
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
}
void primMST(int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {
for (int i = 0; i < vertices; i++) {
key[i] = INF;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < vertices - 1; count++) {
int u = minKey(key, mstSet, vertices);
mstSet[u] = true;
for (int v = 0; v < vertices; v++) {
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
printMST(parent, graph, vertices);
}
int main() {
int vertices;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
printf("Enter the adjacency matrix:\n");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph[i][j]);
}
}
primMST(graph, vertices);
return 0;
}
```
阅读全文