最小生成树---krusakl(克鲁斯卡尔算法)

该程序实现了图的深度优先搜索(DFS)和克鲁斯卡尔算法,用于查找无向图的最小生成树。首先通过DFS遍历图的所有顶点,然后使用冒泡排序按权重对边进行排序,最后应用克鲁斯卡尔算法展示最小生成树的边及权重。代码在B站TyrantLucifer的分享中给出。
摘要由CSDN通过智能技术生成

完整程序:

#include <windows.h>
#include <stdio.h>

typedef struct Graph
{
    char *vexs;     //  顶点元素
    int **arcs;     //  邻接表
    int vexNum;     //  顶点数量
    int arcsNum;    //  边数量
}Graph;

typedef struct Edge
{
    int start;
    int end;
    int weight;    
}Edge;

#define MAX 32767

Graph *initgraph(int vexNum);
void creat_graph(Graph *graph,char *vexs,int *arcs);
void DFS(Graph *graph,int *flag,int index);

Edge *Init_Edge(Graph *graph);
void Sort(Edge *edge,Graph *graph);
void Kruskal(Graph *graph);

void main()
{

    Graph *graph = initgraph(6);
    int *flag = (int *)malloc(sizeof(int) * graph->vexNum);
    for(int i = 0;i < graph->vexNum;i++)
    {
        flag[i] = 0;
    }
    int arcs[6][6] = {
        0,6,1,5,MAX,MAX,
        6,0,5,MAX,3,MAX,
        1,5,0,5,6,4,
        5,MAX,5,0,MAX,2,
        MAX,3,6,MAX,0,6,
        MAX,MAX,4,2,6,0,
    };

    creat_graph(graph,"123456",(int *)arcs);
    printf("DFS遍历结果:\n");
    DFS(graph,flag,0);
    printf("\n"); 
    printf("克鲁斯卡尔算法:\n");
    Kruskal(graph);

    system("pause");
    return ;
}

Graph *initgraph(int vexNum)
{
    Graph *graph = (Graph *)malloc(sizeof(Graph));
    graph->vexs = (char *)malloc(sizeof(char) * vexNum);    //为顶点元素申请内存空间
    graph->arcs = (int **)malloc(sizeof(int *) * vexNum);   //指针数组(存储一维数组首地址)
    for(int i = 0;i < vexNum;i++)
    {
        graph->arcs[i] = (int *)malloc(sizeof(int) * vexNum);   //为一维数组申请内存空间
    }    
    graph->vexNum = vexNum;     //初始化顶点数量
    graph->arcsNum = 0;         //初始化边数量
    return graph;
}

void creat_graph(Graph *graph,char *vexs,int *arcs)
{
    for(int i = 0;i < graph->vexNum;i++)
    {
        graph->vexs[i] = vexs[i];       //为顶点赋值
        for(int j = 0; j < graph->vexNum;j++)
        {
            graph->arcs[i][j] = *(arcs + i * graph->vexNum + j) ;      //创建邻接表
            if(graph->arcs[i][j] != 0 && graph->arcs[i][j] != MAX)     //二维数组不等于0且不等于最大值时存在边,边数加 1
                graph->arcsNum ++;
        }
    }

    graph->arcsNum /= 2;    //无向图两顶点之间是没有方向的,
}

void DFS(Graph *graph,int *flag,int index)
{
    //打印结点内容
    printf("%c\t",graph->vexs[index]);
    //标记已访问
    flag[index] = 1;
    //遍历二维数组
    for(int i = 0;i < graph->vexNum;i++)
    {   //如果未访问过且存在边的关系则访问结点
        if(graph->arcs[index][i] > 0 && graph->arcs[index][i] != MAX && !flag[i])
        {
            DFS(graph,flag,i);
        }
    }
}
//将存在边关系的元素的开始、结束下标和两顶点之间的权重保存
Edge *Init_Edge(Graph *graph)
{
    int index = 0;
    Edge *edge = (Edge *)malloc(sizeof(Edge) * graph->arcsNum);

    for(int i = 0;i < graph->vexNum;i++)
    {
        for(int j = i + 1;j < graph->vexNum;j++)
        {
            if(graph->arcs[i][j] != MAX)
            {
                edge[index].start = i;
                edge[index].end = j;
                edge[index].weight = graph->arcs[i][j];
                index ++;
            }
        }
    }
    return edge;
}

void Sort(Edge *edge,Graph *graph)
{
    //使用冒泡排序,按照权重从小到大排序
    for(int i =0;i < graph->arcsNum - 1;i++)
    {
        for(int j = 0;j < graph->arcsNum - i - 1;j++)
        {
            if(edge[j].weight > edge[j + 1].weight)
            {
                Edge tmp = edge[j];
                edge[j] = edge[j + 1];
                edge[j +1] = tmp;
            }
        }    
    }
}

void Kruskal(Graph *graph)
{   
    int *connect = (int *)malloc(sizeof(int) * graph->vexNum);
    for(int i = 0;i < graph->vexNum;i++)
    {
        connect[i] = i;
    }

    Edge *edge = Init_Edge(graph);
    Sort(edge,graph);

    for(int i = 0;i < graph->arcsNum;i++)
    {
        int start = connect[edge[i].start];
        int end = connect[edge[i].end];
        if(start != end)
        {
            printf("v%c --> v%c weight = %d\n",graph->vexs[edge[i].start],graph->vexs[edge[i].end],edge[i].weight);
            for(int j = 0;j < graph->vexNum;j++)
            {
                if(connect[j] == end)
                {
                    connect[j] = start;
                }
            }
        }
    }
}

测试结果:

在这里插入图片描述
在这里插入图片描述
出处:B站—TyrantLucifer
声明:此文章为学习笔记,如有侵权请联系删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Byte_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值