用C语言编写普利姆算法邻接表的代码
时间: 2024-01-22 08:21:19 浏览: 120
邻接表建立(C语言)
5星 · 资源好评率100%
下面是用C语言编写普利姆算法邻接表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_VERTICES 100
typedef struct node {
int vertex;
int weight;
struct node* next;
} Node;
typedef struct graph {
Node* vertices[MAX_VERTICES];
int num_vertices;
} Graph;
typedef struct heap {
int* vertex_list;
int* weight_list;
int size;
int capacity;
} Heap;
Graph* createGraph(int num_vertices) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->num_vertices = num_vertices;
for (int i = 0; i < num_vertices; i++) {
graph->vertices[i] = NULL;
}
return graph;
}
void addEdge(Graph* graph, int src, int dest, int weight) {
Node* node = (Node*)malloc(sizeof(Node));
node->vertex = dest;
node->weight = weight;
node->next = graph->vertices[src];
graph->vertices[src] = node;
node = (Node*)malloc(sizeof(Node));
node->vertex = src;
node->weight = weight;
node->next = graph->vertices[dest];
graph->vertices[dest] = node;
}
Heap* createHeap(int capacity) {
Heap* heap = (Heap*)malloc(sizeof(Heap));
heap->vertex_list = (int*)malloc(sizeof(int) * capacity);
heap->weight_list = (int*)malloc(sizeof(int) * capacity);
heap->size = 0;
heap->capacity = capacity;
return heap;
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(Heap* heap, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < heap->size && heap->weight_list[left] < heap->weight_list[smallest]) {
smallest = left;
}
if (right < heap->size && heap->weight_list[right] < heap->weight_list[smallest]) {
smallest = right;
}
if (smallest != i) {
swap(&heap->vertex_list[smallest], &heap->vertex_list[i]);
swap(&heap->weight_list[smallest], &heap->weight_list[i]);
heapify(heap, smallest);
}
}
void insert(Heap* heap, int vertex, int weight) {
if (heap->size == heap->capacity) {
return;
}
heap->vertex_list[heap->size] = vertex;
heap->weight_list[heap->size] = weight;
int i = heap->size;
while (i > 0 && heap->weight_list[i] < heap->weight_list[(i - 1) / 2]) {
swap(&heap->vertex_list[i], &heap->vertex_list[(i - 1) / 2]);
swap(&heap->weight_list[i], &heap->weight_list[(i - 1) / 2]);
i = (i - 1) / 2;
}
heap->size++;
}
int extractMin(Heap* heap) {
if (heap->size == 0) {
return -1;
}
int min_vertex = heap->vertex_list[0];
heap->vertex_list[0] = heap->vertex_list[heap->size - 1];
heap->weight_list[0] = heap->weight_list[heap->size - 1];
heap->size--;
heapify(heap, 0);
return min_vertex;
}
int isInHeap(Heap* heap, int vertex) {
for (int i = 0; i < heap->size; i++) {
if (heap->vertex_list[i] == vertex) {
return 1;
}
}
return 0;
}
int prim(Graph* graph) {
int key[MAX_VERTICES];
int inMST[MAX_VERTICES];
int parent[MAX_VERTICES];
Heap* heap = createHeap(graph->num_vertices);
for (int i = 0; i < graph->num_vertices; i++) {
key[i] = INT_MAX;
inMST[i] = 0;
parent[i] = -1;
}
insert(heap, 0, 0);
key[0] = 0;
while (heap->size > 0) {
int u = extractMin(heap);
inMST[u] = 1;
Node* node = graph->vertices[u];
while (node != NULL) {
int v = node->vertex;
int weight = node->weight;
if (!inMST[v] && weight < key[v]) {
key[v] = weight;
parent[v] = u;
if (isInHeap(heap, v)) {
for (int i = 0; i < heap->size; i++) {
if (heap->vertex_list[i] == v) {
heap->weight_list[i] = weight;
break;
}
}
} else {
insert(heap, v, weight);
}
}
node = node->next;
}
}
int total_weight = 0;
for (int i = 1; i < graph->num_vertices; i++) {
total_weight += key[i];
}
return total_weight;
}
int main() {
Graph* graph = createGraph(5);
addEdge(graph, 0, 1, 2);
addEdge(graph, 0, 3, 6);
addEdge(graph, 1, 2, 3);
addEdge(graph, 1, 3, 8);
addEdge(graph, 1, 4, 5);
addEdge(graph, 2, 4, 7);
addEdge(graph, 3, 4, 9);
int total_weight = prim(graph);
printf("Minimum Spanning Tree Weight: %d\n", total_weight);
return 0;
}
```
在上述代码中,我们首先定义了 `Node` 结构体和 `Graph` 结构体,用于表示图的邻接表表示法。然后,我们定义了 `Heap` 结构体,用于实现最小堆。接下来,我们实现了 `createGraph` 和 `addEdge` 函数,用于创建图和添加边。然后,我们实现了 `createHeap` 函数和堆操作函数,包括 `swap`、`heapify`、`insert`、`extractMin` 和 `isInHeap`。最后,我们实现了主函数 `prim`,用于执行普利姆算法,并返回最小生成树的权重。在主函数中,我们首先初始化 `key`、`inMST` 和 `parent` 数组,然后将起始顶点加入堆中。接下来,我们在堆中取出权重最小的顶点,并将其标记为已访问。然后,我们遍历与该顶点相邻的所有顶点,并更新它们的权重和父节点。如果该顶点不在堆中,则将其加入堆中;否则,更新其权重。最后,我们计算最小生成树的权重并返回。
以上代码仅供参考,实际使用时可能需要进行一定的修改。
阅读全文