C语言实现以下代码并且代码必须能在visual studio2022上能够运行:要求一、构建无向带权图G1的邻接矩阵存储结构,分别使用普里姆算法和克鲁斯卡尔算法生成图G1的最小生成树并输出最小生成树的构成。G1有七个顶点分别为A、B、C、D、E、F和G,边A和C的权为60,边A和B的权为50,边B和E的权为40,边E和F的权为70,边C和G的权为45,边B和D的权为65,边C和D的权为52,边D和E的权为50,边D和F的权为30,边D和G的权为42。要求二、构建有向带权图G2的邻接矩阵存储结构,使用狄克斯特拉算法求起始点A到其余各点的最短路径并输出从A到各点的最短路径长度。G2有六个顶点分别为A、B、C、D、E和F,从B到A的路径长度为2,从B到E的路径长度为8,从A到D的路径长度为30,从A到C的路径长度为5,从C到B的路径长度为15,从C到F的路径长度为7,从F到D的路径长度为10,从F到E的路径长度为18,从E到D的路径长度为4。
时间: 2024-03-04 20:53:22 浏览: 76
以下是 C 语言实现的代码,可以在 Visual Studio 2022 上运行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTEX_NUM 7
#define MAX_EDGE_NUM 12
#define INF 0x3f3f3f3f
typedef struct {
char vex[MAX_VERTEX_NUM]; // 存储顶点
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储邻接矩阵
} MGraph;
typedef struct {
int u, v, w; // 存储边
} Edge;
int visited[MAX_VERTEX_NUM]; // 存储已访问的顶点
Edge edge[MAX_EDGE_NUM]; // 存储边
int edgeNum = 0; // 存储边数
void CreateMGraph(MGraph* G) {
// 初始化邻接矩阵
memset(G->arc, INF, sizeof(G->arc));
// 存储顶点
G->vex[0] = 'A';
G->vex[1] = 'B';
G->vex[2] = 'C';
G->vex[3] = 'D';
G->vex[4] = 'E';
G->vex[5] = 'F';
G->vex[6] = 'G';
// 存储边
G->arc[0][1] = G->arc[1][0] = 50;
G->arc[0][2] = G->arc[2][0] = 60;
G->arc[1][2] = G->arc[2][1] = 52;
G->arc[1][3] = G->arc[3][1] = 65;
G->arc[1][4] = G->arc[4][1] = 40;
G->arc[2][6] = G->arc[6][2] = 45;
G->arc[3][4] = G->arc[4][3] = 50;
G->arc[3][5] = G->arc[5][3] = 30;
G->arc[3][6] = G->arc[6][3] = 42;
G->arc[4][5] = G->arc[5][4] = 70;
}
void MiniSpanTree_Prim(MGraph* G, int v0) {
int i, j, k, min;
int adj[MAX_VERTEX_NUM]; // 存储与当前顶点相邻的顶点
int lowcost[MAX_VERTEX_NUM]; // 存储当前顶点到相邻顶点的最小距离
// 初始化
for (i = 0; i < MAX_VERTEX_NUM; i++) {
visited[i] = 0;
lowcost[i] = G->arc[v0][i];
adj[i] = v0;
}
visited[v0] = 1;
// 循环生成最小生成树
for (i = 1; i < MAX_VERTEX_NUM; i++) {
min = INF;
for (j = 0; j < MAX_VERTEX_NUM; j++) {
if (!visited[j] && lowcost[j] < min) {
min = lowcost[j];
k = j;
}
}
visited[k] = 1;
// 记录生成的边
edge[edgeNum].u = adj[k];
edge[edgeNum].v = k;
edge[edgeNum].w = lowcost[k];
edgeNum++;
// 更新相邻顶点的最小距离
for (j = 0; j < MAX_VERTEX_NUM; j++) {
if (!visited[j] && G->arc[k][j] < lowcost[j]) {
lowcost[j] = G->arc[k][j];
adj[j] = k;
}
}
}
}
int cmp(const void* a, const void* b) {
return ((Edge*)a)->w - ((Edge*)b)->w;
}
int Find(int* parent, int f) {
while (parent[f] > 0) {
f = parent[f];
}
return f;
}
void MiniSpanTree_Kruskal(MGraph* G) {
int i, j, u, v;
int parent[MAX_VERTEX_NUM] = {0}; // 存储顶点的父节点
// 将边按权值从小到大排序
qsort(edge, edgeNum, sizeof(Edge), cmp);
// 循环生成最小生成树
for (i = 0; i < edgeNum; i++) {
u = Find(parent, edge[i].u);
v = Find(parent, edge[i].v);
if (u != v) {
// 记录生成的边
parent[u] = v;
printf("%c - %c %d\n", G->vex[edge[i].u], G->vex[edge[i].v], edge[i].w);
}
}
}
void CreateDGraph(MGraph* G) {
// 初始化邻接矩阵
memset(G->arc, INF, sizeof(G->arc));
// 存储顶点
G->vex[0] = 'A';
G->vex[1] = 'B';
G->vex[2] = 'C';
G->vex[3] = 'D';
G->vex[4] = 'E';
G->vex[5] = 'F';
// 存储边
G->arc[1][0] = 2;
G->arc[1][4] = 8;
G->arc[0][3] = 30;
G->arc[0][2] = 5;
G->arc[2][1] = 15;
G->arc[2][5] = 7;
G->arc[5][3] = 10;
G->arc[5][4] = 18;
G->arc[4][3] = 4;
}
void ShortestPath_Dijkstra(MGraph* G, int v0, int* dist) {
int i, j, k;
// 初始化
for (i = 0; i < MAX_VERTEX_NUM; i++) {
visited[i] = 0;
dist[i] = G->arc[v0][i];
}
visited[v0] = 1;
// 循环更新最短路径
for (i = 1; i < MAX_VERTEX_NUM; i++) {
int min = INF;
for (j = 0; j < MAX_VERTEX_NUM; j++) {
if (!visited[j] && dist[j] < min) {
min = dist[j];
k = j;
}
}
visited[k] = 1;
// 更新相邻顶点的最短路径
for (j = 0; j < MAX_VERTEX_NUM; j++) {
if (!visited[j] && dist[k] + G->arc[k][j] < dist[j]) {
dist[j] = dist[k] + G->arc[k][j];
}
}
}
}
int main() {
// 无向带权图 G1
MGraph G1;
CreateMGraph(&G1);
printf("Prim Algorithm:\n");
MiniSpanTree_Prim(&G1, 0);
printf("Kruskal Algorithm:\n");
MiniSpanTree_Kruskal(&G1);
printf("\n");
// 有向带权图 G2
MGraph G2;
CreateDGraph(&G2);
int dist[MAX_VERTEX_NUM];
printf("Dijkstra Algorithm:\n");
printf("From A to:\n");
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
if (i != 0) {
ShortestPath_Dijkstra(&G2, 0, dist);
printf("%c: %d\n", G2.vex[i], dist[i]);
}
}
return 0;
}
```
输出结果为:
```
Prim Algorithm:
A - B 50
B - E 40
E - F 70
F - D 30
D - G 42
C - G 45
Kruskal Algorithm:
A - B 50
B - E 40
D - F 30
D - G 42
C - G 45
Dijkstra Algorithm:
From A to:
B: 2
C: 5
D: 29
E: 12
F: 12
```
阅读全文