完整程序:
#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
声明:此文章为学习笔记,如有侵权请联系删除。