【c语言】Prim算法与Kruskal算法的比较与实现,寻找最小支撑树(生成树)

本文介绍了如何使用C语言编程实现Prim算法和Kruskal算法来找到无向图的最小生成树,通过邻接矩阵表示图,重点展示了Prim算法的cocycle方法实现,并对比了两种算法的应用范围。
摘要由CSDN通过智能技术生成
  • 实验目的

Learn and understand the Prim algorithm, Kruskal algorithm through programming, and implement the prim algorithm by using the cocycle method. Compare and distinguish the application scope of different algorithms in learning

  • 实验内容

Prim algorithm, Kruskal algorithm are implemented in the computer by programming in C-language to find the Minimum spanning tree. The prim algorithm tries to use the cocycle method.

  • 使用环境

Win 11 & visual studio code 2022

  • 算法介绍

  • 调试过程

  1. 调试代码

The program takes the file as the input, and the file content is

Code:

#include <stdio.h>
#include <stdlib.h>
#define V 4 // V是图的顶点数
struct graph
{
    char vert1;
    char vert2;
    int weight;
};
typedef struct graph graph;
graph edge[10];
int n = 0;
int MOG[10][10];
graph minitree[V - 1]; // 最小树的边是顶点数减一

void matrify(int m) // 转换为矩阵
{
    int i, x, y;
    for (i = 0; i < m; i++)
    {
        x = edge[i].vert1 - 65;
        y = edge[i].vert2 - 65;
        MOG[x][y] = MOG[y][x] = edge[i].weight;
    }
}
void showM()
{
    int i, j;
    for (i = 0; i < V; i++) // 打印权重矩阵
    {
        for (j = 0; j < V; j++)
        {
            printf("%d ", MOG[i][j]);
        }
        printf("\n");
    }
}
void PRIM()
{
    int i, j, minw, temp;
    int a, b, s = 0, M = V - 2;
    while (M > 0)
    {
        minw = MOG[0][1];
        for (i = 0; i < V - 1; i++) // 找最小权重
        {
            for (j = i + 1; j < V; j++) // 遍历一半的矩阵即可
            {
                temp = MOG[i][j];
                if (temp < minw && temp != 0)
                {
                    minw = temp;
                    a = i;
                    b = j;
                }
            }
        }
        minitree[s].vert1 = (a + 65);
        minitree[s].vert2 = (b + 65);
        minitree[s].weight = minw;
        s++;
        for (i = 0; i < V; i++) // 合并行
        {
            if (MOG[a][i] != 0 && MOG[b][i] != 0)
                MOG[a][i] = (MOG[a][i] < MOG[b][i]) ? MOG[a][i] : MOG[b][i];
            else
                MOG[a][i] = MOG[a][i] + MOG[b][i];
        }
        for (j = 0; j < V; j++) // 合并列
        {
            if (MOG[j][a] != 0 && MOG[j][b] != 0)
                MOG[j][a] = (MOG[j][a] < MOG[j][b]) ? MOG[j][a] : MOG[j][b];
            else
                MOG[j][a] = MOG[j][a] + MOG[j][b];
        }
        for (i = 0; i < V; i++)
        {
            MOG[b][i] = MOG[i][b] = 0;
            MOG[i][i] = 0; // 对角线为0
        }
        M--;
        printf("Matrix of weights:\n");
        showM(); // 展示每一步
    }
    matrify(n);
    for (i = 0; i < (V - 2); i++)
    {
        a = minitree[i].vert1 - 65;
        b = minitree[i].vert2 - 65;
        MOG[a][b] = MOG[b][a] = 0;
    }
    minw = MOG[0][1];
    for (i = 0; i < V - 1; i++)
    {
        for (j = i + 1; j < V; j++)
        {
            temp = MOG[i][j];
            if (temp < minw && temp != 0)
            {
                minw = temp;
                a = i;
                b = j;
            }
        }
    }
    minitree[s].vert1 = (a + 65);
    minitree[s].vert2 = (b + 65);
    minitree[s].weight = minw;
}

int main()
{
    int i, j;
    FILE *fp;
    fp = fopen("D:/work code/C Storageroom/discrete/graph01.txt", "r"); // 只读的方式打开文件
    if (fp == NULL)                                                     // 判断文件是否正常打开
    {
        printf("can not open the file\n");
        exit(0);
    }
    while (!feof(fp)) // 从文件中读入数据
    {
        edge[n].vert1 = fgetc(fp);
        edge[n].vert2 = fgetc(fp);
        fscanf(fp, "%d", &edge[n].weight);
        n++;
    }
    fclose(fp);             // 关闭文件
    for (i = 0; i < n; i++) // 打印数据
        printf("%c %c %d\n", edge[i].vert1, edge[i].vert2, edge[i].weight);
    matrify(n);
    printf("the matrix of weights\n");
    showM();
    PRIM();
    printf("output:\n");
    for (i = 0; i < (V - 1); i++) // 打印数据
        printf("%c %c %d\n", minitree[i].vert1, minitree[i].vert2, minitree[i].weight);
    return 0;
}

运行结果

  • 总结

The spanning tree of an undirected graph is to select some edges from the edge set of the graph, so that these edges form a connected acyclic graph, that is, a tree. If a weight is added to each edge, the spanning tree with the smallest sum of weights in all spanning trees is called the minimum spanning tree. Prim algorithm and Kruskal algorithm are algorithms that use MST properties to construct the minimum spanning tree. The experimental graph is stored in the form of adjacency matrix. Sometimes the results of Prim algorithm are not unique.

  • 参考文献

[1]谭浩强,C 程序设计[M] (第四版).北京:清华大学出版社,2010年6月(中国高等院校计算机基础教育课程体系规划教材)

[2]谭浩强, C 程序设计( 第四版 )学习辅导 ,北京:清华大学出版社,2010年7月(中国高等院校计算机基础教育课程体系规划教材)

[3]C Primer Plus (第6版)中文版,Stephen Prata 著;姜佑译 ——北京 :人名邮电出版社,2019.11

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值