用c语言建立一个以邻接矩阵形式存储的图; 2、以Prim算法获得最小生成树; 3、以Kruscal算法获得最小生成树; 4、退出系统。
时间: 2023-06-13 18:05:12 浏览: 176
以下是以邻接矩阵形式存储图,以Prim算法和Kruskal算法获取最小生成树的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_VERTICES 100
#define INF INT_MAX
typedef struct {
int weight;
int visited;
} Edge;
typedef struct {
Edge edges[MAX_VERTICES][MAX_VERTICES];
int num_vertices;
int num_edges;
} Graph;
typedef struct {
int from;
int to;
int weight;
} EdgeInfo;
Graph graph;
void init_graph(int num_vertices) {
graph.num_vertices = num_vertices;
graph.num_edges = 0;
for (int i = 0; i < num_vertices; i++) {
for (int j = 0; j < num_vertices; j++) {
graph.edges[i][j].weight = INF;
graph.edges[i][j].visited = 0;
}
}
}
void add_edge(int u, int v, int weight) {
graph.edges[u][v].weight = weight;
graph.edges[v][u].weight = weight;
graph.num_edges++;
}
void remove_edge(int u, int v) {
graph.edges[u][v].weight = INF;
graph.edges[v][u].weight = INF;
graph.num_edges--;
}
int get_next_vertex(int visited[]) {
for (int i = 0; i < graph.num_vertices; i++) {
if (!visited[i]) {
return i;
}
}
return -1;
}
void prim() {
int visited[MAX_VERTICES] = {0};
visited[0] = 1;
int edges_selected = 0;
while (edges_selected < graph.num_vertices - 1) {
int min_weight = INF;
int from, to;
for (int i = 0; i < graph.num_vertices; i++) {
if (visited[i]) {
for (int j = 0; j < graph.num_vertices; j++) {
if (!visited[j] && graph.edges[i][j].weight < min_weight) {
min_weight = graph.edges[i][j].weight;
from = i;
to = j;
}
}
}
}
printf("(%d, %d) %d\n", from, to, min_weight);
visited[to] = 1;
edges_selected++;
}
}
int find(int parent[], int i) {
if (parent[i] == -1) {
return i;
}
return find(parent, parent[i]);
}
void union_vertices(int parent[], int x, int y) {
int xroot = find(parent, x);
int yroot = find(parent, y);
parent[xroot] = yroot;
}
int compare_edge(const void* a, const void* b) {
EdgeInfo* e1 = (EdgeInfo*)a;
EdgeInfo* e2 = (EdgeInfo*)b;
return e1->weight - e2->weight;
}
void kruskal() {
EdgeInfo edges[graph.num_edges];
int index = 0;
for (int i = 0; i < graph.num_vertices; i++) {
for (int j = i + 1; j < graph.num_vertices; j++) {
if (graph.edges[i][j].weight != INF) {
edges[index].from = i;
edges[index].to = j;
edges[index].weight = graph.edges[i][j].weight;
index++;
}
}
}
qsort(edges, graph.num_edges, sizeof(EdgeInfo), compare_edge);
int parent[MAX_VERTICES];
for (int i = 0; i < graph.num_vertices; i++) {
parent[i] = -1;
}
int edges_selected = 0;
int i = 0;
while (edges_selected < graph.num_vertices - 1) {
EdgeInfo next_edge = edges[i++];
int x = find(parent, next_edge.from);
int y = find(parent, next_edge.to);
if (x != y) {
printf("(%d, %d) %d\n", next_edge.from, next_edge.to, next_edge.weight);
union_vertices(parent, x, y);
edges_selected++;
}
}
}
int main() {
init_graph(7);
add_edge(0, 1, 5);
add_edge(0, 2, 7);
add_edge(0, 3, 12);
add_edge(1, 2, 9);
add_edge(1, 4, 7);
add_edge(2, 3, 4);
add_edge(2, 4, 4);
add_edge(2, 5, 3);
add_edge(3, 5, 7);
add_edge(4, 5, 2);
add_edge(4, 6, 5);
add_edge(5, 6, 2);
printf("Prim's algorithm:\n");
prim();
printf("Kruskal's algorithm:\n");
kruskal();
return 0;
}
```
在这个例子中,我们创建了一个具有7个节点的图。然后我们使用`add_edge`函数添加边。最后,我们分别使用Prim算法和Kruskal算法来获取最小生成树,并将结果打印到控制台上。
阅读全文