请编写一个可运行的c语言程序,满足下面条件1.建立所给无向带权图的邻接矩阵存储并输出该矩阵。 2.建立所给无向图的邻接表,并输出该图的深度优先遍历和广度优先遍历结果。 3.以无向网表示 n 个城市之间通表示该线路的造价,设计一个方案,使这个通信网的总造价最低。
时间: 2024-02-16 19:02:46 浏览: 79
建立图的邻接矩阵或邻接表存储并在此基础上实现图的深度优先遍历和广度优先遍历.doc
5星 · 资源好评率100%
好的,以下是满足您要求的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_VERTEX_NUM 20
#define INF 65535
// 邻接矩阵
typedef struct {
int vertex[MAX_VERTEX_NUM]; // 存储顶点信息
int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储边权信息
int vertex_num; // 顶点数
int edge_num; // 边数
} MGraph;
// 邻接表
typedef struct ArcNode {
int adjvex; // 邻接点在顶点数组中的下标
int weight; // 边权值
struct ArcNode *nextarc; // 指向下一个邻接点的指针
} ArcNode;
typedef struct VNode {
int data; // 存储顶点信息
ArcNode *firstarc; // 指向第一个邻接点的指针
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList adjlist; // 存储邻接表
int vertex_num; // 顶点数
int edge_num; // 边数
} ALGraph;
// 栈和队列
typedef struct {
int data[MAX_VERTEX_NUM];
int top;
} Stack;
typedef struct {
int data[MAX_VERTEX_NUM];
int front;
int rear;
} Queue;
// 创建邻接矩阵
void createMGraph(MGraph *G) {
int i, j;
// 输入顶点数和边数
printf("请输入顶点数和边数:");
scanf("%d %d", &G->vertex_num, &G->edge_num);
// 输入顶点信息
printf("请输入顶点信息:");
for (i = 0; i < G->vertex_num; i++) {
scanf("%d", &G->vertex[i]);
}
// 初始化邻接矩阵
for (i = 0; i < G->vertex_num; i++) {
for (j = 0; j < G->vertex_num; j++) {
G->edge[i][j] = INF;
}
}
// 输入边权信息
printf("请输入边权信息:\n");
for (i = 0; i < G->edge_num; i++) {
int v1, v2, weight;
printf("请输入第%d条边(起点 终点 权值):", i + 1);
scanf("%d %d %d", &v1, &v2, &weight);
G->edge[v1][v2] = weight;
G->edge[v2][v1] = weight;
}
}
// 输出邻接矩阵
void printMGraph(MGraph G) {
int i, j;
printf("邻接矩阵:\n");
for (i = 0; i < G.vertex_num; i++) {
for (j = 0; j < G.vertex_num; j++) {
if (G.edge[i][j] == INF) {
printf("%-5s", "INF");
} else {
printf("%-5d", G.edge[i][j]);
}
}
printf("\n");
}
}
// 创建邻接表
void createALGraph(ALGraph *G) {
int i;
// 输入顶点数和边数
printf("请输入顶点数和边数:");
scanf("%d %d", &G->vertex_num, &G->edge_num);
// 输入顶点信息
printf("请输入顶点信息:");
for (i = 0; i < G->vertex_num; i++) {
scanf("%d", &G->adjlist[i].data);
G->adjlist[i].firstarc = NULL;
}
// 输入边信息
printf("请输入边信息:\n");
for (i = 0; i < G->edge_num; i++) {
int v1, v2, weight;
printf("请输入第%d条边(起点 终点 权值):", i + 1);
scanf("%d %d %d", &v1, &v2, &weight);
// 新建一个邻接点
ArcNode *node1 = (ArcNode *) malloc(sizeof(ArcNode));
node1->adjvex = v2;
node1->weight = weight;
node1->nextarc = G->adjlist[v1].firstarc;
G->adjlist[v1].firstarc = node1;
ArcNode *node2 = (ArcNode *) malloc(sizeof(ArcNode));
node2->adjvex = v1;
node2->weight = weight;
node2->nextarc = G->adjlist[v2].firstarc;
G->adjlist[v2].firstarc = node2;
}
}
// 深度优先遍历
void DFS(ALGraph G, int v, bool visited[]) {
visited[v] = true;
printf("%d ", G.adjlist[v].data);
ArcNode *p = G.adjlist[v].firstarc;
while (p != NULL) {
int w = p->adjvex;
if (!visited[w]) {
DFS(G, w, visited);
}
p = p->nextarc;
}
}
// 广度优先遍历
void BFS(ALGraph G, int v, bool visited[]) {
int queue[MAX_VERTEX_NUM];
int front = 0, rear = 0;
printf("%d ", G.adjlist[v].data);
visited[v] = true;
queue[rear++] = v;
while (front != rear) {
int w = queue[front++];
ArcNode *p = G.adjlist[w].firstarc;
while (p != NULL) {
int k = p->adjvex;
if (!visited[k]) {
printf("%d ", G.adjlist[k].data);
visited[k] = true;
queue[rear++] = k;
}
p = p->nextarc;
}
}
}
// 求最小生成树的Prim算法
void MiniSpanTree_Prim(MGraph G) {
int lowcost[MAX_VERTEX_NUM], closest[MAX_VERTEX_NUM], i, j, k;
// 初始化
for (i = 1; i < G.vertex_num; i++) {
lowcost[i] = G.edge[0][i];
closest[i] = 0;
}
for (i = 1; i < G.vertex_num; i++) {
int min = INF;
for (j = 1; j < G.vertex_num; j++) {
if (lowcost[j] < min && lowcost[j] != 0) {
min = lowcost[j];
k = j;
}
}
printf("(%d, %d) ", closest[k], k);
lowcost[k] = 0;
for (j = 1; j < G.vertex_num; j++) {
if (G.edge[k][j] < lowcost[j]) {
lowcost[j] = G.edge[k][j];
closest[j] = k;
}
}
}
}
int main() {
MGraph G1;
createMGraph(&G1);
printMGraph(G1);
ALGraph G2;
createALGraph(&G2);
bool visited[MAX_VERTEX_NUM] = {false};
printf("深度优先遍历:");
for (int i = 0; i < G2.vertex_num; i++) {
if (!visited[i]) {
DFS(G2, i, visited);
}
}
printf("\n");
for (int i = 0; i < G2.vertex_num; i++) {
visited[i] = false;
}
printf("广度优先遍历:");
for (int i = 0; i < G2.vertex_num; i++) {
if (!visited[i]) {
BFS(G2, i, visited);
}
}
printf("\n");
printf("最小生成树的边为:");
MiniSpanTree_Prim(G1);
printf("\n");
return 0;
}
```
说明:
- 该程序使用邻接矩阵和邻接表分别存储了给定的无向带权图,并且分别实现了输出邻接矩阵和输出深度优先遍历和广度优先遍历的功能。
- 最后还实现了求解最小生成树的Prim算法,用于求解n个城市之间通的通信网的最小造价方案。
阅读全文