C语言实现以下代码并且必须能在visual studio2022上能够运行:构建带权无向图G1的邻接矩阵存储结构,G别1有七个顶点分为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,分别使用普里姆算法和克鲁斯卡尔算法生成图G1的最小生成树。构建有向带权图G2的邻接矩阵存储结构,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,使用狄克斯特拉算法求起始点A到其余各点的最短路径。
时间: 2024-02-29 20:53:50 浏览: 69
以下是C语言实现构建带权无向图G1的邻接矩阵存储结构,并使用普里姆算法和克鲁斯卡尔算法生成图G1的最小生成树的代码:
```c
#include <stdio.h>
#include <limits.h>
#define MAX_VERTICES 7
int G1[MAX_VERTICES][MAX_VERTICES] = {
{0, 50, 60, 0, 0, 0, 0},
{50, 0, 0, 65, 40, 0, 0},
{60, 0, 0, 52, 0, 0, 45},
{0, 65, 52, 0, 50, 30, 42},
{0, 40, 0, 50, 0, 70, 0},
{0, 0, 0, 30, 70, 0, 0},
{0, 0, 45, 42, 0, 0, 0}
};
int prim() {
int selected[MAX_VERTICES] = {0};
int dist[MAX_VERTICES];
int parent[MAX_VERTICES];
int i, j, min_dist, u, v;
for (i = 0; i < MAX_VERTICES; i++) {
dist[i] = INT_MAX;
}
dist[0] = 0;
parent[0] = -1;
for (i = 0; i < MAX_VERTICES - 1; i++) {
min_dist = INT_MAX;
for (j = 0; j < MAX_VERTICES; j++) {
if (!selected[j] && dist[j] < min_dist) {
min_dist = dist[j];
u = j;
}
}
selected[u] = 1;
for (v = 0; v < MAX_VERTICES; v++) {
if (G1[u][v] && !selected[v] && G1[u][v] < dist[v]) {
parent[v] = u;
dist[v] = G1[u][v];
}
}
}
int cost = 0;
for (i = 1; i < MAX_VERTICES; i++) {
cost += G1[i][parent[i]];
}
return cost;
}
int find(int parent[], int i) {
if (parent[i] == -1) {
return i;
}
return find(parent, parent[i]);
}
void union_set(int parent[], int x, int y) {
int xset = find(parent, x);
int yset = find(parent, y);
parent[xset] = yset;
}
int kruskal() {
int parent[MAX_VERTICES];
int i, j, min_dist, u, v;
for (i = 0; i < MAX_VERTICES; i++) {
parent[i] = -1;
}
int edges = 0;
int cost = 0;
while (edges < MAX_VERTICES - 1) {
min_dist = INT_MAX;
for (i = 0; i < MAX_VERTICES; i++) {
for (j = 0; j < MAX_VERTICES; j++) {
if (G1[i][j] && find(parent, i) != find(parent, j) && G1[i][j] < min_dist) {
min_dist = G1[i][j];
u = i;
v = j;
}
}
}
union_set(parent, u, v);
cost += min_dist;
edges++;
}
return cost;
}
int main() {
printf("The minimum cost of G1 using Prim's algorithm: %d\n", prim());
printf("The minimum cost of G1 using Kruskal's algorithm: %d\n", kruskal());
return 0;
}
```
以下是C语言实现构建有向带权图G2的邻接矩阵存储结构,并使用狄克斯特拉算法求起始点A到其余各点的最短路径的代码:
```c
#include <stdio.h>
#include <limits.h>
#define MAX_VERTICES 6
int G2[MAX_VERTICES][MAX_VERTICES] = {
{0, 2, 5, 30, 0, 0},
{0, 0, 15, 0, 8, 0},
{0, 0, 0, 0, 0, 7},
{0, 0, 0, 0, 4, 10},
{0, 0, 0, 0, 0, 18},
{0, 0, 0, 0, 0, 0}
};
int dijkstra(int start) {
int dist[MAX_VERTICES];
int visited[MAX_VERTICES] = {0};
int i, j, min_dist, u;
for (i = 0; i < MAX_VERTICES; i++) {
dist[i] = INT_MAX;
}
dist[start] = 0;
for (i = 0; i < MAX_VERTICES - 1; i++) {
min_dist = INT_MAX;
for (j = 0; j < MAX_VERTICES; j++) {
if (!visited[j] && dist[j] < min_dist) {
min_dist = dist[j];
u = j;
}
}
visited[u] = 1;
for (j = 0; j < MAX_VERTICES; j++) {
if (G2[u][j] && !visited[j] && dist[u] != INT_MAX && dist[u] + G2[u][j] < dist[j]) {
dist[j] = dist[u] + G2[u][j];
}
}
}
for (i = 0; i < MAX_VERTICES; i++) {
if (dist[i] == INT_MAX) {
printf("There is no path from A to %c\n", 'A' + i);
} else {
printf("The shortest path from A to %c is %d\n", 'A' + i, dist[i]);
}
}
return 0;
}
int main() {
dijkstra(0);
return 0;
}
```
阅读全文