通过数据结构上机实验写关于图的深度优先和广度优先输出最小路径和全部路径的代码。请写出遇到的几个困难与如何解决的。使用的是c语言
时间: 2024-05-15 15:12:59 浏览: 65
下面是使用 C 语言实现图的深度优先和广度优先搜索并输出最小路径和全部路径的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100 // 最大顶点数
// 定义邻接矩阵结构体
typedef struct {
int vertex[MAX_VERTEX_NUM]; // 存储顶点信息
int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储边信息
int vertex_num; // 顶点数
int edge_num; // 边数
} MGraph;
// 定义队列结构体
typedef struct {
int data[MAX_VERTEX_NUM];
int head;
int tail;
} Queue;
// 初始化队列
void initQueue(Queue *queue) {
queue->head = 0;
queue->tail = 0;
}
// 入队操作
void enqueue(Queue *queue, int data) {
queue->data[queue->tail++] = data;
}
// 出队操作
int dequeue(Queue *queue) {
return queue->data[queue->head++];
}
// 判断队列是否为空
int isQueueEmpty(Queue *queue) {
return queue->head == queue->tail;
}
// 深度优先遍历
void dfs(MGraph *graph, int start, int end, int *visited, int *path, int path_len, int *min_path, int *min_path_len) {
visited[start] = 1;
path[path_len++] = start;
if (start == end) { // 找到一条路径
int i, sum = 0;
for (i = 0; i < path_len - 1; i++) {
sum += graph->edge[path[i]][path[i+1]]; // 计算路径长度
}
if (sum < *min_path) { // 更新最小路径和最小路径长度
*min_path = sum;
*min_path_len = path_len;
}
printf("Path:");
for (i = 0; i < path_len; i++) {
printf(" %d", path[i]); // 输出路径
}
printf("\n");
} else {
int i;
for (i = 0; i < graph->vertex_num; i++) {
if (graph->edge[start][i] != 0 && visited[i] == 0) { // 如果存在边且未访问过,则继续遍历
dfs(graph, i, end, visited, path, path_len, min_path, min_path_len);
}
}
}
visited[start] = 0; // 恢复状态
path_len--;
}
// 广度优先遍历
void bfs(MGraph *graph, int start, int end, int *visited, int *path, int *min_path, int *min_path_len) {
Queue queue;
initQueue(&queue);
visited[start] = 1;
enqueue(&queue, start);
while (!isQueueEmpty(&queue)) {
int vertex = dequeue(&queue);
path[*min_path_len++] = vertex;
if (vertex == end) { // 找到一条路径
int i, sum = 0;
for (i = 0; i < *min_path_len - 1; i++) {
sum += graph->edge[path[i]][path[i+1]]; // 计算路径长度
}
if (sum < *min_path) { // 更新最小路径和最小路径长度
*min_path = sum;
}
printf("Path:");
for (i = 0; i < *min_path_len; i++) {
printf(" %d", path[i]); // 输出路径
}
printf("\n");
} else {
int i;
for (i = 0; i < graph->vertex_num; i++) {
if (graph->edge[vertex][i] != 0 && visited[i] == 0) { // 如果存在边且未访问过,则入队
visited[i] = 1;
enqueue(&queue, i);
}
}
}
}
}
int main() {
MGraph graph;
int visited[MAX_VERTEX_NUM] = {0};
int path[MAX_VERTEX_NUM];
int min_path = 10000;
int min_path_len = 0;
int i, j, start, end;
// 读入图的信息
printf("Enter the number of vertexes and edges:\n");
scanf("%d %d", &graph.vertex_num, &graph.edge_num);
printf("Enter the information of each vertex:\n");
for (i = 0; i < graph.vertex_num; i++) {
scanf("%d", &graph.vertex[i]);
}
printf("Enter the information of each edge:\n");
for (i = 0; i < graph.vertex_num; i++) {
for (j = 0; j < graph.vertex_num; j++) {
graph.edge[i][j] = 0;
}
}
for (i = 0; i < graph.edge_num; i++) {
int v1, v2, weight;
scanf("%d %d %d", &v1, &v2, &weight);
graph.edge[v1][v2] = weight;
graph.edge[v2][v1] = weight;
}
printf("Enter the start and end vertex:\n");
scanf("%d %d", &start, &end);
// 深度优先遍历
printf("DFS:\n");
dfs(&graph, start, end, visited, path, 0, &min_path, &min_path_len);
printf("The minimum path length: %d\n", min_path);
// 广度优先遍历
printf("BFS:\n");
visited[MAX_VERTEX_NUM] = {0};
min_path = 10000;
min_path_len = 0;
bfs(&graph, start, end, visited, path, &min_path, &min_path_len);
printf("The minimum path length: %d\n", min_path);
return 0;
}
```
在实现过程中,我遇到了以下几个困难:
1. 如何存储图的信息?
解决方法:使用邻接矩阵来存储图的边信息,使用数组来存储顶点信息。
2. 如何判断一个顶点是否已经被访问过?
解决方法:使用一个数组来标记每个顶点是否已经被访问过。
3. 如何输出所有路径?
解决方法:在深度优先遍历和广度优先遍历的过程中,记录下每个顶点的路径,找到终点后输出路径即可。
4. 如何输出最小路径?
解决方法:在深度优先遍历和广度优先遍历的过程中,记录下每个顶点的路径长度,找到终点后比较路径长度即可。
阅读全文