#define MAX_VERTEX_NUM 100 #include<stdio.h> typedef char DataType; typedef struct { DataType vertex[MAX_VERTEX_NUM]; // 顶点信息 int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int vertexNum, edgeNum; // 顶点数和边数 } MGraph; int visited[MAX_VERTEX_NUM]; // 全局数组visited[],初始化为0 // 创建无向图的邻接矩阵存储 void CreatGraph(MGraph *G, DataType a[], int n, int e) { int i, j, k; G->vertexNum = n; G->edgeNum = e; for (i = 0; i < G->vertexNum; i++) // 存储顶点信息 G->vertex[i] = a[i]; for (i = 0; i < G->vertexNum; i++) // 初始化邻接矩阵 for (j = 0; j < G->vertexNum; j++) G->edge[i][j] = 0; for (k = 0; k < G->edgeNum; k++) // 依次输入每一条边 { scanf("%d%d", &i, &j); // 输入边依附的顶点编号 G->edge[i][j] = 1; G->edge[j][i] = 1; // 置有边标志 } } // 深度优先遍历 void DFraverse(MGraph *G, int v) { printf("%c ", G->vertex[v]); visited[v] = 1; for (int j = 0; j < G->vertexNum; j++) if (G->edge[v][j] == 1 && visited[j] == 0) DFraverse(G, j); } int main() { MGraph G; DataType vertex[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}; int n = 8, e = 9; CreatGraph(&G, vertex, n, e); printf("深度优先遍历序列:"); for (int i = 0; i < G.vertexNum; i++) visited[i] = 0; for (int i = 0; i < G.vertexNum; i++) if (visited[i] == 0) DFraverse(&G, i); printf("\n"); return 0; }为上述代码添加广度优先遍历的功能
时间: 2024-01-24 21:02:06 浏览: 65
可以使用队列实现广度优先遍历。
以下是修改后的代码:
```c
#define MAX_VERTEX_NUM 100
#include<stdio.h>
#include<stdlib.h> // 添加头文件
typedef char DataType;
typedef struct {
DataType vertex[MAX_VERTEX_NUM]; // 顶点信息
int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵
int vertexNum, edgeNum; // 顶点数和边数
} MGraph;
int visited[MAX_VERTEX_NUM]; // 全局数组visited[],初始化为0
// 创建无向图的邻接矩阵存储
void CreatGraph(MGraph *G, DataType a[], int n, int e) {
int i, j, k;
G->vertexNum = n;
G->edgeNum = e;
for (i = 0; i < G->vertexNum; i++) // 存储顶点信息
G->vertex[i] = a[i];
for (i = 0; i < G->vertexNum; i++) // 初始化邻接矩阵
for (j = 0; j < G->vertexNum; j++)
G->edge[i][j] = 0;
for (k = 0; k < G->edgeNum; k++) // 依次输入每一条边
{
scanf("%d%d", &i, &j); // 输入边依附的顶点编号
G->edge[i][j] = 1;
G->edge[j][i] = 1; // 置有边标志
}
}
// 深度优先遍历
void DFraverse(MGraph *G, int v) {
printf("%c ", G->vertex[v]);
visited[v] = 1;
for (int j = 0; j < G->vertexNum; j++)
if (G->edge[v][j] == 1 && visited[j] == 0)
DFraverse(G, j);
}
// 广度优先遍历
void BFraverse(MGraph *G, int v) {
int queue[MAX_VERTEX_NUM]; // 定义队列
int front = 0, rear = 0; // 队头和队尾指针
printf("%c ", G->vertex[v]);
visited[v] = 1;
queue[rear++] = v; // 将顶点v入队
while (front != rear) {
int w = queue[front++]; // 将队头元素出队,并赋值给w
for (int j = 0; j < G->vertexNum; j++) {
if (G->edge[w][j] == 1 && visited[j] == 0) {
printf("%c ", G->vertex[j]);
visited[j] = 1;
queue[rear++] = j; // 将未访问的邻接点入队
}
}
}
}
int main() {
MGraph G;
DataType vertex[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
int n = 8, e = 9;
CreatGraph(&G, vertex, n, e);
printf("深度优先遍历序列:");
for (int i = 0; i < G.vertexNum; i++)
visited[i] = 0;
for (int i = 0; i < G.vertexNum; i++)
if (visited[i] == 0)
DFraverse(&G, i);
printf("\n");
printf("广度优先遍历序列:");
for (int i = 0; i < G.vertexNum; i++)
visited[i] = 0;
for (int i = 0; i < G.vertexNum; i++)
if (visited[i] == 0)
BFraverse(&G, i);
printf("\n");
return 0;
}
```
阅读全文