最小生成树算法

本文介绍了Prims算法和Kruskals算法,两种在图论中用于寻找最小生成树(MST)的方法,分别通过邻接矩阵表示图,使用优先队列和并查集数据结构实现。文章详细展示了如何在给定的无向图中找到连接所有顶点的边,使得总权重最小。
摘要由CSDN通过智能技术生成

1.prim算法

#include <stdio.h>
#include <limits.h>

#define V 5 // 图中顶点的数量

// 从尚未包含在MST中的顶点集合中找到具有最小键值的顶点的函数
int minKey(int key[], int mstSet[]) {
    int min = INT_MAX, min_index;
    for (int v = 0; v < V; v++)
        if (mstSet[v] == 0 && key[v] < min)
            min = key[v], min_index = v;
    return min_index;
}

// 打印存储在parent[]中的构造MST的函数
void printMST(int parent[], int graph[V][V]) {
    printf("边 \t权重\n");
    for (int i = 1; i < V; i++)
        printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}

// 用邻接矩阵表示的图构造并打印MST的函数
void primMST(int graph[V][V]) {
    int parent[V]; // 存储构造的MST的数组
    int key[V];    // 用于在切割中选择最小权重边的键值
    int mstSet[V]; // 表示包含在MST中的顶点集合

    // 将所有键值初始化为INFINITE
    for (int i = 0; i < V; i++)
        key[i] = INT_MAX, mstSet[i] = 0;

    // 总是将第一个顶点包含在MST中。
    // 使第一个键为0,以便选择此顶点作为第一个顶点。
    key[0] = 0;     // 将第一个键设为0,以便选择此顶点作为第一个顶点
    parent[0] = -1; // 第一个节点始终是MST的根

    // MST将具有V个顶点
    for (int count = 0; count < V - 1; count++) {
        // 从尚未包含在MST中的顶点集合中选择具有最小键值的顶点
        int u = minKey(key, mstSet);

        // 将选定的顶点添加到MST集合中
        mstSet[u] = 1;

        // 更新选定顶点的相邻顶点的键值和父索引
        // 仅考虑尚未包含在MST中的那些顶点
        for (int v = 0; v < V; v++)
            // graph[u][v]仅对m的相邻顶点非零
            // mstSet[v]对于尚未包含在MST中的顶点为false
            // 仅当graph[u][v]小于key[v]时才更新键值
            if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])
                parent[v] = u, key[v] = graph[u][v];
    }

    // 打印构造的MST
    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.kruskal算法

#include <stdio.h>
#include <stdlib.h>

// 定义边的数据结构
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;
}

// 用于快速排序的比较函数
int compare(const void* a, const void* b) {
    struct Edge* aEdge = (struct Edge*)a;
    struct Edge* bEdge = (struct Edge*)b;
    return aEdge->weight - bEdge->weight;
}

// 查找集合的根节点
int find(int parent[], int i) {
    if (parent[i] == -1)
        return i;
    return find(parent, parent[i]);
}

// 将两个集合进行合并
void Union(int parent[], int x, int y) {
    int xroot = find(parent, x);
    int yroot = find(parent, y);
    parent[xroot] = yroot;
}

// Kruskal算法的主要函数
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]), compare);

    // 创建用于检测循环的子集
    int* parent = (int*)malloc(V * sizeof(int));
    for (int v = 0; v < V; v++)
        parent[v] = -1;

    // 在最小生成树中包含V-1条边
    while (e < V - 1 && i < graph->E) {
        // 从排序后的边数组中取出下一条边
        struct Edge next_edge = graph->edge[i++];

        // 检查加入该边是否会导致循环
        int x = find(parent, next_edge.src);
        int y = find(parent, next_edge.dest);

        if (x != y) {
            // 如果不会导致循环,则加入结果数组中
            result[e++] = next_edge;
            Union(parent, x, y);
        }
    }

    // 打印最小生成树
    printf("Edge \tWeight\n");
    for (i = 0; i < e; ++i)
        printf("%d - %d \t%d\n", result[i].src, result[i].dest, result[i].weight);

    free(parent);
}

int main() {
    int V = 4;  // 图中顶点的数量
    int E = 5;  // 图中边的数量

    // 创建图
    struct Graph* graph = createGraph(V, E);

    // 添加边
    // 注意:这里假设图是无向的,因此每条边都应该添加两次
    graph->edge[0].src = 0;
    graph->edge[0].dest = 1;
    graph->edge[0].weight = 10;

    graph->edge[1].src = 0;
    graph->edge[1].dest = 2;
    graph->edge[1].weight = 6;

    graph->edge[2].src = 0;
    graph->edge[2].dest = 3;
    graph->edge[2].weight = 5;

    graph->edge[3].src = 1;
    graph->edge[3].dest = 3;
    graph->edge[3].weight = 15;

    graph->edge[4].src = 2;
    graph->edge[4].dest = 3;
    graph->edge[4].weight = 4;

    // 计算最小生成树
    KruskalMST(graph);

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值